-
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 1 commit
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 | ||||
---|---|---|---|---|---|---|
|
@@ -3555,7 +3555,7 @@ void ServerFamily::StopAllClusterReplicas() { | |||||
} | ||||||
|
||||||
void ServerFamily::ReplicaOf(CmdArgList args, const CommandContext& cmd_cntx) { | ||||||
ReplicaOfInternal(args, cmd_cntx.tx, cmd_cntx.rb, ActionOnConnectionFail::kReturnOnError); | ||||||
ReplicaOfInternalV2(args, cmd_cntx.tx, cmd_cntx.rb, ActionOnConnectionFail::kReturnOnError); | ||||||
} | ||||||
|
||||||
void ServerFamily::Replicate(string_view host, string_view port) { | ||||||
|
@@ -3568,7 +3568,101 @@ void ServerFamily::Replicate(string_view host, string_view port) { | |||||
CmdArgList args_list = absl::MakeSpan(args_vec); | ||||||
io::NullSink sink; | ||||||
facade::RedisReplyBuilder rb(&sink); | ||||||
ReplicaOfInternal(args_list, nullptr, &rb, ActionOnConnectionFail::kContinueReplication); | ||||||
ReplicaOfInternalV2(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); | ||||||
// TODO we should not allow partial sync after NO-ONE. Only after Takeover. | ||||||
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{}; | ||||||
|
GenericError ec{}; | |
GenericError ec; |
romange marked this conversation as resolved.
Show resolved
Hide resolved
romange marked this conversation as resolved.
Show resolved
Hide resolved
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