|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.discovery; |
| 21 | + |
| 22 | +import org.apache.lucene.util.SetOnce; |
| 23 | +import org.elasticsearch.Version; |
| 24 | +import org.elasticsearch.action.ActionListener; |
| 25 | +import org.elasticsearch.cluster.ClusterName; |
| 26 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 27 | +import org.elasticsearch.common.settings.Settings; |
| 28 | +import org.elasticsearch.test.ESTestCase; |
| 29 | +import org.elasticsearch.test.transport.CapturingTransport; |
| 30 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 31 | +import org.elasticsearch.threadpool.ThreadPool; |
| 32 | +import org.elasticsearch.transport.TransportRequest; |
| 33 | +import org.elasticsearch.transport.TransportService; |
| 34 | +import org.elasticsearch.transport.TransportService.HandshakeResponse; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Before; |
| 37 | + |
| 38 | +import java.util.concurrent.CountDownLatch; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static java.util.Collections.emptyMap; |
| 42 | +import static java.util.Collections.emptySet; |
| 43 | +import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING; |
| 44 | +import static org.elasticsearch.discovery.HandshakingTransportAddressConnector.PROBE_HANDSHAKE_TIMEOUT_SETTING; |
| 45 | +import static org.elasticsearch.node.Node.NODE_NAME_SETTING; |
| 46 | +import static org.hamcrest.Matchers.equalTo; |
| 47 | + |
| 48 | +public class HandshakingTransportAddressConnectorTests extends ESTestCase { |
| 49 | + |
| 50 | + private DiscoveryNode remoteNode; |
| 51 | + private TransportService transportService; |
| 52 | + private ThreadPool threadPool; |
| 53 | + private String remoteClusterName; |
| 54 | + private HandshakingTransportAddressConnector handshakingTransportAddressConnector; |
| 55 | + private DiscoveryNode localNode; |
| 56 | + |
| 57 | + private boolean dropHandshake; |
| 58 | + |
| 59 | + @Before |
| 60 | + public void startServices() { |
| 61 | + localNode = new DiscoveryNode("local-node", buildNewFakeTransportAddress(), Version.CURRENT); |
| 62 | + final Settings settings = Settings.builder() |
| 63 | + .put(NODE_NAME_SETTING.getKey(), "node") |
| 64 | + .put(CLUSTER_NAME_SETTING.getKey(), "local-cluster") |
| 65 | + .build(); |
| 66 | + threadPool = new TestThreadPool("node", settings); |
| 67 | + |
| 68 | + remoteNode = null; |
| 69 | + remoteClusterName = null; |
| 70 | + dropHandshake = false; |
| 71 | + |
| 72 | + final CapturingTransport capturingTransport = new CapturingTransport() { |
| 73 | + @Override |
| 74 | + protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) { |
| 75 | + super.onSendRequest(requestId, action, request, node); |
| 76 | + assertThat(action, equalTo(TransportService.HANDSHAKE_ACTION_NAME)); |
| 77 | + assertEquals(remoteNode.getAddress(), node.getAddress()); |
| 78 | + assertNotEquals(remoteNode, node); |
| 79 | + if (dropHandshake == false) { |
| 80 | + handleResponse(requestId, new HandshakeResponse(remoteNode, new ClusterName(remoteClusterName), Version.CURRENT)); |
| 81 | + } |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + transportService = new TransportService(settings, capturingTransport, threadPool, |
| 86 | + TransportService.NOOP_TRANSPORT_INTERCEPTOR, address -> localNode, null, emptySet()); |
| 87 | + |
| 88 | + transportService.start(); |
| 89 | + transportService.acceptIncomingRequests(); |
| 90 | + |
| 91 | + handshakingTransportAddressConnector = new HandshakingTransportAddressConnector(settings, transportService); |
| 92 | + } |
| 93 | + |
| 94 | + @After |
| 95 | + public void stopServices() throws InterruptedException { |
| 96 | + transportService.stop(); |
| 97 | + terminate(threadPool); |
| 98 | + } |
| 99 | + |
| 100 | + public void testConnectsToMasterNode() throws InterruptedException { |
| 101 | + final CountDownLatch completionLatch = new CountDownLatch(1); |
| 102 | + final SetOnce<DiscoveryNode> receivedNode = new SetOnce<>(); |
| 103 | + |
| 104 | + remoteNode = new DiscoveryNode("remote-node", buildNewFakeTransportAddress(), Version.CURRENT); |
| 105 | + remoteClusterName = "local-cluster"; |
| 106 | + |
| 107 | + handshakingTransportAddressConnector.connectToRemoteMasterNode(remoteNode.getAddress(), new ActionListener<DiscoveryNode>() { |
| 108 | + @Override |
| 109 | + public void onResponse(DiscoveryNode discoveryNode) { |
| 110 | + receivedNode.set(discoveryNode); |
| 111 | + completionLatch.countDown(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void onFailure(Exception e) { |
| 116 | + throw new AssertionError(e); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + assertTrue(completionLatch.await(30, TimeUnit.SECONDS)); |
| 121 | + assertEquals(remoteNode, receivedNode.get()); |
| 122 | + } |
| 123 | + |
| 124 | + public void testDoesNotConnectToNonMasterNode() throws InterruptedException { |
| 125 | + remoteNode = new DiscoveryNode("remote-node", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); |
| 126 | + remoteClusterName = "local-cluster"; |
| 127 | + |
| 128 | + FailureListener failureListener = new FailureListener(); |
| 129 | + handshakingTransportAddressConnector.connectToRemoteMasterNode(remoteNode.getAddress(), failureListener); |
| 130 | + failureListener.assertFailure(); |
| 131 | + } |
| 132 | + |
| 133 | + public void testDoesNotConnectToLocalNode() throws Exception { |
| 134 | + remoteNode = localNode; |
| 135 | + remoteClusterName = "local-cluster"; |
| 136 | + |
| 137 | + FailureListener failureListener = new FailureListener(); |
| 138 | + handshakingTransportAddressConnector.connectToRemoteMasterNode(remoteNode.getAddress(), failureListener); |
| 139 | + failureListener.assertFailure(); |
| 140 | + } |
| 141 | + |
| 142 | + public void testDoesNotConnectToDifferentCluster() throws InterruptedException { |
| 143 | + remoteNode = new DiscoveryNode("remote-node", buildNewFakeTransportAddress(), Version.CURRENT); |
| 144 | + remoteClusterName = "another-cluster"; |
| 145 | + |
| 146 | + FailureListener failureListener = new FailureListener(); |
| 147 | + handshakingTransportAddressConnector.connectToRemoteMasterNode(remoteNode.getAddress(), failureListener); |
| 148 | + failureListener.assertFailure(); |
| 149 | + } |
| 150 | + |
| 151 | + public void testHandshakeTimesOut() throws InterruptedException { |
| 152 | + remoteNode = new DiscoveryNode("remote-node", buildNewFakeTransportAddress(), Version.CURRENT); |
| 153 | + remoteClusterName = "local-cluster"; |
| 154 | + dropHandshake = true; |
| 155 | + |
| 156 | + FailureListener failureListener = new FailureListener(); |
| 157 | + handshakingTransportAddressConnector.connectToRemoteMasterNode(remoteNode.getAddress(), failureListener); |
| 158 | + Thread.sleep(PROBE_HANDSHAKE_TIMEOUT_SETTING.get(Settings.EMPTY).millis()); |
| 159 | + failureListener.assertFailure(); |
| 160 | + } |
| 161 | + |
| 162 | + private class FailureListener implements ActionListener<DiscoveryNode> { |
| 163 | + final CountDownLatch completionLatch = new CountDownLatch(1); |
| 164 | + |
| 165 | + @Override |
| 166 | + public void onResponse(DiscoveryNode discoveryNode) { |
| 167 | + fail(discoveryNode.toString()); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onFailure(Exception e) { |
| 172 | + completionLatch.countDown(); |
| 173 | + } |
| 174 | + |
| 175 | + void assertFailure() throws InterruptedException { |
| 176 | + assertTrue(completionLatch.await(30, TimeUnit.SECONDS)); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments