Skip to content

Commit

Permalink
kvserver: export lease preferences status
Browse files Browse the repository at this point in the history
Make `LeasePreferencesStatus` and associated helpers public. These are
later used in the allocator simulator for conformance assertions.

Part of: cockroachdb#111838
  • Loading branch information
kvoli committed Nov 8, 2023
1 parent 12e3d42 commit 9319d63
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions pkg/kv/kvserver/replica_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func calcReplicaMetrics(d calcReplicaMetricsInput) ReplicaMetrics {
validLeaseType = d.leaseStatus.Lease.Type()
if validLeaseOwner {
livenessLease = keys.NodeLivenessSpan.Overlaps(d.desc.RSpan().AsRawSpanWithNoLocals())
switch checkStoreAgainstLeasePreferences(
switch CheckStoreAgainstLeasePreferences(
d.storeID, d.storeAttrs, d.nodeAttrs,
d.nodeLocality, d.conf.LeasePreferences) {
case leasePreferencesViolating:
case LeasePreferencesViolating:
violatingLeasePreferences = true
case leasePreferencesLessPreferred:
case LeasePreferencesLessPreferred:
lessPreferredLease = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kv/kvserver/replica_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,14 @@ func (r *Replica) leasePostApplyLocked(
// lease is valid and owned by the replica before processing.
if iAmTheLeaseHolder && leaseChangingHands &&
LeaseCheckPreferencesOnAcquisitionEnabled.Get(&r.store.cfg.Settings.SV) {
preferenceStatus := checkStoreAgainstLeasePreferences(r.store.StoreID(), r.store.Attrs(),
preferenceStatus := CheckStoreAgainstLeasePreferences(r.store.StoreID(), r.store.Attrs(),
r.store.nodeDesc.Attrs, r.store.nodeDesc.Locality, r.mu.conf.LeasePreferences)
switch preferenceStatus {
case leasePreferencesOK, leasePreferencesLessPreferred:
case LeasePreferencesOK, LeasePreferencesLessPreferred:
// We could also enqueue the lease when we are a less preferred
// leaseholder, however the replicate queue will eventually get to it and
// we already satisfy _some_ preference.
case leasePreferencesViolating:
case LeasePreferencesViolating:
log.VEventf(ctx, 2,
"acquired lease violates lease preferences, enqueuing for transfer [lease=%v preferences=%v]",
newLease, r.mu.conf.LeasePreferences)
Expand Down
36 changes: 18 additions & 18 deletions pkg/kv/kvserver/replica_range_lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,20 +1550,20 @@ func (r *Replica) hasCorrectLeaseTypeRLocked(lease roachpb.Lease) bool {
return hasExpirationLease == r.shouldUseExpirationLeaseRLocked()
}

// leasePreferencesStatus represents the state of satisfying lease preferences.
type leasePreferencesStatus int
// LeasePreferencesStatus represents the state of satisfying lease preferences.
type LeasePreferencesStatus int

const (
_ leasePreferencesStatus = iota
// leasePreferencesViolating indicates the checked store does not satisfy any
_ LeasePreferencesStatus = iota
// LeasePreferencesViolating indicates the checked store does not satisfy any
// lease preference applied.
leasePreferencesViolating
// leasePreferencesLessPreferred indicates the checked store satisfies _some_
LeasePreferencesViolating
// LeasePreferencesLessPreferred indicates the checked store satisfies _some_
// preference, however not the most preferred.
leasePreferencesLessPreferred
// leasePreferencesOK indicates the checked store satisfies the first
LeasePreferencesLessPreferred
// LeasePreferencesOK indicates the checked store satisfies the first
// preference, or no lease preferences are applied.
leasePreferencesOK
LeasePreferencesOK
)

// LeaseViolatesPreferences checks if this replica owns the lease and if it
Expand All @@ -1584,30 +1584,30 @@ func (r *Replica) LeaseViolatesPreferences(ctx context.Context, conf *roachpb.Sp
storeAttrs := r.store.Attrs()
nodeAttrs := r.store.nodeDesc.Attrs
nodeLocality := r.store.nodeDesc.Locality
preferenceStatus := checkStoreAgainstLeasePreferences(
preferenceStatus := CheckStoreAgainstLeasePreferences(
storeID, storeAttrs, nodeAttrs, nodeLocality, preferences)
return preferenceStatus == leasePreferencesViolating
return preferenceStatus == LeasePreferencesViolating
}

// checkStoreAgainstLeasePreferences returns whether the given store would
// CheckStoreAgainstLeasePreferences returns whether the given store would
// violate, be less preferred or ok, leaseholder, according the the lease
// preferences.
func checkStoreAgainstLeasePreferences(
func CheckStoreAgainstLeasePreferences(
storeID roachpb.StoreID,
storeAttrs, nodeAttrs roachpb.Attributes,
nodeLocality roachpb.Locality,
preferences []roachpb.LeasePreference,
) leasePreferencesStatus {
) LeasePreferencesStatus {
if len(preferences) == 0 {
return leasePreferencesOK
return LeasePreferencesOK
}
for i, preference := range preferences {
if constraint.CheckConjunction(storeAttrs, nodeAttrs, nodeLocality, preference.Constraints) {
if i > 0 {
return leasePreferencesLessPreferred
return LeasePreferencesLessPreferred
}
return leasePreferencesOK
return LeasePreferencesOK
}
}
return leasePreferencesViolating
return LeasePreferencesViolating
}

0 comments on commit 9319d63

Please sign in to comment.