static int getExceptionCode(Throwable t) { if (t instanceof BKException) { return ((BKException) t).getCode(); } else if (t.getCause() != null) { return getExceptionCode(t.getCause()); } else { return BKException.Code.UnexpectedConditionException; } }
}).whenCompleteAsync((res, exception) -> { if (exception != null) { cb.readComplete(PulsarMockBookKeeper.getExceptionCode(exception), PulsarMockLedgerHandle.this, null, ctx); } else { cb.readComplete(BKException.Code.OK, PulsarMockLedgerHandle.this, res, ctx);
@Override public void asyncAddEntry(final ByteBuf data, final AddCallback cb, final Object ctx) { data.retain(); bk.getProgrammedFailure().thenComposeAsync((res) -> { try { Thread.sleep(1); } catch (InterruptedException e) { } if (fenced) { return FutureUtils.exception(new BKException.BKLedgerFencedException()); } else { lastEntry = entries.size(); byte[] storedData = new byte[data.readableBytes()]; data.readBytes(storedData); entries.add(LedgerEntryImpl.create(ledgerId, lastEntry, storedData.length, Unpooled.wrappedBuffer(storedData))); return FutureUtils.value(lastEntry); } }, bk.executor).whenCompleteAsync((entryId, exception) -> { data.release(); if (exception != null) { fenced = true; cb.addComplete(PulsarMockBookKeeper.getExceptionCode(exception), PulsarMockLedgerHandle.this, INVALID_ENTRY_ID, ctx); } else { cb.addComplete(BKException.Code.OK, PulsarMockLedgerHandle.this, entryId, ctx); } }, bk.executor); }
@Override public void asyncCreateLedger(int ensSize, int writeQuorumSize, int ackQuorumSize, final DigestType digestType, final byte[] passwd, final CreateCallback cb, final Object ctx, Map<String, byte[]> properties) { getProgrammedFailure().thenComposeAsync((res) -> { try { long id = sequence.getAndIncrement(); log.info("Creating ledger {}", id); PulsarMockLedgerHandle lh = new PulsarMockLedgerHandle(PulsarMockBookKeeper.this, id, digestType, passwd); ledgers.put(id, lh); return FutureUtils.value(lh); } catch (Throwable t) { return FutureUtils.exception(t); } }, executor).whenCompleteAsync((lh, exception) -> { if (exception != null) { cb.createComplete(getExceptionCode(exception), null, ctx); } else { cb.createComplete(BKException.Code.OK, lh, ctx); } }, executor); }
@Override public void asyncDeleteLedger(long lId, DeleteCallback cb, Object ctx) { getProgrammedFailure().thenComposeAsync((res) -> { if (ledgers.containsKey(lId)) { ledgers.remove(lId); return FutureUtils.value(null); } else { return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException()); } }, executor).whenCompleteAsync((res, exception) -> { if (exception != null) { cb.deleteComplete(getExceptionCode(exception), ctx); } else { cb.deleteComplete(BKException.Code.OK, ctx); } }, executor); }
@Override public void asyncClose(CloseCallback cb, Object ctx) { bk.getProgrammedFailure().thenComposeAsync((res) -> { fenced = true; return FutureUtils.value(null); }, bk.executor).whenCompleteAsync((res, exception) -> { if (exception != null) { cb.closeComplete(PulsarMockBookKeeper.getExceptionCode(exception), null, ctx); } else { cb.closeComplete(BKException.Code.OK, this, ctx); } }, bk.executor); }
@Override public void asyncOpenLedger(long lId, DigestType digestType, byte[] passwd, OpenCallback cb, Object ctx) { getProgrammedFailure().thenComposeAsync((res) -> { PulsarMockLedgerHandle lh = ledgers.get(lId); if (lh == null) { return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException()); } else if (lh.digest != digestType) { return FutureUtils.exception(new BKException.BKDigestMatchException()); } else if (!Arrays.equals(lh.passwd, passwd)) { return FutureUtils.exception(new BKException.BKUnauthorizedAccessException()); } else { return FutureUtils.value(lh); } }, executor).whenCompleteAsync((ledger, exception) -> { if (exception != null) { cb.openComplete(getExceptionCode(exception), null, ctx); } else { cb.openComplete(BKException.Code.OK, ledger, ctx); } }, executor); }