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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: IOExceptionTest.java,v 1.22.2.2 2010/01/04 15:30:44 cwl Exp $
*/
package com.sleepycat.je.log;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import junit.framework.TestCase;
import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.je.CheckpointConfig;
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.EnvironmentMutableConfig;
import com.sleepycat.je.EnvironmentStats;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.LockStats;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.RunRecoveryException;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.cleaner.UtilizationProfile;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.util.TestUtils;
import com.sleepycat.je.utilint.DbLsn;
import com.sleepycat.je.utilint.Tracer;
public class IOExceptionTest extends TestCase {
private Environment env;
private Database db;
private File envHome;
public IOExceptionTest()
throws Exception {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
@Override
public void setUp()
throws IOException, DatabaseException {
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
}
@Override
public void tearDown()
throws IOException, DatabaseException {
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
if (db != null) {
db.close();
}
if (env != null) {
env.close();
}
TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
}
public void testRunRecoveryExceptionOnWrite()
throws Exception {
try {
createDatabase(200000, 0, false);
final int N_RECS = 25;
CheckpointConfig chkConf = new CheckpointConfig();
chkConf.setForce(true);
Transaction txn = env.beginTransaction(null, null);
int keyInt = 0;
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
for (int i = 0; i < N_RECS; i++) {
String keyStr = Integer.toString(keyInt);
DatabaseEntry key =
new DatabaseEntry(keyStr.getBytes());
DatabaseEntry data =
new DatabaseEntry(("d" + keyStr).getBytes());
if (i == (N_RECS - 1)) {
FileManager.THROW_RRE_FOR_UNIT_TESTS = true;
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
}
try {
assertTrue(db.put(txn, key, data) ==
OperationStatus.SUCCESS);
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
break;
}
}
try {
txn.commit();
fail("expected DatabaseException");
} catch (RunRecoveryException DE) {
}
forceCloseEnvOnly();
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
FileManager.THROW_RRE_FOR_UNIT_TESTS = false;
db = null;
env = null;
} catch (Throwable E) {
E.printStackTrace();
}
}
public void testIOExceptionNoRecovery()
throws Throwable {
doIOExceptionTest(false);
}
public void testIOExceptionWithRecovery()
throws Throwable {
doIOExceptionTest(true);
}
public void testEviction()
throws Exception {
try {
createDatabase(200000, 0, true);
final int N_RECS = 25;
CheckpointConfig chkConf = new CheckpointConfig();
chkConf.setForce(true);
Transaction txn = env.beginTransaction(null, null);
int keyInt = 0;
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
for (int i = 0; i < N_RECS; i++) {
String keyStr = Integer.toString(keyInt);
DatabaseEntry key =
new DatabaseEntry(keyStr.getBytes());
DatabaseEntry data =
new DatabaseEntry(("d" + keyStr).getBytes());
try {
assertTrue(db.put(txn, key, data) ==
OperationStatus.SUCCESS);
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
break;
}
}
try {
env.checkpoint(chkConf);
fail("expected DatabaseException");
} catch (DatabaseException DE) {
}
EnvironmentStats stats = env.getStats(null);
assertTrue((stats.getNFullINFlush() +
stats.getNFullBINFlush()) > 0);
/* Read back the data and make sure it all looks ok. */
for (int i = 0; i < N_RECS; i++) {
String keyStr = Integer.toString(keyInt);
DatabaseEntry key =
new DatabaseEntry(keyStr.getBytes());
DatabaseEntry data = new DatabaseEntry();
try {
assertTrue(db.get(txn, key, data, null) ==
OperationStatus.SUCCESS);
assertEquals(new String(data.getData()), "d" + keyStr);
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
break;
}
}
/*
* Now we have some IN's in the log buffer and there have been
* IOExceptions that will later force rewriting that buffer.
*/
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
try {
txn.commit();
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
} catch (Exception E) {
E.printStackTrace();
}
}
/*
* Test for SR 13898. Write out some records with
* IO_EXCEPTION_TESTING_ON_WRITE true thereby forcing some commits to be
* rewritten as aborts. Ensure that the checksums are correct on those
* rewritten records by reading them back with a file reader.
*/
public void testIOExceptionReadBack()
throws Exception {
createDatabase(100000, 1000, true);
/*
* Turn off daemons so we can check the size of the log
* deterministically.
*/
EnvironmentMutableConfig newConfig = new EnvironmentMutableConfig();
newConfig.setConfigParam("je.env.runCheckpointer", "false");
newConfig.setConfigParam("je.env.runCleaner", "false");
env.setMutableConfig(newConfig);
final int N_RECS = 25;
/* Intentionally corrupt the transaction commit record. */
CheckpointConfig chkConf = new CheckpointConfig();
chkConf.setForce(true);
Transaction txn = env.beginTransaction(null, null);
for (int i = 0; i < N_RECS; i++) {
String keyStr = Integer.toString(i);
DatabaseEntry key =
new DatabaseEntry(keyStr.getBytes());
DatabaseEntry data =
new DatabaseEntry(new byte[100]);
try {
assertTrue(db.put(txn, key, data) ==
OperationStatus.SUCCESS);
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
break;
}
try {
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
txn.commit();
fail("expected DatabaseException");
} catch (DatabaseException DE) {
}
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
txn = env.beginTransaction(null, null);
}
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
try {
txn.commit();
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
/* Flush the corrupted records to disk. */
try {
env.checkpoint(chkConf);
} catch (DatabaseException DE) {
DE.printStackTrace();
fail("unexpected DatabaseException");
}
EnvironmentStats stats = env.getStats(null);
assertTrue((stats.getNFullINFlush() +
stats.getNFullBINFlush()) > 0);
/*
* Figure out where the log starts and ends, and make a local
* FileReader class to mimic reading the logs. The only action we need
* is to run checksums on the entries.
*/
EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
long lastLsn = envImpl.getFileManager().getLastUsedLsn();
Long firstFile = envImpl.getFileManager().getFirstFileNum();
long firstLsn = DbLsn.makeLsn(firstFile, 0);
FileReader reader = new FileReader
(envImpl,
4096, // readBufferSize
true, // forward
firstLsn,
null, // singleFileNumber
lastLsn, // end of file lsn
DbLsn.NULL_LSN) { // finishLsn
protected boolean processEntry(ByteBuffer entryBuffer)
throws DatabaseException {
entryBuffer.position(entryBuffer.position() +
currentEntryHeader.getItemSize());
return true;
}
};
/* Read the logs, checking checksums. */
while (reader.readNextEntry()) {
}
/* Check that the reader reads all the way to last entry. */
assertEquals("last=" + DbLsn.getNoFormatString(lastLsn) +
" readerlast=" +
DbLsn.getNoFormatString(reader.getLastLsn()),
lastLsn,
reader.getLastLsn());
}
public void testLogBufferOverflowAbortNoDupes()
throws Exception {
doLogBufferOverflowTest(false, false);
}
public void testLogBufferOverflowCommitNoDupes()
throws Exception {
doLogBufferOverflowTest(true, false);
}
public void testLogBufferOverflowAbortDupes()
throws Exception {
doLogBufferOverflowTest(false, true);
}
public void testLogBufferOverflowCommitDupes()
throws Exception {
doLogBufferOverflowTest(true, true);
}
private void doLogBufferOverflowTest(boolean abort, boolean dupes)
throws Exception {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setCacheSize(100000);
envConfig.setConfigParam("java.util.logging.level", "OFF");
env = new Environment(envHome, envConfig);
String databaseName = "ioexceptiondb";
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(true);
dbConfig.setTransactional(true);
db = env.openDatabase(null, databaseName, dbConfig);
Transaction txn = env.beginTransaction(null, null);
DatabaseEntry oneKey =
(dupes ?
new DatabaseEntry("2".getBytes()) :
new DatabaseEntry("1".getBytes()));
DatabaseEntry oneData =
new DatabaseEntry(new byte[10]);
DatabaseEntry twoKey =
new DatabaseEntry("2".getBytes());
DatabaseEntry twoData =
new DatabaseEntry(new byte[100000]);
if (dupes) {
DatabaseEntry temp = oneKey;
oneKey = oneData;
oneData = temp;
temp = twoKey;
twoKey = twoData;
twoData = temp;
}
try {
assertTrue(db.put(txn, oneKey, oneData) ==
OperationStatus.SUCCESS);
db.put(txn, twoKey, twoData);
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
/* Read back the data and make sure it all looks ok. */
try {
assertTrue(db.get(txn, oneKey, oneData, null) ==
OperationStatus.SUCCESS);
assertTrue(oneData.getData().length == (dupes ? 1 : 10));
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
try {
assertTrue(db.get(txn, twoKey, twoData, null) ==
OperationStatus.SUCCESS);
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
try {
if (abort) {
txn.abort();
} else {
txn.commit();
}
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
/* Read back the data and make sure it all looks ok. */
try {
assertTrue(db.get(null, oneKey, oneData, null) ==
(abort ?
OperationStatus.NOTFOUND :
OperationStatus.SUCCESS));
assertTrue(oneData.getData().length == (dupes ? 1 : 10));
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
try {
assertTrue(db.get(null, twoKey, twoData, null) ==
(abort ?
OperationStatus.NOTFOUND :
OperationStatus.SUCCESS));
} catch (DatabaseException DE) {
fail("unexpected DatabaseException");
}
} catch (Exception E) {
E.printStackTrace();
}
}
public void testPutTransactionalWithIOException()
throws Throwable {
try {
createDatabase(100000, 0, true);
Transaction txn = env.beginTransaction(null, null);
int keyInt = 0;
String keyStr;
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
/* Fill up the buffer until we see an IOException. */
while (true) {
keyStr = Integer.toString(++keyInt);
DatabaseEntry key = new DatabaseEntry(keyStr.getBytes());
DatabaseEntry data =
new DatabaseEntry(("d" + keyStr).getBytes());
try {
assertTrue(db.put(txn, key, data) ==
OperationStatus.SUCCESS);
} catch (DatabaseException DE) {
break;
}
}
/* Buffer still hasn't been written. This should also fail. */
try {
db.put(txn,
new DatabaseEntry("shouldFail".getBytes()),
new DatabaseEntry("shouldFailD".getBytes()));
fail("expected DatabaseException");
} catch (IllegalStateException ISE) {
/* Expected. */
}
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
/* Buffer should write out ok now. */
try {
db.put(txn,
new DatabaseEntry("shouldAlsoFail".getBytes()),
new DatabaseEntry("shouldAlsoFailD".getBytes()));
fail("expected DatabaseException");
} catch (IllegalStateException ISE) {
/* Expected. */
}
txn.abort();
/* Txn aborted. None of the entries should be found. */
DatabaseEntry data = new DatabaseEntry();
assertTrue(db.get(null,
new DatabaseEntry("shouldAlsoFail".getBytes()),
data,
null) == OperationStatus.NOTFOUND);
assertTrue(db.get(null,
new DatabaseEntry("shouldFail".getBytes()),
data,
null) == OperationStatus.NOTFOUND);
assertTrue(db.get(null,
new DatabaseEntry("shouldFail".getBytes()),
data,
null) == OperationStatus.NOTFOUND);
assertTrue(db.get(null,
new DatabaseEntry(keyStr.getBytes()),
data,
null) == OperationStatus.NOTFOUND);
for (int i = --keyInt; i > 0; i--) {
keyStr = Integer.toString(i);
assertTrue(db.get(null,
new DatabaseEntry(keyStr.getBytes()),
data,
null) == OperationStatus.NOTFOUND);
}
} catch (Throwable T) {
T.printStackTrace();
}
}
public void testIOExceptionDuringFileFlippingWrite()
throws Throwable {
doIOExceptionDuringFileFlippingWrite(8, 33, 2);
}
private void doIOExceptionDuringFileFlippingWrite(int numIterations,
int exceptionStartWrite,
int exceptionWriteCount)
throws Throwable {
try {
EnvironmentConfig envConfig = new EnvironmentConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setConfigParam("je.log.fileMax", "1000");
envConfig.setConfigParam("je.log.bufferSize", "1025");
envConfig.setConfigParam("je.env.runCheckpointer", "false");
envConfig.setConfigParam("je.env.runCleaner", "false");
env = new Environment(envHome, envConfig);
EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
db = env.openDatabase(null, "foo", dbConfig);
/*
* Put one record into the database so it gets populated w/INs and
* LNs, and we can fake out the RMW commits used below.
*/
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry data = new DatabaseEntry();
IntegerBinding.intToEntry(5, key);
IntegerBinding.intToEntry(5, data);
db.put(null, key, data);
/*
* Now generate trace and commit log entries. The trace records
* aren't forced out, but the commit records are forced.
*/
FileManager.WRITE_COUNT = 0;
FileManager.THROW_ON_WRITE = true;
FileManager.STOP_ON_WRITE_COUNT = exceptionStartWrite;
FileManager.N_BAD_WRITES = exceptionWriteCount;
for (int i = 0; i < numIterations; i++) {
try {
/* Generate a non-forced record. */
if (i == (numIterations - 1)) {
/*
* On the last iteration, write a record that is large
* enough to force a file flip (i.e. an fsync which
* succeeds) followed by the large write (which doesn't
* succeed due to an IOException). In [#15754] the
* large write fails on Out Of Disk Space, rolling back
* the savedLSN to the previous file, even though the
* file has flipped. The subsequent write ends up in
* the flipped file, but at the offset of the older
* file (leaving a hole in the new flipped file).
*/
Tracer.trace(Level.SEVERE, envImpl,
i + "/" + FileManager.WRITE_COUNT +
" " + new String(new byte[2000]));
} else {
Tracer.trace(Level.SEVERE, envImpl,
i + "/" + FileManager.WRITE_COUNT +
" " + "xx");
}
} catch (IllegalStateException ISE) {
/* Eat exception thrown by TraceLogHandler. */
}
/*
* Generate a forced record by calling commit. Since RMW
* transactions that didn't actually do a write won't log a
* commit record, do an addLogInfo to trick the txn into
* logging a commit.
*/
Transaction txn = env.beginTransaction(null, null);
db.get(txn, key, data, LockMode.RMW);
DbInternal.getTxn(txn).addLogInfo(DbLsn.makeLsn(3, 3));
txn.commit();
}
db.close();
/*
* Verify that the log files are ok and have no checksum errors.
*/
FileReader reader =
new FileReader(DbInternal.envGetEnvironmentImpl(env),
4096, true, 0, null, DbLsn.NULL_LSN,
DbLsn.NULL_LSN) {
protected boolean processEntry(ByteBuffer entryBuffer)
throws DatabaseException {
entryBuffer.position(entryBuffer.position() +
currentEntryHeader.getItemSize());
return true;
}
};
DbInternal.envGetEnvironmentImpl(env).getLogManager().flush();
while (reader.readNextEntry()) {
}
/* Make sure the reader really did scan the files. */
assert (DbLsn.getFileNumber(reader.getLastLsn()) == 3) :
DbLsn.toString(reader.getLastLsn());
env.close();
env = null;
db = null;
} catch (Throwable T) {
T.printStackTrace();
} finally {
FileManager.STOP_ON_WRITE_COUNT = Long.MAX_VALUE;
FileManager.N_BAD_WRITES = Long.MAX_VALUE;
}
}
/*
* Test the following sequence:
*
* write LN, commit;
* write same LN (getting an IOException),
* write another LN (getting an IOException) verify fails due to must-abort
* either commit(should fail and abort automatically) + abort (should fail)
* or abort (should success since must-abort).
* Verify UP, ensuring that LSN of LN is not marked obsolete.
*/
public void testSR15761Part1()
throws Throwable {
doSR15761Test(true);
}
public void testSR15761Part2()
throws Throwable {
doSR15761Test(false);
}
private void doSR15761Test(boolean doCommit)
throws Throwable {
try {
createDatabase(100000, 0, false);
Transaction txn = env.beginTransaction(null, null);
int keyInt = 0;
String keyStr;
keyStr = Integer.toString(keyInt);
DatabaseEntry key = new DatabaseEntry(keyStr.getBytes());
DatabaseEntry data = new DatabaseEntry(new byte[2888]);
try {
assertTrue(db.put(txn, key, data) == OperationStatus.SUCCESS);
} catch (DatabaseException DE) {
fail("should have completed");
}
txn.commit();
LockStats stats = env.getLockStats(null);
int nLocksPrePut = stats.getNTotalLocks();
txn = env.beginTransaction(null, null);
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
try {
data = new DatabaseEntry(new byte[10000]);
assertTrue(db.put(txn, key, data) == OperationStatus.SUCCESS);
fail("expected IOException");
} catch (DatabaseException DE) {
/* Expected */
}
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
try {
data = new DatabaseEntry(new byte[10]);
assertTrue(db.put(txn, key, data) == OperationStatus.SUCCESS);
fail("expected IOException");
} catch (IllegalStateException ISE) {
/* Expected */
}
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
if (doCommit) {
try {
txn.commit();
fail("expected must-abort transaction exception");
} catch (DatabaseException DE) {
/* Expected. */
}
try {
txn.abort();
fail("expected failure on commit/abort sequence");
} catch (IllegalStateException ISE) {
/* Expected. */
}
} else {
try {
txn.abort();
} catch (DatabaseException DE) {
fail("expected abort to succeed");
}
}
/* Lock should not be held. */
stats = env.getLockStats(null);
int nLocksPostPut = stats.getNTotalLocks();
assertTrue(nLocksPrePut == nLocksPostPut);
UtilizationProfile up =
DbInternal.envGetEnvironmentImpl(env).getUtilizationProfile();
/*
* Checkpoint the environment to flush all utilization tracking
* information before verifying.
*/
CheckpointConfig ckptConfig = new CheckpointConfig();
ckptConfig.setForce(true);
env.checkpoint(ckptConfig);
assertTrue(up.verifyFileSummaryDatabase());
} catch (Throwable T) {
T.printStackTrace();
}
}
public void testAbortWithIOException()
throws Throwable {
Transaction txn = null;
createDatabase(0, 0, true);
writeAndVerify(null, false, "k1", "d1", false);
writeAndVerify(null, true, "k2", "d2", false);
writeAndVerify(null, false, "k3", "d3", false);
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
LockStats stats = env.getLockStats(null);
int nLocksPreGet = stats.getNTotalLocks();
/* Loop doing aborts until the buffer fills up and we get an IOE. */
while (true) {
txn = env.beginTransaction(null, null);
DatabaseEntry key = new DatabaseEntry("k1".getBytes());
DatabaseEntry returnedData = new DatabaseEntry();
/* Use RMW to get a write lock but not put anything in the log. */
OperationStatus status =
db.get(txn, key, returnedData, LockMode.RMW);
assertTrue(status == (OperationStatus.SUCCESS));
stats = env.getLockStats(null);
try {
txn.abort();
/*
* Keep going until we actually get an IOException from the
* buffer filling up.
*/
continue;
} catch (DatabaseException DE) {
break;
}
}
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
/* Lock should not be held. */
stats = env.getLockStats(null);
int nLocksPostAbort = stats.getNTotalLocks();
assertTrue(nLocksPreGet == nLocksPostAbort);
}
private void doIOExceptionTest(boolean doRecovery)
throws Throwable {
Transaction txn = null;
createDatabase(0, 0, true);
writeAndVerify(null, false, "k1", "d1", doRecovery);
writeAndVerify(null, true, "k2", "d2", doRecovery);
writeAndVerify(null, false, "k3", "d3", doRecovery);
txn = env.beginTransaction(null, null);
writeAndVerify(txn, false, "k4", "d4", false);
txn.abort();
verify(null, true, "k4", doRecovery);
verify(null, false, "k1", doRecovery);
verify(null, false, "k3", doRecovery);
txn = env.beginTransaction(null, null);
writeAndVerify(txn, false, "k4", "d4", false);
txn.commit();
verify(null, false, "k4", doRecovery);
txn = env.beginTransaction(null, null);
writeAndVerify(txn, true, "k5", "d5", false);
/* Ensure that writes after IOExceptions don't succeed. */
writeAndVerify(txn, false, "k5a", "d5a", false);
txn.abort();
verify(null, true, "k5", doRecovery);
verify(null, true, "k5a", doRecovery);
txn = env.beginTransaction(null, null);
LockStats stats = env.getLockStats(null);
int nLocksPrePut = stats.getNTotalLocks();
writeAndVerify(txn, false, "k6", "d6", false);
writeAndVerify(txn, true, "k6a", "d6a", false);
stats = env.getLockStats(null);
try {
txn.commit();
fail("expected DatabaseException");
} catch (DatabaseException DE) {
}
/* Lock should not be held. */
stats = env.getLockStats(null);
int nLocksPostCommit = stats.getNTotalLocks();
assertTrue(nLocksPrePut == nLocksPostCommit);
verify(null, true, "k6", doRecovery);
verify(null, true, "k6a", doRecovery);
txn = env.beginTransaction(null, null);
writeAndVerify(txn, false, "k6", "d6", false);
writeAndVerify(txn, true, "k6a", "d6a", false);
writeAndVerify(txn, false, "k6b", "d6b", false);
try {
txn.commit();
} catch (DatabaseException DE) {
fail("expected success");
}
/*
* k6a will still exist because the writeAndVerify didn't fail -- there
* was no write. The write happens at commit time.
*/
verify(null, false, "k6", doRecovery);
verify(null, false, "k6a", doRecovery);
verify(null, false, "k6b", doRecovery);
}
private void writeAndVerify(Transaction txn,
boolean throwIOException,
String keyString,
String dataString,
boolean doRecovery)
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry(keyString.getBytes());
DatabaseEntry data = new DatabaseEntry(dataString.getBytes());
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = throwIOException;
try {
assertTrue(db.put(txn, key, data) == OperationStatus.SUCCESS);
/*
* We don't expect an IOException if we're in a transaction because
* the put() only writes to the buffer, not the disk. The write to
* disk doesn't happen until the commit/abort.
*/
if (throwIOException && txn == null) {
fail("didn't catch DatabaseException.");
}
} catch (DatabaseException DE) {
if (!throwIOException) {
fail("caught DatabaseException.");
}
}
verify(txn, throwIOException, keyString, doRecovery);
}
private void verify(Transaction txn,
boolean expectFailure,
String keyString,
boolean doRecovery)
throws DatabaseException {
if (doRecovery) {
db.close();
forceCloseEnvOnly();
createDatabase(0, 0, true);
}
DatabaseEntry key = new DatabaseEntry(keyString.getBytes());
DatabaseEntry returnedData = new DatabaseEntry();
OperationStatus status =
db.get(txn,
key,
returnedData,
LockMode.DEFAULT);
assertTrue(status == ((expectFailure && txn == null) ?
OperationStatus.NOTFOUND :
OperationStatus.SUCCESS));
}
private void createDatabase(long cacheSize, long maxFileSize, boolean dups)
throws DatabaseException {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
envConfig.setConfigParam
(EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
envConfig.setConfigParam
(EnvironmentParams.LOG_MEM_SIZE.getName(),
EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
if (maxFileSize != 0) {
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam
(EnvironmentParams.LOG_FILE_MAX.getName(), "" + maxFileSize);
}
if (cacheSize != 0) {
envConfig.setCacheSize(cacheSize);
envConfig.setConfigParam("java.util.logging.level", "OFF");
}
env = new Environment(envHome, envConfig);
String databaseName = "ioexceptiondb";
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(dups);
dbConfig.setTransactional(true);
db = env.openDatabase(null, databaseName, dbConfig);
}
/* Force the environment to be closed even with outstanding handles.*/
private void forceCloseEnvOnly()
throws DatabaseException {
/* Close w/out checkpointing, in order to exercise recovery better.*/
try {
DbInternal.envGetEnvironmentImpl(env).close(false);
} catch (DatabaseException DE) {
if (!FileManager.IO_EXCEPTION_TESTING_ON_WRITE) {
throw DE;
} else {
/* Expect an exception from flushing the log manager. */
}
}
env = null;
}
}
|