/** * Get a clean page and provision it for the specified ledger and firstEntry within the ledger * * @param ledgerId Ledger id * @param firstEntry Id of the first entry in the page * @returns LedgerEntryPage if present */ LedgerEntryPage grabCleanPage(long ledgerId, long firstEntry) { LedgerEntryPage lep = null; while (lruCleanPageMap.size() > 0) { lep = null; synchronized (lruCleanPageMap) { Iterator<Map.Entry<EntryKey, LedgerEntryPage>> iterator = lruCleanPageMap.entrySet().iterator(); Map.Entry<EntryKey, LedgerEntryPage> entry = null; while (iterator.hasNext()) { entry = iterator.next(); iterator.remove(); if (entry.getValue().isClean() && !entry.getValue().inUse()) { lep = entry.getValue(); break; } } if (null == lep) { LOG.debug("Did not find eligible page in the first pass"); return null; } } // We found a candidate page, lets see if we can reclaim it before its re-used ConcurrentMap<Long, LedgerEntryPage> pageMap = pages.get(lep.getLedger()); // Remove from map only if nothing has changed since we checked this lep. // Its possible for the ledger to have been deleted or the page to have already // been reclaimed. The page map is the definitive source of information, if anything // has changed we should leave this page along and continue iterating to find // another suitable page. if ((null != pageMap) && (pageMap.remove(lep.getFirstEntry(), lep))) { if (!lep.isClean()) { // Someone wrote to this page while we were reclaiming it. pageMap.put(lep.getFirstEntry(), lep); lep = null; } else { // Do some bookkeeping on the page table pages.remove(lep.getLedger(), EMPTY_PAGE_MAP); // We can now safely reset this lep and return it. lep.usePage(); lep.zeroPage(); lep.setLedgerAndFirstEntry(ledgerId, firstEntry); return lep; } } else { lep = null; } } return lep; }
/** * Add a LedgerEntryPage to the page map * * @param lep Ledger Entry Page object */ private LedgerEntryPage putPage(LedgerEntryPage lep) { // Do a get here to avoid too many new ConcurrentHashMaps() as putIntoTable is called // frequently. ConcurrentMap<Long, LedgerEntryPage> map = pages.get(lep.getLedger()); if (null == map) { ConcurrentMap<Long, LedgerEntryPage> mapToPut = new ConcurrentHashMap<Long, LedgerEntryPage>(); map = pages.putIfAbsent(lep.getLedger(), mapToPut); if (null == map) { map = mapToPut; } } LedgerEntryPage oldPage = map.putIfAbsent(lep.getFirstEntry(), lep); if (null == oldPage) { oldPage = lep; // Also include this in the clean page map if it qualifies. // Note: This is done for symmetry and correctness, however it should never // get exercised since we shouldn't attempt a put without the page being in use addToCleanPagesList(lep); } return oldPage; }
void updatePage(LedgerEntryPage lep) throws IOException { if (!lep.isClean()) { throw new IOException("Trying to update a dirty page"); } FileInfo fi = null; try { fi = getFileInfo(lep.getLedger(), null); long pos = lep.getFirstEntryPosition(); if (pos >= fi.size()) { lep.zeroPage(); } else { lep.readPage(fi); } } finally { if (fi != null) { fi.release(); } } }