From fb024536861371e61ba8e694834dfe586d71d66a Mon Sep 17 00:00:00 2001 From: Piotr Dulikowski Date: Mon, 5 Feb 2024 12:28:04 +0100 Subject: [PATCH] system_keyspace: add read_cdc_generation_opt The `system_keyspace::read_cdc_generation` loads a cdc generation from the system tables. One of its preconditions is that the generation exists - this precondition is quite easy to satisfy in raft mode, and the function was designed to be used solely in that mode. In legacy mode however, in case when we revert from raft mode through recovery, it might be necessary to use generations created in raft mode for some time. In order to make the function useful as a fallback in case lookup of a generation in legacy mode fails, introduce a relaxed variant of `read_cdc_generation` which returns std::nullopt if the generation does not exist. --- db/system_keyspace.cc | 16 +++++++++++++--- db/system_keyspace.hh | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/db/system_keyspace.cc b/db/system_keyspace.cc index f34ad84617..5bfbc141d9 100644 --- a/db/system_keyspace.cc +++ b/db/system_keyspace.cc @@ -2948,6 +2948,18 @@ std::optional system_keyspace::decode_topology_featu future system_keyspace::read_cdc_generation(utils::UUID id) { + auto gen_desc = co_await read_cdc_generation_opt(id); + + if (!gen_desc) { + on_internal_error(slogger, format( + "read_cdc_generation: data for CDC generation {} not present", id)); + } + + co_return std::move(*gen_desc); +} + +future> +system_keyspace::read_cdc_generation_opt(utils::UUID id) { utils::chunked_vector entries; co_await _qp.query_internal( format("SELECT range_end, streams, ignore_msb FROM {}.{} WHERE key = '{}' AND id = ?", @@ -2966,9 +2978,7 @@ system_keyspace::read_cdc_generation(utils::UUID id) { }); if (entries.empty()) { - // The data must be present by precondition. - on_internal_error(slogger, format( - "read_cdc_generation: data for CDC generation {} not present", id)); + co_return std::nullopt; } co_return cdc::topology_description{std::move(entries)}; diff --git a/db/system_keyspace.hh b/db/system_keyspace.hh index aad5ea5cb5..ea5307ed8e 100644 --- a/db/system_keyspace.hh +++ b/db/system_keyspace.hh @@ -529,6 +529,13 @@ public: // Precondition: the data is known to be present in the table (because it was committed earlier through group 0). future read_cdc_generation(utils::UUID id); + // Read CDC generation data with the given UUID as key. + // Unlike `read_cdc_generation`, does not require the data to be present. + // This method is meant to be used after switching back to legacy mode due to raft recovery, + // as the node will need to fetch definition of a CDC generation that was + // previously created in raft topology mode. + future> read_cdc_generation_opt(utils::UUID id); + // Loads the current clean-up candidate for the CDC generation data. If there is no candidate, returns std::nullopt. future> get_cdc_generations_cleanup_candidate();