-
Notifications
You must be signed in to change notification settings - Fork 881
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
feat(server): support client side tracking (resp3 protocol only) #2233
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
91fed71
fix(connection): Add ConnectionRef to replace pubsub wait token
dranikpg 0300a15
fix: add check to migrate
dranikpg 21ac976
fix: rename to weakref
dranikpg 814f73d
fix: add back migration backpressure update
dranikpg 9300710
fix: use monostate
dranikpg 402fb31
fix: rebase on new helio GetPoolIndex
dranikpg e2eb0b1
fix: fix internal compiler error??
dranikpg e35dbac
fix: fix test framework
dranikpg 2cdb8a9
fix: add comment about EnsureMemoryBudget
dranikpg fbd1748
support client side tracking using resp3
theyueli 31fd097
support verbatim string for resp3 and let INFO command uses it.
theyueli 0cbdcfe
tidy
theyueli 9992a85
clean
theyueli 203f9ed
Refurbish transaction before its reuse
theyueli 15e62b3
if hrandfield returns one element, just send bulkstring
theyueli 8e0fc9a
only track keys of its own shard.
theyueli 7f44f1d
review comments
theyueli 32bd6b3
rebase against vlad's fork (safe-ptr branch)
theyueli 062f227
Merge branch 'main' into relay
theyueli ed6c75e
fix typo
theyueli 002bc58
use weakref and fix hrandfield output
theyueli a17eed1
allow client tracking only when resp3 is used.
theyueli bfdd399
fix QUIT's property, makes WeakRef remember client id.
theyueli 3991c92
fix reply format of hrandfield when count is negative
theyueli 508bf21
fix reply format of hrandfield when count and withvalues are used.
theyueli 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,7 @@ struct Connection::DispatchOperations { | |
void operator()(const AclUpdateMessage& msg); | ||
void operator()(const MigrationRequestMessage& msg); | ||
void operator()(CheckpointMessage msg); | ||
void operator()(const InvalidationMessage& msg); | ||
|
||
template <typename T, typename D> void operator()(unique_ptr<T, D>& ptr) { | ||
operator()(*ptr.get()); | ||
|
@@ -215,6 +216,9 @@ size_t Connection::MessageHandle::UsedMemory() const { | |
size_t operator()(const CheckpointMessage& msg) { | ||
return 0; // no access to internal type, memory usage negligible | ||
} | ||
size_t operator()(const InvalidationMessage& msg) { | ||
return 0; | ||
} | ||
}; | ||
|
||
return sizeof(MessageHandle) + visit(MessageSize{}, this->handle); | ||
|
@@ -282,6 +286,19 @@ void Connection::DispatchOperations::operator()(CheckpointMessage msg) { | |
msg.bc.Dec(); | ||
} | ||
|
||
void Connection::DispatchOperations::operator()(const InvalidationMessage& msg) { | ||
RedisReplyBuilder* rbuilder = (RedisReplyBuilder*)builder; | ||
rbuilder->SetResp3(true); | ||
rbuilder->StartCollection(2, facade::RedisReplyBuilder::CollectionType::PUSH); | ||
rbuilder->SendBulkString("invalidate"); | ||
if (msg.invalidate_due_to_flush) { | ||
rbuilder->SendNull(); | ||
} else { | ||
std::vector<string_view> keys{msg.key}; | ||
rbuilder->SendStringArr(keys); | ||
} | ||
} | ||
|
||
Connection::Connection(Protocol protocol, util::HttpListenerBase* http_listener, SSL_CTX* ctx, | ||
ServiceInterface* service) | ||
: io_buf_(kMinReadSize), http_listener_(http_listener), ctx_(ctx), service_(service), name_{} { | ||
|
@@ -313,6 +330,10 @@ Connection::Connection(Protocol protocol, util::HttpListenerBase* http_listener, | |
|
||
migration_enabled_ = absl::GetFlag(FLAGS_migrate_connections); | ||
|
||
// Create shared_ptr with empty value and associate it with `this` pointer (aliasing constructor). | ||
// We use it for reference counting and accessing `this` (without managing it). | ||
self_ = {std::make_shared<std::monostate>(std::monostate{}), this}; | ||
|
||
#ifdef DFLY_USE_SSL | ||
// Increment reference counter so Listener won't free the context while we're | ||
// still using it. | ||
|
@@ -1168,11 +1189,19 @@ void Connection::Migrate(util::fb2::ProactorBase* dest) { | |
// connections | ||
CHECK(!cc_->async_dispatch); | ||
CHECK_EQ(cc_->subscriptions, 0); // are bound to thread local caches | ||
CHECK_EQ(self_.use_count(), 1u); // references cache our thread and backpressure | ||
CHECK(!dispatch_fb_.IsJoinable()); // can't move once it started | ||
|
||
listener()->Migrate(this, dest); | ||
} | ||
|
||
Connection::WeakRef Connection::Borrow() { | ||
DCHECK(self_); | ||
DCHECK_GT(cc_->subscriptions, 0); | ||
|
||
return WeakRef(self_, queue_backpressure_, socket_->proactor()->GetPoolIndex()); | ||
} | ||
|
||
void Connection::ShutdownThreadLocal() { | ||
pipeline_req_pool_.clear(); | ||
} | ||
|
@@ -1189,6 +1218,10 @@ void Connection::SendPubMessageAsync(PubMessage msg) { | |
SendAsync({PubMessagePtr{new (ptr) PubMessage{move(msg)}, MessageDeleter{}}}); | ||
} | ||
|
||
void Connection::SendInvalidationMessageAsync(InvalidationMessage msg) { | ||
SendAsync({std::move(msg)}); | ||
} | ||
|
||
void Connection::SendMonitorMessageAsync(string msg) { | ||
SendAsync({MonitorMessage{move(msg)}}); | ||
} | ||
|
@@ -1277,10 +1310,6 @@ void Connection::RecycleMessage(MessageHandle msg) { | |
} | ||
} | ||
|
||
void Connection::EnsureAsyncMemoryBudget() { | ||
queue_backpressure_->EnsureBelowLimit(); | ||
} | ||
|
||
std::string Connection::LocalBindStr() const { | ||
if (socket_->IsUDS()) | ||
return "unix-domain-socket"; | ||
|
@@ -1327,6 +1356,15 @@ void Connection::RequestAsyncMigration(util::fb2::ProactorBase* dest) { | |
migration_request_ = dest; | ||
} | ||
|
||
void Connection::EnableTracking() { | ||
tracking_enabled_ = true; | ||
cc_->subscriptions++; | ||
} | ||
|
||
void Connection::DisableTracking() { | ||
tracking_enabled_ = false; | ||
} | ||
|
||
Connection::MemoryUsage Connection::GetMemoryUsage() const { | ||
size_t mem = sizeof(*this) + dfly::HeapSize(dispatch_q_) + dfly::HeapSize(name_) + | ||
dfly::HeapSize(tmp_parse_args_) + dfly::HeapSize(tmp_cmd_vec_) + | ||
|
@@ -1344,6 +1382,35 @@ Connection::MemoryUsage Connection::GetMemoryUsage() const { | |
}; | ||
} | ||
|
||
Connection::WeakRef::WeakRef(std::shared_ptr<Connection> ptr, QueueBackpressure* backpressure, | ||
unsigned thread) | ||
: ptr_{ptr}, backpressure_{backpressure}, thread_{thread}, client_id_{ptr->GetClientId()} { | ||
} | ||
|
||
unsigned Connection::WeakRef::Thread() const { | ||
return thread_; | ||
} | ||
|
||
Connection* Connection::WeakRef::Get() const { | ||
// DCHECK_EQ(ProactorBase::me()->GetPoolIndex(), int(thread_)); | ||
return ptr_.lock().get(); | ||
} | ||
|
||
uint32_t Connection::WeakRef::GetClientId() const { | ||
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. @dranikpg added this feature to WeakRef as well. |
||
return client_id_; | ||
} | ||
|
||
bool Connection::WeakRef::EnsureMemoryBudget() const { | ||
// Simple optimization: If a connection was closed, don't check memory budget. | ||
if (!ptr_.expired()) { | ||
// We don't rely on the connection ptr staying valid because we only access | ||
// the threads backpressure | ||
backpressure_->EnsureBelowLimit(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void Connection::DecreaseStatsOnClose() { | ||
stats_->read_buf_capacity -= io_buf_.Capacity(); | ||
|
||
|
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
Oops, something went wrong.
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.
@dranikpg i had to comment this check... otherwise i'm running into the following segfault:
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.
Yep, it was there for a reason. Will update the interface 🙂