void checkProgrammedFail() throws BKException, InterruptedException { try { getProgrammedFailure().get(); } catch (ExecutionException ee) { if (ee.getCause() instanceof BKException) { throw (BKException)ee.getCause(); } else { throw new BKException.BKUnexpectedConditionException(); } } }
@Override public CompletableFuture<LedgerEntries> readAsync(long firstEntry, long lastEntry) { return bk.getProgrammedFailure().thenComposeAsync((res) -> { log.debug("readEntries: first={} last={} total={}", firstEntry, lastEntry, entries.size()); List<LedgerEntry> seq = new ArrayList<>(); long entryId = firstEntry; while (entryId <= lastEntry && entryId < entries.size()) { seq.add(entries.get((int) entryId++).duplicate()); } log.debug("Entries read: {}", seq); return FutureUtils.value(LedgerEntriesImpl.create(seq)); }); }
@Override public void asyncReadEntries(final long firstEntry, final long lastEntry, final ReadCallback cb, final Object ctx) { bk.getProgrammedFailure().thenComposeAsync((res) -> { log.debug("readEntries: first={} last={} total={}", firstEntry, lastEntry, entries.size()); final Queue<LedgerEntry> seq = new ArrayDeque<LedgerEntry>();
@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 CompletableFuture<ReadHandle> execute() { return getProgrammedFailure().thenCompose( (res) -> { if (!validate()) { return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException()); } PulsarMockLedgerHandle lh = ledgers.get(ledgerId); if (lh == null) { return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsException()); } else if (lh.digest != DigestType.fromApiDigestType(digestType)) { return FutureUtils.exception(new BKException.BKDigestMatchException()); } else if (!Arrays.equals(lh.passwd, password)) { return FutureUtils.exception(new BKException.BKUnauthorizedAccessException()); } else { return FutureUtils.value(new PulsarMockReadHandle(PulsarMockBookKeeper.this, ledgerId, lh.getLedgerMetadata(), lh.entries)); } }); } };
@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); }