Skip to content

Commit bc9fdcd

Browse files
committed
allocator: hoist some calls to Locality
store.Locality() allocates. Previously we were calling this inside an double-nested loop in diversityRebalanceScore which itself is called in a loop. This is probably just a drop in the bucket given how much is going on in the callers of this function, but perhaps it helps a bit: BenchmarkRebalanceToDiversityScore 427075 2734 ns/op 1920 B/op 12 allocs/op BenchmarkRebalanceToDiversityScore 904476 1443 ns/op 160 B/op 1 allocs/op Informs #147800 Release note: None
1 parent e883406 commit bc9fdcd

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

pkg/kv/kvserver/allocator/allocatorimpl/allocator_scorer.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ func rankedCandidateListForAllocation(
11591159
continue
11601160
}
11611161

1162-
diversityScore := diversityAllocateScore(s, existingStoreLocalities)
1162+
diversityScore := diversityAllocateScore(s.Locality(), existingStoreLocalities)
11631163
balanceScore := options.balanceScore(validStoreList, s.Capacity)
11641164
var hasNonVoter bool
11651165
if targetType == VoterTarget {
@@ -1656,9 +1656,10 @@ func rankedCandidateListForRebalancing(
16561656
// this stage, in additon to hard checks and validation.
16571657
// TODO(kvoli,ayushshah15): Refactor this to make it harder to
16581658
// inadvertently break the invariant above,
1659+
locality := store.Locality()
16591660
constraintsOK, necessary, voterNecessary := rebalanceConstraintsChecker(store, existing.store)
16601661
diversityScore := diversityRebalanceFromScore(
1661-
store, existing.store.StoreID, existingStoreLocalities)
1662+
locality, existing.store.StoreID, existingStoreLocalities)
16621663
cand := candidate{
16631664
store: store,
16641665
valid: constraintsOK,
@@ -1676,7 +1677,7 @@ func rankedCandidateListForRebalancing(
16761677
"s%d: should-rebalance(necessary/diversity=s%d): oldNecessary:%t, newNecessary:%t, "+
16771678
"oldDiversity:%f, newDiversity:%f, locality:%q",
16781679
existing.store.StoreID, store.StoreID, existing.necessary, cand.necessary,
1679-
existing.diversityScore, cand.diversityScore, store.Locality())
1680+
existing.diversityScore, cand.diversityScore, locality)
16801681
}
16811682
}
16821683
}
@@ -2236,7 +2237,7 @@ func RangeDiversityScore(existingStoreLocalities map[roachpb.StoreID]roachpb.Loc
22362237
// desirable it would be to add a replica to store. A higher score means the
22372238
// store is a better fit.
22382239
func diversityAllocateScore(
2239-
store roachpb.StoreDescriptor, existingStoreLocalities map[roachpb.StoreID]roachpb.Locality,
2240+
storeLocality roachpb.Locality, existingStoreLocalities map[roachpb.StoreID]roachpb.Locality,
22402241
) float64 {
22412242
var sumScore float64
22422243
var numSamples int
@@ -2245,7 +2246,7 @@ func diversityAllocateScore(
22452246
// consider adding the pairwise average diversity of the existing replicas
22462247
// is the same.
22472248
for _, locality := range existingStoreLocalities {
2248-
newScore := store.Locality().DiversityScore(locality)
2249+
newScore := storeLocality.DiversityScore(locality)
22492250
sumScore += newScore
22502251
numSamples++
22512252
}
@@ -2296,8 +2297,9 @@ func diversityRebalanceScore(
22962297
var maxScore float64
22972298
// For every existing node, calculate what the diversity score would be if we
22982299
// remove that node's replica to replace it with one on the provided store.
2300+
storeLocality := store.Locality()
22992301
for removedStoreID := range existingStoreLocalities {
2300-
score := diversityRebalanceFromScore(store, removedStoreID, existingStoreLocalities)
2302+
score := diversityRebalanceFromScore(storeLocality, removedStoreID, existingStoreLocalities)
23012303
if score > maxScore {
23022304
maxScore = score
23032305
}
@@ -2312,7 +2314,7 @@ func diversityRebalanceScore(
23122314
// A higher score indicates that the provided store is a better fit for the
23132315
// range.
23142316
func diversityRebalanceFromScore(
2315-
store roachpb.StoreDescriptor,
2317+
storeLocality roachpb.Locality,
23162318
fromStoreID roachpb.StoreID,
23172319
existingStoreLocalities map[roachpb.StoreID]roachpb.Locality,
23182320
) float64 {
@@ -2324,7 +2326,7 @@ func diversityRebalanceFromScore(
23242326
if storeID == fromStoreID {
23252327
continue
23262328
}
2327-
newScore := store.Locality().DiversityScore(locality)
2329+
newScore := storeLocality.DiversityScore(locality)
23282330
sumScore += newScore
23292331
numSamples++
23302332
for otherStoreID, otherLocality := range existingStoreLocalities {

pkg/kv/kvserver/allocator/allocatorimpl/allocator_scorer_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ func TestAllocateDiversityScore(t *testing.T) {
17671767
continue
17681768
}
17691769
var score storeScore
1770-
actualScore := diversityAllocateScore(s, existingStoreLocalities)
1770+
actualScore := diversityAllocateScore(s.Locality(), existingStoreLocalities)
17711771
score.storeID = s.StoreID
17721772
score.score = actualScore
17731773
scores = append(scores, score)
@@ -1858,6 +1858,23 @@ func TestRebalanceToDiversityScore(t *testing.T) {
18581858
}
18591859
}
18601860

1861+
func BenchmarkRebalanceToDiversityScore(b *testing.B) {
1862+
existingStoreLocalities := make(map[roachpb.StoreID]roachpb.Locality, len(testStores))
1863+
store := testStores[testStoreUSa15]
1864+
for sID := range testStores {
1865+
if sID == testStoreUSa15 {
1866+
continue
1867+
}
1868+
existingStoreLocalities[testStores[sID].StoreID] = testStores[sID].Locality()
1869+
}
1870+
1871+
b.ResetTimer()
1872+
sum := float64(0)
1873+
for i := 0; i < b.N; i++ {
1874+
sum += diversityRebalanceScore(store, existingStoreLocalities)
1875+
}
1876+
}
1877+
18611878
func TestRemovalDiversityScore(t *testing.T) {
18621879
defer leaktest.AfterTest(t)()
18631880
defer log.Scope(t).Close(t)
@@ -1973,7 +1990,7 @@ func TestDiversityScoreEquivalence(t *testing.T) {
19731990
s := testStores[storeID]
19741991
fromStoreID := s.StoreID
19751992
s.StoreID = 99
1976-
rebalanceScore := diversityRebalanceFromScore(s, fromStoreID, existingLocalities)
1993+
rebalanceScore := diversityRebalanceFromScore(s.Locality(), fromStoreID, existingLocalities)
19771994
if a, e := rebalanceScore, tc.expected; !scoresAlmostEqual(a, e) {
19781995
t.Errorf("diversityRebalanceFromScore(%v, %d, %v) got %f, want %f",
19791996
s, fromStoreID, existingLocalities, a, e)

0 commit comments

Comments
 (0)