use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerTerminatedException in project incubator-pulsar by apache.
the class ManagedLedgerTerminationTest method terminateReopen.
@Test(timeOut = 20000)
public void terminateReopen() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
Position p0 = ledger.addEntry("entry-0".getBytes());
Position lastPosition = ledger.terminate();
assertEquals(lastPosition, p0);
ledger.close();
ledger = factory.open("my_test_ledger");
try {
ledger.addEntry("entry-1".getBytes());
fail("Should have thrown exception");
} catch (ManagedLedgerTerminatedException e) {
// Expected
}
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerTerminatedException in project incubator-pulsar by apache.
the class ManagedLedgerTerminationTest method terminateSimple.
@Test(timeOut = 20000)
public void terminateSimple() throws Exception {
ManagedLedger ledger = factory.open("my_test_ledger");
Position p0 = ledger.addEntry("entry-0".getBytes());
Position lastPosition = ledger.terminate();
assertEquals(lastPosition, p0);
try {
ledger.addEntry("entry-1".getBytes());
} catch (ManagedLedgerTerminatedException e) {
// Expected
}
}
use of org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerTerminatedException in project incubator-pulsar by apache.
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.Terminated) {
callback.addFailed(new ManagedLedgerTerminatedException("Managed ledger was already terminated"), 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, Collections.emptyMap());
}
} else {
checkArgument(state == State.LedgerOpened, "ledger=%s is not opened", state);
// 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();
}
}
Aggregations