bk.asyncOpenLedger(ledgerId, DigestType.CRC32, passwd, new SafeAsynBKCallback.OpenCallback() {
@Override
public void safeOpenComplete(int rc, LedgerHandle ledgerHandle, Object ctx) {
if (rc != BKException.Code.OK) {
BKException bke = BKException.create(rc);
logger.error("While acquiring topic: " + topic.toStringUtf8()
+ ", could not open unrecovered ledger: " + ledgerId, bke);
cb.operationFailed(ctx, new PubSubException.ServiceDownException(bke));
return;
}
final long numEntriesInLastLedger = ledgerHandle.getLastAddConfirmed() + 1;
if (numEntriesInLastLedger <= 0) {
// this was an empty ledger that someone created but
// couldn't write to, so just ignore it
logger.info("Pruning empty ledger: " + ledgerId + " for topic: " + topic.toStringUtf8());
closeLedger(ledgerHandle);
openNewTopicLedger(topic, expectedVersionOfLedgerNode, topicInfo,
expectedStartSeqId, false, cb, ctx);
return;
}
// we have to read the last entry of the ledger to find
// out the last seq-id
ledgerHandle.asyncReadEntries(numEntriesInLastLedger - 1, numEntriesInLastLedger - 1,
new SafeAsynBKCallback.ReadCallback() {
@Override
public void safeReadComplete(int rc, LedgerHandle lh, Enumeration<LedgerEntry> seq,
Object ctx) {
if (rc != BKException.Code.OK || !seq.hasMoreElements()) {
if (rc == BKException.Code.OK) {
// means that there is no entries read, provide a meaningful exception
rc = BKException.Code.NoSuchEntryException;
}
logger.info("Received error code {}", rc);
BKException bke = BKException.create(rc);
logger.error("While recovering ledger: " + ledgerId + " for topic: "
+ topic.toStringUtf8() + ", could not read last entry", bke);
cb.operationFailed(ctx, new PubSubException.ServiceDownException(bke));
return;
}