use of org.apache.ignite.internal.affinity.AffinityUtils 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