Skip to content

Generate correct slot information in cluster shards command on primary failure - #790

Merged
PingXie merged 6 commits into
valkey-io:unstablefrom
hpatro:cluster-shards-op
Jul 19, 2024
Merged

PingXie merged 6 commits into
valkey-io:unstablefrom
hpatro:cluster-shards-op

Conversation

@hpatro

@hpatro hpatro commented Jul 15, 2024 •

Copy link
Copy Markdown
Contributor

Fix #784

Prior to the change, CLUSTER SHARDS command processing might pick a failed primary node which won't have the slot coverage information and the slots output in turn would be empty. This change finds an appropriate node which has the slot coverage information served by a given shard and correctly displays it as part of CLUSTER SHARDS output.

Before:

1) 1) "slots"
  2)  (empty array)
  3) "nodes"
  4) 1)  1) "id"
         2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
         ...
         9) "role"
        10) "master"
        ...
        13) "health"
        14) "fail"

After:

1) 1) "slots"
  2) 1) 0
      2) 5461
  3) "nodes"
  4) 1)  1) "id"
         2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
         ...
         9) "role"
        10) "master"
        ...
        13) "health"
        14) "fail"

@codecov

codecov Bot commented Jul 15, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.42%. Comparing base (66d0f7d) to head (aa00339).
Report is 4 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable     #790      +/-   ##
============================================
+ Coverage     70.08%   70.42%   +0.34%     
============================================
  Files           112      112              
  Lines         60587    61139     +552     
============================================
+ Hits          42461    43058     +597     
+ Misses        18126    18081      -45     
Files Coverage Δ
src/cluster_legacy.c 85.65% <100.00%> (-0.06%) ⬇️

... and 18 files with indirect coverage changes

@enjoy-binbin enjoy-binbin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look like you forget to push the commit? there is only one commit that only contains the TCL code.

Comment thread tests/unit/cluster/cluster-shards.tcl Resolved
@madolson

Copy link
Copy Markdown
Member

I think Hari just pushed a reproduction this pr and will update it with a fix once it's ready,

@hpatro

hpatro commented Jul 16, 2024

Copy link
Copy Markdown
Contributor Author

look like you forget to push the commit? there is only one commit that only contains the TCL code.

Apologies for the confusion. Mentioned it on the issue, this is just the reproduction. Currently, the test checks if the slot array is empty for cluster shards response for a particular state and it passes (which is incorrect).

Will push out the fix soon and update the test.

@hpatro
hpatro marked this pull request as draft July 16, 2024 07:48
@enjoy-binbin

enjoy-binbin commented Jul 16, 2024 •

Copy link
Copy Markdown
Member

ok, i take a quick look and i think the issue is in here:

/* Add the shard reply of a single shard based off the given primary node. */
void addShardReplyForClusterShards(client *c, list *nodes) {
    serverAssert(listLength(nodes) > 0);
    clusterNode *n = listNodeValue(listFirst(nodes));
    addReplyMapLen(c, 2);
    addReplyBulkCString(c, "slots");

    /* Use slot_info_pairs from the primary only */
    n = clusterNodeGetPrimary(n);

the listFirst, that is, n, is the failed primary, so when we can clusterNodeGetPrimary, it will return itself, so at last, the n is the failed primary, so in other node view, n's slot info is empty.

i think we should try to look at a normal node like:

/* Get the primary from the shard list. */
+clusterNode *clusterShardsGetPrimary(list *nodes) {
+    clusterNode *primary;
+    listIter li;
+    listRewind(nodes, &li);
+    for (listNode *ln = listNext(&li); ln != NULL; ln = listNext(&li)) {
+       primary = listNodeValue(ln);
+        /* Skip failed nodes because their state is stale. */
+       if (nodeFailed(primary)) continue;
+    }
+    return clusterNodeGetPrimary(primary);
+}

 void addShardReplyForClusterShards(client *c, list *nodes) {
     serverAssert(listLength(nodes) > 0);
-    clusterNode *n = listNodeValue(listFirst(nodes));
+    clusterNode *n = clusterShardsGetPrimary(nodes);
     addReplyMapLen(c, 2);
     addReplyBulkCString(c, "slots");

-    /* Use slot_info_pairs from the primary only */
-    n = clusterNodeGetPrimary(n);

or another way is try to remove it from the shards list, but it looks like it will be troublesome.

Or move the new primary node to the head

+void clusterLinkNodeHeadInShard(clusterNode *node) {
+    sds s = sdsnewlen(node->shard_id, CLUSTER_NAMELEN);
+    dictEntry *de = dictFind(server.cluster->shards, s);
+    if (de != NULL) {
+        list *l = dictGetVal(de);
+        listNode *ln = listSearchKey(l, node);
+        if (ln != NULL) {
+            listUnlinkNode(l, ln);
+            listLinkNodeHead(l, ln);
+        }
+    }
+    sdsfree(s);
+}

@@ -2328,6 +2355,9 @@ void clusterSetNodeAsPrimary(clusterNode *n) {
     n->flags |= CLUSTER_NODE_PRIMARY;
     n->replicaof = NULL;
 
+    /* here, update the shard, link to head */
+    clusterLinkNodeHeadInShard(n);
+
     /* Update config and state. */
     clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE);
 }

@PingXie i think Ping can also take a look.

@hpatro

hpatro commented Jul 16, 2024

Copy link
Copy Markdown
Contributor Author

I thought on similar line to get active primary (primary + healthy) for a given shard and use slot information of that node. However, I'm not sure if we should ever have multiple primaries in a shard.

When we promote a node to primary for a shard, we should ideally mark the rest of the nodes in the shard as replica. However, we currently don't do it. What are your thoughts @madolson @enjoy-binbin @PingXie ?

@PingXie

PingXie commented Jul 17, 2024

Copy link
Copy Markdown
Member

I don't think we should go look for a primary node in this case. Rather, we should just go find the node in the list that has a non-NULL slot_info_pairs because this is the same decision made by clusterGenNodesSlotsInfo and what really matters.

Comment thread src/cluster_legacy.c Resolved
@hpatro

hpatro commented Jul 17, 2024

Copy link
Copy Markdown
Contributor Author

I don't think we should go look for a primary node in this case. Rather, we should just go find the node in the list that has a non-NULL slot_info_pairs because this is the same decision made by clusterGenNodesSlotsInfo and what really matters.

Your suggestion should work. However, the problem I've with the system in place is we aren't able to guarantee that there will be only one primary for a given shard. Maybe we can think about it separately.

@hpatro

hpatro commented Jul 17, 2024

Copy link
Copy Markdown
Contributor Author

For the test failure, issue has been filled already #734. Unrelated to this PR.

@hpatro
hpatro marked this pull request as ready for review July 17, 2024 18:14
@PingXie

PingXie commented Jul 17, 2024

Copy link
Copy Markdown
Member

Your suggestion should work. However, the problem I've with the system in place is we aren't able to guarantee that there will be only one primary for a given shard. Maybe we can think about it separately.

Agreed - I think #798 could lead to the "multi-primaries-in-a-shard" situation. I will work on a fix.

@hpatro
hpatro requested a review from madolson July 17, 2024 19:59
@hpatro

hpatro commented Jul 17, 2024

Copy link
Copy Markdown
Contributor Author

PTAL @madolson / @enjoy-binbin / @PingXie

@PingXie PingXie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall.

Comment thread src/cluster_legacy.c Resolved
Comment thread tests/unit/cluster/cluster-shards.tcl Resolved

@enjoy-binbin enjoy-binbin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Please also fill some desc to the top comment to describe the problem so that we can squash it into the commit message.

Signed-off-by: Harkrishn Patro <[email protected]>
@hpatro

hpatro commented Jul 18, 2024

Copy link
Copy Markdown
Contributor Author

@PingXie PTAL

@PingXie PingXie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @hpatro!

@hpatro

hpatro commented Jul 19, 2024

Copy link
Copy Markdown
Contributor Author

Shall we merge this ?

@PingXie
PingXie merged commit 816acce into valkey-io:unstable Jul 19, 2024
@PingXie

PingXie commented Jul 19, 2024

Copy link
Copy Markdown
Member

Merged.

Next question - I think that we should backport it to 7.2 too. WDYT?

@hpatro

hpatro commented Jul 19, 2024

Copy link
Copy Markdown
Contributor Author

Merged.

Next question - I think that we should backport it to 7.2 too. WDYT?

Makes sense 👍

PingXie pushed a commit to PingXie/valkey that referenced this pull request Jul 22, 2024
…y failure (valkey-io#790)

Fix valkey-io#784

Prior to the change, `CLUSTER SHARDS` command processing might pick a
failed primary node which won't have the slot coverage information and
the slots `output` in turn would be empty. This change finds an
appropriate node which has the slot coverage information served by a
given shard and correctly displays it as part of `CLUSTER SHARDS`
output.

 Before:

 ```
 1) 1) "slots"
   2)  (empty array)
   3) "nodes"
   4) 1)  1) "id"
          2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
          ...
          9) "role"
         10) "master"
         ...
         13) "health"
         14) "fail"
 ```

 After:

 ```
 1) 1) "slots"
   2) 1) 0
       2) 5461
   3) "nodes"
   4) 1)  1) "id"
          2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
          ...
          9) "role"
         10) "master"
         ...
         13) "health"
         14) "fail"
 ```

---------

Signed-off-by: Harkrishn Patro <[email protected]>
PingXie pushed a commit that referenced this pull request Jul 23, 2024
…y failure (#790)

Fix #784

Prior to the change, `CLUSTER SHARDS` command processing might pick a
failed primary node which won't have the slot coverage information and
the slots `output` in turn would be empty. This change finds an
appropriate node which has the slot coverage information served by a
given shard and correctly displays it as part of `CLUSTER SHARDS`
output.

 Before:

 ```
 1) 1) "slots"
   2)  (empty array)
   3) "nodes"
   4) 1)  1) "id"
          2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
          ...
          9) "role"
         10) "master"
         ...
         13) "health"
         14) "fail"
 ```

 After:

 ```
 1) 1) "slots"
   2) 1) 0
       2) 5461
   3) "nodes"
   4) 1)  1) "id"
          2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
          ...
          9) "role"
         10) "master"
         ...
         13) "health"
         14) "fail"
 ```

---------

Signed-off-by: Harkrishn Patro <[email protected]>
hwware pushed a commit to hwware/valkey that referenced this pull request Jul 25, 2024
…y failure (valkey-io#790)

Fix valkey-io#784 

Prior to the change, `CLUSTER SHARDS` command processing might pick a
failed primary node which won't have the slot coverage information and
the slots `output` in turn would be empty. This change finds an
appropriate node which has the slot coverage information served by a
given shard and correctly displays it as part of `CLUSTER SHARDS`
output.

 Before:
 
 ```
 1) 1) "slots"
   2)  (empty array)
   3) "nodes"
   4) 1)  1) "id"
          2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
          ...
          9) "role"
         10) "master"
         ...
         13) "health"
         14) "fail"
 ```
 
 After:
 

 ```
 1) 1) "slots"
   2) 1) 0
       2) 5461
   3) "nodes"
   4) 1)  1) "id"
          2) "2936f22a490095a0a851b7956b0a88f2b67a5d44"
          ...
          9) "role"
         10) "master"
         ...
         13) "health"
         14) "fail"
 ```

---------

Signed-off-by: Harkrishn Patro <[email protected]>
sarthakaggarwal97 added a commit to sarthakaggarwal97/valkey that referenced this pull request Nov 17, 2026
…key-io#790)

Run the existing CLUSTER SHARDS slot-information regressions in the default Tcl suite.

This follow-up changes test coverage only. Server code and the existing
regression expectations are unchanged.

Backport-Source-PR: 790
sarthakaggarwal97 added a commit that referenced this pull request Nov 18, 2026
Run the existing CLUSTER SHARDS slot-information regressions in the default Tcl suite.

This follow-up changes test coverage only. Server code and the existing
regression expectations are unchanged.

Backport-Source-PR: 790
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] CLUSTER SHARDS command returns "empty array" in slots section

4 participants