Search in sources :

Example 1 with Timestamp

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

the class ItInternalTableScanTest method setUp.

/**
 * Prepare test environment.
 * <ol>
 * <li>Start network node.</li>
 * <li>Start raft server.</li>
 * <li>Prepare partitioned raft group.</li>
 * <li>Prepare partitioned raft group service.</li>
 * <li>Prepare internal table as a test object.</li>
 * </ol>
 *
 * @throws Exception If any.
 */
@BeforeEach
public void setUp(TestInfo testInfo) throws Exception {
    NetworkAddress nodeNetworkAddress = new NetworkAddress("localhost", 20_000);
    network = ClusterServiceTestUtils.clusterService(testInfo, 20_000, new StaticNodeFinder(List.of(nodeNetworkAddress)), NETWORK_FACTORY);
    network.start();
    raftSrv = new RaftServerImpl(network, FACTORY);
    raftSrv.start();
    String grpName = "test_part_grp";
    List<Peer> conf = List.of(new Peer(nodeNetworkAddress));
    mockStorage = mock(PartitionStorage.class);
    txManager = new TxManagerImpl(network, new HeapLockManager());
    txManager.start();
    UUID tblId = UUID.randomUUID();
    raftSrv.startRaftGroup(grpName, new PartitionListener(tblId, new VersionedRowStore(mockStorage, txManager) {

        @Override
        protected Pair<BinaryRow, BinaryRow> versionedRow(@Nullable DataRow row, Timestamp timestamp) {
            // Return as is.
            return new Pair<>(new ByteBufferRow(row.valueBytes()), null);
        }
    }), conf);
    executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
    RaftGroupService raftGrpSvc = RaftGroupServiceImpl.start(RAFT_GRP_ID, network, FACTORY, 10_000, conf, true, 200, executor).get(3, TimeUnit.SECONDS);
    internalTbl = new InternalTableImpl(TEST_TABLE_NAME, tblId, Int2ObjectMaps.singleton(0, raftGrpSvc), 1, NetworkAddress::toString, txManager, mock(TableStorage.class));
}
Also used : VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) Peer(org.apache.ignite.raft.client.Peer) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) SimpleDataRow(org.apache.ignite.internal.storage.basic.SimpleDataRow) DataRow(org.apache.ignite.internal.storage.DataRow) Timestamp(org.apache.ignite.internal.tx.Timestamp) RaftServerImpl(org.apache.ignite.internal.raft.server.impl.RaftServerImpl) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) NetworkAddress(org.apache.ignite.network.NetworkAddress) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) UUID(java.util.UUID) Nullable(org.jetbrains.annotations.Nullable) PartitionStorage(org.apache.ignite.internal.storage.PartitionStorage) Pair(org.apache.ignite.internal.util.Pair) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with Timestamp

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

the class VersionedRowStore method unpack.

/**
 * Unpacks a raw value into (cur, old, ts) triplet. TODO asch IGNITE-15934 not very efficient.
 *
 * @param row The row.
 * @return The value.
 */
private static Value unpack(@Nullable DataRow row) {
    if (row == null) {
        return new Value(null, null, null);
    }
    ByteBuffer buf = row.value();
    BinaryRow newVal = null;
    BinaryRow oldVal = null;
    int l1 = buf.asIntBuffer().get();
    int pos = 4;
    buf.position(pos);
    if (l1 != 0) {
        // TODO asch IGNITE-15934 get rid of copying
        byte[] tmp = new byte[l1];
        buf.get(tmp);
        newVal = new ByteBufferRow(tmp);
        pos += l1;
    }
    buf.position(pos);
    int l2 = buf.asIntBuffer().get();
    pos += 4;
    buf.position(pos);
    if (l2 != 0) {
        // TODO asch get rid of copying
        byte[] tmp = new byte[l2];
        buf.get(tmp);
        oldVal = new ByteBufferRow(tmp);
        pos += l2;
    }
    buf.position(pos);
    long ts = buf.getLong();
    long nodeId = buf.getLong();
    return new Value(newVal, oldVal, new Timestamp(ts, nodeId));
}
Also used : ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) ByteBuffer(java.nio.ByteBuffer) Timestamp(org.apache.ignite.internal.tx.Timestamp)

Example 3 with Timestamp

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

the class TransactionImpl method finish.

/**
 * Finishes a transaction.
 *
 * @param commit {@code true} to commit, false to rollback.
 * @return The future.
 */
private CompletableFuture<Void> finish(boolean commit) {
    Map<NetworkAddress, Set<String>> tmp = new HashMap<>();
    // Group by common leader addresses.
    for (RaftGroupService svc : enlisted) {
        NetworkAddress addr = svc.leader().address();
        tmp.computeIfAbsent(addr, k -> new HashSet<>()).add(svc.groupId());
    }
    CompletableFuture[] futs = new CompletableFuture[tmp.size() + 1];
    int i = 0;
    for (Map.Entry<NetworkAddress, Set<String>> entry : tmp.entrySet()) {
        boolean local = address.equals(entry.getKey());
        futs[i++] = txManager.finishRemote(entry.getKey(), timestamp, commit, entry.getValue());
        LOG.debug("finish [addr={}, commit={}, ts={}, local={}, groupIds={}", address, commit, timestamp, local, entry.getValue());
    }
    // Handle coordinator's tx.
    futs[i] = tmp.containsKey(address) ? CompletableFuture.completedFuture(null) : commit ? txManager.commitAsync(timestamp) : txManager.rollbackAsync(timestamp);
    return CompletableFuture.allOf(futs);
}
Also used : TransactionException(org.apache.ignite.tx.TransactionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Timestamp(org.apache.ignite.internal.tx.Timestamp) IgniteLogger(org.apache.ignite.lang.IgniteLogger) TxManager(org.apache.ignite.internal.tx.TxManager) NetworkAddress(org.apache.ignite.network.NetworkAddress) HashSet(java.util.HashSet) ExecutionException(java.util.concurrent.ExecutionException) Nullable(org.jetbrains.annotations.Nullable) TxState(org.apache.ignite.internal.tx.TxState) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) InternalTransaction(org.apache.ignite.internal.tx.InternalTransaction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkAddress(org.apache.ignite.network.NetworkAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with Timestamp

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

use of org.apache.ignite.internal.tx.Timestamp 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

Timestamp (org.apache.ignite.internal.tx.Timestamp)9 TxState (org.apache.ignite.internal.tx.TxState)4 Set (java.util.Set)3 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 Nullable (org.jetbrains.annotations.Nullable)3 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)2 VersionedRowStore (org.apache.ignite.internal.table.distributed.storage.VersionedRowStore)2 InternalTransaction (org.apache.ignite.internal.tx.InternalTransaction)2 TxManager (org.apache.ignite.internal.tx.TxManager)2 HeapLockManager (org.apache.ignite.internal.tx.impl.HeapLockManager)2 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)2 TransactionException (org.apache.ignite.tx.TransactionException)2