Skip to content

Commit

Permalink
optimize work with task tree in PolicyRuleSuspendTaskExecutor and Ope…
Browse files Browse the repository at this point in the history
…rationExecutionRecorder.
  • Loading branch information
katkav committed Sep 28, 2020
1 parent 7dfa017 commit 918e19f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
Expand Up @@ -21,6 +21,8 @@
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.ObjectTreeDeltas;
import com.evolveum.midpoint.schema.ResourceShadowDiscriminator;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
Expand Down Expand Up @@ -109,4 +111,6 @@ default String dumpFocusPolicyRules(int indent) {
Long getSequenceCounter(String sequenceOid);

void setSequenceCounter(String sequenceOid, long counter);

String getTaskTreeOid(Task task, OperationResult result);
}
Expand Up @@ -218,6 +218,8 @@ public enum ExportType {
@NotNull private transient final List<ObjectReferenceType> operationApprovedBy = new ArrayList<>();
@NotNull private transient final List<String> operationApproverComments = new ArrayList<>();

private String taskTreeOid;

public LensContext(Class<F> focusClass, PrismContext prismContext,
ProvisioningService provisioningService) {
Validate.notNull(prismContext, "No prismContext");
Expand Down Expand Up @@ -1725,4 +1727,11 @@ public boolean isExperimentalCodeEnabled() {
return systemConfiguration != null && systemConfiguration.asObjectable().getInternals() != null &&
Boolean.TRUE.equals(systemConfiguration.asObjectable().getInternals().isEnableExperimentalCode());
}

public String getTaskTreeOid(Task task, OperationResult result) throws SchemaException {
if (taskTreeOid == null) {
taskTreeOid = task.getTaskTreeId(result);
}
return taskTreeOid;
}
}
Expand Up @@ -108,8 +108,7 @@ private <F extends ObjectType> boolean recordFocusOperationExecution(LensContext
List<LensObjectDeltaOperation<F>> executedDeltas = getExecutedDeltas(focusContext,
(Class<F>) objectNew.asObjectable().getClass(), clockworkException, result);
LOGGER.trace("recordFocusOperationExecution: executedDeltas: {}", executedDeltas.size());
return recordOperationExecution(objectNew, false, executedDeltas, now, context.getChannel(),
getSkipWhenSuccess(context), task, result);
return recordOperationExecution(context, objectNew, false, executedDeltas, now, task, result);
}

@NotNull
Expand Down Expand Up @@ -149,16 +148,14 @@ private <F extends ObjectType> void recordProjectionOperationExecution(LensConte
}
List<LensObjectDeltaOperation<ShadowType>> executedDeltas = getExecutedDeltas(projectionContext, ShadowType.class,
clockworkException, result);
recordOperationExecution(object, true, executedDeltas, now,
context.getChannel(), getSkipWhenSuccess(context), task, result);
recordOperationExecution(context, object, true, executedDeltas, now, task, result);
}

/**
* @return true if the operation execution was recorded (or would be recorded, but skipped because of the configuration)
*/
private <F extends ObjectType> boolean recordOperationExecution(PrismObject<F> object, boolean deletedOk,
List<LensObjectDeltaOperation<F>> executedDeltas, XMLGregorianCalendar now,
String channel, boolean skipWhenSuccess, Task task, OperationResult result)
private <O extends ObjectType, F extends ObjectType> boolean recordOperationExecution(LensContext<O> context, PrismObject<F> object, boolean deletedOk,
List<LensObjectDeltaOperation<F>> executedDeltas, XMLGregorianCalendar now, Task task, OperationResult result)
throws ObjectAlreadyExistsException, ObjectNotFoundException, SchemaException {
OperationExecutionType operation = new OperationExecutionType(prismContext);
OperationResult summaryResult = new OperationResult("recordOperationExecution");
Expand All @@ -176,10 +173,11 @@ private <F extends ObjectType> boolean recordOperationExecution(PrismObject<F> o
LOGGER.trace("recordOperationExecution: skipping because oid is null for object = {}", object);
return false;
}
createTaskRef(context, operation, task, result);
summaryResult.computeStatus();
OperationResultStatusType overallStatus = summaryResult.getStatus().createStatusType();
setOperationContext(operation, overallStatus, now, channel, task, result);
storeOperationExecution(object, oid, operation, deletedOk, skipWhenSuccess, result);
setOperationContext(operation, overallStatus, now, context.getChannel(), task);
storeOperationExecution(object, oid, operation, deletedOk, getSkipWhenSuccess(context), result);
return true;
}

Expand Down Expand Up @@ -283,17 +281,31 @@ private <F extends ObjectType> void storeOperationExecution(@NotNull PrismObject
}

private void setOperationContext(OperationExecutionType operation,
OperationResultStatusType overallStatus, XMLGregorianCalendar now, String channel, Task task, OperationResult result) throws SchemaException {
OperationResultStatusType overallStatus, XMLGregorianCalendar now, String channel, Task task) {

operation.setStatus(overallStatus);
operation.setInitiatorRef(ObjectTypeUtil.createObjectRef(task.getOwner(), prismContext)); // TODO what if the real initiator is different? (e.g. when executing approved changes)
operation.setChannel(channel);
operation.setTimestamp(now);
}

private <O extends ObjectType> void createTaskRef(LensContext<O> context, OperationExecutionType operation, Task task, OperationResult result) {
if (task instanceof RunningTask && ((RunningTask) task).getParentForLightweightAsynchronousTask() != null) {
task = ((RunningTask) task).getParentForLightweightAsynchronousTask();
}
if (task.isPersistent()) {
operation.setTaskRef(ObjectTypeUtil.createObjectRef(task.getTaskTreeId(result), ObjectTypes.TASK));
String taskOid;
try {
taskOid = context.getTaskTreeOid(task, result);
} catch (SchemaException e) {
//if something unexpeced happened, let's try current task oid
LOGGER.warn("Cannot get task tree oid, current task oid will be used, task: {}, \nreason: {}", task, e.getMessage(), e);
result.recordWarning("Cannot get task tree oid, current task oid will be used, task: " + task + "\nreason: " + e.getMessage(), e);
taskOid = task.getOid();
}

operation.setTaskRef(ObjectTypeUtil.createObjectRef(taskOid, ObjectTypes.TASK));
}
operation.setStatus(overallStatus);
operation.setInitiatorRef(ObjectTypeUtil.createObjectRef(task.getOwner(), prismContext)); // TODO what if the real initiator is different? (e.g. when executing approved changes)
operation.setChannel(channel);
operation.setTimestamp(now);
}

private <F extends ObjectType> ObjectDeltaOperationType createObjectDeltaOperation(LensObjectDeltaOperation<F> deltaOperation) {
Expand Down
Expand Up @@ -43,7 +43,7 @@ public <O extends ObjectType> void execute(@NotNull ModelContext<O> context, Tas
return;
}

String id = task.getTaskTreeId(result);
String id = context.getTaskTreeOid(task, result);
if (id == null) {
LOGGER.trace("No persistent task context, no counting!");
return;
Expand Down

0 comments on commit 918e19f

Please sign in to comment.