Search in sources :

Example 1 with TxState

use of org.apache.ignite.internal.tx.TxState in project ignite-3 by apache.

the class TxManagerImpl method readLock.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Void> readLock(IgniteUuid lockId, ByteBuffer keyData, Timestamp ts) {
    TxState state = state(ts);
    if (state != null && state != TxState.PENDING) {
        return failedFuture(new TransactionException("The operation is attempted for completed transaction"));
    }
    LockKey key = new LockKey(lockId, keyData);
    return lockManager.tryAcquireShared(key, ts).thenAccept(ignored -> recordLock(key, ts, Boolean.TRUE));
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) TxState(org.apache.ignite.internal.tx.TxState)

Example 2 with TxState

use of org.apache.ignite.internal.tx.TxState in project ignite-3 by apache.

the class TxManagerImpl method writeLock.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Void> writeLock(IgniteUuid lockId, ByteBuffer keyData, Timestamp ts) {
    // TODO IGNITE-15933 process tx messages in striped fasion to avoid races. But locks can be acquired from any thread !
    TxState state = state(ts);
    if (state != null && state != TxState.PENDING) {
        return failedFuture(new TransactionException("The operation is attempted for completed transaction"));
    }
    // Should rollback tx on lock error.
    LockKey key = new LockKey(lockId, keyData);
    return lockManager.tryAcquire(key, ts).thenAccept(ignored -> recordLock(key, ts, Boolean.FALSE));
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) TxState(org.apache.ignite.internal.tx.TxState)

Example 3 with TxState

use of org.apache.ignite.internal.tx.TxState in project ignite-3 by apache.

the class VersionedRowStore method resolve.

/**
 * Resolves a multi-versioned value depending on a viewer's timestamp.
 *
 * @param val        The value.
 * @param timestamp  The timestamp
 * @return New and old rows pair.
 * @see #versionedRow
 */
private Pair<BinaryRow, BinaryRow> resolve(Value val, Timestamp timestamp) {
    if (val.timestamp == null) {
        // New or after reset.
        assert val.oldRow == null : val;
        return new Pair<>(val.newRow, null);
    }
    // Checks "inTx" condition. Will be false if this is a first transactional op.
    if (val.timestamp.equals(timestamp)) {
        return new Pair<>(val.newRow, val.oldRow);
    }
    TxState state = txManager.state(val.timestamp);
    BinaryRow cur;
    if (state == TxState.ABORTED) {
        // Was aborted and had written a temp value.
        cur = val.oldRow;
    } else {
        cur = val.newRow;
    }
    return new Pair<>(cur, cur);
}
Also used : TxState(org.apache.ignite.internal.tx.TxState) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Pair(org.apache.ignite.internal.util.Pair)

Example 4 with TxState

use of org.apache.ignite.internal.tx.TxState in project ignite-3 by apache.

the class PartitionListener method tryEnlistIntoTransaction.

/**
 * Attempts to enlist a command into a transaction.
 *
 * @param command The command.
 * @param clo     The closure.
 * @return {@code true} if a command is compatible with a transaction state or a command is not transactional.
 */
private boolean tryEnlistIntoTransaction(Command command, CommandClosure<?> clo) {
    if (command instanceof TransactionalCommand) {
        Timestamp ts = ((TransactionalCommand) command).getTimestamp();
        TxState state = txManager.getOrCreateTransaction(ts);
        if (state != null && state != TxState.PENDING) {
            clo.result(new TransactionException(format("Failed to enlist a key into a transaction, state={}", state)));
            return false;
        }
    }
    return true;
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) TxState(org.apache.ignite.internal.tx.TxState) Timestamp(org.apache.ignite.internal.tx.Timestamp) TransactionalCommand(org.apache.ignite.internal.table.distributed.command.TransactionalCommand)

Aggregations

TxState (org.apache.ignite.internal.tx.TxState)4 TransactionException (org.apache.ignite.tx.TransactionException)3 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)1 TransactionalCommand (org.apache.ignite.internal.table.distributed.command.TransactionalCommand)1 Timestamp (org.apache.ignite.internal.tx.Timestamp)1 Pair (org.apache.ignite.internal.util.Pair)1