Esempio n. 1
0
    /**
     * 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;
    }
Esempio n. 2
0
 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();
     }
   }
 }