Update codes (#11804)

In this PR, we use function pointer *isPresent replace the variable "present" in auxFieldHandler, so that in the future, when we have more aux fields, we could decide if the aux field is displayed or not.
This commit is contained in:
Wen Hui 2023-02-14 16:47:55 -05:00 committed by GitHub
parent 9483ab0b8e
commit a705184522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,6 +88,7 @@ list *clusterLookupNodeListByShardId(const char *shard_id);
void clusterRemoveNodeFromShard(clusterNode *node);
int auxShardIdSetter(clusterNode *n, void *value, int length);
sds auxShardIdGetter(clusterNode *n, sds s);
int auxShardIdPresent(clusterNode *n);
static void clusterBuildMessageHdr(clusterMsg *hdr, int type, size_t msglen);
/* Links to the next and previous entries for keys in the same slot are stored
@ -159,11 +160,13 @@ typedef int (aux_value_setter) (clusterNode* n, void *value, int length);
* the aux value */
typedef sds (aux_value_getter) (clusterNode* n, sds s);
typedef int (aux_value_present) (clusterNode* n);
typedef struct {
char *field;
aux_value_setter *setter;
aux_value_getter *getter;
int present;
aux_value_present *isPresent;
} auxFieldHandler;
/* Assign index to each aux field */
@ -178,7 +181,7 @@ typedef enum {
* indices as defined in auxFieldIndex
* 2. aux name can contain characters that pass the isValidAuxChar check only */
auxFieldHandler auxFieldHandlers[] = {
{"shard-id", auxShardIdSetter, auxShardIdGetter, 0},
{"shard-id", auxShardIdSetter, auxShardIdGetter, auxShardIdPresent},
};
int isValidAuxChar(int c) {
@ -212,6 +215,10 @@ sds auxShardIdGetter(clusterNode *n, sds s) {
return sdscatprintf(s, "%.40s", n->shard_id);
}
int auxShardIdPresent(clusterNode *n) {
return strlen(n->shard_id);
}
/* clusterLink send queue blocks */
typedef struct {
size_t totlen; /* Total length of this block including the message */
@ -378,7 +385,6 @@ int clusterLoadConfig(char *filename) {
sdsfreesplitres(argv,argc);
goto fmterr;
}
auxFieldHandlers[j].present = 1;
}
if (field_found == 0) {
@ -463,7 +469,7 @@ int clusterLoadConfig(char *filename) {
/* shard_id can be absent if we are loading a nodes.conf generated
* by an older version of Redis; we should follow the primary's
* shard_id in this case */
if (auxFieldHandlers[af_shard_id].present == 0) {
if (auxFieldHandlers[af_shard_id].isPresent(n) == 0) {
memcpy(n->shard_id, master->shard_id, CLUSTER_NAMELEN);
clusterAddNodeToShard(master->shard_id, n);
} else if (clusterGetNodesInMyShard(master) != NULL &&
@ -475,7 +481,7 @@ int clusterLoadConfig(char *filename) {
}
n->slaveof = master;
clusterNodeAddSlave(master,n);
} else if (auxFieldHandlers[af_shard_id].present == 0) {
} else if (auxFieldHandlers[af_shard_id].isPresent(n) == 0) {
/* n is a primary but it does not have a persisted shard_id.
* This happens if we are loading a nodes.conf generated by
* an older version of Redis. We should manually update the
@ -5016,8 +5022,10 @@ sds clusterGenNodeDescription(clusterNode *node, int use_pport) {
/* Node's aux fields */
for (int i = af_start; i < af_count; i++) {
ci = sdscatprintf(ci, ",%s=", auxFieldHandlers[i].field);
ci = auxFieldHandlers[i].getter(node, ci);
if (auxFieldHandlers[i].isPresent(node)) {
ci = sdscatprintf(ci, ",%s=", auxFieldHandlers[i].field);
ci = auxFieldHandlers[i].getter(node, ci);
}
}
/* Flags */