use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException in project pulsar by yahoo.
the class ManagedLedgerImpl method asyncAddEntry.
@Override
public synchronized void asyncAddEntry(ByteBuf buffer, AddEntryCallback callback, Object ctx) {
if (log.isDebugEnabled()) {
log.debug("[{}] asyncAddEntry size={} state={}", name, buffer.readableBytes(), state);
}
final State state = STATE_UPDATER.get(this);
if (state == State.Fenced) {
callback.addFailed(new ManagedLedgerFencedException(), ctx);
return;
} else if (state == State.Closed) {
callback.addFailed(new ManagedLedgerException("Managed ledger was already closed"), ctx);
return;
}
OpAddEntry addOperation = OpAddEntry.create(this, buffer, callback, ctx);
pendingAddEntries.add(addOperation);
if (state == State.ClosingLedger || state == State.CreatingLedger) {
// We are waiting for a new ledger to be created
if (log.isDebugEnabled()) {
log.debug("[{}] Queue addEntry request", name);
}
} else if (state == State.ClosedLedger) {
long now = System.currentTimeMillis();
if (now < lastLedgerCreationFailureTimestamp + WaitTimeAfterLedgerCreationFailureMs) {
// Deny the write request, since we haven't waited enough time since last attempt to create a new ledger
pendingAddEntries.remove(addOperation);
callback.addFailed(new ManagedLedgerException("Waiting for new ledger creation to complete"), ctx);
return;
}
// No ledger and no pending operations. Create a new ledger
if (log.isDebugEnabled()) {
log.debug("[{}] Creating a new ledger", name);
}
if (STATE_UPDATER.compareAndSet(this, State.ClosedLedger, State.CreatingLedger)) {
this.lastLedgerCreationInitiationTimestamp = System.nanoTime();
mbean.startDataLedgerCreateOp();
bookKeeper.asyncCreateLedger(config.getEnsembleSize(), config.getWriteQuorumSize(), config.getAckQuorumSize(), config.getDigestType(), config.getPassword(), this, ctx);
}
} else {
checkArgument(state == State.LedgerOpened);
// Write into lastLedger
addOperation.setLedger(currentLedger);
++currentLedgerEntries;
currentLedgerSize += buffer.readableBytes();
if (log.isDebugEnabled()) {
log.debug("[{}] Write into current ledger lh={} entries={}", name, currentLedger.getId(), currentLedgerEntries);
}
if (currentLedgerIsFull()) {
if (log.isDebugEnabled()) {
log.debug("[{}] Closing current ledger lh={}", name, currentLedger.getId());
}
// This entry will be the last added to current ledger
addOperation.setCloseWhenDone(true);
STATE_UPDATER.set(this, State.ClosingLedger);
}
addOperation.initiate();
}
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException in project pulsar by yahoo.
the class ManagedLedgerTest method fenceManagedLedger.
@Test(enabled = false)
public void fenceManagedLedger() throws Exception {
ManagedLedgerFactory factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ManagedLedger ledger1 = factory1.open("my_test_ledger");
ManagedCursor cursor1 = ledger1.openCursor("c1");
ledger1.addEntry("entry-1".getBytes(Encoding));
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ManagedLedger ledger2 = factory2.open("my_test_ledger");
ManagedCursor cursor2 = ledger2.openCursor("c1");
// At this point ledger1 must have been fenced
try {
ledger1.addEntry("entry-1".getBytes(Encoding));
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
try {
ledger1.addEntry("entry-2".getBytes(Encoding));
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
try {
cursor1.readEntries(10);
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
try {
ledger1.openCursor("new cursor");
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
ledger2.addEntry("entry-2".getBytes(Encoding));
assertEquals(cursor2.getNumberOfEntries(), 2);
factory1.shutdown();
factory2.shutdown();
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException in project incubator-pulsar by apache.
the class ManagedLedgerErrorsTest method recoverAfterZnodeVersionError.
@Test
public void recoverAfterZnodeVersionError() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger", new ManagedLedgerConfig().setMaxEntriesPerLedger(1));
zkc.failNow(Code.BADVERSION);
// First write will succeed
ledger.addEntry("test".getBytes());
try {
// This write will try to create a ledger and it will fail at it
ledger.addEntry("entry".getBytes());
fail("should fail");
} catch (BadVersionException e) {
// ok
}
try {
// At this point the ledger should be fenced for good
ledger.addEntry("entry".getBytes());
fail("should fail");
} catch (ManagedLedgerFencedException e) {
// ok
}
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException in project incubator-pulsar by apache.
the class ManagedLedgerTest method fenceManagedLedger.
@Test(enabled = false)
public void fenceManagedLedger() throws Exception {
ManagedLedgerFactory factory1 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ManagedLedger ledger1 = factory1.open("my_test_ledger");
ManagedCursor cursor1 = ledger1.openCursor("c1");
ledger1.addEntry("entry-1".getBytes(Encoding));
ManagedLedgerFactory factory2 = new ManagedLedgerFactoryImpl(bkc, bkc.getZkHandle());
ManagedLedger ledger2 = factory2.open("my_test_ledger");
ManagedCursor cursor2 = ledger2.openCursor("c1");
// At this point ledger1 must have been fenced
try {
ledger1.addEntry("entry-1".getBytes(Encoding));
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
try {
ledger1.addEntry("entry-2".getBytes(Encoding));
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
try {
cursor1.readEntries(10);
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
try {
ledger1.openCursor("new cursor");
fail("Expecting exception");
} catch (ManagedLedgerFencedException e) {
}
ledger2.addEntry("entry-2".getBytes(Encoding));
assertEquals(cursor2.getNumberOfEntries(), 2);
factory1.shutdown();
factory2.shutdown();
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerFencedException in project incubator-pulsar by apache.
the class ManagedLedgerImpl method asyncTerminate.
@Override
public synchronized void asyncTerminate(TerminateCallback callback, Object ctx) {
if (state == State.Fenced) {
callback.terminateFailed(new ManagedLedgerFencedException(), ctx);
return;
} else if (state == State.Terminated) {
if (log.isDebugEnabled()) {
log.debug("[{}] Ignoring request to terminate an already terminated managed ledger", name);
}
callback.terminateComplete(lastConfirmedEntry, ctx);
return;
}
log.info("[{}] Terminating managed ledger", name);
state = State.Terminated;
LedgerHandle lh = currentLedger;
if (log.isDebugEnabled()) {
log.debug("[{}] Closing current writing ledger {}", name, lh.getId());
}
mbean.startDataLedgerCloseOp();
lh.asyncClose((rc, lh1, ctx1) -> {
if (log.isDebugEnabled()) {
log.debug("[{}] Close complete for ledger {}: rc = {}", name, lh.getId(), rc);
}
mbean.endDataLedgerCloseOp();
if (rc != BKException.Code.OK) {
callback.terminateFailed(createManagedLedgerException(rc), ctx);
} else {
lastConfirmedEntry = new PositionImpl(lh.getId(), lh.getLastAddConfirmed());
// Store the new state in metadata
store.asyncUpdateLedgerIds(name, getManagedLedgerInfo(), ledgersStat, new MetaStoreCallback<Void>() {
@Override
public void operationComplete(Void result, Stat stat) {
ledgersStat = stat;
log.info("[{}] Terminated managed ledger at {}", name, lastConfirmedEntry);
callback.terminateComplete(lastConfirmedEntry, ctx);
}
@Override
public void operationFailed(MetaStoreException e) {
log.error("[{}] Failed to terminate managed ledger: {}", name, e.getMessage());
callback.terminateFailed(new ManagedLedgerException(e), ctx);
}
});
}
}, null);
}
Aggregations