-
Notifications
You must be signed in to change notification settings - Fork 468
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
Support blocking migration for the cluster migrate command #1418
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d32acb9
support sync migration
ellutionist ab22913
add license header
ellutionist 35c0592
use unique_ptr instead of shared_ptr to hold `SyncMigrateContext`
ellutionist 0989076
Improve the command args parsing
ellutionist 990f7dd
Merge branch 'unstable' into sync_migrate_slot
PragmaTwice 2c74650
Format Code
ellutionist 383dcfe
Adapt to the recent changes && add more go test cases
ellutionist a564974
Simplify code && rename
ellutionist 4711829
Merge branch 'unstable' into sync_migrate_slot
PragmaTwice 5eab45e
initialize pointer of ctx with `nullptr`
ellutionist a99f9e5
Merge branch 'unstable' into sync_migrate_slot
git-hulk 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#include "cluster/sync_migrate_context.h" | ||
|
||
SyncMigrateContext::~SyncMigrateContext() { | ||
if (timer_) { | ||
event_free(timer_); | ||
timer_ = nullptr; | ||
} | ||
} | ||
|
||
void SyncMigrateContext::StartBlock() { | ||
auto bev = conn_->GetBufferEvent(); | ||
bufferevent_setcb(bev, nullptr, WriteCB, EventCB, this); | ||
|
||
if (timeout_) { | ||
timer_ = evtimer_new(bufferevent_get_base(bev), TimerCB, this); | ||
timeval tm = {timeout_, 0}; | ||
evtimer_add(timer_, &tm); | ||
} | ||
} | ||
|
||
void SyncMigrateContext::Wakeup(const Status &migrate_result) { | ||
migrate_result_ = migrate_result; | ||
auto s = conn_->Owner()->EnableWriteEvent(conn_->GetFD()); | ||
if (!s.IsOK()) { | ||
LOG(ERROR) << "[server] Failed to enable write event on the sync migrate connection " << conn_->GetFD() << ": " | ||
<< s.Msg(); | ||
} | ||
} | ||
|
||
void SyncMigrateContext::EventCB(bufferevent *bev, int16_t events, void *ctx) { | ||
auto self = reinterpret_cast<SyncMigrateContext *>(ctx); | ||
auto &&slot_migrator = self->svr_->slot_migrator; | ||
|
||
if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) { | ||
if (self->timer_ != nullptr) { | ||
event_free(self->timer_); | ||
self->timer_ = nullptr; | ||
} | ||
|
||
slot_migrator->CancelBlocking(); | ||
} | ||
redis::Connection::OnEvent(bev, events, self->conn_); | ||
} | ||
|
||
void SyncMigrateContext::TimerCB(int, int16_t events, void *ctx) { | ||
auto self = reinterpret_cast<SyncMigrateContext *>(ctx); | ||
auto &&slot_migrator = self->svr_->slot_migrator; | ||
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. ditto |
||
|
||
self->conn_->Reply(redis::NilString()); | ||
event_free(self->timer_); | ||
self->timer_ = nullptr; | ||
|
||
slot_migrator->CancelBlocking(); | ||
|
||
auto bev = self->conn_->GetBufferEvent(); | ||
bufferevent_setcb(bev, redis::Connection::OnRead, redis::Connection::OnWrite, redis::Connection::OnEvent, | ||
self->conn_); | ||
bufferevent_enable(bev, EV_READ); | ||
} | ||
|
||
void SyncMigrateContext::WriteCB(bufferevent *bev, void *ctx) { | ||
auto self = reinterpret_cast<SyncMigrateContext *>(ctx); | ||
|
||
if (self->migrate_result_) { | ||
self->conn_->Reply(redis::SimpleString("OK")); | ||
} else { | ||
self->conn_->Reply(redis::Error("ERR " + self->migrate_result_.Msg())); | ||
} | ||
|
||
if (self->timer_) { | ||
event_free(self->timer_); | ||
self->timer_ = nullptr; | ||
} | ||
|
||
bufferevent_setcb(bev, redis::Connection::OnRead, redis::Connection::OnWrite, redis::Connection::OnEvent, | ||
self->conn_); | ||
bufferevent_enable(bev, EV_READ); | ||
|
||
bufferevent_trigger(bev, EV_READ, BEV_TRIG_IGNORE_WATERMARKS); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
#include "server/server.h" | ||
|
||
class SyncMigrateContext { | ||
public: | ||
SyncMigrateContext(Server *svr, redis::Connection *conn, int timeout) : svr_(svr), conn_(conn), timeout_(timeout){}; | ||
SyncMigrateContext(SyncMigrateContext &&) = delete; | ||
SyncMigrateContext(const SyncMigrateContext &) = delete; | ||
|
||
SyncMigrateContext &operator=(SyncMigrateContext &&) = delete; | ||
SyncMigrateContext &operator=(const SyncMigrateContext &) = delete; | ||
|
||
~SyncMigrateContext(); | ||
|
||
void StartBlock(); | ||
void Wakeup(const Status &migrate_result); | ||
static void WriteCB(bufferevent *bev, void *ctx); | ||
static void EventCB(bufferevent *bev, int16_t events, void *ctx); | ||
static void TimerCB(int, int16_t events, void *ctx); | ||
|
||
private: | ||
Server *svr_; | ||
redis::Connection *conn_; | ||
int timeout_ = 0; | ||
event *timer_ = nullptr; | ||
|
||
Status migrate_result_; | ||
}; |
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.
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.
Why using
auto &&
here?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.
This is a reference to
std::unique_ptr<SlotMigrator>
, which cannot be copied.Technically
auto&
would suffice, but I personally preferauto&&
since it accepts more kinds of value (e.g.const
, not this case though).Of course, it is also okay to use
auto slot_migrator = self->svr_->slot_migrator.get()
(raw pointer) here. Just a choice of style.