Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/ring/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 21 additions & 6 deletions pkg/ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/ring/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down