Search in sources :

Example 1 with TransactionIdOutOfOrderException

use of com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException in project distributedlog by twitter.

the class BKLogWriteHandler method doStartLogSegment.

protected void doStartLogSegment(final long txId, final boolean bestEffort, final boolean allowMaxTxID, final Promise<BKLogSegmentWriter> promise) {
    // validate the tx id
    if ((txId < 0) || (!allowMaxTxID && (txId == DistributedLogConstants.MAX_TXID))) {
        FutureUtils.setException(promise, new IOException("Invalid Transaction Id " + txId));
        return;
    }
    if (this.sanityCheckTxnId) {
        long highestTxIdWritten = maxTxId.get();
        if (txId < highestTxIdWritten) {
            if (highestTxIdWritten == DistributedLogConstants.MAX_TXID) {
                LOG.error("We've already marked the stream as ended and attempting to start a new log segment");
                FutureUtils.setException(promise, new EndOfStreamException("Writing to a stream after it has been marked as completed"));
                return;
            } else {
                LOG.error("We've already seen TxId {} the max TXId is {}", txId, highestTxIdWritten);
                FutureUtils.setException(promise, new TransactionIdOutOfOrderException(txId, highestTxIdWritten));
                return;
            }
        }
    }
    try {
        ledgerAllocator.allocate();
    } catch (IOException e) {
        // failed to issue an allocation request
        failStartLogSegment(promise, bestEffort, e);
        return;
    }
    // start the transaction from zookeeper
    final ZKTransaction txn = new ZKTransaction(zooKeeperClient);
    // failpoint injected before creating ledger
    try {
        FailpointUtils.checkFailPoint(FailpointUtils.FailPointName.FP_StartLogSegmentBeforeLedgerCreate);
    } catch (IOException ioe) {
        failStartLogSegment(promise, bestEffort, ioe);
        return;
    }
    ledgerAllocator.tryObtain(txn, new Transaction.OpListener<LedgerHandle>() {

        @Override
        public void onCommit(LedgerHandle lh) {
        // no-op
        }

        @Override
        public void onAbort(Throwable t) {
        // no-op
        }
    }).addEventListener(new FutureEventListener<LedgerHandle>() {

        @Override
        public void onSuccess(LedgerHandle lh) {
            // try-obtain succeed
            createInprogressLogSegment(txn, txId, lh, bestEffort, promise);
        }

        @Override
        public void onFailure(Throwable cause) {
            failStartLogSegment(promise, bestEffort, cause);
        }
    });
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) EndOfStreamException(com.twitter.distributedlog.exceptions.EndOfStreamException) TransactionIdOutOfOrderException(com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException) IOException(java.io.IOException) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction)

Aggregations

EndOfStreamException (com.twitter.distributedlog.exceptions.EndOfStreamException)1 TransactionIdOutOfOrderException (com.twitter.distributedlog.exceptions.TransactionIdOutOfOrderException)1 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)1 IOException (java.io.IOException)1 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)1