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;
    }
  }