Skip to content

Commit 82b2cfe

Browse files
committed
mma: move some methods of PendingRangeChange
This is in preparation for them becoming methods on ExternalRangeChange.
1 parent 5fa041d commit 82b2cfe

File tree

3 files changed

+198
-185
lines changed

3 files changed

+198
-185
lines changed

pkg/kv/kvserver/allocator/mmaprototype/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go_library(
1313
"load.go",
1414
"memo_helper.go",
1515
"messages.go",
16+
"range_change.go",
1617
"rebalance_advisor.go",
1718
"store_load_summary.go",
1819
"store_set.go",

pkg/kv/kvserver/allocator/mmaprototype/cluster_state.go

Lines changed: 0 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1817
"github.com/cockroachdb/cockroach/pkg/roachpb"
1918
"github.com/cockroachdb/cockroach/pkg/util/log"
2019
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
@@ -505,38 +504,6 @@ func MakePendingRangeChange(rangeID roachpb.RangeID, changes []ReplicaChange) Pe
505504
}
506505
}
507506

508-
func (prc PendingRangeChange) String() string {
509-
return redact.StringWithoutMarkers(prc)
510-
}
511-
512-
// SafeFormat implements the redact.SafeFormatter interface.
513-
//
514-
// This is adhoc for debugging. A nicer string format would include the
515-
// previous state and next state.
516-
func (prc PendingRangeChange) SafeFormat(w redact.SafePrinter, _ rune) {
517-
w.Printf("r%v=[", prc.RangeID)
518-
found := false
519-
if prc.IsTransferLease() {
520-
w.Printf("transfer_to=%v", prc.LeaseTransferTarget())
521-
found = true
522-
}
523-
if prc.IsChangeReplicas() {
524-
w.Printf("change_replicas=%v", prc.ReplicationChanges())
525-
found = true
526-
}
527-
if !found {
528-
panic("unknown change type")
529-
}
530-
w.Print(" cids=")
531-
for i, c := range prc.pendingReplicaChanges {
532-
if i > 0 {
533-
w.Print(",")
534-
}
535-
w.Printf("%v", c.changeID)
536-
}
537-
w.Print("]")
538-
}
539-
540507
// StringForTesting prints the untransformed internal state for testing.
541508
func (prc PendingRangeChange) StringForTesting() string {
542509
var b strings.Builder
@@ -557,158 +524,6 @@ func (prc PendingRangeChange) SortForTesting() {
557524
})
558525
}
559526

560-
// TODO(sumeer): A single PendingRangeChange can model a bunch of replica
561-
// changes and a lease transfer. Classifying the change as either
562-
// IsChangeReplicas or IsTransferLease is unnecessarily limiting. The only
563-
// code that really relies on this is integration code:
564-
// mma_store_rebalancer.go, allocator_sync.go, asim. We should fix those and
565-
// consider removing these methods.
566-
567-
// IsChangeReplicas returns true if the pending range change is a change
568-
// replicas operation.
569-
func (prc PendingRangeChange) IsChangeReplicas() bool {
570-
for _, c := range prc.pendingReplicaChanges {
571-
if c.isAddition() || c.isRemoval() || c.isPromoDemo() {
572-
continue
573-
} else {
574-
return false
575-
}
576-
}
577-
return true
578-
}
579-
580-
// IsTransferLease returns true if the pending range change is a transfer lease
581-
// operation.
582-
func (prc PendingRangeChange) IsTransferLease() bool {
583-
if len(prc.pendingReplicaChanges) != 2 {
584-
return false
585-
}
586-
var foundAddLease, foundRemoveLease bool
587-
for _, c := range prc.pendingReplicaChanges {
588-
if c.isAddition() || c.isRemoval() || c.isPromoDemo() {
589-
// Any changes to the replica type or replicaID are not lease transfers,
590-
// since they require a replication change.
591-
return false
592-
}
593-
if c.prev.IsLeaseholder && !c.next.IsLeaseholder {
594-
foundRemoveLease = true
595-
} else if !c.prev.IsLeaseholder && c.next.IsLeaseholder {
596-
foundAddLease = true
597-
} else {
598-
return false
599-
}
600-
}
601-
return foundAddLease && foundRemoveLease
602-
}
603-
604-
// ReplicationChanges returns the replication changes for the pending range
605-
// change. It panics if the pending range change is not a change replicas
606-
// operation.
607-
//
608-
// TODO(tbg): The ReplicationChanges can include a new leaseholder replica,
609-
// but the incoming leaseholder is not explicitly represented in
610-
// kvpb.ReplicationChanges. This is an existing modeling deficiency in the
611-
// kvserver code. In Replica.maybeTransferLeaseDuringLeaveJoint the first
612-
// VOTER_INCOMING is considered the new leaseholder. So the code below places
613-
// the new leaseholder (an ADD_VOTER) at index 0. It is not clear whether this
614-
// is sufficient for all integration use-cases. Verify and fix as needed.
615-
//
616-
// TODO(sumeer): this method is limiting, since a single PendingRangeChange
617-
// should be allowed to model any set of changes (see the existing TODO on
618-
// IsChangeReplicas).
619-
func (prc PendingRangeChange) ReplicationChanges() kvpb.ReplicationChanges {
620-
if !prc.IsChangeReplicas() {
621-
panic("RangeChange is not a change replicas")
622-
}
623-
chgs := make([]kvpb.ReplicationChange, 0, len(prc.pendingReplicaChanges))
624-
newLeaseholderIndex := -1
625-
for _, c := range prc.pendingReplicaChanges {
626-
switch c.replicaChangeType {
627-
case ChangeReplica, AddReplica, RemoveReplica:
628-
// These are the only permitted cases.
629-
default:
630-
panic(errors.AssertionFailedf("change type %v is not a change replicas", c.replicaChangeType))
631-
}
632-
// The kvserver code represents a change in replica type as an
633-
// addition and a removal of the same replica. For example, if a
634-
// replica changes from VOTER_FULL to NON_VOTER, we will emit a pair
635-
// of {ADD_NON_VOTER, REMOVE_VOTER} for the replica. The ordering of
636-
// this pair does not matter.
637-
//
638-
// TODO(tbg): confirm that the ordering does not matter.
639-
if c.replicaChangeType == ChangeReplica || c.replicaChangeType == AddReplica {
640-
chg := kvpb.ReplicationChange{Target: c.target}
641-
isNewLeaseholder := false
642-
switch c.next.ReplicaType.ReplicaType {
643-
case roachpb.VOTER_FULL:
644-
chg.ChangeType = roachpb.ADD_VOTER
645-
if c.next.IsLeaseholder {
646-
isNewLeaseholder = true
647-
}
648-
case roachpb.NON_VOTER:
649-
chg.ChangeType = roachpb.ADD_NON_VOTER
650-
default:
651-
panic(errors.AssertionFailedf("unexpected replica type %s", c.next.ReplicaType.ReplicaType))
652-
}
653-
if isNewLeaseholder {
654-
if newLeaseholderIndex >= 0 {
655-
panic(errors.AssertionFailedf(
656-
"multiple new leaseholders in change replicas"))
657-
}
658-
newLeaseholderIndex = len(chgs)
659-
}
660-
chgs = append(chgs, chg)
661-
}
662-
if c.replicaChangeType == ChangeReplica || c.replicaChangeType == RemoveReplica {
663-
chg := kvpb.ReplicationChange{Target: c.target}
664-
prevType := mapReplicaTypeToVoterOrNonVoter(c.prev.ReplicaType.ReplicaType)
665-
switch prevType {
666-
case roachpb.VOTER_FULL:
667-
chg.ChangeType = roachpb.REMOVE_VOTER
668-
case roachpb.NON_VOTER:
669-
chg.ChangeType = roachpb.REMOVE_NON_VOTER
670-
default:
671-
panic(errors.AssertionFailedf("unexpected replica type %s", c.prev.ReplicaType.ReplicaType))
672-
}
673-
chgs = append(chgs, chg)
674-
}
675-
}
676-
if newLeaseholderIndex >= 0 {
677-
// Move the new leaseholder to index 0.
678-
chgs[0], chgs[newLeaseholderIndex] = chgs[newLeaseholderIndex], chgs[0]
679-
}
680-
return chgs
681-
}
682-
683-
// LeaseTransferTarget returns the store ID of the store that is the target of
684-
// the lease transfer. It panics if the pending range change is not a transfer
685-
// lease operation.
686-
func (prc PendingRangeChange) LeaseTransferTarget() roachpb.StoreID {
687-
if !prc.IsTransferLease() {
688-
panic("pendingRangeChange is not a lease transfer")
689-
}
690-
for _, c := range prc.pendingReplicaChanges {
691-
if !c.prev.IsLeaseholder && c.next.IsLeaseholder {
692-
return c.target.StoreID
693-
}
694-
}
695-
panic("unreachable")
696-
}
697-
698-
// LeaseTransferFrom returns the store ID of the store that is the source of
699-
// the lease transfer. It panics if the pending range change is not a
700-
func (prc PendingRangeChange) LeaseTransferFrom() roachpb.StoreID {
701-
if !prc.IsTransferLease() {
702-
panic("pendingRangeChange is not a lease transfer")
703-
}
704-
for _, c := range prc.pendingReplicaChanges {
705-
if c.prev.IsLeaseholder && !c.next.IsLeaseholder {
706-
return c.target.StoreID
707-
}
708-
}
709-
panic("unreachable")
710-
}
711-
712527
// pendingReplicaChange is a proposed change to a single replica. Some
713528
// external entity (the leaseholder of the range) may choose to enact this
714529
// change. It may not be enacted if it will cause some invariant (like the

0 commit comments

Comments
 (0)