Skip to content

Commit 1cf568a

Browse files
Ring: read one DynamoDB row for self-only ingester readiness (#7984)
* Ring: read one DynamoDB row for self-only ingester readiness When readiness only checks the local ingester, query DynamoDB with the ring key and the ingester ID. Use the time of that read to check heartbeat age. Keep the whole-ring read for the full readiness check and as a fallback for stores without a single-instance lookup. Exercise the query keys, fallback, and readiness decisions in unit tests. Signed-off-by: Hudson Humphries <hudsohum@amazon.com> * Add the CHANGELOG entry Signed-off-by: Hudson Humphries <hudsohum@amazon.com> * Ring: make GetInstance a kv.Client method Every KV client now implements GetInstance. The DynamoDB client stores a MultiKey value as one row per ID and reads the one row. Consul, etcd and memberlist keep the value as one entry and read the whole value, like Get. This replaces the optional InstanceGetter interface and the kv.GetInstance helper. GetInstance returns no time, so its signature matches Get. The self-only readiness check measures the heartbeat age from the time of the check. Signed-off-by: Hudson Humphries <hudsohum@amazon.com> * Ring: correct the GetInstance and LastUpdateTime comments The DynamoDB client also sets LastUpdateTime after a CAS and a watch poll, not only after a read of the whole ring. A one-row read returns at most the one entry, with the type that Get returns. Signed-off-by: Hudson Humphries <hudsohum@amazon.com> --------- Signed-off-by: Hudson Humphries <hudsohum@amazon.com>
1 parent a754316 commit 1cf568a

15 files changed

Lines changed: 310 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* [ENHANCEMENT] Ruler: Add new limit `-ruler.list-rules-max-rules` on the total number of rules returned by the Prometheus ListRules API. Responses exceeding the limit are truncated on a rule group boundary and return a `groupNextToken` for retrieving the remaining groups. A rule group is never split, so a single group larger than the limit is still returned whole. Defaults to 0, which is unlimited. #7785
3030
* [ENHANCEMENT] Compactor: Add the `cortex_compactor_oldest_level_1_block_offset_seconds` metric, reporting per tenant how long ago the oldest level 1 block still listed in the bucket index was uploaded. Blocks marked for deletion or for no compaction are excluded. The bucket index entry now carries the block's compaction level; an entry written before this reports an unknown level and is skipped. #7959
3131
* [ENHANCEMENT] Ingester: Renewing tokens via `/ingester/renewTokens` no longer makes queries using shuffle sharding with lookback miss data, so tokens can be renewed in more than one zone at a time. For the lookback period after a renewal, the renewed ingester is treated as newly registered, so subrings reaching its new tokens keep including the previous owners, and it is included in every tenant's subring, since it may still hold data of tenants whose subring it left. #7983
32+
* [ENHANCEMENT] Ring: Read only the ingester's own entry from the DynamoDB KV store in the readiness check when `-ingester.readiness-check-ring-health=false`, instead of the whole ring. #7984
3233
* [BUGFIX] Update prometheus alertmanager version to v0.34.1 to apply upstream inhibit rule bug fixes. #7877
3334
* [BUGFIX] Tenant Federation: Fix regex tenant federation not working in single binary mode, and failing with `multiple org IDs present` when a matched tenant ID contains regex metacharacters and also matches other tenants as a regex. #7869
3435
* [BUGFIX] Query Frontend: Fix the data selection time window computed for an instant query sent without a `time` parameter. The default evaluation time was passed to `util.ParseTimeParam` in milliseconds, but that helper formats the default and parses it as seconds, so the value overflowed `UnixNano()` and wrapped to a timestamp decades away from `now`. This made the `data_select_min_time` and `data_select_max_time` query stats wrong, and stopped the `time_window` query attribute of query priority and query rejection from matching such queries correctly. #7880

‎pkg/ring/kv/client.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ type Client interface {
9797
// If the key does not exist, Get will return nil and no error.
9898
Get(ctx context.Context, key string) (any, error)
9999

100+
// GetInstance reads the entry instanceID of the value stored under key, such
101+
// as one member of a ring. The value has the type that Get returns. A store
102+
// that keeps a codec.MultiKey value as one entry per ID reads only that
103+
// entry, and the value holds at most that entry. Any other store reads the
104+
// whole value, like Get. The read might not refresh LastUpdateTime.
105+
GetInstance(ctx context.Context, key, instanceID string) (any, error)
106+
100107
// Delete a specific key. Deletions are best-effort and no error will
101108
// be returned if the key does not exist.
102109
Delete(ctx context.Context, key string) error

‎pkg/ring/kv/consul/client.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
401401
return c.codec.Decode(kvp.Value)
402402
}
403403

404+
// GetInstance implements kv.GetInstance. Consul keeps the value as one entry, so it reads the whole value.
405+
func (c *Client) GetInstance(ctx context.Context, key, _ string) (any, error) {
406+
return c.Get(ctx, key)
407+
}
408+
404409
// Delete implements kv.Delete.
405410
func (c *Client) Delete(ctx context.Context, key string) error {
406411
_, err := c.kv.Delete(key, writeOptions.WithContext(ctx))

‎pkg/ring/kv/dynamodb/client.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dynamodb
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
"sync"
@@ -114,6 +115,24 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
114115
return res, nil
115116
}
116117

118+
// GetInstance reads the one row of key whose sort key is instanceID. It did not
119+
// observe the other rows, so it leaves the data and the time that the client
120+
// keeps for the whole key (see LastUpdateTime) as they are.
121+
func (c *Client) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
122+
if instanceID == "" {
123+
// An empty sort key would read every row of the key.
124+
return nil, errors.New("instance ID is empty")
125+
}
126+
127+
resp, _, err := c.kv.Query(ctx, dynamodbKey{primaryKey: key, sortKey: instanceID}, false)
128+
if err != nil {
129+
level.Warn(c.logger).Log("msg", "error GetInstance", "key", key, "instance", instanceID, "err", err)
130+
return nil, err
131+
}
132+
133+
return c.decodeMultikey(resp)
134+
}
135+
117136
func (c *Client) Delete(ctx context.Context, key string) error {
118137
resp, _, err := c.kv.Query(ctx, dynamodbKey{primaryKey: key}, false)
119138
if err != nil {

‎pkg/ring/kv/dynamodb/client_test.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
9+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
810
"github.com/go-kit/log"
911
"github.com/gogo/protobuf/proto"
1012
"github.com/pkg/errors"
@@ -373,6 +375,57 @@ func Test_UpdateStaleData(t *testing.T) {
373375

374376
}
375377

378+
func Test_GetInstance(t *testing.T) {
379+
equals := func(value string) types.Condition {
380+
return types.Condition{
381+
ComparisonOperator: types.ComparisonOperatorEq,
382+
AttributeValueList: []types.AttributeValue{&types.AttributeValueMemberS{Value: value}},
383+
}
384+
}
385+
var conditions map[string]types.Condition
386+
ddbClientMock := &mockDynamodb{
387+
query: func(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error) {
388+
conditions = input.KeyConditions
389+
return &dynamodb.QueryOutput{}, nil
390+
},
391+
}
392+
// The instrumentation layer and the real Query, down to the DynamoDB API.
393+
ddb := dynamodbInstrumentation{
394+
kv: *newDynamodbClientMock("TEST", ddbClientMock, 0),
395+
ddbMetrics: newDynamoDbMetrics(prometheus.NewPedanticRegistry()),
396+
}
397+
codecMock := &CodecMock{}
398+
descMock := &DescMock{}
399+
c := NewClientMock(ddb, codecMock, TestLogger{}, prometheus.NewPedanticRegistry(), defaultPullTime, defaultBackoff)
400+
codecMock.On("DecodeMultiKey").Return(descMock, nil)
401+
402+
value, err := c.GetInstance(context.TODO(), key, "ing-a")
403+
404+
require.NoError(t, err)
405+
require.EqualValues(t, descMock, value)
406+
require.Equal(t, map[string]types.Condition{primaryKey: equals(key), sortKey: equals("ing-a")}, conditions)
407+
408+
// The read saw one row, so the client keeps neither data nor a time for the whole key.
409+
require.Empty(t, c.staleData)
410+
require.True(t, c.LastUpdateTime(key).IsZero())
411+
412+
// A read of the whole key still has no condition on the sort key.
413+
_, err = c.Get(context.TODO(), key)
414+
require.NoError(t, err)
415+
require.Equal(t, map[string]types.Condition{primaryKey: equals(key)}, conditions)
416+
}
417+
418+
func Test_GetInstance_EmptyInstanceID(t *testing.T) {
419+
ddbMock := NewDynamodbClientMock()
420+
c := NewClientMock(ddbMock, &CodecMock{}, TestLogger{}, prometheus.NewPedanticRegistry(), defaultPullTime, defaultBackoff)
421+
422+
// An empty sort key would read every row of the key.
423+
_, err := c.GetInstance(context.TODO(), key, "")
424+
425+
require.Error(t, err)
426+
ddbMock.AssertNotCalled(t, "Query")
427+
}
428+
376429
func Test_DynamodbKVWithTimeout(t *testing.T) {
377430
ddbMock := NewDynamodbClientMock()
378431
// Backend has delay of 5s while the client timeout is 1s.

‎pkg/ring/kv/dynamodb/dynamodb.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ func (kv dynamodbKV) Query(ctx context.Context, key dynamodbKey, isPrefix bool)
159159
},
160160
}
161161

162+
// With a sort key, the query returns only that row and not every row of the key.
163+
if key.sortKey != "" {
164+
input.KeyConditions[sortKey] = types.Condition{
165+
ComparisonOperator: types.ComparisonOperatorEq,
166+
AttributeValueList: []types.AttributeValue{
167+
&types.AttributeValueMemberS{Value: key.sortKey},
168+
},
169+
}
170+
}
171+
162172
paginator := dynamodb.NewQueryPaginator(kv.ddbClient, input)
163173

164174
for paginator.HasMorePages() {

‎pkg/ring/kv/etcd/etcd.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
314314
return c.codec.Decode(resp.Kvs[0].Value)
315315
}
316316

317+
// GetInstance implements kv.Client. etcd keeps the value as one entry, so it reads the whole value.
318+
func (c *Client) GetInstance(ctx context.Context, key, _ string) (any, error) {
319+
return c.Get(ctx, key)
320+
}
321+
317322
// Delete implements kv.Client.
318323
func (c *Client) Delete(ctx context.Context, key string) error {
319324
opsCtx, cancel := c.opsContext(ctx)

‎pkg/ring/kv/get_instance_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package kv
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/go-kit/log"
8+
"github.com/prometheus/client_golang/prometheus"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// instanceStore records the reads of one instance.
13+
type instanceStore struct {
14+
Client
15+
instanceReads [][2]string
16+
}
17+
18+
func (s *instanceStore) GetInstance(_ context.Context, key, instanceID string) (any, error) {
19+
s.instanceReads = append(s.instanceReads, [2]string{key, instanceID})
20+
return "one instance", nil
21+
}
22+
23+
// The layers that NewClient adds around a multi store with a prefix and metrics
24+
// pass the read of one instance on to the store.
25+
func TestGetInstance_Wrappers(t *testing.T) {
26+
store := &instanceStore{}
27+
multi := NewMultiClient(MultiConfig{}, []kvclient{{client: store, name: "primary"}}, log.NewNopLogger(), nil)
28+
client := newMetricsClient("multi", PrefixClient(multi, "ingester/"), prometheus.NewRegistry())
29+
30+
value, err := client.GetInstance(context.Background(), "ring", "ing-a")
31+
32+
require.NoError(t, err)
33+
require.Equal(t, "one instance", value)
34+
require.Equal(t, [][2]string{{"ingester/ring", "ing-a"}}, store.instanceReads)
35+
}

‎pkg/ring/kv/memberlist/memberlist_client.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
7272
return c.kv.Get(key, c.codec)
7373
}
7474

75+
// GetInstance is part of kv.Client interface. Memberlist keeps the value as one entry, so it reads the whole value.
76+
func (c *Client) GetInstance(ctx context.Context, key, _ string) (any, error) {
77+
return c.Get(ctx, key)
78+
}
79+
7580
// Delete is part of kv.Client interface.
7681
func (c *Client) Delete(ctx context.Context, key string) error {
7782
err := c.awaitKVRunningOrStopping(ctx)

‎pkg/ring/kv/metrics.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ func (m metrics) Get(ctx context.Context, key string) (any, error) {
8686
return result, err
8787
}
8888

89+
// GetInstance is tracked as a GET, like Get.
90+
func (m metrics) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
91+
var result any
92+
err := instrument.CollectedRequest(ctx, "GET", m.requestDuration, instrument.ErrorCode, func(ctx context.Context) error {
93+
var err error
94+
result, err = m.c.GetInstance(ctx, key, instanceID)
95+
return err
96+
})
97+
return result, err
98+
}
99+
89100
func (m metrics) Delete(ctx context.Context, key string) error {
90101
err := instrument.CollectedRequest(ctx, "Delete", m.requestDuration, instrument.ErrorCode, func(ctx context.Context) error {
91102
return m.c.Delete(ctx, key)

0 commit comments

Comments
 (0)