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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: LogManagerTest.java,v 1.85.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.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.EnvironmentStats;
import com.sleepycat.je.StatsConfig;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.log.entry.SingleItemEntry;
import com.sleepycat.je.util.TestUtils;
import com.sleepycat.je.utilint.DbLsn;
import com.sleepycat.je.utilint.Tracer;
/**
* Test basic log management.
*/
public class LogManagerTest extends TestCase {
static private final boolean DEBUG = false;
private FileManager fileManager;
private LogManager logManager;
private File envHome;
private EnvironmentImpl env;
public LogManagerTest() {
super();
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void setUp()
throws DatabaseException, IOException {
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
}
public void tearDown()
throws IOException, DatabaseException {
TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
}
/**
* Log and retrieve objects, with log in memory
*/
public void testBasicInMemory()
throws DatabaseException {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
envConfig.setConfigParam
(EnvironmentParams.LOG_FILE_MAX.getName(), "1000");
turnOffDaemons(envConfig);
envConfig.setAllowCreate(true);
env = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
fileManager = env.getFileManager();
logManager = env.getLogManager();
logAndRetrieve();
env.close();
}
/**
* Log and retrieve objects, with log completely flushed to disk
*/
public void testBasicOnDisk()
throws Throwable {
try {
/*
* Force the buffers and files to be small. The log buffer is
* actually too small, will have to grow dynamically. Each file
* only holds one test item (each test item is 50 bytes).
*/
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam(
EnvironmentParams.LOG_MEM_SIZE.getName(),
EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
envConfig.setConfigParam(
EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
envConfig.setConfigParam(
EnvironmentParams.LOG_FILE_MAX.getName(), "79");
envConfig.setConfigParam(
EnvironmentParams.NODE_MAX.getName(), "6");
envConfig.setConfigParam
(EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
/* Disable noisy UtilizationProfile database creation. */
DbInternal.setCreateUP(envConfig, false);
/* Don't checkpoint utilization info for this test. */
DbInternal.setCheckpointUP(envConfig, false);
/* Don't run the cleaner without a UtilizationProfile. */
envConfig.setConfigParam
(EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
/*
* Don't run any daemons, those emit trace messages and other log
* entries and mess up our accounting.
*/
turnOffDaemons(envConfig);
envConfig.setAllowCreate(true);
/*
* Recreate the file manager and log manager w/different configs.
*/
env = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
fileManager = env.getFileManager();
logManager = env.getLogManager();
logAndRetrieve();
/*
* Expect 13 je files, 7 to hold logged records, 1 to hold root, 3
* to hold recovery messages, 2 for checkpoint records
*/
String[] names = fileManager.listFiles(FileManager.JE_SUFFIXES);
assertEquals("Should be 13 files on disk", 13, names.length);
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
env.close();
}
}
/**
* Log and retrieve objects, with some of log flushed to disk, some of log
* in memory.
*/
public void testComboDiskMemory()
throws Throwable {
try {
/*
* Force the buffers and files to be small. The log buffer is
* actually too small, will have to grow dynamically. Each file
* only holds one test item (each test item is 50 bytes)
*/
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam
(EnvironmentParams.LOG_MEM_SIZE.getName(),
EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
envConfig.setConfigParam
(EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
envConfig.setConfigParam
(EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
"64");
envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(),
"6");
/* Disable noisy UtilizationProfile database creation. */
DbInternal.setCreateUP(envConfig, false);
/* Don't checkpoint utilization info for this test. */
DbInternal.setCheckpointUP(envConfig, false);
/* Don't run the cleaner without a UtilizationProfile. */
envConfig.setConfigParam
(EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
/*
* Don't run the cleaner or the checkpointer daemons, those create
* more log entries and mess up our accounting
*/
turnOffDaemons(envConfig);
envConfig.setAllowCreate(true);
env = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
fileManager = env.getFileManager();
logManager = env.getLogManager();
logAndRetrieve();
/*
* Expect 13 je files:
* trace
* root
* ckptstart
* trace
* ckptend
* trace
* trace trace
* trace trace
* trace trace
* trace trace
* ckptstart
* trace
* ckptend
*
* This is based on a manual perusal of the log files and their
* contents. Changes in the sizes of log entries can throw this
* test off, and require that a check and a change to the assertion
* value.
*/
String[] names = fileManager.listFiles(FileManager.JE_SUFFIXES);
assertEquals("Should be 13 files on disk", 13, names.length);
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
env.close();
}
}
/**
* Log and retrieve objects, with some of log flushed to disk, some
* of log in memory. Force the read buffer to be very small
*/
public void testFaultingIn()
throws Throwable {
try {
/*
* Force the buffers and files to be small. The log buffer is
* actually too small, will have to grow dynamically. We read in 32
* byte chunks, will have to re-read only holds one test item (each
* test item is 50 bytes)
*/
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam
(EnvironmentParams.LOG_MEM_SIZE.getName(),
EnvironmentParams.LOG_MEM_SIZE_MIN_STRING);
envConfig.setConfigParam
(EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2");
envConfig.setConfigParam
(EnvironmentParams.LOG_FILE_MAX.getName(), "200");
envConfig.setConfigParam
(EnvironmentParams.LOG_FAULT_READ_SIZE.getName(), "32");
envConfig.setConfigParam
(EnvironmentParams.NODE_MAX.getName(), "6");
envConfig.setAllowCreate(true);
env = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
fileManager = env.getFileManager();
logManager = env.getLogManager();
logAndRetrieve();
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
env.close();
}
}
/**
* Log several objects, retrieve them.
*/
private void logAndRetrieve()
throws DatabaseException {
/* Make test loggable objects. */
List<Tracer> testRecs = new ArrayList<Tracer>();
for (int i = 0; i < 10; i++) {
testRecs.add(new Tracer("Hello there, rec " + (i+1)));
}
/* Log three of them, remember their LSNs. */
List<Long> testLsns = new ArrayList<Long>();
for (int i = 0; i < 3; i++) {
long lsn = testRecs.get(i).log(logManager);
if (DEBUG) {
System.out.println("i = " + i + " test LSN: file = " +
DbLsn.getFileNumber(lsn) +
" offset = " +
DbLsn.getFileOffset(lsn));
}
testLsns.add(new Long(lsn));
}
/* Ask for them back, out of order. */
assertEquals(testRecs.get(2),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(2))));
assertEquals(testRecs.get(0),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(0))));
assertEquals(testRecs.get(1),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(1))));
/* Intersperse logging and getting. */
testLsns.add(new Long(testRecs.get(3).log(logManager)));
testLsns.add(new Long(testRecs.get(4).log(logManager)));
assertEquals(testRecs.get(2),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(2))));
assertEquals(testRecs.get(4),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(4))));
/* Intersperse logging and getting. */
testLsns.add(new Long(testRecs.get(5).log(logManager)));
testLsns.add(new Long(testRecs.get(6).log(logManager)));
testLsns.add(new Long(testRecs.get(7).log(logManager)));
assertEquals(testRecs.get(7),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(7))));
assertEquals(testRecs.get(0),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(0))));
assertEquals(testRecs.get(6),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(6))));
/*
* Check that we can retrieve log entries as byte buffers, and get the
* correct object back. Used by replication.
*/
long lsn = testLsns.get(7).longValue();
ByteBuffer buffer = logManager.getByteBufferFromLog(lsn);
LogUtils.HeaderAndEntry contents =
LogUtils.readHeaderAndEntry(buffer,
null, // envImpl
false, // anticipateChecksumError
true); // readFullItem
assertEquals(testRecs.get(7),
(Tracer) contents.entry.getMainItem());
assertEquals(LogEntryType.LOG_TRACE.getTypeNum(),
contents.header.getType());
assertEquals(LogEntryType.LOG_VERSION,
contents.header.getVersion());
}
private void turnOffDaemons(EnvironmentConfig envConfig) {
envConfig.setConfigParam(
EnvironmentParams.ENV_RUN_CLEANER.getName(),
"false");
envConfig.setConfigParam(
EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(),
"false");
envConfig.setConfigParam(
EnvironmentParams.ENV_RUN_EVICTOR.getName(),
"false");
envConfig.setConfigParam(
EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(),
"false");
}
/**
* Log a few items, then hit exceptions. Make sure LSN state is correctly
* maintained and that items logged after the exceptions are at the correct
* locations on disk.
*/
public void testExceptions()
throws Throwable {
int logBufferSize = ((int) EnvironmentParams.LOG_MEM_SIZE_MIN) / 3;
int numLogBuffers = 5;
int logBufferMemSize = logBufferSize * numLogBuffers;
int logFileMax = 1000;
int okCounter = 0;
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
DbInternal.disableParameterValidation(envConfig);
envConfig.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(),
new Integer(logBufferMemSize).toString());
envConfig.setConfigParam
(EnvironmentParams.NUM_LOG_BUFFERS.getName(),
new Integer(numLogBuffers).toString());
envConfig.setConfigParam
(EnvironmentParams.LOG_FILE_MAX.getName(),
new Integer(logFileMax).toString());
envConfig.setConfigParam(
EnvironmentParams.NODE_MAX.getName(), "6");
envConfig.setConfigParam
(EnvironmentParams.JE_LOGGING_LEVEL.getName(), "SEVERE");
/* Disable noisy UtilizationProfile database creation. */
DbInternal.setCreateUP(envConfig, false);
/* Don't checkpoint utilization info for this test. */
DbInternal.setCheckpointUP(envConfig, false);
/* Don't run the cleaner without a UtilizationProfile. */
envConfig.setConfigParam
(EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
/*
* Don't run any daemons, those emit trace messages and other log
* entries and mess up our accounting.
*/
turnOffDaemons(envConfig);
envConfig.setAllowCreate(true);
env = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
fileManager = env.getFileManager();
logManager = env.getLogManager();
/* Keep track of items logged and their LSNs. */
ArrayList<Tracer> testRecs = new ArrayList<Tracer>();
ArrayList<Long> testLsns = new ArrayList<Long>();
/*
* Intersperse:
* - log successfully
* - log w/failure because the item doesn't fit in the log buffer
* - have I/O failures writing out the log
* Verify that all expected items can be read. Some will come
* from the log buffer pool.
* Then close and re-open the environment, to verify that
* all log items are faulted from disk
*/
/* Successful log. */
addOkayItem(logManager, okCounter++,
testRecs, testLsns, logBufferSize);
/* Item that's too big for the log buffers. */
attemptTooBigItem(logManager, logBufferSize, testRecs, testLsns);
/* Successful log. */
addOkayItem(logManager, okCounter++,
testRecs, testLsns, logBufferSize);
/*
* This verify read the items from the log buffers. Note before SR
* #12638 existed (LSN state not restored properly after exception
* because of too-small log buffer), this verify hung.
*/
verifyOkayItems(logManager, testRecs, testLsns, true);
/* More successful logs, along with a few too-big items. */
for (;okCounter < 23; okCounter++) {
addOkayItem(logManager, okCounter, testRecs,
testLsns, logBufferSize);
if ((okCounter % 4) == 0) {
attemptTooBigItem(logManager, logBufferSize,
testRecs, testLsns);
}
/*
* If we verify in the loop, sometimes we'll read from disk and
* sometimes from the log buffer pool.
*/
verifyOkayItems(logManager, testRecs, testLsns, true);
}
/*
* Test the case where we flip files and write the old write buffer
* out before we try getting a log buffer for the new item. We need
* to
*
* - hit a log-too-small exceptin
* - right after, we need to log an item that is small enough
* to fit in the log buffer but big enough to require that
* we flip to a new file.
*/
long nextLsn = fileManager.getNextLsn();
long fileOffset = DbLsn.getFileOffset(nextLsn);
assertTrue((logFileMax - fileOffset ) < logBufferSize);
attemptTooBigItem(logManager, logBufferSize, testRecs, testLsns);
addOkayItem(logManager, okCounter++,
testRecs, testLsns, logBufferSize,
((int)(logFileMax - fileOffset)));
verifyOkayItems(logManager, testRecs, testLsns, true);
/* Invoke some i/o exceptions. */
for (;okCounter < 50; okCounter++) {
attemptIOException(logManager, fileManager, testRecs,
testLsns, false);
addOkayItem(logManager, okCounter,
testRecs, testLsns, logBufferSize);
verifyOkayItems(logManager, testRecs, testLsns, false);
}
/*
* Finally, close this environment and re-open, and read all
* expected items from disk.
*/
env.close();
envConfig.setAllowCreate(false);
env = new EnvironmentImpl(envHome,
envConfig,
null /*sharedCacheEnv*/,
false /*replicationIntended*/);
fileManager = env.getFileManager();
logManager = env.getLogManager();
verifyOkayItems(logManager, testRecs, testLsns, false);
/* Check that we read these items off disk. */
EnvironmentStats stats = new EnvironmentStats();
StatsConfig config = new StatsConfig();
logManager.loadStats(config, stats);
assertTrue(stats.getEndOfLog() > 0);
assertTrue(stats.getNNotResident() >= testRecs.size());
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally {
env.close();
}
}
private void addOkayItem(LogManager logManager,
int tag,
List<Tracer> testRecs,
List<Long> testLsns,
int logBufferSize,
int fillerLen)
throws DatabaseException {
String filler = new String(new byte[fillerLen]);
Tracer t = new Tracer("okay" + filler + tag );
assertTrue(logBufferSize > t.getLogSize());
testRecs.add(t);
long lsn = t.log(logManager);
testLsns.add(new Long(lsn));
}
private void addOkayItem(LogManager logManager,
int tag,
List<Tracer> testRecs,
List<Long> testLsns,
int logBufferSize)
throws DatabaseException {
addOkayItem(logManager, tag, testRecs, testLsns, logBufferSize, 0);
}
private void attemptTooBigItem(LogManager logManager,
int logBufferSize,
Tracer big,
List<Tracer> testRecs,
List<Long> testLsns) {
assertTrue(big.getLogSize() > logBufferSize);
try {
long lsn = big.log(logManager);
testLsns.add(new Long(lsn));
testRecs.add(big);
} catch (DatabaseException expected) {
fail("Should not have hit exception.");
}
}
private void attemptTooBigItem(LogManager logManager,
int logBufferSize,
List<Tracer> testRecs,
List<Long> testLsns) {
String stuff = "12345679890123456798901234567989012345679890";
while (stuff.length() < EnvironmentParams.LOG_MEM_SIZE_MIN) {
stuff += stuff;
}
Tracer t = new Tracer(stuff);
attemptTooBigItem(logManager, logBufferSize, t, testRecs, testLsns);
}
private void attemptIOException(LogManager logManager,
FileManager fileManager,
List<Tracer> testRecs,
List<Long> testLsns,
boolean forceFlush) {
Tracer t = new Tracer("ioException");
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = true;
try {
/*
* This object might get flushed to disk -- depend on whether
* the ioexception happened before or after the copy into the
* log buffer. Both are valid, but the test doesn't yet
* know how to differentiate the cases.
testLsns.add(new Long(fileManager.getNextLsn()));
testRecs.add(t);
*/
logManager.logForceFlush
(new SingleItemEntry(LogEntryType.LOG_TRACE, t),
true, // fsyncRequired
ReplicationContext.NO_REPLICATE);
fail("expect io exception");
} catch (DatabaseException expected) {
} finally {
FileManager.IO_EXCEPTION_TESTING_ON_WRITE = false;
}
}
private void verifyOkayItems(LogManager logManager,
ArrayList<Tracer> testRecs,
ArrayList<Long> testLsns,
boolean checkOrder)
throws DatabaseException {
/* read forwards. */
for (int i = 0; i < testRecs.size(); i++) {
assertEquals(testRecs.get(i),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(i))));
}
/* Make sure LSNs are adjacent */
assertEquals(testLsns.size(), testRecs.size());
if (checkOrder) {
/*
* TODO: sometimes an ioexception entry will make it into the write
* buffer, and sometimes it won't. It depends on whether the IO
* exception was thrown when before or after the logabble item was
* written into the buffer. I haven't figure out yet how to tell
* the difference, so for now, we don't check order in the portion
* of the test that issues IO exceptions.
*/
for (int i = 1; i < testLsns.size(); i++) {
long lsn = testLsns.get(i).longValue();
long lsnFile = DbLsn.getFileNumber(lsn);
long lsnOffset = DbLsn.getFileOffset(lsn);
long prevLsn = testLsns.get(i-1).longValue();
long prevFile = DbLsn.getFileNumber(prevLsn);
long prevOffset = DbLsn.getFileOffset(prevLsn);
if (prevFile == lsnFile) {
assertEquals("item " + i + "prev = " +
DbLsn.toString(prevLsn) +
" current=" + DbLsn.toString(lsn),
(testRecs.get(i-1).getLogSize() +
LogEntryHeader.MIN_HEADER_SIZE),
lsnOffset - prevOffset);
} else {
assertEquals(prevFile+1, lsnFile);
assertEquals(FileManager.firstLogEntryOffset(),
lsnOffset);
}
}
}
/* read backwards. */
for (int i = testRecs.size() - 1; i > -1; i--) {
assertEquals(testRecs.get(i),
(Tracer) logManager.get
(DbLsn.longToLsn(testLsns.get(i))));
}
}
}
|