Skip to content

Commit

Permalink
split: init replica lb splitter with global rand
Browse files Browse the repository at this point in the history
In #93838 we started initializing a new seeded rand for use in the load
based splitter's reservoir sampling algorithm. Previously, the splitter
was initialized using the global rand.

`rand.Source` heap allocates on init approximately 4kb. When initialized
per-replica, this is problematic as nodes scale to large replica counts.

This patch replaces initializing a new random source per-replica with
using the global rand instead.

resolves: #94752
resolves: #94737

Release note: None
  • Loading branch information
kvoli committed Jan 5, 2023
1 parent 9317a5f commit 891e7a0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
4 changes: 1 addition & 3 deletions pkg/kv/kvserver/replica_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package kvserver
import (
"bytes"
"context"
"math/rand"
"time"

"github.com/cockroachdb/cockroach/pkg/keys"
Expand Down Expand Up @@ -96,8 +95,7 @@ func newUnloadedReplica(
r.mu.stateLoader = stateloader.Make(desc.RangeID)
r.mu.quiescent = true
r.mu.conf = store.cfg.DefaultSpanConfig
randSource := rand.New(rand.NewSource(timeutil.Now().UnixNano()))
split.Init(&r.loadBasedSplitter, store.cfg.Settings, randSource, func() float64 {
split.Init(&r.loadBasedSplitter, store.cfg.Settings, split.GlobalRandSource(), func() float64 {
return float64(SplitByLoadQPSThreshold.Get(&store.cfg.Settings.SV))
}, func() time.Duration {
return kvserverbase.SplitByLoadMergeDelay.Get(&store.cfg.Settings.SV)
Expand Down
22 changes: 22 additions & 0 deletions pkg/kv/kvserver/split/decider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package split
import (
"context"
"fmt"
"math/rand"
"time"

"github.com/cockroachdb/cockroach/pkg/keys"
Expand Down Expand Up @@ -64,6 +65,27 @@ type RandSource interface {
Intn(n int) int
}

// globalRandSource implements the RandSource interface.
type globalRandSource struct{}

// Float64 returns, as a float64, a pseudo-random number in the half-open
// interval [0.0,1.0) from the RandSource.
func (g globalRandSource) Float64() float64 {
return rand.Float64()
}

// Intn returns, as an int, a non-negative pseudo-random number in the
// half-open interval [0,n).
func (g globalRandSource) Intn(n int) int {
return rand.Intn(n)
}

// GlobalRandSource returns an implementation of the RandSource interface that
// redirects calls to the global rand.
func GlobalRandSource() RandSource {
return globalRandSource{}
}

var enableUnweightedLBSplitFinder = settings.RegisterBoolSetting(
settings.SystemOnly,
"kv.unweighted_lb_split_finder.enabled",
Expand Down

0 comments on commit 891e7a0

Please sign in to comment.