use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.
the class TxAbstractTest method testLockOrdering.
@Test
public void testLockOrdering() throws InterruptedException {
accounts.recordView().upsert(null, makeValue(1, 50.));
InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
InternalTransaction tx2 = (InternalTransaction) igniteTransactions.begin();
InternalTransaction tx3 = (InternalTransaction) igniteTransactions.begin();
InternalTransaction tx4 = (InternalTransaction) igniteTransactions.begin();
assertTrue(tx2.timestamp().compareTo(tx.timestamp()) > 0);
assertTrue(tx3.timestamp().compareTo(tx2.timestamp()) > 0);
assertTrue(tx4.timestamp().compareTo(tx3.timestamp()) > 0);
RecordView<Tuple> acc0 = accounts.recordView();
RecordView<Tuple> acc2 = accounts.recordView();
RecordView<Tuple> acc3 = accounts.recordView();
RecordView<Tuple> acc4 = accounts.recordView();
acc0.upsert(tx, makeValue(1, 100.));
CompletableFuture<Void> fut = acc3.upsertAsync(tx3, makeValue(1, 300.));
Thread.sleep(100);
assertFalse(fut.isDone());
CompletableFuture<Void> fut2 = acc4.upsertAsync(tx3, makeValue(1, 400.));
Thread.sleep(100);
assertFalse(fut2.isDone());
CompletableFuture<Void> fut3 = acc2.upsertAsync(tx2, makeValue(1, 200.));
assertFalse(fut3.isDone());
}
use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.
the class TxAbstractTest method testTwoTables.
@Test
public void testTwoTables() throws TransactionException {
customers.recordView().upsert(null, makeValue(1, "test"));
accounts.recordView().upsert(null, makeValue(1, 100.));
assertEquals("test", customers.recordView().get(null, makeKey(1)).stringValue("name"));
assertEquals(100., accounts.recordView().get(null, makeKey(1)).doubleValue("balance"));
InternalTransaction tx = (InternalTransaction) igniteTransactions.begin();
InternalTransaction tx2 = (InternalTransaction) igniteTransactions.begin();
var txCust = customers.recordView();
var txAcc = accounts.recordView();
txCust.upsert(tx, makeValue(1, "test2"));
txAcc.upsert(tx, makeValue(1, 200.));
Tuple txValCust = txCust.get(tx, makeKey(1));
assertEquals("test2", txValCust.stringValue("name"));
txValCust.set("accountNumber", 2L);
txValCust.set("name", "test3");
Tuple txValAcc = txAcc.get(tx, makeKey(1));
assertEquals(200., txValAcc.doubleValue("balance"));
txValAcc.set("accountNumber", 2L);
txValAcc.set("balance", 300.);
tx.commit();
tx2.commit();
assertEquals("test2", customers.recordView().get(null, makeKey(1)).stringValue("name"));
assertEquals(200., accounts.recordView().get(null, makeKey(1)).doubleValue("balance"));
assertTrue(lockManager(accounts).isEmpty());
}
use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.
the class InternalTableImpl method enlist.
/**
* Enlists a partition.
*
* @param partId Partition id.
* @param tx The transaction.
* @return The enlist future (then will a leader become known).
*/
protected CompletableFuture<RaftGroupService> enlist(int partId, InternalTransaction tx) {
RaftGroupService svc = partitionMap.get(partId);
CompletableFuture<Void> fut0 = svc.leader() == null ? svc.refreshLeader() : completedFuture(null);
// Enlist the leaseholder.
return fut0.thenAccept(ignored -> tx.enlist(svc)).thenApply(ignored -> svc);
}
use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.
the class InternalTableImpl method enlistInTx.
/**
* Enlists a single row into a transaction.
*
* @param row The row.
* @param tx The transaction.
* @param op Command factory.
* @param trans Transform closure.
* @param <R> Transform input.
* @param <T> Transform output.
* @return The future.
*/
private <R, T> CompletableFuture<T> enlistInTx(BinaryRow row, InternalTransaction tx, Function<InternalTransaction, Command> op, Function<R, T> trans) {
final boolean implicit = tx == null;
final InternalTransaction tx0 = implicit ? txManager.begin() : tx;
int partId = partId(row);
CompletableFuture<T> fut = enlist(partId, tx0).thenCompose(svc -> svc.<R>run(op.apply(tx0)).thenApply(trans::apply));
return postEnlist(fut, implicit, tx0);
}
use of org.apache.ignite.internal.tx.InternalTransaction in project ignite-3 by apache.
the class InternalTableImpl method enlistInTx.
/**
* Enlists multiple rows into a transaction.
*
* @param keyRows Rows.
* @param tx The transaction.
* @param op Command factory.
* @param reducer The reducer.
* @param <R> Reducer's input.
* @param <T> Reducer's output.
* @return The future.
*/
private <R, T> CompletableFuture<T> enlistInTx(Collection<BinaryRow> keyRows, InternalTransaction tx, BiFunction<Collection<BinaryRow>, InternalTransaction, Command> op, Function<CompletableFuture<R>[], CompletableFuture<T>> reducer) {
final boolean implicit = tx == null;
final InternalTransaction tx0 = implicit ? txManager.begin() : tx;
Int2ObjectOpenHashMap<List<BinaryRow>> keyRowsByPartition = mapRowsToPartitions(keyRows);
CompletableFuture<R>[] futures = new CompletableFuture[keyRowsByPartition.size()];
int batchNum = 0;
for (Int2ObjectOpenHashMap.Entry<List<BinaryRow>> partToRows : keyRowsByPartition.int2ObjectEntrySet()) {
CompletableFuture<RaftGroupService> fut = enlist(partToRows.getIntKey(), tx0);
futures[batchNum++] = fut.thenCompose(svc -> svc.run(op.apply(partToRows.getValue(), tx0)));
}
CompletableFuture<T> fut = reducer.apply(futures);
return postEnlist(fut, implicit, tx0);
}
Aggregations