Search in sources :

Example 6 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService 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)

Example 7 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.

the class InboundDecoderTest method testPartialHeader.

/**
 * Tests that an {@link InboundDecoder} doesn't hang if it encounters a byte buffer with only partially written header.
 *
 * @throws Exception If failed.
 */
@Test
public void testPartialHeader() throws Exception {
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var perSessionSerializationService = new PerSessionSerializationService(serializationService);
    var channel = new EmbeddedChannel(new InboundDecoder(perSessionSerializationService));
    ByteBuf buffer = allocator.buffer();
    buffer.writeByte(1);
    CompletableFuture.runAsync(() -> {
        channel.writeInbound(buffer);
        // In case if a header was written partially, readInbound() should not loop forever, as
        // there might not be new data anymore (example: remote host gone offline).
        channel.readInbound();
    }).get(3, TimeUnit.SECONDS);
}
Also used : PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) ByteBuf(io.netty.buffer.ByteBuf) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with SerializationService

use of org.apache.ignite.internal.network.serialization.SerializationService in project ignite-3 by apache.

the class InboundDecoderTest method sendAndReceive.

/**
 * Serializes and then deserializes the given message.
 */
private <T extends NetworkMessage> T sendAndReceive(T msg) {
    var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
    var perSessionSerializationService = new PerSessionSerializationService(serializationService);
    var channel = new EmbeddedChannel(new InboundDecoder(perSessionSerializationService));
    var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
    MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
    ByteBuffer buf = ByteBuffer.allocate(10_000);
    T received;
    do {
        buf.clear();
        writer.setBuffer(buf);
        serializer.writeMessage(msg, writer);
        buf.flip();
        ByteBuf buffer = allocator.buffer(buf.limit());
        buffer.writeBytes(buf);
        channel.writeInbound(buffer);
    } while ((received = channel.readInbound()) == null);
    return received;
}
Also used : DirectMessageWriter(org.apache.ignite.internal.network.direct.DirectMessageWriter) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) SerializationService(org.apache.ignite.internal.network.serialization.SerializationService) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) PerSessionSerializationService(org.apache.ignite.internal.network.serialization.PerSessionSerializationService) NetworkMessage(org.apache.ignite.network.NetworkMessage) ByteBuf(io.netty.buffer.ByteBuf) UserObjectSerializationContext(org.apache.ignite.internal.network.serialization.UserObjectSerializationContext) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SerializationService (org.apache.ignite.internal.network.serialization.SerializationService)8 UserObjectSerializationContext (org.apache.ignite.internal.network.serialization.UserObjectSerializationContext)7 NettyBootstrapFactory (org.apache.ignite.network.NettyBootstrapFactory)5 ByteBuf (io.netty.buffer.ByteBuf)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 UUID (java.util.UUID)4 NetworkView (org.apache.ignite.configuration.schemas.network.NetworkView)4 NetworkMessagesFactory (org.apache.ignite.internal.network.NetworkMessagesFactory)4 ConnectionManager (org.apache.ignite.internal.network.netty.ConnectionManager)3 PerSessionSerializationService (org.apache.ignite.internal.network.serialization.PerSessionSerializationService)3 NetworkMessage (org.apache.ignite.network.NetworkMessage)3 ByteBuffer (java.nio.ByteBuffer)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 NetworkConfiguration (org.apache.ignite.configuration.schemas.network.NetworkConfiguration)2 DirectMessageWriter (org.apache.ignite.internal.network.direct.DirectMessageWriter)2 RecoveryClientHandshakeManager (org.apache.ignite.internal.network.recovery.RecoveryClientHandshakeManager)2 RecoveryServerHandshakeManager (org.apache.ignite.internal.network.recovery.RecoveryServerHandshakeManager)2 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)2 TestMessageSerializationRegistryImpl (org.apache.ignite.network.TestMessageSerializationRegistryImpl)2