Skip to content

Commit

Permalink
HBASE-29013 Addendum forward port branch-2 changes to use ThreadLocal…
Browse files Browse the repository at this point in the history
…Random instead of Random

Co-authored-by: Duo Zhang <[email protected]>
Signed-off-by: Duo Zhang <[email protected]>
  • Loading branch information
junegunn and Apache9 committed Dec 29, 2024
1 parent c7b5195 commit 28c4353
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1166,17 +1166,8 @@ public long getBufferSize() {
* A test. Subclass to particularize what happens per row.
*/
static abstract class TestBase {
// Below is make it so when Tests are all running in the one
// jvm, that they each have a differently seeded Random.
private static final Random randomSeed = new Random(EnvironmentEdgeManager.currentTime());

private static long nextRandomSeed() {
return randomSeed.nextLong();
}

private final long everyN;

protected final Random rand = new Random(nextRandomSeed());
protected final Configuration conf;
protected final TestOptions opts;

Expand Down Expand Up @@ -1207,14 +1198,15 @@ private static long nextRandomSeed() {
this.testName = this.getClass().getSimpleName();
everyN = (long) (opts.totalRows / (opts.totalRows * opts.sampleRate));
if (options.isValueZipf()) {
this.zipf = new RandomDistribution.Zipf(this.rand, 1, options.getValueSize(), 1.2);
this.zipf =
new RandomDistribution.Zipf(ThreadLocalRandom.current(), 1, options.getValueSize(), 1.2);
}
LOG.info("Sampling 1 every " + everyN + " out of " + opts.perClientRunRows + " total rows.");
}

int getValueLength(final Random r) {
int getValueLength() {
if (this.opts.isValueRandom()) {
return r.nextInt(opts.valueSize);
return ThreadLocalRandom.current().nextInt(opts.valueSize);
} else if (this.opts.isValueZipf()) {
return Math.abs(this.zipf.nextInt());
} else {
Expand Down Expand Up @@ -1562,7 +1554,7 @@ boolean testRow(final long i, final long startTime) throws IOException, Interrup
if (opts.randomSleep > 0) {
Thread.sleep(ThreadLocalRandom.current().nextInt(opts.randomSleep));
}
Get get = new Get(getRandomRow(this.rand, opts.totalRows));
Get get = new Get(getRandomRow(opts.totalRows));
for (int family = 0; family < opts.families; family++) {
byte[] familyName = Bytes.toBytes(FAMILY_NAME_BASE + family);
if (opts.addColumns) {
Expand Down Expand Up @@ -1638,7 +1630,7 @@ static class AsyncRandomWriteTest extends AsyncSequentialWriteTest {

@Override
protected byte[] generateRow(final long i) {
return getRandomRow(this.rand, opts.totalRows);
return getRandomRow(opts.totalRows);
}
}

Expand Down Expand Up @@ -1748,9 +1740,9 @@ boolean testRow(final long i, final long startTime) throws IOException, Interrup
byte[] familyName = Bytes.toBytes(FAMILY_NAME_BASE + family);
for (int column = 0; column < opts.columns; column++) {
byte[] qualifier = column == 0 ? COLUMN_ZERO : Bytes.toBytes("" + column);
byte[] value = generateData(this.rand, getValueLength(this.rand));
byte[] value = generateData(getValueLength());
if (opts.useTags) {
byte[] tag = generateData(this.rand, TAG_LENGTH);
byte[] tag = generateData(TAG_LENGTH);
Tag[] tags = new Tag[opts.noOfTags];
for (int n = 0; n < opts.noOfTags; n++) {
Tag t = new ArrayBackedTag((byte) n, tag);
Expand Down Expand Up @@ -1817,10 +1809,9 @@ static class RandomSeekScanTest extends TableTest {

@Override
boolean testRow(final long i, final long startTime) throws IOException {
Scan scan =
new Scan().withStartRow(getRandomRow(this.rand, opts.totalRows)).setCaching(opts.caching)
.setCacheBlocks(opts.cacheBlocks).setAsyncPrefetch(opts.asyncPrefetch)
.setReadType(opts.scanReadType).setScanMetricsEnabled(true);
Scan scan = new Scan().withStartRow(getRandomRow(opts.totalRows)).setCaching(opts.caching)
.setCacheBlocks(opts.cacheBlocks).setAsyncPrefetch(opts.asyncPrefetch)
.setReadType(opts.scanReadType).setScanMetricsEnabled(true);
FilterList list = new FilterList();
for (int family = 0; family < opts.families; family++) {
byte[] familyName = Bytes.toBytes(FAMILY_NAME_BASE + family);
Expand Down Expand Up @@ -1907,7 +1898,7 @@ boolean testRow(final long i, final long startTime) throws IOException {
protected abstract Pair<byte[], byte[]> getStartAndStopRow();

protected Pair<byte[], byte[]> generateStartAndStopRows(long maxRange) {
long start = this.rand.nextLong(Long.MAX_VALUE) % opts.totalRows;
long start = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE) % opts.totalRows;
long stop = start + maxRange;
return new Pair<>(format(start), format(stop));
}
Expand Down Expand Up @@ -1981,7 +1972,7 @@ boolean testRow(final long i, final long startTime) throws IOException, Interrup
if (opts.randomSleep > 0) {
Thread.sleep(ThreadLocalRandom.current().nextInt(opts.randomSleep));
}
Get get = new Get(getRandomRow(this.rand, opts.totalRows));
Get get = new Get(getRandomRow(opts.totalRows));
for (int family = 0; family < opts.families; family++) {
byte[] familyName = Bytes.toBytes(FAMILY_NAME_BASE + family);
if (opts.addColumns) {
Expand Down Expand Up @@ -2060,10 +2051,10 @@ void onStartup() throws IOException {
@Override
boolean testRow(final long i, final long startTime) throws IOException, InterruptedException {
if (opts.randomSleep > 0) {
Thread.sleep(rand.nextInt(opts.randomSleep));
Thread.sleep(ThreadLocalRandom.current().nextInt(opts.randomSleep));
}
HRegionLocation hRegionLocation =
regionLocator.getRegionLocation(getSplitKey(rand.nextLong(opts.perClientRunRows)), true);
HRegionLocation hRegionLocation = regionLocator.getRegionLocation(
getSplitKey(ThreadLocalRandom.current().nextLong(opts.perClientRunRows)), true);
LOG.debug("get location for region: " + hRegionLocation);
return true;
}
Expand All @@ -2087,7 +2078,7 @@ static class RandomWriteTest extends SequentialWriteTest {

@Override
protected byte[] generateRow(final long i) {
return getRandomRow(this.rand, opts.totalRows);
return getRandomRow(opts.totalRows);
}

}
Expand All @@ -2099,7 +2090,7 @@ static class RandomDeleteTest extends SequentialDeleteTest {

@Override
protected byte[] generateRow(final long i) {
return getRandomRow(this.rand, opts.totalRows);
return getRandomRow(opts.totalRows);
}

}
Expand Down Expand Up @@ -2401,9 +2392,9 @@ boolean testRow(final long i, final long startTime) throws IOException {
byte familyName[] = Bytes.toBytes(FAMILY_NAME_BASE + family);
for (int column = 0; column < opts.columns; column++) {
byte[] qualifier = column == 0 ? COLUMN_ZERO : Bytes.toBytes("" + column);
byte[] value = generateData(this.rand, getValueLength(this.rand));
byte[] value = generateData(getValueLength());
if (opts.useTags) {
byte[] tag = generateData(this.rand, TAG_LENGTH);
byte[] tag = generateData(TAG_LENGTH);
Tag[] tags = new Tag[opts.noOfTags];
for (int n = 0; n < opts.noOfTags; n++) {
Tag t = new ArrayBackedTag((byte) n, tag);
Expand Down Expand Up @@ -2486,7 +2477,7 @@ boolean testRow(final long i, final long startTime) throws IOException {

// write the serverName columns
MetaTableAccessor.updateRegionLocation(connection, regionInfo,
ServerName.valueOf("localhost", 60010, rand.nextLong()), i,
ServerName.valueOf("localhost", 60010, ThreadLocalRandom.current().nextLong()), i,
EnvironmentEdgeManager.currentTime());
return true;
}
Expand All @@ -2505,7 +2496,7 @@ static class FilteredScanTest extends TableTest {

@Override
boolean testRow(long i, final long startTime) throws IOException {
byte[] value = generateData(this.rand, getValueLength(this.rand));
byte[] value = generateData(getValueLength());
Scan scan = constructScan(value);
ResultScanner scanner = null;
try {
Expand Down Expand Up @@ -2581,10 +2572,11 @@ public static byte[] format(final long number) {
* test, generation of the key and value consumes about 30% of CPU time.
* @return Generated random value to insert into a table cell.
*/
public static byte[] generateData(final Random r, int length) {
public static byte[] generateData(int length) {
byte[] b = new byte[length];
int i;

Random r = ThreadLocalRandom.current();
for (i = 0; i < (length - 8); i += 8) {
b[i] = (byte) (65 + r.nextInt(26));
b[i + 1] = b[i];
Expand All @@ -2603,12 +2595,12 @@ public static byte[] generateData(final Random r, int length) {
return b;
}

static byte[] getRandomRow(final Random random, final long totalRows) {
return format(generateRandomRow(random, totalRows));
static byte[] getRandomRow(final long totalRows) {
return format(generateRandomRow(totalRows));
}

static long generateRandomRow(final Random random, final long totalRows) {
return random.nextLong(Long.MAX_VALUE) % totalRows;
static long generateRandomRow(final long totalRows) {
return ThreadLocalRandom.current().nextLong(Long.MAX_VALUE) % totalRows;
}

static RunResult runOneClient(final Class<? extends TestBase> cmd, Configuration conf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
Expand Down Expand Up @@ -162,9 +160,8 @@ public void testRandomReadCalculation() {
assertEquals(1000, opts.getPerClientRunRows());
// assuming we will get one before this loop expires
boolean foundValue = false;
Random rand = ThreadLocalRandom.current();
for (int i = 0; i < 10000000; i++) {
long randomRow = PerformanceEvaluation.generateRandomRow(rand, opts.totalRows);
long randomRow = PerformanceEvaluation.generateRandomRow(opts.totalRows);
if (randomRow > 1000) {
foundValue = true;
break;
Expand All @@ -186,7 +183,7 @@ public void testZipfian() throws NoSuchMethodException, SecurityException, Insta
ctor.setAccessible(true);
Histogram histogram = (Histogram) ctor.newInstance(new UniformReservoir(1024 * 500));
for (int i = 0; i < 100; i++) {
histogram.update(rrt.getValueLength(null));
histogram.update(rrt.getValueLength());
}
Snapshot snapshot = histogram.getSnapshot();
double stddev = snapshot.getStdDev();
Expand Down

0 comments on commit 28c4353

Please sign in to comment.