Search in sources :

Example 6 with DirectMessageWriter

use of org.apache.ignite.internal.direct.DirectMessageWriter in project ignite by apache.

the class GridIoManager method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws IgniteCheckedException {
    ctx.addNodeAttribute(DIRECT_PROTO_VER_ATTR, DIRECT_PROTO_VER);
    MessageFormatter[] formatterExt = ctx.plugins().extensions(MessageFormatter.class);
    if (formatterExt != null && formatterExt.length > 0) {
        if (formatterExt.length > 1)
            throw new IgniteCheckedException("More than one MessageFormatter extension is defined. Check your " + "plugins configuration and make sure that only one of them provides custom message format.");
        formatter = formatterExt[0];
    } else {
        formatter = new MessageFormatter() {

            @Override
            public MessageWriter writer(UUID rmtNodeId) throws IgniteCheckedException {
                assert rmtNodeId != null;
                return new DirectMessageWriter(U.directProtocolVersion(ctx, rmtNodeId));
            }

            @Override
            public MessageReader reader(UUID rmtNodeId, MessageFactory msgFactory) throws IgniteCheckedException {
                return new DirectMessageReader(msgFactory, rmtNodeId != null ? U.directProtocolVersion(ctx, rmtNodeId) : DIRECT_PROTO_VER);
            }
        };
    }
    MessageFactory[] msgs = ctx.plugins().extensions(MessageFactory.class);
    if (msgs == null)
        msgs = EMPTY;
    List<MessageFactory> compMsgs = new ArrayList<>();
    compMsgs.add(new GridIoMessageFactory());
    for (IgniteComponentType compType : IgniteComponentType.values()) {
        MessageFactory f = compType.messageFactory();
        if (f != null)
            compMsgs.add(f);
    }
    if (!compMsgs.isEmpty())
        msgs = F.concat(msgs, compMsgs.toArray(new MessageFactory[compMsgs.size()]));
    msgFactory = new IgniteMessageFactoryImpl(msgs);
    CommunicationSpi<Serializable> spi = getSpi();
    if ((CommunicationSpi<?>) spi instanceof TcpCommunicationSpi)
        getTcpCommunicationSpi().setConnectionRequestor(invConnHandler);
    startSpi();
    MetricRegistry ioMetric = ctx.metric().registry(COMM_METRICS);
    ioMetric.register(OUTBOUND_MSG_QUEUE_CNT, spi::getOutboundMessagesQueueSize, "Outbound messages queue size.");
    ioMetric.register(SENT_MSG_CNT, spi::getSentMessagesCount, "Sent messages count.");
    ioMetric.register(SENT_BYTES_CNT, spi::getSentBytesCount, "Sent bytes count.");
    ioMetric.register(RCVD_MSGS_CNT, spi::getReceivedMessagesCount, "Received messages count.");
    ioMetric.register(RCVD_BYTES_CNT, spi::getReceivedBytesCount, "Received bytes count.");
    getSpi().setListener(commLsnr = new CommunicationListenerEx<Serializable>() {

        @Override
        public void onMessage(UUID nodeId, Serializable msg, IgniteRunnable msgC) {
            try {
                onMessage0(nodeId, (GridIoMessage) msg, msgC);
            } catch (ClassCastException ignored) {
                U.error(log, "Communication manager received message of unknown type (will ignore): " + msg.getClass().getName() + ". Most likely GridCommunicationSpi is being used directly, " + "which is illegal - make sure to send messages only via GridProjection API.");
            }
        }

        @Override
        public void onDisconnected(UUID nodeId) {
            for (GridDisconnectListener lsnr : disconnectLsnrs) lsnr.onNodeDisconnected(nodeId);
        }

        @Override
        public void onChannelOpened(UUID rmtNodeId, Serializable initMsg, Channel channel) {
            try {
                onChannelOpened0(rmtNodeId, (GridIoMessage) initMsg, channel);
            } catch (ClassCastException ignored) {
                U.error(log, "Communication manager received message of unknown type (will ignore): " + initMsg.getClass().getName() + ". Most likely GridCommunicationSpi is being used directly, " + "which is illegal - make sure to send messages only via GridProjection API.");
            }
        }
    });
    if (log.isDebugEnabled())
        log.debug(startInfo());
    addMessageListener(GridTopic.TOPIC_IO_TEST, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            ClusterNode node = ctx.discovery().node(nodeId);
            if (node == null)
                return;
            IgniteIoTestMessage msg0 = (IgniteIoTestMessage) msg;
            msg0.senderNodeId(nodeId);
            if (msg0.request()) {
                IgniteIoTestMessage res = new IgniteIoTestMessage(msg0.id(), false, null);
                res.flags(msg0.flags());
                res.onRequestProcessed();
                res.copyDataFromRequest(msg0);
                try {
                    sendToGridTopic(node, GridTopic.TOPIC_IO_TEST, res, GridIoPolicy.SYSTEM_POOL);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to send IO test response [msg=" + msg0 + "]", e);
                }
            } else {
                IoTestFuture fut = ioTestMap().get(msg0.id());
                msg0.onResponseProcessed();
                if (fut == null)
                    U.warn(log, "Failed to find IO test future [msg=" + msg0 + ']');
                else
                    fut.onResponse(msg0);
            }
        }
    });
}
Also used : Serializable(java.io.Serializable) DirectMessageReader(org.apache.ignite.internal.direct.DirectMessageReader) ArrayList(java.util.ArrayList) DirectMessageReader(org.apache.ignite.internal.direct.DirectMessageReader) MessageReader(org.apache.ignite.plugin.extensions.communication.MessageReader) MessageFormatter(org.apache.ignite.plugin.extensions.communication.MessageFormatter) TcpCommunicationSpi(org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteComponentType(org.apache.ignite.internal.IgniteComponentType) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) CommunicationListenerEx(org.apache.ignite.spi.communication.tcp.internal.CommunicationListenerEx) MessageFactory(org.apache.ignite.plugin.extensions.communication.MessageFactory) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) Channel(java.nio.channels.Channel) WritableByteChannel(java.nio.channels.WritableByteChannel) SocketChannel(java.nio.channels.SocketChannel) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) MessageWriter(org.apache.ignite.plugin.extensions.communication.MessageWriter) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject)

Example 7 with DirectMessageWriter

use of org.apache.ignite.internal.direct.DirectMessageWriter in project ignite by apache.

the class IgniteCacheContinuousQueryImmutableEntryTest method testCacheContinuousQueryEntrySerialization.

/**
 */
@Test
public void testCacheContinuousQueryEntrySerialization() {
    CacheContinuousQueryEntry e0 = new CacheContinuousQueryEntry(1, EventType.UPDATED, new KeyCacheObjectImpl(1, new byte[] { 0, 0, 0, 1 }, 1), new CacheObjectImpl(2, new byte[] { 0, 0, 0, 2 }), new CacheObjectImpl(2, new byte[] { 0, 0, 0, 3 }), true, 1, 1L, new AffinityTopologyVersion(1L), (byte) 0);
    e0.markFiltered();
    ByteBuffer buf = ByteBuffer.allocate(4096);
    DirectMessageWriter writer = new DirectMessageWriter((byte) 1);
    // Skip write class header.
    writer.onHeaderWritten();
    e0.writeTo(buf, writer);
    CacheContinuousQueryEntry e1 = new CacheContinuousQueryEntry();
    IgniteMessageFactoryImpl msgFactory = new IgniteMessageFactoryImpl(new MessageFactory[] { new GridIoMessageFactory() });
    e1.readFrom(ByteBuffer.wrap(buf.array()), new DirectMessageReader(msgFactory, (byte) 1));
    assertEquals(e0.cacheId(), e1.cacheId());
    assertEquals(e0.eventType(), e1.eventType());
    assertEquals(e0.isFiltered(), e1.isFiltered());
    assertEquals(e0.isBackup(), e1.isBackup());
    assertEquals(e0.isKeepBinary(), e1.isKeepBinary());
    assertEquals(e0.partition(), e1.partition());
    assertEquals(e0.updateCounter(), e1.updateCounter());
    // Key and value shouldn't be serialized in case an event is filtered.
    assertNull(e1.key());
    assertNotNull(e0.key());
    assertNull(e1.oldValue());
    assertNotNull(e0.oldValue());
    assertNull(e1.value());
    assertNotNull(e0.value());
}
Also used : IgniteMessageFactoryImpl(org.apache.ignite.internal.managers.communication.IgniteMessageFactoryImpl) DirectMessageReader(org.apache.ignite.internal.direct.DirectMessageReader) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) CacheObjectImpl(org.apache.ignite.internal.processors.cache.CacheObjectImpl) GridIoMessageFactory(org.apache.ignite.internal.managers.communication.GridIoMessageFactory) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) ByteBuffer(java.nio.ByteBuffer) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with DirectMessageWriter

use of org.apache.ignite.internal.direct.DirectMessageWriter in project ignite by apache.

the class GridLongListSelfTest method testSerializationInsufficientBuffer.

/**
 */
@Test
public void testSerializationInsufficientBuffer() {
    MessageWriter writer = new DirectMessageWriter(GridIoManager.DIRECT_PROTO_VER);
    ByteBuffer buf = ByteBuffer.allocate(10);
    GridLongList ll = new GridLongList(new long[] { 1L, 2L, 3L });
    Assert.assertFalse(ll.writeTo(buf, writer));
    Assert.assertEquals(10, buf.position());
}
Also used : DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) GridLongList(org.apache.ignite.internal.util.GridLongList) DirectMessageWriter(org.apache.ignite.internal.direct.DirectMessageWriter) MessageWriter(org.apache.ignite.plugin.extensions.communication.MessageWriter) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

DirectMessageWriter (org.apache.ignite.internal.direct.DirectMessageWriter)8 MessageWriter (org.apache.ignite.plugin.extensions.communication.MessageWriter)7 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.Test)6 GridLongList (org.apache.ignite.internal.util.GridLongList)5 DirectMessageReader (org.apache.ignite.internal.direct.DirectMessageReader)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 IgniteComponentType (org.apache.ignite.internal.IgniteComponentType)2 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)2 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)2 MessageFactory (org.apache.ignite.plugin.extensions.communication.MessageFactory)2 MessageFormatter (org.apache.ignite.plugin.extensions.communication.MessageFormatter)2 MessageReader (org.apache.ignite.plugin.extensions.communication.MessageReader)2 Channel (java.nio.channels.Channel)1 FileChannel (java.nio.channels.FileChannel)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1