Search in sources :

Example 6 with CachedFileInfo

use of org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo in project bookkeeper by apache.

the class IndexPersistenceMgrTest method testGetFileInfoWriteBeforeRead.

@Test
public void testGetFileInfoWriteBeforeRead() throws Exception {
    IndexPersistenceMgr indexPersistenceMgr = null;
    try {
        indexPersistenceMgr = createIndexPersistenceManager(1);
        CachedFileInfo writeFileInfo = indexPersistenceMgr.getFileInfo(lid, masterKey);
        assertEquals(2, writeFileInfo.getRefCount());
        assertEquals(1, indexPersistenceMgr.writeFileInfoCache.size());
        assertEquals(0, indexPersistenceMgr.readFileInfoCache.size());
        writeFileInfo.release();
        CachedFileInfo readFileInfo = indexPersistenceMgr.getFileInfo(lid, null);
        assertEquals(3, readFileInfo.getRefCount());
        assertEquals(1, indexPersistenceMgr.writeFileInfoCache.size());
        assertEquals(1, indexPersistenceMgr.readFileInfoCache.size());
        readFileInfo.release();
        assertEquals(2, writeFileInfo.getRefCount());
        assertEquals(2, readFileInfo.getRefCount());
    } finally {
        if (null != indexPersistenceMgr) {
            indexPersistenceMgr.close();
        }
    }
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Test(org.junit.Test)

Example 7 with CachedFileInfo

use of org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo in project bookkeeper by apache.

the class IndexPersistenceMgrTest method testGetFileInfoReadBeforeWrite.

@Test
public void testGetFileInfoReadBeforeWrite() throws Exception {
    IndexPersistenceMgr indexPersistenceMgr = null;
    try {
        indexPersistenceMgr = createIndexPersistenceManager(1);
        // get the file info for read
        try {
            indexPersistenceMgr.getFileInfo(lid, null);
            fail("Should fail get file info for reading if the file doesn't exist");
        } catch (Bookie.NoLedgerException nle) {
        // exepcted
        }
        assertEquals(0, indexPersistenceMgr.writeFileInfoCache.size());
        assertEquals(0, indexPersistenceMgr.readFileInfoCache.size());
        CachedFileInfo writeFileInfo = indexPersistenceMgr.getFileInfo(lid, masterKey);
        assertEquals(2, writeFileInfo.getRefCount());
        assertEquals(1, indexPersistenceMgr.writeFileInfoCache.size());
        assertEquals(0, indexPersistenceMgr.readFileInfoCache.size());
        writeFileInfo.release();
        assertEquals(1, writeFileInfo.getRefCount());
    } finally {
        if (null != indexPersistenceMgr) {
            indexPersistenceMgr.close();
        }
    }
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Test(org.junit.Test)

Example 8 with CachedFileInfo

use of org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo in project bookkeeper by apache.

the class IndexPersistenceMgrTest method testEvictBeforeReleaseRace.

@Test
public void testEvictBeforeReleaseRace() throws Exception {
    IndexPersistenceMgr indexPersistenceMgr = null;
    Watcher<LastAddConfirmedUpdateNotification> watcher = notification -> notification.recycle();
    try {
        indexPersistenceMgr = createIndexPersistenceManager(1);
        indexPersistenceMgr.getFileInfo(1L, masterKey);
        indexPersistenceMgr.getFileInfo(2L, masterKey);
        indexPersistenceMgr.getFileInfo(3L, masterKey);
        indexPersistenceMgr.getFileInfo(4L, masterKey);
        CachedFileInfo fi = indexPersistenceMgr.getFileInfo(1L, masterKey);
        // trigger eviction
        indexPersistenceMgr.getFileInfo(2L, masterKey);
        indexPersistenceMgr.getFileInfo(3L, null);
        indexPersistenceMgr.getFileInfo(4L, null);
        Thread.sleep(1000);
        fi.setFenced();
        fi.release();
        assertTrue(indexPersistenceMgr.isFenced(1));
    } finally {
        if (null != indexPersistenceMgr) {
            indexPersistenceMgr.close();
        }
    }
}
Also used : NullStatsLogger(org.apache.bookkeeper.stats.NullStatsLogger) DiskChecker(org.apache.bookkeeper.util.DiskChecker) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) UTF_8(com.google.common.base.Charsets.UTF_8) File(java.io.File) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) Assert.assertNull(org.junit.Assert.assertNull) Watcher(org.apache.bookkeeper.common.util.Watcher) After(org.junit.After) Assert.fail(org.junit.Assert.fail) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Assert.assertEquals(org.junit.Assert.assertEquals) SnapshotMap(org.apache.bookkeeper.util.SnapshotMap) Before(org.junit.Before) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Test(org.junit.Test)

Example 9 with CachedFileInfo

use of org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo in project bookkeeper by apache.

the class LedgerCacheTest method testLedgerCacheFlushFailureOnDiskFull.

/**
 * Test Ledger Cache flush failure.
 */
@Test
public void testLedgerCacheFlushFailureOnDiskFull() throws Exception {
    File ledgerDir1 = createTempDir("bkTest", ".dir");
    File ledgerDir2 = createTempDir("bkTest", ".dir");
    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setLedgerDirNames(new String[] { ledgerDir1.getAbsolutePath(), ledgerDir2.getAbsolutePath() });
    Bookie bookie = new Bookie(conf);
    InterleavedLedgerStorage ledgerStorage = ((InterleavedLedgerStorage) bookie.ledgerStorage);
    LedgerCacheImpl ledgerCache = (LedgerCacheImpl) ledgerStorage.ledgerCache;
    // Create ledger index file
    ledgerStorage.setMasterKey(1, "key".getBytes());
    CachedFileInfo fileInfo = ledgerCache.getIndexPersistenceManager().getFileInfo(Long.valueOf(1), null);
    // Add entries
    ledgerStorage.addEntry(generateEntry(1, 1));
    ledgerStorage.addEntry(generateEntry(1, 2));
    ledgerStorage.flush();
    ledgerStorage.addEntry(generateEntry(1, 3));
    // add the dir to failed dirs
    bookie.getIndexDirsManager().addToFilledDirs(fileInfo.getLf().getParentFile().getParentFile().getParentFile());
    File before = fileInfo.getLf();
    // flush after disk is added as failed.
    ledgerStorage.flush();
    File after = fileInfo.getLf();
    assertFalse("After flush index file should be changed", before.equals(after));
    // Verify written entries
    Assert.assertEquals(generateEntry(1, 1), ledgerStorage.getEntry(1, 1));
    Assert.assertEquals(generateEntry(1, 2), ledgerStorage.getEntry(1, 2));
    Assert.assertEquals(generateEntry(1, 3), ledgerStorage.getEntry(1, 3));
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) File(java.io.File) Test(org.junit.Test)

Example 10 with CachedFileInfo

use of org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo in project bookkeeper by apache.

the class IndexPersistenceMgrTest method testReadFileInfoCacheEviction.

@Test
public void testReadFileInfoCacheEviction() throws Exception {
    IndexPersistenceMgr indexPersistenceMgr = null;
    try {
        indexPersistenceMgr = createIndexPersistenceManager(1);
        for (int i = 0; i < 3; i++) {
            CachedFileInfo fileInfo = indexPersistenceMgr.getFileInfo(lid + i, masterKey);
            // We need to make sure index file is created, otherwise the test case can be flaky
            fileInfo.checkOpen(true);
            fileInfo.release();
            // load into read cache also
            indexPersistenceMgr.getFileInfo(lid + i, null).release();
        }
        indexPersistenceMgr.getFileInfo(lid, masterKey);
        assertEquals(1, indexPersistenceMgr.writeFileInfoCache.size());
        assertEquals(2, indexPersistenceMgr.readFileInfoCache.size());
        // trigger file info eviction on read file info cache
        for (int i = 1; i <= 2; i++) {
            indexPersistenceMgr.getFileInfo(lid + i, null);
        }
        assertEquals(1, indexPersistenceMgr.writeFileInfoCache.size());
        assertEquals(2, indexPersistenceMgr.readFileInfoCache.size());
        CachedFileInfo fileInfo = indexPersistenceMgr.writeFileInfoCache.asMap().get(lid);
        assertNotNull(fileInfo);
        assertEquals(2, fileInfo.getRefCount());
        fileInfo = indexPersistenceMgr.writeFileInfoCache.asMap().get(lid + 1);
        assertNull(fileInfo);
        fileInfo = indexPersistenceMgr.writeFileInfoCache.asMap().get(lid + 2);
        assertNull(fileInfo);
        fileInfo = indexPersistenceMgr.readFileInfoCache.asMap().get(lid);
        assertNull(fileInfo);
        fileInfo = indexPersistenceMgr.readFileInfoCache.asMap().get(lid + 1);
        assertNotNull(fileInfo);
        assertEquals(2, fileInfo.getRefCount());
        fileInfo = indexPersistenceMgr.readFileInfoCache.asMap().get(lid + 2);
        assertNotNull(fileInfo);
        assertEquals(2, fileInfo.getRefCount());
    } finally {
        if (null != indexPersistenceMgr) {
            indexPersistenceMgr.close();
        }
    }
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) Test(org.junit.Test)

Aggregations

CachedFileInfo (org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo)17 Test (org.junit.Test)9 File (java.io.File)6 IOException (java.io.IOException)5 After (org.junit.After)4 Before (org.junit.Before)4 Cache (com.google.common.cache.Cache)3 CacheBuilder (com.google.common.cache.CacheBuilder)3 RemovalNotification (com.google.common.cache.RemovalNotification)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Random (java.util.Random)3 Set (java.util.Set)3 Callable (java.util.concurrent.Callable)3 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 Future (java.util.concurrent.Future)3