Search in sources :

Example 1 with TransactionException

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

the class TxAbstractTest method testIncrement2.

/**
 * Tests if a lost update is not happening on concurrent increment.
 */
@Test
public void testIncrement2() throws TransactionException, InterruptedException {
    InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
    InternalTransaction tx2 = (InternalTransaction) igniteTransactions.begin();
    Tuple key = makeKey(1);
    Tuple val = makeValue(1, 100.);
    // Creates implicit transaction.
    accounts.recordView().upsert(null, val);
    var table = accounts.recordView();
    var table2 = accounts.recordView();
    // Read in tx
    double valTx = table.get(tx, key).doubleValue("balance");
    // Read in tx2
    double valTx2 = table2.get(tx2, key).doubleValue("balance");
    // Write in tx2 (should wait for read unlock in tx1)
    CompletableFuture<Void> fut = table2.upsertAsync(tx2, makeValue(1, valTx2 + 1));
    // Give some time to update lock queue TODO asch IGNITE-15928
    Thread.sleep(300);
    assertFalse(fut.isDone());
    CompletableFuture<Void> fut2 = fut.thenCompose(ret -> tx2.commitAsync());
    // Write in tx
    table.upsert(tx, makeValue(1, valTx + 1));
    tx.commit();
    Exception err = assertThrows(Exception.class, () -> fut2.get(5, TimeUnit.SECONDS));
    assertTrue(err.getMessage().contains("Failed to acquire a lock"), err.getMessage());
    assertEquals(101., accounts.recordView().get(null, key).doubleValue("balance"));
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) TransactionException(org.apache.ignite.tx.TransactionException) IgniteException(org.apache.ignite.lang.IgniteException) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 2 with TransactionException

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

the class TxAbstractTest method testPutAll.

@Test
public void testPutAll() throws TransactionException {
    igniteTransactions.runInTransaction(tx -> {
        accounts.recordView().upsertAll(tx, List.of(makeValue(1, 100.), makeValue(2, 200.)));
    });
    validateBalance(accounts.recordView().getAll(null, List.of(makeKey(2), makeKey(1))), 200., 100.);
    assertThrows(IgniteException.class, () -> igniteTransactions.runInTransaction(tx -> {
        accounts.recordView().upsertAll(tx, List.of(makeValue(3, 300.), makeValue(4, 400.)));
        if (true) {
            throw new IgniteException();
        }
    }));
    assertNull(accounts.recordView().get(null, makeKey(3)));
    assertNull(accounts.recordView().get(null, makeKey(4)));
}
Also used : RecordView(org.apache.ignite.table.RecordView) BeforeEach(org.junit.jupiter.api.BeforeEach) Pair(org.apache.ignite.internal.util.Pair) TransactionException(org.apache.ignite.tx.TransactionException) Random(java.util.Random) Disabled(org.junit.jupiter.api.Disabled) Row(org.apache.ignite.internal.schema.row.Row) TxState(org.apache.ignite.internal.tx.TxState) KeyValueView(org.apache.ignite.table.KeyValueView) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Flow(java.util.concurrent.Flow) Map(java.util.Map) CyclicBarrier(java.util.concurrent.CyclicBarrier) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Collection(java.util.Collection) UUID(java.util.UUID) TxManager(org.apache.ignite.internal.tx.TxManager) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) COMMITED(org.apache.ignite.internal.tx.TxState.COMMITED) IgniteTransactions(org.apache.ignite.tx.IgniteTransactions) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Strictness(org.mockito.quality.Strictness) ABORTED(org.apache.ignite.internal.tx.TxState.ABORTED) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) LongAdder(java.util.concurrent.atomic.LongAdder) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) IgniteException(org.apache.ignite.lang.IgniteException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) Transaction(org.apache.ignite.tx.Transaction) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) IgniteTestUtils(org.apache.ignite.internal.testframework.IgniteTestUtils) NativeTypes(org.apache.ignite.internal.schema.NativeTypes) LockManager(org.apache.ignite.internal.tx.LockManager) Column(org.apache.ignite.internal.schema.Column) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) Table(org.apache.ignite.table.Table) Tuple(org.apache.ignite.table.Tuple) IgniteException(org.apache.ignite.lang.IgniteException) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 3 with TransactionException

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

the class TxManagerImpl method finishRemote.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Void> finishRemote(NetworkAddress addr, Timestamp ts, boolean commit, Set<String> groups) {
    assert groups != null && !groups.isEmpty();
    TxFinishRequest req = FACTORY.txFinishRequest().timestamp(ts).groups(groups).commit(commit).build();
    CompletableFuture<NetworkMessage> fut = clusterService.messagingService().invoke(addr, req, TIMEOUT);
    // Submit response to a dedicated pool to avoid deadlocks. TODO: IGNITE-15389
    return fut.thenApplyAsync(resp -> ((TxFinishResponse) resp).errorMessage()).thenCompose(msg -> msg == null ? completedFuture(null) : failedFuture(new TransactionException(msg)));
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) BiFunction(java.util.function.BiFunction) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteStringFormatter.format(org.apache.ignite.lang.IgniteStringFormatter.format) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) ByteBuffer(java.nio.ByteBuffer) TxState(org.apache.ignite.internal.tx.TxState) Map(java.util.Map) TxMessageGroup(org.apache.ignite.internal.tx.message.TxMessageGroup) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) NetworkMessageHandler(org.apache.ignite.network.NetworkMessageHandler) TxFinishResponse(org.apache.ignite.internal.tx.message.TxFinishResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) NetworkMessage(org.apache.ignite.network.NetworkMessage) Timestamp(org.apache.ignite.internal.tx.Timestamp) TxManager(org.apache.ignite.internal.tx.TxManager) TxFinishResponseBuilder(org.apache.ignite.internal.tx.message.TxFinishResponseBuilder) NetworkAddress(org.apache.ignite.network.NetworkAddress) Objects(java.util.Objects) TestOnly(org.jetbrains.annotations.TestOnly) CompletableFuture.failedFuture(java.util.concurrent.CompletableFuture.failedFuture) Nullable(org.jetbrains.annotations.Nullable) LockException(org.apache.ignite.internal.tx.LockException) LockManager(org.apache.ignite.internal.tx.LockManager) TxMessagesFactory(org.apache.ignite.internal.tx.message.TxMessagesFactory) ClusterService(org.apache.ignite.network.ClusterService) IgniteUuid(org.apache.ignite.lang.IgniteUuid) TxFinishResponse(org.apache.ignite.internal.tx.message.TxFinishResponse) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) TransactionException(org.apache.ignite.tx.TransactionException) NetworkMessage(org.apache.ignite.network.NetworkMessage)

Example 4 with TransactionException

use of org.apache.ignite.tx.TransactionException 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 5 with TransactionException

use of org.apache.ignite.tx.TransactionException 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)

Aggregations

TransactionException (org.apache.ignite.tx.TransactionException)10 Test (org.junit.jupiter.api.Test)6 InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)5 TxState (org.apache.ignite.internal.tx.TxState)5 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)4 IgniteException (org.apache.ignite.lang.IgniteException)4 Transaction (org.apache.ignite.tx.Transaction)4 Tuple (org.apache.ignite.table.Tuple)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 LockManager (org.apache.ignite.internal.tx.LockManager)2 TxManager (org.apache.ignite.internal.tx.TxManager)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Objects (java.util.Objects)1