Skip to content

Commit

Permalink
Simplify isNodeAvailable function and add comments
Browse files Browse the repository at this point in the history
Use getNodeReplicationOffset to replace the original logic
and add the corresponding annotations.

Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin committed Sep 5, 2024
1 parent 9033734 commit 158805c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1332,16 +1332,18 @@ void addNodeToNodeReply(client *c, clusterNode *node) {
* not finished their initial sync, in failed state, or are
* otherwise considered not available to serve read commands. */
int isNodeAvailable(clusterNode *node) {
/* We don't consider PFAIL here because it's not a reliable indicator
* for node available and we don't want clients to use it. */
if (clusterNodeIsFailing(node)) {
return 0;
}
long long repl_offset = clusterNodeReplOffset(node);
if (clusterNodeIsMyself(node)) {
/* Nodes do not update their own information
* in the cluster node list. */
repl_offset = getNodeReplicationOffset(node);

/* Hide empty replicas in here, from a data-path POV, an empty replica
* is not available. */
if (getNodeReplicationOffset(node) == 0) {
return 0;
}
return (repl_offset != 0);
return 1;
}

void addNodeReplyForClusterSlot(client *c, clusterNode *node, int start_slot, int end_slot) {
Expand Down
1 change: 0 additions & 1 deletion src/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ clusterNode *getNodeBySlot(int slot);
int clusterNodeClientPort(clusterNode *n, int use_tls);
char *clusterNodeHostname(clusterNode *node);
const char *clusterNodePreferredEndpoint(clusterNode *n, client *c);
long long clusterNodeReplOffset(clusterNode *node);
clusterNode *clusterLookupNode(const char *name, int length);
int detectAndUpdateCachedNodeHealth(void);
client *createCachedResponseClient(int resp);
Expand Down
8 changes: 4 additions & 4 deletions src/cluster_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -5797,6 +5797,10 @@ void clusterUpdateSlots(client *c, unsigned char *slots, int del) {
}
}

/* Get the replication offset of a node.
*
* Nodes do not update their own information in the cluster node list,
* so for myself node, we cannot use node->repl_offset directly. */
long long getNodeReplicationOffset(clusterNode *node) {
if (node->flags & CLUSTER_NODE_MYSELF) {
return nodeIsReplica(node) ? replicationGetReplicaOffset() : server.primary_repl_offset;
Expand Down Expand Up @@ -6793,10 +6797,6 @@ char *clusterNodeHostname(clusterNode *node) {
return node->hostname;
}

long long clusterNodeReplOffset(clusterNode *node) {
return node->repl_offset;
}

const char *clusterNodePreferredEndpoint(clusterNode *n, client *c) {
char *hostname = clusterNodeHostname(n);
switch (server.cluster_preferred_endpoint_type) {
Expand Down

0 comments on commit 158805c

Please sign in to comment.