Skip to content

Dynamodb kv batch on slices #5136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2023
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 @@ -35,6 +35,7 @@
* [BUGFIX] Tracing: Fix missing object storage span instrumentation. #5074
* [BUGFIX] Ingester: Ingesters returning empty response for metadata APIs. #5081
* [BUGFIX] Ingester: Fix panic when querying metadata from blocks that are being deleted. #5119
* [BUGFIX] Ring: Fix case when dynamodb kv reaches the limit of 25 actions per batch call. #5136
* [FEATURE] Alertmanager: Add support for time_intervals. #5102

## 1.14.0 2022-12-02
Expand Down
1 change: 1 addition & 0 deletions pkg/ring/kv/dynamodb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func NewClient(cfg Config, cc codec.Codec, logger log.Logger, registerer prometh
ddbMetrics: ddbMetrics,
staleData: make(map[string]staleData),
}
level.Info(c.logger).Log("dynamodb kv initialized")
return c, nil
}

Expand Down
48 changes: 34 additions & 14 deletions pkg/ring/kv/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dynamodb
import (
"context"
"fmt"
"math"
"strconv"
"time"

Expand All @@ -13,6 +14,12 @@ import (
"github.com/go-kit/log"
)

const (
// DdbBatchSizeLimit Current limit of 25 actions per batch
// https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
DdbBatchSizeLimit = 25
)

type dynamodbKey struct {
primaryKey string
sortKey string
Expand Down Expand Up @@ -176,38 +183,51 @@ func (kv dynamodbKV) Batch(ctx context.Context, put map[dynamodbKey][]byte, dele
return nil
}

writeRequests := make([]*dynamodb.WriteRequest, 0, writeRequestSize)
writeRequestsSlices := make([][]*dynamodb.WriteRequest, int(math.Ceil(float64(writeRequestSize)/float64(DdbBatchSizeLimit))))
for i := 0; i < len(writeRequestsSlices); i++ {
writeRequestsSlices[i] = make([]*dynamodb.WriteRequest, 0, DdbBatchSizeLimit)
}

currIdx := 0
for key, data := range put {
item := kv.generatePutItemRequest(key, data)
writeRequests = append(writeRequests, &dynamodb.WriteRequest{
writeRequestsSlices[currIdx] = append(writeRequestsSlices[currIdx], &dynamodb.WriteRequest{
PutRequest: &dynamodb.PutRequest{
Item: item,
},
})
if len(writeRequestsSlices[currIdx]) == DdbBatchSizeLimit {
currIdx++
}
}

for _, key := range delete {
item := generateItemKey(key)
writeRequests = append(writeRequests, &dynamodb.WriteRequest{
writeRequestsSlices[currIdx] = append(writeRequestsSlices[currIdx], &dynamodb.WriteRequest{
DeleteRequest: &dynamodb.DeleteRequest{
Key: item,
},
})
if len(writeRequestsSlices[currIdx]) == DdbBatchSizeLimit {
currIdx++
}
}

input := &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest{
*kv.tableName: writeRequests,
},
}
for _, slice := range writeRequestsSlices {
input := &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]*dynamodb.WriteRequest{
*kv.tableName: slice,
},
}

resp, err := kv.ddbClient.BatchWriteItemWithContext(ctx, input)
if err != nil {
return err
}
resp, err := kv.ddbClient.BatchWriteItemWithContext(ctx, input)
if err != nil {
return err
}

if resp.UnprocessedItems != nil && len(resp.UnprocessedItems) > 0 {
return fmt.Errorf("error processing batch request for %s requests", resp.UnprocessedItems)
if resp.UnprocessedItems != nil && len(resp.UnprocessedItems) > 0 {
return fmt.Errorf("error processing batch request for %s requests", resp.UnprocessedItems)
}
}

return nil
Expand Down
55 changes: 54 additions & 1 deletion pkg/ring/kv/dynamodb/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,59 @@ func Test_Batch(t *testing.T) {
require.NoError(t, err)
}

func Test_BatchSlices(t *testing.T) {
tableName := "TEST"
ddbKeyDelete := dynamodbKey{
primaryKey: "PKDelete",
sortKey: "SKDelete",
}
numOfCalls := 0
ddbClientMock := &mockDynamodb{
batchWriteItem: func(input *dynamodb.BatchWriteItemInput) (*dynamodb.BatchWriteItemOutput, error) {
numOfCalls++
return &dynamodb.BatchWriteItemOutput{}, nil
},
}
ddb := newDynamodbClientMock(tableName, ddbClientMock, 5*time.Hour)

for _, tc := range []struct {
name string
numOfExecutions int
expectedCalls int
}{
// These tests follow each other (end state of KV in state is starting point in the next state).
{
name: "Test slice on lower bound",
numOfExecutions: 24,
expectedCalls: 1,
},
{
name: "Test slice on exact size",
numOfExecutions: 25,
expectedCalls: 1,
},
{
name: "Test slice on upper bound",
numOfExecutions: 26,
expectedCalls: 2,
},
} {
t.Run(tc.name, func(t *testing.T) {
numOfCalls = 0
delete := make([]dynamodbKey, 0, tc.numOfExecutions)
for i := 0; i < tc.numOfExecutions; i++ {
delete = append(delete, ddbKeyDelete)
}

err := ddb.Batch(context.TODO(), nil, delete)
require.NoError(t, err)
require.EqualValues(t, tc.expectedCalls, numOfCalls)

})
}

}

func Test_EmptyBatch(t *testing.T) {
tableName := "TEST"
ddbClientMock := &mockDynamodb{}
Expand Down Expand Up @@ -156,7 +209,7 @@ func (m *mockDynamodb) PutItemWithContext(_ context.Context, input *dynamodb.Put
return m.putItem(input), nil
}

func (m *mockDynamodb) BatchWriteItemWithContext(_ context.Context, input *dynamodb.BatchWriteItemInput, _ ...request.Option) (*dynamodb.BatchWriteItemOutput, error) {
func (m *mockDynamodb) BatchWriteItemWithContext(ctx context.Context, input *dynamodb.BatchWriteItemInput, opts ...request.Option) (*dynamodb.BatchWriteItemOutput, error) {
return m.batchWriteItem(input)
}

Expand Down