app/{vmalert,vmagent}: improve the distribution of scrape offsets among targets / rules

Previously only the lower part of 64-bit hash was used for calculating the offset.
This may give uneven distribution in some cases. So let's use all the available 64 bits from the hash
for calculating the offset.
This commit is contained in:
Aliaksandr Valialkin 2021-10-27 19:59:13 +03:00
parent e3a91b186a
commit 6608705652
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 4 additions and 4 deletions

View File

@ -226,7 +226,7 @@ func (g *Group) start(ctx context.Context, nts []notifier.Notifier, rw *remotewr
// Spread group rules evaluation over time in order to reduce load on VictoriaMetrics.
if !skipRandSleepOnGroupStart {
randSleep := uint64(float64(g.Interval) * (float64(uint32(g.ID())) / (1 << 32)))
randSleep := uint64(float64(g.Interval) * (float64(g.ID()) / (1 << 64)))
sleepOffset := uint64(time.Now().UnixNano()) % uint64(g.Interval)
if randSleep < sleepOffset {
randSleep += uint64(g.Interval)

View File

@ -986,7 +986,7 @@ func badgeState(state string) string {
func streambadgeRestored(qw422016 *qt422016.Writer) {
//line app/vmalert/web.qtpl:303
qw422016.N().S(`
<span class="badge bg-warning text-dark" title="Alert state was restored after reload from remote storage">restored</span>
<span class="badge bg-warning text-dark" title="Alert state was restored after the service restart from remote storage">restored</span>
`)
//line app/vmalert/web.qtpl:305
}

View File

@ -270,8 +270,8 @@ func (sw *scrapeWork) run(stopCh <-chan struct{}) {
// This also makes consistent scrape times across restarts
// for a target with the same ScrapeURL and labels.
key := fmt.Sprintf("ScrapeURL=%s, Labels=%s", sw.Config.ScrapeURL, sw.Config.LabelsString())
h := uint32(xxhash.Sum64(bytesutil.ToUnsafeBytes(key)))
randSleep = uint64(float64(scrapeInterval) * (float64(h) / (1 << 32)))
h := xxhash.Sum64(bytesutil.ToUnsafeBytes(key))
randSleep = uint64(float64(scrapeInterval) * (float64(h) / (1 << 64)))
sleepOffset := uint64(time.Now().UnixNano()) % uint64(scrapeInterval)
if randSleep < sleepOffset {
randSleep += uint64(scrapeInterval)