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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: ReleaseLatchesTest.java,v 1.21.2.2 2010/01/04 15:30:48 cwl Exp $
*/
package com.sleepycat.je.tree;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.je.CheckpointConfig;
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.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.RunRecoveryException;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.latch.LatchSupport;
import com.sleepycat.je.log.FileManager;
import com.sleepycat.je.util.TestUtils;
import com.sleepycat.je.utilint.TestHook;
/**
* Check that latches are release properly even if we run into read errors.
*/
public class ReleaseLatchesTest extends TestCase {
private static final boolean DEBUG = false;
private Environment env;
private File envHome;
private Database db;
private TestDescriptor testActivity;
/*
* The OPERATIONS declared here define the test cases for this test. Each
* TestDescriptor describes a particular JE activity. The
* testCheckLatchLeaks method generates read i/o exceptions during the test
* descriptor's action, and will check that we come up clean.
*/
public static TestDescriptor[] OPERATIONS = {
/*
* TestDescriptor params:
* - operation name: for debugging
* - number of times to generate an exception. For example if N,
* the test action will be executed in a loop N times, with an
* read/io on read 1, read 2, read 3 ... read n-1
* - number of records in the database.
*/
new TestDescriptor("database put", 6, 30, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.populate(false);
}
void reinit(ReleaseLatchesTest test)
throws DatabaseException{
test.closeDb();
test.getEnv().truncateDatabase(null, "foo", false);
}
},
new TestDescriptor("cursor scan", 31, 20, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.scan();
}
},
new TestDescriptor("cursor scan duplicates", 23, 3, true) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.scan();
}
},
new TestDescriptor("database get", 31, 20, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.get();
}
},
new TestDescriptor("database delete", 40, 30, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.delete();
}
void reinit(ReleaseLatchesTest test)
throws DatabaseException{
test.populate(false);
}
},
new TestDescriptor("checkpoint", 40, 10, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.modify(exceptionCount);
CheckpointConfig config = new CheckpointConfig();
config.setForce(true);
if (DEBUG) {
System.out.println("Got to checkpoint");
}
test.getEnv().checkpoint(config);
}
},
new TestDescriptor("clean", 100, 5, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.modify(exceptionCount);
CheckpointConfig config = new CheckpointConfig();
config.setForce(true);
if (DEBUG) {
System.out.println("Got to cleaning");
}
test.getEnv().cleanLog();
}
},
new TestDescriptor("compress", 20, 10, false) {
void doAction(ReleaseLatchesTest test, int exceptionCount)
throws DatabaseException {
test.delete();
if (DEBUG) {
System.out.println("Got to compress");
}
test.getEnv().compress();
}
void reinit(ReleaseLatchesTest test)
throws DatabaseException{
test.populate(false);
}
}
};
public static Test suite() {
TestSuite allTests = new TestSuite();
for (int i = 0; i < OPERATIONS.length; i++) {
TestSuite suite = new TestSuite(ReleaseLatchesTest.class);
Enumeration e = suite.tests();
while (e.hasMoreElements()) {
ReleaseLatchesTest t = (ReleaseLatchesTest) e.nextElement();
t.initTest(OPERATIONS[i]);
allTests.addTest(t);
}
}
return allTests;
}
public ReleaseLatchesTest() {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void setUp()
throws IOException, DatabaseException {
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
}
public void tearDown()
throws IOException, DatabaseException {
setName(getName() + ":" + testActivity.getName());
TestUtils.removeFiles("TearDown", envHome,
FileManager.JE_SUFFIX, true);
}
private void init(boolean duplicates)
throws DatabaseException {
openEnvAndDb();
populate(duplicates);
env.checkpoint(null);
db.close();
db = null;
env.close();
env = null;
}
private void openEnvAndDb()
throws DatabaseException {
/*
* Make an environment with small nodes and no daemons.
*/
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setAllowCreate(true);
envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "4");
envConfig.setConfigParam("je.env.runEvictor", "false");
envConfig.setConfigParam("je.env.runCheckpointer", "false");
envConfig.setConfigParam("je.env.runCleaner", "false");
envConfig.setConfigParam("je.env.runINCompressor", "false");
envConfig.setConfigParam
(EnvironmentParams.CLEANER_MIN_UTILIZATION.getName(), "90");
envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
Integer.toString(20000));
env = new Environment(envHome, envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(true);
db = env.openDatabase(null, "foo", dbConfig);
}
/* Calling close under -ea will check for leaked latches. */
private void doCloseAndCheckLeaks()
throws Throwable {
try {
if (db != null) {
db.close();
db = null;
}
if (env != null) {
env.close();
env = null;
}
} catch (Throwable t) {
System.out.println("operation = " + testActivity.name);
t.printStackTrace();
throw t;
}
}
private void closeDb()
throws DatabaseException {
if (db != null) {
db.close();
db = null;
}
}
private Environment getEnv() {
return env;
}
private void initTest(TestDescriptor action) {
this.testActivity = action;
}
/*
* This is the heart of the unit test. Given a TestDescriptor, run the
* operation's activity in a loop, generating read i/o exceptions at
* different points. Check for latch leaks after the i/o exception
* happens.
*/
public void testCheckLatchLeaks()
throws Throwable {
int maxExceptionCount = testActivity.getNumExceptions();
if (DEBUG) {
System.out.println("Starting test: " + testActivity.getName());
}
try {
init(testActivity.getDuplicates());
/*
* Run the action repeatedly, generating exceptions at different
* points.
*/
for (int i = 1; i <= maxExceptionCount; i++) {
/*
* Open the env and database anew each time, so that we need to
* fault in objects and will trigger read i/o exceptions.
*/
openEnvAndDb();
EnvironmentImpl envImpl =
DbInternal.envGetEnvironmentImpl(env);
boolean exceptionOccurred = false;
try {
ReadIOExceptionHook readHook = new ReadIOExceptionHook(i);
envImpl.getLogManager().setReadHook(readHook);
testActivity.doAction(this, i);
} catch (Throwable e) {
while (e.getCause() != null &&
!(e instanceof DatabaseException)) {
e = e.getCause();
}
if (e instanceof RunRecoveryException) {
/*
* It's possible for a read error to induce a
* RunRecoveryException if the read error happens when
* we are opening a new write file channel. (We read
* and validate the file header). In that case, check
* for latches, and re-open the database.
*/
checkLatchCount((DatabaseException) e, i);
env.close();
openEnvAndDb();
exceptionOccurred = true;
} else if (e instanceof DatabaseException) {
checkLatchCount((DatabaseException) e, i);
exceptionOccurred = true;
} else {
throw e;
}
}
if (DEBUG && !exceptionOccurred) {
System.out.println("Don't need ex count " + i +
" for test activity " +
testActivity.getName());
}
envImpl.getLogManager().setReadHook(null);
testActivity.reinit(this);
doCloseAndCheckLeaks();
}
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
private void checkLatchCount(DatabaseException e,
int exceptionCount)
throws DatabaseException {
/* Only rethrow the exception if we didn't clean up latches. */
if (LatchSupport.countLatchesHeld() > 0) {
LatchSupport.dumpLatchesHeld();
System.out.println("Operation = " + testActivity.getName() +
" exception count=" + exceptionCount +
" Held latches = " +
LatchSupport.countLatchesHeld());
/* Show stacktrace where the latch was lost. */
e.printStackTrace();
throw e;
}
}
/* Insert records into a database. */
private void populate(boolean duplicates)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
DatabaseEntry data1 = new DatabaseEntry();
DatabaseEntry data2 = new DatabaseEntry();
DatabaseEntry data3 = new DatabaseEntry();
DatabaseEntry data4 = new DatabaseEntry();
IntegerBinding.intToEntry(0, data);
IntegerBinding.intToEntry(1, data1);
IntegerBinding.intToEntry(2, data2);
IntegerBinding.intToEntry(3, data3);
IntegerBinding.intToEntry(4, data4);
for (int i = 0; i < testActivity.getNumRecords(); i++) {
IntegerBinding.intToEntry(i, key);
assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
if (duplicates) {
assertEquals(OperationStatus.SUCCESS,
db.put(null, key, data1));
assertEquals(OperationStatus.SUCCESS,
db.put(null, key, data2));
assertEquals(OperationStatus.SUCCESS,
db.put(null, key, data3));
assertEquals(OperationStatus.SUCCESS,
db.put(null, key, data4));
}
}
}
/* Modify the database. */
private void modify(int dataVal)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
IntegerBinding.intToEntry(dataVal, data);
for (int i = 0; i < testActivity.getNumRecords(); i++) {
IntegerBinding.intToEntry(i, key);
assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
}
}
/* Cursor scan the data. */
private void scan()
throws DatabaseException {
Cursor cursor = null;
try {
cursor = db.openCursor(null, null);
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
while (cursor.getNext(key, data, LockMode.DEFAULT) ==
OperationStatus.SUCCESS) {
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
/* Database.get() for all records. */
private void get()
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
for (int i = 0; i < testActivity.getNumRecords(); i++) {
IntegerBinding.intToEntry(i, key);
assertEquals(OperationStatus.SUCCESS,
db.get(null, key, data, LockMode.DEFAULT));
}
}
/* Delete all records. */
private void delete()
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry();
for (int i = 0; i < testActivity.getNumRecords(); i++) {
IntegerBinding.intToEntry(i, key);
assertEquals("key = " + IntegerBinding.entryToInt(key),
OperationStatus.SUCCESS, db.delete(null, key));
}
}
/*
* This TestHook implementation generates io exceptions during reads.
*/
static class ReadIOExceptionHook implements TestHook {
private int counter = 0;
private int throwCount;
ReadIOExceptionHook(int throwCount) {
this.throwCount = throwCount;
}
public void doIOHook()
throws IOException {
if (throwCount == counter) {
counter++;
throw new IOException("Generated exception: " +
this.getClass().getName());
} else {
counter++;
}
}
public Object getHookValue() {
throw new UnsupportedOperationException();
}
public void doHook() {
throw new UnsupportedOperationException();
}
public void hookSetup() {
throw new UnsupportedOperationException();
}
}
static abstract class TestDescriptor {
private String name;
private int numExceptions;
private int numRecords;
private boolean duplicates;
TestDescriptor(String name,
int numExceptions,
int numRecords,
boolean duplicates) {
this.name = name;
this.numExceptions = numExceptions;
this.numRecords = numRecords;
this.duplicates = duplicates;
}
int getNumRecords() {
return numRecords;
}
int getNumExceptions() {
return numExceptions;
}
String getName() {
return name;
}
boolean getDuplicates() {
return duplicates;
}
/* Do a series of operations. */
abstract void doAction(ReleaseLatchesTest test,
int exceptionCount)
throws DatabaseException;
/* Reinitialize the database if doAction modified it. */
void reinit(ReleaseLatchesTest test)
throws DatabaseException {
}
}
}
|