Search in sources :

Example 1 with AbstractTransactionalContext

use of org.corfudb.runtime.object.transactions.AbstractTransactionalContext in project CorfuDB by CorfuDB.

the class ObjectsView method TXAbort.

/**
     * Aborts a transaction on the current thread.
     * Modifications to objects in the current transactional
     * context will be discarded.
     */
public void TXAbort() {
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    if (context == null) {
        log.warn("Attempted to abort a transaction, but no transaction active!");
    } else {
        TxResolutionInfo txInfo = new TxResolutionInfo(context.getTransactionID(), context.getSnapshotTimestamp());
        context.abortTransaction(new TransactionAbortedException(txInfo, null, AbortCause.USER));
        TransactionalContext.removeContext();
    }
}
Also used : TxResolutionInfo(org.corfudb.protocols.wireprotocol.TxResolutionInfo) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException)

Example 2 with AbstractTransactionalContext

use of org.corfudb.runtime.object.transactions.AbstractTransactionalContext in project CorfuDB by CorfuDB.

the class ObjectsView method TXEnd.

/**
     * End a transaction on the current thread.
     *
     * @throws TransactionAbortedException If the transaction could not be executed successfully.
     *
     * @return The address of the transaction, if it commits.
     */
public long TXEnd() throws TransactionAbortedException {
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    if (context == null) {
        log.warn("Attempted to end a transaction, but no transaction active!");
        return AbstractTransactionalContext.UNCOMMITTED_ADDRESS;
    } else {
        // TODO remove this, doesn't belong here!
        long totalTime = System.currentTimeMillis() - context.getStartTime();
        log.trace("TXCommit[{}] time={} ms", context, totalTime);
        // TODO up to here
        try {
            return TransactionalContext.getCurrentContext().commitTransaction();
        } catch (TransactionAbortedException e) {
            TransactionalContext.getCurrentContext().abortTransaction(e);
            throw e;
        } catch (Exception e) {
            log.trace("TXCommit[{}] Exception {}", context, e);
            AbortCause abortCause;
            if (e instanceof NetworkException) {
                abortCause = AbortCause.NETWORK;
            } else {
                abortCause = AbortCause.UNDEFINED;
            }
            long snapshot_timestamp;
            try {
                snapshot_timestamp = context.getSnapshotTimestamp();
            } catch (NetworkException ne) {
                snapshot_timestamp = -1L;
            }
            TxResolutionInfo txInfo = new TxResolutionInfo(context.getTransactionID(), snapshot_timestamp);
            TransactionAbortedException tae = new TransactionAbortedException(txInfo, null, abortCause);
            context.abortTransaction(tae);
            throw tae;
        } finally {
            TransactionalContext.removeContext();
        }
    }
}
Also used : TxResolutionInfo(org.corfudb.protocols.wireprotocol.TxResolutionInfo) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext) AbortCause(org.corfudb.runtime.exceptions.AbortCause) NetworkException(org.corfudb.runtime.exceptions.NetworkException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException) NetworkException(org.corfudb.runtime.exceptions.NetworkException)

Example 3 with AbstractTransactionalContext

use of org.corfudb.runtime.object.transactions.AbstractTransactionalContext in project CorfuDB by CorfuDB.

the class CheckpointWriter method startCheckpoint.

/** Append a checkpoint START record to this object's stream.
     *
     *  Corfu client transaction management, if desired, is the
     *  caller's responsibility.
     *
     * @return Global log address of the START record.
     */
public long startCheckpoint() {
    startTime = LocalDateTime.now();
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    long txBeginGlobalAddress = context.getSnapshotTimestamp();
    this.mdKV.put(CheckpointEntry.CheckpointDictKey.START_TIME, startTime.toString());
    this.mdKV.put(CheckpointEntry.CheckpointDictKey.START_LOG_ADDRESS, Long.toString(txBeginGlobalAddress));
    ImmutableMap<CheckpointEntry.CheckpointDictKey, String> mdKV = ImmutableMap.copyOf(this.mdKV);
    CheckpointEntry cp = new CheckpointEntry(CheckpointEntry.CheckpointEntryType.START, author, checkpointID, mdKV, null);
    startAddress = sv.append(Collections.singleton(streamID), cp, null);
    postAppendFunc.accept(cp, startAddress);
    return startAddress;
}
Also used : CheckpointEntry(org.corfudb.protocols.logprotocol.CheckpointEntry) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext)

Example 4 with AbstractTransactionalContext

use of org.corfudb.runtime.object.transactions.AbstractTransactionalContext in project CorfuDB by CorfuDB.

the class CorfuCompileProxy method abortTransaction.

private void abortTransaction(Exception e) {
    long snapshot_timestamp;
    AbortCause abortCause;
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    if (e instanceof NetworkException) {
        // If a 'NetworkException' was received within a transactional context, an attempt to
        // 'getSnapshotTimestamp' will also fail (as it requests it to the Sequencer). A new NetworkException
        // would prevent the earliest to be propagated and encapsulated as a TransactionAbortedException.
        snapshot_timestamp = -1L;
        abortCause = AbortCause.NETWORK;
    } else {
        snapshot_timestamp = context.getSnapshotTimestamp();
        abortCause = AbortCause.UNDEFINED;
    }
    TxResolutionInfo txInfo = new TxResolutionInfo(context.getTransactionID(), snapshot_timestamp);
    TransactionAbortedException tae = new TransactionAbortedException(txInfo, null, abortCause);
    context.abortTransaction(tae);
    TransactionalContext.removeContext();
    throw tae;
}
Also used : TxResolutionInfo(org.corfudb.protocols.wireprotocol.TxResolutionInfo) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext) AbortCause(org.corfudb.runtime.exceptions.AbortCause) NetworkException(org.corfudb.runtime.exceptions.NetworkException) TransactionAbortedException(org.corfudb.runtime.exceptions.TransactionAbortedException)

Example 5 with AbstractTransactionalContext

use of org.corfudb.runtime.object.transactions.AbstractTransactionalContext in project CorfuDB by CorfuDB.

the class CheckpointWriter method startGlobalSnapshotTxn.

public static long startGlobalSnapshotTxn(CorfuRuntime rt) {
    TokenResponse tokenResponse = rt.getSequencerView().nextToken(Collections.EMPTY_SET, 0);
    long globalTail = tokenResponse.getToken().getTokenValue();
    rt.getObjectsView().TXBuild().setType(TransactionType.SNAPSHOT).setSnapshot(globalTail).begin();
    AbstractTransactionalContext context = TransactionalContext.getCurrentContext();
    return context.getSnapshotTimestamp();
}
Also used : TokenResponse(org.corfudb.protocols.wireprotocol.TokenResponse) AbstractTransactionalContext(org.corfudb.runtime.object.transactions.AbstractTransactionalContext)

Aggregations

AbstractTransactionalContext (org.corfudb.runtime.object.transactions.AbstractTransactionalContext)5 TxResolutionInfo (org.corfudb.protocols.wireprotocol.TxResolutionInfo)3 TransactionAbortedException (org.corfudb.runtime.exceptions.TransactionAbortedException)3 AbortCause (org.corfudb.runtime.exceptions.AbortCause)2 NetworkException (org.corfudb.runtime.exceptions.NetworkException)2 CheckpointEntry (org.corfudb.protocols.logprotocol.CheckpointEntry)1 TokenResponse (org.corfudb.protocols.wireprotocol.TokenResponse)1