dynamo db引发部分写入异常

siv3szwd  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(206)

我正在创建dynamo db批处理的25个请求。在这25个请求中,几乎每次至少有一个写操作失败。我试着监视数据库,但从度量中看不出为什么每次都有错误。没有写请求的限制,dynamo db提供超出度量的错误。
从代码来看,它每次都是在这个if条件下从下面的代码中掉出来的

if (!result.getUnprocessedItems().isEmpty() && result.getUnprocessedItems().get(tableName).size() < values.size()) {
            metrics.dynamoDbRequests(OperationStatus.SUCCESS, OperationType.PUT).add(timer.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
            throw new WriteFailedException(result.getUnprocessedItems().get(tableName).size());
        }

编写代码:

public void write(Map<String, String> values) {
        ImmutableList.Builder<WriteRequest> writeRequestBuilder = ImmutableList.builder();
        for (Map.Entry<String, String> entry : values.entrySet()) {
            writeRequestBuilder.add(new WriteRequest().withPutRequest(new PutRequest().withItem(asItem(entry.getKey(), entry.getValue()))));
        }

        List<WriteRequest> writeRequests = writeRequestBuilder.build();
        BatchWriteItemRequest request = new BatchWriteItemRequest().withRequestItems(ImmutableMap.of(tableName, writeRequests));

        BatchWriteItemResult result;
        try {
            result = client.batchWriteItem(request);
            if (result.getUnprocessedItems().isEmpty()) {
                metrics.dynamoDbRequests(OperationStatus.SUCCESS, OperationType.PUT).add(timer.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
                return;
            }
        }
        catch (ProvisionedThroughputExceededException e) {
            metrics.dynamoDbRequests(OperationStatus.PROVISION_EXCEEDED_ERROR, OperationType.PUT).add(timer.elapsed(TimeUnit.MILLISECONDS),
                    TimeUnit.MILLISECONDS);
            throw new WriteFailedException(values.size(), e);
        }
        catch (Exception e) {
            metrics.dynamoDbRequests(OperationStatus.ERROR, OperationType.PUT).add(timer.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
            throw new WriteFailedException(values.size(), e);
        }
        if (!result.getUnprocessedItems().isEmpty() && result.getUnprocessedItems().get(tableName).size() < values.size()) {
            metrics.dynamoDbRequests(OperationStatus.SUCCESS, OperationType.PUT).add(timer.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
            throw new WriteFailedException(result.getUnprocessedItems().get(tableName).size());
        }
        metrics.dynamoDbRequests(OperationStatus.ERROR, OperationType.PUT).add(timer.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
        throw new WriteFailedException(result.getUnprocessedItems().get(tableName).size());
}

我正在努力找出这个错误的原因。但从我掌握的指标和日志来看,这并不十分明显。如果有人能提供更多的见解那就太好了。
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题