1 8 9 package com.sleepycat.je.log; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.RandomAccessFile ; 14 import java.util.Set ; 15 16 import junit.framework.TestCase; 17 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseConfig; 20 import com.sleepycat.je.DatabaseEntry; 21 import com.sleepycat.je.DatabaseException; 22 import com.sleepycat.je.DbInternal; 23 import com.sleepycat.je.Environment; 24 import com.sleepycat.je.EnvironmentConfig; 25 import com.sleepycat.je.config.EnvironmentParams; 26 import com.sleepycat.je.dbi.EnvironmentImpl; 27 import com.sleepycat.je.util.TestUtils; 28 import com.sleepycat.je.utilint.DbLsn; 29 30 33 public class FileManagerTest extends TestCase { 34 35 static private int FILE_SIZE = 120; 36 37 private EnvironmentImpl envImpl; 38 private FileManager fileManager; 39 private File envHome; 40 41 public FileManagerTest() { 42 super(); 43 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 44 } 45 46 protected void setUp() 47 throws DatabaseException, IOException { 48 49 50 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 51 52 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 53 DbInternal.disableParameterValidation(envConfig); 54 envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(), 55 new Integer (FILE_SIZE).toString()); 56 57 envConfig.setConfigParam 58 (EnvironmentParams.LOG_FILE_CACHE_SIZE.getName(), "3"); 59 envConfig.setAllowCreate(true); 60 envImpl = new EnvironmentImpl(envHome, envConfig); 61 62 63 envImpl.close(); 64 envImpl.open(); 65 fileManager = new FileManager(envImpl, envHome, false); 66 67 72 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 73 } 74 75 protected void tearDown() 76 throws IOException , DatabaseException { 77 78 if (fileManager != null) { 79 fileManager.clear(); 80 fileManager.close(); 81 } 82 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 83 } 84 85 88 public void testLsnBumping() 89 throws Exception { 90 91 119 120 try { 121 122 123 124 long hdrSize = FileManager.firstLogEntryOffset(); 125 126 fileManager.bumpLsn(30L); 127 128 assertEquals(DbLsn.makeLsn(0, hdrSize), 129 fileManager.getLastUsedLsn()); 130 131 assertEquals(0, fileManager.getPrevEntryOffset()); 132 133 fileManager.bumpLsn(50L); 134 135 assertEquals(DbLsn.makeLsn(0, (hdrSize + 30)), 136 fileManager.getLastUsedLsn()); 137 assertEquals(hdrSize, fileManager.getPrevEntryOffset()); 138 139 140 fileManager.bumpLsn(40L); 141 142 assertEquals(DbLsn.makeLsn(1, hdrSize), 143 fileManager.getLastUsedLsn()); 144 assertEquals(0, fileManager.getPrevEntryOffset()); 145 146 fileManager.bumpLsn(20L); 147 148 assertEquals(DbLsn.makeLsn(1,(hdrSize+40)), 149 fileManager.getLastUsedLsn()); 150 assertEquals(hdrSize, fileManager.getPrevEntryOffset()); 151 152 fileManager.bumpLsn(10L); 153 154 assertEquals(DbLsn.makeLsn(1,(hdrSize+60)), 155 fileManager.getLastUsedLsn()); 156 assertEquals(hdrSize+40, fileManager.getPrevEntryOffset()); 157 158 fileManager.bumpLsn(5L); 159 160 assertEquals(DbLsn.makeLsn(1,(hdrSize+70)), 161 fileManager.getLastUsedLsn()); 162 assertEquals(hdrSize+60, fileManager.getPrevEntryOffset()); 163 164 165 fileManager.bumpLsn(75L); 166 167 assertEquals(DbLsn.makeLsn(2, hdrSize), 168 fileManager.getLastUsedLsn()); 169 assertEquals(0, fileManager.getPrevEntryOffset()); 170 171 172 fileManager.bumpLsn(125L); 173 174 assertEquals(DbLsn.makeLsn(3, hdrSize), 175 fileManager.getLastUsedLsn()); 176 assertEquals(0, fileManager.getPrevEntryOffset()); 177 178 179 fileManager.bumpLsn(10L); 180 181 assertEquals(DbLsn.makeLsn(4, hdrSize), 182 fileManager.getLastUsedLsn()); 183 assertEquals(0, fileManager.getPrevEntryOffset()); 184 185 fileManager.bumpLsn(20L); 186 187 assertEquals(DbLsn.makeLsn(4, (hdrSize+10)), 188 fileManager.getLastUsedLsn()); 189 assertEquals(hdrSize, fileManager.getPrevEntryOffset()); 190 191 fileManager.bumpLsn(30L); 192 193 assertEquals(DbLsn.makeLsn(4, (hdrSize+30)), 194 fileManager.getLastUsedLsn()); 195 assertEquals((hdrSize+10), fileManager.getPrevEntryOffset()); 196 197 } catch (Exception e) { 198 e.printStackTrace(); 199 throw e; 200 } 201 } 202 203 206 public void testSetLastPosition() 207 throws DatabaseException { 208 209 212 fileManager.setLastPosition( DbLsn.makeLsn(79L, 88L), 214 DbLsn.makeLsn(79L, 77), 215 66L); 216 217 218 fileManager.bumpLsn(11L); 219 assertEquals(DbLsn.makeLsn(79L, 88L), fileManager.getLastUsedLsn()); 220 assertEquals(77L, fileManager.getPrevEntryOffset()); 221 222 223 fileManager.bumpLsn(22L); 224 assertEquals(DbLsn.makeLsn(80L, FileManager.firstLogEntryOffset()), 225 fileManager.getLastUsedLsn()); 226 assertEquals(0, fileManager.getPrevEntryOffset()); 227 } 228 229 232 public void testFileNameFormat() 233 throws DatabaseException { 234 235 String filePrefix = envHome + File.separator; 236 assertEquals(filePrefix + "00000001.jdb", 237 fileManager.getFullFileNames(1L)[0]); 238 assertEquals(filePrefix + "0000007b.jdb", 239 fileManager.getFullFileNames(123L)[0]); 240 } 241 242 245 public void testFileCreation() 246 throws IOException , DatabaseException { 247 248 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 249 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 250 251 String [] jeFiles = fileManager.listFiles(FileManager.JE_SUFFIXES); 252 253 assertEquals("Should have two files", 2, jeFiles.length); 254 255 256 File fakeFile1 = new File (envHome, "00000abx.jdb"); 257 File fakeFile2 = new File (envHome, "10.10.jdb"); 258 fakeFile1.createNewFile(); 259 fakeFile2.createNewFile(); 260 261 jeFiles = fileManager.listFiles(FileManager.JE_SUFFIXES); 262 assertEquals("Should have two files", 2, jeFiles.length); 263 264 265 FileHandle file0Handle = fileManager.getFileHandle(0L); 266 FileHandle file1Handle = fileManager.getFileHandle(1L); 267 268 jeFiles = fileManager.listFiles(FileManager.JE_SUFFIXES); 269 assertEquals("Should have two files", 2, jeFiles.length); 270 file0Handle.release(); 271 file1Handle.release(); 272 273 274 fileManager.clear(); 275 file0Handle = fileManager.getFileHandle(0L); 276 file1Handle = fileManager.getFileHandle(1L); 277 278 jeFiles = fileManager.listFiles(FileManager.JE_SUFFIXES); 279 assertEquals("Should have two files", 2, jeFiles.length); 280 file0Handle.close(); 281 file1Handle.close(); 282 file0Handle.release(); 283 file1Handle.release(); 284 285 fakeFile1.delete(); 286 fakeFile2.delete(); 287 } 288 289 292 public void testLastFile() 293 throws IOException , DatabaseException { 294 295 296 String [] jeFiles = fileManager.listFiles(FileManager.JE_SUFFIXES); 297 assertTrue(jeFiles.length == 0); 298 299 300 assertNull("No last file", fileManager.getLastFileNum()); 301 302 303 File fakeFile1 = new File (envHome, "108.cif"); 304 fakeFile1.createNewFile(); 305 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 306 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 307 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 308 309 assertEquals("Should have 2 as last file", 2L, 310 fileManager.getLastFileNum().longValue()); 311 fakeFile1.delete(); 312 } 313 314 317 public void testFollowingFile() 318 throws IOException , DatabaseException { 319 320 321 String [] jeFiles = fileManager.listFiles(FileManager.JE_SUFFIXES); 322 assertTrue(jeFiles.length == 0); 323 324 325 assertNull("No last file", fileManager.getFollowingFileNum(0, true)); 326 assertNull("No last file", fileManager.getFollowingFileNum(0, false)); 327 assertNull("No last file", fileManager.getFollowingFileNum(1, true)); 328 assertNull("No last file", fileManager.getFollowingFileNum(-1, false)); 329 330 331 File okFile1 = new File (envHome, "00000001.jdb"); 332 okFile1.createNewFile(); 333 334 File fakeFile3 = new File (envHome, "003.jdb"); 335 fakeFile3.createNewFile(); 336 337 File okFile6 = new File (envHome, "00000006.jdb"); 338 okFile6.createNewFile(); 339 340 File okFile9 = new File (envHome, "00000009.jdb"); 341 okFile9.createNewFile(); 342 343 344 345 assertEquals("Should get 6 next", 6L, 346 fileManager.getFollowingFileNum(2, true).longValue()); 347 assertEquals("Should get 9 next, testing non-existent file", 9L, 348 fileManager.getFollowingFileNum(8, true).longValue()); 349 assertNull("Should get null next", 350 fileManager.getFollowingFileNum(9, true)); 351 assertNull("Should get null next", 352 fileManager.getFollowingFileNum(10, true)); 353 354 355 assertEquals("Should get 6 next, testing non-existent file", 6L, 356 fileManager.getFollowingFileNum(8, false).longValue()); 357 assertEquals("Should get 6 next", 6L, 358 fileManager.getFollowingFileNum(9, false).longValue()); 359 assertNull("Should get null next", 360 fileManager.getFollowingFileNum(1, false)); 361 assertNull("Should get null next", 362 fileManager.getFollowingFileNum(0, false)); 363 364 okFile1.delete(); 365 fakeFile3.delete(); 366 okFile6.delete(); 367 okFile9.delete(); 368 } 369 370 373 public void testBadHeader() 374 throws IOException , DatabaseException { 375 376 377 try { 378 FileManager test = 379 new FileManager(envImpl, new File ("xxyy"), true); 380 fail("expect creation of " + test + "to fail."); 381 } catch (LogException e) { 382 383 } 384 385 386 try { 387 FileManager test = 388 new FileManager(envImpl, new File ("xxyy"), false); 389 fail("expect creation of " + test + "to fail."); 390 } catch (DatabaseException e) { 391 392 } 393 394 395 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 396 397 byte [] badData = new byte[]{1,1}; 398 int headerSize = FileManager.firstLogEntryOffset(); 399 RandomAccessFile file0 = 400 new RandomAccessFile 401 (fileManager.getFullFileName(0, FileManager.JE_SUFFIX), 402 FileManager.FileMode.READWRITE_MODE.getModeValue()); 403 file0.write(badData); 404 file0.close(); 405 fileManager.clear(); 406 407 try { 408 FileHandle file0Handle = fileManager.getFileHandle(0L); 409 fail("expect to catch a checksum error"); 410 } catch (DbChecksumException e) { 411 } 412 } 413 414 public void testTruncatedHeader() 415 throws IOException , DatabaseException { 416 417 418 FileManagerTestUtils.createLogFile(fileManager, envImpl, FILE_SIZE); 419 420 421 RandomAccessFile file0 = 422 new RandomAccessFile 423 (fileManager.getFullFileName(0, FileManager.JE_SUFFIX), 424 FileManager.FileMode.READWRITE_MODE.getModeValue()); 425 file0.getChannel().truncate(FileManager.firstLogEntryOffset()/2); 426 file0.close(); 427 428 try { 429 fileManager.getFileHandle(0); 430 fail("Should see assertion"); 431 } catch (DatabaseException e) { 432 } 433 } 434 435 438 public void testCache() 439 throws Throwable { 440 441 try { 442 443 446 FileManagerTestUtils.createLogFile 447 (fileManager, envImpl, FILE_SIZE); 448 FileManagerTestUtils.createLogFile 449 (fileManager, envImpl, FILE_SIZE); 450 FileManagerTestUtils.createLogFile 451 (fileManager, envImpl, FILE_SIZE); 452 FileManagerTestUtils.createLogFile 453 (fileManager, envImpl, FILE_SIZE); 454 FileManagerTestUtils.createLogFile 455 (fileManager, envImpl, FILE_SIZE); 456 457 Long f0 = new Long (0L); 458 Long f1 = new Long (1L); 459 Long f2 = new Long (2L); 460 Long f3 = new Long (3L); 461 Long f4 = new Long (4L); 462 463 Set keySet = fileManager.getCacheKeys(); 464 assertEquals("should have 0 keys", 0, keySet.size()); 465 466 470 FileHandle f0Handle = fileManager.getFileHandle(0); 471 FileHandle f1Handle = fileManager.getFileHandle(1); 472 FileHandle f2Handle = fileManager.getFileHandle(2); 473 keySet = fileManager.getCacheKeys(); 474 assertEquals("should have 3 keys", 3, keySet.size()); 475 assertTrue(keySet.contains(f0)); 476 assertTrue(keySet.contains(f1)); 477 assertTrue(keySet.contains(f2)); 478 479 487 CachingThread otherThread = new CachingThread(fileManager, 3); 488 otherThread.start(); 489 otherThread.join(); 490 491 keySet = fileManager.getCacheKeys(); 492 assertEquals("should have 4 keys", 4, keySet.size()); 493 assertTrue(keySet.contains(f0)); 494 assertTrue(keySet.contains(f1)); 495 assertTrue(keySet.contains(f2)); 496 assertTrue(keySet.contains(f3)); 497 498 502 f0Handle.release(); 503 f1Handle.release(); 504 f2Handle.release(); 505 FileHandle f4Handle = fileManager.getFileHandle(4); 506 keySet = fileManager.getCacheKeys(); 507 assertEquals("should have 4 keys", 4, keySet.size()); 508 assertTrue(keySet.contains(f4)); 509 510 f4Handle.release(); 511 512 513 fileManager.clear(); 514 assertEquals("should have 0 keys", 0, 515 fileManager.getCacheKeys().size()); 516 } catch (Throwable t) { 517 t.printStackTrace(); 518 throw t; 519 } 520 } 521 522 public void testFlipFile() 523 throws Throwable { 524 525 532 fileManager.clear(); 533 fileManager.close(); 534 fileManager = null; 535 536 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 537 envConfig.setAllowCreate(true); 538 envConfig.setTransactional(true); 539 Environment env = new Environment(envHome, envConfig); 540 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 541 FileManager fileManager = envImpl.getFileManager(); 542 543 DatabaseConfig dbConfig = new DatabaseConfig(); 544 dbConfig.setAllowCreate(true); 545 Database exampleDb = 546 env.openDatabase(null, "simpleDb", dbConfig); 547 548 assertEquals("Should have 0 as current file", 0L, 549 fileManager.getCurrentFileNum()); 550 long flipLsn = envImpl.forceLogFileFlip(); 551 assertEquals("LSN should be 1 post-flip", 1L, 552 DbLsn.getFileNumber(flipLsn)); 553 DatabaseEntry key = new DatabaseEntry(); 554 DatabaseEntry data = new DatabaseEntry(); 555 key.setData("key".getBytes()); 556 data.setData("data".getBytes()); 557 exampleDb.put(null, key, data); 558 assertEquals("Should have 1 as last file", 1L, 559 fileManager.getCurrentFileNum()); 560 exampleDb.close(); 561 env.close(); 562 } 563 564 class CachingThread extends Thread { 565 private FileManager fileManager; 566 private long fileNum; 567 568 private FileHandle handle; 569 570 CachingThread(FileManager fileManager, long fileNum) { 571 this.fileManager = fileManager; 572 this.fileNum = fileNum; 573 } 574 575 public void run() { 576 try { 577 handle = fileManager.getFileHandle(fileNum); 578 handle.release(); 579 } catch (Exception e) { 580 fail(e.getMessage()); 581 } 582 } 583 584 FileHandle getHandle() { 585 return handle; 586 } 587 } 588 } 589 | Popular Tags |