Java Code Examples for org.apache.bookkeeper.client.LedgerHandle#getLastAddConfirmed()

The following examples show how to use org.apache.bookkeeper.client.LedgerHandle#getLastAddConfirmed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BookKeeperEditLogInputStream.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Construct BookKeeper edit log input stream. 
 * Starts reading from firstBookKeeperEntry. This allows the stream
 * to take a shortcut during recovery, as it doesn't have to read
 * every edit log transaction to find out what the last one is.
 */
BookKeeperEditLogInputStream(LedgerHandle lh, EditLogLedgerMetadata metadata,
                             long firstBookKeeperEntry) 
    throws IOException {
  this.lh = lh;
  this.firstTxId = metadata.getFirstTxId();
  this.lastTxId = metadata.getLastTxId();
  this.logVersion = metadata.getDataLayoutVersion();
  this.inProgress = metadata.isInProgress();

  if (firstBookKeeperEntry < 0
      || firstBookKeeperEntry > lh.getLastAddConfirmed()) {
    throw new IOException("Invalid first bk entry to read: "
        + firstBookKeeperEntry + ", LAC: " + lh.getLastAddConfirmed());
  }
  BufferedInputStream bin = new BufferedInputStream(
      new LedgerInputStream(lh, firstBookKeeperEntry));
  tracker = new FSEditLogLoader.PositionTrackingInputStream(bin);
  DataInputStream in = new DataInputStream(tracker);

  reader = new FSEditLogOp.Reader(in, tracker, logVersion);
}
 
Example 2
Source File: BookKeeperEditLogInputStream.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Construct BookKeeper edit log input stream. 
 * Starts reading from firstBookKeeperEntry. This allows the stream
 * to take a shortcut during recovery, as it doesn't have to read
 * every edit log transaction to find out what the last one is.
 */
BookKeeperEditLogInputStream(LedgerHandle lh, EditLogLedgerMetadata metadata,
                             long firstBookKeeperEntry) 
    throws IOException {
  this.lh = lh;
  this.firstTxId = metadata.getFirstTxId();
  this.lastTxId = metadata.getLastTxId();
  this.logVersion = metadata.getDataLayoutVersion();
  this.inProgress = metadata.isInProgress();

  if (firstBookKeeperEntry < 0
      || firstBookKeeperEntry > lh.getLastAddConfirmed()) {
    throw new IOException("Invalid first bk entry to read: "
        + firstBookKeeperEntry + ", LAC: " + lh.getLastAddConfirmed());
  }
  BufferedInputStream bin = new BufferedInputStream(
      new LedgerInputStream(lh, firstBookKeeperEntry));
  tracker = new FSEditLogLoader.PositionTrackingInputStream(bin);
  DataInputStream in = new DataInputStream(tracker);

  reader = new FSEditLogOp.Reader(in, tracker, logVersion);
}
 
Example 3
Source File: LedgerStreamReader.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public LedgerStreamReader(LedgerHandle lh,
                          ReadEntryListener readEntryListener,
                          int concurrency) {
    this.lh = lh;
    this.lac = lh.getLastAddConfirmed();
    this.readEntryListener = readEntryListener;
    this.concurrency = concurrency;
    for (int i = 0; i < concurrency; i++) {
        long entryId = nextReadEntry.getAndIncrement();
        if (entryId > lac) {
            break;
        }
        PendingReadRequest request = new PendingReadRequest(entryId);
        pendingReads.add(request);
        request.read();
    }
    if (pendingReads.isEmpty()) {
        done.countDown();
    }
}
 
Example 4
Source File: LedgerStreamReader.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public LedgerStreamReader(LedgerHandle lh,
                          ReadEntryListener readEntryListener,
                          int concurrency) {
    this.lh = lh;
    this.lac = lh.getLastAddConfirmed();
    this.readEntryListener = readEntryListener;
    this.concurrency = concurrency;
    for (int i = 0; i < concurrency; i++) {
        long entryId = nextReadEntry.getAndIncrement();
        if (entryId > lac) {
            break;
        }
        PendingReadRequest request = new PendingReadRequest(entryId);
        pendingReads.add(request);
        request.read();
    }
    if (pendingReads.isEmpty()) {
        done.countDown();
    }
}
 
Example 5
Source File: BookKeeperEditLogInputStream.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Construct ledger input stream
 * @param lh the ledger handle to read from
 * @param firstBookKeeperEntry ledger entry to start reading from
 */
LedgerInputStream(LedgerHandle lh, long firstBookKeeperEntry) 
    throws IOException {
  this.lh = lh;
  readEntries = firstBookKeeperEntry;

  maxEntry = lh.getLastAddConfirmed();
}
 
Example 6
Source File: BookKeeperEditLogInputStream.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Construct ledger input stream
 * @param lh the ledger handle to read from
 * @param firstBookKeeperEntry ledger entry to start reading from
 */
LedgerInputStream(LedgerHandle lh, long firstBookKeeperEntry) 
    throws IOException {
  this.lh = lh;
  readEntries = firstBookKeeperEntry;

  maxEntry = lh.getLastAddConfirmed();
}
 
Example 7
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void repairLogSegment(BookKeeperAdmin bkAdmin,
                                MetadataUpdater metadataUpdater,
                                LogSegmentMetadata segment) throws Exception {
    if (segment.isInProgress()) {
        System.out.println("Skip inprogress log segment " + segment);
        return;
    }
    LedgerHandle lh = bkAdmin.openLedger(segment.getLogSegmentId());
    long lac = lh.getLastAddConfirmed();
    Enumeration<LedgerEntry> entries = lh.readEntries(lac, lac);
    if (!entries.hasMoreElements()) {
        throw new IOException("Entry " + lac + " isn't found for " + segment);
    }
    LedgerEntry lastEntry = entries.nextElement();
    Entry.Reader reader = Entry.newBuilder()
            .setLogSegmentInfo(segment.getLogSegmentSequenceNumber(), segment.getStartSequenceId())
            .setEntryId(lastEntry.getEntryId())
            .setEnvelopeEntry(LogSegmentMetadata.supportsEnvelopedEntries(segment.getVersion()))
            .setEntry(lastEntry.getEntryBuffer())
            .buildReader();
    lastEntry.getEntryBuffer().release();
    LogRecordWithDLSN record = reader.nextRecord();
    LogRecordWithDLSN lastRecord = null;
    while (null != record) {
        lastRecord = record;
        record = reader.nextRecord();
    }
    if (null == lastRecord) {
        throw new IOException("No record found in entry " + lac + " for " + segment);
    }
    System.out.println("Updating last record for " + segment + " to " + lastRecord);
    if (!IOUtils.confirmPrompt("Do you want to make this change (Y/N): ")) {
        return;
    }
    metadataUpdater.updateLastRecord(segment, lastRecord);
}
 
Example 8
Source File: DistributedLogTool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void repairLogSegment(BookKeeperAdmin bkAdmin,
                                MetadataUpdater metadataUpdater,
                                LogSegmentMetadata segment) throws Exception {
    if (segment.isInProgress()) {
        System.out.println("Skip inprogress log segment " + segment);
        return;
    }
    LedgerHandle lh = bkAdmin.openLedger(segment.getLedgerId(), true);
    long lac = lh.getLastAddConfirmed();
    Enumeration<LedgerEntry> entries = lh.readEntries(lac, lac);
    if (!entries.hasMoreElements()) {
        throw new IOException("Entry " + lac + " isn't found for " + segment);
    }
    LedgerEntry lastEntry = entries.nextElement();
    Entry.Reader reader = Entry.newBuilder()
            .setLogSegmentInfo(segment.getLogSegmentSequenceNumber(), segment.getStartSequenceId())
            .setEntryId(lastEntry.getEntryId())
            .setEnvelopeEntry(LogSegmentMetadata.supportsEnvelopedEntries(segment.getVersion()))
            .setInputStream(lastEntry.getEntryInputStream())
            .buildReader();
    LogRecordWithDLSN record = reader.nextRecord();
    LogRecordWithDLSN lastRecord = null;
    while (null != record) {
        lastRecord = record;
        record = reader.nextRecord();
    }
    if (null == lastRecord) {
        throw new IOException("No record found in entry " + lac + " for " + segment);
    }
    System.out.println("Updating last record for " + segment + " to " + lastRecord);
    if (!IOUtils.confirmPrompt("Do you want to make this change (Y/N): ")) {
        return;
    }
    metadataUpdater.updateLastRecord(segment, lastRecord);
}
 
Example 9
Source File: ManagedCursorImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
boolean shouldCloseLedger(LedgerHandle lh) {
    long now = clock.millis();
    if ((lh.getLastAddConfirmed() >= config.getMetadataMaxEntriesPerLedger()
            || lastLedgerSwitchTimestamp < (now - config.getLedgerRolloverTimeout() * 1000))
            && (STATE_UPDATER.get(this) != State.Closed && STATE_UPDATER.get(this) != State.Closing)) {
        // It's safe to modify the timestamp since this method will be only called from a callback, implying that
        // calls will be serialized on one single thread
        lastLedgerSwitchTimestamp = now;
        return true;
    } else {
        return false;
    }
}
 
Example 10
Source File: BookKeeperJournalManager.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Find the id of the last edit log transaction writen to a edit log
 * ledger.
 */
private long recoverLastTxId(EditLogLedgerMetadata l, boolean fence)
    throws IOException, SegmentEmptyException {
  LedgerHandle lh = null;
  try {
    if (fence) {
      lh = bkc.openLedger(l.getLedgerId(),
                          BookKeeper.DigestType.MAC,
                          digestpw.getBytes(Charsets.UTF_8));
    } else {
      lh = bkc.openLedgerNoRecovery(l.getLedgerId(),
                                    BookKeeper.DigestType.MAC,
                                    digestpw.getBytes(Charsets.UTF_8));
    }
  } catch (BKException bke) {
    throw new IOException("Exception opening ledger for " + l, bke);
  } catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
    throw new IOException("Interrupted opening ledger for " + l, ie);
  }

  BookKeeperEditLogInputStream in = null;

  try {
    long lastAddConfirmed = lh.getLastAddConfirmed();
    if (lastAddConfirmed == -1) {
      throw new SegmentEmptyException();
    }

    in = new BookKeeperEditLogInputStream(lh, l, lastAddConfirmed);

    long endTxId = HdfsConstants.INVALID_TXID;
    FSEditLogOp op = in.readOp();
    while (op != null) {
      if (endTxId == HdfsConstants.INVALID_TXID
          || op.getTransactionId() == endTxId+1) {
        endTxId = op.getTransactionId();
      }
      op = in.readOp();
    }
    return endTxId;
  } finally {
    if (in != null) {
      in.close();
    }
  }
}
 
Example 11
Source File: BookKeeperJournalManager.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Find the id of the last edit log transaction writen to a edit log
 * ledger.
 */
private long recoverLastTxId(EditLogLedgerMetadata l, boolean fence)
    throws IOException, SegmentEmptyException {
  LedgerHandle lh = null;
  try {
    if (fence) {
      lh = bkc.openLedger(l.getLedgerId(),
                          BookKeeper.DigestType.MAC,
                          digestpw.getBytes(Charsets.UTF_8));
    } else {
      lh = bkc.openLedgerNoRecovery(l.getLedgerId(),
                                    BookKeeper.DigestType.MAC,
                                    digestpw.getBytes(Charsets.UTF_8));
    }
  } catch (BKException bke) {
    throw new IOException("Exception opening ledger for " + l, bke);
  } catch (InterruptedException ie) {
    Thread.currentThread().interrupt();
    throw new IOException("Interrupted opening ledger for " + l, ie);
  }

  BookKeeperEditLogInputStream in = null;

  try {
    long lastAddConfirmed = lh.getLastAddConfirmed();
    if (lastAddConfirmed == -1) {
      throw new SegmentEmptyException();
    }

    in = new BookKeeperEditLogInputStream(lh, l, lastAddConfirmed);

    long endTxId = HdfsConstants.INVALID_TXID;
    FSEditLogOp op = in.readOp();
    while (op != null) {
      if (endTxId == HdfsConstants.INVALID_TXID
          || op.getTransactionId() == endTxId+1) {
        endTxId = op.getTransactionId();
      }
      op = in.readOp();
    }
    return endTxId;
  } finally {
    if (in != null) {
      in.close();
    }
  }
}
 
Example 12
Source File: ManagedLedgerImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
synchronized void ledgerClosed(final LedgerHandle lh) {
    final State state = STATE_UPDATER.get(this);
    LedgerHandle currentLedger = this.currentLedger;
    if (currentLedger == lh && (state == State.ClosingLedger || state == State.LedgerOpened)) {
        STATE_UPDATER.set(this, State.ClosedLedger);
    } else if (state == State.Closed) {
        // The managed ledger was closed during the write operation
        clearPendingAddEntries(new ManagedLedgerAlreadyClosedException("Managed ledger was already closed"));
        return;
    } else {
        // In case we get multiple write errors for different outstanding write request, we should close the ledger
        // just once
        return;
    }

    long entriesInLedger = lh.getLastAddConfirmed() + 1;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Ledger has been closed id={} entries={}", name, lh.getId(), entriesInLedger);
    }
    if (entriesInLedger > 0) {
        LedgerInfo info = LedgerInfo.newBuilder().setLedgerId(lh.getId()).setEntries(entriesInLedger)
                .setSize(lh.getLength()).setTimestamp(clock.millis()).build();
        ledgers.put(lh.getId(), info);
    } else {
        // The last ledger was empty, so we can discard it
        ledgers.remove(lh.getId());
        mbean.startDataLedgerDeleteOp();
        bookKeeper.asyncDeleteLedger(lh.getId(), (rc, ctx) -> {
            mbean.endDataLedgerDeleteOp();
            log.info("[{}] Delete complete for empty ledger {}. rc={}", name, lh.getId(), rc);
        }, null);
    }

    trimConsumedLedgersInBackground();

    maybeOffloadInBackground(NULL_OFFLOAD_PROMISE);

    if (!pendingAddEntries.isEmpty()) {
        // Need to create a new ledger to write pending entries
        log.info("[{}] Creating a new ledger", name);
        STATE_UPDATER.set(this, State.CreatingLedger);
        this.lastLedgerCreationInitiationTimestamp = System.currentTimeMillis();
        mbean.startDataLedgerCreateOp();
        asyncCreateLedger(bookKeeper, config, digestType, this, Collections.emptyMap());
    }
}
 
Example 13
Source File: ManagedCursorImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public long getCursorLedgerLastEntry() {
    LedgerHandle lh = cursorLedger;
    return lh != null ? lh.getLastAddConfirmed() : -1;
}