From d10b2f3173ebfcc673d4ec36efef512802e3a535 Mon Sep 17 00:00:00 2001 From: caozb <1162650653@qq.com> Date: Thu, 13 Aug 2020 19:30:51 +0800 Subject: [PATCH] wait command optimization (#7333) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Client that issued WAIT last will most likely have the highest replication offset, so imagine a probably common case where all clients are waiting for the same number of replicas. we prefer the loop to start from the last client (one waiting for the highest offset), so that the optimization in the function will call replicationCountAcksByOffset for each client until it found a good one, and stop calling it for the rest of the clients. the way the loop was implemented would mean that in such case it is likely to call replicationCountAcksByOffset for all clients. Note: the change from > to >= is not directly related to the above. Co-authored-by: 曹正斌 --- src/replication.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/replication.c b/src/replication.c index 846f3a26b..1e1be345c 100644 --- a/src/replication.c +++ b/src/replication.c @@ -3068,7 +3068,7 @@ void waitCommand(client *c) { c->bpop.timeout = timeout; c->bpop.reploffset = offset; c->bpop.numreplicas = numreplicas; - listAddNodeTail(server.clients_waiting_acks,c); + listAddNodeHead(server.clients_waiting_acks,c); blockClient(c,BLOCKED_WAIT); /* Make sure that the server will send an ACK request to all the slaves @@ -3103,8 +3103,8 @@ void processClientsWaitingReplicas(void) { * offset and number of replicas, we remember it so the next client * may be unblocked without calling replicationCountAcksByOffset() * if the requested offset / replicas were equal or less. */ - if (last_offset && last_offset > c->bpop.reploffset && - last_numreplicas > c->bpop.numreplicas) + if (last_offset && last_offset >= c->bpop.reploffset && + last_numreplicas >= c->bpop.numreplicas) { unblockClient(c); addReplyLongLong(c,last_numreplicas);