Improve restore replication role (#1089)

This commit is contained in:
Andi 2023-08-01 21:51:52 +02:00 committed by GitHub
parent 514fed51c4
commit f0bac53e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1973,28 +1973,26 @@ void InMemoryStorage::RestoreReplicationRole() {
}
spdlog::info("Restoring replication role.");
uint16_t port = replication::kDefaultReplicationPort;
for (const auto &[replica_name, replica_data] : *storage_) {
const auto maybe_replica_status = replication::JSONToReplicationStatus(nlohmann::json::parse(replica_data));
if (!maybe_replica_status.has_value()) {
LOG_FATAL("Cannot parse previously saved configuration of replica {}.", replica_name);
}
if (replica_name != replication::kReservedReplicationRoleName) {
continue;
}
const auto replication_data = storage_->Get(replication::kReservedReplicationRoleName);
if (!replication_data.has_value()) {
spdlog::debug("Cannot find data needed for restore replication role in persisted metadata.");
return;
}
auto replica_status = *maybe_replica_status;
const auto maybe_replication_status = replication::JSONToReplicationStatus(nlohmann::json::parse(*replication_data));
if (!maybe_replication_status.has_value()) {
LOG_FATAL("Cannot parse previously saved configuration of replication role {}.",
replication::kReservedReplicationRoleName);
}
if (!replica_status.role.has_value()) {
replication_role_.store(replication::ReplicationRole::MAIN);
} else {
replication_role_.store(*replica_status.role);
port = replica_status.port;
}
break;
const auto replication_status = *maybe_replication_status;
if (!replication_status.role.has_value()) {
replication_role_.store(replication::ReplicationRole::MAIN);
} else {
replication_role_.store(*replication_status.role);
port = replication_status.port;
}
if (replication_role_ == replication::ReplicationRole::REPLICA) {