|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.util.nio; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.UUID; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | +import java.util.concurrent.ThreadLocalRandom; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | +import java.util.function.Supplier; |
| 28 | +import org.apache.ignite.cluster.ClusterNode; |
| 29 | +import org.apache.ignite.internal.util.nio.ssl.BlockingSslHandler; |
| 30 | +import org.apache.ignite.internal.util.typedef.internal.U; |
| 31 | +import org.apache.ignite.lang.IgniteRunnable; |
| 32 | +import org.apache.ignite.plugin.extensions.communication.Message; |
| 33 | +import org.apache.ignite.spi.communication.CommunicationListener; |
| 34 | +import org.apache.ignite.spi.communication.CommunicationSpi; |
| 35 | +import org.apache.ignite.spi.communication.GridAbstractCommunicationSelfTest; |
| 36 | +import org.apache.ignite.spi.communication.TestVolatilePayloadMessage; |
| 37 | +import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi; |
| 38 | +import org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper; |
| 39 | +import org.apache.ignite.spi.communication.tcp.messages.RecoveryLastReceivedMessage; |
| 40 | +import org.apache.ignite.testframework.GridTestUtils; |
| 41 | +import org.junit.Test; |
| 42 | + |
| 43 | +import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; |
| 44 | + |
| 45 | +/** |
| 46 | + * Tests the case when regular communications messages are sent along with the last handshake messages and SSL is enabled. |
| 47 | + * It asserts that if not all received by network bytes are processed by {@link BlockingSslHandler} during the handshake |
| 48 | + * phase, then all remaining bytes are properly copied to {@code GridNioSslHandler}, which replaces |
| 49 | + * {@link BlockingSslHandler} after the handshake phase. |
| 50 | + * The steps that can lead to mentioned above conditions: |
| 51 | + * <p> |
| 52 | + * 1. Node B sends a MESSAGE to Node A and stores it in the local recovery descriptor until an acknowledgment is received |
| 53 | + * from Node A. |
| 54 | + * <p> |
| 55 | + * 2. Node A, for whatever reason, reestablishes connection with node B and starts handshake negotiation. |
| 56 | + * <p> |
| 57 | + * 3. Node B during the final phase of handshake sends {@link RecoveryLastReceivedMessage} and resends not acknowledged |
| 58 | + * MESSAGE from step 1. But all sent bytes are divided into two network packets. Let's assume that the first packet |
| 59 | + * contains all bytes related to {@link RecoveryLastReceivedMessage} and only half of the MESSAGE bytes. |
| 60 | + * <p> |
| 61 | + * 4. Node A decodes {@link RecoveryLastReceivedMessage} from the received network packet and finishes the handshake. |
| 62 | + * But the MESSAGE cannot be processed because not enough bytes were received. So we must save remaining bytes from the |
| 63 | + * first network packet, wait for the next network packet and finish MESSAGE deserialization. |
| 64 | + * |
| 65 | + */ |
| 66 | +public class TcpCommunicationSpiSslVolatilePayloadTest extends GridAbstractCommunicationSelfTest<CommunicationSpi<Message>> { |
| 67 | + /** */ |
| 68 | + private static final int TEST_ITERATION_CNT = 1000; |
| 69 | + |
| 70 | + /** The number of messages intended to fill the network buffer during last handshake message sending. */ |
| 71 | + private static final int RECOVERY_DESCRIPTOR_QUEUE_MESSAGE_CNT = 50; |
| 72 | + |
| 73 | + /** */ |
| 74 | + private static final AtomicInteger msgCreatedCntr = new AtomicInteger(); |
| 75 | + |
| 76 | + /** */ |
| 77 | + private static final AtomicInteger msgReceivedCntr = new AtomicInteger(); |
| 78 | + |
| 79 | + /** */ |
| 80 | + private static final Map<Integer, TestVolatilePayloadMessage> messages = new ConcurrentHashMap<>(); |
| 81 | + |
| 82 | + /** {@inheritDoc} */ |
| 83 | + @Override protected CommunicationSpi<Message> getSpi(int idx) { |
| 84 | + return new TcpCommunicationSpi().setLocalPort(GridTestUtils.getNextCommPort(getClass())) |
| 85 | + .setIdleConnectionTimeout(2000) |
| 86 | + .setTcpNoDelay(true); |
| 87 | + } |
| 88 | + |
| 89 | + /** {@inheritDoc} */ |
| 90 | + @Override protected CommunicationListener<Message> createMessageListener(UUID nodeId) { |
| 91 | + return new TestCommunicationListener(); |
| 92 | + } |
| 93 | + |
| 94 | + /** {@inheritDoc} */ |
| 95 | + @Override protected Map<Short, Supplier<Message>> customMessageTypes() { |
| 96 | + return Collections.singletonMap(TestVolatilePayloadMessage.DIRECT_TYPE, TestVolatilePayloadMessage::new); |
| 97 | + } |
| 98 | + |
| 99 | + /** {@inheritDoc} */ |
| 100 | + @Override protected boolean isSslEnabled() { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + /** */ |
| 105 | + @Test |
| 106 | + public void test() throws Exception { |
| 107 | + ClusterNode from = nodes.get(0); |
| 108 | + ClusterNode to = nodes.get(1); |
| 109 | + |
| 110 | + for (int i = 0; i < TEST_ITERATION_CNT; i++) { |
| 111 | + // Force connection to be established. |
| 112 | + sendMessage(from, to, createMessage()); |
| 113 | + |
| 114 | + GridNioRecoveryDescriptor fromDesc = extractRecoveryDescriptor(from); |
| 115 | + GridNioRecoveryDescriptor toDesc = extractRecoveryDescriptor(to); |
| 116 | + |
| 117 | + // Stores multiple dummy messages in a recovery descriptor. When the connection is restored, they will be |
| 118 | + // written to the network buffer along with the last handshake message. |
| 119 | + // See TcpHandshakeExecutor#receiveAcknowledge |
| 120 | + for (int j = 0; j < RECOVERY_DESCRIPTOR_QUEUE_MESSAGE_CNT; j++) |
| 121 | + toDesc.add(new GridNioServer.WriteRequestImpl(toDesc.session(), createMessage(), false, null)); |
| 122 | + |
| 123 | + // Close connection to re-initiate handshake between nodes. |
| 124 | + if (fromDesc.session() != null) |
| 125 | + fromDesc.session().close(); |
| 126 | + } |
| 127 | + |
| 128 | + assertTrue(waitForCondition(() -> msgCreatedCntr.get() == msgReceivedCntr.get(), 5000)); |
| 129 | + } |
| 130 | + |
| 131 | + /** */ |
| 132 | + public GridNioRecoveryDescriptor extractRecoveryDescriptor(ClusterNode node) throws Exception { |
| 133 | + CommunicationSpi<Message> spi = spis.get(node.id()); |
| 134 | + |
| 135 | + GridNioServerWrapper wrapper = U.field(spi, "nioSrvWrapper"); |
| 136 | + |
| 137 | + assertTrue(waitForCondition(() -> !wrapper.recoveryDescs().values().isEmpty(), getTestTimeout())); |
| 138 | + |
| 139 | + return wrapper.recoveryDescs().values().stream().findFirst().get(); |
| 140 | + } |
| 141 | + |
| 142 | + /** */ |
| 143 | + private Message createMessage() { |
| 144 | + byte[] payload = new byte[ThreadLocalRandom.current().nextInt(10, 1024)]; |
| 145 | + |
| 146 | + ThreadLocalRandom.current().nextBytes(payload); |
| 147 | + |
| 148 | + TestVolatilePayloadMessage msg = new TestVolatilePayloadMessage(msgCreatedCntr.getAndIncrement(), payload); |
| 149 | + |
| 150 | + messages.put(msg.index(), msg); |
| 151 | + |
| 152 | + return msg; |
| 153 | + } |
| 154 | + |
| 155 | + /** */ |
| 156 | + private void sendMessage(ClusterNode from, ClusterNode to, Message msg) { |
| 157 | + spis.get(from.id()).sendMessage(to, msg); |
| 158 | + } |
| 159 | + |
| 160 | + /** */ |
| 161 | + private static class TestCommunicationListener implements CommunicationListener<Message> { |
| 162 | + /** {@inheritDoc} */ |
| 163 | + @Override public void onMessage(UUID nodeId, Message msg, IgniteRunnable msgC) { |
| 164 | + msgC.run(); |
| 165 | + |
| 166 | + if (msg instanceof TestVolatilePayloadMessage) { |
| 167 | + TestVolatilePayloadMessage testMsg = (TestVolatilePayloadMessage)msg; |
| 168 | + |
| 169 | + TestVolatilePayloadMessage expMsg = messages.get(testMsg.index()); |
| 170 | + |
| 171 | + assertNotNull(expMsg); |
| 172 | + |
| 173 | + assertTrue(Arrays.equals(expMsg.payload(), testMsg.payload())); |
| 174 | + |
| 175 | + msgReceivedCntr.incrementAndGet(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** {@inheritDoc} */ |
| 180 | + @Override public void onDisconnected(UUID nodeId) { |
| 181 | + // No-op. |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments