Skip to content

Commit

Permalink
kvserver: improve handling for removal of a replica, when multiple re…
Browse files Browse the repository at this point in the history
…plicas already exist on the same node

Fixes #60545

The allocator in some cases allows for a range to have a replica
on multiple stores of the same node. If that happens, it should allow
itself to fix the situation by removing one of the offending replicas.
This was only half working due to an ordering problem in how the replicas
appeared in the descriptor. It could remove the first replica, but not the second one.

.

Release note: None
  • Loading branch information
lunevalex committed Feb 12, 2021
1 parent 005c156 commit 688740b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
40 changes: 37 additions & 3 deletions pkg/kv/kvserver/replica_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,26 @@ func maybeLeaveAtomicChangeReplicasAndRemoveLearners(
return desc, nil
}

func validateReplicationChangesMultipleReplicasOnTheSameStore(
byStoreID map[roachpb.StoreID]roachpb.ReplicationChange, descs []roachpb.ReplicaDescriptor,
) error {
if len(byStoreID) != 1 {
return errors.Errorf(
"An unexpected number of changes %s for range %s, the only valid operation when there are already multiple replicas on the same node is removal", byStoreID, descs)
}
for _, rDesc := range descs {
chg, ok := byStoreID[rDesc.StoreID]
if ok {
if !chg.ChangeType.IsRemoval() {
return errors.Errorf(
"Expected replica to be removed from %v instead got %v.", rDesc, chg)
}
return nil
}
}
return errors.Errorf("Expected a removal of one of the replicas in %s, instead got %s", descs, byStoreID)
}

func validateReplicationChanges(
desc *roachpb.RangeDescriptor, chgs roachpb.ReplicationChanges,
) error {
Expand Down Expand Up @@ -1180,15 +1200,29 @@ func validateReplicationChanges(
byStoreID[chg.Target.StoreID] = chg
}

descriptorsByNodeID := make(map[roachpb.NodeID][]roachpb.ReplicaDescriptor, len(desc.Replicas().Descriptors()))
for _, rDesc := range desc.Replicas().Descriptors() {
descriptorsByNodeID[rDesc.NodeID] = append(descriptorsByNodeID[rDesc.NodeID], rDesc)
}

// Then, check that we're not adding a second replica on nodes that already
// have one, or "re-add" an existing replica. We delete from byNodeAndStoreID so that
// after this loop, it contains only Nodes that we haven't seen in desc.
for _, rDesc := range desc.Replicas().Descriptors() {
byStoreID, ok := byNodeAndStoreID[rDesc.NodeID]
for nodeID, descs := range descriptorsByNodeID {
byStoreID, ok := byNodeAndStoreID[nodeID]
if !ok {
continue
}
delete(byNodeAndStoreID, rDesc.NodeID)
delete(byNodeAndStoreID, nodeID)
// The only valid thing to do when we already have multiple replicas on the
// same node is to remove a replica.
if len(descs) > 1 {
if err := validateReplicationChangesMultipleReplicasOnTheSameStore(byStoreID, descs); err != nil {
return err
}
continue
}
rDesc := descs[0]
if len(byStoreID) == 2 {
chg, k := byStoreID[rDesc.StoreID]
// We should be removing the replica from the existing store during a
Expand Down
12 changes: 9 additions & 3 deletions pkg/kv/kvserver/replica_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,26 @@ func TestValidateReplicationChanges(t *testing.T) {
})
require.NoError(t, err)

// Test Case 15: Do an add while rebalancing within a node
// Test Case 15: same as 14 but remove the second node
err = validateReplicationChanges(descRebalancing, roachpb.ReplicationChanges{
{ChangeType: roachpb.REMOVE_VOTER, Target: roachpb.ReplicationTarget{NodeID: 1, StoreID: 2}},
})
require.NoError(t, err)

// Test Case 16: Do an add while rebalancing within a node
err = validateReplicationChanges(descRebalancing, roachpb.ReplicationChanges{
{ChangeType: roachpb.ADD_VOTER, Target: roachpb.ReplicationTarget{NodeID: 3, StoreID: 3}},
})
require.NoError(t, err)

// Test Case 16: Remove/Add within a node is not allowed, since we expect Add/Remove
// Test Case 17: Remove/Add within a node is not allowed, since we expect Add/Remove
err = validateReplicationChanges(desc, roachpb.ReplicationChanges{
{ChangeType: roachpb.REMOVE_VOTER, Target: roachpb.ReplicationTarget{NodeID: 1, StoreID: 1}},
{ChangeType: roachpb.ADD_VOTER, Target: roachpb.ReplicationTarget{NodeID: 1, StoreID: 2}},
})
require.Regexp(t, "can only add-remove a replica within a node, but got ", err)

// Test Case 17: We are rebalancing within a node and have only one replica
// Test Case 18: We are rebalancing within a node and have only one replica
descSingle := &roachpb.RangeDescriptor{
InternalReplicas: []roachpb.ReplicaDescriptor{
{NodeID: 1, StoreID: 1},
Expand Down

0 comments on commit 688740b

Please sign in to comment.