Search in sources :

Example 11 with InternalTransaction

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());
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 12 with InternalTransaction

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());
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 13 with InternalTransaction

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);
}
Also used : DeleteCommand(org.apache.ignite.internal.table.distributed.command.DeleteCommand) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Subscription(java.util.concurrent.Flow.Subscription) BiFunction(java.util.function.BiFunction) InsertCommand(org.apache.ignite.internal.table.distributed.command.InsertCommand) DeleteAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteAllCommand) DeleteExactAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactAllCommand) IgniteLogger(org.apache.ignite.lang.IgniteLogger) GetAndDeleteCommand(org.apache.ignite.internal.table.distributed.command.GetAndDeleteCommand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) ScanRetrieveBatchCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanRetrieveBatchCommand) GetAllCommand(org.apache.ignite.internal.table.distributed.command.GetAllCommand) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ScanCloseCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanCloseCommand) Collection(java.util.Collection) UUID(java.util.UUID) ReplaceCommand(org.apache.ignite.internal.table.distributed.command.ReplaceCommand) Collectors(java.util.stream.Collectors) Command(org.apache.ignite.raft.client.Command) TxManager(org.apache.ignite.internal.tx.TxManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) GetAndReplaceCommand(org.apache.ignite.internal.table.distributed.command.GetAndReplaceCommand) ScanInitCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanInitCommand) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) InternalTable(org.apache.ignite.internal.table.InternalTable) UpsertCommand(org.apache.ignite.internal.table.distributed.command.UpsertCommand) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) IgniteUuidGenerator(org.apache.ignite.lang.IgniteUuidGenerator) Publisher(java.util.concurrent.Flow.Publisher) GetAndUpsertCommand(org.apache.ignite.internal.table.distributed.command.GetAndUpsertCommand) DeleteExactCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactCommand) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) ReplaceIfExistCommand(org.apache.ignite.internal.table.distributed.command.ReplaceIfExistCommand) MultiRowsResponse(org.apache.ignite.internal.table.distributed.command.response.MultiRowsResponse) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) NetworkAddress(org.apache.ignite.network.NetworkAddress) TestOnly(org.jetbrains.annotations.TestOnly) Peer(org.apache.ignite.raft.client.Peer) Subscriber(java.util.concurrent.Flow.Subscriber) GetCommand(org.apache.ignite.internal.table.distributed.command.GetCommand) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) UpsertAllCommand(org.apache.ignite.internal.table.distributed.command.UpsertAllCommand) InsertAllCommand(org.apache.ignite.internal.table.distributed.command.InsertAllCommand) Comparator(java.util.Comparator) SingleRowResponse(org.apache.ignite.internal.table.distributed.command.response.SingleRowResponse) IgniteUuid(org.apache.ignite.lang.IgniteUuid) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService)

Example 14 with InternalTransaction

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);
}
Also used : InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction)

Example 15 with InternalTransaction

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);
}
Also used : DeleteCommand(org.apache.ignite.internal.table.distributed.command.DeleteCommand) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Subscription(java.util.concurrent.Flow.Subscription) BiFunction(java.util.function.BiFunction) InsertCommand(org.apache.ignite.internal.table.distributed.command.InsertCommand) DeleteAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteAllCommand) DeleteExactAllCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactAllCommand) IgniteLogger(org.apache.ignite.lang.IgniteLogger) GetAndDeleteCommand(org.apache.ignite.internal.table.distributed.command.GetAndDeleteCommand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) ScanRetrieveBatchCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanRetrieveBatchCommand) GetAllCommand(org.apache.ignite.internal.table.distributed.command.GetAllCommand) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ScanCloseCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanCloseCommand) Collection(java.util.Collection) UUID(java.util.UUID) ReplaceCommand(org.apache.ignite.internal.table.distributed.command.ReplaceCommand) Collectors(java.util.stream.Collectors) Command(org.apache.ignite.raft.client.Command) TxManager(org.apache.ignite.internal.tx.TxManager) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) GetAndReplaceCommand(org.apache.ignite.internal.table.distributed.command.GetAndReplaceCommand) ScanInitCommand(org.apache.ignite.internal.table.distributed.command.scan.ScanInitCommand) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) InternalTable(org.apache.ignite.internal.table.InternalTable) UpsertCommand(org.apache.ignite.internal.table.distributed.command.UpsertCommand) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) IgniteUuidGenerator(org.apache.ignite.lang.IgniteUuidGenerator) Publisher(java.util.concurrent.Flow.Publisher) GetAndUpsertCommand(org.apache.ignite.internal.table.distributed.command.GetAndUpsertCommand) DeleteExactCommand(org.apache.ignite.internal.table.distributed.command.DeleteExactCommand) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) ReplaceIfExistCommand(org.apache.ignite.internal.table.distributed.command.ReplaceIfExistCommand) MultiRowsResponse(org.apache.ignite.internal.table.distributed.command.response.MultiRowsResponse) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) NetworkAddress(org.apache.ignite.network.NetworkAddress) TestOnly(org.jetbrains.annotations.TestOnly) Peer(org.apache.ignite.raft.client.Peer) Subscriber(java.util.concurrent.Flow.Subscriber) GetCommand(org.apache.ignite.internal.table.distributed.command.GetCommand) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) UpsertAllCommand(org.apache.ignite.internal.table.distributed.command.UpsertAllCommand) InsertAllCommand(org.apache.ignite.internal.table.distributed.command.InsertAllCommand) Comparator(java.util.Comparator) SingleRowResponse(org.apache.ignite.internal.table.distributed.command.response.SingleRowResponse) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)24 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)14 Test (org.junit.jupiter.api.Test)14 Tuple (org.apache.ignite.table.Tuple)12 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)8 NotNull (org.jetbrains.annotations.NotNull)8 Row (org.apache.ignite.internal.schema.row.Row)6 ArrayList (java.util.ArrayList)4 IgniteException (org.apache.ignite.lang.IgniteException)4 TransactionException (org.apache.ignite.tx.TransactionException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2