Skip to content

Commit

Permalink
cmd/tempo-vulture: share rand.Rand instance across multiple searches
Browse files Browse the repository at this point in the history
grafana#1760 noted that creating a new rand.Rand every time a
random number is generated causes a poor distrubution of the search
space, where numbers appear 7x more frequently than they do when using a
shared rand.Rand instance.

This is demonstrated using the following Go playground link: https://go.dev/play/p/EkhINRfOO5p

This commit changes Vulture to use a shared rand.Rand instance instead
of creating a new one each time a search or read is performed.
  • Loading branch information
rfratto committed Sep 27, 2022
1 parent 62e7fc1 commit 6310e59
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cmd/tempo-vulture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func main() {
startTime := actualStartTime
tickerWrite := time.NewTicker(tempoWriteBackoffDuration)

r := rand.New(rand.NewSource(actualStartTime.Unix()))

var tickerRead *time.Ticker
if tempoReadBackoffDuration > 0 {
tickerRead = time.NewTicker(tempoReadBackoffDuration)
Expand Down Expand Up @@ -140,7 +142,7 @@ func main() {
go func() {
for now := range tickerRead.C {
var seed time.Time
startTime, seed = selectPastTimestamp(startTime, now, interval, tempoRetentionDuration)
startTime, seed = selectPastTimestamp(startTime, now, interval, tempoRetentionDuration, r)

log := logger.With(
zap.String("org_id", tempoOrgID),
Expand Down Expand Up @@ -173,7 +175,7 @@ func main() {
if tickerSearch != nil {
go func() {
for now := range tickerSearch.C {
_, seed := selectPastTimestamp(startTime, now, interval, tempoRetentionDuration)
_, seed := selectPastTimestamp(startTime, now, interval, tempoRetentionDuration, r)
log := logger.With(
zap.String("org_id", tempoOrgID),
zap.Int64("seed", seed.Unix()),
Expand Down Expand Up @@ -251,7 +253,7 @@ func pushMetrics(metrics traceMetrics) {
metricTracesErrors.WithLabelValues("notfound_search_attribute").Add(float64(metrics.notFoundSearchAttribute))
}

func selectPastTimestamp(start, stop time.Time, interval time.Duration, retention time.Duration) (newStart, ts time.Time) {
func selectPastTimestamp(start, stop time.Time, interval, retention time.Duration, r *rand.Rand) (newStart, ts time.Time) {
oldest := stop.Add(-retention)

if oldest.After(start) {
Expand All @@ -260,7 +262,7 @@ func selectPastTimestamp(start, stop time.Time, interval time.Duration, retentio
newStart = start
}

ts = time.Unix(generateRandomInt(newStart.Unix(), stop.Unix(), rand.New(rand.NewSource(start.Unix()))), 0)
ts = time.Unix(generateRandomInt(newStart.Unix(), stop.Unix(), r), 0)

return newStart.Round(interval), ts.Round(interval)
}
Expand Down

0 comments on commit 6310e59

Please sign in to comment.