Search in sources :

Example 1 with ClusterImpl

use of io.scalecube.cluster.ClusterImpl in project alibaba-rsocket-broker by alibaba.

the class RSocketBrokerManagerGossipImpl method start.

@Override
public void start() {
    final String localIp = NetworkUtil.LOCAL_IP;
    monoCluster = new ClusterImpl().config(clusterConfig -> clusterConfig.externalHost(localIp).externalPort(gossipListenPort)).membership(membershipConfig -> membershipConfig.seedMembers(seedMembers()).syncInterval(5_000)).transportFactory(TcpTransportFactory::new).transport(transportConfig -> transportConfig.port(gossipListenPort)).handler(cluster1 -> this).start();
    // subscribe and start & join the cluster
    monoCluster.subscribe();
    this.localBroker = new RSocketBroker(localIp, brokerProperties.getExternalDomain());
    this.consistentHash = new KetamaConsistentHash<>(12, Collections.singletonList(localIp));
    brokers.put(localIp, localBroker);
    log.info(RsocketErrorCode.message("RST-300002"));
    Metrics.globalRegistry.gauge("cluster.broker.count", this, (DoubleFunction<RSocketBrokerManagerGossipImpl>) brokerManagerGossip -> brokerManagerGossip.brokers.size());
    this.status = 1;
}
Also used : ClusterImpl(io.scalecube.cluster.ClusterImpl) Sinks(reactor.core.publisher.Sinks) java.util(java.util) Json(com.alibaba.rsocket.cloudevents.Json) Cluster(io.scalecube.cluster.Cluster) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JsonRpcResponse(com.alibaba.spring.boot.rsocket.broker.cluster.jsonrpc.JsonRpcResponse) Metrics(io.micrometer.core.instrument.Metrics) Value(org.springframework.beans.factory.annotation.Value) Shutdown(org.springframework.boot.web.server.Shutdown) CloudEventImpl(com.alibaba.rsocket.cloudevents.CloudEventImpl) RSocketFilterEnableEvent(com.alibaba.spring.boot.rsocket.broker.events.RSocketFilterEnableEvent) Message(io.scalecube.cluster.transport.api.Message) JsonRpcRequest(com.alibaba.spring.boot.rsocket.broker.cluster.jsonrpc.JsonRpcRequest) ClusterImpl(io.scalecube.cluster.ClusterImpl) ServerProperties(org.springframework.boot.autoconfigure.web.ServerProperties) DoubleFunction(org.eclipse.collections.api.block.function.primitive.DoubleFunction) Address(io.scalecube.net.Address) Logger(org.slf4j.Logger) RsocketErrorCode(com.alibaba.rsocket.observability.RsocketErrorCode) Mono(reactor.core.publisher.Mono) GracefulShutdownResult(org.springframework.boot.web.server.GracefulShutdownResult) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) SmartLifecycle(org.springframework.context.SmartLifecycle) RSocketFilter(com.alibaba.rsocket.route.RSocketFilter) Nullable(org.jetbrains.annotations.Nullable) Flux(reactor.core.publisher.Flux) Stream(java.util.stream.Stream) RSocketBrokerProperties(com.alibaba.spring.boot.rsocket.broker.RSocketBrokerProperties) Member(io.scalecube.cluster.Member) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) NetworkUtil(com.alibaba.rsocket.transport.NetworkUtil) TcpTransportFactory(io.scalecube.transport.netty.tcp.TcpTransportFactory) ClusterMessageHandler(io.scalecube.cluster.ClusterMessageHandler) NotNull(org.jetbrains.annotations.NotNull) ServiceLocator(com.alibaba.rsocket.ServiceLocator) GracefulShutdownCallback(org.springframework.boot.web.server.GracefulShutdownCallback) TcpTransportFactory(io.scalecube.transport.netty.tcp.TcpTransportFactory)

Example 2 with ClusterImpl

use of io.scalecube.cluster.ClusterImpl in project ignite-3 by apache.

the class ItScaleCubeNetworkMessagingTest method stopForcefully.

/**
 * Find the cluster's transport and force it to stop.
 *
 * @param cluster Cluster to be shutdown.
 * @throws Exception If failed to stop.
 */
private static void stopForcefully(ClusterService cluster) throws Exception {
    Field clusterSvcImplField = cluster.getClass().getDeclaredField("val$clusterSvc");
    clusterSvcImplField.setAccessible(true);
    ClusterService innerClusterSvc = (ClusterService) clusterSvcImplField.get(cluster);
    Field clusterImplField = innerClusterSvc.getClass().getDeclaredField("cluster");
    clusterImplField.setAccessible(true);
    ClusterImpl clusterImpl = (ClusterImpl) clusterImplField.get(innerClusterSvc);
    Field transportField = clusterImpl.getClass().getDeclaredField("transport");
    transportField.setAccessible(true);
    Transport transport = (Transport) transportField.get(clusterImpl);
    Method stop = transport.getClass().getDeclaredMethod("stop");
    stop.setAccessible(true);
    Mono<?> invoke = (Mono<?>) stop.invoke(transport);
    invoke.block();
}
Also used : ClusterImpl(io.scalecube.cluster.ClusterImpl) Field(java.lang.reflect.Field) ClusterService(org.apache.ignite.network.ClusterService) Mono(reactor.core.publisher.Mono) Method(java.lang.reflect.Method) Transport(io.scalecube.cluster.transport.api.Transport)

Example 3 with ClusterImpl

use of io.scalecube.cluster.ClusterImpl 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();
        }
    };
}
Also used : ClusterImpl(io.scalecube.cluster.ClusterImpl) DefaultUserObjectMarshaller(org.apache.ignite.internal.network.serialization.marshal.DefaultUserObjectMarshaller) NettyBootstrapFactory(org.apache.ignite.network.NettyBootstrapFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) ClassDescriptorRegistry(org.apache.ignite.internal.network.serialization.ClassDescriptorRegistry) RecoveryClientHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager) ClusterConfig(io.scalecube.cluster.ClusterConfig) ClusterMembershipView(org.apache.ignite.configuration.schemas.network.ClusterMembershipView) RecoveryServerHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager) NetworkConfiguration(org.apache.ignite.configuration.schemas.network.NetworkConfiguration) ScaleCubeView(org.apache.ignite.configuration.schemas.network.ScaleCubeView) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) ClusterImpl(io.scalecube.cluster.ClusterImpl) DefaultMessagingService(org.apache.ignite.network.DefaultMessagingService) NodeFinder(org.apache.ignite.network.NodeFinder) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView) Address(io.scalecube.net.Address) NodeFinderFactory(org.apache.ignite.network.NodeFinderFactory) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) ClassDescriptorFactory(org.apache.ignite.internal.network.serialization.ClassDescriptorFactory) ClusterLocalConfiguration(org.apache.ignite.network.ClusterLocalConfiguration) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) AbstractClusterService(org.apache.ignite.network.AbstractClusterService) ClusterService(org.apache.ignite.network.ClusterService) ClusterMessageHandler(io.scalecube.cluster.ClusterMessageHandler) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) MembershipEvent(io.scalecube.cluster.membership.MembershipEvent) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionManager(org.apache.ignite.internal.network.netty.ConnectionManager) NetworkMessagesFactory(org.apache.ignite.internal.network.NetworkMessagesFactory) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) TimeoutException(java.util.concurrent.TimeoutException) ClusterMessageHandler(io.scalecube.cluster.ClusterMessageHandler) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) DefaultMessagingService(org.apache.ignite.network.DefaultMessagingService) AbstractClusterService(org.apache.ignite.network.AbstractClusterService) NetworkView(org.apache.ignite.configuration.schemas.network.NetworkView) NodeFinder(org.apache.ignite.network.NodeFinder) RecoveryClientHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager) RecoveryServerHandshakeManager(org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager)

Aggregations

ClusterImpl (io.scalecube.cluster.ClusterImpl)3 ClusterMessageHandler (io.scalecube.cluster.ClusterMessageHandler)2 MembershipEvent (io.scalecube.cluster.membership.MembershipEvent)2 Address (io.scalecube.net.Address)2 Collectors (java.util.stream.Collectors)2 ClusterService (org.apache.ignite.network.ClusterService)2 Mono (reactor.core.publisher.Mono)2 ServiceLocator (com.alibaba.rsocket.ServiceLocator)1 CloudEventImpl (com.alibaba.rsocket.cloudevents.CloudEventImpl)1 Json (com.alibaba.rsocket.cloudevents.Json)1 RsocketErrorCode (com.alibaba.rsocket.observability.RsocketErrorCode)1 RSocketFilter (com.alibaba.rsocket.route.RSocketFilter)1 NetworkUtil (com.alibaba.rsocket.transport.NetworkUtil)1 RSocketBrokerProperties (com.alibaba.spring.boot.rsocket.broker.RSocketBrokerProperties)1 JsonRpcRequest (com.alibaba.spring.boot.rsocket.broker.cluster.jsonrpc.JsonRpcRequest)1 JsonRpcResponse (com.alibaba.spring.boot.rsocket.broker.cluster.jsonrpc.JsonRpcResponse)1 RSocketFilterEnableEvent (com.alibaba.spring.boot.rsocket.broker.events.RSocketFilterEnableEvent)1 Metrics (io.micrometer.core.instrument.Metrics)1 Cluster (io.scalecube.cluster.Cluster)1 ClusterConfig (io.scalecube.cluster.ClusterConfig)1