1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2008 Oracle. All rights reserved.
*
* $Id: BigDW.java,v 1.4 2008/05/30 14:04:23 mark Exp $
*/
import java.io.File;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DeadlockException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
/**
* A large database with random key distribution has lots of IN waste,
* especially if records are small; this creates a worst-case scenario for the
* cleaner and also possibly for the evictor. Simulate such an application and
* measure how well the cleaner and evictor keep up.
*
* Some commonly used command lines for running this program are:
*
* # Init new DB, causes duplicates to be created and deleted [#15588]
* java BigDW -h HOME -init -dupdel
*
* Each transaction does the following in "grow" mode. In "no grow" mode, it
* does one less insert, keeping the total number of keys constant.
*
* 2 inserts, 1 delete, 1 update, 10 reads
*
* The delete and update operations include a read to find the record.
*
*/
public class BigDW implements Runnable {
private String homeDir = "tmp";
private Environment env;
private Database refDB;
private Database testDB;
private boolean done;
private int nDeadlocks;
private boolean init;
private boolean verbose;
private boolean dupDel;
private int nTransactions;
private int nMaxTransactions = 20000;
private int nThreads = 4;
private int keySize = 10;
private int dataSize = 10;
private int nReadsPerWrite = 1;
private int maxRetries = 100;
private float totalSecs;
private float throughput;
private SecureRandom random = new SecureRandom();
private long startTime;
private long time;
private long mainCacheSize = 20000000;
public static void main(String args[]) {
try {
new BigDW().run(args);
System.exit(0);
} catch (Throwable e) {
e.printStackTrace(System.out);
System.exit(1);
}
}
/* Output command-line input arguments to log. */
private void printArgs(String[] args) {
System.out.print("\nCommand line arguments:");
for (String arg : args) {
System.out.print(' ');
System.out.print(arg);
}
System.out.println();
}
private void usage(String error) {
if (error != null) {
System.err.println(error);
}
System.err.println
("java " + getClass().getName() + '\n' +
" [-h <homeDir>] [-v] [-init] [-dupdel]\n" +
" [-txns <maxTxns>]\n");
System.exit(1);
}
private void run(String args[]) throws Exception {
try {
if (args.length == 0) {
throw new IllegalArgumentException();
}
/* Parse command-line input arguments. */
for (int i = 0; i < args.length; i += 1) {
String arg = args[i];
boolean moreArgs = i < args.length - 1;
if (arg.equals("-v")) {
verbose = true;
} else if (arg.equals("-dupdel")) {
dupDel = true;
} else if (arg.equals("-h") && moreArgs) {
homeDir = args[++i];
} else if (arg.equals("-init")) {
init = true;
} else if (arg.equals("-txns") && moreArgs) {
nMaxTransactions = Integer.parseInt(args[++i]);
} else if (arg.equals("-threads") && moreArgs) {
nThreads = Integer.parseInt(args[++i]);
} else {
usage("Unknown arg: " + arg);
}
}
printArgs(args);
} catch (IllegalArgumentException e) {
usage("IllegalArguments! ");
e.printStackTrace();
System.exit(1);
}
openEnv();
startTime = System.currentTimeMillis();
Thread[] threads = new Thread[nThreads];
for (int i = 0; i < nThreads; i += 1) {
threads[i] = new Thread(this);
threads[i].start();
Thread.sleep(1000); /* Stagger threads. */
}
for (int i = 0; i < nThreads; i += 1) {
if (threads[i] != null) {
threads[i].join();
}
}
time = System.currentTimeMillis();
closeEnv();
totalSecs = (float) (time - startTime) / 1000;
throughput = (float) nTransactions / totalSecs;
if (verbose) {
System.out.println("\nTotal seconds: " + totalSecs +
" txn/sec: " + throughput);
}
}
private void openEnv() throws Exception {
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(init);
envConfig.setCacheSize(mainCacheSize);
env = new Environment(new File(homeDir), envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(init);
dbConfig.setExclusiveCreate(init);
dbConfig.setSortedDuplicates(dupDel);
refDB = env.openDatabase(null, "BigDWRef", dbConfig);
dbConfig.setDeferredWrite(true);
testDB = env.openDatabase(null, "BigDWTest", dbConfig);
compare();
}
private void closeEnv()
throws Exception {
refDB.close();
testDB.sync();
testDB.close();
env.close();
}
public void run() {
DatabaseEntry data = new DatabaseEntry();
DatabaseEntry key = new DatabaseEntry();
byte[] lastInsertKey = null;
while (!done) {
/* JE-only begin */
try {
/* Perform the transaction. */
for (int retry = 0;; retry += 1) {
Cursor refCursor = refDB.openCursor(null, null);
Cursor testCursor = testDB.openCursor(null, null);
try {
if (init) {
key.setData(lastInsertKey);
insert(refCursor, testCursor, key, data);
lastInsertKey = copyData(key);
}
/* Insert */
key.setData(lastInsertKey);
insert(refCursor, testCursor, key, data);
lastInsertKey = copyData(key);
/* Dup-key insert. */
byte[] dupDataBA = copyData(data);
for (int i = 0; i < 5; i++) {
dupDataBA[0]++;
DatabaseEntry dupData =
new DatabaseEntry(dupDataBA);
OperationStatus status1 =
refCursor.put(key, dupData);
@SuppressWarnings("unused")
boolean insertDone1 = checkInsertStatus(status1);
if (status1 != OperationStatus.SUCCESS) {
throw new RuntimeException("insert1 " +
status1);
}
OperationStatus status2 =
testCursor.put(key, dupData);
if (status2 != OperationStatus.SUCCESS) {
throw new RuntimeException("insert2 " +
status2);
}
@SuppressWarnings("unused")
boolean insertDone2 = checkInsertStatus(status2);
}
/* Delete */
getRandom(refCursor, "BigDWRef",
testCursor, "BigDWTest",
key, data, LockMode.RMW);
DatabaseEntry dummy1 = new DatabaseEntry();
DatabaseEntry dummy2 = new DatabaseEntry();
while (refCursor.delete() ==
OperationStatus.SUCCESS &&
refCursor.getNextDup
(dummy1, dummy2, null) ==
OperationStatus.SUCCESS) {
}
while (testCursor.delete() ==
OperationStatus.SUCCESS &&
refCursor.getNextDup
(dummy1, dummy2, null) ==
OperationStatus.SUCCESS) {
}
/* Read */
for (int i = 0; i < nReadsPerWrite; i += 1) {
getRandom(refCursor, "BigDWRef",
testCursor, "BigDWTest",
key, data, LockMode.RMW);
}
refCursor.close();
testCursor.close();
nTransactions += 1;
if (nMaxTransactions != 0 &&
nTransactions >= nMaxTransactions) {
done = true;
}
break;
} catch (DeadlockException e) {
refCursor.close();
testCursor.close();
if (retry >= maxRetries) {
throw e;
}
/* Break deadlock cycle with a small sleep. */
Thread.sleep(5);
nDeadlocks += 1;
}
} /* for */
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
}
private void checkStatus(OperationStatus status)
throws Exception {
if (status != OperationStatus.SUCCESS) {
throw new Exception("problemStatus = " + status);
}
}
private void compare()
throws Exception {
DatabaseEntry refKey = new DatabaseEntry();
DatabaseEntry refData = new DatabaseEntry();
DatabaseEntry testKey = new DatabaseEntry();
DatabaseEntry testData = new DatabaseEntry();
Cursor refCursor = refDB.openCursor(null, null);
Cursor testCursor = testDB.openCursor(null, null);
System.out.println("Compare starts");
try {
while (refCursor.getNext(refKey, refData, LockMode.DEFAULT) ==
OperationStatus.SUCCESS) {
checkStatus(testCursor.getNext(testKey, testData,
LockMode.DEFAULT));
if (!Arrays.equals(refKey.getData(),
testKey.getData())) {
throw new Exception("Keys don't match");
}
if (!Arrays.equals(refData.getData(),
testData.getData())) {
throw new Exception("Data don't match");
}
}
if (testCursor.getNext(testKey, testData, LockMode.DEFAULT) !=
OperationStatus.NOTFOUND) {
throw new Exception("testCursor has extra data");
}
} finally {
refCursor.close();
testCursor.close();
}
System.out.println("Compare ends");
}
private void insert(Cursor c1, Cursor c2,
DatabaseEntry key, DatabaseEntry data)
throws DatabaseException {
makeData(data);
boolean insertDone1 = false;
while (!insertDone1) {
makeInsertKey(key);
OperationStatus status1 = c1.putNoOverwrite(key, data);
insertDone1 = checkInsertStatus(status1);
OperationStatus status2 = c2.putNoOverwrite(key, data);
boolean insertDone2 = checkInsertStatus(status2);
assert insertDone1 == insertDone2 :
"status1=" + status1 +
" status2=" + status2;
}
}
private boolean checkInsertStatus(OperationStatus status)
throws DatabaseException {
if (status == OperationStatus.KEYEXIST) {
System.out.println("****** Duplicate random key.");
return false; // try again.
} else {
if (status != OperationStatus.SUCCESS) {
System.out.println
("Unexpected return value from insert(): " + status);
}
return true; // end one way or another
}
}
private void getRandom(Cursor c1, String db1,
Cursor c2, String db2,
DatabaseEntry key, DatabaseEntry data,
LockMode lockMode)
throws DatabaseException {
makeRandomKey(key);
getRandomWork(c1, db1, key, data, lockMode);
getRandomWork(c2, db2, key, data, lockMode);
}
private void getRandomWork(Cursor c,
String dbName,
DatabaseEntry key,
DatabaseEntry data,
LockMode lockMode)
throws DatabaseException {
OperationStatus status = c.getSearchKeyRange(key, data, lockMode);
if (status == OperationStatus.NOTFOUND) {
status = c.getLast(key, data, lockMode);
if (status != OperationStatus.SUCCESS) {
System.out.println
("Unexpected return value from " + dbName +
".getRandomWork(): " + status);
}
}
}
private void makeInsertKey(DatabaseEntry key)
throws DatabaseException {
if (key.getData() != null) {
BigInteger num = new BigInteger(copyData(key));
num = num.add(BigInteger.ONE);
key.setData(num.toByteArray());
} else {
makeRandomKey(key);
}
}
private void makeRandomKey(DatabaseEntry key) {
byte[] bytes = new byte[keySize];
random.nextBytes(bytes);
key.setData(bytes);
}
private void makeData(DatabaseEntry data) {
byte[] bytes = new byte[dataSize];
for (int i = 0; i < bytes.length; i += 1) {
bytes[i] = (byte) i;
}
data.setData(bytes);
}
private byte[] copyData(DatabaseEntry data) {
byte[] buf = new byte[data.getSize()];
System.arraycopy(data.getData(), data.getOffset(), buf, 0, buf.length);
return buf;
}
}
|