Skip to content

Commit

Permalink
[kie-roadmap#52] Improve LogCleanupCommand, do not delete RequestInfo…
Browse files Browse the repository at this point in the history
… and ErrorInfo records of active process instances
  • Loading branch information
martinweiler committed May 5, 2023
1 parent 3e967fb commit 495ba16
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<mapping-file>META-INF/Executor-orm.xml</mapping-file>
<class>org.jbpm.executor.entities.ErrorInfo</class>
<class>org.jbpm.executor.entities.RequestInfo</class>
<class>org.jbpm.process.audit.ProcessInstanceLog</class>
<class>org.jbpm.runtime.manager.impl.jpa.ExecutionErrorInfo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jbpm.process.audit.JPAAuditLogService;
import org.jbpm.process.audit.query.AbstractAuditDeleteBuilderImpl;
import org.kie.api.runtime.CommandExecutor;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.internal.runtime.manager.audit.query.ErrorInfoDeleteBuilder;

import static org.kie.internal.query.QueryParameterIdentifiers.EXECUTOR_TIME_LIST;
Expand Down Expand Up @@ -78,4 +79,25 @@ protected String getQueryTable() {
return ERROR_INFO_LOG_DELETE;
}

@Override
protected boolean isSubquerySupported() {
return true;
}

@Override
protected Subquery applyParameters(Subquery subquery) {
return subquery;
}

@Override
protected Subquery getSubQuery() {
String queryBaseStr = "SELECT r.id from RequestInfo r where r.status in ('ERROR', 'CANCELLED', 'DONE') AND " +
"(r.processInstanceId is null OR r.processInstanceId not in " +
"(SELECT spl.processInstanceId FROM ProcessInstanceLog spl where spl.status in (" +
ProcessInstance.STATE_PENDING + "," + // 0
ProcessInstance.STATE_ACTIVE + "," + // 1
ProcessInstance.STATE_SUSPENDED + // 4
")))";
return new Subquery("l.requestInfo", queryBaseStr, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jbpm.process.audit.query.AbstractAuditDeleteBuilderImpl;
import org.kie.api.executor.STATUS;
import org.kie.api.runtime.CommandExecutor;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.internal.runtime.manager.audit.query.RequestInfoLogDeleteBuilder;

import static org.kie.internal.query.QueryParameterIdentifiers.DEPLOYMENT_ID_LIST;
Expand Down Expand Up @@ -84,7 +85,7 @@ public RequestInfoLogDeleteBuilder status(STATUS... status) {
if (checkIfNull(status)) {
return this;
}

addObjectParameter(EXECUTOR_STATUS_LIST, "status", status);
return this;
}
Expand All @@ -99,5 +100,24 @@ protected String getQueryTable() {
return REQUES_INFO_LOG_DELETE;
}

@Override
protected boolean isSubquerySupported() {
return true;
}

@Override
protected Subquery applyParameters(Subquery subquery) {
return subquery;
}

@Override
protected Subquery getSubQuery() {
String queryBaseStr = "select ri.id from RequestInfo ri where ri.processInstanceId is null " +
"or ri.processInstanceId not in (select pil.processInstanceId from ProcessInstanceLog pil where pil.status in (" +
ProcessInstance.STATE_PENDING + "," + // 0
ProcessInstance.STATE_ACTIVE + "," + // 1
ProcessInstance.STATE_SUSPENDED + // 4
"))";
return new Subquery("l.id", queryBaseStr, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<mapping-file>META-INF/Executor-orm.xml</mapping-file>
<class>org.jbpm.executor.entities.ErrorInfo</class>
<class>org.jbpm.executor.entities.RequestInfo</class>
<class>org.jbpm.process.audit.ProcessInstanceLog</class>
<class>org.jbpm.runtime.manager.impl.jpa.ExecutionErrorInfo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import org.jbpm.executor.ExecutorServiceFactory;
import org.jbpm.executor.impl.ExecutorServiceImpl;
import org.jbpm.executor.impl.jpa.ExecutorJPAAuditService;
import org.jbpm.executor.test.CountDownAsyncJobListener;
import org.jbpm.process.audit.JPAAuditLogService;
import org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler;
Expand Down Expand Up @@ -186,7 +187,7 @@ public void testRunProcessWithAsyncHandlerDontDeleteActive() throws Exception {
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {

Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
handlers.put("async", new DoNothingWorkItemHandler());
handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.ThrowExceptionCommand"));
return handlers;
}

Expand All @@ -204,17 +205,20 @@ public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
assertEquals(0, getNodeInstanceLogSize("ScriptTask"));
assertEquals(0, getTaskLogSize("ScriptTask"));
assertEquals(0, getVariableLogSize("ScriptTask"));
assertEquals(0, getRequestInfoSize());
assertEquals(0, getErrorInfoSize());

Date startDate = new Date();

ProcessInstance processInstance = ksession.startProcess("ScriptTask");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());

assertEquals(1, getProcessLogSize("ScriptTask"));
assertEquals(5, getNodeInstanceLogSize("ScriptTask"));
assertEquals(0, getTaskLogSize("ScriptTask"));
assertEquals(0, getVariableLogSize("ScriptTask"));

assertEquals(1, getRequestInfoSize());

scheduleLogCleanup(false, true, false, startDate, "ScriptTask", "yyyy-MM-dd", manager.getIdentifier());
countDownListener.waitTillCompleted();
System.out.println("Aborting process instance " + processInstance.getId());
Expand All @@ -225,6 +229,8 @@ public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
assertEquals(5, getNodeInstanceLogSize("ScriptTask"));
assertEquals(0, getTaskLogSize("ScriptTask"));
assertEquals(0, getVariableLogSize("ScriptTask"));
assertEquals(2, getRequestInfoSize());
assertEquals(1, getErrorInfoSize());

runtime.getKieSession().abortProcessInstance(processInstance.getId());

Expand All @@ -235,6 +241,8 @@ public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
assertEquals(6, getNodeInstanceLogSize("ScriptTask"));
assertEquals(0, getTaskLogSize("ScriptTask"));
assertEquals(0, getVariableLogSize("ScriptTask"));
assertEquals(2, getRequestInfoSize());
assertEquals(1, getErrorInfoSize());

// and start another one to keep it active while cleanup happens
processInstance = ksession.startProcess("ScriptTask");
Expand All @@ -244,6 +252,8 @@ public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
assertEquals(11, getNodeInstanceLogSize("ScriptTask"));
assertEquals(0, getTaskLogSize("ScriptTask"));
assertEquals(0, getVariableLogSize("ScriptTask"));
assertEquals(3, getRequestInfoSize());
assertEquals(2, getErrorInfoSize());

Thread.sleep(1000);

Expand All @@ -255,6 +265,8 @@ public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
assertEquals(5, getNodeInstanceLogSize("ScriptTask"));
assertEquals(0, getTaskLogSize("ScriptTask"));
assertEquals(0, getVariableLogSize("ScriptTask"));
assertEquals(2, getRequestInfoSize());
assertEquals(1, getErrorInfoSize());
}

@Test
Expand Down Expand Up @@ -327,9 +339,33 @@ private ExecutorService buildExecutorService() {
executorService = ExecutorServiceFactory.newExecutorService(emf);

executorService.init();
executorService.setRetries(0);

return executorService;
}

@Test
public void testCleanupLogOfScheduler() throws Exception {
CountDownAsyncJobListener countDownListener = configureListener(1);
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
.userGroupCallback(userGroupCallback)
.entityManagerFactory(emf)
.get();

manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
int NO_OF_JOBS = 3;
for(int i=0;i<NO_OF_JOBS;i++) {
scheduleLogCleanup(true, true, true, new Date(), null, "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
}
assertEquals(NO_OF_JOBS, getRequestInfoSize());
Thread.sleep(1000);

scheduleLogCleanup(true, true, false, new Date(), null, "yyyy-MM-dd HH:mm:ss", manager.getIdentifier());
countDownListener.reset(1);
countDownListener.waitTillCompleted();

assertEquals(1, getRequestInfoSize());
}

private void scheduleLogCleanup(boolean skipProcessLog,
boolean skipTaskLog, boolean skipExecutorLog, Date olderThan,
Expand Down Expand Up @@ -387,4 +423,18 @@ private int getTaskVariableLogSize(String processId) {
.getResultList()
.size();
}

private int getRequestInfoSize() {
return new ExecutorJPAAuditService(emf).requestInfoQueryBuilder()
.build()
.getResultList()
.size();
}

private int getErrorInfoSize() {
return new ExecutorJPAAuditService(emf).errorInfoQueryBuilder()
.build()
.getResultList()
.size();
}
}

0 comments on commit 495ba16

Please sign in to comment.