From a4e5418f50c264d619265648b3911edbb283f72a Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 12 Jun 2018 21:23:34 +0200 Subject: [PATCH] Randomize period of ComputeInternalStateColumnStats to avoid CPU peaks --- blockbook.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/blockbook.go b/blockbook.go index 95bd4582..4743f457 100644 --- a/blockbook.go +++ b/blockbook.go @@ -4,6 +4,7 @@ import ( "context" "flag" "log" + "math/rand" "os" "os/signal" "strings" @@ -123,6 +124,8 @@ func main() { defer glog.Flush() + rand.Seed(time.Now().UTC().UnixNano()) + chanOsSignal = make(chan os.Signal, 1) signal.Notify(chanOsSignal, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM) @@ -417,11 +420,13 @@ func storeInternalStateLoop() { close(stopCompute) close(chanStoreInternalStateDone) }() - glog.Info("storeInternalStateLoop starting") - lastCompute := time.Now() var computeRunning bool + lastCompute := time.Now() + // randomize the duration between ComputeInternalStateColumnStats to avoid peeks after reboot of machine with multiple blockbooks + computePeriod := 9*time.Hour + time.Duration(rand.Float64()*float64((2*time.Hour).Nanoseconds())) + glog.Info("storeInternalStateLoop starting with internal state compute period ", computePeriod) tickAndDebounce(storeInternalStatePeriodMs*time.Millisecond, (storeInternalStatePeriodMs-1)*time.Millisecond, chanStoreInternalState, func() { - if !computeRunning && lastCompute.Add(10*time.Hour).Before(time.Now()) { + if !computeRunning && lastCompute.Add(computePeriod).Before(time.Now()) { computeRunning = true go func() { err := index.ComputeInternalStateColumnStats(stopCompute)