use of org.apache.ignite.network.NetworkAddress 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.NetworkAddress 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);
}
}
use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.
the class RaftGroupServiceTest method mockUserInput.
/**
* @param delay {@code True} to create a delay before response.
* @param peer Fail the request targeted to given peer.
*/
private void mockUserInput(boolean delay, @Nullable Peer peer) {
when(messagingService.invoke(any(NetworkAddress.class), argThat(new ArgumentMatcher<ActionRequest>() {
@Override
public boolean matches(ActionRequest arg) {
return arg.command() instanceof TestCommand;
}
}), anyLong())).then(invocation -> {
NetworkAddress target = invocation.getArgument(0);
if (peer != null && target.equals(peer.address()))
return failedFuture(new IgniteInternalException(new ConnectException()));
if (delay) {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
fail();
}
return FACTORY.actionResponse().result(new TestResponse()).build();
});
}
Object resp;
if (leader == null)
resp = FACTORY.errorResponse().errorCode(RaftError.EPERM.getNumber()).build();
else if (!target.equals(leader.address()))
resp = FACTORY.errorResponse().errorCode(RaftError.EPERM.getNumber()).leaderId(PeerId.fromPeer(leader).toString()).build();
else
resp = FACTORY.actionResponse().result(new TestResponse()).build();
return completedFuture(resp);
});
}
use of org.apache.ignite.network.NetworkAddress 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.NetworkAddress in project ignite-3 by apache.
the class TableManagerTest method mockManagersAndCreateTableWithDelay.
/**
* Instantiates a table and prepares Table manager. When the latch would open, the method completes.
*
* @param tableDefinition Configuration schema for a table.
* @param tblManagerFut Future for table manager.
* @param phaser Phaser for the wait.
* @return Table manager.
* @throws NodeStoppingException If something went wrong.
*/
@NotNull
private TableImpl mockManagersAndCreateTableWithDelay(TableDefinition tableDefinition, CompletableFuture<TableManager> tblManagerFut, Phaser phaser) 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 CompletableFuture.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));
}
try (MockedStatic<AffinityUtils> affinityServiceMock = mockStatic(AffinityUtils.class)) {
ArrayList<List<ClusterNode>> assignment = new ArrayList<>(PARTITIONS);
for (int part = 0; part < PARTITIONS; part++) {
assignment.add(new ArrayList<>(Collections.singleton(node)));
}
affinityServiceMock.when(() -> AffinityUtils.calculateAssignments(any(), anyInt(), anyInt())).thenReturn(assignment);
}
TableManager tableManager = createTableManager(tblManagerFut);
final int tablesBeforeCreation = tableManager.tables().size();
tblsCfg.tables().listen(ctx -> {
boolean createTbl = ctx.newValue().get(tableDefinition.canonicalName()) != null && ctx.oldValue().get(tableDefinition.canonicalName()) == null;
boolean dropTbl = ctx.oldValue().get(tableDefinition.canonicalName()) != null && ctx.newValue().get(tableDefinition.canonicalName()) == null;
if (!createTbl && !dropTbl) {
return CompletableFuture.completedFuture(null);
}
if (phaser != null) {
phaser.arriveAndAwaitAdvance();
}
return CompletableFuture.completedFuture(null);
});
TableImpl tbl2 = (TableImpl) tableManager.createTable(tableDefinition.canonicalName(), tblCh -> SchemaConfigurationConverter.convert(tableDefinition, tblCh).changeReplicas(REPLICAS).changePartitions(PARTITIONS));
assertNotNull(tbl2);
assertEquals(tablesBeforeCreation + 1, tableManager.tables().size());
return tbl2;
}
Aggregations