Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): Populating process instance id in jobs #4334

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected void postProcessJob(RestartProcessInstancesBatchConfiguration configur
.findDeployedProcessDefinitionById(configuration.getProcessDefinitionId());
job.setDeploymentId(processDefinitionEntity.getDeploymentId());
}
job.setProcessDefinitionId(configuration.getProcessDefinitionId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ protected void createJobEntities(BatchEntity batch, T configuration, String depl
ByteArrayEntity configurationEntity = saveConfiguration(byteArrayManager, jobConfiguration);

JobEntity job = createBatchJob(batch, configurationEntity);

if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this will be required to avoid populating the ids for entities that are not process instance:

Suggested change
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1
&& !(this instanceof DecisionSetRemovalTimeJobHandler)
&& !(this instanceof DeleteHistoricDecisionInstancesJobHandler)
&& !(this instanceof SetJobRetriesJobHandler)
&& !(this instanceof SetExternalTaskRetriesJobHandler)
// && !(this instanceof BatchSetRemovalTimeJobHandler)
) {

job.setProcessInstanceId(jobConfiguration.getIds().get(0));
}

job.setDeploymentId(deploymentId);
postProcessJob(configuration, job, jobConfiguration);
jobManager.insertAndHintJobExecutor(job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ protected MessageCorrelationBatchConfiguration createJobConfiguration(MessageCor

@Override
protected void postProcessJob(MessageCorrelationBatchConfiguration configuration, JobEntity job, MessageCorrelationBatchConfiguration jobConfiguration) {
// if there is only one process instance to adjust, set its ID to the job so exclusive scheduling is possible
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
job.setProcessInstanceId(jobConfiguration.getIds().get(0));
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public String getType() {

@Override
protected void postProcessJob(BatchConfiguration configuration, JobEntity job, BatchConfiguration jobConfiguration) {
// if there is only one process instance to adjust, set its ID to the job so exclusive scheduling is possible
if (jobConfiguration.getIds() != null && jobConfiguration.getIds().size() == 1) {
job.setProcessInstanceId(jobConfiguration.getIds().get(0));
}

}

protected ByteArrayEntity findByteArrayById(String byteArrayId, CommandContext commandContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.camunda.bpm.engine.test.api.history;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -147,6 +148,19 @@ public void removeBatches() {
ClockUtil.reset();
}

@Test
public void shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations(){
// when
Batch batch = historyService.deleteHistoricDecisionInstancesAsync(decisionInstanceIds, null);
helper.executeSeedJob(batch);

//then
//Making sure that processInstanceId is set in execution jobs #4205
assertThat(helper.getExecutionJobs(batch))
.extracting("processInstanceId")
.containsExactlyInAnyOrder(decisionInstanceIds.toArray());
}

Comment on lines +151 to +163
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ The processInstanceId should not be populated for this type of a batch. It is incorrect to store the decision ids as process instance ids.

Suggested change
@Test
public void shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations(){
// when
Batch batch = historyService.deleteHistoricDecisionInstancesAsync(decisionInstanceIds, null);
helper.executeSeedJob(batch);
//then
//Making sure that processInstanceId is set in execution jobs #4205
assertThat(helper.getExecutionJobs(batch))
.extracting("processInstanceId")
.containsExactlyInAnyOrder(decisionInstanceIds.toArray());
}

@Test
public void createBatchDeletionByIds() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.camunda.bpm.engine.test.api.history;

import static org.assertj.core.api.Assertions.assertThat;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.not;
Expand Down Expand Up @@ -109,6 +111,21 @@ public void testDeleteHistoryProcessInstancesAsyncWithList() throws Exception {
assertAllHistoricProcessInstancesAreDeleted();
}

@Test
public void testDeleteHistoryProcessInstances_shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Better

Suggested change
public void testDeleteHistoryProcessInstances_shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
public void shouldPopulateProcessInstanceIdToBatchJobsForSingleInvocations() {

//when
Batch batch = historyService.deleteHistoricProcessInstancesAsync(historicProcessInstances, TEST_REASON);

completeSeedJobs(batch);
List<Job> executionJobs = managementService.createJobQuery().jobDefinitionId(batch.getBatchJobDefinitionId()).list();

// then
//Making sure that processInstanceId is set in execution jobs #4205
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(historicProcessInstances.toArray());
}

@Test
public void testDeleteHistoryProcessInstancesAsyncWithListForDeletedDeployment() throws Exception {
// given a second deployment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,34 @@ public void shouldSetRemovalTimeForBatch_MultipleInvocationsPerBatchJob() {
assertThat(historicBatches.get(1).getRemovalTime()).isEqualTo(REMOVAL_TIME);
}

@Test
public void testRemovalTimeProcess_shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
// given
testRule.getProcessEngineConfiguration().setInvocationsPerBatchJob(1);

String processInstanceIdOne = testRule.process().userTask().deploy().start();
String processInstanceIdTwo = testRule.process().userTask().deploy().start();

// when
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
Batch batch = historyService.setRemovalTimeToHistoricProcessInstances()
.absoluteRemovalTime(REMOVAL_TIME)
.byQuery(query)
.executeAsync();

testRule.executeSeedJobs(batch);

// then
//Making sure that processInstanceId is set in execution jobs #4205
List<Job> executionJobs = testRule.getExecutionJobs(batch);
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(processInstanceIdOne, processInstanceIdTwo);

// clear
managementService.deleteBatch(batch.getId(), true);
}

@Test
public void shouldSetRemovalTime_SingleInvocationPerBatchJob() {
// given
Expand Down Expand Up @@ -2835,6 +2863,9 @@ public void shouldSetExecutionStartTimeInBatchAndHistoryForDecisions() {
.putValue("temperature", 32)
.putValue("dayType", "Weekend")
).evaluate();

HistoricDecisionInstanceQuery query = historyService.createHistoricDecisionInstanceQuery();

Comment on lines +2866 to +2868
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Not needed:

Suggested change
HistoricDecisionInstanceQuery query = historyService.createHistoricDecisionInstanceQuery();

Batch batch = historyService.setRemovalTimeToHistoricDecisionInstances()
.absoluteRemovalTime(CURRENT_DATE)
.byQuery(historyService.createHistoricDecisionInstanceQuery())
Expand All @@ -2851,6 +2882,11 @@ public void shouldSetExecutionStartTimeInBatchAndHistoryForDecisions() {

assertThat(batch.getExecutionStartTime()).isEqualToIgnoringMillis(CURRENT_DATE);
assertThat(historicBatch.getExecutionStartTime()).isEqualToIgnoringMillis(CURRENT_DATE);

//Making sure that processInstanceId is set in execution jobs #4205
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(query.list().stream().map(HistoricDecisionInstance::getId).toArray());
Comment on lines +2885 to +2889
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Process instance id should not be populated for decision related batches:

Suggested change
//Making sure that processInstanceId is set in execution jobs #4205
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(query.list().stream().map(HistoricDecisionInstance::getId).toArray());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ public void shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
rule.executeSeedJobs(batch);

// then
//Making sure that processInstanceId is set in execution jobs #4205
List<Job> executionJobs = rule.getExecutionJobs(batch);
assertThat(executionJobs)
.extracting("processInstanceId")
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ ✍️ Please check test #createModificationJobs() is failing. processInstanceId assertions must be adjusted.

Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ public void createBatchModification() {
Batch batch = runtimeService.createModification(processDefinition.getId()).startAfterActivity("user2").processInstanceIds(processInstanceIds).executeAsync();

assertBatchCreated(batch, 2);

//Making sure that processInstanceId is set in execution jobs #4205
helper.executeSeedJob(batch);
List<Job> executionJobs = helper.getExecutionJobs(batch);
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(processInstanceIds.toArray());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.camunda.bpm.engine.batch.Batch;
import org.camunda.bpm.engine.batch.history.HistoricBatch;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.history.HistoricJobLog;
import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.cfg.multitenancy.TenantIdProvider;
Expand Down Expand Up @@ -1150,6 +1151,35 @@ public void shouldSetExecutionStartTimeInBatchAndHistory() {
Assertions.assertThat(historicBatch.getExecutionStartTime()).isEqualToIgnoringMillis(TEST_DATE);
}

@Test
public void shouldSetProcessInstanceAndDefinitionIdInHistoryJobLog() {
// given

ProcessDefinition processDefinition = testRule.deployAndGetDefinition(ProcessModels.TWO_TASKS_PROCESS);
ProcessInstance processInstance = runtimeService.createProcessInstanceById(processDefinition.getId())
.startBeforeActivity("userTask1")
.execute();
runtimeService.deleteProcessInstance(processInstance.getId(), "test");
Batch batch = runtimeService.restartProcessInstances(processDefinition.getId())
.startAfterActivity("userTask2")
.processInstanceIds(processInstance.getId())
.executeAsync();
helper.executeSeedJob(batch);

Job executionJob = helper.getExecutionJobs(batch).get(0);
assertEquals(processInstance.getProcessDefinitionId(), executionJob.getProcessDefinitionId());
assertEquals(processInstance.getRootProcessInstanceId(), executionJob.getProcessInstanceId());

yanavasileva marked this conversation as resolved.
Show resolved Hide resolved
// when
helper.executeJob(executionJob);

// then
HistoricJobLog jobLog = historyService.createHistoricJobLogQuery().jobDefinitionType(Batch.TYPE_PROCESS_INSTANCE_RESTART).list().get(0);

assertEquals(processInstance.getProcessDefinitionId(), jobLog.getProcessDefinitionId());
assertEquals(processInstance.getRootProcessInstanceId(), jobLog.getProcessInstanceId());
Comment on lines +1179 to +1180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ The checks are performed for the historic job log, I would expect to check the jobs as well.

}

protected void assertBatchCreated(Batch batch, int processInstanceCount) {
assertNotNull(batch);
assertNotNull(batch.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ public void testDeleteProcessInstancesAsyncWithList() throws Exception {
assertProcessInstancesAreDeleted();
}

@Deployment(resources = {
"org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml"})
@Test
public void testDeleteProcessInstances_shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
// given
List<String> processIds = startTestProcesses(2);

// when
Batch batch = runtimeService.deleteProcessInstancesAsync(processIds, null, TESTING_INSTANCE_DELETE);
completeSeedJobs(batch);

List<Job> executionJobs = managementService.createJobQuery().jobDefinitionId(batch.getBatchJobDefinitionId()).list();

// then
//Making sure that processInstanceId is set in execution jobs #4205
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(processIds.toArray());
}


Comment on lines +115 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@Deployment(resources = {
"org/camunda/bpm/engine/test/api/oneTaskProcess.bpmn20.xml"})
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ public void shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
rule.executeSeedJobs(batch);

// then
//Making sure that processInstanceId is set in execution jobs #4205
List<Job> executionJobs = rule.getExecutionJobs(batch);
assertThat(executionJobs)
.extracting("processInstanceId")
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself:
❓ Tests failures due to Entity was updated by another transaction concurrently. What's wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: org.camunda.bpm.engine.impl.persistence.entity.JobManager.updateJobSuspensionStateByProcessInstanceId(String, SuspensionState)

Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,20 @@ public void testBatchActivationById() {
// when
Batch suspendprocess = runtimeService.updateProcessInstanceSuspensionState().byProcessInstanceIds(Arrays.asList(processInstance1.getId(), processInstance2.getId())).suspendAsync();
helper.completeSeedJobs(suspendprocess);

//Making sure that processInstanceId is set in execution jobs #4205
assertThat(helper.getExecutionJobs(suspendprocess))
.extracting("processInstanceId")
.containsExactlyInAnyOrder(processInstance1.getId(), processInstance2.getId());

helper.executeJobs(suspendprocess);
Batch activateprocess = runtimeService.updateProcessInstanceSuspensionState().byProcessInstanceIds(Arrays.asList(processInstance1.getId(), processInstance2.getId())).activateAsync();
helper.completeSeedJobs(activateprocess);

//Making sure that processInstanceId is set in execution jobs #4205
assertThat(helper.getExecutionJobs(activateprocess))
.extracting("processInstanceId")
.containsExactlyInAnyOrder(processInstance1.getId(), processInstance2.getId());
helper.executeJobs(activateprocess);


Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to myself:
Tests failures due to

java.lang.NullPointerException: null
	at org.camunda.bpm.engine.impl.persistence.entity.JobEntity.setExecution(JobEntity.java:259)
	at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.restoreProcessInstance(ExecutionEntity.java:1384)

See org.camunda.bpm.engine.impl.migration.instance.parser.MigratingInstanceParser.fetchJobs(CommandContext, String)

Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,41 @@ public void shouldSetExecutionStartTimeInBatchAndHistory() {
Assertions.assertThat(historicBatch.getExecutionStartTime()).isEqualToIgnoringMillis(TEST_DATE);
}

@Test
public void shouldCreateProcessInstanceRelatedBatchJobsForSingleInvocations() {
RuntimeService runtimeService = engineRule.getRuntimeService();
int processInstanceCount = 2;

ProcessDefinition sourceProcessDefinition = migrationRule.deployAndGetDefinition(ProcessModels.ONE_TASK_PROCESS);
ProcessDefinition targetProcessDefinition = migrationRule.deployAndGetDefinition(ProcessModels.ONE_TASK_PROCESS);

List<String> processInstanceIds = new ArrayList<>();
for (int i = 0; i < processInstanceCount; i++) {
processInstanceIds.add(
runtimeService.startProcessInstanceById(sourceProcessDefinition.getId()).getId()
);
}

MigrationPlan migrationPlan = engineRule.getRuntimeService()
.createMigrationPlan(sourceProcessDefinition.getId(), targetProcessDefinition.getId())
.mapEqualActivities()
.build();

// when
Batch batch = runtimeService.newMigration(migrationPlan)
.processInstanceIds(processInstanceIds)
.executeAsync();

helper.executeSeedJob(batch);
List<Job> executionJobs = helper.getExecutionJobs(batch);

// then
//Making sure that processInstanceId is set in execution jobs #4205
assertThat(executionJobs)
.extracting("processInstanceId")
.containsExactlyInAnyOrder(processInstanceIds.toArray());
}

protected void assertBatchCreated(Batch batch, int processInstanceCount) {
assertNotNull(batch);
assertNotNull(batch.getId());
Expand Down