Skip to content

Commit

Permalink
Use ThreadLocalRandom instead of unnecessary SecureRandom (#31694)
Browse files Browse the repository at this point in the history
  • Loading branch information
menghaoranss authored Jun 14, 2024
1 parent 233d142 commit 2dd4e97
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* Random generator for MySQL.
Expand All @@ -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.
*
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import lombok.Getter;

import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* Authentication hex data for openGauss.
Expand All @@ -38,7 +38,7 @@ public 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* Random generator for PostgreSQL.
Expand All @@ -31,8 +30,6 @@ public final class PostgreSQLRandomGenerator {

private static final PostgreSQLRandomGenerator INSTANCE = new PostgreSQLRandomGenerator();

private final Random random = new SecureRandom();

/**
* Get instance.
*
Expand All @@ -50,7 +47,7 @@ public static PostgreSQLRandomGenerator getInstance() {
*/
public byte[] generateRandomBytes(final int length) {
byte[] result = new byte[length];
random.nextBytes(result);
ThreadLocalRandom.current().nextBytes(result);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
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;

/**
Expand All @@ -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;
Expand Down Expand Up @@ -81,6 +79,7 @@ public String mask(final Object plainValue) {
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') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
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;
Expand All @@ -58,7 +57,7 @@
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.
Expand All @@ -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;
Expand Down Expand Up @@ -331,7 +328,7 @@ public String getRandomPhysicalDataSourceName() {
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("\\.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
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;

Expand All @@ -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();
Expand All @@ -64,7 +61,7 @@ void assertAckCallbackResultSortable() {
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;
}
Expand Down

0 comments on commit 2dd4e97

Please sign in to comment.