This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 2dd4e97967a Use ThreadLocalRandom instead of unnecessary SecureRandom
(#31694)
2dd4e97967a is described below
commit 2dd4e97967a8056c9e45640e95898c6e0a1ea46e
Author: Haoran Meng <[email protected]>
AuthorDate: Fri Jun 14 11:41:50 2024 +0800
Use ThreadLocalRandom instead of unnecessary SecureRandom (#31694)
---
.../db/protocol/mysql/packet/handshake/MySQLRandomGenerator.java | 7 ++-----
.../packet/authentication/OpenGaussAuthenticationHexData.java | 4 ++--
.../postgresql/packet/handshake/PostgreSQLRandomGenerator.java | 7 ++-----
.../mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java | 5 ++---
.../jdbc/core/connection/DriverDatabaseConnectionManager.java | 7 ++-----
.../data/pipeline/core/channel/MultiplexPipelineChannelTest.java | 7 ++-----
6 files changed, 12 insertions(+), 25 deletions(-)
diff --git
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLRandomGenerator.java
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLRandomGenerator.java
index 6db5a6ab867..fb7288bf834 100644
---
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLRandomGenerator.java
+++
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLRandomGenerator.java
@@ -21,8 +21,7 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
-import java.security.SecureRandom;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
/**
* Random generator for MySQL.
@@ -38,8 +37,6 @@ public final class MySQLRandomGenerator {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
- private final Random random = new SecureRandom();
-
/**
* Generate random bytes.
*
@@ -49,7 +46,7 @@ public final class MySQLRandomGenerator {
public byte[] generateRandomBytes(final int length) {
byte[] result = new byte[length];
for (int i = 0; i < length; i++) {
- result[i] = SEED[random.nextInt(SEED.length)];
+ result[i] = SEED[ThreadLocalRandom.current().nextInt(SEED.length)];
}
return result;
}
diff --git
a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationHexData.java
b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationHexData.java
index 7eacf5a3552..05d8e959731 100644
---
a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationHexData.java
+++
b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/authentication/OpenGaussAuthenticationHexData.java
@@ -19,8 +19,8 @@ package
org.apache.shardingsphere.db.protocol.opengauss.packet.authentication;
import lombok.Getter;
-import java.security.SecureRandom;
import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
/**
* Authentication hex data for openGauss.
@@ -38,7 +38,7 @@ public final class OpenGaussAuthenticationHexData {
}
private String generate(final int length) {
- Random random = new SecureRandom();
+ Random random = ThreadLocalRandom.current();
StringBuilder result = new StringBuilder(length);
for (int i = 0; i < result.capacity(); i++) {
result.append(Integer.toString(random.nextInt(0x10), 0x10));
diff --git
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLRandomGenerator.java
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLRandomGenerator.java
index 507746361c8..f17759b6e14 100644
---
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLRandomGenerator.java
+++
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/handshake/PostgreSQLRandomGenerator.java
@@ -20,8 +20,7 @@ package
org.apache.shardingsphere.db.protocol.postgresql.packet.handshake;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import java.security.SecureRandom;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
/**
* Random generator for PostgreSQL.
@@ -31,8 +30,6 @@ public final class PostgreSQLRandomGenerator {
private static final PostgreSQLRandomGenerator INSTANCE = new
PostgreSQLRandomGenerator();
- private final Random random = new SecureRandom();
-
/**
* Get instance.
*
@@ -50,7 +47,7 @@ public final class PostgreSQLRandomGenerator {
*/
public byte[] generateRandomBytes(final int length) {
byte[] result = new byte[length];
- random.nextBytes(result);
+ ThreadLocalRandom.current().nextBytes(result);
return result;
}
}
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
index 0e14b329a44..5788e503802 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
@@ -23,10 +23,10 @@ import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitial
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
-import java.security.SecureRandom;
import java.util.List;
import java.util.Properties;
import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
/**
@@ -50,8 +50,6 @@ public final class GenericTableRandomReplaceAlgorithm
implements MaskAlgorithm<O
private static final String DEFAULT_SPECIAL_CODES =
"~,!,@,#,$,%,^,&,*,:,<,>,|";
- private final Random random = new SecureRandom();
-
private List<Character> uppercaseLetterCodes;
private List<Character> lowercaseLetterCodes;
@@ -81,6 +79,7 @@ public final class GenericTableRandomReplaceAlgorithm
implements MaskAlgorithm<O
return result;
}
char[] chars = result.toCharArray();
+ Random random = ThreadLocalRandom.current();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ('A' <= c && c <= 'Z') {
diff --git
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index c58bc9d831e..82a6519c680 100644
---
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -46,7 +46,6 @@ import
org.apache.shardingsphere.transaction.ConnectionTransaction;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import javax.sql.DataSource;
-import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
@@ -58,7 +57,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
/**
* Database connection manager of ShardingSphere-JDBC.
@@ -84,8 +83,6 @@ public final class DriverDatabaseConnectionManager implements
OnlineDatabaseConn
private final ForceExecuteTemplate<Connection> forceExecuteTemplate = new
ForceExecuteTemplate<>();
- private final Random random = new SecureRandom();
-
public DriverDatabaseConnectionManager(final String databaseName, final
ContextManager contextManager) {
this.contextManager = contextManager;
this.databaseName = databaseName;
@@ -331,7 +328,7 @@ public final class DriverDatabaseConnectionManager
implements OnlineDatabaseConn
private String[] getRandomPhysicalDatabaseAndDataSourceName() {
Collection<String> cachedPhysicalDataSourceNames =
Sets.intersection(physicalDataSourceMap.keySet(), cachedConnections.keySet());
Collection<String> databaseAndDatasourceNames =
cachedPhysicalDataSourceNames.isEmpty() ? physicalDataSourceMap.keySet() :
cachedPhysicalDataSourceNames;
- return new
ArrayList<>(databaseAndDatasourceNames).get(random.nextInt(databaseAndDatasourceNames.size())).split("\\.");
+ return new
ArrayList<>(databaseAndDatasourceNames).get(ThreadLocalRandom.current().nextInt(databaseAndDatasourceNames.size())).split("\\.");
}
/**
diff --git
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/channel/MultiplexPipelineChannelTest.java
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/channel/MultiplexPipelineChannelTest.java
index 1d7f852b89b..3df5f438115 100644
---
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/channel/MultiplexPipelineChannelTest.java
+++
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/channel/MultiplexPipelineChannelTest.java
@@ -30,11 +30,10 @@ import
org.apache.shardingsphere.data.pipeline.core.ingest.record.Record;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;
-import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
-import java.util.Random;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -46,8 +45,6 @@ class MultiplexPipelineChannelTest {
private static final int CHANNEL_NUMBER = 2;
- private final Random random = new SecureRandom();
-
@Test
void assertAckCallbackResultSortable() {
Record[] records = mockRecords();
@@ -64,7 +61,7 @@ class MultiplexPipelineChannelTest {
private Record[] mockRecords() {
Record[] result = new Record[100];
for (int i = 1; i <= result.length; i++) {
- result[i - 1] = random.nextBoolean() ? new
DataRecord(PipelineSQLOperationType.INSERT, "t1", new IntPosition(i), 0) : new
PlaceholderRecord(new IntPosition(i));
+ result[i - 1] = ThreadLocalRandom.current().nextBoolean() ? new
DataRecord(PipelineSQLOperationType.INSERT, "t1", new IntPosition(i), 0) : new
PlaceholderRecord(new IntPosition(i));
}
return result;
}