]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: zone list, setup changes
authorYehuda Sadeh <yehuda@inktank.com>
Thu, 21 Feb 2013 23:37:57 +0000 (15:37 -0800)
committerYehuda Sadeh <yehuda@inktank.com>
Wed, 8 May 2013 17:54:53 +0000 (10:54 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/rgw/rgw_admin.cc
src/rgw/rgw_rados.cc
src/rgw/rgw_rados.h

index 0874ce10ab876814d481c0a2519c10d54b90fa4f..84cb2a4a10bbce849f3c9bf139e495e14b2ec70e 100644 (file)
@@ -62,10 +62,12 @@ void _usage()
   cerr << "  object rm                  remove object\n";
   cerr << "  object unlink              unlink object from bucket index\n";
   cerr << "  region info                show region info\n";
-  cerr << "  region list                list all regions\n";
+  cerr << "  regions list               list all regions set on this cluster\n";
   cerr << "  region set                 set region info\n";
   cerr << "  region default             set default region\n";
-  cerr << "  zone info                  show zone params info\n";
+  cerr << "  zone info                  show zone cluster params\n";
+  cerr << "  zone set                   set zone cluster params\n";
+  cerr << "  zone list                  list all zones set on this cluster\n";
   cerr << "  pool add                   add an existing pool for data placement\n";
   cerr << "  pool rm                    remove an existing pool from data placement set\n";
   cerr << "  pools list                 list placement active set\n";
@@ -175,6 +177,7 @@ enum {
   OPT_REGION_DEFAULT,
   OPT_ZONE_INFO,
   OPT_ZONE_SET,
+  OPT_ZONE_LIST,
   OPT_CAPS_ADD,
   OPT_CAPS_RM,
 };
@@ -196,6 +199,7 @@ static int get_cmd(const char *cmd, const char *prev_cmd, bool *need_more)
       strcmp(cmd, "usage") == 0 ||
       strcmp(cmd, "user") == 0 ||
       strcmp(cmd, "region") == 0 ||
+      strcmp(cmd, "regions") == 0 ||
       strcmp(cmd, "zone") == 0) {
     *need_more = true;
     return 0;
@@ -275,6 +279,8 @@ static int get_cmd(const char *cmd, const char *prev_cmd, bool *need_more)
       return OPT_POOL_ADD;
     if (strcmp(cmd, "rm") == 0)
       return OPT_POOL_RM;
+    if (strcmp(cmd, "list") == 0)
+      return OPT_POOLS_LIST;
   } else if (strcmp(prev_cmd, "pools") == 0) {
     if (strcmp(cmd, "list") == 0)
       return OPT_POOLS_LIST;
@@ -292,11 +298,19 @@ static int get_cmd(const char *cmd, const char *prev_cmd, bool *need_more)
       return OPT_REGION_SET;
     if (strcmp(cmd, "default") == 0)
       return OPT_REGION_DEFAULT;
+  } else if (strcmp(prev_cmd, "regions") == 0) {
+    if (strcmp(cmd, "list") == 0)
+      return OPT_REGION_LIST;
   } else if (strcmp(prev_cmd, "zone") == 0) {
     if (strcmp(cmd, "info") == 0)
       return OPT_ZONE_INFO;
     if (strcmp(cmd, "set") == 0)
       return OPT_ZONE_SET;
+    if (strcmp(cmd, "list") == 0)
+      return OPT_ZONE_LIST;
+  } else if (strcmp(prev_cmd, "zones") == 0) {
+    if (strcmp(cmd, "list") == 0)
+      return OPT_ZONE_LIST;
   } else if (strcmp(prev_cmd, "gc") == 0) {
     if (strcmp(cmd, "list") == 0)
       return OPT_GC_LIST;
@@ -646,7 +660,9 @@ int main(int argc, char **argv)
   RGWStreamFlusher f(formatter, cout);
 
   bool raw_storage_op = (opt_cmd == OPT_REGION_INFO || opt_cmd == OPT_REGION_LIST ||
-                         opt_cmd == OPT_REGION_SET || opt_cmd == OPT_REGION_DEFAULT);
+                         opt_cmd == OPT_REGION_SET || opt_cmd == OPT_REGION_DEFAULT ||
+                         opt_cmd == OPT_ZONE_INFO || opt_cmd == OPT_ZONE_SET ||
+                         opt_cmd == OPT_ZONE_LIST);
 
 
   if (raw_storage_op) {
@@ -736,6 +752,57 @@ int main(int argc, char **argv)
       }
     }
 
+    if (opt_cmd == OPT_ZONE_INFO) {
+      RGWRegion region;
+      int ret = region.init(g_ceph_context, store);
+      if (ret < 0) {
+        cerr << "WARNING: failed to initialize region" << std::endl;
+      }
+      RGWZoneParams zone;
+      ret = zone.init(g_ceph_context, store, region);
+      if (ret < 0) {
+       cerr << "unable to initialize zone: " << cpp_strerror(-ret) << std::endl;
+       return -ret;
+      }
+      encode_json("zone", zone, formatter);
+      formatter->flush(cout);
+    }
+
+    if (opt_cmd == OPT_ZONE_SET) {
+      RGWRegion region;
+      int ret = region.init(g_ceph_context, store);
+      if (ret < 0) {
+        cerr << "WARNING: failed to initialize region" << std::endl;
+      }
+      RGWZoneParams zone;
+      zone.init_default();
+      ret = read_decode_json(infile, zone);
+      if (ret < 0) {
+        return 1;
+      }
+
+      ret = zone.store_info(g_ceph_context, store, region);
+      if (ret < 0) {
+        cerr << "ERROR: couldn't store zone info: " << cpp_strerror(-ret) << std::endl;
+        return 1;
+      }
+
+      encode_json("zone", zone, formatter);
+      formatter->flush(cout);
+    }
+    if (opt_cmd == OPT_ZONE_LIST) {
+      list<string> zones;
+      int ret = store->list_zones(zones);
+      if (ret < 0) {
+        cerr << "failed to list zones: " << cpp_strerror(-ret) << std::endl;
+       return -ret;
+      }
+      formatter->open_object_section("zones_list");
+      encode_json("zones", zones, formatter);
+      formatter->close_section();
+      formatter->flush(cout);
+      cout << std::endl;
+    }
     return 0;
   }
 
@@ -1279,29 +1346,6 @@ next:
     }
   }
 
-  if (opt_cmd == OPT_ZONE_INFO) {
-    encode_json("zone", store->zone, formatter);
-    formatter->flush(cout);
-  }
-
-  if (opt_cmd == OPT_ZONE_SET) {
-    RGWZoneParams zone;
-    zone.init_default();
-    int ret = read_decode_json(infile, zone);
-    if (ret < 0) {
-      return 1;
-    }
-
-    ret = zone.store_info(g_ceph_context, store);
-    if (ret < 0) {
-      cerr << "ERROR: couldn't store zone info: " << cpp_strerror(-ret) << std::endl;
-      return 1;
-    }
-
-    encode_json("zone", store->zone, formatter);
-    formatter->flush(cout);
-  }
-
   if (opt_cmd == OPT_USER_CHECK) {
     check_bad_user_bucket_mapping(store, user_id, fix);
   }
index 97db750ebe89ebdb7a9591ce34ef942bde92327d..545f43b23fd0639c0ba1f6677614963b5e7b1942 100644 (file)
@@ -48,8 +48,8 @@ static string dir_oid_prefix = ".dir.";
 static string default_storage_pool = ".rgw.buckets";
 static string avail_pools = ".pools.avail";
 
-static string zone_info_oid = "zone_info";
-static string region_info_oid_prefix = "region_info";
+static string zone_info_oid_prefix = "zone_info.";
+static string region_info_oid_prefix = "region_info.";
 
 static string default_region_info_oid = "default.region";
 
@@ -166,7 +166,7 @@ int RGWRegion::init(CephContext *_cct, RGWRados *_store, bool setup_region)
   rgw_bucket pool(pool_name.c_str());
   bufferlist bl;
 
-  string oid = region_info_oid_prefix + "." + name;
+  string oid = region_info_oid_prefix + name;
 
   int ret = rgw_get_obj(store, NULL, pool, oid, bl);
   if (ret < 0) {
@@ -196,7 +196,7 @@ int RGWRegion::create_default()
   RGWZoneParams zone_params;
   zone_params.name = zone_name;
   zone_params.init_default();
-  int r = zone_params.store_info(cct, store);
+  int r = zone_params.store_info(cct, store, *this);
   if (r < 0) {
     derr << "error storing zone params: " << cpp_strerror(-r) << dendl;
     return r;
@@ -217,7 +217,7 @@ int RGWRegion::store_info(bool exclusive)
 
   rgw_bucket pool(pool_name.c_str());
 
-  string oid = region_info_oid_prefix + "." + name;
+  string oid = region_info_oid_prefix + name;
 
   bufferlist bl;
   ::encode(*this, bl);
@@ -242,31 +242,39 @@ void RGWZoneParams::init_default()
   user_uid_pool = ".users.uid";
 }
 
-string RGWZoneParams::get_pool_name(CephContext *cct, const string& zone_name)
+string RGWZoneParams::get_pool_name(CephContext *cct)
 {
   string pool_name = cct->_conf->rgw_zone_root_pool;
   if (pool_name.empty()) {
     pool_name = RGW_DEFAULT_ZONE_ROOT_POOL;
-    if (!zone_name.empty()) {
-      pool_name.append("." + zone_name);
-    }
   }
   return pool_name;
 }
 
-int RGWZoneParams::init(CephContext *cct, RGWRados *store, bool create_zone)
+void RGWZoneParams::init_name(CephContext *cct, RGWRegion& region)
 {
   name = cct->_conf->rgw_zone;
-  string pool_name = get_pool_name(cct, name);
+
+  if (name.empty()) {
+    name = region.master_zone;
+
+    if (name.empty()) {
+      name = "default";
+    }
+  }
+}
+
+int RGWZoneParams::init(CephContext *cct, RGWRados *store, RGWRegion& region)
+{
+  init_name(cct, region);
+
+  string pool_name = get_pool_name(cct);
 
   rgw_bucket pool(pool_name.c_str());
   bufferlist bl;
 
-  int ret = rgw_get_obj(store, NULL, pool, zone_info_oid, bl);
-  if (ret == -ENOENT && create_zone) {
-    init_default();
-    return 0; // don't try to store obj, we're not fully initialized yet
-  }
+  string oid = zone_info_oid_prefix + name;
+  int ret = rgw_get_obj(store, NULL, pool, oid, bl);
   if (ret < 0)
     return ret;
 
@@ -274,22 +282,25 @@ int RGWZoneParams::init(CephContext *cct, RGWRados *store, bool create_zone)
     bufferlist::iterator iter = bl.begin();
     ::decode(*this, iter);
   } catch (buffer::error& err) {
-    ldout(cct, 0) << "ERROR: failed to decode zone info from " << pool << ":" << zone_info_oid << dendl;
+    ldout(cct, 0) << "ERROR: failed to decode zone info from " << pool << ":" << oid << dendl;
     return -EIO;
   }
 
   return 0;
 }
 
-int RGWZoneParams::store_info(CephContext *cct, RGWRados *store)
+int RGWZoneParams::store_info(CephContext *cct, RGWRados *store, RGWRegion& region)
 {
-  string pool_name = get_pool_name(cct, name);
+  init_name(cct, region);
+
+  string pool_name = get_pool_name(cct);
 
   rgw_bucket pool(pool_name.c_str());
+  string oid = zone_info_oid_prefix + name;
 
   bufferlist bl;
   ::encode(*this, bl);
-  int ret = rgw_put_system_obj(store, pool, zone_info_oid, bl.c_str(), bl.length(), false, NULL);
+  int ret = rgw_put_system_obj(store, pool, oid, bl.c_str(), bl.length(), false, NULL);
 
   return ret;
 }
@@ -386,7 +397,7 @@ int RGWRados::init_complete()
   if (ret < 0)
     return ret;
 
-  ret = zone.init(cct, this, create_zone);
+  ret = zone.init(cct, this, region);
   if (ret < 0)
     return ret;
 
@@ -444,29 +455,43 @@ void RGWRados::finalize_watch()
   delete[] watchers;
 }
 
-int RGWRados::list_regions(list<string>& regions)
+int RGWRados::list_raw_prefixed_objs(string pool_name, const string& prefix, list<string>& result)
 {
-  string pool_name = RGWRegion::get_pool_name(cct);
-
   rgw_bucket pool(pool_name.c_str());
   bool is_truncated;
   RGWListRawObjsCtx ctx;
   do {
     vector<string> oids;
-    int r = list_raw_objects(pool, region_info_oid_prefix, 1000,
+    int r = list_raw_objects(pool, prefix, 1000,
                             ctx, oids, &is_truncated);
     if (r < 0) {
       return r;
     }
     vector<string>::iterator iter;
     for (iter = oids.begin(); iter != oids.end(); ++iter) {
-      regions.push_back(iter->substr(region_info_oid_prefix.size() + 1));
+      string& val = *iter;
+      if (val.size() > prefix.size())
+        result.push_back(val.substr(prefix.size()));
     }
   } while (is_truncated);
 
   return 0;
 }
 
+int RGWRados::list_regions(list<string>& regions)
+{
+  string pool_name = RGWRegion::get_pool_name(cct);
+
+  return list_raw_prefixed_objs(pool_name, region_info_oid_prefix, regions);
+}
+
+int RGWRados::list_zones(list<string>& zones)
+{
+  string pool_name = RGWZoneParams::get_pool_name(cct);
+
+  return list_raw_prefixed_objs(pool_name, zone_info_oid_prefix, zones);
+}
+
 /**
  * Open the pool used as root for this gateway
  * Returns: 0 on success, -ERR# otherwise.
index f31d8c73174e1fd274f1b930a86df91a031c4956..9d215908a7e29545135920c07860ab119ba93716 100644 (file)
@@ -246,6 +246,8 @@ struct RGWListRawObjsCtx {
   RGWListRawObjsCtx() : initialized(false) {}
 };
 
+struct RGWRegion;
+
 struct RGWZoneParams {
   rgw_bucket domain_root;
   rgw_bucket control_pool;
@@ -261,10 +263,11 @@ struct RGWZoneParams {
 
   string name;
 
-  string get_pool_name(CephContext *cct, const string& zone_name);
-  int init(CephContext *cct, RGWRados *store, bool create_zone);
+  static string get_pool_name(CephContext *cct);
+  void init_name(CephContext *cct, RGWRegion& region);
+  int init(CephContext *cct, RGWRados *store, RGWRegion& region);
   void init_default();
-  int store_info(CephContext *cct, RGWRados *store);
+  int store_info(CephContext *cct, RGWRados *store, RGWRegion& region);
 
   void encode(bufferlist& bl) const {
     ENCODE_START(2, 1, bl);
@@ -505,28 +508,24 @@ protected:
   string region_name;
   string zone_name;
 
-  bool create_zone;
-
 public:
   RGWRados() : lock("rados_timer_lock"), timer(NULL),
                gc(NULL), use_gc_thread(false),
                num_watchers(0), watchers(NULL), watch_handles(NULL),
                bucket_id_lock("rados_bucket_id"), max_bucket_id(0),
                cct(NULL), rados(NULL),
-               pools_initialized(false),
-              create_zone(false) {}
+               pools_initialized(false) {}
 
   void set_context(CephContext *_cct) {
     cct = _cct;
   }
 
-  void set_region(const string& name, bool create) {
+  void set_region(const string& name) {
     region_name = name;
   }
 
-  void set_zone(const string& name, bool create) {
+  void set_zone(const string& name) {
     zone_name = name;
-    create_zone = create;
   }
 
   RGWRegion region;
@@ -539,7 +538,9 @@ public:
     }
   }
 
+  int list_raw_prefixed_objs(string pool_name, const string& prefix, list<string>& result);
   int list_regions(list<string>& regions);
+  int list_zones(list<string>& zones);
 
   void tick();