MCPcopy
hub / github.com/grafana/dskit / getCachedShuffledSubringWithLookback

Method getCachedShuffledSubringWithLookback

ring/ring.go:1345–1384  ·  view source on GitHub ↗
(identifier string, size int, lookbackPeriod time.Duration, now time.Time)

Source from the content-addressed store, hash-verified

1343}
1344
1345func (r *Ring) getCachedShuffledSubringWithLookback(identifier string, size int, lookbackPeriod time.Duration, now time.Time) *Ring {
1346 if r.cfg.SubringCacheDisabled {
1347 return nil
1348 }
1349
1350 r.mtx.RLock()
1351 defer r.mtx.RUnlock()
1352
1353 cached, ok := r.shuffledSubringWithLookbackCache[subringCacheKey{identifier: identifier, shardSize: size, lookbackPeriod: lookbackPeriod}]
1354 if !ok {
1355 return nil
1356 }
1357
1358 lookbackWindowStart := now.Add(-lookbackPeriod).Unix()
1359 if lookbackWindowStart < cached.validForLookbackWindowsStartingAfter || lookbackWindowStart > cached.validForLookbackWindowsStartingBefore {
1360 // The cached subring is not valid for the lookback window that has been requested.
1361 return nil
1362 }
1363
1364 cachedSubring := cached.subring
1365
1366 // No need to update the cached subring if it is the original ring itself.
1367 if r == cachedSubring {
1368 return cachedSubring
1369 }
1370
1371 cachedSubring.mtx.Lock()
1372 defer cachedSubring.mtx.Unlock()
1373
1374 // Update instance states and timestamps. We know that the topology is the same,
1375 // so zones and tokens are equal.
1376 for name, cachedIng := range cachedSubring.ringDesc.Ingesters {
1377 ing := r.ringDesc.Ingesters[name]
1378 cachedIng.State = ing.State
1379 cachedIng.Timestamp = ing.Timestamp
1380 cachedSubring.ringDesc.Ingesters[name] = cachedIng
1381 }
1382
1383 return cachedSubring
1384}
1385
1386func (r *Ring) setCachedShuffledSubringWithLookback(identifier string, size int, lookbackPeriod time.Duration, now time.Time, subring *Ring) {
1387 if subring == nil || r.cfg.SubringCacheDisabled {

Callers 1

Calls 1

AddMethod · 0.65

Tested by

no test coverage detected