This is an automated email from the ASF dual-hosted git repository. xxyu pushed a commit to branch kylin5 in repository https://gitbox.apache.org/repos/asf/kylin.git
commit 00b3a17c2af9392f799d390bdc98ac5dfc8f31b1 Author: jlf <longfei.ji...@kyligence.io> AuthorDate: Mon Mar 27 11:00:43 2023 +0800 KYLIN-5580 [FOLLOWUP] Use SecureRandom to get random integer --- .../org/apache/kylin/common/util/RandomUtil.java | 7 ++++- .../apache/kylin/common/util/RandomUtilTest.java | 35 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/core-common/src/main/java/org/apache/kylin/common/util/RandomUtil.java b/src/core-common/src/main/java/org/apache/kylin/common/util/RandomUtil.java index abd9820cec..534233f870 100644 --- a/src/core-common/src/main/java/org/apache/kylin/common/util/RandomUtil.java +++ b/src/core-common/src/main/java/org/apache/kylin/common/util/RandomUtil.java @@ -18,12 +18,17 @@ package org.apache.kylin.common.util; +import java.security.SecureRandom; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import org.apache.kylin.guava30.shaded.common.base.Preconditions; +import lombok.experimental.UtilityClass; + +@UtilityClass public class RandomUtil { + private static final SecureRandom random = new SecureRandom(); public static UUID randomUUID() { return new UUID(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong()); @@ -42,7 +47,7 @@ public class RandomUtil { return startInclusive; } - return startInclusive + ThreadLocalRandom.current().nextInt(endExclusive - startInclusive); + return startInclusive + random.nextInt(endExclusive - startInclusive); } public static int nextInt(final int endExclusive) { diff --git a/src/core-common/src/test/java/org/apache/kylin/common/util/RandomUtilTest.java b/src/core-common/src/test/java/org/apache/kylin/common/util/RandomUtilTest.java index 3d8e4b8d5d..db190ff48c 100644 --- a/src/core-common/src/test/java/org/apache/kylin/common/util/RandomUtilTest.java +++ b/src/core-common/src/test/java/org/apache/kylin/common/util/RandomUtilTest.java @@ -22,8 +22,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.List; import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.apache.kylin.guava30.shaded.common.collect.Lists; import org.junit.jupiter.api.Test; import lombok.val; @@ -64,4 +70,33 @@ public class RandomUtilTest { } } } + + @Test + void nextIntMultithreading() throws Exception { + concurrentTest(10, 30); + } + + public static void concurrentTest(long concurrentThreads, int times) throws Exception { + ExecutorService executor = Executors.newFixedThreadPool((int) concurrentThreads); + List<Future<Integer>> results = Lists.newArrayList(); + + for (int i = 1; i < times; i++) { + int finalI = i; + Callable<Integer> callable1 = () -> RandomUtil.nextInt(100 * finalI, 200 * finalI); + Callable<Integer> callable2 = () -> RandomUtil.nextInt(200000 * finalI, 300000 * finalI); + results.add(executor.submit(callable1)); + results.add(executor.submit(callable2)); + } + executor.shutdown(); + + for (int i = 0; i < times - 1; i++) { + Integer result1 = results.get(2 * i).get(); + assertTrue(result1 < 200 * (i + 1)); + assertTrue(result1 >= 100 * (i + 1)); + + Integer result2 = results.get(2 * i + 1).get(); + assertTrue(result2 < 300000 * (i + 1)); + assertTrue(result2 >= 200000 * (i + 1)); + } + } }