use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.
the class AffinityServiceTest method testCalculatedAssignmentHappyPath.
@Test
public void testCalculatedAssignmentHappyPath() {
List<List<ClusterNode>> assignments = AffinityUtils.calculateAssignments(Arrays.asList(new ClusterNode(UUID.randomUUID().toString(), "node0", new NetworkAddress("localhost", 8080)), new ClusterNode(UUID.randomUUID().toString(), "node1", new NetworkAddress("localhost", 8081))), 10, 3);
assertEquals(10, assignments.size());
for (List<ClusterNode> partitionAssignment : assignments) {
assertEquals(2, partitionAssignment.size());
}
}
use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.
the class ItMetaStorageRaftGroupTest method beforeTest.
/**
* Run {@code NODES} cluster nodes.
*/
@BeforeEach
public void beforeTest(TestInfo testInfo) {
List<NetworkAddress> localAddresses = findLocalAddresses(NODE_PORT_BASE, NODE_PORT_BASE + NODES);
var nodeFinder = new StaticNodeFinder(localAddresses);
localAddresses.stream().map(addr -> ClusterServiceTestUtils.clusterService(testInfo, addr.port(), nodeFinder, NETWORK_FACTORY)).forEach(clusterService -> {
clusterService.start();
cluster.add(clusterService);
});
for (ClusterService node : cluster) {
assertTrue(waitForTopology(node, NODES, 1000));
}
LOG.info("Cluster started.");
executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
}
use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.
the class ItMetaStorageRaftGroupTest method testRangeNextWorksCorrectlyAfterLeaderChange.
/**
* Tests that {@link MetaStorageService#range(ByteArray, ByteArray, long)}} next command works correctly
* after leader changing.
*
* @throws Exception If failed.
*/
@Test
public void testRangeNextWorksCorrectlyAfterLeaderChange() throws Exception {
final AtomicInteger replicatorStartedCounter = new AtomicInteger(0);
final AtomicInteger replicatorStoppedCounter = new AtomicInteger(0);
when(mockStorage.range(EXPECTED_RESULT_ENTRY1.key().bytes(), new byte[] { 4 })).thenAnswer(invocation -> {
List<org.apache.ignite.internal.metastorage.server.Entry> entries = new ArrayList<>(List.of(EXPECTED_SRV_RESULT_ENTRY1, EXPECTED_SRV_RESULT_ENTRY2));
return new Cursor<org.apache.ignite.internal.metastorage.server.Entry>() {
private final Iterator<org.apache.ignite.internal.metastorage.server.Entry> it = entries.iterator();
@Override
public void close() {
}
@NotNull
@Override
public Iterator<org.apache.ignite.internal.metastorage.server.Entry> iterator() {
return it;
}
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public org.apache.ignite.internal.metastorage.server.Entry next() {
return it.next();
}
};
});
List<Pair<RaftServer, RaftGroupService>> raftServersRaftGroups = prepareJraftMetaStorages(replicatorStartedCounter, replicatorStoppedCounter);
List<RaftServer> raftServers = raftServersRaftGroups.stream().map(p -> p.key).collect(Collectors.toList());
NetworkAddress oldLeader = raftServersRaftGroups.get(0).value.leader().address();
Optional<RaftServer> oldLeaderServer = raftServers.stream().filter(s -> s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
// Server that will be alive after we stop leader.
Optional<RaftServer> liveServer = raftServers.stream().filter(s -> !s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
if (oldLeaderServer.isEmpty() || liveServer.isEmpty()) {
fail();
}
RaftGroupService raftGroupServiceOfLiveServer = raftServersRaftGroups.stream().filter(p -> p.key.equals(liveServer.get())).findFirst().get().value;
MetaStorageService metaStorageSvc = new MetaStorageServiceImpl(raftGroupServiceOfLiveServer, "some_node");
Cursor<Entry> cursor = metaStorageSvc.range(EXPECTED_RESULT_ENTRY1.key(), new ByteArray(new byte[] { 4 }));
assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
assertTrue(cursor.hasNext());
assertEquals(EXPECTED_RESULT_ENTRY1, (cursor.iterator().next()));
// Ensure that leader has not been changed.
// In a stable topology unexpected leader election shouldn't happen.
assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
// stop leader
oldLeaderServer.get().stopRaftGroup(METASTORAGE_RAFT_GROUP_NAME);
oldLeaderServer.get().stop();
cluster.stream().filter(c -> c.topologyService().localMember().address().equals(oldLeader)).findFirst().get().stop();
raftGroupServiceOfLiveServer.refreshLeader().get();
assertNotSame(oldLeader, raftGroupServiceOfLiveServer.leader().address());
// ensure that leader has been changed only once
assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 4, 5_000), replicatorStartedCounter.get() + "");
assertTrue(TestUtils.waitForCondition(() -> replicatorStoppedCounter.get() == 2, 5_000), replicatorStoppedCounter.get() + "");
assertTrue(cursor.hasNext());
assertEquals(EXPECTED_RESULT_ENTRY2, (cursor.iterator().next()));
}
use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.
the class ItNodeTest method createService.
/**
* @param groupId Group id.
* @param peerId Peer id.
* @param nodeOptions Node options.
* @return Raft group service.
*/
private RaftGroupService createService(String groupId, PeerId peerId, NodeOptions nodeOptions) {
Configuration initialConf = nodeOptions.getInitialConf();
nodeOptions.setStripes(1);
Stream<PeerId> peers = initialConf == null ? Stream.empty() : Stream.concat(initialConf.getPeers().stream(), initialConf.getLearners().stream());
List<NetworkAddress> addressList = peers.map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
var nodeManager = new NodeManager();
ClusterService clusterService = ClusterServiceTestUtils.clusterService(testInfo, peerId.getEndpoint().getPort(), new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
ExecutorService requestExecutor = JRaftUtils.createRequestExecutor(nodeOptions);
executors.add(requestExecutor);
IgniteRpcServer rpcServer = new TestIgniteRpcServer(clusterService, nodeManager, nodeOptions, requestExecutor);
nodeOptions.setRpcClient(new IgniteRpcClient(clusterService));
clusterService.start();
var service = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager) {
@Override
public synchronized void shutdown() {
rpcServer.shutdown();
super.shutdown();
clusterService.stop();
}
};
services.add(service);
return service;
}
use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.
the class ItJraftCounterServerTest method startClient.
/**
* Starts client.
*
* @param groupId Group id.
* @return The client.
* @throws Exception If failed.
*/
private RaftGroupService startClient(String groupId) throws Exception {
var addr = new NetworkAddress(getLocalAddress(), PORT);
ClusterService clientNode = clusterService(CLIENT_PORT + clients.size(), List.of(addr), true);
RaftGroupService client = RaftGroupServiceImpl.start(groupId, clientNode, FACTORY, 10_000, List.of(new Peer(addr)), false, 200, executor).get(3, TimeUnit.SECONDS);
clients.add(client);
return client;
}
Aggregations