Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.Objects;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -90,6 +91,7 @@ public Long migrateTransactionally(HistoricProcessInstance c7ProcessInstance) {
var builder = new ProcessInstanceDbModelBuilder();

Long processInstanceKey = getNextKey();
Long rootProcessInstanceKey = null;
builder.processInstanceKey(processInstanceKey);
if (processDefinitionKey != null) {
builder.processDefinitionKey(processDefinitionKey);
Expand All @@ -112,6 +114,7 @@ public Long migrateTransactionally(HistoricProcessInstance c7ProcessInstance) {
} else if (c7RootProcessInstanceId != null && isMigrated(c7RootProcessInstanceId, HISTORY_PROCESS_INSTANCE)) {
ProcessInstanceEntity rootProcessInstance = findProcessInstanceByC7Id(c7RootProcessInstanceId);
if (rootProcessInstance != null && rootProcessInstance.processInstanceKey() != null) {
rootProcessInstanceKey = rootProcessInstance.processInstanceKey();
builder.rootProcessInstanceKey(rootProcessInstance.processInstanceKey());
}
}
Expand All @@ -123,7 +126,8 @@ public Long migrateTransactionally(HistoricProcessInstance c7ProcessInstance) {

builder
.historyCleanupDate(c8HistoryCleanupDate)
.endDate(c8EndTime);
.endDate(c8EndTime)
.treePath(generateTreepath(rootProcessInstanceKey, processInstanceKey));

ProcessInstanceDbModel dbModel = convert(C7Entity.of(c7ProcessInstance), builder);

Expand Down Expand Up @@ -176,5 +180,20 @@ protected void resolveParentFlowNodeInstanceKey(
}
}

/**
* Generates a tree path for process instances in the format:
* PI_rootProcessInstanceKey/PI_processInstanceKey or
* PI_processInstanceKey if this instance is the root of the hierarchy
*
* @param rootProcessInstanceKey the root process instance key
* @param processInstanceKey the process instance key
* @return the tree path string
*/
public static String generateTreepath(Long rootProcessInstanceKey, Long processInstanceKey) {
return rootProcessInstanceKey == null || Objects.equals(rootProcessInstanceKey, processInstanceKey) ?
"PI_" + processInstanceKey :
"PI_" + rootProcessInstanceKey + "/PI_" + processInstanceKey;
Copy link
Member

Choose a reason for hiding this comment

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

❌ If the hierarchy has more then 2 levels for example a chain A→B→C, process C's treePath would be PI_A/PI_C, skipping B.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See the ticket description/slack link, the treepath is truncated to only keep root and leaf

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I missed that 👍

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void execute(HistoricProcessInstance entity, ProcessInstanceDbModelBuilde
.tenantId(getTenantId(entity.getTenantId()))
.version(entity.getProcessDefinitionVersion())
.tags(getDefaultTags(entity))
.treePath(null)
.numIncidents(0)
.partitionId(C7_HISTORY_PARTITION_ID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ public void shouldCheckCalledProcessParentElementKey() {
assertThat(searchHistoricProcessInstances("calledProcessInstanceId")).isEmpty();
}

@Test
public void shouldPopulateTreePathForCallActivity() {
// given
deployer.deployCamunda7Process("callActivityProcess.bpmn");
deployer.deployCamunda7Process("calledActivitySubprocess.bpmn");
runtimeService.startProcessInstanceByKey("callingProcessId");

// when
historyMigrator.migrate();
historyMigrator.retry();

// then
List<ProcessInstanceEntity> parentProcessInstances = searchHistoricProcessInstances("callingProcessId");
List<ProcessInstanceEntity> subProcessInstances = searchHistoricProcessInstances("calledProcessInstanceId");
assertThat(parentProcessInstances).hasSize(1);
assertThat(subProcessInstances).hasSize(1);

ProcessInstanceEntity parent = parentProcessInstances.getFirst();
ProcessInstanceEntity sub = subProcessInstances.getFirst();

assertThat(parent.treePath()).isNotNull().isEqualTo("PI_" + parent.processInstanceKey());

assertThat(sub.treePath()).isNotNull()
.isEqualTo("PI_" + parent.processInstanceKey() + "/PI_" + sub.processInstanceKey());
}

@Test
public void shouldPopulateRootProcessInstanceKeyForCallActivity() {
// given
Expand All @@ -196,7 +222,7 @@ public void shouldPopulateRootProcessInstanceKeyForCallActivity() {

// Sub process should have rootProcessInstanceKey pointing to the parent
assertThat(sub.rootProcessInstanceKey()).isEqualTo(parent.processInstanceKey());

// Verify that flow nodes also have rootProcessInstanceKey
List<FlowNodeInstanceEntity> subFlowNodes = searchHistoricFlowNodes(sub.processInstanceKey());
assertThat(subFlowNodes).isNotEmpty();
Expand Down Expand Up @@ -310,7 +336,13 @@ protected void verifyProcessInstanceFields(ProcessInstanceEntity processInstance
}

assertThat(processInstance.hasIncident()).isEqualTo(hasIncidents);
assertThat(processInstance.treePath()).isNull();

assertThat(processInstance.treePath()).isNotNull();
if (hasParent) {
assertThat(processInstance.treePath()).matches("PI_\\d+/PI_" + processInstance.processInstanceKey());
} else {
assertThat(processInstance.treePath()).isEqualTo("PI_" + processInstance.processInstanceKey());
}
}

@Test
Expand Down