Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kvserver: improve handling for removal of a replica, when multiple replicas already exist on the same node #60546

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions pkg/kv/kvserver/replica_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,27 @@ func maybeLeaveAtomicChangeReplicasAndRemoveLearners(
return desc, nil
}

func validateReplicationChangesMultipleReplicasOnTheSameNode(
byStoreID map[roachpb.StoreID]roachpb.ReplicationChange,
rDescsForNode []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, rDescsForNode)
}
for _, rDesc := range rDescsForNode {
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", rDescsForNode, byStoreID)
}

func validateReplicationChanges(
desc *roachpb.RangeDescriptor, chgs roachpb.ReplicationChanges,
) error {
Expand Down Expand Up @@ -1188,15 +1209,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, rDescsForNode := 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(rDescsForNode) > 1 {
if err := validateReplicationChangesMultipleReplicasOnTheSameNode(byStoreID, rDescsForNode); err != nil {
return err
}
continue
}
rDesc := rDescsForNode[0]
if len(byStoreID) == 2 {
chg, k := byStoreID[rDesc.StoreID]
// We should be removing the replica from the existing store during a
Expand Down
14 changes: 10 additions & 4 deletions pkg/kv/kvserver/replica_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,34 @@ func TestValidateReplicationChanges(t *testing.T) {
InternalReplicas: []roachpb.ReplicaDescriptor{
{NodeID: 1, StoreID: 1},
{NodeID: 2, StoreID: 2},
{NodeID: 1, StoreID: 2, Type: &learnerType},
{NodeID: 1, StoreID: 3, Type: &learnerType},
},
}
err = validateReplicationChanges(descRebalancing, roachpb.ReplicationChanges{
{ChangeType: roachpb.REMOVE_VOTER, Target: roachpb.ReplicationTarget{NodeID: 1, StoreID: 1}},
})
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: 3}},
})
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