Skip to content

Commit a47f5a0

Browse files
authored
Fix unable to promote the new master while sequence=0 during cluster shard failover (#377)
1 parent b52b89c commit a47f5a0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

store/cluster_shard.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ func (shard *Shard) removeNode(nodeID string) error {
151151
func (shard *Shard) getNewMasterNodeIndex(ctx context.Context, masterNodeIndex int, preferredNodeID string) int {
152152
newMasterNodeIndex := -1
153153
var newestOffset uint64
154+
155+
// Get master sequence to handle empty shard case (issue #366)
156+
var masterSequence uint64
157+
if masterNodeIndex >= 0 && masterNodeIndex < len(shard.Nodes) {
158+
masterNode := shard.Nodes[masterNodeIndex]
159+
if _, err := masterNode.GetClusterInfo(ctx); err == nil {
160+
if masterInfo, err := masterNode.GetClusterNodeInfo(ctx); err == nil {
161+
masterSequence = masterInfo.Sequence
162+
}
163+
}
164+
}
165+
154166
for i, node := range shard.Nodes {
155167
// don't promote the current master node
156168
if i == masterNodeIndex {
@@ -176,12 +188,14 @@ func (shard *Shard) getNewMasterNodeIndex(ctx context.Context, masterNodeIndex i
176188
).Warn("Skip the node due to failed to get info of node")
177189
continue
178190
}
179-
if clusterNodeInfo.Role != RoleSlave || clusterNodeInfo.Sequence == 0 {
191+
// Fix #366: allow sequence == 0 only when master sequence is also 0 (empty shard)
192+
if clusterNodeInfo.Role != RoleSlave || (clusterNodeInfo.Sequence == 0 && masterSequence != 0) {
180193
logger.Get().With(
181194
zap.String("id", node.ID()),
182195
zap.String("addr", node.Addr()),
183196
zap.String("role", clusterNodeInfo.Role),
184197
zap.Uint64("sequence", clusterNodeInfo.Sequence),
198+
zap.Uint64("master_sequence", masterSequence),
185199
).Warn("Skip the node due to role or sequence invalid")
186200
continue
187201
}

0 commit comments

Comments
 (0)