Skip to content

Commit 335a448

Browse files
author
Burak Serdar
committed
Add measure to ctx, add measures to mediator methods
1 parent a63dc56 commit 335a448

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

crud/src/main/java/com/redhat/lightblue/crud/CRUDOperationContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.redhat.lightblue.hooks.HookManager;
3434
import com.redhat.lightblue.util.Error;
3535
import com.redhat.lightblue.util.JsonDoc;
36+
import com.redhat.lightblue.util.Measure;
3637

3738
/**
3839
* An implementation of this class is passed into CRUD operation
@@ -56,6 +57,8 @@ public abstract class CRUDOperationContext implements MetadataResolver, Serializ
5657
private final Set<String> documentVersions=new HashSet<>();
5758
private boolean updateIfCurrent;
5859

60+
public final Measure measure=new Measure();
61+
5962
/**
6063
* This is the constructor used to represent the context of an operation
6164
*/

crud/src/main/java/com/redhat/lightblue/mediator/Mediator.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class Mediator {
9090
public static final String CRUD_MSG_PREFIX = "CRUD controller={}";
9191

9292
private static final Logger LOGGER = LoggerFactory.getLogger(Mediator.class);
93+
private static final Logger METRICS = LoggerFactory.getLogger("metrics."+Mediator.class.getName());
9394

9495
private static final Path OBJECT_TYPE_PATH = new Path("objectType");
9596

@@ -115,8 +116,10 @@ public Response insert(InsertionRequest req) {
115116
LOGGER.debug("insert {}", req.getEntityVersion());
116117
Error.push("insert(" + req.getEntityVersion().toString() + ")");
117118
Response response = new Response(factory.getNodeFactory());
119+
OperationContext ctx=null;
118120
try {
119-
OperationContext ctx = newCtx(req, CRUDOperation.INSERT);
121+
ctx = newCtx(req, CRUDOperation.INSERT);
122+
ctx.measure.begin("insert");
120123
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
121124
EntityMetadata md = ctx.getTopLevelEntityMetadata();
122125
if (!md.getAccess().getInsert().hasAccess(ctx.getCallerRoles())) {
@@ -131,12 +134,14 @@ public Response insert(InsertionRequest req) {
131134
LOGGER.debug(CRUD_MSG_PREFIX, controller.getClass().getName());
132135
controller.insert(ctx, req.getReturnFields());
133136
ctx.getHookManager().queueMediatorHooks(ctx);
137+
ctx.measure.begin("postProcessInsertedDocs");
134138
List<JsonDoc> insertedDocuments = ctx.getOutputDocumentsWithoutErrors();
135139
if (insertedDocuments != null && !insertedDocuments.isEmpty()) {
136140
response.setEntityData(JsonDoc.listToDoc(applyRange(req, insertedDocuments), factory.getNodeFactory()));
137141
response.setResultMetadata(ctx.getOutputDocumentMetadataWithoutErrors());
138142
response.setModifiedCount(insertedDocuments.size());
139143
}
144+
ctx.measure.end("postProcessInsertedDocs");
140145
if (!ctx.hasErrors() && !ctx.hasDocumentErrors()
141146
&& insertedDocuments != null && insertedDocuments.size() == ctx.getDocuments().size()) {
142147
ctx.setStatus(OperationStatus.COMPLETE);
@@ -163,6 +168,10 @@ public Response insert(InsertionRequest req) {
163168
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
164169
response.setStatus(OperationStatus.ERROR);
165170
} finally {
171+
if(ctx!=null) {
172+
ctx.measure.end("insert");
173+
METRICS.debug("insert",ctx.measure);
174+
}
166175
Error.pop();
167176
}
168177
return response;
@@ -184,8 +193,10 @@ public Response save(SaveRequest req) {
184193
LOGGER.debug("save {}", req.getEntityVersion());
185194
Error.push("save(" + req.getEntityVersion().toString() + ")");
186195
Response response = new Response(factory.getNodeFactory());
196+
OperationContext ctx=null;
187197
try {
188-
OperationContext ctx = newCtx(req, CRUDOperation.SAVE);
198+
ctx = newCtx(req, CRUDOperation.SAVE);
199+
ctx.measure.begin("save");
189200
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
190201
EntityMetadata md = ctx.getTopLevelEntityMetadata();
191202
if (!md.getAccess().getUpdate().hasAccess(ctx.getCallerRoles())
@@ -201,12 +212,14 @@ public Response save(SaveRequest req) {
201212
LOGGER.debug(CRUD_MSG_PREFIX, controller.getClass().getName());
202213
controller.save(ctx, req.isUpsert(), req.getReturnFields());
203214
ctx.getHookManager().queueMediatorHooks(ctx);
215+
ctx.measure.begin("postProcessSavedDocs");
204216
List<JsonDoc> updatedDocuments = ctx.getOutputDocumentsWithoutErrors();
205217
if (updatedDocuments != null && !updatedDocuments.isEmpty()) {
206218
response.setEntityData(JsonDoc.listToDoc(applyRange(req, updatedDocuments), factory.getNodeFactory()));
207219
response.setResultMetadata(ctx.getOutputDocumentMetadataWithoutErrors());
208220
response.setModifiedCount(updatedDocuments.size());
209221
}
222+
ctx.measure.end("postProcessSavedDocs");
210223
if (!ctx.hasErrors() && !ctx.hasDocumentErrors()
211224
&& updatedDocuments != null && updatedDocuments.size() == ctx.getDocuments().size()) {
212225
ctx.setStatus(OperationStatus.COMPLETE);
@@ -231,6 +244,10 @@ public Response save(SaveRequest req) {
231244
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
232245
response.setStatus(OperationStatus.ERROR);
233246
} finally {
247+
if(ctx!=null) {
248+
ctx.measure.end("save");
249+
METRICS.debug("save",ctx.measure);
250+
}
234251
Error.pop();
235252
}
236253
return response;
@@ -253,8 +270,10 @@ public Response update(UpdateRequest req) {
253270
LOGGER.debug("update {}", req.getEntityVersion());
254271
Error.push("update(" + req.getEntityVersion().toString() + ")");
255272
Response response = new Response(factory.getNodeFactory());
273+
OperationContext ctx=null;
256274
try {
257-
OperationContext ctx = newCtx(req, CRUDOperation.UPDATE);
275+
ctx = newCtx(req, CRUDOperation.UPDATE);
276+
ctx.measure.begin("update");
258277
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
259278
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
260279
if (!md.getAccess().getUpdate().hasAccess(ctx.getCallerRoles())) {
@@ -284,14 +303,16 @@ public Response update(UpdateRequest req) {
284303
}
285304
}
286305
ctx.getHookManager().queueMediatorHooks(ctx);
287-
LOGGER.debug("# Updated", updateResponse.getNumUpdated());
306+
ctx.measure.begin("postProcessUpdatedDocs");
307+
LOGGER.debug("# Updated", updateResponse.getNumUpdated());
288308
response.setModifiedCount(updateResponse.getNumUpdated());
289309
response.setMatchCount(updateResponse.getNumMatched());
290310
List<JsonDoc> updatedDocuments = ctx.getOutputDocumentsWithoutErrors();
291311
if (updatedDocuments != null && !updatedDocuments.isEmpty()) {
292312
response.setEntityData(JsonDoc.listToDoc(applyRange(req, updatedDocuments), factory.getNodeFactory()));
293313
response.setResultMetadata(ctx.getOutputDocumentMetadataWithoutErrors());
294314
}
315+
ctx.measure.end("postProcessUpdatedDocs");
295316
if (ctx.hasErrors()) {
296317
ctx.setStatus(OperationStatus.ERROR);
297318
} else if (ctx.hasDocumentErrors()) {
@@ -314,7 +335,11 @@ public Response update(UpdateRequest req) {
314335
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
315336
response.setStatus(OperationStatus.ERROR);
316337
} finally {
317-
Error.pop();
338+
if(ctx!=null) {
339+
ctx.measure.end("update");
340+
METRICS.debug("update",ctx.measure);
341+
}
342+
Error.pop();
318343
}
319344
return response;
320345
}
@@ -323,8 +348,10 @@ public Response delete(DeleteRequest req) {
323348
LOGGER.debug("delete {}", req.getEntityVersion());
324349
Error.push("delete(" + req.getEntityVersion().toString() + ")");
325350
Response response = new Response(factory.getNodeFactory());
351+
OperationContext ctx=null;
326352
try {
327-
OperationContext ctx = newCtx(req, CRUDOperation.DELETE);
353+
ctx = newCtx(req, CRUDOperation.DELETE);
354+
ctx.measure.begin("delete");
328355
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
329356
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
330357
if (!md.getAccess().getDelete().hasAccess(ctx.getCallerRoles())) {
@@ -371,6 +398,10 @@ public Response delete(DeleteRequest req) {
371398
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
372399
response.setStatus(OperationStatus.ERROR);
373400
} finally {
401+
if(ctx!=null) {
402+
ctx.measure.end("delete");
403+
METRICS.debug("delete",ctx.measure);
404+
}
374405
Error.pop();
375406
}
376407
return response;
@@ -446,8 +477,10 @@ public Response find(FindRequest req) {
446477
Error.push("find(" + req.getEntityVersion().toString() + ")");
447478
Response response = new Response(factory.getNodeFactory());
448479
response.setStatus(OperationStatus.ERROR);
480+
OperationContext ctx=null;
449481
try {
450-
OperationContext ctx = newCtx(req, CRUDOperation.FIND);
482+
ctx = newCtx(req, CRUDOperation.FIND);
483+
ctx.measure.begin("find");
451484
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
452485
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
453486
if (!md.getAccess().getFind().hasAccess(ctx.getCallerRoles())) {
@@ -467,8 +500,11 @@ public Response find(FindRequest req) {
467500
((CompositeFindImpl) finder).setParallelism(9);
468501
}
469502

503+
ctx.measure.begin("finder.find");
470504
CRUDFindResponse result = finder.find(ctx, req.getCRUDFindRequest());
505+
ctx.measure.end("finder.find");
471506

507+
ctx.measure.begin("postProcessFound");
472508
List<JsonDoc> foundDocuments = ctx.getOutputDocumentsWithoutErrors();
473509
if (foundDocuments != null && foundDocuments.size() == ctx.getDocuments().size()) {
474510
ctx.setStatus(OperationStatus.COMPLETE);
@@ -490,6 +526,7 @@ public Response find(FindRequest req) {
490526
response.setEntityData(JsonDoc.listToDoc(resultList, factory.getNodeFactory()));
491527
response.setResultMetadata(resultMetadata);
492528
}
529+
ctx.measure.end("postProcessFound");
493530

494531
factory.getInterceptors().callInterceptors(InterceptPoint.POST_MEDIATOR_FIND, ctx);
495532
}
@@ -508,6 +545,10 @@ public Response find(FindRequest req) {
508545
LOGGER.debug("Exception during find:{}", e);
509546
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
510547
} finally {
548+
if(ctx!=null) {
549+
ctx.measure.end("find");
550+
METRICS.debug("find",ctx.measure);
551+
}
511552
Error.pop();
512553
}
513554
return response;
@@ -687,6 +728,7 @@ protected OperationContext newCtx(Request request, CRUDOperation CRUDOperation)
687728
*/
688729
private void runBulkConstraintValidation(OperationContext ctx) {
689730
LOGGER.debug("Bulk constraint validation");
731+
ctx.measure.begin("runBulkConstraintValidation");
690732
EntityMetadata md = ctx.getTopLevelEntityMetadata();
691733
ConstraintValidator constraintValidator = factory.getConstraintValidator(md);
692734
List<DocCtx> docs = ctx.getDocumentsWithoutErrors();
@@ -704,9 +746,11 @@ private void runBulkConstraintValidation(OperationContext ctx) {
704746
ctx.addErrors(errors);
705747
}
706748
LOGGER.debug("Constraint validation complete");
749+
ctx.measure.end("runBulkConstraintValidation");
707750
}
708751

709752
private void updatePredefinedFields(OperationContext ctx, CRUDController controller, String entity) {
753+
ctx.measure.begin("updatePredefinedFields");
710754
for (JsonDoc doc : ctx.getDocuments()) {
711755
PredefinedFields.updateArraySizes(ctx.getTopLevelEntityMetadata(), factory.getNodeFactory(), doc);
712756
JsonNode node = doc.get(OBJECT_TYPE_PATH);
@@ -717,6 +761,7 @@ private void updatePredefinedFields(OperationContext ctx, CRUDController control
717761
}
718762
controller.updatePredefinedFields(ctx, doc);
719763
}
764+
ctx.measure.end("updatePredefinedFields");
720765
}
721766

722767
/**

0 commit comments

Comments
 (0)