github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/ycsb/skewed_latest_generator.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package ycsb
    12  
    13  import (
    14  	"math/rand"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/util/syncutil"
    17  )
    18  
    19  // SkewedLatestGenerator is a random number generator that generates numbers in
    20  // the range [iMin, iMax], but skews it towards iMax using a zipfian
    21  // distribution.
    22  type SkewedLatestGenerator struct {
    23  	mu struct {
    24  		syncutil.Mutex
    25  		iMax    uint64
    26  		zipfGen *ZipfGenerator
    27  	}
    28  }
    29  
    30  // NewSkewedLatestGenerator constructs a new SkewedLatestGenerator with the
    31  // given parameters. It returns an error if the parameters are outside the
    32  // accepted range.
    33  func NewSkewedLatestGenerator(
    34  	rng *rand.Rand, iMin, iMax uint64, theta float64, verbose bool,
    35  ) (*SkewedLatestGenerator, error) {
    36  
    37  	z := SkewedLatestGenerator{}
    38  	z.mu.iMax = iMax
    39  	zipfGen, err := NewZipfGenerator(rng, 0, iMax-iMin, theta, verbose)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	z.mu.zipfGen = zipfGen
    44  
    45  	return &z, nil
    46  }
    47  
    48  // IncrementIMax increments iMax by count.
    49  func (z *SkewedLatestGenerator) IncrementIMax(count uint64) error {
    50  	z.mu.Lock()
    51  	defer z.mu.Unlock()
    52  	z.mu.iMax += count
    53  	return z.mu.zipfGen.IncrementIMax(count)
    54  }
    55  
    56  // Uint64 returns a random Uint64 between iMin and iMax, where keys near iMax
    57  // are most likely to be drawn.
    58  func (z *SkewedLatestGenerator) Uint64() uint64 {
    59  	z.mu.Lock()
    60  	defer z.mu.Unlock()
    61  	return z.mu.iMax - z.mu.zipfGen.Uint64()
    62  }