use of org.apache.ignite.network.ClusterLocalConfiguration 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.ClusterLocalConfiguration in project ignite-3 by apache.
the class MockedStructuresTest method mockManagers.
// todo copy-paste from TableManagerTest will be removed after https://issues.apache.org/jira/browse/IGNITE-16050
/**
* Instantiates a table and prepares Table manager.
*
* @return Table manager.
*/
private TableManager mockManagers() throws NodeStoppingException {
when(rm.prepareRaftGroup(any(), any(), any())).thenAnswer(mock -> {
RaftGroupService raftGrpSrvcMock = mock(RaftGroupService.class);
when(raftGrpSrvcMock.leader()).thenReturn(new Peer(new NetworkAddress("localhost", 47500)));
return completedFuture(raftGrpSrvcMock);
});
when(ts.getByAddress(any(NetworkAddress.class))).thenReturn(new ClusterNode(UUID.randomUUID().toString(), "node0", new NetworkAddress("localhost", 47500)));
try (MockedStatic<SchemaUtils> schemaServiceMock = mockStatic(SchemaUtils.class)) {
schemaServiceMock.when(() -> SchemaUtils.prepareSchemaDescriptor(anyInt(), any())).thenReturn(mock(SchemaDescriptor.class));
}
when(cs.messagingService()).thenAnswer(invocation -> {
MessagingService ret = mock(MessagingService.class);
return ret;
});
when(cs.localConfiguration()).thenAnswer(invocation -> {
ClusterLocalConfiguration ret = mock(ClusterLocalConfiguration.class);
when(ret.getName()).thenReturn("node1");
return ret;
});
when(cs.topologyService()).thenAnswer(invocation -> {
TopologyService ret = mock(TopologyService.class);
when(ret.localMember()).thenReturn(new ClusterNode("1", "node1", null));
return ret;
});
TableManager tableManager = createTableManager();
return tableManager;
}
use of org.apache.ignite.network.ClusterLocalConfiguration 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.ClusterLocalConfiguration in project ignite-3 by apache.
the class LozaTest method testLozaStop.
/**
* Checks that the all API methods throw the exception ({@link org.apache.ignite.lang.NodeStoppingException})
* when Loza is closed.
*
* @throws Exception If fail.
*/
@Test
public void testLozaStop() throws Exception {
Mockito.doReturn(new ClusterLocalConfiguration("test_node", null)).when(clusterNetSvc).localConfiguration();
Mockito.doReturn(Mockito.mock(MessagingService.class)).when(clusterNetSvc).messagingService();
Mockito.doReturn(Mockito.mock(TopologyService.class)).when(clusterNetSvc).topologyService();
Loza loza = new Loza(clusterNetSvc, workDir);
loza.start();
loza.beforeNodeStop();
loza.stop();
String raftGroupId = "test_raft_group";
List<ClusterNode> nodes = List.of(new ClusterNode(UUID.randomUUID().toString(), UUID.randomUUID().toString(), NetworkAddress.from("127.0.0.1:123")));
List<ClusterNode> newNodes = List.of(new ClusterNode(UUID.randomUUID().toString(), UUID.randomUUID().toString(), NetworkAddress.from("127.0.0.1:124")), new ClusterNode(UUID.randomUUID().toString(), UUID.randomUUID().toString(), NetworkAddress.from("127.0.0.1:125")));
Supplier<RaftGroupListener> lsnrSupplier = () -> null;
assertThrows(NodeStoppingException.class, () -> loza.updateRaftGroup(raftGroupId, nodes, newNodes, lsnrSupplier));
assertThrows(NodeStoppingException.class, () -> loza.stopRaftGroup(raftGroupId));
assertThrows(NodeStoppingException.class, () -> loza.prepareRaftGroup(raftGroupId, nodes, lsnrSupplier));
assertThrows(NodeStoppingException.class, () -> loza.changePeers(raftGroupId, nodes, newNodes));
}
Aggregations