use of org.apache.ignite.network.ClusterService in project ignite-3 by apache.
the class ItMetaStorageServiceTest method beforeTest.
/**
* Run {@code NODES} cluster nodes.
*/
@BeforeEach
public void beforeTest(TestInfo testInfo) throws Exception {
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));
metaStorageSvc = prepareMetaStorage();
}
use of org.apache.ignite.network.ClusterService in project ignite-3 by apache.
the class ItTxDistributedTestSingleNode method before.
/**
* Initialize the test state.
*/
@Override
@BeforeEach
public void before() throws Exception {
int nodes = nodes();
int replicas = replicas();
assertTrue(nodes > 0);
assertTrue(replicas > 0);
List<NetworkAddress> localAddresses = findLocalAddresses(NODE_PORT_BASE, NODE_PORT_BASE + nodes);
var nodeFinder = new StaticNodeFinder(localAddresses);
nodeFinder.findNodes().parallelStream().map(addr -> startNode(testInfo, addr.toString(), addr.port(), nodeFinder)).forEach(cluster::add);
for (ClusterService node : cluster) {
assertTrue(waitForTopology(node, nodes, 1000));
}
log.info("The cluster has been started");
if (startClient()) {
client = startNode(testInfo, "client", NODE_PORT_BASE - 1, nodeFinder);
assertTrue(waitForTopology(client, nodes + 1, 1000));
log.info("The client has been started");
}
// Start raft servers. Each raft server can hold multiple groups.
raftServers = new HashMap<>(nodes);
txManagers = new HashMap<>(nodes);
executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
for (int i = 0; i < nodes; i++) {
var raftSrv = new Loza(cluster.get(i), workDir);
raftSrv.start();
ClusterNode node = cluster.get(i).topologyService().localMember();
raftServers.put(node, raftSrv);
TableTxManagerImpl txMgr = new TableTxManagerImpl(cluster.get(i), new HeapLockManager());
txMgr.start();
txManagers.put(node, txMgr);
}
log.info("Raft servers have been started");
final String accountsName = "accounts";
final String customersName = "customers";
UUID accTblId = UUID.randomUUID();
UUID custTblId = UUID.randomUUID();
accRaftClients = startTable(accountsName, accTblId);
custRaftClients = startTable(customersName, custTblId);
log.info("Partition groups have been started");
TxManager txMgr;
if (startClient()) {
txMgr = new TxManagerImpl(client, new HeapLockManager());
} else {
// Collocated mode.
txMgr = txManagers.get(accRaftClients.get(0).clusterService().topologyService().localMember());
}
assertNotNull(txMgr);
igniteTransactions = new IgniteTransactionsImpl(txMgr);
this.accounts = new TableImpl(new InternalTableImpl(accountsName, accTblId, accRaftClients, 1, NetworkAddress::toString, txMgr, Mockito.mock(TableStorage.class)), new DummySchemaManagerImpl(ACCOUNTS_SCHEMA));
this.customers = new TableImpl(new InternalTableImpl(customersName, custTblId, custRaftClients, 1, NetworkAddress::toString, txMgr, Mockito.mock(TableStorage.class)), new DummySchemaManagerImpl(CUSTOMERS_SCHEMA));
log.info("Tables have been started");
}
use of org.apache.ignite.network.ClusterService in project ignite-3 by apache.
the class ItTxDistributedTestSingleNode method startTable.
/**
* Starts a table.
*
* @param name The name.
* @param tblId Table id.
* @return Groups map.
*/
protected Int2ObjectOpenHashMap<RaftGroupService> startTable(String name, UUID tblId) throws Exception {
List<List<ClusterNode>> assignment = RendezvousAffinityFunction.assignPartitions(cluster.stream().map(node -> node.topologyService().localMember()).collect(Collectors.toList()), 1, replicas(), false, null);
Int2ObjectOpenHashMap<RaftGroupService> clients = new Int2ObjectOpenHashMap<>();
for (int p = 0; p < assignment.size(); p++) {
List<ClusterNode> partNodes = assignment.get(p);
String grpId = name + "-part-" + p;
List<Peer> conf = partNodes.stream().map(n -> n.address()).map(Peer::new).collect(Collectors.toList());
for (ClusterNode node : partNodes) {
raftServers.get(node).prepareRaftGroup(grpId, partNodes, () -> new PartitionListener(tblId, new VersionedRowStore(new ConcurrentHashMapPartitionStorage(), txManagers.get(node))));
}
if (startClient()) {
RaftGroupService service = RaftGroupServiceImpl.start(grpId, client, FACTORY, 10_000, conf, true, 200, executor).get(5, TimeUnit.SECONDS);
clients.put(p, service);
} else {
// Create temporary client to find a leader address.
ClusterService tmpSvc = raftServers.values().stream().findFirst().get().service();
RaftGroupService service = RaftGroupServiceImpl.start(grpId, tmpSvc, FACTORY, 10_000, conf, true, 200, executor).get(5, TimeUnit.SECONDS);
Peer leader = service.leader();
service.shutdown();
Loza leaderSrv = raftServers.get(tmpSvc.topologyService().getByAddress(leader.address()));
RaftGroupService leaderClusterSvc = RaftGroupServiceImpl.start(grpId, leaderSrv.service(), FACTORY, 10_000, conf, true, 200, executor).get(5, TimeUnit.SECONDS);
clients.put(p, leaderClusterSvc);
}
}
return clients;
}
use of org.apache.ignite.network.ClusterService in project ignite-3 by apache.
the class TestCluster method start.
public boolean start(Endpoint listenAddr, boolean emptyPeers, int snapshotIntervalSecs, boolean enableMetrics, SnapshotThrottle snapshotThrottle, RaftOptions raftOptions, int priority) throws IOException {
this.lock.lock();
try {
if (this.serverMap.get(listenAddr) != null) {
return true;
}
// Start node in non shared pools mode. Pools will be managed by node itself.
NodeOptions nodeOptions = new NodeOptions();
nodeOptions.setServerName(listenAddr.toString());
nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
nodeOptions.setEnableMetrics(enableMetrics);
nodeOptions.setSnapshotThrottle(snapshotThrottle);
nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
nodeOptions.setServiceFactory(this.raftServiceFactory);
if (raftOptions != null) {
nodeOptions.setRaftOptions(raftOptions);
}
String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
new File(serverDataPath).mkdirs();
nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
nodeOptions.setElectionPriority(priority);
// Align rpc options with election timeout.
nodeOptions.setRpcConnectTimeoutMs(this.electionTimeoutMs / 3);
nodeOptions.setRpcDefaultTimeout(this.electionTimeoutMs / 2);
// Reduce default threads count per test node.
nodeOptions.setRaftRpcThreadPoolSize(Utils.cpus());
nodeOptions.setTimerPoolSize(Utils.cpus() * 2);
nodeOptions.setRpcProcessorThreadPoolSize(Utils.cpus() * 3);
nodeOptions.setElectionTimeoutStrategy(new ExponentialBackoffTimeoutStrategy());
MockStateMachine fsm = new MockStateMachine(listenAddr);
nodeOptions.setFsm(fsm);
if (!emptyPeers)
nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
List<NetworkAddress> addressList = (emptyPeers ? Stream.<PeerId>empty() : peers.stream()).map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
NodeManager nodeManager = new NodeManager();
ClusterService clusterService = ClusterServiceTestUtils.clusterService(testInfo, listenAddr.getPort(), new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
var rpcClient = new IgniteRpcClient(clusterService);
nodeOptions.setRpcClient(rpcClient);
ExecutorService requestExecutor = JRaftUtils.createRequestExecutor(nodeOptions);
var rpcServer = new TestIgniteRpcServer(clusterService, nodeManager, nodeOptions, requestExecutor);
clusterService.start();
if (optsClo != null)
optsClo.accept(nodeOptions);
RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0, priority), nodeOptions, rpcServer, nodeManager) {
@Override
public synchronized void shutdown() {
// This stop order is consistent with JRaftServerImpl
rpcServer.shutdown();
ExecutorServiceHelper.shutdownAndAwaitTermination(requestExecutor);
super.shutdown();
// Network service must be stopped after a node because raft initiates timeoutnowrequest on stop for faster
// leader election.
clusterService.stop();
}
};
this.serverMap.put(listenAddr, server);
Node node = server.start();
this.fsms.put(new PeerId(listenAddr, 0), fsm);
this.nodes.add((NodeImpl) node);
return true;
} finally {
this.lock.unlock();
}
}
use of org.apache.ignite.network.ClusterService in project ignite-3 by apache.
the class IgniteRpcTest method waitForTopology.
/**
* {@inheritDoc}
*/
@Override
protected boolean waitForTopology(RpcClient client, int expected, long timeout) {
IgniteRpcClient client0 = (IgniteRpcClient) client;
ClusterService service = client0.clusterService();
return waitForTopology(service, expected, timeout);
}
Aggregations