Unverified Commit a7051845 authored by Wen Hui's avatar Wen Hui Committed by GitHub
Browse files

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