diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cebf7f928..f32e39e4ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ * [BUGFIX] Distributor: Fix a `panic: send on closed channel` crash during shutdown when `-distributor.num-push-workers` or `-distributor.num-query-workers` is greater than 0. #7964 * [BUGFIX] Ring: Fix `DoBatch` counting a key more than once when more replicas than the quorum succeed, which in rare cases could hide a write error from a key that did not reach quorum. A distributor push now always waits until every series reaches quorum, so it returns the error of a series that misses quorum, and takes as long as its slowest series to reach quorum. #7975 * [BUGFIX] Compactor: Stop downloading the parquet files and the `parquet-converter-mark.json` and `parquet-no-convert-mark.json` markers written by the parquet converter under a block's prefix. Compaction never reads them, so they only used local disk and bandwidth until the compaction job ended. #7972 +* [BUGFIX] Ring: Fix shuffle-sharding subrings treating instances with a stale heartbeat as healthy when using the DynamoDB KV store. #7976 ## 1.22.0 2026-11-19 diff --git a/pkg/ring/http.go b/pkg/ring/http.go index 023b716fef..7b6dfc73e8 100644 --- a/pkg/ring/http.go +++ b/pkg/ring/http.go @@ -154,7 +154,7 @@ func (r *Ring) ServeHTTP(w http.ResponseWriter, req *http.Request) { } sort.Strings(ingesterIDs) - storageLastUpdate := r.KVClient.LastUpdateTime(r.key) + storageLastUpdate := r.lastUpdateTime() var ingesters []ingesterDesc numTokens, ownedByAz := r.countTokensByAz() _, owned := r.countTokens() diff --git a/pkg/ring/ring.go b/pkg/ring/ring.go index 3d87f2d8bc..37ed4e16a4 100644 --- a/pkg/ring/ring.go +++ b/pkg/ring/ring.go @@ -199,6 +199,10 @@ type Ring struct { // When did a set of instances change the last time (instance changing state or heartbeat is ignored for this timestamp). lastTopologyChange time.Time + // Set only on a subring: the KV store's last update time when the subring copied its + // instances from the parent ring. Heartbeat checks compare the copied heartbeats with it. + storageLastUpdate time.Time + // List of zones for which there's at least 1 instance in the ring. This list is guaranteed // to be sorted alphabetically. ringZones []string @@ -415,6 +419,14 @@ func (r *Ring) updateRingState(ringDesc *Desc) { r.updateRingMetrics(rc) } +// lastUpdateTime returns the time to compare heartbeats with. It must be called with r.mtx held. +func (r *Ring) lastUpdateTime() time.Time { + if !r.storageLastUpdate.IsZero() { + return r.storageLastUpdate + } + return r.KVClient.LastUpdateTime(r.key) +} + // Get returns n (or more) instances which form the replicas for the given key. // This implementation guarantees: // - Stability: given the same ring, two invocations returns the same set for same operation. @@ -494,7 +506,7 @@ func (r *Ring) Get(key uint32, op Operation, bufDescs []InstanceDesc, bufHosts [ instances = append(instances, instance) } - healthyInstances, maxFailure, err := r.strategy.Filter(instances, op, r.cfg.ReplicationFactor, r.cfg.HeartbeatTimeout, r.cfg.ZoneAwarenessEnabled, r.KVClient.LastUpdateTime(r.key)) + healthyInstances, maxFailure, err := r.strategy.Filter(instances, op, r.cfg.ReplicationFactor, r.cfg.HeartbeatTimeout, r.cfg.ZoneAwarenessEnabled, r.lastUpdateTime()) if err != nil { return ReplicationSet{}, err } @@ -514,7 +526,7 @@ func (r *Ring) GetAllHealthy(op Operation) (ReplicationSet, error) { return ReplicationSet{}, ErrEmptyRing } - storageLastUpdate := r.KVClient.LastUpdateTime(r.key) + storageLastUpdate := r.lastUpdateTime() instances := make([]InstanceDesc, 0, len(r.ringDesc.Ingesters)) for _, instance := range r.ringDesc.Ingesters { if r.IsHealthy(&instance, op, storageLastUpdate) { @@ -538,7 +550,7 @@ func (r *Ring) GetAllInstanceDescs(op Operation) ([]InstanceDesc, []InstanceDesc } healthyInstances := make([]InstanceDesc, 0, len(r.ringDesc.Ingesters)) unhealthyInstances := make([]InstanceDesc, 0, len(r.ringDesc.Ingesters)) - storageLastUpdate := r.KVClient.LastUpdateTime(r.key) + storageLastUpdate := r.lastUpdateTime() for _, instance := range r.ringDesc.Ingesters { if r.IsHealthy(&instance, op, storageLastUpdate) { healthyInstances = append(healthyInstances, instance) @@ -559,7 +571,7 @@ func (r *Ring) GetInstanceDescsForOperation(op Operation) (map[string]InstanceDe return map[string]InstanceDesc{}, ErrEmptyRing } - storageLastUpdate := r.KVClient.LastUpdateTime(r.key) + storageLastUpdate := r.lastUpdateTime() instanceDescs := make(map[string]InstanceDesc, 0) for id, instance := range r.ringDesc.Ingesters { if r.IsHealthy(&instance, op, storageLastUpdate) { @@ -582,7 +594,7 @@ func (r *Ring) GetReplicationSetForOperation(op Operation) (ReplicationSet, erro // Build the initial replication set, excluding unhealthy instances. healthyInstances := make([]InstanceDesc, 0, len(r.ringDesc.Ingesters)) zoneFailures := make(map[string]struct{}) - storageLastUpdate := r.KVClient.LastUpdateTime(r.key) + storageLastUpdate := r.lastUpdateTime() for _, instance := range r.ringDesc.Ingesters { if r.IsHealthy(&instance, op, storageLastUpdate) { @@ -734,7 +746,7 @@ func (r *Ring) updateRingMetrics(compareResult CompareResult) { for _, instance := range r.ringDesc.Ingesters { s := instance.State.String() - if !r.IsHealthy(&instance, Reporting, r.KVClient.LastUpdateTime(r.key)) { + if !r.IsHealthy(&instance, Reporting, r.lastUpdateTime()) { s = unhealthy } if _, ok := numByStateByZone[s]; !ok { @@ -997,6 +1009,8 @@ func (r *Ring) shuffleShard(identifier string, size int, lookbackPeriod time.Dur // For caching to work, remember this value. lastTopologyChange: r.lastTopologyChange, + + storageLastUpdate: r.lastUpdateTime(), }, shuffleShardExpiry } @@ -1067,6 +1081,7 @@ func (r *Ring) getCachedShuffledSubring(identifier string, size int, lookbackPer cachedIng.Timestamp = ing.Timestamp subring.ringDesc.Ingesters[name] = cachedIng } + subring.storageLastUpdate = r.lastUpdateTime() return subring } diff --git a/pkg/ring/ring_test.go b/pkg/ring/ring_test.go index c6e252453d..b75080efd3 100644 --- a/pkg/ring/ring_test.go +++ b/pkg/ring/ring_test.go @@ -2515,6 +2515,79 @@ func TestRing_ShuffleShardWithLookback(t *testing.T) { } } +// keyedLastUpdateKV returns a last update time only for its own key, and the +// zero time for any other key, as the DynamoDB client does. +type keyedLastUpdateKV struct { + MockClient + key string + lastUpdate time.Time +} + +func (k *keyedLastUpdateKV) LastUpdateTime(key string) time.Time { + if key == k.key { + return k.lastUpdate + } + return time.Time{} +} + +func TestRing_ShuffleShard_ShouldHonorHeartbeatTimeout(t *testing.T) { + const heartbeatTimeout = time.Minute + // A fixed time in the past, so the test fails if health checks use the wall clock. + lastUpdate := time.Unix(1_700_000_000, 0) + stale := lastUpdate.Add(-2 * heartbeatTimeout).Unix() + + // Four instances and a shard size of 3, so ShuffleShard builds a subring. For "user" it + // holds zone-a-1, which has a stale heartbeat, zone-b-1, and zone-c-1. + ringDesc := &Desc{Ingesters: map[string]InstanceDesc{ + "zone-a-1": {Addr: "zone-a-1", Zone: "zone-a", State: ACTIVE, Timestamp: stale, Tokens: []uint32{100}}, + "zone-a-2": {Addr: "zone-a-2", Zone: "zone-a", State: ACTIVE, Timestamp: stale, Tokens: []uint32{200}}, + "zone-b-1": {Addr: "zone-b-1", Zone: "zone-b", State: ACTIVE, Timestamp: lastUpdate.Unix(), Tokens: []uint32{300}}, + "zone-c-1": {Addr: "zone-c-1", Zone: "zone-c", State: ACTIVE, Timestamp: lastUpdate.Unix(), Tokens: []uint32{400}}, + }} + + kvClient := &keyedLastUpdateKV{key: "ring", lastUpdate: lastUpdate} + ring := Ring{ + cfg: Config{HeartbeatTimeout: heartbeatTimeout, ZoneAwarenessEnabled: true, ReplicationFactor: 3}, + key: "ring", + ringDesc: ringDesc, + ringTokens: ringDesc.GetTokens(), + ringTokensByZone: ringDesc.getTokensByZone(), + ringInstanceByToken: ringDesc.getTokensInfo(), + ringZones: getZones(ringDesc.getTokensByZone()), + strategy: NewDefaultReplicationStrategy(), + KVClient: kvClient, + shuffledSubringCache: map[subringCacheKey]*cachedSubring{}, + } + + subring := ring.ShuffleShard("user", 3) + require.True(t, subring.HasInstance("zone-a-1")) + set, err := subring.Get(0, Write, nil, nil, nil) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"zone-b-1", "zone-c-1"}, set.GetAddresses()) + + set, err = ring.ShuffleShardWithLookback("user", 3, time.Hour, lastUpdate).GetReplicationSetForOperation(Read) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"zone-b-1", "zone-c-1"}, set.GetAddresses()) + + // The KV store polls again. zone-a-1 and zone-c-1 heartbeat, and zone-b-1 misses the poll. + kvClient.lastUpdate = lastUpdate.Add(heartbeatTimeout + time.Second) + for _, id := range []string{"zone-a-1", "zone-c-1"} { + instance := ringDesc.Ingesters[id] + instance.Timestamp = kvClient.lastUpdate.Unix() + ringDesc.Ingesters[id] = instance + } + + // A subring held across the poll compares its copied heartbeats with the time they were read. + set, err = subring.Get(0, Write, nil, nil, nil) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"zone-b-1", "zone-c-1"}, set.GetAddresses()) + + // A subring returned from the cache after the poll has the new heartbeats and time. + set, err = ring.ShuffleShard("user", 3).Get(0, Write, nil, nil, nil) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"zone-a-1", "zone-c-1"}, set.GetAddresses()) +} + func TestRing_ShuffleShardWithReadOnlyIngesters(t *testing.T) { g := NewRandomTokenGenerator()