Skip to content

Commit 0e44c0a

Browse files
author
Ramkumar Chinchani
committed
fix: work around AWS S3 limits
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]>
1 parent 58c9c9c commit 0e44c0a

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

pkg/meta/dynamodb/dynamodb.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,24 +1970,39 @@ func (dwr DynamoDB) DeleteUserData(ctx context.Context) error {
19701970
return err
19711971
}
19721972

1973+
const AwsS3BatchLimit = 100
1974+
19731975
func (dwr *DynamoDB) fetchImageMetaAttributesByDigest(ctx context.Context, digests []string,
19741976
) ([]map[string]types.AttributeValue, error) {
1975-
resp, err := dwr.Client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
1976-
RequestItems: map[string]types.KeysAndAttributes{
1977-
dwr.ImageMetaTablename: {
1978-
Keys: getBatchImageKeys(digests),
1977+
// AWS S3 as a limit (=100) on number of keys that can retrieved in one
1978+
// request, so break it up
1979+
batchedResp := []map[string]types.AttributeValue{}
1980+
1981+
var end int
1982+
1983+
for start := 0; start < len(digests); {
1984+
end = min(len(digests)-start, AwsS3BatchLimit)
1985+
1986+
resp, err := dwr.Client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
1987+
RequestItems: map[string]types.KeysAndAttributes{
1988+
dwr.ImageMetaTablename: {
1989+
Keys: getBatchImageKeys(digests[start:end]),
1990+
},
19791991
},
1980-
},
1981-
})
1982-
if err != nil {
1983-
return nil, err
1984-
}
1992+
})
1993+
if err != nil {
1994+
return nil, err
1995+
}
19851996

1986-
if len(resp.Responses[dwr.ImageMetaTablename]) != len(digests) {
1987-
return nil, zerr.ErrImageMetaNotFound
1997+
if len(resp.Responses[dwr.ImageMetaTablename]) != (end - start) {
1998+
return nil, zerr.ErrImageMetaNotFound
1999+
}
2000+
2001+
batchedResp = append(batchedResp, resp.Responses[dwr.ImageMetaTablename]...)
2002+
start = end
19882003
}
19892004

1990-
return resp.Responses[dwr.ImageMetaTablename], nil
2005+
return batchedResp, nil
19912006
}
19922007

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

0 commit comments

Comments
 (0)