]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/test/librados/test_common.cc
update sources to 12.2.7
[ceph.git] / ceph / src / test / librados / test_common.cc
index c276d33ff7973688b97daeff0485ec39e8e8e9a2..2d4185289f2f2b4f27791b5b124ed3ab1da5692c 100644 (file)
@@ -101,34 +101,67 @@ int rados_pool_set(
   return ret;
 }
 
-}
-
-std::string set_pg_num(
-    rados_t *cluster, const std::string &pool_name, uint32_t pg_num)
-{
-  // Wait for 'creating' to clear
-  int r = wait_for_healthy(cluster);
-  if (r != 0) {
-    goto err;
+struct pool_op_error : std::exception {
+  string msg;
+  pool_op_error(const std::string& pool_name,
+               const std::string& func_name,
+               int err) {
+    std::ostringstream oss;
+    oss << func_name << "(" << pool_name << ") failed with error " << err;
+    msg = oss.str();
   }
-
-  // Adjust pg_num
-  r = rados_pool_set(cluster, pool_name, "pg_num", stringify(pg_num));
-  if (r != 0) {
-    goto err;
+  const char* what() const noexcept override {
+    return msg.c_str();
   }
+};
 
-  // Wait for 'creating' to clear
-  r = wait_for_healthy(cluster);
-  if (r != 0) {
-    goto err;
+template<typename Func>
+std::string with_healthy_cluster(rados_t* cluster,
+                                const std::string& pool_name,
+                                Func&& func)
+{
+  try {
+    // Wait for 'creating/backfilling' to clear
+    int r = wait_for_healthy(cluster);
+    if (r != 0) {
+      throw pool_op_error{pool_name, "wait_for_healthy", r};
+    }
+    func();
+    // Wait for 'creating/backfilling' to clear
+    r = wait_for_healthy(cluster);
+    if (r != 0) {
+      throw pool_op_error{pool_name, "wait_for_healthy", r};
+    }
+  } catch (const pool_op_error& e) {
+    rados_shutdown(*cluster);
+    return e.what();
   }
-
   return "";
+}
+}
+
+std::string set_pg_num(
+    rados_t *cluster, const std::string &pool_name, uint32_t pg_num)
+{
+  return with_healthy_cluster(cluster, pool_name, [&] {
+    // Adjust pg_num
+      int r = rados_pool_set(cluster, pool_name, "pg_num",
+                            stringify(pg_num));
+      if (r != 0) {
+       throw pool_op_error{pool_name, "set_pg_num", r};
+      }
+  });
+}
 
-err:
-  rados_shutdown(*cluster);
-  std::ostringstream oss;
-  oss << __func__ << "(" << pool_name << ") failed with error " << r;
-  return oss.str();
+std::string set_pgp_num(
+    rados_t *cluster, const std::string &pool_name, uint32_t pgp_num)
+{
+  return with_healthy_cluster(cluster, pool_name, [&] {
+    // Adjust pgp_num
+    int r = rados_pool_set(cluster, pool_name, "pgp_num",
+                          stringify(pgp_num));
+    if (r != 0) {
+      throw pool_op_error{pool_name, "set_pgp_num", r};
+    }
+  });
 }