|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | +package org.flowable.engine.test.bpmn.async; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.atomic.AtomicLong; |
| 22 | + |
| 23 | +import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; |
| 24 | +import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent; |
| 25 | +import org.flowable.common.engine.api.delegate.event.FlowableEvent; |
| 26 | +import org.flowable.common.engine.api.delegate.event.FlowableEventListener; |
| 27 | +import org.flowable.common.engine.impl.interceptor.Command; |
| 28 | +import org.flowable.common.engine.impl.interceptor.CommandConfig; |
| 29 | +import org.flowable.common.engine.impl.interceptor.CommandExecutor; |
| 30 | +import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; |
| 31 | +import org.flowable.engine.impl.interceptor.CommandInvoker; |
| 32 | +import org.flowable.engine.impl.jobexecutor.AsyncContinuationJobHandler; |
| 33 | +import org.flowable.engine.impl.jobexecutor.ParallelMultiInstanceActivityCompletionJobHandler; |
| 34 | +import org.flowable.engine.runtime.ProcessInstance; |
| 35 | +import org.flowable.engine.test.Deployment; |
| 36 | +import org.flowable.engine.test.impl.CustomConfigurationFlowableTestCase; |
| 37 | +import org.flowable.job.api.FlowableUnrecoverableJobException; |
| 38 | +import org.flowable.job.api.Job; |
| 39 | +import org.flowable.job.service.impl.cmd.LockExclusiveJobCmd; |
| 40 | +import org.flowable.job.service.impl.persistence.entity.JobEntity; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author Filip Hrisafov |
| 45 | + */ |
| 46 | +class ParallelMultiInstanceAsyncNonExclusiveTest extends CustomConfigurationFlowableTestCase { |
| 47 | + |
| 48 | + protected CustomCommandInvoker customCommandInvoker; |
| 49 | + protected CustomEventListener customEventListener; |
| 50 | + protected CollectingAsyncRunnableExecutionExceptionHandler executionExceptionHandler; |
| 51 | + |
| 52 | + public ParallelMultiInstanceAsyncNonExclusiveTest() { |
| 53 | + super("parallelMultiInstanceAsyncNonExclusiveTest"); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void configureConfiguration(ProcessEngineConfigurationImpl processEngineConfiguration) { |
| 58 | + customCommandInvoker = new CustomCommandInvoker(); |
| 59 | + processEngineConfiguration.setCommandInvoker(customCommandInvoker); |
| 60 | + processEngineConfiguration.getAsyncExecutorConfiguration().setGlobalAcquireLockEnabled(true); |
| 61 | + executionExceptionHandler = new CollectingAsyncRunnableExecutionExceptionHandler(); |
| 62 | + processEngineConfiguration.setCustomAsyncRunnableExecutionExceptionHandlers(Collections.singletonList(executionExceptionHandler)); |
| 63 | + customEventListener = new CustomEventListener(); |
| 64 | + processEngineConfiguration.setEventListeners(Collections.singletonList(customEventListener)); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @Deployment |
| 70 | + public void parallelMultiInstanceNonExclusiveJobs() { |
| 71 | + // This test is trying to cause an optimistic locking exception when using non-exclusive parallel multi instance jobs. |
| 72 | + // This is mimicking the following scenario: |
| 73 | + // 4 async jobs complete in the same time, and thus they create 4 parallel-multi-instance-complete exclusive jobs |
| 74 | + // 3 of those jobs will fail to get the exclusive lock and unacquire their jobs and 1 will get the lock |
| 75 | + // the one that will get the lock will continue to the next step of the process and perform the multi instance cleanup |
| 76 | + // the cleanup of the multi instance should not fail. |
| 77 | + |
| 78 | + ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder() |
| 79 | + .processDefinitionKey("parallelScriptTask") |
| 80 | + .start(); |
| 81 | + |
| 82 | + List<Job> jobs = managementService.createJobQuery().list(); |
| 83 | + assertThat(jobs).hasSize(4); |
| 84 | + assertThat(jobs) |
| 85 | + .extracting(Job::getJobHandlerType) |
| 86 | + .containsOnly(AsyncContinuationJobHandler.TYPE); |
| 87 | + customCommandInvoker.lockExclusiveCounter = new AtomicLong(0L); |
| 88 | + |
| 89 | + customCommandInvoker.executeLockReleaseLatch = new CountDownLatch(1); |
| 90 | + customEventListener.parallelMultiInstanceCompleteLatch = customCommandInvoker.executeLockReleaseLatch; |
| 91 | + |
| 92 | + customCommandInvoker.executeAsyncRunnableLatch = new CountDownLatch(4); |
| 93 | + customEventListener.asyncContinuationLatch = new CountDownLatch(4); |
| 94 | + |
| 95 | + customCommandInvoker.executeLockCountLatch = new CountDownLatch(3); |
| 96 | + customEventListener.parallelMultiInstanceWaitCompleteLatch = customCommandInvoker.executeLockCountLatch; |
| 97 | + |
| 98 | + waitForJobExecutorToProcessAllJobs(15_000, 200); |
| 99 | + |
| 100 | + assertThat(executionExceptionHandler.getExceptions()).isEmpty(); |
| 101 | + assertThat(managementService.createJobQuery().processInstanceId(processInstance.getId()).list()).isEmpty(); |
| 102 | + assertThat(managementService.createDeadLetterJobQuery().processInstanceId(processInstance.getId()).list()).isEmpty(); |
| 103 | + } |
| 104 | + |
| 105 | + protected static class CustomCommandInvoker extends CommandInvoker { |
| 106 | + |
| 107 | + protected AtomicLong lockExclusiveCounter = new AtomicLong(); |
| 108 | + protected CountDownLatch executeLockCountLatch; |
| 109 | + protected CountDownLatch executeLockReleaseLatch; |
| 110 | + protected CountDownLatch executeAsyncRunnableLatch; |
| 111 | + |
| 112 | + protected CustomCommandInvoker() { |
| 113 | + super(((commandContext, runnable) -> runnable.run()), null); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public <T> T execute(CommandConfig config, Command<T> command, CommandExecutor commandExecutor) { |
| 118 | + if (command instanceof LockExclusiveJobCmd) { |
| 119 | + if (lockExclusiveCounter.incrementAndGet() > 1) { |
| 120 | + // We let the first exclusive to run without waiting |
| 121 | + // we then wait to complete this transaction until the execute lock exclusive is released |
| 122 | + try { |
| 123 | + executeLockCountLatch.countDown(); |
| 124 | + executeLockReleaseLatch.await(4, TimeUnit.SECONDS); |
| 125 | + } catch (InterruptedException e) { |
| 126 | + Thread.currentThread().interrupt(); |
| 127 | + throw new RuntimeException(e); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return super.execute(config, command, commandExecutor); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + protected static class CustomEventListener implements FlowableEventListener { |
| 136 | + |
| 137 | + protected CountDownLatch asyncContinuationLatch; |
| 138 | + protected CountDownLatch parallelMultiInstanceCompleteLatch; |
| 139 | + protected CountDownLatch parallelMultiInstanceWaitCompleteLatch; |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onEvent(FlowableEvent event) { |
| 143 | + if (FlowableEngineEventType.JOB_EXECUTION_SUCCESS.equals(event.getType()) && event instanceof FlowableEntityEvent) { |
| 144 | + JobEntity entity = (JobEntity) ((FlowableEntityEvent) event).getEntity(); |
| 145 | + String jobHandlerType = entity.getJobHandlerType(); |
| 146 | + if (AsyncContinuationJobHandler.TYPE.equals(jobHandlerType)) { |
| 147 | + // We are going to wait for all the async jobs to complete in the same time |
| 148 | + asyncContinuationLatch.countDown(); |
| 149 | + try { |
| 150 | + if (!asyncContinuationLatch.await(4, TimeUnit.SECONDS)) { |
| 151 | + throw new FlowableUnrecoverableJobException("asyncContinuationLatch did not reach 0"); |
| 152 | + } |
| 153 | + } catch (InterruptedException e) { |
| 154 | + Thread.currentThread().interrupt(); |
| 155 | + throw new RuntimeException(e); |
| 156 | + } |
| 157 | + } else if (ParallelMultiInstanceActivityCompletionJobHandler.TYPE.equals(jobHandlerType)) { |
| 158 | + // There will be one multi instance complete job, so we count it down to release the rest of the lock exclusive commands |
| 159 | + parallelMultiInstanceCompleteLatch.countDown(); |
| 160 | + |
| 161 | + try { |
| 162 | + // Wait for the rest of the lock exclusive commands to complete before resuming this transaction |
| 163 | + if (!parallelMultiInstanceWaitCompleteLatch.await(4, TimeUnit.SECONDS)) { |
| 164 | + throw new FlowableUnrecoverableJobException("parallelMultiInstanceWaitLatch did not reach 0"); |
| 165 | + } |
| 166 | + } catch (InterruptedException e) { |
| 167 | + Thread.currentThread().interrupt(); |
| 168 | + throw new RuntimeException(e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean isFailOnException() { |
| 178 | + return true; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public boolean isFireOnTransactionLifecycleEvent() { |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public String getOnTransaction() { |
| 188 | + return null; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments