Skip to content

Commit 3964b3d

Browse files
author
Burak Serdar
committed
Merge branch 'master' into streams
2 parents c032833 + cf497c9 commit 3964b3d

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
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
@@ -35,6 +35,7 @@
3535
import com.redhat.lightblue.hooks.HookManager;
3636
import com.redhat.lightblue.util.Error;
3737
import com.redhat.lightblue.util.JsonDoc;
38+
import com.redhat.lightblue.util.Measure;
3839

3940
/**
4041
* An implementation of this class is passed into CRUD operation
@@ -60,6 +61,8 @@ public abstract class CRUDOperationContext implements MetadataResolver, Serializ
6061
private final Set<String> documentVersions=new HashSet<>();
6162
private boolean updateIfCurrent;
6263

64+
public final Measure measure=new Measure();
65+
6366
/**
6467
* This is the constructor used to represent the context of an operation
6568
*/

crud/src/main/java/com/redhat/lightblue/eval/NaryValueRelationalExpressionEvaluator.java

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
package com.redhat.lightblue.eval;
2020

21-
import java.util.List;
22-
import java.util.ArrayList;
21+
import java.util.Set;
22+
import java.util.HashSet;
2323

2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
@@ -40,7 +40,7 @@ public class NaryValueRelationalExpressionEvaluator extends QueryEvaluator {
4040
private final Path field;
4141
private final FieldTreeNode fieldMd;
4242
private final NaryRelationalOperator operator;
43-
private final List<Object> values;
43+
private final Set<Object> values;
4444

4545
public NaryValueRelationalExpressionEvaluator(NaryValueRelationalExpression expr, FieldTreeNode context) {
4646
field = expr.getField();
@@ -49,12 +49,9 @@ public NaryValueRelationalExpressionEvaluator(NaryValueRelationalExpression expr
4949
throw new EvaluationError(expr, CrudConstants.ERR_FIELD_NOT_THERE + field);
5050
}
5151
operator = expr.getOp();
52-
List<Value> l = expr.getValues();
53-
values = new ArrayList<>(l.size());
54-
for (Value x : l) {
55-
if (x != null) {
56-
values.add(x.getValue());
57-
}
52+
values = new HashSet<>();
53+
for (Value x : expr.getValues()) {
54+
values.add(fieldMd.getType().cast(x.getValue()));
5855
}
5956
LOGGER.debug("ctor {} {} {}", expr.getField(), operator, values);
6057
}
@@ -67,25 +64,9 @@ public boolean evaluate(QueryEvaluationContext ctx) {
6764
while (cursor.hasNext()) {
6865
cursor.next();
6966
JsonNode valueNode = cursor.getCurrentValue();
70-
Object docValue;
71-
if (valueNode != null) {
72-
docValue = fieldMd.getType().fromJson(valueNode);
73-
} else {
74-
docValue = null;
75-
}
67+
Object docValue = fieldMd.getType().fromJson(valueNode);
7668
LOGGER.debug(" value={}", valueNode);
77-
boolean in = false;
78-
for (Object x : values) {
79-
if (docValue == null) {
80-
if (x == null) {
81-
in = true;
82-
break;
83-
}
84-
} else if (x != null && fieldMd.getType().compare(docValue, x) == 0) {
85-
in = true;
86-
break;
87-
}
88-
}
69+
boolean in = values.contains(docValue);
8970
LOGGER.debug(" result={}", in);
9071
if (in) {
9172
ret = true;

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class Mediator {
9595
public static final String CRUD_MSG_PREFIX = "CRUD controller={}";
9696

9797
private static final Logger LOGGER = LoggerFactory.getLogger(Mediator.class);
98+
private static final Logger METRICS = LoggerFactory.getLogger("metrics."+Mediator.class.getName());
9899

99100
private static final Path OBJECT_TYPE_PATH = new Path("objectType");
100101

@@ -120,8 +121,10 @@ public Response insert(InsertionRequest req) {
120121
LOGGER.debug("insert {}", req.getEntityVersion());
121122
Error.push("insert(" + req.getEntityVersion().toString() + ")");
122123
Response response = new Response(factory.getNodeFactory());
124+
OperationContext ctx=null;
123125
try {
124-
OperationContext ctx = newCtx(req, CRUDOperation.INSERT);
126+
ctx = newCtx(req, CRUDOperation.INSERT);
127+
ctx.measure.begin("insert");
125128
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
126129
EntityMetadata md = ctx.getTopLevelEntityMetadata();
127130
if (!md.getAccess().getInsert().hasAccess(ctx.getCallerRoles())) {
@@ -136,9 +139,11 @@ public Response insert(InsertionRequest req) {
136139
LOGGER.debug(CRUD_MSG_PREFIX, controller.getClass().getName());
137140
CRUDInsertionResponse ir=controller.insert(ctx, req.getReturnFields());
138141
ctx.getHookManager().queueMediatorHooks(ctx);
142+
ctx.measure.begin("postProcessInsertedDocs");
139143
response.setModifiedCount(ir.getNumInserted());
140144
List<DataError> dataErrors=setResponseResults(ctx,req,response);
141145
response.getDataErrors().addAll(dataErrors);
146+
ctx.measure.begin("postProcessInsertedDocs");
142147
if (!ctx.hasErrors() && dataErrors.isEmpty() && ctx.getInputDocuments().size()==ir.getNumInserted()) {
143148
ctx.setStatus(OperationStatus.COMPLETE);
144149
} else if (ir.getNumInserted()>0) {
@@ -164,6 +169,10 @@ public Response insert(InsertionRequest req) {
164169
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
165170
response.setStatus(OperationStatus.ERROR);
166171
} finally {
172+
if(ctx!=null) {
173+
ctx.measure.end("insert");
174+
METRICS.debug("insert",ctx.measure);
175+
}
167176
Error.pop();
168177
}
169178
return response;
@@ -185,8 +194,10 @@ public Response save(SaveRequest req) {
185194
LOGGER.debug("save {}", req.getEntityVersion());
186195
Error.push("save(" + req.getEntityVersion().toString() + ")");
187196
Response response = new Response(factory.getNodeFactory());
197+
OperationContext ctx=null;
188198
try {
189-
OperationContext ctx = newCtx(req, CRUDOperation.SAVE);
199+
ctx = newCtx(req, CRUDOperation.SAVE);
200+
ctx.measure.begin("save");
190201
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
191202
EntityMetadata md = ctx.getTopLevelEntityMetadata();
192203
if (!md.getAccess().getUpdate().hasAccess(ctx.getCallerRoles())
@@ -202,9 +213,11 @@ public Response save(SaveRequest req) {
202213
LOGGER.debug(CRUD_MSG_PREFIX, controller.getClass().getName());
203214
CRUDSaveResponse sr=controller.save(ctx, req.isUpsert(), req.getReturnFields());
204215
ctx.getHookManager().queueMediatorHooks(ctx);
216+
ctx.measure.begin("postProcessSavedDocs");
205217
response.setModifiedCount(sr.getNumSaved());
206218
List<DataError> dataErrors=setResponseResults(ctx,req,response);
207219
response.getDataErrors().addAll(dataErrors);
220+
ctx.measure.end("postProcessSavedDocs");
208221
if (!ctx.hasErrors() && dataErrors.isEmpty() && ctx.getInputDocuments().size()==sr.getNumSaved()) {
209222
ctx.setStatus(OperationStatus.COMPLETE);
210223
} else if (sr.getNumSaved()>0) {
@@ -229,6 +242,10 @@ public Response save(SaveRequest req) {
229242
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
230243
response.setStatus(OperationStatus.ERROR);
231244
} finally {
245+
if(ctx!=null) {
246+
ctx.measure.end("save");
247+
METRICS.debug("save",ctx.measure);
248+
}
232249
Error.pop();
233250
}
234251
return response;
@@ -251,8 +268,10 @@ public Response update(UpdateRequest req) {
251268
LOGGER.debug("update {}", req.getEntityVersion());
252269
Error.push("update(" + req.getEntityVersion().toString() + ")");
253270
Response response = new Response(factory.getNodeFactory());
271+
OperationContext ctx=null;
254272
try {
255-
OperationContext ctx = newCtx(req, CRUDOperation.UPDATE);
273+
ctx = newCtx(req, CRUDOperation.UPDATE);
274+
ctx.measure.begin("update");
256275
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
257276
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
258277
if (!md.getAccess().getUpdate().hasAccess(ctx.getCallerRoles())) {
@@ -282,11 +301,13 @@ public Response update(UpdateRequest req) {
282301
}
283302
}
284303
ctx.getHookManager().queueMediatorHooks(ctx);
285-
LOGGER.debug("# Updated", updateResponse.getNumUpdated());
304+
ctx.measure.begin("postProcessUpdatedDocs");
305+
LOGGER.debug("# Updated", updateResponse.getNumUpdated());
286306
response.setModifiedCount(updateResponse.getNumUpdated());
287307
response.setMatchCount(updateResponse.getNumMatched());
288308
List<DataError> dataErrors=setResponseResults(ctx,req,response);
289309
response.getDataErrors().addAll(dataErrors);
310+
ctx.measure.end("postProcessUpdatedDocs");
290311
if (ctx.hasErrors()) {
291312
ctx.setStatus(OperationStatus.ERROR);
292313
} else if (!dataErrors.isEmpty()) {
@@ -307,7 +328,11 @@ public Response update(UpdateRequest req) {
307328
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
308329
response.setStatus(OperationStatus.ERROR);
309330
} finally {
310-
Error.pop();
331+
if(ctx!=null) {
332+
ctx.measure.end("update");
333+
METRICS.debug("update",ctx.measure);
334+
}
335+
Error.pop();
311336
}
312337
return response;
313338
}
@@ -316,8 +341,10 @@ public Response delete(DeleteRequest req) {
316341
LOGGER.debug("delete {}", req.getEntityVersion());
317342
Error.push("delete(" + req.getEntityVersion().toString() + ")");
318343
Response response = new Response(factory.getNodeFactory());
344+
OperationContext ctx=null;
319345
try {
320-
OperationContext ctx = newCtx(req, CRUDOperation.DELETE);
346+
ctx = newCtx(req, CRUDOperation.DELETE);
347+
ctx.measure.begin("delete");
321348
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
322349
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
323350
if (!md.getAccess().getDelete().hasAccess(ctx.getCallerRoles())) {
@@ -363,6 +390,10 @@ public Response delete(DeleteRequest req) {
363390
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
364391
response.setStatus(OperationStatus.ERROR);
365392
} finally {
393+
if(ctx!=null) {
394+
ctx.measure.end("delete");
395+
METRICS.debug("delete",ctx.measure);
396+
}
366397
Error.pop();
367398
}
368399
return response;
@@ -441,8 +472,10 @@ public Response find(FindRequest req) {
441472
Error.push("find(" + req.getEntityVersion().toString() + ")");
442473
Response response = new Response(factory.getNodeFactory());
443474
response.setStatus(OperationStatus.ERROR);
475+
OperationContext ctx=null;
444476
try {
445-
OperationContext ctx = newCtx(req, CRUDOperation.FIND);
477+
ctx = newCtx(req, CRUDOperation.FIND);
478+
ctx.measure.begin("find");
446479
response.setEntity(ctx.getTopLevelEntityName(),ctx.getTopLevelEntityVersion());
447480
CompositeMetadata md = ctx.getTopLevelEntityMetadata();
448481
if (!md.getAccess().getFind().hasAccess(ctx.getCallerRoles())) {
@@ -462,7 +495,11 @@ public Response find(FindRequest req) {
462495
((CompositeFindImpl) finder).setParallelism(9);
463496
}
464497

498+
ctx.measure.begin("finder.find");
465499
CRUDFindResponse result = finder.find(ctx, req.getCRUDFindRequest());
500+
ctx.measure.end("finder.find");
501+
502+
ctx.measure.begin("postProcessFound");
466503
DocumentStream<DocCtx> docStream=ctx.getDocumentStream();
467504
List<ResultMetadata> rmd=new ArrayList<>();
468505
response.setEntityData(factory.getNodeFactory().arrayNode());
@@ -479,12 +516,12 @@ public Response find(FindRequest req) {
479516
}
480517
docStream.close();
481518
response.setResultMetadata(rmd);
519+
ctx.measure.end("postProcessFound");
482520
if (!ctx.hasErrors()) {
483521
ctx.setStatus(OperationStatus.COMPLETE);
484522
} else {
485523
ctx.setStatus(OperationStatus.ERROR);
486524
}
487-
488525
response.setMatchCount(result == null ? 0 : result.getSize());
489526
}
490527
// call any queued up hooks (regardless of status)
@@ -501,6 +538,10 @@ public Response find(FindRequest req) {
501538
LOGGER.debug("Exception during find:{}", e);
502539
response.getErrors().add(Error.get(CrudConstants.ERR_CRUD, e));
503540
} finally {
541+
if(ctx!=null) {
542+
ctx.measure.end("find");
543+
METRICS.debug("find",ctx.measure);
544+
}
504545
Error.pop();
505546
}
506547
return response;
@@ -676,6 +717,7 @@ protected OperationContext newCtx(Request request, CRUDOperation CRUDOperation)
676717
*/
677718
private void runBulkConstraintValidation(OperationContext ctx) {
678719
LOGGER.debug("Bulk constraint validation");
720+
ctx.measure.begin("runBulkConstraintValidation");
679721
EntityMetadata md = ctx.getTopLevelEntityMetadata();
680722
ConstraintValidator constraintValidator = factory.getConstraintValidator(md);
681723
List<DocCtx> docs = ctx.getInputDocumentsWithoutErrors();
@@ -693,9 +735,11 @@ private void runBulkConstraintValidation(OperationContext ctx) {
693735
ctx.addErrors(errors);
694736
}
695737
LOGGER.debug("Constraint validation complete");
738+
ctx.measure.end("runBulkConstraintValidation");
696739
}
697740

698741
private void updatePredefinedFields(OperationContext ctx, CRUDController controller, String entity) {
742+
ctx.measure.begin("updatePredefinedFields");
699743
for (JsonDoc doc : ctx.getInputDocuments()) {
700744
PredefinedFields.updateArraySizes(ctx.getTopLevelEntityMetadata(), factory.getNodeFactory(), doc);
701745
JsonNode node = doc.get(OBJECT_TYPE_PATH);
@@ -706,6 +750,7 @@ private void updatePredefinedFields(OperationContext ctx, CRUDController control
706750
}
707751
controller.updatePredefinedFields(ctx, doc);
708752
}
753+
ctx.measure.end("updatePredefinedFields");
709754
}
710755

711756
/**

0 commit comments

Comments
 (0)