From ff6419658bd8c8800734a7f19520c2aa7cd68a90 Mon Sep 17 00:00:00 2001 From: judeng Date: Tue, 21 Jun 2022 12:02:22 +0800 Subject: Optimize the performance of clusterSendPing for large clusters (#10624) Optimize the performance of clusterSendPing by improving speed of checking for duplicate items in gossip. --- src/cluster.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'src/cluster.c') diff --git a/src/cluster.c b/src/cluster.c index 0975db53a..cdbc565ec 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -964,6 +964,7 @@ clusterNode *createClusterNode(char *nodename, int flags) { node->numslaves = 0; node->slaves = NULL; node->slaveof = NULL; + node->last_in_ping_gossip = 0; node->ping_sent = node->pong_received = 0; node->data_received = 0; node->fail_time = 0; @@ -2847,18 +2848,6 @@ void clusterBuildMessageHdr(clusterMsg *hdr, int type) { * totlen field is up to the caller. */ } -/* Return non zero if the node is already present in the gossip section of the - * message pointed by 'hdr' and having 'count' gossip entries. Otherwise - * zero is returned. Helper for clusterSendPing(). */ -int clusterNodeIsInGossipSection(clusterMsg *hdr, int count, clusterNode *n) { - int j; - for (j = 0; j < count; j++) { - if (memcmp(hdr->data.ping.gossip[j].nodename,n->name, - CLUSTER_NAMELEN) == 0) break; - } - return j != count; -} - /* Set the i-th entry of the gossip section in the message pointed by 'hdr' * to the info of the specified node 'n'. */ void clusterSetGossipEntry(clusterMsg *hdr, int i, clusterNode *n) { @@ -2878,6 +2867,8 @@ void clusterSetGossipEntry(clusterMsg *hdr, int i, clusterNode *n) { /* Send a PING or PONG packet to the specified node, making sure to add enough * gossip information. */ void clusterSendPing(clusterLink *link, int type) { + static unsigned long long cluster_pings_sent = 0; + cluster_pings_sent++; unsigned char *buf; clusterMsg *hdr; int gossipcount = 0; /* Number of gossip sections added so far. */ @@ -2967,10 +2958,11 @@ void clusterSendPing(clusterLink *link, int type) { } /* Do not add a node we already have. */ - if (clusterNodeIsInGossipSection(hdr,gossipcount,this)) continue; + if (this->last_in_ping_gossip == cluster_pings_sent) continue; /* Add it */ clusterSetGossipEntry(hdr,gossipcount,this); + this->last_in_ping_gossip = cluster_pings_sent; freshnodes--; gossipcount++; } @@ -2987,7 +2979,6 @@ void clusterSendPing(clusterLink *link, int type) { if (node->flags & CLUSTER_NODE_NOADDR) continue; if (!(node->flags & CLUSTER_NODE_PFAIL)) continue; clusterSetGossipEntry(hdr,gossipcount,node); - freshnodes--; gossipcount++; /* We take the count of the slots we allocated, since the * PFAIL stats may not match perfectly with the current number -- cgit v1.2.1