Skip to content

fix(cluster): seed a per-pod shard-id so primaries appear in CLUSTER SHARDS - #106

Merged
chideat merged 2 commits into
mainfrom
fix/cluster-shard-id-seeding
Sep 11, 2026
Merged

chideat merged 2 commits into
mainfrom
fix/cluster-shard-id-seeding

Conversation

@chideat

@chideat chideat commented Sep 11, 2026

Copy link
Copy Markdown
Owner

A freshly seeded primary is missing from its own `CLUSTER SHARDS` reply, taking the slot range it owns with it.

valkey-go reads `CLUSTER SHARDS` on servers >= 8 and rebuilds a fresh `[16384]conn` from one node's answer, so whenever its background topology refresh happens to query such a primary, a third of the keyspace is left unrouted and every key in it fails with the slot has no valkey node.

Why

valkey inserts a node into server.cluster->shards from only two places: auxShardIdSetter, which runs only when the nodes.conf line actually carries a shard-id= aux field, and a clusterLoadConfig fallback for a primary "without a persisted shard_id". That fallback is unreachable — auxShardIdPresent() measures strlen(n->shard_id) against the random value createClusterNode has already written into every node, so it always reports the id as present.

The helper seeds a primary line with a node-id and no shard-id, so such a node loads into server.cluster->nodes but never into server.cluster->shards. Nothing else gives it away: CLUSTER NODES, CLUSTER SLOTS, slot coverage and the on-disk shard-ids all stay correct. Replicas escape it because CLUSTER REPLICATE re-homes them through updateShardId, and a restart repairs it because valkey has by then rewritten nodes.conf with a real shard-id.

cmd/helper/commands/cluster/heal.go:286 currently states the omission is deliberate — that is the state this fixes.

This is not a revert

It does not revert the earlier removal of the StatefulSet-derived shard-id (#98). That one was shared by a primary and its replica from first boot, which put the replica's first post-REPLICATE announcement on valkey's same-shard stale-packet branch, where 9.1's tightened check (valkey-io/valkey#2811) dropped it forever and failover elections drew zero votes.

The id seeded here is unique per pod, reproducing what valkey does natively (createClusterNode assigns a random shard-id per node), so the announcement still takes the safe cross-shard-move branch and valkey re-homes the replica onto its primary's shard-id.

Tests

heal_test.go asserts the seeded record carries a shard-id and that ids differ per pod. Verified to fail against the previous implementation.

Ported from the downstream release-2.0 branch. Independent of the other open port PRs.

chideat and others added 2 commits September 11, 2026 03:42
A freshly seeded primary was missing from its own CLUSTER SHARDS reply,
taking the slot range it owns with it. valkey-go reads CLUSTER SHARDS on
servers >= 8 and rebuilds a fresh [16384]conn from one node's answer, so
whenever its background topology refresh happened to query a primary, a
third of the keyspace was left unrouted and every key in it failed with
"the slot has no valkey node".

valkey inserts a node into server.cluster->shards from only two places:
auxShardIdSetter, which runs only when the nodes.conf line actually
carries a "shard-id=" aux field, and a clusterLoadConfig fallback for a
primary "without a persisted shard_id". That fallback is unreachable —
auxShardIdPresent() measures strlen(n->shard_id) against the random value
createClusterNode has already written into every node, so it always
reports the id as present. The helper seeded a primary line with a
node-id and no shard-id, so such a node loaded into
server.cluster->nodes but never into server.cluster->shards. Nothing else
gave the state away: CLUSTER NODES, CLUSTER SLOTS, slot coverage and the
on-disk shard-ids all stayed correct and consistent. Replicas escaped it
because CLUSTER REPLICATE re-homes them through updateShardId, and a
restart repaired it because valkey had by then rewritten nodes.conf with
a real shard-id.

Seed a shard-id derived from the pod. It must be unique per pod: this is
not a revert of a5d5c51, which removed a shard-id derived from the
StatefulSet and therefore shared by a primary and its replica from first
boot. That sharing put the replica's first post-REPLICATE announcement on
valkey's same-shard stale-packet branch, where valkey 9.1's tightened
check (valkey-io/valkey#2811) dropped it forever and failover elections
drew zero votes. A per-pod id reproduces what valkey does natively —
createClusterNode assigns a random shard-id per node — so the
announcement still takes the safe cross-shard-move branch and valkey
re-homes the replica onto its primary's shard-id.

Verified on a 3x2 cluster: every primary now reports all six nodes and
all three slot ranges, and each primary/replica pair is grouped into one
shard, confirming updateShardId still converges them. Full e2e against a
live cluster ran 533 specs across cluster/failover/replica x 7.2/8.0/8.1/
9.0/9.1 x ClusterIP/NodePort with no failures, including the 9.1
master-kill and sentinel-promotion specs that guard the a5d5c51 fix.

(cherry picked from commit 681754759a0e806a00939bcefcb3f32599ae30b8)
(cherry picked from commit ec7f4334cd38356b8b96e3c1a4bae88a0afa99ed)
@chideat
chideat merged commit 71e5576 into main Sep 11, 2026
4 checks passed
@chideat
chideat deleted the fix/cluster-shard-id-seeding branch September 11, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant