Search in sources :

Example 1 with CachedFileInfo

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

the class IndexPersistenceMgr method handleLedgerEviction.

/**
 * When a ledger is evicted, we need to make sure there's no other thread
 * trying to get FileInfo for that ledger at the same time when we close
 * the FileInfo.
 */
private void handleLedgerEviction(RemovalNotification<Long, CachedFileInfo> notification) {
    CachedFileInfo fileInfo = notification.getValue();
    if (null == fileInfo || null == notification.getKey()) {
        return;
    }
    if (notification.wasEvicted()) {
        evictedLedgersCounter.inc();
    }
    fileInfo.release();
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo)

Example 2 with CachedFileInfo

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

the class IndexPersistenceMgr method flushLedgerEntries.

void flushLedgerEntries(long l, List<LedgerEntryPage> entries) throws IOException {
    CachedFileInfo fi = null;
    try {
        Collections.sort(entries, new Comparator<LedgerEntryPage>() {

            @Override
            public int compare(LedgerEntryPage o1, LedgerEntryPage o2) {
                return (int) (o1.getFirstEntry() - o2.getFirstEntry());
            }
        });
        int[] versions = new int[entries.size()];
        try {
            fi = getFileInfo(l, null);
        } catch (Bookie.NoLedgerException nle) {
            // ledger has been deleted
            LOG.info("No ledger {} found when flushing entries.", l);
            return;
        }
        // flush the header if necessary
        relocateIndexFileAndFlushHeader(l, fi);
        int start = 0;
        long lastOffset = -1;
        for (int i = 0; i < entries.size(); i++) {
            versions[i] = entries.get(i).getVersion();
            if (lastOffset != -1 && (entries.get(i).getFirstEntry() - lastOffset) != entriesPerPage) {
                // send up a sequential list
                int count = i - start;
                if (count == 0) {
                    LOG.warn("Count cannot possibly be zero!");
                }
                writeBuffers(l, entries, fi, start, count);
                start = i;
            }
            lastOffset = entries.get(i).getFirstEntry();
        }
        if (entries.size() - start == 0 && entries.size() != 0) {
            LOG.warn("Nothing to write, but there were entries!");
        }
        writeBuffers(l, entries, fi, start, entries.size() - start);
        for (int i = 0; i < entries.size(); i++) {
            LedgerEntryPage lep = entries.get(i);
            lep.setClean(versions[i]);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Flushed ledger {} with {} pages.", l, entries.size());
        }
    } finally {
        if (fi != null) {
            fi.release();
        }
    }
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo)

Example 3 with CachedFileInfo

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

the class IndexPersistenceMgr method setExplicitLac.

void setExplicitLac(long ledgerId, ByteBuf lac) throws IOException {
    CachedFileInfo fi = null;
    try {
        fi = getFileInfo(ledgerId, null);
        fi.setExplicitLac(lac);
        return;
    } finally {
        if (null != fi) {
            fi.release();
        }
    }
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo)

Example 4 with CachedFileInfo

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

the class IndexPersistenceMgr method flushLedgerHeader.

void flushLedgerHeader(long ledger) throws IOException {
    CachedFileInfo fi = null;
    try {
        fi = getFileInfo(ledger, null);
        relocateIndexFileAndFlushHeader(ledger, fi);
    } catch (Bookie.NoLedgerException nle) {
        // ledger has been deleted
        LOG.info("No ledger {} found when flushing header.", ledger);
        return;
    } finally {
        if (null != fi) {
            fi.release();
        }
    }
}
Also used : CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo)

Example 5 with CachedFileInfo

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

the class TestFileInfoBackingCache method testRefCountRace.

@Test
public void testRefCountRace() throws Exception {
    AtomicBoolean done = new AtomicBoolean(false);
    FileInfoBackingCache cache = new FileInfoBackingCache((ledgerId, createIfNotFound) -> {
        File f = new File(baseDir, String.valueOf(ledgerId));
        f.deleteOnExit();
        return f;
    });
    Iterable<Future<Set<CachedFileInfo>>> futures = IntStream.range(0, 2).mapToObj((i) -> {
        Callable<Set<CachedFileInfo>> c = () -> {
            Set<CachedFileInfo> allFileInfos = new HashSet<>();
            while (!done.get()) {
                CachedFileInfo fi = cache.loadFileInfo(1, masterKey);
                Assert.assertFalse(fi.isClosed());
                allFileInfos.add(fi);
                fi.release();
            }
            return allFileInfos;
        };
        return executor.submit(c);
    }).collect(Collectors.toList());
    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
    done.set(true);
    // ensure all threads are finished operating on cache, before checking any
    for (Future<Set<CachedFileInfo>> f : futures) {
        f.get();
    }
    for (Future<Set<CachedFileInfo>> f : futures) {
        for (CachedFileInfo fi : f.get()) {
            Assert.assertTrue(fi.isClosed());
            Assert.assertEquals(FileInfoBackingCache.DEAD_REF, fi.getRefCount());
        }
    }
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) IntStream(java.util.stream.IntStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) After(org.junit.After) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) RemovalNotification(com.google.common.cache.RemovalNotification) LongStream(java.util.stream.LongStream) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) Assert(org.junit.Assert) Collections(java.util.Collections) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CachedFileInfo(org.apache.bookkeeper.bookie.FileInfoBackingCache.CachedFileInfo) HashSet(java.util.HashSet) Set(java.util.Set) Future(java.util.concurrent.Future) File(java.io.File) Callable(java.util.concurrent.Callable) HashSet(java.util.HashSet) 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