-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore: new ReplicaOf algorithm #5774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+123
−19
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,7 @@ ABSL_DECLARE_FLAG(string, tls_ca_cert_file); | |
ABSL_DECLARE_FLAG(string, tls_ca_cert_dir); | ||
ABSL_DECLARE_FLAG(int, replica_priority); | ||
ABSL_DECLARE_FLAG(double, rss_oom_deny_ratio); | ||
ABSL_DECLARE_FLAG(bool, experimental_replicaof_v2); | ||
|
||
bool AbslParseFlag(std::string_view in, ReplicaOfFlag* flag, std::string* err) { | ||
#define RETURN_ON_ERROR(cond, m) \ | ||
|
@@ -3555,6 +3556,11 @@ void ServerFamily::StopAllClusterReplicas() { | |
} | ||
|
||
void ServerFamily::ReplicaOf(CmdArgList args, const CommandContext& cmd_cntx) { | ||
const bool use_replica_of_v2 = absl::GetFlag(FLAGS_experimental_replicaof_v2); | ||
if (use_replica_of_v2) { | ||
ReplicaOfInternalV2(args, cmd_cntx.tx, cmd_cntx.rb, ActionOnConnectionFail::kReturnOnError); | ||
return; | ||
} | ||
ReplicaOfInternal(args, cmd_cntx.tx, cmd_cntx.rb, ActionOnConnectionFail::kReturnOnError); | ||
} | ||
|
||
|
@@ -3568,9 +3574,109 @@ void ServerFamily::Replicate(string_view host, string_view port) { | |
CmdArgList args_list = absl::MakeSpan(args_vec); | ||
io::NullSink sink; | ||
facade::RedisReplyBuilder rb(&sink); | ||
const bool use_replica_of_v2 = absl::GetFlag(FLAGS_experimental_replicaof_v2); | ||
if (use_replica_of_v2) { | ||
ReplicaOfInternalV2(args_list, nullptr, &rb, ActionOnConnectionFail::kContinueReplication); | ||
return; | ||
} | ||
ReplicaOfInternal(args_list, nullptr, &rb, ActionOnConnectionFail::kContinueReplication); | ||
} | ||
|
||
void ServerFamily::ReplicaOfNoOne(SinkReplyBuilder* builder) { | ||
romange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
util::fb2::LockGuard lk(replicaof_mu_); | ||
ServerState* ss = ServerState::tlocal(); | ||
|
||
if (!ss->is_master) { | ||
CHECK(replica_); | ||
|
||
// flip flag before clearing replica_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: please keep an empty line before comment lines. here and everywhere. |
||
SetMasterFlagOnAllThreads(true); | ||
|
||
last_master_data_ = replica_->Stop(); | ||
replica_.reset(); | ||
StopAllClusterReplicas(); | ||
} | ||
|
||
// May not switch to ACTIVE if the process is, for example, shutting down at the same time. | ||
service_.SwitchState(GlobalState::LOADING, GlobalState::ACTIVE); | ||
|
||
return builder->SendOk(); | ||
} | ||
|
||
void ServerFamily::ReplicaOfInternalV2(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder, | ||
ActionOnConnectionFail on_error) | ||
ABSL_LOCKS_EXCLUDED(replicaof_mu_) { | ||
auto replicaof_args = ReplicaOfArgs::FromCmdArgs(args, builder); | ||
if (!replicaof_args.has_value()) { | ||
return; | ||
} | ||
|
||
LOG(INFO) << "Initiate replication with: " << *replicaof_args; | ||
// This is a "weak" check. For example, if the node is already a replica, | ||
// it could be the case that one of the flows disconnects. The MainReplicationFiber | ||
// will then loop and if it can't partial sync it will enter LOADING state because of | ||
// full sync. Note that the fiber is not aware of the replicaof_mu_ so even | ||
// if that mutex is locked below before any state check we can't really enforce | ||
// that the old replication fiber won't try to full sync and update the state to LOADING. | ||
// What is more here is that we always call `replica->Stop()`. So even if we end up in the | ||
// scenario described, the semantics are well defined. First, cancel the old replica and | ||
// move on with the new one. Cancelation will be slower and ReplicaOf() will | ||
// induce higher latency -- but that's ok because it's an highly improbable flow with | ||
// well defined semantics. | ||
ServerState* ss = ServerState::tlocal(); | ||
|
||
if (ss->is_master && ss->gstate() == GlobalState::LOADING) { | ||
romange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builder->SendError(kLoadingErr); | ||
return; | ||
} | ||
|
||
// replicaof no one | ||
if (replicaof_args->IsReplicaOfNoOne()) { | ||
return ReplicaOfNoOne(builder); | ||
} | ||
|
||
auto new_replica = make_shared<Replica>(replicaof_args->host, replicaof_args->port, &service_, | ||
master_replid(), replicaof_args->slot_range); | ||
GenericError ec; | ||
switch (on_error) { | ||
case ActionOnConnectionFail::kReturnOnError: | ||
ec = new_replica->Start(); | ||
romange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
case ActionOnConnectionFail::kContinueReplication: | ||
new_replica->EnableReplication(); | ||
break; | ||
}; | ||
|
||
if (ec || new_replica->IsContextCancelled()) { | ||
return builder->SendError(ec ? ec.Format() : "replication cancelled"); | ||
} | ||
|
||
// Critical section. | ||
// 1. Stop the old replica_ if it exists | ||
// 2. Update all the pointers to the new replica and update master flag | ||
// 3. Start the main replication fiber | ||
// 4. Send OK | ||
util::fb2::LockGuard lk(replicaof_mu_); | ||
std::optional<Replica::LastMasterSyncData> last_master_data; | ||
if (replica_) | ||
last_master_data = replica_->Stop(); | ||
|
||
StopAllClusterReplicas(); | ||
|
||
if (ServerState::tlocal()->gstate() == GlobalState::TAKEN_OVER) | ||
service_.SwitchState(GlobalState::TAKEN_OVER, GlobalState::LOADING); | ||
|
||
// TODO Update thread locals. That way INFO never blocks | ||
replica_ = new_replica; | ||
SetMasterFlagOnAllThreads(false); | ||
|
||
if (on_error == ActionOnConnectionFail::kReturnOnError) { | ||
replica_->StartMainReplicationFiber(last_master_data); | ||
} | ||
|
||
builder->SendOk(); | ||
} | ||
|
||
// REPLTAKEOVER <seconds> [SAVE] | ||
// SAVE is used only by tests. | ||
void ServerFamily::ReplTakeOver(CmdArgList args, const CommandContext& cmd_cntx) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that changes are grouped together nicely. Let's introduce the flag
experimental_replicaof_v2
set to true so we could have the fallback to the old variant