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();
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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());
}
}
}
Aggregations