예제 #1
0
  private LedgerEntryPage grabCleanPage(long ledger, long entry) throws IOException {
    if (entry % entriesPerPage != 0) {
      throw new IllegalArgumentException(entry + " is not a multiple of " + entriesPerPage);
    }

    while (true) {
      boolean canAllocate = false;
      if (pageCount.incrementAndGet() <= pageLimit) {
        canAllocate = true;
      } else {
        pageCount.decrementAndGet();
      }

      if (canAllocate) {
        LedgerEntryPage lep = new LedgerEntryPage(pageSize, entriesPerPage, pageMapAndList);
        lep.setLedgerAndFirstEntry(ledger, entry);
        lep.usePage();
        return lep;
      }

      LedgerEntryPage lep = pageMapAndList.grabCleanPage(ledger, entry);
      if (null != lep) {
        return lep;
      }
      LOG.info(
          "Could not grab a clean page for ledger {}, entry {}, force flushing dirty ledgers.",
          ledger,
          entry);
      flushOneOrMoreLedgers(false);
    }
  }
예제 #2
0
 /**
  * Grab ledger entry page whose first entry is <code>pageEntry</code>.
  *
  * <p>If the page doesn't existed before, we allocate a memory page. Otherwise, we grab a clean
  * page and read it from disk.
  *
  * @param ledger Ledger Id
  * @param pageEntry Start entry of this entry page.
  */
 private LedgerEntryPage grabLedgerEntryPage(long ledger, long pageEntry) throws IOException {
   LedgerEntryPage lep = grabCleanPage(ledger, pageEntry);
   try {
     // should get the up to date page from the persistence manager
     // before we put it into table otherwise we would put
     // an empty page in it
     indexPersistenceManager.updatePage(lep);
     LedgerEntryPage oldLep;
     if (lep != (oldLep = pageMapAndList.putPage(lep))) {
       lep.releasePage();
       // Decrement the page count because we couldn't put this lep in the page cache.
       pageCount.decrementAndGet();
       // Increment the use count of the old lep because this is unexpected
       oldLep.usePage();
       lep = oldLep;
     }
   } catch (IOException ie) {
     // if we grab a clean page, but failed to update the page
     // we are exhausting the count of ledger entry pages.
     // since this page will be never used, so we need to decrement
     // page count of ledger cache.
     lep.releasePage();
     pageCount.decrementAndGet();
     throw ie;
   }
   return lep;
 }
예제 #3
0
 LedgerEntryPage getLedgerEntryPage(Long ledger, Long firstEntry, boolean onlyDirty) {
   LedgerEntryPage lep = pageMapAndList.getPage(ledger, firstEntry);
   if (onlyDirty && null != lep && lep.isClean()) {
     return null;
   }
   if (null != lep) {
     lep.usePage();
   }
   return lep;
 }
예제 #4
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;
    }
예제 #5
0
 /**
  * Traverse the pages for a given ledger in memory and find the highest entry amongst these
  * pages
  *
  * @param ledgerId Ledger id
  * @returns last entry in the in memory pages
  */
 private long getLastEntryInMem(long ledgerId) {
   long lastEntry = 0;
   // Find the last entry in the cache
   ConcurrentMap<Long, LedgerEntryPage> map = pages.get(ledgerId);
   if (map != null) {
     for (LedgerEntryPage lep : map.values()) {
       if (lep.getMaxPossibleEntry() < lastEntry) {
         continue;
       }
       lep.usePage();
       long highest = lep.getLastEntry();
       if (highest > lastEntry) {
         lastEntry = highest;
       }
       lep.releasePage();
     }
   }
   return lastEntry;
 }