use of org.apache.ignite.network.NodeFinder in project ignite-3 by apache.
the class ClusterServiceTestUtils method clusterService.
/**
* Creates a cluster service and required node configuration manager beneath it. Populates node configuration with specified port.
* Manages configuration manager lifecycle: on cluster service start starts node configuration manager, on cluster service stop - stops
* node configuration manager.
*
* @param testInfo Test info.
* @param port Local port.
* @param nodeFinder Node finder.
* @param clusterSvcFactory Cluster service factory.
*/
public static ClusterService clusterService(TestInfo testInfo, int port, NodeFinder nodeFinder, TestScaleCubeClusterServiceFactory clusterSvcFactory) {
var registry = new MessageSerializationRegistryImpl();
REGISTRY_INITIALIZERS.forEach(c -> {
try {
c.invoke(c.getDeclaringClass(), registry);
} catch (Throwable e) {
throw new RuntimeException("Failed to invoke registry initializer", e);
}
});
var ctx = new ClusterLocalConfiguration(testNodeName(testInfo, port), registry);
ConfigurationManager nodeConfigurationMgr = new ConfigurationManager(Collections.singleton(NetworkConfiguration.KEY), Map.of(), new TestConfigurationStorage(ConfigurationType.LOCAL), List.of(), List.of());
NetworkConfiguration configuration = nodeConfigurationMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
var bootstrapFactory = new NettyBootstrapFactory(configuration, ctx.getName());
var clusterSvc = clusterSvcFactory.createClusterService(ctx, configuration, bootstrapFactory);
assert nodeFinder instanceof StaticNodeFinder : "Only StaticNodeFinder is supported at the moment";
return new ClusterService() {
@Override
public TopologyService topologyService() {
return clusterSvc.topologyService();
}
@Override
public MessagingService messagingService() {
return clusterSvc.messagingService();
}
@Override
public ClusterLocalConfiguration localConfiguration() {
return clusterSvc.localConfiguration();
}
@Override
public boolean isStopped() {
return clusterSvc.isStopped();
}
@Override
public void start() {
nodeConfigurationMgr.start();
NetworkConfiguration configuration = nodeConfigurationMgr.configurationRegistry().getConfiguration(NetworkConfiguration.KEY);
configuration.change(netCfg -> netCfg.changePort(port).changeNodeFinder(c -> c.changeType(NodeFinderType.STATIC.toString()).changeNetClusterNodes(nodeFinder.findNodes().stream().map(NetworkAddress::toString).toArray(String[]::new)))).join();
bootstrapFactory.start();
clusterSvc.start();
}
@Override
public void stop() {
try {
clusterSvc.stop();
bootstrapFactory.stop();
nodeConfigurationMgr.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
}
use of org.apache.ignite.network.NodeFinder in project ignite-3 by apache.
the class ScaleCubeClusterServiceFactory method createClusterService.
/**
* Creates a new {@link ClusterService} using the provided context. The created network will not be in the "started" state.
*
* @param context Cluster context.
* @param networkConfiguration Network configuration.
* @param nettyBootstrapFactory Bootstrap factory.
* @return New cluster service.
*/
public ClusterService createClusterService(ClusterLocalConfiguration context, NetworkConfiguration networkConfiguration, NettyBootstrapFactory nettyBootstrapFactory) {
var messageFactory = new NetworkMessagesFactory();
var topologyService = new ScaleCubeTopologyService();
UserObjectSerializationContext userObjectSerialization = createUserObjectSerializationContext();
var messagingService = new DefaultMessagingService(messageFactory, topologyService, userObjectSerialization);
return new AbstractClusterService(context, topologyService, messagingService) {
private volatile ClusterImpl cluster;
private volatile ConnectionManager connectionMgr;
private volatile CompletableFuture<Void> shutdownFuture;
/**
* {@inheritDoc}
*/
@Override
public void start() {
String consistentId = context.getName();
var serializationService = new SerializationService(context.getSerializationRegistry(), userObjectSerialization);
UUID launchId = UUID.randomUUID();
NetworkView configView = networkConfiguration.value();
connectionMgr = new ConnectionManager(configView, serializationService, consistentId, () -> new RecoveryServerHandshakeManager(launchId, consistentId, messageFactory), () -> new RecoveryClientHandshakeManager(launchId, consistentId, messageFactory), nettyBootstrapFactory);
connectionMgr.start();
var transport = new ScaleCubeDirectMarshallerTransport(connectionMgr.getLocalAddress(), messagingService, topologyService, messageFactory);
NodeFinder finder = NodeFinderFactory.createNodeFinder(configView.nodeFinder());
cluster = new ClusterImpl(clusterConfig(configView.membership())).handler(cl -> new ClusterMessageHandler() {
/**
* {@inheritDoc}
*/
@Override
public void onMembershipEvent(MembershipEvent event) {
topologyService.onMembershipEvent(event);
}
}).config(opts -> opts.memberAlias(consistentId)).transport(opts -> opts.transportFactory(transportConfig -> transport)).membership(opts -> opts.seedMembers(parseAddresses(finder.findNodes())));
shutdownFuture = cluster.onShutdown().toFuture();
// resolve cyclic dependencies
topologyService.setCluster(cluster);
messagingService.setConnectionManager(connectionMgr);
cluster.startAwait();
// emit an artificial event as if the local member has joined the topology (ScaleCube doesn't do that)
var localMembershipEvent = MembershipEvent.createAdded(cluster.member(), null, System.currentTimeMillis());
topologyService.onMembershipEvent(localMembershipEvent);
}
/**
* {@inheritDoc}
*/
@Override
public void stop() {
// local member will be null, if cluster has not been started
if (cluster.member() == null) {
return;
}
cluster.shutdown();
try {
shutdownFuture.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IgniteInternalException("Interrupted while waiting for the ClusterService to stop", e);
} catch (TimeoutException e) {
throw new IgniteInternalException("Timeout while waiting for the ClusterService to stop", e);
} catch (ExecutionException e) {
throw new IgniteInternalException("Unable to stop the ClusterService", e.getCause());
}
connectionMgr.stop();
// Messaging service checks connection manager's status before sending a message, so connection manager should be
// stopped before messaging service
messagingService.stop();
}
/**
* {@inheritDoc}
*/
@Override
public void beforeNodeStop() {
stop();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isStopped() {
return shutdownFuture.isDone();
}
};
}
use of org.apache.ignite.network.NodeFinder 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");
}
Aggregations