Skip to content

Commit 98c8e28

Browse files
authored
fix: work around AWS S3 limits (#2629)
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 98c8e28

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

pkg/meta/dynamodb/dynamodb.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,24 +1970,38 @@ 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+
for start := 0; start < len(digests); {
1982+
size := min(len(digests)-start, AwsS3BatchLimit)
1983+
end := start + size
1984+
1985+
resp, err := dwr.Client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
1986+
RequestItems: map[string]types.KeysAndAttributes{
1987+
dwr.ImageMetaTablename: {
1988+
Keys: getBatchImageKeys(digests[start:end]),
1989+
},
19791990
},
1980-
},
1981-
})
1982-
if err != nil {
1983-
return nil, err
1984-
}
1991+
})
1992+
if err != nil {
1993+
return nil, err
1994+
}
19851995

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

1990-
return resp.Responses[dwr.ImageMetaTablename], nil
2004+
return batchedResp, nil
19912005
}
19922006

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

0 commit comments

Comments
 (0)