-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
31902: workload: add zipfian distribution option to kv r=ridwanmsharif a=ridwanmsharif Adds a workload distribution that spreads load according to a Zipfian distribution. This is part of the experimentation with load based splitting. Release note: None Co-authored-by: Ridwan Sharif <[email protected]>
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2009 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// W.Hormann, G.Derflinger: | ||
// "Rejection-Inversion to Generate Variates | ||
// from Monotone Discrete Distributions" | ||
// http://eeyore.wu-wien.ac.at/papers/96-04-04.wh-der.ps.gz | ||
|
||
package kv | ||
|
||
import ( | ||
"math" | ||
"math/rand" | ||
) | ||
|
||
// A zipf generates Zipf distributed variates. | ||
// This was added here from math/rand so we could | ||
// have the Zipfian distribution give us a deterministic | ||
// result based on the read key so we don't read missing | ||
// entries. | ||
// | ||
// Our changes involve being supplied with | ||
// a seeded rand object during the time of retrieval instead | ||
// of a rand object during creation. | ||
type zipf struct { | ||
imax float64 | ||
v float64 | ||
q float64 | ||
s float64 | ||
oneminusQ float64 | ||
oneminusQinv float64 | ||
hxm float64 | ||
hx0minusHxm float64 | ||
} | ||
|
||
func (z *zipf) h(x float64) float64 { | ||
return math.Exp(z.oneminusQ*math.Log(z.v+x)) * z.oneminusQinv | ||
} | ||
|
||
func (z *zipf) hinv(x float64) float64 { | ||
return math.Exp(z.oneminusQinv*math.Log(z.oneminusQ*x)) - z.v | ||
} | ||
|
||
// newZipf returns a Zipf variate generator. | ||
// The generator generates values k ∈ [0, imax] | ||
// such that P(k) is proportional to (v + k) ** (-s). | ||
// Requirements: s > 1 and v >= 1. | ||
func newZipf(s float64, v float64, imax uint64) *zipf { | ||
z := new(zipf) | ||
if s <= 1.0 || v < 1 { | ||
return nil | ||
} | ||
z.imax = float64(imax) | ||
z.v = v | ||
z.q = s | ||
z.oneminusQ = 1.0 - z.q | ||
z.oneminusQinv = 1.0 / z.oneminusQ | ||
z.hxm = z.h(z.imax + 0.5) | ||
z.hx0minusHxm = z.h(0.5) - math.Exp(math.Log(z.v)*(-z.q)) - z.hxm | ||
z.s = 1 - z.hinv(z.h(1.5)-math.Exp(-z.q*math.Log(z.v+1.0))) | ||
return z | ||
} | ||
|
||
// Uint64 returns a value drawn from the Zipf distribution described | ||
// by the Zipf object. | ||
func (z *zipf) Uint64(random *rand.Rand) uint64 { | ||
if z == nil { | ||
panic("rand: nil Zipf") | ||
} | ||
k := 0.0 | ||
|
||
for { | ||
r := random.Float64() // r in [0.0, 1.0] | ||
ur := z.hxm + r*z.hx0minusHxm | ||
x := z.hinv(ur) | ||
k = math.Floor(x + 0.5) | ||
if k-x <= z.s { | ||
break | ||
} | ||
if ur >= z.h(k+0.5)-math.Exp(-math.Log(k+z.v)*z.q) { | ||
break | ||
} | ||
} | ||
return uint64(int64(k)) | ||
} |