Trigger compaction probabilistically to avoid lots of concurrent attempts
@@ -6,6 +6,8 @@ package nbs
import (
"fmt"
+ "math"
+ "math/rand"
"os"
"path"
"sort"
@@ -407,6 +409,8 @@ var (
errOptimisticLockFailedTables = fmt.Errorf("Tables changed")
)
+var compactThreshRand = rand.New(rand.NewSource(time.Now().UnixNano()))
+
func (nbs *NomsBlockStore) updateManifest(current, last hash.Hash) error {
nbs.mu.Lock()
defer nbs.mu.Unlock()
@@ -421,7 +425,14 @@ func (nbs *NomsBlockStore) updateManifest(current, last hash.Hash) error {
candidate := nbs.tables
var compactees chunkSources
- if candidate.Size() > nbs.maxTables {
+
+ shouldCompact := func() bool {
+ // As the number of tables grows from 1 to maxTables, the probability of compacting, grows from 0 to 1
+ thresh := float64(math.Max(0, float64(len(candidate.upstream)-1))) / float64(nbs.maxTables-1)
+ return compactThreshRand.Float64() < thresh
+ }
+
+ if shouldCompact() {
candidate, compactees = candidate.Compact() // Compact() must only compact upstream tables (BUG 3142)
}
