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"));
}
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)));
}
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)));
}
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));
}
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));
}
Aggregations