From 495ba16c10a9b53950dcc7597b2e85f1ad4260f6 Mon Sep 17 00:00:00 2001 From: Martin Weiler Date: Thu, 4 May 2023 14:53:00 -0600 Subject: [PATCH] [kie-roadmap#52] Improve LogCleanupCommand, do not delete RequestInfo and ErrorInfo records of active process instances --- .../META-INF/persistence.xml | 1 + .../impl/jpa/ErrorInfoDeleteBuilderImpl.java | 22 ++++++++ .../jpa/RequestInfoDeleteBuilderImpl.java | 22 +++++++- .../META-INF/persistence.xml | 1 + .../wih/CleanupLogCommandWithProcessTest.java | 56 ++++++++++++++++++- 5 files changed, 98 insertions(+), 4 deletions(-) diff --git a/jbpm-services/jbpm-executor-cdi/src/test/filtered-resources/META-INF/persistence.xml b/jbpm-services/jbpm-executor-cdi/src/test/filtered-resources/META-INF/persistence.xml index 395b7b6cb6..5ba4e8de5e 100644 --- a/jbpm-services/jbpm-executor-cdi/src/test/filtered-resources/META-INF/persistence.xml +++ b/jbpm-services/jbpm-executor-cdi/src/test/filtered-resources/META-INF/persistence.xml @@ -10,6 +10,7 @@ META-INF/Executor-orm.xml org.jbpm.executor.entities.ErrorInfo org.jbpm.executor.entities.RequestInfo + org.jbpm.process.audit.ProcessInstanceLog org.jbpm.runtime.manager.impl.jpa.ExecutionErrorInfo true diff --git a/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/ErrorInfoDeleteBuilderImpl.java b/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/ErrorInfoDeleteBuilderImpl.java index a04f5dd919..440a7fb496 100644 --- a/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/ErrorInfoDeleteBuilderImpl.java +++ b/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/ErrorInfoDeleteBuilderImpl.java @@ -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; @@ -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); + } } diff --git a/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/RequestInfoDeleteBuilderImpl.java b/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/RequestInfoDeleteBuilderImpl.java index 0865e72bdb..c7922b293d 100644 --- a/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/RequestInfoDeleteBuilderImpl.java +++ b/jbpm-services/jbpm-executor/src/main/java/org/jbpm/executor/impl/jpa/RequestInfoDeleteBuilderImpl.java @@ -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; @@ -84,7 +85,7 @@ public RequestInfoLogDeleteBuilder status(STATUS... status) { if (checkIfNull(status)) { return this; } - + addObjectParameter(EXECUTOR_STATUS_LIST, "status", status); return this; } @@ -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); + } } diff --git a/jbpm-services/jbpm-executor/src/test/filtered-resources/META-INF/persistence.xml b/jbpm-services/jbpm-executor/src/test/filtered-resources/META-INF/persistence.xml index ee6f6ce0db..0f2370f75c 100644 --- a/jbpm-services/jbpm-executor/src/test/filtered-resources/META-INF/persistence.xml +++ b/jbpm-services/jbpm-executor/src/test/filtered-resources/META-INF/persistence.xml @@ -10,6 +10,7 @@ META-INF/Executor-orm.xml org.jbpm.executor.entities.ErrorInfo org.jbpm.executor.entities.RequestInfo + org.jbpm.process.audit.ProcessInstanceLog org.jbpm.runtime.manager.impl.jpa.ExecutionErrorInfo true diff --git a/jbpm-services/jbpm-executor/src/test/java/org/jbpm/executor/impl/wih/CleanupLogCommandWithProcessTest.java b/jbpm-services/jbpm-executor/src/test/java/org/jbpm/executor/impl/wih/CleanupLogCommandWithProcessTest.java index af64b3c13c..850777190f 100644 --- a/jbpm-services/jbpm-executor/src/test/java/org/jbpm/executor/impl/wih/CleanupLogCommandWithProcessTest.java +++ b/jbpm-services/jbpm-executor/src/test/java/org/jbpm/executor/impl/wih/CleanupLogCommandWithProcessTest.java @@ -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; @@ -186,7 +187,7 @@ public void testRunProcessWithAsyncHandlerDontDeleteActive() throws Exception { public Map getWorkItemHandlers(RuntimeEngine runtime) { Map handlers = super.getWorkItemHandlers(runtime); - handlers.put("async", new DoNothingWorkItemHandler()); + handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.ThrowExceptionCommand")); return handlers; } @@ -204,17 +205,20 @@ public Map 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()); @@ -225,6 +229,8 @@ public Map 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()); @@ -235,6 +241,8 @@ public Map 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"); @@ -244,6 +252,8 @@ public Map getWorkItemHandlers(RuntimeEngine runtime) { assertEquals(11, getNodeInstanceLogSize("ScriptTask")); assertEquals(0, getTaskLogSize("ScriptTask")); assertEquals(0, getVariableLogSize("ScriptTask")); + assertEquals(3, getRequestInfoSize()); + assertEquals(2, getErrorInfoSize()); Thread.sleep(1000); @@ -255,6 +265,8 @@ public Map getWorkItemHandlers(RuntimeEngine runtime) { assertEquals(5, getNodeInstanceLogSize("ScriptTask")); assertEquals(0, getTaskLogSize("ScriptTask")); assertEquals(0, getVariableLogSize("ScriptTask")); + assertEquals(2, getRequestInfoSize()); + assertEquals(1, getErrorInfoSize()); } @Test @@ -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