Skip to content

Commit ca54ca6

Browse files
zouxiangzou.xiang
andauthored
feat:add request unit statistics for db operations (#108)
Co-authored-by: zou.xiang <zou.xiang@smartcompany.jp>
1 parent fcf7fc3 commit ca54ca6

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

src/main/java/io/github/thunderz99/cosmos/CosmosDatabase.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class CosmosDatabase {
4141

4242
static final int MAX_BATCH_NUMBER_OF_OPERATION = 100;
4343

44+
static final int FIND_PREFERRED_PAGE_SIZE = 10;
45+
4446
String db;
4547
CosmosClient clientV4;
4648

@@ -93,7 +95,7 @@ public CosmosDocument create(String coll, Object data, String partition) throws
9395

9496
var item = response.getItem();
9597

96-
log.info("created Document:{}/docs/{}, partition:{}, account:{}", collectionLink, getId(item), partition, getAccount());
98+
log.info("created Document:{}/docs/{}, partition:{}, account:{}, request charge:{}", collectionLink, getId(item), partition, getAccount(), response.getRequestCharge());
9799

98100
return new CosmosDocument(item);
99101
}
@@ -211,8 +213,12 @@ private List<CosmosDocument> doBatchWithRetry(CosmosContainer container, CosmosB
211213
new CosmosBatchResponseWrapper(container.executeCosmosBatch(batch))
212214
);
213215

216+
log.info("Document batch operations: partition key:{}, account:{}, request charge:{}",
217+
Objects.nonNull(batch.getPartitionKeyValue()) ? batch.getPartitionKeyValue().toString() : "", getAccount(), response.cosmosBatchReponse.getRequestCharge());
218+
214219
var successDocuments = new ArrayList<CosmosDocument>();
215220
for (CosmosBatchOperationResult cosmosBatchOperationResult : response.cosmosBatchReponse.getResults()) {
221+
log.info("Document batch operation: operation type:{}, request charge:{}", cosmosBatchOperationResult.getOperation().getOperationType().name(), cosmosBatchOperationResult.getRequestCharge());
216222
var item = cosmosBatchOperationResult.getItem(mapInstance.getClass());
217223
if (item == null) continue;
218224
successDocuments.add(new CosmosDocument(item));
@@ -323,6 +329,8 @@ private CosmosBulkResult doBulkWithRetry(String coll, List<CosmosItemOperation>
323329
continue;
324330
}
325331

332+
log.info("Document bulk operation: operation type:{}, request charge:{}", operation.getOperationType().name(), response.getRequestCharge());
333+
326334
if (RetryUtil.shouldRetry(response.getStatusCode())) {
327335
delay = Math.max(delay, response.getRetryAfterDuration().toMillis());
328336
retryTasks.add(operation);
@@ -438,7 +446,7 @@ public CosmosDocument read(String coll, String id, String partition) throws Exce
438446
mapInstance.getClass()
439447
));
440448

441-
log.info("read Document:{}, partition:{}, account:{}", documentLink, partition, getAccount());
449+
log.info("read Document:{}, partition:{}, account:{}, request charge: {}", documentLink, partition, getAccount(), response.getRequestCharge());
442450

443451
return new CosmosDocument(response.getItem());
444452
}
@@ -524,7 +532,7 @@ public CosmosDocument update(String coll, Object data, String partition) throws
524532
));
525533

526534

527-
log.info("updated Document:{}, partition:{}, account:{}", documentLink, partition, getAccount());
535+
log.info("updated Document:{}, partition:{}, account:{}, request charge:{}", documentLink, partition, getAccount(), response.getRequestCharge());
528536

529537
return new CosmosDocument(response.getItem());
530538
}
@@ -768,7 +776,7 @@ public CosmosDocument upsert(String coll, Object data, String partition) throws
768776
new CosmosItemRequestOptions()
769777
));
770778

771-
log.info("upsert Document:{}/docs/{}, partition:{}, account:{}", collectionLink, id, partition, getAccount());
779+
log.info("upsert Document:{}/docs/{}, partition:{}, account:{}, request charge:{}", collectionLink, id, partition, getAccount(), response.getRequestCharge());
772780

773781
return new CosmosDocument(response.getItem());
774782
}
@@ -812,7 +820,7 @@ public CosmosDatabase delete(String coll, String id, String partition) throws Ex
812820
new CosmosItemRequestOptions()
813821
));
814822

815-
log.info("deleted Document:{}, partition:{}, account:{}", documentLink, partition, getAccount());
823+
log.info("deleted Document:{}, partition:{}, account:{}, request charge:{}", documentLink, partition, getAccount(), response.getRequestCharge());
816824

817825
} catch (Exception e) {
818826
if (Cosmos.isResourceNotFoundException(e)) {
@@ -892,6 +900,13 @@ CosmosDocumentList find(String coll, Aggregate aggregate, Condition cond, String
892900
// process query without join
893901
var docs = RetryUtil.executeWithRetry(() ->
894902
container.queryItems(querySpec.toSqlQuerySpecV4(), queryRequestOptions, mapInstance.getClass()));
903+
904+
if(log.isInfoEnabled()) {
905+
docs.iterableByPage(FIND_PREFERRED_PAGE_SIZE).forEach(response -> {
906+
log.info("find Document:{}/docs/, partition:{}, result size:{}, request charge:{}", collectionLink, partition, response.getResults().size(), response.getRequestCharge());
907+
});
908+
}
909+
895910
List maps = docs.stream().collect(Collectors.toList());
896911

897912
if (aggregate != null) {
@@ -1188,6 +1203,12 @@ public int count(String coll, Condition cond, String partition) throws Exception
11881203
() -> container.queryItems(querySpec.toSqlQuerySpecV4(), queryRequestOptions, mapInstance.getClass())
11891204
);
11901205

1206+
if(log.isInfoEnabled()) {
1207+
docs.iterableByPage(FIND_PREFERRED_PAGE_SIZE).forEach(response -> {
1208+
log.info("count Document:{}/docs/, partition:{}, result size:{}, request charge:{}", collectionLink, partition, response.getResults().size(), response.getRequestCharge());
1209+
});
1210+
}
1211+
11911212
List<Map> maps = docs.stream().collect(Collectors.toList());
11921213

11931214
if (log.isInfoEnabled()) {
@@ -1233,7 +1254,7 @@ public CosmosDocument increment(String coll, String id, String path, int value,
12331254
));
12341255

12351256
var item = response.getItem();
1236-
log.info("increment Document:{}, partition:{}, account:{}", documentLink, partition, getAccount());
1257+
log.info("increment Document:{}, partition:{}, account:{}, request charge:{}", documentLink, partition, getAccount(), response.getRequestCharge());
12371258

12381259
return new CosmosDocument(item);
12391260
}
@@ -1283,7 +1304,7 @@ public CosmosDocument patch(String coll, String id, PatchOperations operations,
12831304
));
12841305

12851306
var item = response.getItem();
1286-
log.info("patch Document:{}, partition:{}, account:{}", documentLink, partition, getAccount());
1307+
log.info("patch Document:{}, partition:{}, account:{}, request charge:{}", documentLink, partition, getAccount(), response.getRequestCharge());
12871308

12881309
return new CosmosDocument(item);
12891310
}

0 commit comments

Comments
 (0)