1 8 9 package com.sleepycat.je.log; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import junit.framework.TestCase; 17 18 import com.sleepycat.je.DatabaseException; 19 import com.sleepycat.je.DbInternal; 20 import com.sleepycat.je.EnvironmentConfig; 21 import com.sleepycat.je.EnvironmentStats; 22 import com.sleepycat.je.StatsConfig; 23 import com.sleepycat.je.config.EnvironmentParams; 24 import com.sleepycat.je.dbi.EnvironmentImpl; 25 import com.sleepycat.je.util.TestUtils; 26 import com.sleepycat.je.utilint.DbLsn; 27 import com.sleepycat.je.utilint.Tracer; 28 29 32 public class LogManagerTest extends TestCase { 33 34 static private final boolean DEBUG = false; 35 36 private FileManager fileManager; 37 private LogManager logManager; 38 private File envHome; 39 private EnvironmentImpl env; 40 41 public LogManagerTest() { 42 super(); 43 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 44 } 45 46 public void setUp() 47 throws DatabaseException, IOException { 48 49 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 50 TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX); 51 } 52 53 public void tearDown() 54 throws IOException , DatabaseException { 55 56 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 57 } 58 59 62 public void testBasicInMemory() 63 throws DatabaseException { 64 65 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 66 DbInternal.disableParameterValidation(envConfig); 67 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6"); 68 envConfig.setConfigParam 69 (EnvironmentParams.LOG_FILE_MAX.getName(), "1000"); 70 71 envConfig.setAllowCreate(true); 72 env = new EnvironmentImpl(envHome, envConfig); 73 fileManager = env.getFileManager(); 74 logManager = env.getLogManager(); 75 76 logAndRetrieve(); 77 env.close(); 78 } 79 80 83 public void testBasicOnDisk() 84 throws Throwable { 85 86 try { 87 88 93 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 94 DbInternal.disableParameterValidation(envConfig); 95 envConfig.setConfigParam( 96 EnvironmentParams.LOG_MEM_SIZE.getName(), 97 EnvironmentParams.LOG_MEM_SIZE_MIN_STRING); 98 envConfig.setConfigParam( 99 EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2"); 100 envConfig.setConfigParam( 101 EnvironmentParams.LOG_FILE_MAX.getName(), "79"); 102 envConfig.setConfigParam( 103 EnvironmentParams.NODE_MAX.getName(), "6"); 104 envConfig.setConfigParam 105 (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG"); 106 107 108 DbInternal.setCreateUP(envConfig, false); 109 110 DbInternal.setCheckpointUP(envConfig, false); 111 112 envConfig.setConfigParam 113 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false"); 114 115 119 turnOffDaemons(envConfig); 120 envConfig.setAllowCreate(true); 121 122 125 env = new EnvironmentImpl(envHome, envConfig); 126 fileManager = env.getFileManager(); 127 logManager = env.getLogManager(); 128 129 logAndRetrieve(); 130 131 135 String [] names = fileManager.listFiles(FileManager.JE_SUFFIXES); 136 assertEquals("Should be 13 files on disk", 13, names.length); 137 } catch (Throwable t) { 138 t.printStackTrace(); 139 throw t; 140 } finally { 141 env.close(); 142 } 143 } 144 145 149 public void testComboDiskMemory() 150 throws Throwable { 151 152 try { 153 154 159 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 160 DbInternal.disableParameterValidation(envConfig); 161 envConfig.setConfigParam 162 (EnvironmentParams.LOG_MEM_SIZE.getName(), 163 EnvironmentParams.LOG_MEM_SIZE_MIN_STRING); 164 envConfig.setConfigParam 165 (EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2"); 166 envConfig.setConfigParam 167 (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG"); 168 envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(), 169 "142"); 170 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), 171 "6"); 172 173 174 DbInternal.setCreateUP(envConfig, false); 175 176 DbInternal.setCheckpointUP(envConfig, false); 177 178 envConfig.setConfigParam 179 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false"); 180 181 185 turnOffDaemons(envConfig); 186 envConfig.setAllowCreate(true); 187 188 env = new EnvironmentImpl(envHome, envConfig); 189 fileManager = env.getFileManager(); 190 logManager = env.getLogManager(); 191 192 logAndRetrieve(); 193 194 198 String [] names = fileManager.listFiles(FileManager.JE_SUFFIXES); 199 assertEquals("Should be 8 files on disk", 8, names.length); 200 } catch (Throwable t) { 201 t.printStackTrace(); 202 throw t; 203 } finally { 204 env.close(); 205 } 206 } 207 208 212 public void testFaultingIn() 213 throws Throwable { 214 215 try { 216 217 223 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 224 DbInternal.disableParameterValidation(envConfig); 225 envConfig.setConfigParam 226 (EnvironmentParams.LOG_MEM_SIZE.getName(), 227 EnvironmentParams.LOG_MEM_SIZE_MIN_STRING); 228 envConfig.setConfigParam 229 (EnvironmentParams.NUM_LOG_BUFFERS.getName(), "2"); 230 envConfig.setConfigParam 231 (EnvironmentParams.LOG_FILE_MAX.getName(), "200"); 232 envConfig.setConfigParam 233 (EnvironmentParams.LOG_FAULT_READ_SIZE.getName(), "32"); 234 envConfig.setConfigParam 235 (EnvironmentParams.NODE_MAX.getName(), "6"); 236 envConfig.setAllowCreate(true); 237 env = new EnvironmentImpl(envHome, envConfig); 238 fileManager = env.getFileManager(); 239 logManager = env.getLogManager(); 240 241 logAndRetrieve(); 242 } catch (Throwable t) { 243 t.printStackTrace(); 244 throw t; 245 } finally { 246 env.close(); 247 } 248 } 249 250 253 private void logAndRetrieve() 254 throws DatabaseException { 255 256 257 258 List testRecs = new ArrayList (); 259 for (int i = 0; i < 10; i++) { 260 testRecs.add(new Tracer("Hello there, rec " + (i+1))); 261 } 262 263 264 List testLsns = new ArrayList (); 265 266 for (int i = 0; i < 3; i++) { 267 long lsn = logManager.log((Tracer)testRecs.get(i)); 268 if (DEBUG) { 269 System.out.println("i = " + i + " test LSN: file = " + 270 DbLsn.getFileNumber(lsn) + 271 " offset = " + 272 DbLsn.getFileOffset(lsn)); 273 } 274 testLsns.add(new Long (lsn)); 275 } 276 277 278 assertEquals((Tracer) testRecs.get(2), 279 (Tracer) logManager.get 280 (DbLsn.longToLsn((Long ) testLsns.get(2)))); 281 assertEquals((Tracer) testRecs.get(0), 282 (Tracer) logManager.get 283 (DbLsn.longToLsn((Long ) testLsns.get(0)))); 284 assertEquals((Tracer) testRecs.get(1), 285 (Tracer) logManager.get 286 (DbLsn.longToLsn((Long ) testLsns.get(1)))); 287 288 289 testLsns.add(new Long (logManager.log((Tracer)testRecs.get(3)))); 290 testLsns.add(new Long (logManager.log((Tracer)testRecs.get(4)))); 291 292 assertEquals((Tracer) testRecs.get(2), 293 (Tracer) logManager.get 294 (DbLsn.longToLsn((Long ) testLsns.get(2)))); 295 assertEquals((Tracer) testRecs.get(4), 296 (Tracer) logManager.get 297 (DbLsn.longToLsn((Long ) testLsns.get(4)))); 298 299 300 testLsns.add(new Long (logManager.log((Tracer) testRecs.get(5)))); 301 testLsns.add(new Long (logManager.log((Tracer) testRecs.get(6)))); 302 testLsns.add(new Long (logManager.log((Tracer) testRecs.get(7)))); 303 304 assertEquals((Tracer) testRecs.get(7), 305 (Tracer) logManager.get 306 (DbLsn.longToLsn((Long ) testLsns.get(7)))); 307 assertEquals((Tracer) testRecs.get(0), 308 (Tracer) logManager.get 309 (DbLsn.longToLsn((Long ) testLsns.get(0)))); 310 assertEquals((Tracer) testRecs.get(6), 311 (Tracer) logManager.get 312 (DbLsn.longToLsn((Long ) testLsns.get(6)))); 313 } 314 315 private void turnOffDaemons(EnvironmentConfig envConfig) { 316 envConfig.setConfigParam( 317 EnvironmentParams.ENV_RUN_CLEANER.getName(), 318 "false"); 319 envConfig.setConfigParam( 320 EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), 321 "false"); 322 envConfig.setConfigParam( 323 EnvironmentParams.ENV_RUN_EVICTOR.getName(), 324 "false"); 325 envConfig.setConfigParam( 326 EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), 327 "false"); 328 } 329 330 335 public void testExceptions() 336 throws Throwable { 337 338 int logBufferSize = ((int) EnvironmentParams.LOG_MEM_SIZE_MIN) / 3; 339 int numLogBuffers = 5; 340 int logBufferMemSize = logBufferSize * numLogBuffers; 341 int logFileMax = 1000; 342 int okCounter = 0; 343 344 try { 345 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 346 DbInternal.disableParameterValidation(envConfig); 347 envConfig.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(), 348 new Integer (logBufferMemSize).toString()); 349 envConfig.setConfigParam 350 (EnvironmentParams.NUM_LOG_BUFFERS.getName(), 351 new Integer (numLogBuffers).toString()); 352 envConfig.setConfigParam 353 (EnvironmentParams.LOG_FILE_MAX.getName(), 354 new Integer (logFileMax).toString()); 355 envConfig.setConfigParam( 356 EnvironmentParams.NODE_MAX.getName(), "6"); 357 envConfig.setConfigParam 358 (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "SEVERE"); 359 360 361 DbInternal.setCreateUP(envConfig, false); 362 363 DbInternal.setCheckpointUP(envConfig, false); 364 365 envConfig.setConfigParam 366 (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false"); 367 368 372 turnOffDaemons(envConfig); 373 envConfig.setAllowCreate(true); 374 env = new EnvironmentImpl(envHome, envConfig); 375 fileManager = env.getFileManager(); 376 logManager = env.getLogManager(); 377 378 379 ArrayList testRecs = new ArrayList (); 380 ArrayList testLsns = new ArrayList (); 381 382 392 393 394 addOkayItem(logManager, okCounter++, 395 testRecs, testLsns, logBufferSize); 396 397 398 attemptTooBigItem(logManager, logBufferSize, testRecs, testLsns); 399 400 401 addOkayItem(logManager, okCounter++, 402 testRecs, testLsns, logBufferSize); 403 404 409 verifyOkayItems(logManager, testRecs, testLsns, true); 410 411 412 413 for (;okCounter < 23; okCounter++) { 414 addOkayItem(logManager, okCounter, testRecs, 415 testLsns, logBufferSize); 416 417 if ((okCounter % 4) == 0) { 418 attemptTooBigItem(logManager, logBufferSize, 419 testRecs, testLsns); 420 } 421 425 verifyOkayItems(logManager, testRecs, testLsns, true); 426 } 427 428 438 long nextLsn = fileManager.getNextLsn(); 439 long fileOffset = DbLsn.getFileOffset(nextLsn); 440 441 assertTrue((logFileMax - fileOffset ) < logBufferSize); 442 attemptTooBigItem(logManager, logBufferSize, testRecs, testLsns); 443 addOkayItem(logManager, okCounter++, 444 testRecs, testLsns, logBufferSize, 445 ((int)(logFileMax - fileOffset))); 446 verifyOkayItems(logManager, testRecs, testLsns, true); 447 448 449 for (;okCounter < 50; okCounter++) { 450 attemptIOException(logManager, fileManager, testRecs, 451 testLsns, false); 452 addOkayItem(logManager, okCounter, 453 testRecs, testLsns, logBufferSize); 454 verifyOkayItems(logManager, testRecs, testLsns, false); 455 } 456 457 461 env.close(); 462 envConfig.setAllowCreate(false); 463 env = new EnvironmentImpl(envHome, envConfig); 464 fileManager = env.getFileManager(); 465 logManager = env.getLogManager(); 466 verifyOkayItems(logManager, testRecs, testLsns, false); 467 468 469 EnvironmentStats stats = new EnvironmentStats(); 470 StatsConfig config = new StatsConfig(); 471 logManager.loadStats(config, stats); 472 assertTrue(stats.getNNotResident() >= testRecs.size()); 473 474 } catch (Throwable t) { 475 t.printStackTrace(); 476 throw t; 477 } finally { 478 env.close(); 479 } 480 } 481 482 private void addOkayItem(LogManager logManager, 483 int tag, 484 List testRecs, 485 List testLsns, 486 int logBufferSize, 487 int fillerLen) 488 throws DatabaseException { 489 490 String filler = new String (new byte[fillerLen]); 491 Tracer t = new Tracer("okay" + filler + tag ); 492 assertTrue(logBufferSize > t.getLogSize()); 493 testRecs.add(t); 494 long lsn = logManager.log(t); 495 testLsns.add(new Long (lsn)); 496 } 497 498 private void addOkayItem(LogManager logManager, 499 int tag, 500 List testRecs, 501 List testLsns, 502 int logBufferSize) 503 throws DatabaseException { 504 505 addOkayItem(logManager, tag, testRecs, testLsns, logBufferSize, 0); 506 } 507 508 private void attemptTooBigItem(LogManager logManager, 509 int logBufferSize, 510 LoggableObject l, 511 List testRecs, 512 List testLsns) { 513 assertTrue(l.getLogSize() > logBufferSize); 514 515 try { 516 long lsn = logManager.log(l); 517 testLsns.add(new Long (lsn)); 518 testRecs.add(l); 519 } catch (DatabaseException expected) { 520 fail("Should not have hit exception."); 521 } 522 } 523 private void attemptTooBigItem(LogManager logManager, 524 int logBufferSize, 525 List testRecs, 526 List testLsns) { 527 String stuff = "12345679890123456798901234567989012345679890"; 528 while (stuff.length() < EnvironmentParams.LOG_MEM_SIZE_MIN) { 529 stuff += stuff; 530 } 531 Tracer t = new Tracer(stuff); 532 attemptTooBigItem(logManager, logBufferSize, t, testRecs, testLsns); 533 } 534 535 private void attemptIOException(LogManager logManager, 536 FileManager fileManager, 537 List testRecs, 538 List testLsns, 539 boolean forceFlush) { 540 Tracer t = new Tracer("ioException"); 541 FileManager.IO_EXCEPTION_TESTING = true; 542 try { 543 544 553 logManager.logForceFlush(t, true); 554 fail("expect io exception"); 555 } catch (DatabaseException expected) { 556 } finally { 557 FileManager.IO_EXCEPTION_TESTING = false; 558 } 559 } 560 561 private void verifyOkayItems(LogManager logManager, 562 ArrayList testRecs, 563 ArrayList testLsns, 564 boolean checkOrder) 565 throws DatabaseException { 566 567 568 for (int i = 0; i < testRecs.size(); i++) { 569 assertEquals((Tracer) testRecs.get(i), 570 (Tracer) logManager.get 571 (DbLsn.longToLsn((Long ) testLsns.get(i)))); 572 573 } 574 575 576 assertEquals(testLsns.size(), testRecs.size()); 577 578 if (checkOrder) { 579 580 588 for (int i = 1; i < testLsns.size(); i++) { 589 590 long lsn = ((Long ) testLsns.get(i)).longValue(); 591 long lsnFile = DbLsn.getFileNumber(lsn); 592 long lsnOffset = DbLsn.getFileOffset(lsn); 593 long prevLsn = ((Long ) testLsns.get(i-1)).longValue(); 594 long prevFile = DbLsn.getFileNumber(prevLsn); 595 long prevOffset = DbLsn.getFileOffset(prevLsn); 596 if (prevFile == lsnFile) { 597 assertEquals("item " + i + "prev = " + 598 DbLsn.toString(prevLsn) + 599 " current=" + DbLsn.toString(lsn), 600 (((Tracer) testRecs.get(i-1)).getLogSize() + 601 LogManager.HEADER_BYTES), 602 lsnOffset - prevOffset); 603 } else { 604 assertEquals(prevFile+1, lsnFile); 605 assertEquals(FileManager.firstLogEntryOffset(), 606 lsnOffset); 607 } 608 } 609 } 610 611 612 for (int i = testRecs.size() - 1; i > -1; i--) { 613 assertEquals((Tracer) testRecs.get(i), 614 (Tracer) logManager.get 615 (DbLsn.longToLsn((Long ) testLsns.get(i)))); 616 617 } 618 } 619 } 620 | Popular Tags |