PragmaTwice commented on code in PR #3047:
URL: https://github.com/apache/kvrocks/pull/3047#discussion_r2203760800
##########
src/server/server.cc:
##########
@@ -700,6 +701,63 @@ void Server::OnEntryAddedToStream(const std::string &ns,
const std::string &key,
}
}
+void Server::BlockOnWait(redis::Connection *conn, rocksdb::SequenceNumber
target_seq, uint64_t num_replicas) {
+ std::lock_guard<std::mutex> guard(wait_contexts_mu_);
+
+ wait_contexts_.emplace_back(conn, target_seq, num_replicas);
+ IncrBlockedClientNum();
+}
+
+void Server::WakeupWaitConnections(rocksdb::SequenceNumber seq) {
+ std::lock_guard<std::mutex> guard(wait_contexts_mu_);
+
+ for (auto it = wait_contexts_.begin(); it != wait_contexts_.end();) {
+ // Check if target sequence is reached
+ if (seq >= it->target_seq) {
+ // Count how many replicas have reached the target sequence
+ int reached_replicas = GetReplicasReachedSequence(it->target_seq);
+
+ // If enough replicas have reached the target sequence, wake up the
connection
+ if (reached_replicas >= static_cast<int>(it->num_replicas)) {
+ // Send the response with the number of replicas that have reached the
target sequence
+ it->conn->Reply(redis::Integer(reached_replicas));
+
+ auto s = it->conn->Owner()->EnableWriteEvent(it->conn->GetFD());
+ if (!s.IsOK()) {
+ error("[server] Failed to enable write event on WAIT connection {}:
{}", it->conn->GetFD(), s.Msg());
+ }
+ it = wait_contexts_.erase(it);
+ DecrBlockedClientNum();
+ continue;
+ }
+ }
+
+ ++it;
+ }
+}
+
+void Server::CleanupWaitConnection(redis::Connection *conn) {
+ std::lock_guard<std::mutex> guard(wait_contexts_mu_);
+
+ auto it = std::find_if(wait_contexts_.begin(), wait_contexts_.end(),
+ [conn](const auto &context) { return context.conn ==
conn; });
+ if (it != wait_contexts_.end()) {
+ wait_contexts_.erase(it);
+ DecrBlockedClientNum();
+ }
+}
+
+int Server::GetReplicasReachedSequence(rocksdb::SequenceNumber target_seq) {
+ std::lock_guard<std::mutex> slave_guard(slave_threads_mu_);
+ int reached_replicas = 0;
+ for (const auto &slave : slave_threads_) {
+ if (!slave->IsStopped() && slave->GetCurrentReplSeq() >= target_seq) {
+ reached_replicas++;
+ }
+ }
+ return reached_replicas;
+}
Review Comment:
```suggestion
size_t Server::GetReplicasReachedSequence(rocksdb::SequenceNumber
target_seq) {
std::lock_guard<std::mutex> slave_guard(slave_threads_mu_);
size_t reached_replicas = 0;
for (const auto &slave : slave_threads_) {
if (!slave->IsStopped() && slave->GetCurrentReplSeq() >= target_seq) {
reached_replicas++;
}
}
return reached_replicas;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]