Skip to content

Commit

Permalink
fix: work around AWS S3 limits (#2629)
Browse files Browse the repository at this point in the history
Fixes issue #2627

We get/put metadata in dynamodb and it appears there are limits enforced
by AWS S3 APIs.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html

If you request more than 100 items, BatchGetItem returns a
ValidationException with the message "Too many items requested for the
BatchGetItem call."

Signed-off-by: Ramkumar Chinchani <[email protected]>
  • Loading branch information
rchincha authored Sep 12, 2024
1 parent 58c9c9c commit 98c8e28
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions pkg/meta/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1970,24 +1970,38 @@ func (dwr DynamoDB) DeleteUserData(ctx context.Context) error {
return err
}

const AwsS3BatchLimit = 100

func (dwr *DynamoDB) fetchImageMetaAttributesByDigest(ctx context.Context, digests []string,
) ([]map[string]types.AttributeValue, error) {
resp, err := dwr.Client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
RequestItems: map[string]types.KeysAndAttributes{
dwr.ImageMetaTablename: {
Keys: getBatchImageKeys(digests),
// AWS S3 as a limit (=100) on number of keys that can retrieved in one
// request, so break it up
batchedResp := []map[string]types.AttributeValue{}

for start := 0; start < len(digests); {
size := min(len(digests)-start, AwsS3BatchLimit)
end := start + size

resp, err := dwr.Client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
RequestItems: map[string]types.KeysAndAttributes{
dwr.ImageMetaTablename: {
Keys: getBatchImageKeys(digests[start:end]),
},
},
},
})
if err != nil {
return nil, err
}
})
if err != nil {
return nil, err
}

if len(resp.Responses[dwr.ImageMetaTablename]) != len(digests) {
return nil, zerr.ErrImageMetaNotFound
if len(resp.Responses[dwr.ImageMetaTablename]) != size {
return nil, zerr.ErrImageMetaNotFound
}

batchedResp = append(batchedResp, resp.Responses[dwr.ImageMetaTablename]...)
start = end
}

return resp.Responses[dwr.ImageMetaTablename], nil
return batchedResp, nil
}

func getBatchImageKeys(digests []string) []map[string]types.AttributeValue {
Expand Down

0 comments on commit 98c8e28

Please sign in to comment.