Search in sources :

Example 6 with ClusterNode

use of org.apache.ignite.network.ClusterNode in project ignite-3 by apache.

the class TableManager method createTableLocally.

/**
 * Creates local structures for a table.
 *
 * @param causalityToken Causality token.
 * @param name  Table name.
 * @param tblId Table id.
 * @param assignment Affinity assignment.
 */
private void createTableLocally(long causalityToken, String name, UUID tblId, List<List<ClusterNode>> assignment, SchemaDescriptor schemaDesc) {
    int partitions = assignment.size();
    var partitionsGroupsFutures = new ArrayList<CompletableFuture<RaftGroupService>>();
    Path storageDir = partitionsStoreDir.resolve(name);
    try {
        Files.createDirectories(storageDir);
    } catch (IOException e) {
        throw new IgniteInternalException("Failed to create partitions store directory for " + name + ": " + e.getMessage(), e);
    }
    TableConfiguration tableCfg = tablesCfg.tables().get(name);
    DataRegion dataRegion = dataRegions.computeIfAbsent(tableCfg.dataRegion().value(), dataRegionName -> {
        DataRegion newDataRegion = engine.createDataRegion(dataStorageCfg.regions().get(dataRegionName));
        try {
            newDataRegion.start();
        } catch (Exception e) {
            try {
                newDataRegion.stop();
            } catch (Exception stopException) {
                e.addSuppressed(stopException);
            }
            throw e;
        }
        return newDataRegion;
    });
    TableStorage tableStorage = engine.createTable(storageDir, tableCfg, dataRegion);
    tableStorage.start();
    for (int p = 0; p < partitions; p++) {
        int partId = p;
        try {
            partitionsGroupsFutures.add(raftMgr.prepareRaftGroup(raftGroupName(tblId, p), assignment.get(p), () -> new PartitionListener(tblId, new VersionedRowStore(tableStorage.getOrCreatePartition(partId), txManager))));
        } catch (NodeStoppingException e) {
            throw new AssertionError("Loza was stopped before Table manager", e);
        }
    }
    CompletableFuture.allOf(partitionsGroupsFutures.toArray(CompletableFuture[]::new)).thenRun(() -> {
        try {
            Int2ObjectOpenHashMap<RaftGroupService> partitionMap = new Int2ObjectOpenHashMap<>(partitions);
            for (int p = 0; p < partitions; p++) {
                CompletableFuture<RaftGroupService> future = partitionsGroupsFutures.get(p);
                assert future.isDone();
                RaftGroupService service = future.join();
                partitionMap.put(p, service);
            }
            InternalTableImpl internalTable = new InternalTableImpl(name, tblId, partitionMap, partitions, netAddrResolver, txManager, tableStorage);
            var schemaRegistry = new SchemaRegistryImpl(v -> {
                if (!busyLock.enterBusy()) {
                    throw new IgniteException(new NodeStoppingException());
                }
                try {
                    return tableSchema(tblId, v);
                } finally {
                    busyLock.leaveBusy();
                }
            }, () -> {
                if (!busyLock.enterBusy()) {
                    throw new IgniteException(new NodeStoppingException());
                }
                try {
                    return latestSchemaVersion(tblId);
                } finally {
                    busyLock.leaveBusy();
                }
            });
            schemaRegistry.onSchemaRegistered(schemaDesc);
            var table = new TableImpl(internalTable, schemaRegistry);
            tablesVv.update(causalityToken, previous -> {
                var val = previous == null ? new HashMap() : new HashMap<>(previous);
                val.put(name, table);
                return val;
            }, th -> {
                throw new IgniteInternalException(IgniteStringFormatter.format("Cannot create a table [name={}, id={}]", name, tblId), th);
            });
            tablesByIdVv.update(causalityToken, previous -> {
                var val = previous == null ? new HashMap() : new HashMap<>(previous);
                val.put(tblId, table);
                return val;
            }, th -> {
                throw new IgniteInternalException(IgniteStringFormatter.format("Cannot create a table [name={}, id={}]", name, tblId), th);
            });
            completeApiCreateFuture(table);
            fireEvent(TableEvent.CREATE, new TableEventParameters(causalityToken, table), null);
        } catch (Exception e) {
            fireEvent(TableEvent.CREATE, new TableEventParameters(causalityToken, tblId, name), e);
        }
    }).join();
}
Also used : TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) ConfigurationUtil.directProxy(org.apache.ignite.internal.configuration.util.ConfigurationUtil.directProxy) ConfigurationUtil.getByInternalId(org.apache.ignite.internal.configuration.util.ConfigurationUtil.getByInternalId) ExtendedTableChange(org.apache.ignite.internal.configuration.schema.ExtendedTableChange) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) Path(java.nio.file.Path) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) ExtendedTableView(org.apache.ignite.internal.configuration.schema.ExtendedTableView) IgniteComponent(org.apache.ignite.internal.manager.IgniteComponent) SchemaConfiguration(org.apache.ignite.internal.configuration.schema.SchemaConfiguration) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DEFAULT_DATA_REGION_NAME(org.apache.ignite.configuration.schemas.store.DataStorageConfigurationSchema.DEFAULT_DATA_REGION_NAME) CompletionException(java.util.concurrent.CompletionException) Producer(org.apache.ignite.internal.manager.Producer) StorageEngine(org.apache.ignite.internal.storage.engine.StorageEngine) UUID(java.util.UUID) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) Collectors(java.util.stream.Collectors) TxManager(org.apache.ignite.internal.tx.TxManager) TableView(org.apache.ignite.configuration.schemas.table.TableView) ClusterNode(org.apache.ignite.network.ClusterNode) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) InternalTable(org.apache.ignite.internal.table.InternalTable) IgniteException(org.apache.ignite.lang.IgniteException) Loza(org.apache.ignite.internal.raft.Loza) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) ByteUtils(org.apache.ignite.internal.util.ByteUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaSerializerImpl(org.apache.ignite.internal.schema.marshaller.schema.SchemaSerializerImpl) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SchemaView(org.apache.ignite.internal.configuration.schema.SchemaView) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteSpinBusyLock(org.apache.ignite.internal.util.IgniteSpinBusyLock) IgniteTables(org.apache.ignite.table.manager.IgniteTables) NoSuchElementException(java.util.NoSuchElementException) VersionedValue(org.apache.ignite.internal.causality.VersionedValue) TopologyService(org.apache.ignite.network.TopologyService) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) Files(java.nio.file.Files) NamedListView(org.apache.ignite.configuration.NamedListView) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IgniteObjectName(org.apache.ignite.internal.util.IgniteObjectName) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) BaselineManager(org.apache.ignite.internal.baseline.BaselineManager) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) ConfigurationNamedListListener(org.apache.ignite.configuration.notifications.ConfigurationNamedListListener) NetworkAddress(org.apache.ignite.network.NetworkAddress) Consumer(java.util.function.Consumer) ConfigurationNotificationEvent(org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent) TableEvent(org.apache.ignite.internal.table.event.TableEvent) AffinityUtils(org.apache.ignite.internal.affinity.AffinityUtils) EventListener(org.apache.ignite.internal.manager.EventListener) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) Table(org.apache.ignite.table.Table) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) RocksDbStorageEngine(org.apache.ignite.internal.storage.rocksdb.RocksDbStorageEngine) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) TableImpl(org.apache.ignite.internal.table.TableImpl) CompletableFuture(java.util.concurrent.CompletableFuture) IgniteException(org.apache.ignite.lang.IgniteException) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) Path(java.nio.file.Path) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) IOException(java.io.IOException) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) CompletionException(java.util.concurrent.CompletionException) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteException(org.apache.ignite.lang.IgniteException) NoSuchElementException(java.util.NoSuchElementException) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IOException(java.io.IOException) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion)

Example 7 with ClusterNode

use of org.apache.ignite.network.ClusterNode in project ignite-3 by apache.

the class TableManager method updateAssignments.

/**
 * Update assignments for all current tables according to input nodes list. These approach has known issues {@link
 * Ignite#setBaseline(Set)}.
 *
 * @param clusterNodes Set of nodes for assignment.
 */
private void updateAssignments(Set<ClusterNode> clusterNodes) {
    var setBaselineFut = new CompletableFuture<>();
    var changePeersQueue = new ArrayList<Supplier<CompletableFuture<Void>>>();
    tablesCfg.tables().change(tbls -> {
        changePeersQueue.clear();
        for (int i = 0; i < tbls.size(); i++) {
            tbls.createOrUpdate(tbls.get(i).name(), changeX -> {
                ExtendedTableChange change = (ExtendedTableChange) changeX;
                byte[] currAssignments = change.assignments();
                List<List<ClusterNode>> recalculatedAssignments = AffinityUtils.calculateAssignments(clusterNodes, change.partitions(), change.replicas());
                if (!recalculatedAssignments.equals(ByteUtils.fromBytes(currAssignments))) {
                    change.changeAssignments(ByteUtils.toBytes(recalculatedAssignments));
                    changePeersQueue.add(() -> updateRaftTopology((List<List<ClusterNode>>) ByteUtils.fromBytes(currAssignments), recalculatedAssignments, change.id()));
                }
            });
        }
    }).thenCompose((v) -> {
        CompletableFuture<?>[] changePeersFutures = new CompletableFuture<?>[changePeersQueue.size()];
        int i = 0;
        for (Supplier<CompletableFuture<Void>> task : changePeersQueue) {
            changePeersFutures[i++] = task.get();
        }
        return CompletableFuture.allOf(changePeersFutures);
    }).whenComplete((res, th) -> {
        if (th != null) {
            setBaselineFut.completeExceptionally(th);
        } else {
            setBaselineFut.complete(null);
        }
    });
    setBaselineFut.join();
}
Also used : ExtendedTableChange(org.apache.ignite.internal.configuration.schema.ExtendedTableChange) ClusterNode(org.apache.ignite.network.ClusterNode) IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) ConfigurationUtil.directProxy(org.apache.ignite.internal.configuration.util.ConfigurationUtil.directProxy) ConfigurationUtil.getByInternalId(org.apache.ignite.internal.configuration.util.ConfigurationUtil.getByInternalId) ExtendedTableChange(org.apache.ignite.internal.configuration.schema.ExtendedTableChange) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Map(java.util.Map) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) Path(java.nio.file.Path) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) ExtendedTableView(org.apache.ignite.internal.configuration.schema.ExtendedTableView) IgniteComponent(org.apache.ignite.internal.manager.IgniteComponent) SchemaConfiguration(org.apache.ignite.internal.configuration.schema.SchemaConfiguration) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DEFAULT_DATA_REGION_NAME(org.apache.ignite.configuration.schemas.store.DataStorageConfigurationSchema.DEFAULT_DATA_REGION_NAME) CompletionException(java.util.concurrent.CompletionException) Producer(org.apache.ignite.internal.manager.Producer) StorageEngine(org.apache.ignite.internal.storage.engine.StorageEngine) UUID(java.util.UUID) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) Collectors(java.util.stream.Collectors) TxManager(org.apache.ignite.internal.tx.TxManager) TableView(org.apache.ignite.configuration.schemas.table.TableView) ClusterNode(org.apache.ignite.network.ClusterNode) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) NotNull(org.jetbrains.annotations.NotNull) InternalTable(org.apache.ignite.internal.table.InternalTable) IgniteException(org.apache.ignite.lang.IgniteException) Loza(org.apache.ignite.internal.raft.Loza) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TableConfiguration(org.apache.ignite.configuration.schemas.table.TableConfiguration) ByteUtils(org.apache.ignite.internal.util.ByteUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaSerializerImpl(org.apache.ignite.internal.schema.marshaller.schema.SchemaSerializerImpl) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SchemaView(org.apache.ignite.internal.configuration.schema.SchemaView) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteSpinBusyLock(org.apache.ignite.internal.util.IgniteSpinBusyLock) IgniteTables(org.apache.ignite.table.manager.IgniteTables) NoSuchElementException(java.util.NoSuchElementException) VersionedValue(org.apache.ignite.internal.causality.VersionedValue) TopologyService(org.apache.ignite.network.TopologyService) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) Files(java.nio.file.Files) NamedListView(org.apache.ignite.configuration.NamedListView) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IgniteObjectName(org.apache.ignite.internal.util.IgniteObjectName) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) BaselineManager(org.apache.ignite.internal.baseline.BaselineManager) TableStorage(org.apache.ignite.internal.storage.engine.TableStorage) ConfigurationNamedListListener(org.apache.ignite.configuration.notifications.ConfigurationNamedListListener) NetworkAddress(org.apache.ignite.network.NetworkAddress) Consumer(java.util.function.Consumer) ConfigurationNotificationEvent(org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent) TableEvent(org.apache.ignite.internal.table.event.TableEvent) AffinityUtils(org.apache.ignite.internal.affinity.AffinityUtils) EventListener(org.apache.ignite.internal.manager.EventListener) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) Table(org.apache.ignite.table.Table) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) RocksDbStorageEngine(org.apache.ignite.internal.storage.rocksdb.RocksDbStorageEngine) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Supplier(java.util.function.Supplier)

Example 8 with ClusterNode

use of org.apache.ignite.network.ClusterNode 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;
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) BeforeEach(org.junit.jupiter.api.BeforeEach) Disabled(org.junit.jupiter.api.Disabled) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) SchemaConfigurationConverter(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter) Collection(java.util.Collection) DataStorageConfiguration(org.apache.ignite.configuration.schemas.store.DataStorageConfiguration) TablesConfiguration(org.apache.ignite.configuration.schemas.table.TablesConfiguration) UUID(java.util.UUID) ColumnType(org.apache.ignite.schema.definition.ColumnType) TxManager(org.apache.ignite.internal.tx.TxManager) ClusterNode(org.apache.ignite.network.ClusterNode) Test(org.junit.jupiter.api.Test) List(java.util.List) MockedStatic(org.mockito.MockedStatic) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) NotNull(org.jetbrains.annotations.NotNull) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Strictness(org.mockito.quality.Strictness) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) HashIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.HashIndexConfigurationSchema) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) IgniteException(org.apache.ignite.lang.IgniteException) Mock(org.mockito.Mock) Mockito.mockStatic(org.mockito.Mockito.mockStatic) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Loza(org.apache.ignite.internal.raft.Loza) CompletableFuture(java.util.concurrent.CompletableFuture) PartialIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.PartialIndexConfigurationSchema) SortedIndexConfigurationSchema(org.apache.ignite.configuration.schemas.table.SortedIndexConfigurationSchema) ArrayList(java.util.ArrayList) InjectConfiguration(org.apache.ignite.internal.configuration.testframework.InjectConfiguration) ConfigurationExtension(org.apache.ignite.internal.configuration.testframework.ConfigurationExtension) RocksDbDataRegionConfigurationSchema(org.apache.ignite.configuration.schemas.store.RocksDbDataRegionConfigurationSchema) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TableManager(org.apache.ignite.internal.table.distributed.TableManager) TopologyService(org.apache.ignite.network.TopologyService) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) ConfigurationStorageRevisionListenerHolder(org.apache.ignite.internal.configuration.notifications.ConfigurationStorageRevisionListenerHolder) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) BaselineManager(org.apache.ignite.internal.baseline.BaselineManager) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Mockito.verify(org.mockito.Mockito.verify) NetworkAddress(org.apache.ignite.network.NetworkAddress) Consumer(java.util.function.Consumer) LockManager(org.apache.ignite.internal.tx.LockManager) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ExtendedTableConfigurationSchema(org.apache.ignite.internal.configuration.schema.ExtendedTableConfigurationSchema) Phaser(java.util.concurrent.Phaser) AffinityUtils(org.apache.ignite.internal.affinity.AffinityUtils) InjectRevisionListenerHolder(org.apache.ignite.internal.configuration.testframework.InjectRevisionListenerHolder) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) Table(org.apache.ignite.table.Table) Collections(java.util.Collections) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) ArrayList(java.util.ArrayList) SchemaUtils(org.apache.ignite.internal.schema.SchemaUtils) NetworkAddress(org.apache.ignite.network.NetworkAddress) AffinityUtils(org.apache.ignite.internal.affinity.AffinityUtils) TableManager(org.apache.ignite.internal.table.distributed.TableManager) List(java.util.List) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull)

Example 9 with ClusterNode

use of org.apache.ignite.network.ClusterNode in project ignite-3 by apache.

the class RendezvousAffinityFunction method assignPartition.

/**
 * Returns collection of nodes for specified partition.
 *
 * @param part              Partition.
 * @param nodes             Nodes.
 * @param replicas          Number partition replicas.
 * @param neighborhoodCache Neighborhood.
 * @param exclNeighbors     If true neighbors are excluded, false otherwise.
 * @param nodeFilter        Filter for nodes.
 * @return Assignment.
 */
public static List<ClusterNode> assignPartition(int part, List<ClusterNode> nodes, int replicas, Map<String, Collection<ClusterNode>> neighborhoodCache, boolean exclNeighbors, BiPredicate<ClusterNode, List<ClusterNode>> nodeFilter) {
    if (nodes.size() <= 1) {
        return nodes;
    }
    IgniteBiTuple<Long, ClusterNode>[] hashArr = (IgniteBiTuple<Long, ClusterNode>[]) new IgniteBiTuple[nodes.size()];
    for (int i = 0; i < nodes.size(); i++) {
        ClusterNode node = nodes.get(i);
        Object nodeHash = resolveNodeHash(node);
        long hash = hash(nodeHash.hashCode(), part);
        hashArr[i] = new IgniteBiTuple<>(hash, node);
    }
    final int effectiveReplicas = replicas == Integer.MAX_VALUE ? nodes.size() : Math.min(replicas, nodes.size());
    Iterable<ClusterNode> sortedNodes = new LazyLinearSortedContainer(hashArr, effectiveReplicas);
    // REPLICATED cache case
    if (replicas == Integer.MAX_VALUE) {
        return replicatedAssign(nodes, sortedNodes);
    }
    Iterator<ClusterNode> it = sortedNodes.iterator();
    List<ClusterNode> res = new ArrayList<>(effectiveReplicas);
    Collection<ClusterNode> allNeighbors = new HashSet<>();
    ClusterNode first = it.next();
    res.add(first);
    if (exclNeighbors) {
        allNeighbors.addAll(neighborhoodCache.get(first.id()));
    }
    // Select another replicas.
    if (replicas > 1) {
        while (it.hasNext() && res.size() < effectiveReplicas) {
            ClusterNode node = it.next();
            if (exclNeighbors) {
                if (!allNeighbors.contains(node)) {
                    res.add(node);
                    allNeighbors.addAll(neighborhoodCache.get(node.id()));
                }
            } else if (nodeFilter == null || nodeFilter.test(node, res)) {
                res.add(node);
                if (exclNeighbors) {
                    allNeighbors.addAll(neighborhoodCache.get(node.id()));
                }
            }
        }
    }
    if (res.size() < effectiveReplicas && nodes.size() >= effectiveReplicas && exclNeighbors) {
        // Need to iterate again in case if there are no nodes which pass exclude neighbors replicas criteria.
        it = sortedNodes.iterator();
        it.next();
        while (it.hasNext() && res.size() < effectiveReplicas) {
            ClusterNode node = it.next();
            if (!res.contains(node)) {
                res.add(node);
            }
        }
        if (!exclNeighborsWarn) {
            LOG.warn("Affinity function excludeNeighbors property is ignored " + "because topology has no enough nodes to assign all replicas.");
            exclNeighborsWarn = true;
        }
    }
    assert res.size() <= effectiveReplicas;
    return res;
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 10 with ClusterNode

use of org.apache.ignite.network.ClusterNode in project ignite-3 by apache.

the class RendezvousAffinityFunctionTest method testPartitionDistribution.

@Test
public void testPartitionDistribution() {
    int nodes = 50;
    int parts = 10_000;
    int replicas = 4;
    List<ClusterNode> clusterNodes = prepareNetworkTopology(nodes);
    assertTrue(parts > nodes, "Partitions should be more that nodes");
    int ideal = (parts * replicas) / nodes;
    List<List<ClusterNode>> assignment = RendezvousAffinityFunction.assignPartitions(clusterNodes, parts, replicas, false, null);
    HashMap<ClusterNode, ArrayList<Integer>> assignmentByNode = new HashMap<>(nodes);
    int part = 0;
    for (List<ClusterNode> partNodes : assignment) {
        for (ClusterNode node : partNodes) {
            ArrayList<Integer> nodeParts = assignmentByNode.get(node);
            if (nodeParts == null) {
                assignmentByNode.put(node, nodeParts = new ArrayList<>());
            }
            nodeParts.add(part);
        }
        part++;
    }
    for (ClusterNode node : clusterNodes) {
        ArrayList<Integer> nodeParts = assignmentByNode.get(node);
        assertNotNull(nodeParts);
        assertTrue(nodeParts.size() > ideal * (1 - AFFINITY_DEVIATION_RATIO) && nodeParts.size() < ideal * (1 + AFFINITY_DEVIATION_RATIO), "Partition distribution is too far from ideal [node=" + node + ", size=" + nodeParts.size() + ", idealSize=" + ideal + ", parts=" + compact(nodeParts) + ']');
    }
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterNode (org.apache.ignite.network.ClusterNode)25 NetworkAddress (org.apache.ignite.network.NetworkAddress)11 List (java.util.List)10 ClusterService (org.apache.ignite.network.ClusterService)9 Loza (org.apache.ignite.internal.raft.Loza)8 TopologyService (org.apache.ignite.network.TopologyService)8 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)8 Test (org.junit.jupiter.api.Test)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)6 Map (java.util.Map)6 UUID (java.util.UUID)6 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)6 IgniteLogger (org.apache.ignite.lang.IgniteLogger)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 Consumer (java.util.function.Consumer)5 Collectors (java.util.stream.Collectors)5