Skip to content

Commit

Permalink
Merge #60633
Browse files Browse the repository at this point in the history
60633: release-20.2: kvserver: improve handling for removal of a replica, when multiple replicas already exist on the same node r=aayushshah15 a=lunevalex

Backport 1/1 commits from #60546.

/cc @cockroachdb/release

---

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


Co-authored-by: Alex Lunev <alexl@cockroachlabs.com>
  • Loading branch information
craig[bot] and lunevalex committed Feb 17, 2021
2 parents b81a3da + 014312e commit 6b701b5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
41 changes: 38 additions & 3 deletions pkg/kv/kvserver/replica_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,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 != roachpb.REMOVE_REPLICA {
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 @@ -1150,15 +1171,29 @@ func validateReplicationChanges(
byStoreID[chg.Target.StoreID] = chg
}

descriptorsByNodeID := make(map[roachpb.NodeID][]roachpb.ReplicaDescriptor, len(desc.Replicas().All()))
for _, rDesc := range desc.Replicas().All() {
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().All() {
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_REPLICA, 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_REPLICA, 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_REPLICA, 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_REPLICA, Target: roachpb.ReplicationTarget{NodeID: 1, StoreID: 1}},
{ChangeType: roachpb.ADD_REPLICA, 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 6b701b5

Please sign in to comment.