Skip to content
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

fix: assign threadlocal data structures during connection migration #2237

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/facade/dragonfly_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,7 @@ void Connection::ConnectionFlow(FiberSocketBase* peer) {
DCHECK(dispatch_q_.empty());

service_->OnClose(cc_.get());

stats_->read_buf_capacity -= io_buf_.Capacity();

// Update num_replicas if this was a replica connection.
if (cc_->replica_conn) {
--stats_->num_replicas;
}
DecreaseStatsOnClose();

// We wait for dispatch_fb to finish writing the previous replies before replying to the last
// offending request.
Expand Down Expand Up @@ -657,8 +651,6 @@ void Connection::ConnectionFlow(FiberSocketBase* peer) {
LOG(WARNING) << "Socket error for connection " << conn_info << " " << GetName() << ": " << ec
<< " " << ec.message();
}

--stats_->num_conns;
}

void Connection::DispatchCommand(uint32_t consumed, mi_heap_t* heap) {
Expand Down Expand Up @@ -826,10 +818,29 @@ void Connection::HandleMigrateRequest() {
// handles. We can't check above, as the queue might have contained a subscribe request.
if (cc_->subscriptions == 0) {
migration_request_ = nullptr;

DecreaseStatsOnClose();

this->Migrate(dest);

// We're now running in `dest` thread
queue_backpressure_ = &tl_queue_backpressure_;
auto update_tl_vars = [this] [[gnu::noinline]] {
// The compiler barrier that does not allow reordering memory accesses
// to before this function starts. See https://stackoverflow.com/a/75622732
asm volatile("");
romange marked this conversation as resolved.
Show resolved Hide resolved

queue_backpressure_ = &tl_queue_backpressure_;

stats_ = service_->GetThreadLocalConnectionStats();
++stats_->num_conns;
stats_->read_buf_capacity += io_buf_.Capacity();
if (cc_->replica_conn) {
++stats_->num_replicas;
}
};

// We're now running in `dest` thread. We use non-inline lambda to force reading new thread's
// thread local vars.
update_tl_vars();
}

DCHECK(dispatch_q_.empty());
Expand Down Expand Up @@ -1333,4 +1344,14 @@ Connection::MemoryUsage Connection::GetMemoryUsage() const {
};
}

void Connection::DecreaseStatsOnClose() {
adiholden marked this conversation as resolved.
Show resolved Hide resolved
stats_->read_buf_capacity -= io_buf_.Capacity();

// Update num_replicas if this was a replica connection.
if (cc_->replica_conn) {
--stats_->num_replicas;
}
--stats_->num_conns;
}

} // namespace facade
2 changes: 2 additions & 0 deletions src/facade/dragonfly_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ class Connection : public util::Connection {
std::unique_ptr<ConnectionContext> cc_; // Null for http connections

private:
void DecreaseStatsOnClose();

std::deque<MessageHandle> dispatch_q_; // dispatch queue
dfly::EventCount evc_; // dispatch queue waker
util::fb2::Fiber dispatch_fb_; // dispatch fiber (if started)
Expand Down
Loading