From 6a72fd4bb4cd7d95b73dea462ce22963f8ba14e4 Mon Sep 17 00:00:00 2001 From: Aleksandra Martyniuk Date: Tue, 28 Oct 2025 15:30:58 +0100 Subject: [PATCH] api: storage_service: tasks: force_keyspace_cleanup Currently, all apis that start a compaction have two versions: synchronous and asynchronous. They share most of the implementation, but some checks and params have diverged. Unify the handlers of /storage_service/keyspace_cleanup/{keyspace} and /tasks/compaction/keyspace_cleanup/{keyspace}. (cherry picked from commit 044b001bb40b8fb95abc59e5747b305a37b859d0) --- api/storage_service.cc | 21 +++------------------ api/tasks.cc | 38 ++++++++++++++++++++++++++------------ api/tasks.hh | 1 + 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/api/storage_service.cc b/api/storage_service.cc index 2396a7cab4..1516fc52b6 100644 --- a/api/storage_service.cc +++ b/api/storage_service.cc @@ -765,25 +765,10 @@ rest_force_keyspace_compaction(http_context& ctx, std::unique_ptr static future rest_force_keyspace_cleanup(http_context& ctx, sharded& ss, std::unique_ptr req) { - auto& db = ctx.db; - auto [keyspace, table_infos] = parse_table_infos(ctx, *req); - const auto& rs = db.local().find_keyspace(keyspace).get_replication_strategy(); - if (rs.get_type() == locator::replication_strategy_type::local || !rs.is_vnode_based()) { - auto reason = rs.get_type() == locator::replication_strategy_type::local ? "require" : "support"; - apilog.info("Keyspace {} does not {} cleanup", keyspace, reason); - co_return json::json_return_type(0); + auto task = co_await force_keyspace_cleanup(ctx, ss, std::move(req)); + if (task) { + co_await task->done(); } - apilog.info("force_keyspace_cleanup: keyspace={} tables={}", keyspace, table_infos); - if (!co_await ss.local().is_cleanup_allowed(keyspace)) { - auto msg = "Can not perform cleanup operation when topology changes"; - apilog.warn("force_keyspace_cleanup: keyspace={} tables={}: {}", keyspace, table_infos, msg); - co_await coroutine::return_exception(std::runtime_error(msg)); - } - - auto& compaction_module = db.local().get_compaction_manager().get_task_manager_module(); - auto task = co_await compaction_module.make_and_start_task( - {}, std::move(keyspace), db, table_infos, flush_mode::all_tables, tasks::is_user_task::yes); - co_await task->done(); co_return json::json_return_type(0); } diff --git a/api/tasks.cc b/api/tasks.cc index 8bc219ed70..e67e4716ba 100644 --- a/api/tasks.cc +++ b/api/tasks.cc @@ -51,6 +51,27 @@ future force_keyspace_compaction(http_context& ct return compaction_module.make_and_start_task({}, std::move(keyspace), tasks::task_id::create_null_id(), db, table_infos, fmopt, consider_only_existing_data); } +future force_keyspace_cleanup(http_context& ctx, sharded& ss, std::unique_ptr req) { + auto& db = ctx.db; + auto [keyspace, table_infos] = parse_table_infos(ctx, *req); + const auto& rs = db.local().find_keyspace(keyspace).get_replication_strategy(); + if (rs.get_type() == locator::replication_strategy_type::local || !rs.is_vnode_based()) { + auto reason = rs.get_type() == locator::replication_strategy_type::local ? "require" : "support"; + apilog.info("Keyspace {} does not {} cleanup", keyspace, reason); + co_return nullptr; + } + apilog.info("force_keyspace_cleanup: keyspace={} tables={}", keyspace, table_infos); + if (!co_await ss.local().is_cleanup_allowed(keyspace)) { + auto msg = "Can not perform cleanup operation when topology changes"; + apilog.warn("force_keyspace_cleanup: keyspace={} tables={}: {}", keyspace, table_infos, msg); + co_await coroutine::return_exception(std::runtime_error(msg)); + } + + auto& compaction_module = db.local().get_compaction_manager().get_task_manager_module(); + co_return co_await compaction_module.make_and_start_task( + {}, std::move(keyspace), db, table_infos, compaction::flush_mode::all_tables, tasks::is_user_task::yes); +} + void set_tasks_compaction_module(http_context& ctx, routes& r, sharded& ss, sharded& snap_ctl) { t::force_keyspace_compaction_async.set(r, [&ctx](std::unique_ptr req) -> future { auto task = co_await force_keyspace_compaction(ctx, std::move(req)); @@ -58,19 +79,12 @@ void set_tasks_compaction_module(http_context& ctx, routes& r, sharded req) -> future { - auto& db = ctx.db; - auto [keyspace, table_infos] = parse_table_infos(ctx, *req); - apilog.info("force_keyspace_cleanup_async: keyspace={} tables={}", keyspace, table_infos); - if (!co_await ss.local().is_cleanup_allowed(keyspace)) { - auto msg = "Can not perform cleanup operation when topology changes"; - apilog.warn("force_keyspace_cleanup_async: keyspace={} tables={}: {}", keyspace, table_infos, msg); - co_await coroutine::return_exception(std::runtime_error(msg)); + tasks::task_id id = tasks::task_id::create_null_id(); + auto task = co_await force_keyspace_cleanup(ctx, ss, std::move(req)); + if (task) { + id = task->get_status().id; } - - auto& compaction_module = db.local().get_compaction_manager().get_task_manager_module(); - auto task = co_await compaction_module.make_and_start_task({}, std::move(keyspace), db, table_infos, flush_mode::all_tables, tasks::is_user_task::yes); - - co_return json::json_return_type(task->get_status().id.to_sstring()); + co_return json::json_return_type(id.to_sstring()); }); t::perform_keyspace_offstrategy_compaction_async.set(r, wrap_ks_cf(ctx, [] (http_context& ctx, std::unique_ptr req, sstring keyspace, std::vector table_infos) -> future { diff --git a/api/tasks.hh b/api/tasks.hh index cb55489840..149c86ec32 100644 --- a/api/tasks.hh +++ b/api/tasks.hh @@ -30,5 +30,6 @@ void set_tasks_compaction_module(http_context& ctx, httpd::routes& r, sharded force_keyspace_compaction(http_context& ctx, std::unique_ptr req); +future force_keyspace_cleanup(http_context& ctx, sharded& ss, std::unique_ptr req); }