Skip to content

Commit 669569e

Browse files
committed
Fix the random number generation
Previously, the random number data generation consistently generated the same data sequence for every thread's database. This seems to have been introduced in commit 183b93e.
1 parent 3bf0461 commit 669569e

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

src/sqlancer/Main.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,17 @@ public static class DBMSExecutor<G extends GlobalState<O, ?>, O extends DBMSSpec
296296
private final MainOptions options;
297297
private final O command;
298298
private final String databaseName;
299-
private final long seed;
300299
private StateLogger logger;
301300
private StateToReproduce stateToRepro;
301+
private final Randomly r;
302302

303303
public DBMSExecutor(DatabaseProvider<G, O> provider, MainOptions options, O dbmsSpecificOptions,
304-
String databaseName, long seed) {
304+
String databaseName, Randomly r) {
305305
this.provider = provider;
306306
this.options = options;
307307
this.databaseName = databaseName;
308-
this.seed = seed;
309308
this.command = dbmsSpecificOptions;
309+
this.r = r;
310310
}
311311

312312
private G createGlobalState() {
@@ -324,10 +324,9 @@ public O getCommand() {
324324
public void run() throws SQLException {
325325
G state = createGlobalState();
326326
stateToRepro = provider.getStateToReproduce(databaseName);
327-
stateToRepro.seedValue = seed;
327+
stateToRepro.seedValue = r.getSeed();
328328
state.setState(stateToRepro);
329329
logger = new StateLogger(databaseName, provider, options);
330-
Randomly r = new Randomly(seed);
331330
state.setRandomly(r);
332331
state.setDatabaseName(databaseName);
333332
state.setMainOptions(options);
@@ -390,10 +389,10 @@ public O getCommand() {
390389
}
391390

392391
@SuppressWarnings("unchecked")
393-
public DBMSExecutor<G, O> getDBMSExecutor(String databaseName, long seed) {
392+
public DBMSExecutor<G, O> getDBMSExecutor(String databaseName, Randomly r) {
394393
try {
395394
return new DBMSExecutor<G, O>(provider.getClass().getDeclaredConstructor().newInstance(), options,
396-
command, databaseName, seed);
395+
command, databaseName, r);
397396
} catch (Exception e) {
398397
throw new AssertionError(e);
399398
}
@@ -461,7 +460,6 @@ private String formatInteger(long intValue) {
461460
} else {
462461
seed = options.getRandomSeed() + i;
463462
}
464-
465463
execService.execute(new Runnable() {
466464

467465
@Override
@@ -471,17 +469,17 @@ public void run() {
471469
}
472470

473471
private void runThread(final String databaseName) {
472+
Randomly r = new Randomly(seed);
474473
try {
475474
if (options.getMaxGeneratedDatabases() == -1) {
476475
// run without a limit
477476
boolean continueRunning = true;
478477
while (continueRunning) {
479-
continueRunning = run(options, execService, executorFactory, seed, databaseName);
478+
continueRunning = run(options, execService, executorFactory, r, databaseName);
480479
}
481480
} else {
482481
for (int i = 0; i < options.getMaxGeneratedDatabases(); i++) {
483-
boolean continueRunning = run(options, execService, executorFactory, seed,
484-
databaseName);
482+
boolean continueRunning = run(options, execService, executorFactory, r, databaseName);
485483
if (!continueRunning) {
486484
break;
487485
}
@@ -496,8 +494,8 @@ private void runThread(final String databaseName) {
496494
}
497495

498496
private boolean run(MainOptions options, ExecutorService execService,
499-
DBMSExecutorFactory<?, ?> executorFactory, final long seed, final String databaseName) {
500-
DBMSExecutor<?, ?> executor = executorFactory.getDBMSExecutor(databaseName, seed);
497+
DBMSExecutorFactory<?, ?> executorFactory, Randomly r, final String databaseName) {
498+
DBMSExecutor<?, ?> executor = executorFactory.getDBMSExecutor(databaseName, r);
501499
try {
502500
executor.run();
503501
return true;

src/sqlancer/Randomly.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public final class Randomly {
2020
private Supplier<String> provider;
2121

2222
private static final ThreadLocal<Random> THREAD_RANDOM = new ThreadLocal<>();
23+
private long seed;
2324

2425
private void addToCache(long val) {
2526
if (USE_CACHING && cachedLongs.size() < CACHE_SIZE && !cachedLongs.contains(val)) {
@@ -383,11 +384,12 @@ public Randomly(Supplier<String> provider) {
383384
}
384385

385386
public Randomly() {
386-
getThreadRandom().set(new Random());
387+
THREAD_RANDOM.set(new Random());
387388
}
388389

389390
public Randomly(long seed) {
390-
getThreadRandom().set(new Random(seed));
391+
this.seed = seed;
392+
THREAD_RANDOM.set(new Random(seed));
391393
}
392394

393395
public static double getUncachedDouble() {
@@ -429,4 +431,8 @@ private static int getNextInt(int lower, int upper) {
429431
return (int) getNextLong(lower, upper);
430432
}
431433

434+
public long getSeed() {
435+
return seed;
436+
}
437+
432438
}

0 commit comments

Comments
 (0)