From 891e7a0645a76756afcf15b3195029a1e51b8a05 Mon Sep 17 00:00:00 2001 From: Austen McClernon Date: Thu, 5 Jan 2023 15:29:36 +0000 Subject: [PATCH] split: init replica lb splitter with global rand 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 --- pkg/kv/kvserver/replica_init.go | 4 +--- pkg/kv/kvserver/split/decider.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/kv/kvserver/replica_init.go b/pkg/kv/kvserver/replica_init.go index d1c59716cd0a..28a4eea7d9d6 100644 --- a/pkg/kv/kvserver/replica_init.go +++ b/pkg/kv/kvserver/replica_init.go @@ -13,7 +13,6 @@ package kvserver import ( "bytes" "context" - "math/rand" "time" "github.com/cockroachdb/cockroach/pkg/keys" @@ -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) diff --git a/pkg/kv/kvserver/split/decider.go b/pkg/kv/kvserver/split/decider.go index 0c5ea9165210..e34937bb4641 100644 --- a/pkg/kv/kvserver/split/decider.go +++ b/pkg/kv/kvserver/split/decider.go @@ -15,6 +15,7 @@ package split import ( "context" "fmt" + "math/rand" "time" "github.com/cockroachdb/cockroach/pkg/keys" @@ -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",