From fb8f3edc5adaa42a48389895f3a16a173495e935 Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Tue, 9 Jul 2024 09:58:03 +0800 Subject: [PATCH] Use SecureRandom rather than ThreadLocalRandom (#16291) --- .../cluster/loadbalancer/RandomWorkerLoadBalancer.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/cluster/loadbalancer/RandomWorkerLoadBalancer.java b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/cluster/loadbalancer/RandomWorkerLoadBalancer.java index df0d074b7c..4d348bd1ea 100644 --- a/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/cluster/loadbalancer/RandomWorkerLoadBalancer.java +++ b/dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/cluster/loadbalancer/RandomWorkerLoadBalancer.java @@ -21,9 +21,9 @@ import org.apache.dolphinscheduler.server.master.cluster.WorkerClusters; import org.apache.commons.collections4.CollectionUtils; +import java.security.SecureRandom; import java.util.List; import java.util.Optional; -import java.util.concurrent.ThreadLocalRandom; import org.jetbrains.annotations.NotNull; @@ -34,8 +34,11 @@ public class RandomWorkerLoadBalancer implements IWorkerLoadBalancer { private final WorkerClusters workerClusters; + private final SecureRandom secureRandom; + public RandomWorkerLoadBalancer(WorkerClusters workerClusters) { this.workerClusters = workerClusters; + this.secureRandom = new SecureRandom(); } @Override @@ -44,7 +47,7 @@ public class RandomWorkerLoadBalancer implements IWorkerLoadBalancer { if (CollectionUtils.isEmpty(workerServerAddresses)) { return Optional.empty(); } - int index = ThreadLocalRandom.current().nextInt(workerServerAddresses.size()); + int index = secureRandom.nextInt(workerServerAddresses.size()); return Optional.of(workerServerAddresses.get(index)); }