Skip to content

Commit

Permalink
system_keyspace: persistence for Raft Group 0 id and Raft Server Id
Browse files Browse the repository at this point in the history
Implement system_keyspace helpers to persist Raft Group 0 id
and Raft Server id.

Do not use coroutines in a template function to work around
https://bugs.llvm.org/show_bug.cgi?id=50345
  • Loading branch information
kostja committed Nov 25, 2021
1 parent 65e5499 commit fd29585
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
50 changes: 41 additions & 9 deletions db/system_keyspace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1595,19 +1595,33 @@ template future<> system_keyspace::update_peer_info<sstring>(gms::inet_address e
template future<> system_keyspace::update_peer_info<utils::UUID>(gms::inet_address ep, sstring column_name, utils::UUID);
template future<> system_keyspace::update_peer_info<net::inet_address>(gms::inet_address ep, sstring column_name, net::inet_address);

template <typename T>
future<> set_scylla_local_param_as(const sstring& key, const T& value) {
sstring req = format("UPDATE system.{} SET value = ? WHERE key = ?", system_keyspace::SCYLLA_LOCAL);
auto type = data_type_for<T>();
return qctx->execute_cql(req, type->to_string_impl(data_value(value)), key).discard_result();
}

template <typename T>
future<std::optional<T>> get_scylla_local_param_as(const sstring& key) {
sstring req = format("SELECT value FROM system.{} WHERE key = ?", system_keyspace::SCYLLA_LOCAL);
return qctx->execute_cql(req, key).then([] (::shared_ptr<cql3::untyped_result_set> res)
-> future<std::optional<T>> {
if (res->empty() || !res->one().has("value")) {
return make_ready_future<std::optional<T>>(std::optional<T>());
}
auto type = data_type_for<T>();
return make_ready_future<std::optional<T>>(value_cast<T>(type->deserialize(
type->from_string(res->one().get_as<sstring>("value")))));
});
}

future<> system_keyspace::set_scylla_local_param(const sstring& key, const sstring& value) {
sstring req = format("UPDATE system.{} SET value = ? WHERE key = ?", SCYLLA_LOCAL);
return qctx->execute_cql(req, value, key).discard_result();
return set_scylla_local_param_as<sstring>(key, value);
}

future<std::optional<sstring>> system_keyspace::get_scylla_local_param(const sstring& key){
sstring req = format("SELECT value FROM system.{} WHERE key = ?", SCYLLA_LOCAL);
return qctx->execute_cql(req, key).then([] (::shared_ptr<cql3::untyped_result_set> res) {
if (res->empty() || !res->one().has("value")) {
return std::optional<sstring>();
}
return std::optional<sstring>(res->one().get_as<sstring>("value"));
});
return get_scylla_local_param_as<sstring>(key);
}

future<> system_keyspace::update_schema_version(utils::UUID version) {
Expand Down Expand Up @@ -2932,6 +2946,24 @@ future<> system_keyspace::delete_paxos_decision(const schema& s, const partition
).discard_result();
}

future<utils::UUID> system_keyspace::get_raft_group0_id() {
auto opt = co_await get_scylla_local_param_as<utils::UUID>("raft_group0_id");
co_return opt.value_or<utils::UUID>({});
}

future<utils::UUID> system_keyspace::get_raft_server_id() {
auto opt = co_await get_scylla_local_param_as<utils::UUID>("raft_server_id");
co_return opt.value_or<utils::UUID>({});
}

future<> system_keyspace::set_raft_group0_id(utils::UUID uuid) {
return set_scylla_local_param_as<utils::UUID>("raft_group0_id", uuid);
}

future<> system_keyspace::set_raft_server_id(utils::UUID uuid) {
return set_scylla_local_param_as<utils::UUID>("raft_server_id", uuid);
}

sstring system_keyspace_name() {
return system_keyspace::NAME;
}
Expand Down
12 changes: 12 additions & 0 deletions db/system_keyspace.hh
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,18 @@ public:
static future<bool> cdc_is_rewritten();
static future<> cdc_set_rewritten(std::optional<cdc::generation_id_v1>);

// Load Raft Group 0 id from scylla.local
static future<utils::UUID> get_raft_group0_id();

// Load this server id from scylla.local
static future<utils::UUID> get_raft_server_id();

// Persist Raft Group 0 id. Should be a TIMEUUID.
static future<> set_raft_group0_id(utils::UUID id);

// Called once at fresh server startup to make sure every server
// has a Raft ID
static future<> set_raft_server_id(utils::UUID id);
}; // class system_keyspace

future<> system_keyspace_make(distributed<database>& db, distributed<service::storage_service>& ss);
Expand Down

0 comments on commit fd29585

Please sign in to comment.