Search in sources :

Example 16 with ClusterNode

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

the class ItSimpleCounterServerTest method before.

/**
 * Before each.
 */
@BeforeEach
void before() throws Exception {
    LOG.info(">>>> Starting test {}", testInfo.getTestMethod().orElseThrow().getName());
    var addr = new NetworkAddress("localhost", PORT);
    ClusterService service = clusterService(PORT, List.of(addr), true);
    server = new JraftServerImpl(service, dataPath) {

        @Override
        public synchronized void stop() {
            super.stop();
            service.stop();
        }
    };
    server.start();
    ClusterNode serverNode = server.clusterService().topologyService().localMember();
    assertTrue(server.startRaftGroup(COUNTER_GROUP_ID_0, new CounterListener(), List.of(new Peer(serverNode.address()))));
    assertTrue(server.startRaftGroup(COUNTER_GROUP_ID_1, new CounterListener(), List.of(new Peer(serverNode.address()))));
    ClusterService clientNode1 = clusterService(PORT + 1, List.of(addr), true);
    executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
    client1 = RaftGroupServiceImpl.start(COUNTER_GROUP_ID_0, clientNode1, FACTORY, 1000, List.of(new Peer(serverNode.address())), false, 200, executor).get(3, TimeUnit.SECONDS);
    ClusterService clientNode2 = clusterService(PORT + 2, List.of(addr), true);
    client2 = RaftGroupServiceImpl.start(COUNTER_GROUP_ID_1, clientNode2, FACTORY, 1000, List.of(new Peer(serverNode.address())), false, 200, executor).get(3, TimeUnit.SECONDS);
    assertTrue(waitForTopology(service, 3, 1000));
    assertTrue(waitForTopology(clientNode1, 3, 1000));
    assertTrue(waitForTopology(clientNode2, 3, 1000));
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) Peer(org.apache.ignite.raft.client.Peer) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 17 with ClusterNode

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

the class JraftServerImpl method getServerDataPath.

/**
 * Returns path to persistence folder.
 *
 * @param groupId Group id.
 * @return The path to persistence folder.
 */
public Path getServerDataPath(String groupId) {
    ClusterNode clusterNode = service.topologyService().localMember();
    String dirName = groupId + "_" + clusterNode.address().toString().replace(':', '_');
    return this.dataPath.resolve(dirName);
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode)

Example 18 with ClusterNode

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

the class ScaleCubeDirectMarshallerTransport method send.

/**
 * {@inheritDoc}
 */
@Override
public Mono<Void> send(Address address, Message message) {
    var addr = InetSocketAddress.createUnresolved(address.host(), address.port());
    return Mono.fromFuture(() -> {
        NetworkAddress networkAddress = NetworkAddress.from(addr);
        ClusterNode node = topologyService.getByAddress(networkAddress);
        if (node == null) {
            node = new ClusterNode(null, null, networkAddress);
        }
        return messagingService.send(node, fromMessage(message));
    });
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) NetworkAddress(org.apache.ignite.network.NetworkAddress)

Example 19 with ClusterNode

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

the class ScaleCubeTopologyService method onMembershipEvent.

/**
 * Delegates the received topology event to the registered event handlers.
 *
 * @param event Membership event.
 */
void onMembershipEvent(MembershipEvent event) {
    ClusterNode member = fromMember(event.member());
    if (event.isAdded()) {
        members.put(member.address(), member);
        consistentIdToMemberMap.put(member.name(), member);
        LOG.info("Node joined: " + member);
        fireAppearedEvent(member);
    } else if (event.isRemoved()) {
        members.compute(member.address(), (addr, node) -> {
            // Ignore stale remove event.
            if (node == null || node.id().equals(member.id())) {
                return null;
            } else {
                return node;
            }
        });
        consistentIdToMemberMap.compute(member.name(), (consId, node) -> {
            // Ignore stale remove event.
            if (node == null || node.id().equals(member.id())) {
                return null;
            } else {
                return node;
            }
        });
        LOG.info("Node left: " + member);
        fireDisappearedEvent(member);
    }
    if (LOG.isInfoEnabled()) {
        StringBuilder snapshotMsg = new StringBuilder("Topology snapshot [nodes=").append(members.size()).append("]\n");
        for (ClusterNode node : members.values()) {
            snapshotMsg.append("  ^-- ").append(node).append('\n');
        }
        LOG.info(snapshotMsg.toString().trim());
    }
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) TopologyService(org.apache.ignite.network.TopologyService) Cluster(io.scalecube.cluster.Cluster) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteLogger(org.apache.ignite.lang.IgniteLogger) AbstractTopologyService(org.apache.ignite.network.AbstractTopologyService) ConcurrentMap(java.util.concurrent.ConcurrentMap) ClusterNode(org.apache.ignite.network.ClusterNode) NetworkAddress(org.apache.ignite.network.NetworkAddress) TopologyEventHandler(org.apache.ignite.network.TopologyEventHandler) Member(io.scalecube.cluster.Member) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) Collections(java.util.Collections)

Example 20 with ClusterNode

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

the class TableManager method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() {
    tablesCfg.tables().listenElements(new ConfigurationNamedListListener<>() {

        @Override
        public CompletableFuture<?> onCreate(ConfigurationNotificationEvent<TableView> ctx) {
            if (!busyLock.enterBusy()) {
                String tblName = ctx.newValue().name();
                UUID tblId = ((ExtendedTableView) ctx.newValue()).id();
                fireEvent(TableEvent.CREATE, new TableEventParameters(ctx.storageRevision(), tblId, tblName), new NodeStoppingException());
                return CompletableFuture.failedFuture(new NodeStoppingException());
            }
            try {
                onTableCreateInternal(ctx);
            } finally {
                busyLock.leaveBusy();
            }
            return CompletableFuture.completedFuture(null);
        }

        /**
         * Method for handle a table configuration event.
         *
         * @param ctx Configuration event.
         */
        private void onTableCreateInternal(ConfigurationNotificationEvent<TableView> ctx) {
            String tblName = ctx.newValue().name();
            UUID tblId = ((ExtendedTableView) ctx.newValue()).id();
            // configuration, which is not supported now.
            assert ((ExtendedTableView) ctx.newValue()).assignments() != null : IgniteStringFormatter.format("Table [id={}, name={}] has empty assignments.", tblId, tblName);
            // TODO: IGNITE-16369 Listener with any placeholder should be used instead.
            ((ExtendedTableConfiguration) tablesCfg.tables().get(tblName)).schemas().listenElements(new ConfigurationNamedListListener<>() {

                @Override
                public CompletableFuture<?> onCreate(ConfigurationNotificationEvent<SchemaView> schemasCtx) {
                    long causalityToken = schemasCtx.storageRevision();
                    if (!busyLock.enterBusy()) {
                        fireEvent(TableEvent.ALTER, new TableEventParameters(causalityToken, tblId, tblName), new NodeStoppingException());
                        return CompletableFuture.failedFuture(new NodeStoppingException());
                    }
                    try {
                        // FIXME: https://issues.apache.org/jira/browse/IGNITE-16369
                        if (ctx.storageRevision() != schemasCtx.storageRevision()) {
                            return tablesByIdVv.get(causalityToken).thenAccept(tablesById -> {
                                TableImpl table = tablesById.get(tblId);
                                ((SchemaRegistryImpl) table.schemaView()).onSchemaRegistered(SchemaSerializerImpl.INSTANCE.deserialize((schemasCtx.newValue().schema())));
                                fireEvent(TableEvent.ALTER, new TableEventParameters(causalityToken, table), null);
                            });
                        }
                        return CompletableFuture.completedFuture(null);
                    } catch (Exception e) {
                        fireEvent(TableEvent.ALTER, new TableEventParameters(causalityToken, tblId, tblName), e);
                        return CompletableFuture.failedFuture(e);
                    } finally {
                        busyLock.leaveBusy();
                    }
                }
            });
            ((ExtendedTableConfiguration) tablesCfg.tables().get(tblName)).assignments().listen(assignmentsCtx -> {
                if (!busyLock.enterBusy()) {
                    return CompletableFuture.failedFuture(new NodeStoppingException());
                }
                try {
                    // FIXME: https://issues.apache.org/jira/browse/IGNITE-16369
                    if (ctx.storageRevision() == assignmentsCtx.storageRevision()) {
                        return CompletableFuture.completedFuture(null);
                    } else {
                        return updateAssignmentInternal(assignmentsCtx.storageRevision(), tblId, assignmentsCtx);
                    }
                } finally {
                    busyLock.leaveBusy();
                }
            });
            createTableLocally(ctx.storageRevision(), tblName, tblId, (List<List<ClusterNode>>) ByteUtils.fromBytes(((ExtendedTableView) ctx.newValue()).assignments()), SchemaSerializerImpl.INSTANCE.deserialize(((ExtendedTableView) ctx.newValue()).schemas().get(String.valueOf(INITIAL_SCHEMA_VERSION)).schema()));
        }

        private CompletableFuture<?> updateAssignmentInternal(long causalityToken, UUID tblId, ConfigurationNotificationEvent<byte[]> assignmentsCtx) {
            List<List<ClusterNode>> oldAssignments = (List<List<ClusterNode>>) ByteUtils.fromBytes(assignmentsCtx.oldValue());
            List<List<ClusterNode>> newAssignments = (List<List<ClusterNode>>) ByteUtils.fromBytes(assignmentsCtx.newValue());
            CompletableFuture<?>[] futures = new CompletableFuture<?>[oldAssignments.size()];
            // TODO: be exact same amount of partitions and replicas for both old and new assignments
            for (int i = 0; i < oldAssignments.size(); i++) {
                int partId = i;
                List<ClusterNode> oldPartitionAssignment = oldAssignments.get(partId);
                List<ClusterNode> newPartitionAssignment = newAssignments.get(partId);
                var toAdd = new HashSet<>(newPartitionAssignment);
                toAdd.removeAll(oldPartitionAssignment);
                // Create new raft nodes according to new assignments.
                futures[i] = tablesByIdVv.get(causalityToken).thenCompose(tablesById -> {
                    InternalTable internalTable = tablesById.get(tblId).internalTable();
                    try {
                        return raftMgr.updateRaftGroup(raftGroupName(tblId, partId), newPartitionAssignment, toAdd, () -> new PartitionListener(tblId, new VersionedRowStore(internalTable.storage().getOrCreatePartition(partId), txManager))).thenAccept(updatedRaftGroupService -> ((InternalTableImpl) internalTable).updateInternalTableRaftGroupService(partId, updatedRaftGroupService)).exceptionally(th -> {
                            LOG.error("Failed to update raft groups one the node", th);
                            return null;
                        });
                    } catch (NodeStoppingException e) {
                        throw new AssertionError("Loza was stopped before Table manager", e);
                    }
                });
            }
            return CompletableFuture.allOf(futures);
        }

        @Override
        public CompletableFuture<?> onRename(String oldName, String newName, ConfigurationNotificationEvent<TableView> ctx) {
            return CompletableFuture.completedFuture(null);
        }

        @Override
        public CompletableFuture<?> onDelete(ConfigurationNotificationEvent<TableView> ctx) {
            if (!busyLock.enterBusy()) {
                String tblName = ctx.oldValue().name();
                UUID tblId = ((ExtendedTableView) ctx.oldValue()).id();
                fireEvent(TableEvent.DROP, new TableEventParameters(ctx.storageRevision(), tblId, tblName), new NodeStoppingException());
                return CompletableFuture.failedFuture(new NodeStoppingException());
            }
            try {
                dropTableLocally(ctx.storageRevision(), ctx.oldValue().name(), ((ExtendedTableView) ctx.oldValue()).id(), (List<List<ClusterNode>>) ByteUtils.fromBytes(((ExtendedTableView) ctx.oldValue()).assignments()));
            } finally {
                busyLock.leaveBusy();
            }
            return CompletableFuture.completedFuture(null);
        }
    });
    engine.start();
    DataRegion defaultDataRegion = engine.createDataRegion(dataStorageCfg.defaultRegion());
    dataRegions.put(DEFAULT_DATA_REGION_NAME, defaultDataRegion);
    defaultDataRegion.start();
}
Also used : 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) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) TableImpl(org.apache.ignite.internal.table.TableImpl) ExtendedTableConfiguration(org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationNamedListListener(org.apache.ignite.configuration.notifications.ConfigurationNamedListListener) ConfigurationNotificationEvent(org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) ExtendedTableView(org.apache.ignite.internal.configuration.schema.ExtendedTableView) TableView(org.apache.ignite.configuration.schemas.table.TableView) ClusterNode(org.apache.ignite.network.ClusterNode) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) ExtendedTableView(org.apache.ignite.internal.configuration.schema.ExtendedTableView) SchemaRegistryImpl(org.apache.ignite.internal.schema.registry.SchemaRegistryImpl) InternalTable(org.apache.ignite.internal.table.InternalTable) 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) TableEventParameters(org.apache.ignite.internal.table.event.TableEventParameters) PartitionListener(org.apache.ignite.internal.table.distributed.raft.PartitionListener) InternalTableImpl(org.apache.ignite.internal.table.distributed.storage.InternalTableImpl) DataRegion(org.apache.ignite.internal.storage.engine.DataRegion)

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