Search in sources :

Example 11 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class PartitionCommandListenerTest method before.

/**
 * Initializes a table listener before tests.
 */
@BeforeEach
public void before() {
    ClusterService clusterService = Mockito.mock(ClusterService.class, RETURNS_DEEP_STUBS);
    NetworkAddress addr = new NetworkAddress("127.0.0.1", 5003);
    Mockito.when(clusterService.topologyService().localMember().address()).thenReturn(addr);
    commandListener = new PartitionListener(UUID.randomUUID(), new VersionedRowStore(new ConcurrentHashMapPartitionStorage(), new TxManagerImpl(clusterService, new HeapLockManager())));
}
Also used : VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) ConcurrentHashMapPartitionStorage(org.apache.ignite.internal.storage.basic.ConcurrentHashMapPartitionStorage) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress 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 13 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress 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 14 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class TxManagerImpl method onReceived.

/**
 * {@inheritDoc}
 */
@Override
public void onReceived(NetworkMessage message, NetworkAddress senderAddr, @Nullable Long correlationId) {
    // Support raft and transactions interop.
    if (message instanceof TxFinishRequest) {
        TxFinishRequest req = (TxFinishRequest) message;
        Set<String> groups = req.groups();
        CompletableFuture[] futs = new CompletableFuture[groups.size()];
        int i = 0;
        // Finish a tx for enlisted groups.
        for (String grp : groups) {
            futs[i++] = finish(grp, req.timestamp(), req.commit());
        }
        CompletableFuture.allOf(futs).thenCompose(ignored -> req.commit() ? commitAsync(req.timestamp()) : rollbackAsync(req.timestamp())).handle(new BiFunction<Void, Throwable, Void>() {

            @Override
            public Void apply(Void ignored, Throwable err) {
                TxFinishResponseBuilder resp = FACTORY.txFinishResponse();
                if (err != null) {
                    resp.errorMessage(err.getMessage());
                }
                clusterService.messagingService().respond(senderAddr, resp.build(), correlationId);
                return null;
            }
        });
    }
}
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) TxFinishRequest(org.apache.ignite.internal.tx.message.TxFinishRequest) CompletableFuture(java.util.concurrent.CompletableFuture) TxFinishResponseBuilder(org.apache.ignite.internal.tx.message.TxFinishResponseBuilder)

Example 15 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class ItClusterServiceTest method testShutdown.

@Test
void testShutdown(TestInfo testInfo) {
    var addr = new NetworkAddress("localhost", 10000);
    ClusterService service = clusterService(testInfo, addr.port(), new StaticNodeFinder(List.of(addr)), new TestScaleCubeClusterServiceFactory());
    service.start();
    service.stop();
    assertThat(service.isStopped(), is(true));
    ExecutionException e = assertThrows(ExecutionException.class, () -> service.messagingService().send(mock(ClusterNode.class), mock(NetworkMessage.class)).get(5, TimeUnit.SECONDS));
    assertThat(e.getCause(), isA(NodeStoppingException.class));
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) ExecutionException(java.util.concurrent.ExecutionException) NetworkMessage(org.apache.ignite.network.NetworkMessage) Test(org.junit.jupiter.api.Test)

Aggregations

NetworkAddress (org.apache.ignite.network.NetworkAddress)32 ClusterService (org.apache.ignite.network.ClusterService)18 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)12 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)11 Peer (org.apache.ignite.raft.client.Peer)11 Test (org.junit.jupiter.api.Test)11 BeforeEach (org.junit.jupiter.api.BeforeEach)9 ClusterNode (org.apache.ignite.network.ClusterNode)8 List (java.util.List)7 ArrayList (java.util.ArrayList)6 ExecutionException (java.util.concurrent.ExecutionException)6 JraftServerImpl (org.apache.ignite.internal.raft.server.impl.JraftServerImpl)6 TestScaleCubeClusterServiceFactory (org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory)6 Path (java.nio.file.Path)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)5 InternalTableImpl (org.apache.ignite.internal.table.distributed.storage.InternalTableImpl)5 NamedThreadFactory (org.apache.ignite.internal.thread.NamedThreadFactory)5 TxManager (org.apache.ignite.internal.tx.TxManager)5 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)5