(identifier string, size int, lookbackPeriod time.Duration, now time.Time, subring *Ring)
| 1384 | } |
| 1385 | |
| 1386 | func (r *Ring) setCachedShuffledSubringWithLookback(identifier string, size int, lookbackPeriod time.Duration, now time.Time, subring *Ring) { |
| 1387 | if subring == nil || r.cfg.SubringCacheDisabled { |
| 1388 | return |
| 1389 | } |
| 1390 | |
| 1391 | lookbackWindowStart := now.Add(-lookbackPeriod).Unix() |
| 1392 | validForLookbackWindowsStartingBefore := int64(math.MaxInt64) |
| 1393 | |
| 1394 | for _, instance := range subring.ringDesc.Ingesters { |
| 1395 | if instance.RegisteredTimestamp >= lookbackWindowStart && instance.RegisteredTimestamp < validForLookbackWindowsStartingBefore { |
| 1396 | validForLookbackWindowsStartingBefore = instance.RegisteredTimestamp |
| 1397 | } |
| 1398 | if instance.ReadOnlyUpdatedTimestamp >= lookbackWindowStart && instance.ReadOnlyUpdatedTimestamp < validForLookbackWindowsStartingBefore { |
| 1399 | validForLookbackWindowsStartingBefore = instance.ReadOnlyUpdatedTimestamp |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | r.mtx.Lock() |
| 1404 | defer r.mtx.Unlock() |
| 1405 | |
| 1406 | // Only cache if *this* ring hasn't changed since computing result |
| 1407 | // (which can happen between releasing the read lock and getting read-write lock). |
| 1408 | // Note that shuffledSubringWithLookbackCache can be only nil when set by test. |
| 1409 | if r.shuffledSubringWithLookbackCache == nil { |
| 1410 | return |
| 1411 | } |
| 1412 | |
| 1413 | if !r.lastTopologyChange.Equal(subring.lastTopologyChange) { |
| 1414 | return |
| 1415 | } |
| 1416 | |
| 1417 | // Only update cache if subring's lookback window starts later than the previously cached subring for this identifier, |
| 1418 | // if there is one. This prevents cache thrashing due to different calls competing if their lookback windows start |
| 1419 | // before and after the time of an instance registering. |
| 1420 | key := subringCacheKey{identifier: identifier, shardSize: size, lookbackPeriod: lookbackPeriod} |
| 1421 | |
| 1422 | if existingEntry, haveCached := r.shuffledSubringWithLookbackCache[key]; !haveCached || existingEntry.validForLookbackWindowsStartingAfter < lookbackWindowStart { |
| 1423 | r.shuffledSubringWithLookbackCache[key] = cachedSubringWithLookback[*Ring]{ |
| 1424 | subring: subring, |
| 1425 | validForLookbackWindowsStartingAfter: lookbackWindowStart, |
| 1426 | validForLookbackWindowsStartingBefore: validForLookbackWindowsStartingBefore, |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | func (r *Ring) CleanupShuffleShardCache(identifier string) { |
| 1432 | if r.cfg.SubringCacheDisabled { |
no test coverage detected