Search in sources :

Example 6 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class RaftGroupServiceTest method testAddPeer.

/**
 * @throws Exception
 */
@Test
public void testAddPeer() throws Exception {
    String groupId = "test";
    List<String> respPeers = peersToIds(NODES);
    when(messagingService.invoke(any(NetworkAddress.class), eq(FACTORY.addPeerRequest().peerId(PeerId.parsePeer(NODES.get(2).address().host() + ":" + NODES.get(2).address().port()).toString()).groupId(groupId).build()), anyLong())).then(invocation -> completedFuture(FACTORY.addPeerResponse().newPeersList(respPeers).build()));
    mockLeaderRequest(false);
    RaftGroupService service = RaftGroupServiceImpl.start(groupId, cluster, FACTORY, TIMEOUT, NODES.subList(0, 2), true, DELAY, executor).get(3, TimeUnit.SECONDS);
    assertEquals(NODES.subList(0, 2), service.peers());
    assertEquals(Collections.emptyList(), service.learners());
    service.addPeer(NODES.get(2)).get();
    assertEquals(NODES, service.peers());
    assertEquals(Collections.emptyList(), service.learners());
}
Also used : NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test)

Example 7 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class RaftGroupServiceTest method testRemoveLearners.

/**
 * @throws Exception
 */
@Test
public void testRemoveLearners() throws Exception {
    String groupId = "test";
    List<String> addLearners = peersToIds(NODES.subList(1, 3));
    List<String> removeLearners = peersToIds(NODES.subList(2, 3));
    List<String> resultLearners = NODES.subList(1, 2).stream().map(p -> PeerId.fromPeer(p).toString()).collect(Collectors.toList());
    when(messagingService.invoke(any(NetworkAddress.class), eq(FACTORY.removeLearnersRequest().learnersList(removeLearners).groupId(groupId).build()), anyLong())).then(invocation -> completedFuture(FACTORY.learnersOpResponse().newLearnersList(resultLearners).build()));
    mockAddLearners(groupId, addLearners, addLearners);
    mockLeaderRequest(false);
    RaftGroupService service = RaftGroupServiceImpl.start(groupId, cluster, FACTORY, TIMEOUT, NODES.subList(0, 1), true, DELAY, executor).get(3, TimeUnit.SECONDS);
    service.addLearners(NODES.subList(1, 3)).get();
    assertEquals(NODES.subList(0, 1), service.peers());
    assertEquals(NODES.subList(1, 3), service.learners());
    service.removeLearners(NODES.subList(2, 3)).get();
    assertEquals(NODES.subList(0, 1), service.peers());
    assertEquals(NODES.subList(1, 2), service.learners());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) TimeoutException(java.util.concurrent.TimeoutException) Timer(java.util.Timer) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse) ArgumentMatcher(org.mockito.ArgumentMatcher) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) TimerTask(java.util.TimerTask) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Status(org.apache.ignite.raft.jraft.Status) GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Collectors(java.util.stream.Collectors) CliRequests(org.apache.ignite.raft.jraft.rpc.CliRequests) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) CompletableFuture.failedFuture(java.util.concurrent.CompletableFuture.failedFuture) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Stream(java.util.stream.Stream) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RaftGroupServiceImpl(org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl) RaftError(org.apache.ignite.raft.jraft.error.RaftError) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Loza(org.apache.ignite.internal.raft.Loza) CompletableFuture(java.util.concurrent.CompletableFuture) WriteCommand(org.apache.ignite.raft.client.WriteCommand) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConnectException(java.net.ConnectException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MessagingService(org.apache.ignite.network.MessagingService) Mockito.when(org.mockito.Mockito.when) ActionRequest(org.apache.ignite.raft.jraft.rpc.ActionRequest) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RaftRpcFactory(org.apache.ignite.raft.jraft.rpc.RaftRpcFactory) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ClusterService(org.apache.ignite.network.ClusterService) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) Collections(java.util.Collections) RaftException(org.apache.ignite.raft.jraft.rpc.impl.RaftException) NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test)

Example 8 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class RaftGroupServiceTest method testSnapshotExecutionException.

/**
 * @throws Exception If failed.
 */
@Test
public void testSnapshotExecutionException() throws Exception {
    String groupId = "test";
    mockSnapshotRequest(1);
    RaftGroupService service = RaftGroupServiceImpl.start(groupId, cluster, FACTORY, TIMEOUT, NODES, false, DELAY, executor).get(3, TimeUnit.SECONDS);
    var addr = new NetworkAddress("localhost", 8082);
    CompletableFuture<Void> fut = service.snapshot(new Peer(addr));
    try {
        fut.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof IgniteInternalException);
    }
}
Also used : NetworkAddress(org.apache.ignite.network.NetworkAddress) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 9 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class RaftGroupServiceTest method testRefreshMembers.

/**
 * @throws Exception
 */
@Test
public void testRefreshMembers() throws Exception {
    String groupId = "test";
    List<String> respPeers = peersToIds(NODES.subList(0, 2));
    List<String> respLearners = peersToIds(NODES.subList(2, 2));
    when(messagingService.invoke(any(NetworkAddress.class), eq(FACTORY.getPeersRequest().onlyAlive(false).groupId(groupId).build()), anyLong())).then(invocation -> completedFuture(FACTORY.getPeersResponse().peersList(respPeers).learnersList(respLearners).build()));
    mockLeaderRequest(false);
    RaftGroupService service = RaftGroupServiceImpl.start(groupId, cluster, FACTORY, TIMEOUT, NODES, true, DELAY, executor).get(3, TimeUnit.SECONDS);
    assertEquals(NODES, service.peers());
    assertEquals(Collections.emptyList(), service.learners());
    service.refreshMembers(false).get(3, TimeUnit.SECONDS);
    assertEquals(NODES.subList(0, 2), service.peers());
    assertEquals(NODES.subList(2, 2), service.learners());
}
Also used : NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test)

Example 10 with RaftGroupService

use of org.apache.ignite.raft.client.service.RaftGroupService in project ignite-3 by apache.

the class RaftGroupServiceTest method testRefreshLeaderElectedAfterDelay.

/**
 * @throws Exception
 */
@Test
public void testRefreshLeaderElectedAfterDelay() throws Exception {
    String groupId = "test";
    mockLeaderRequest(false);
    // Simulate running elections.
    leader = null;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            leader = NODES.get(0);
        }
    }, 500);
    RaftGroupService service = RaftGroupServiceImpl.start(groupId, cluster, FACTORY, TIMEOUT, NODES, false, DELAY, executor).get(3, TimeUnit.SECONDS);
    assertNull(service.leader());
    service.refreshLeader().get();
    assertEquals(NODES.get(0), service.leader());
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Test(org.junit.jupiter.api.Test)

Aggregations

RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)46 Test (org.junit.jupiter.api.Test)33 NetworkAddress (org.apache.ignite.network.NetworkAddress)24 Peer (org.apache.ignite.raft.client.Peer)22 List (java.util.List)11 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 TimeoutException (java.util.concurrent.TimeoutException)8 Collectors (java.util.stream.Collectors)8 Loza (org.apache.ignite.internal.raft.Loza)8 IgniteLogger (org.apache.ignite.lang.IgniteLogger)8 NotNull (org.jetbrains.annotations.NotNull)8 ExecutionException (java.util.concurrent.ExecutionException)7 TxManager (org.apache.ignite.internal.tx.TxManager)7 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)7 AfterEach (org.junit.jupiter.api.AfterEach)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 IOException (java.io.IOException)6 UUID (java.util.UUID)6