MCPcopy Create free account
hub / github.com/cortexproject/cortex / Batch

Method Batch

pkg/ring/kv/dynamodb/dynamodb.go:218–281  ·  view source on GitHub ↗
(ctx context.Context, put map[dynamodbKey]dynamodbItem, delete []dynamodbKey)

Source from the content-addressed store, hash-verified

216}
217
218func (kv dynamodbKV) Batch(ctx context.Context, put map[dynamodbKey]dynamodbItem, delete []dynamodbKey) (float64, bool, error) {
219 totalCapacity := float64(0)
220 writeRequestSize := len(put) + len(delete)
221 if writeRequestSize == 0 {
222 return totalCapacity, false, nil
223 }
224
225 writeRequestsSlices := make([][]types.TransactWriteItem, int(math.Ceil(float64(writeRequestSize)/float64(DdbBatchSizeLimit))))
226 for i := range writeRequestsSlices {
227 writeRequestsSlices[i] = make([]types.TransactWriteItem, 0, DdbBatchSizeLimit)
228 }
229
230 currIdx := 0
231 for key, ddbItem := range put {
232 item := kv.generatePutItemRequest(key, ddbItem)
233 ddbPut := &types.Put{
234 TableName: kv.tableName,
235 Item: item,
236 ConditionExpression: aws.String("attribute_not_exists(version) OR version = :v"),
237 ExpressionAttributeValues: map[string]types.AttributeValue{
238 ":v": &types.AttributeValueMemberN{Value: strconv.FormatInt(ddbItem.version, 10)},
239 },
240 }
241
242 writeRequestsSlices[currIdx] = append(writeRequestsSlices[currIdx], types.TransactWriteItem{Put: ddbPut})
243 if len(writeRequestsSlices[currIdx]) == DdbBatchSizeLimit {
244 currIdx++
245 }
246 }
247
248 for _, key := range delete {
249 item := generateItemKey(key)
250 ddbDelete := &types.Delete{
251 TableName: kv.tableName,
252 Key: item,
253 }
254 writeRequestsSlices[currIdx] = append(writeRequestsSlices[currIdx], types.TransactWriteItem{Delete: ddbDelete})
255 if len(writeRequestsSlices[currIdx]) == DdbBatchSizeLimit {
256 currIdx++
257 }
258 }
259
260 for _, slice := range writeRequestsSlices {
261 if len(slice) == 0 {
262 continue
263 }
264 resp, err := kv.ddbClient.TransactWriteItems(ctx, &dynamodb.TransactWriteItemsInput{
265 TransactItems: slice,
266 })
267 if err != nil {
268 var checkFailed *types.ConditionalCheckFailedException
269 isCheckFailed := errors.As(err, &checkFailed)
270 if isCheckFailed {
271 kv.logger.Log("msg", "conditional check failed on DynamoDB Batch", "err", err)
272 }
273 return totalCapacity, isCheckFailed, err
274 }
275 for _, consumedCapacity := range resp.ConsumedCapacity {

Callers

nothing calls this directly

Calls 6

generateItemKeyFunction · 0.85
getCapacityUnitsFunction · 0.85
StringMethod · 0.65
TransactWriteItemsMethod · 0.65
LogMethod · 0.45

Tested by

no test coverage detected