Example #1
0
  /**
   * Fences a ledger. From this point on, clients will be unable to write to this ledger. Only
   * recoveryAddEntry will be able to add entries to the ledger. This method is idempotent. Once a
   * ledger is fenced, it can never be unfenced. Fencing a fenced ledger has no effect.
   */
  public SettableFuture<Boolean> fenceLedger(long ledgerId, byte[] masterKey)
      throws IOException, BookieException {
    LedgerDescriptor handle = handles.getHandle(ledgerId, masterKey);
    boolean success;
    synchronized (handle) {
      success = handle.setFenced();
    }
    if (success) {
      // fenced first time, we should add the key to journal ensure we can rebuild
      ByteBuffer bb = ByteBuffer.allocate(8 + 8);
      bb.putLong(ledgerId);
      bb.putLong(METAENTRY_ID_FENCE_KEY);
      bb.flip();

      FutureWriteCallback fwc = new FutureWriteCallback();
      LOG.debug("record fenced state for ledger {} in journal.", ledgerId);
      journal.logAddEntry(bb, fwc, null);
      return fwc.getResult();
    } else {
      // already fenced
      SettableFuture<Boolean> successFuture = SettableFuture.create();
      successFuture.set(true);
      return successFuture;
    }
  }
Example #2
0
 /**
  * Add entry to a ledger.
  *
  * @throws BookieException.LedgerFencedException if the ledger is fenced
  */
 public void addEntry(ByteBuffer entry, WriteCallback cb, Object ctx, byte[] masterKey)
     throws IOException, BookieException {
   long requestNanos = MathUtils.nowInNano();
   boolean success = false;
   try {
     LedgerDescriptor handle = getLedgerForEntry(entry, masterKey);
     synchronized (handle) {
       if (handle.isFenced()) {
         throw BookieException.create(BookieException.Code.LedgerFencedException);
       }
       addEntryInternal(handle, entry, cb, ctx);
     }
     success = true;
   } catch (NoWritableLedgerDirException e) {
     transitionToReadOnlyMode();
     throw new IOException(e);
   } finally {
     long elapsedMicros = MathUtils.elapsedMicroSec(requestNanos);
     if (success) {
       addEntryStats.registerSuccessfulEvent(elapsedMicros);
     } else {
       addEntryStats.registerFailedEvent(elapsedMicros);
     }
   }
 }
Example #3
0
  /** Add an entry to a ledger as specified by handle. */
  private void addEntryInternal(
      LedgerDescriptor handle, ByteBuffer entry, WriteCallback cb, Object ctx)
      throws IOException, BookieException {
    long ledgerId = handle.getLedgerId();
    entry.rewind();
    long entryId = handle.addEntry(entry);

    entry.rewind();
    LOG.trace("Adding {}@{}", entryId, ledgerId);
    journal.logAddEntry(entry, cb, ctx);
  }
Example #4
0
 public long readLastAddConfirmed(long ledgerId) throws IOException {
   long requestNanos = MathUtils.nowInNano();
   boolean success = false;
   try {
     LedgerDescriptor handle = handles.getReadOnlyHandle(ledgerId);
     long lac = handle.getLastAddConfirmed();
     success = true;
     return lac;
   } finally {
     long elapsedMicros = MathUtils.elapsedMicroSec(requestNanos);
     if (success) {
       readLastConfirmedStats.registerSuccessfulEvent(elapsedMicros);
     } else {
       readLastConfirmedStats.registerFailedEvent(elapsedMicros);
     }
   }
 }
Example #5
0
 public ByteBuffer readEntry(long ledgerId, long entryId) throws IOException, NoLedgerException {
   long requestNanos = MathUtils.nowInNano();
   boolean success = false;
   try {
     LedgerDescriptor handle = handles.getReadOnlyHandle(ledgerId);
     LOG.trace("Reading {}@{}", entryId, ledgerId);
     ByteBuffer data = handle.readEntry(entryId);
     success = true;
     return data;
   } finally {
     long elapsedMicros = MathUtils.elapsedMicroSec(requestNanos);
     if (success) {
       readEntryStats.registerSuccessfulEvent(elapsedMicros);
     } else {
       readEntryStats.registerFailedEvent(elapsedMicros);
     }
   }
 }
Example #6
0
 public Observable waitForLastAddConfirmedUpdate(
     long ledgerId, long previoisLAC, Observer observer) throws IOException {
   LedgerDescriptor handle = handles.getReadOnlyHandle(ledgerId);
   return handle.waitForLastAddConfirmedUpdate(previoisLAC, observer);
 }
Example #7
0
 protected void addEntryByLedgerId(long ledgerId, ByteBuffer entry)
     throws IOException, BookieException {
   byte[] key = ledgerStorage.readMasterKey(ledgerId);
   LedgerDescriptor handle = handles.getHandle(ledgerId, key);
   handle.addEntry(entry);
 }