Skip to content

Commit

Permalink
[kie-issue-509] fix tests and add SLA process migration
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 4, 2023
1 parent 08e49fa commit 22344f9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ public MigrationReport migrate(Map<String, String> nodeMapping) {
Map<Long, List<TimerInstance>> stateBasedTimer = null;
Map<Long, List<TimerInstance>> slaTimerMigrated = null;
Map<Long, List<TimerInstance>> humanTaskSuspended = null;
TimerInstance slaProcessInstance = null;
try {

// collect and cancel any active timers before migration
timerMigrated = cancelActiveTimersBeforeMigration(currentManager, TimerNodeInstance.class, active -> asList(active.getTimerId()));
stateBasedTimer = cancelActiveTimersBeforeMigration(currentManager, StateBasedNodeInstance.class, active -> active.getTimerInstances());
slaTimerMigrated = cancelActiveTimersBeforeMigration(currentManager, org.jbpm.workflow.instance.impl.NodeInstanceImpl.class, active -> asList(active.getSlaTimerId()));
humanTaskSuspended = cancelActiveTimersBeforeMigration(currentManager, HumanTaskNodeInstance.class, active -> asList((active.getSuspendUntilTimerId())));

slaProcessInstance = cancelSLAActiveTimerProcessInstance(currentManager);

// start transaction to secure consistency of the migration
txm = TransactionManagerFactory.get().newTransactionManager(currentManager.getEnvironment().getEnvironment());
transactionOwner = txm.begin();
Expand Down Expand Up @@ -272,7 +274,7 @@ public MigrationReport migrate(Map<String, String> nodeMapping) {
rescheduleTimersAfterMigration(toBeManager, StateBasedNodeInstance.class, stateBasedTimer, (active, timers) -> active.internalSetTimerInstances(timers.stream().map(TimerInstance::getId).collect(Collectors.toList())));
rescheduleTimersAfterMigration(toBeManager, org.jbpm.workflow.instance.impl.NodeInstanceImpl.class, slaTimerMigrated, (active, timers) -> active.internalSetSlaTimerId(toSingletonTimerId(timers)));
rescheduleTimersAfterMigration(toBeManager, HumanTaskNodeInstance.class, humanTaskSuspended, (active, timers) -> active.setSuspendUntilTimerId(toSingletonTimerId(timers)));

rescheduleTimersAfterMigration(toBeManager, slaProcessInstance);

em.flush();
} finally {
Expand All @@ -292,7 +294,7 @@ public MigrationReport migrate(Map<String, String> nodeMapping) {
rescheduleTimersAfterMigration(currentManager, StateBasedNodeInstance.class, stateBasedTimer, (active, timers) -> active.internalSetTimerInstances(timers.stream().map(TimerInstance::getId).collect(Collectors.toList())));
rescheduleTimersAfterMigration(currentManager, org.jbpm.workflow.instance.impl.NodeInstanceImpl.class, slaTimerMigrated, (active, timers) -> active.internalSetSlaTimerId(toSingletonTimerId(timers)));
rescheduleTimersAfterMigration(currentManager, HumanTaskNodeInstance.class, humanTaskSuspended, (active, timers) -> active.setSuspendUntilTimerId(toSingletonTimerId(timers)));

rescheduleTimersAfterMigration(currentManager, slaProcessInstance);
report.addEntry(Type.ERROR, "Migration of process instance (" + migrationSpec.getProcessInstanceId() + ") failed due to " + e.getMessage());

} finally {
Expand Down Expand Up @@ -592,6 +594,67 @@ protected TimerManager getTimerManager(KieSession ksession) {
return ((InternalProcessRuntime) ((StatefulKnowledgeSessionImpl) internal).getProcessRuntime()).getTimerManager();
}

protected TimerInstance cancelSLAActiveTimerProcessInstance(RuntimeManager manager) {
RuntimeEngine engineBefore = manager.getRuntimeEngine(ProcessInstanceIdContext.get(migrationSpec.getProcessInstanceId()));
try {
TimerInstance timerMigrated = engineBefore.getKieSession().execute(new ExecutableCommand<TimerInstance>() {

@Override
public TimerInstance execute(Context context) {
KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
TimerManager timerManager = getTimerManager(kieSession);
WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kieSession.getProcessInstance(migrationSpec.getProcessInstanceId());
if (processInstance.getSlaTimerId() > 0) {
TimerInstance timerInstance = timerManager.getTimerMap().get(processInstance.getSlaTimerId());
if (timerInstance != null) {
timerManager.cancelTimer(processInstance.getId(), timerInstance.getId());
}

return timerInstance;
}
return null;
}

});
return timerMigrated;
} finally {
manager.disposeRuntimeEngine(engineBefore);
}

}

protected void rescheduleTimersAfterMigration(RuntimeManager manager, TimerInstance timerMigrated) {
if(timerMigrated == null) {
return;
}

RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(migrationSpec.getProcessInstanceId()));
try {
engine.getKieSession().execute(new ExecutableCommand<Void>() {

@Override
public Void execute(Context context) {
KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);

TimerManager timerManager = getTimerManager(kieSession);

WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kieSession.getProcessInstance(migrationSpec.getProcessInstanceId());

long delay = timerMigrated.getDelay() - (System.currentTimeMillis() - timerMigrated.getActivated().getTime());
timerMigrated.setDelay(delay);

updateBasedOnTrigger(timerMigrated);

timerManager.registerTimer(timerMigrated, processInstance);

processInstance.internalSetSlaTimerId(timerMigrated.getId());
return null;
}});
} finally {
manager.disposeRuntimeEngine(engine);
}
}

protected <T extends NodeInstance> Map<Long, List<TimerInstance>> cancelActiveTimersBeforeMigration(RuntimeManager manager, Class<T> type, Function<T, List<Long>> getTimerInstances ) {
RuntimeEngine engineBefore = manager.getRuntimeEngine(ProcessInstanceIdContext.get(migrationSpec.getProcessInstanceId()));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
<bpmn2:participant id="_12CADCE6-BF64-4B86-BD88-8CEE522EDFC2" name="Pool Participant" processRef="BPMN2_ProcessSLA_v1"/>
</bpmn2:collaboration>
<bpmn2:process id="BPMN2-ProcessSLA-v1" drools:packageName="com.example" drools:version="1.0" drools:adHoc="false" name="BPMN2-ProcessSLA-v1" isExecutable="true" processType="Private">
<bpmn2:extensionElements>
<drools:metaData name="customSLADueDate">
<drools:metaValue><![CDATA[PT5S]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
<bpmn2:sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2"/>
<bpmn2:sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3"/>
<bpmn2:startEvent id="_1" name="StartProcess">
<bpmn2:extensionElements>
<drools:metaData name="elementname">
<drools:metaValue><![CDATA[StartProcess]]></drools:metaValue>
</drools:metaData>
<drools:metaData name="customSLADueDate">
<drools:metaValue><![CDATA[3s]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
<bpmn2:outgoing>_1-_2</bpmn2:outgoing>
</bpmn2:startEvent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
<bpmn2:participant id="_79DE678D-5C9E-487D-B14A-035878CC1137" name="Pool Participant" processRef="BPMN2_ProcessSLA_v2"/>
</bpmn2:collaboration>
<bpmn2:process id="BPMN2-ProcessSLA-v2" drools:packageName="com.example" drools:version="1.0" drools:adHoc="false" name="BPMN2-ProcessSLA-v2" isExecutable="true" processType="Private">
<bpmn2:extensionElements>
<drools:metaData name="customSLADueDate">
<drools:metaValue><![CDATA[PT5S]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
<bpmn2:sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2"/>
<bpmn2:sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3"/>
<bpmn2:startEvent id="_1" name="StartProcess">
<bpmn2:extensionElements>
<drools:metaData name="elementname">
<drools:metaValue><![CDATA[StartProcess]]></drools:metaValue>
</drools:metaData>
<drools:metaData name="customSLADueDate">
<drools:metaValue><![CDATA[3s]]></drools:metaValue>
</drools:metaData>
</bpmn2:extensionElements>
<bpmn2:outgoing>_1-_2</bpmn2:outgoing>
</bpmn2:startEvent>
Expand Down

0 comments on commit 22344f9

Please sign in to comment.