Skip to content

Commit e8b9902

Browse files
committed
fix: random orchestrator uses repeatable random seed
1 parent eb10fba commit e8b9902

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

plugins/orchestrator-plugin/src/main/java/org/rundeck/plugin/example/MaxPercentageOrchestator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class MaxPercentageOrchestator implements Orchestrator {
3636

3737

3838
public MaxPercentageOrchestator(StepExecutionContext context, Collection<INodeEntry> nodes, int percentage) {
39-
this.list = new ArrayList<INodeEntry>(nodes);
39+
this.list = new ArrayList<>(nodes);
4040
percentage = validPercentage(percentage);
4141
this.max = calculateMax(nodes.size(), percentage);
4242
if (context != null) {

plugins/orchestrator-plugin/src/main/java/org/rundeck/plugin/example/RandomSubsetOrchestrator.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@
3131
*/
3232
public class RandomSubsetOrchestrator implements Orchestrator {
3333

34-
Random random = new Random(System.currentTimeMillis());
34+
Random random;
35+
long seed;
3536
final int count;
3637
List<INodeEntry> nodes;
3738

38-
public RandomSubsetOrchestrator(int count, StepExecutionContext context, Collection<INodeEntry> nodes) {
39+
public RandomSubsetOrchestrator(
40+
int count,
41+
StepExecutionContext context,
42+
Collection<INodeEntry> nodes, final long seed
43+
)
44+
{
45+
this.seed = seed;
46+
this.random = new Random(seed);
3947
this.count = count;
4048
this.nodes = select(count, nodes);
4149
}

plugins/orchestrator-plugin/src/main/java/org/rundeck/plugin/example/RandomSubsetOrchestratorPlugin.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.dtolabs.rundeck.plugins.orchestrator.OrchestratorPlugin;
2727

2828
import java.util.Collection;
29+
import java.util.List;
30+
import java.util.stream.Collectors;
2931

3032
@Plugin(name = "subset", service = ServiceNameConstants.Orchestrator)
3133
@PluginDescription(title = "Random Subset", description = "Chooses only a random subset of the target nodes.")
@@ -37,6 +39,22 @@ public class RandomSubsetOrchestratorPlugin implements OrchestratorPlugin {
3739

3840
@Override
3941
public Orchestrator createOrchestrator(StepExecutionContext context, Collection<INodeEntry> nodes) {
40-
return new RandomSubsetOrchestrator(count, context, nodes);
42+
String ident = createWFLayerIdent(context);
43+
//use the ident as random seed to allow repeatable sequence of random nodes
44+
//if this orchestrator config is invoked more than once in the same
45+
//workflow layer
46+
return new RandomSubsetOrchestrator(count, context, nodes, (long) ident.hashCode());
47+
}
48+
49+
public String createWFLayerIdent(final StepExecutionContext context) {
50+
//create a string which identifies this execution layer uniquely
51+
List<Integer> stepContext = context.getStepContext();
52+
String ident = context.getFrameworkProject();
53+
if (context.getDataContext().get("job") != null && context.getDataContext().get("job").get("execid") != null) {
54+
ident += context.getDataContext().get("job").get("execid");
55+
}
56+
57+
return ident +
58+
String.join(",", stepContext.stream().map(Object::toString).collect(Collectors.toList()));
4159
}
4260
}

0 commit comments

Comments
 (0)