Skip to content

Commit

Permalink
[kie-issues#843] Replace random UUID generator with a deterministic r…
Browse files Browse the repository at this point in the history
…andom one. (apache#3394)
  • Loading branch information
baldimir authored and rgdoliveira committed Mar 11, 2024
1 parent d7e0974 commit 3d30404
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand All @@ -53,6 +52,7 @@
import org.jbpm.process.core.impl.DataTransformerRegistry;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.jbpm.util.PatternConstants;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.DroolsAction;
import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.NodeContainer;
Expand Down Expand Up @@ -650,7 +650,7 @@ private String cleanUp(String expression) {
}

private DataDefinition toDataExpression(String expression) {
DataDefinition dataSpec = new DataDefinition(UUID.randomUUID().toString(), "EXPRESSION (" + expression + ")", null);
DataDefinition dataSpec = new DataDefinition(UUIDGenerator.getID(), "EXPRESSION (" + expression + ")", null);
dataSpec.setExpression(expression);
return dataSpec;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.jbpm.compiler.canonical;

import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;

Expand Down Expand Up @@ -117,7 +118,9 @@ public void visitNode(String factoryField, T node, BlockStmt body, VariableScope
}

protected void addWorkItemParameters(Work work, BlockStmt body, String variableName) {
for (Entry<String, Object> entry : work.getParameters().entrySet()) {
// This is to ensure that each run of the generator produces the same code.
final List<Entry<String, Object>> sortedParameters = work.getParameters().entrySet().stream().sorted(Entry.comparingByKey()).toList();
for (Entry<String, Object> entry : sortedParameters) {
if (entry.getValue() == null) {
continue; // interfaceImplementationRef ?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.jbpm.process.instance.ProcessInstanceManager;
import org.jbpm.util.UUIDGenerator;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;

public class DefaultProcessInstanceManager implements ProcessInstanceManager {
Expand All @@ -36,7 +36,7 @@ public class DefaultProcessInstanceManager implements ProcessInstanceManager {

public void addProcessInstance(KogitoProcessInstance processInstance) {
if (Objects.isNull(processInstance.getStringId())) {
((org.jbpm.process.instance.ProcessInstance) processInstance).setId(UUID.randomUUID().toString());
((org.jbpm.process.instance.ProcessInstance) processInstance).setId(UUIDGenerator.getID());
}
internalAddProcessInstance(processInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import java.io.Serializable;
import java.util.Map;
import java.util.UUID;

import org.jbpm.process.instance.impl.Action;
import org.jbpm.ruleflow.core.Metadata;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.impl.NodeIoHelper;
import org.jbpm.workflow.instance.impl.NodeInstanceImpl;
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
Expand All @@ -45,7 +45,7 @@ public HandleMessageAction(String messageType, String variableName) {
@Override
public void execute(KogitoProcessContext context) throws Exception {
KogitoWorkItemImpl workItem = new KogitoWorkItemImpl();
workItem.setId(UUID.randomUUID().toString());
workItem.setId(UUIDGenerator.getID());
workItem.setName("Send Task");
workItem.setNodeInstanceId((context.getNodeInstance()).getStringId());
workItem.setProcessInstanceId((context.getProcessInstance()).getStringId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

import org.jbpm.process.instance.impl.Action;
import org.jbpm.process.instance.impl.util.VariableUtil;
import org.jbpm.util.UUIDGenerator;
import org.kie.kogito.internal.process.runtime.KogitoNodeInstance;
import org.kie.kogito.internal.process.runtime.KogitoProcessContext;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void execute(KogitoProcessContext context) throws Exception {
context.getProcessInstance().signalEvent(signalName, signal);
} else if (EXTERNAL_SCOPE.equals(scope)) {
KogitoWorkItemImpl workItem = new KogitoWorkItemImpl();
workItem.setId(UUID.randomUUID().toString());
workItem.setId(UUIDGenerator.getID());
workItem.setName("External Send Task");
workItem.setNodeInstanceId(context.getNodeInstance().getStringId());
workItem.setProcessInstanceId(context.getProcessInstance().getStringId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.nio.file.Paths;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.instance.NodeInstance;
import org.jbpm.workflow.instance.node.WorkItemNodeInstance;
import org.kie.kogito.MapOutput;
Expand Down Expand Up @@ -179,6 +179,6 @@ private static <K extends Serializable, T extends Serializable, C extends TaskMe
}

private static String getNewId() {
return UUID.randomUUID().toString();
return UUIDGenerator.getID();
}
}
37 changes: 37 additions & 0 deletions jbpm/jbpm-flow/src/main/java/org/jbpm/util/UUIDGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.jbpm.util;

import java.util.Random;
import java.util.UUID;

public final class UUIDGenerator {
private static final Random RANDOM = new Random(1);

private UUIDGenerator() {
// It is not allowed to create instances of a util class.
}

public static synchronized String getID() {
byte[] array = new byte[16];
RANDOM.nextBytes(array);
return String.valueOf(UUID.nameUUIDFromBytes(array));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
package org.jbpm.workflow.core.impl;

import java.io.Serializable;
import java.util.UUID;

import org.jbpm.util.UUIDGenerator;

public class DataDefinition implements Serializable {

Expand All @@ -31,7 +32,7 @@ public class DataDefinition implements Serializable {
private String expression;

public DataDefinition(String expression) {
this.id = UUID.randomUUID().toString();
this.id = UUIDGenerator.getID();
this.label = "EXPRESSION - (" + expression + ")";
this.type = "java.lang.Object";
this.expression = expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -52,6 +51,7 @@
import org.jbpm.process.instance.impl.ProcessInstanceImpl;
import org.jbpm.ruleflow.core.Metadata;
import org.jbpm.util.PatternConstants;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.DroolsAction;
import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.impl.NodeImpl;
Expand Down Expand Up @@ -165,7 +165,7 @@ public void addNodeInstance(final NodeInstance nodeInstance) {
if (nodeInstance.getStringId() == null) {
// assign new id only if it does not exist as it might already be set by marshalling
// it's important to keep same ids of node instances as they might be references e.g. exclusive group
((NodeInstanceImpl) nodeInstance).setId(UUID.randomUUID().toString());
((NodeInstanceImpl) nodeInstance).setId(UUIDGenerator.getID());
}
this.nodeInstances.add(nodeInstance);
}
Expand Down Expand Up @@ -563,7 +563,7 @@ public TimerInstance configureSLATimer(String slaDueDateExpression) {

private TimerInstance createDurationTimer(long duration) {
TimerInstance timerInstance = new TimerInstance();
timerInstance.setId(UUID.randomUUID().toString());
timerInstance.setId(UUIDGenerator.getID());
timerInstance.setTimerId("-1");
timerInstance.setDelay(duration);
timerInstance.setPeriod(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;

import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.node.CompositeNode;
import org.jbpm.workflow.core.node.EventNode;
Expand Down Expand Up @@ -191,7 +191,7 @@ public void addNodeInstance(final NodeInstance nodeInstance) {
if (nodeInstance.getStringId() == null) {
// assign new id only if it does not exist as it might already be set by marshalling
// it's important to keep same ids of node instances as they might be references e.g. exclusive group
((NodeInstanceImpl) nodeInstance).setId(UUID.randomUUID().toString());
((NodeInstanceImpl) nodeInstance).setId(UUIDGenerator.getID());
}
this.nodeInstances.add(nodeInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;

import org.drools.core.common.InternalWorkingMemory;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.util.PatternConstants;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
import org.jbpm.workflow.instance.impl.MVELProcessHelper;
import org.jbpm.workflow.instance.impl.NodeInstanceImpl;
Expand Down Expand Up @@ -94,7 +94,7 @@ private static void internalAddDynamicWorkItem(
String workItemName,
Map<String, Object> parameters) {
final KogitoWorkItemImpl workItem = new KogitoWorkItemImpl();
workItem.setId(UUID.randomUUID().toString());
workItem.setId(UUIDGenerator.getID());
workItem.setState(WorkItem.ACTIVE);
workItem.setProcessInstanceId(processInstance.getStringId());
workItem.setDeploymentId((String) ksession.getEnvironment().get(EnvironmentName.DEPLOYMENT_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import org.jbpm.process.core.context.exception.ExceptionScope;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.context.exception.ExceptionScopeInstance;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.node.TimerNode;
import org.kie.api.runtime.process.EventListener;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void internalTrigger(KogitoNodeInstance from, String type) {
if (getTimerInstances() == null) {
addTimerListener();
}
internalSetTimerId(UUID.randomUUID().toString());
internalSetTimerId(UUIDGenerator.getID());
final InternalProcessRuntime processRuntime = (InternalProcessRuntime) getProcessInstance().getKnowledgeRuntime().getProcessRuntime();
//Deffer the timer scheduling to the end of current UnitOfWork execution chain
processRuntime.getUnitOfWorkManager().currentUnitOfWork().intercept(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;

import org.drools.core.WorkItemHandlerNotFoundException;
Expand All @@ -47,6 +46,7 @@
import org.jbpm.process.instance.context.variable.VariableScopeInstance;
import org.jbpm.process.instance.impl.ContextInstanceFactory;
import org.jbpm.process.instance.impl.ContextInstanceFactoryRegistry;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.Node;
import org.jbpm.workflow.core.impl.DataAssociation;
import org.jbpm.workflow.core.impl.NodeIoHelper;
Expand Down Expand Up @@ -222,7 +222,7 @@ protected InternalKogitoWorkItem newWorkItem() {
protected InternalKogitoWorkItem createWorkItem(WorkItemNode workItemNode) {
Work work = workItemNode.getWork();
workItem = newWorkItem();
workItem.setId(UUID.randomUUID().toString());
workItem.setId(UUIDGenerator.getID());
workItem.setName(work.getName());
workItem.setProcessInstanceId(getProcessInstance().getStringId());
workItem.setProcessInstance(this.getKogitoProcessInstance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
package org.kie.kogito.process.impl;

import java.util.UUID;

import org.jbpm.process.instance.impl.DefaultProcessInstanceManager;
import org.jbpm.util.UUIDGenerator;
import org.junit.jupiter.api.Test;
import org.kie.kogito.internal.process.runtime.KogitoProcessInstance;

Expand All @@ -34,7 +33,7 @@ public class DefaultProcessInstanceManagerTest {
public void testCreateProcessInstance() {
DefaultProcessInstanceManager pim = new DefaultProcessInstanceManager();
final String processId = "processId";
final String instanceId = UUID.randomUUID().toString();
final String instanceId = UUIDGenerator.getID();
KogitoProcessInstance kpi = mock(KogitoProcessInstance.class);
when(kpi.getProcessId()).thenReturn(processId);
when(kpi.getStringId()).thenReturn(instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -42,6 +41,7 @@
import org.jbpm.process.instance.impl.humantask.InternalHumanTaskWorkItem;
import org.jbpm.process.instance.impl.humantask.Reassignment;
import org.jbpm.ruleflow.instance.RuleFlowProcessInstance;
import org.jbpm.util.UUIDGenerator;
import org.jbpm.workflow.core.node.AsyncEventNodeInstance;
import org.jbpm.workflow.instance.impl.NodeInstanceImpl;
import org.jbpm.workflow.instance.node.CompositeContextNodeInstance;
Expand Down Expand Up @@ -566,7 +566,7 @@ private WorkItemNodeInstance instanceWorkItem(WorkItemNodeInstanceContent conten
} else {
WorkItemNodeInstance nodeInstance = new WorkItemNodeInstance();
KogitoWorkItemImpl workItem = new KogitoWorkItemImpl();
workItem.setId(UUID.randomUUID().toString());
workItem.setId(UUIDGenerator.getID());
nodeInstance.internalSetWorkItem(workItem);
return nodeInstance;
}
Expand Down

0 comments on commit 3d30404

Please sign in to comment.