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 @@ -29,6 +29,7 @@
* [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
* [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
* [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
* [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
* [BUGFIX] Update prometheus alertmanager version to v0.34.1 to apply upstream inhibit rule bug fixes. #7877
* [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
* [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
Expand Down
7 changes: 7 additions & 0 deletions pkg/ring/kv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ type Client interface {
// If the key does not exist, Get will return nil and no error.
Get(ctx context.Context, key string) (any, error)

// GetInstance reads the entry instanceID of the value stored under key, such
// as one member of a ring. The value has the type that Get returns. A store
// that keeps a codec.MultiKey value as one entry per ID reads only that
// entry, and the value holds at most that entry. Any other store reads the
// whole value, like Get. The read might not refresh LastUpdateTime.
GetInstance(ctx context.Context, key, instanceID string) (any, error)

// Delete a specific key. Deletions are best-effort and no error will
// be returned if the key does not exist.
Delete(ctx context.Context, key string) error
Expand Down
5 changes: 5 additions & 0 deletions pkg/ring/kv/consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
return c.codec.Decode(kvp.Value)
}

// GetInstance implements kv.GetInstance. Consul keeps the value as one entry, so it reads the whole value.
func (c *Client) GetInstance(ctx context.Context, key, _ string) (any, error) {
return c.Get(ctx, key)
}

// Delete implements kv.Delete.
func (c *Client) Delete(ctx context.Context, key string) error {
_, err := c.kv.Delete(key, writeOptions.WithContext(ctx))
Expand Down
19 changes: 19 additions & 0 deletions pkg/ring/kv/dynamodb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dynamodb

import (
"context"
"errors"
"flag"
"fmt"
"sync"
Expand Down Expand Up @@ -114,6 +115,24 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
return res, nil
}

// GetInstance reads the one row of key whose sort key is instanceID. It did not
// observe the other rows, so it leaves the data and the time that the client
// keeps for the whole key (see LastUpdateTime) as they are.
func (c *Client) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
if instanceID == "" {
// An empty sort key would read every row of the key.
return nil, errors.New("instance ID is empty")
}

resp, _, err := c.kv.Query(ctx, dynamodbKey{primaryKey: key, sortKey: instanceID}, false)
if err != nil {
level.Warn(c.logger).Log("msg", "error GetInstance", "key", key, "instance", instanceID, "err", err)
return nil, err
}

return c.decodeMultikey(resp)
}

func (c *Client) Delete(ctx context.Context, key string) error {
resp, _, err := c.kv.Query(ctx, dynamodbKey{primaryKey: key}, false)
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions pkg/ring/kv/dynamodb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

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

}

func Test_GetInstance(t *testing.T) {
equals := func(value string) types.Condition {
return types.Condition{
ComparisonOperator: types.ComparisonOperatorEq,
AttributeValueList: []types.AttributeValue{&types.AttributeValueMemberS{Value: value}},
}
}
var conditions map[string]types.Condition
ddbClientMock := &mockDynamodb{
query: func(input *dynamodb.QueryInput) (*dynamodb.QueryOutput, error) {
conditions = input.KeyConditions
return &dynamodb.QueryOutput{}, nil
},
}
// The instrumentation layer and the real Query, down to the DynamoDB API.
ddb := dynamodbInstrumentation{
kv: *newDynamodbClientMock("TEST", ddbClientMock, 0),
ddbMetrics: newDynamoDbMetrics(prometheus.NewPedanticRegistry()),
}
codecMock := &CodecMock{}
descMock := &DescMock{}
c := NewClientMock(ddb, codecMock, TestLogger{}, prometheus.NewPedanticRegistry(), defaultPullTime, defaultBackoff)
codecMock.On("DecodeMultiKey").Return(descMock, nil)

value, err := c.GetInstance(context.TODO(), key, "ing-a")

require.NoError(t, err)
require.EqualValues(t, descMock, value)
require.Equal(t, map[string]types.Condition{primaryKey: equals(key), sortKey: equals("ing-a")}, conditions)

// The read saw one row, so the client keeps neither data nor a time for the whole key.
require.Empty(t, c.staleData)
require.True(t, c.LastUpdateTime(key).IsZero())

// A read of the whole key still has no condition on the sort key.
_, err = c.Get(context.TODO(), key)
require.NoError(t, err)
require.Equal(t, map[string]types.Condition{primaryKey: equals(key)}, conditions)
}

func Test_GetInstance_EmptyInstanceID(t *testing.T) {
ddbMock := NewDynamodbClientMock()
c := NewClientMock(ddbMock, &CodecMock{}, TestLogger{}, prometheus.NewPedanticRegistry(), defaultPullTime, defaultBackoff)

// An empty sort key would read every row of the key.
_, err := c.GetInstance(context.TODO(), key, "")

require.Error(t, err)
ddbMock.AssertNotCalled(t, "Query")
}

func Test_DynamodbKVWithTimeout(t *testing.T) {
ddbMock := NewDynamodbClientMock()
// Backend has delay of 5s while the client timeout is 1s.
Expand Down
10 changes: 10 additions & 0 deletions pkg/ring/kv/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ func (kv dynamodbKV) Query(ctx context.Context, key dynamodbKey, isPrefix bool)
},
}

// With a sort key, the query returns only that row and not every row of the key.
if key.sortKey != "" {
input.KeyConditions[sortKey] = types.Condition{
ComparisonOperator: types.ComparisonOperatorEq,
AttributeValueList: []types.AttributeValue{
&types.AttributeValueMemberS{Value: key.sortKey},
},
}
}

paginator := dynamodb.NewQueryPaginator(kv.ddbClient, input)

for paginator.HasMorePages() {
Expand Down
5 changes: 5 additions & 0 deletions pkg/ring/kv/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
return c.codec.Decode(resp.Kvs[0].Value)
}

// GetInstance implements kv.Client. etcd keeps the value as one entry, so it reads the whole value.
func (c *Client) GetInstance(ctx context.Context, key, _ string) (any, error) {
return c.Get(ctx, key)
}

// Delete implements kv.Client.
func (c *Client) Delete(ctx context.Context, key string) error {
opsCtx, cancel := c.opsContext(ctx)
Expand Down
35 changes: 35 additions & 0 deletions pkg/ring/kv/get_instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kv

import (
"context"
"testing"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

// instanceStore records the reads of one instance.
type instanceStore struct {
Client
instanceReads [][2]string
}

func (s *instanceStore) GetInstance(_ context.Context, key, instanceID string) (any, error) {
s.instanceReads = append(s.instanceReads, [2]string{key, instanceID})
return "one instance", nil
}

// The layers that NewClient adds around a multi store with a prefix and metrics
// pass the read of one instance on to the store.
func TestGetInstance_Wrappers(t *testing.T) {
store := &instanceStore{}
multi := NewMultiClient(MultiConfig{}, []kvclient{{client: store, name: "primary"}}, log.NewNopLogger(), nil)
client := newMetricsClient("multi", PrefixClient(multi, "ingester/"), prometheus.NewRegistry())

value, err := client.GetInstance(context.Background(), "ring", "ing-a")

require.NoError(t, err)
require.Equal(t, "one instance", value)
require.Equal(t, [][2]string{{"ingester/ring", "ing-a"}}, store.instanceReads)
}
5 changes: 5 additions & 0 deletions pkg/ring/kv/memberlist/memberlist_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (c *Client) Get(ctx context.Context, key string) (any, error) {
return c.kv.Get(key, c.codec)
}

// GetInstance is part of kv.Client interface. Memberlist keeps the value as one entry, so it reads the whole value.
func (c *Client) GetInstance(ctx context.Context, key, _ string) (any, error) {
return c.Get(ctx, key)
}

// Delete is part of kv.Client interface.
func (c *Client) Delete(ctx context.Context, key string) error {
err := c.awaitKVRunningOrStopping(ctx)
Expand Down
11 changes: 11 additions & 0 deletions pkg/ring/kv/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ func (m metrics) Get(ctx context.Context, key string) (any, error) {
return result, err
}

// GetInstance is tracked as a GET, like Get.
func (m metrics) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
var result any
err := instrument.CollectedRequest(ctx, "GET", m.requestDuration, instrument.ErrorCode, func(ctx context.Context) error {
var err error
result, err = m.c.GetInstance(ctx, key, instanceID)
return err
})
return result, err
}

func (m metrics) Delete(ctx context.Context, key string) error {
err := instrument.CollectedRequest(ctx, "Delete", m.requestDuration, instrument.ErrorCode, func(ctx context.Context) error {
return m.c.Delete(ctx, key)
Expand Down
4 changes: 4 additions & 0 deletions pkg/ring/kv/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (m mockClient) Get(ctx context.Context, key string) (any, error) {
return "", nil
}

func (m mockClient) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
return m.Get(ctx, key)
}

func (m mockClient) Delete(ctx context.Context, key string) error {
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/ring/kv/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ func (m *MultiClient) Get(ctx context.Context, key string) (any, error) {
return kv.client.Get(ctx, key)
}

// GetInstance is a part of the kv.Client interface. Like Get, it reads the primary store.
func (m *MultiClient) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
_, kv := m.getPrimaryClient()
return kv.client.GetInstance(ctx, key, instanceID)
}

// Delete is a part of the kv.Client interface.
func (m *MultiClient) Delete(ctx context.Context, key string) error {
_, kv := m.getPrimaryClient()
Expand Down
5 changes: 5 additions & 0 deletions pkg/ring/kv/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (c *prefixedKVClient) Get(ctx context.Context, key string) (any, error) {
return c.client.Get(ctx, c.prefix+key)
}

// GetInstance reads one instance of the object under the given key.
func (c *prefixedKVClient) GetInstance(ctx context.Context, key, instanceID string) (any, error) {
return c.client.GetInstance(ctx, c.prefix+key, instanceID)
}

// Delete removes a given object from its key.
func (c *prefixedKVClient) Delete(ctx context.Context, key string) error {
return c.client.Delete(ctx, c.prefix+key)
Expand Down
17 changes: 14 additions & 3 deletions pkg/ring/lifecycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,16 @@ func (i *Lifecycler) checkRingHealthForReadiness(ctx context.Context) error {
}

// If ring health checking is enabled we make sure all instances in the ring are ACTIVE and healthy,
// otherwise we just check this instance.
desc, err := i.KVStore.Get(ctx, i.RingKey)
// otherwise we just check this instance, and we read only this instance when the KV store can.
var (
desc any
err error
)
if i.cfg.ReadinessCheckRingHealth {
desc, err = i.KVStore.Get(ctx, i.RingKey)
} else {
desc, err = i.KVStore.GetInstance(ctx, i.RingKey, i.ID)
}
if err != nil {
level.Error(i.logger).Log("msg", "error talking to the KV store", "ring", i.RingName, "err", err)
return fmt.Errorf("error talking to the KV store: %s", err)
Expand All @@ -314,7 +322,10 @@ func (i *Lifecycler) checkRingHealthForReadiness(ctx context.Context) error {
return fmt.Errorf("instance %s not found in the ring", i.ID)
}

if err := instance.IsReady(i.KVStore.LastUpdateTime(i.RingKey), i.cfg.RingConfig.HeartbeatTimeout); err != nil {
// The read above just observed this instance, so the heartbeat age is measured from now.
// GetInstance might not refresh LastUpdateTime, which can then be older, or unset,
// and it would make an expired heartbeat look healthy.
if err := instance.IsReady(time.Now(), i.cfg.RingConfig.HeartbeatTimeout); err != nil {
return err
}
}
Expand Down
Loading