|
| 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 | +package org.elasticsearch.transport; |
| 20 | + |
| 21 | +import org.elasticsearch.Version; |
| 22 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; |
| 23 | +import org.elasticsearch.client.Client; |
| 24 | +import org.elasticsearch.cluster.ClusterName; |
| 25 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.test.ESTestCase; |
| 28 | +import org.elasticsearch.test.transport.MockTransportService; |
| 29 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 30 | +import org.elasticsearch.threadpool.ThreadPool; |
| 31 | + |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import static org.elasticsearch.transport.RemoteClusterConnectionTests.startTransport; |
| 36 | + |
| 37 | +public class RemoteClusterClientTests extends ESTestCase { |
| 38 | + private final ThreadPool threadPool = new TestThreadPool(getClass().getName()); |
| 39 | + |
| 40 | + @Override |
| 41 | + public void tearDown() throws Exception { |
| 42 | + super.tearDown(); |
| 43 | + ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS); |
| 44 | + } |
| 45 | + |
| 46 | + public void testConnectAndExecuteRequest() throws Exception { |
| 47 | + Settings remoteSettings = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "foo_bar_cluster").build(); |
| 48 | + try (MockTransportService remoteTransport = startTransport("remote_node", Collections.emptyList(), Version.CURRENT, threadPool, |
| 49 | + remoteSettings)) { |
| 50 | + DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode(); |
| 51 | + |
| 52 | + Settings localSettings = Settings.builder() |
| 53 | + .put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), true) |
| 54 | + .put("search.remote.test.seeds", remoteNode.getAddress().getAddress() + ":" + remoteNode.getAddress().getPort()).build(); |
| 55 | + try (MockTransportService service = MockTransportService.createNewService(localSettings, Version.CURRENT, threadPool, null)) { |
| 56 | + service.start(); |
| 57 | + service.acceptIncomingRequests(); |
| 58 | + RemoteClusterService remoteClusterService = service.getRemoteClusterService(); |
| 59 | + assertTrue(remoteClusterService.isRemoteNodeConnected("test", remoteNode)); |
| 60 | + Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test"); |
| 61 | + ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get(); |
| 62 | + assertNotNull(clusterStateResponse); |
| 63 | + assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value()); |
| 64 | + // also test a failure, there is no handler for search registered |
| 65 | + ActionNotFoundTransportException ex = expectThrows(ActionNotFoundTransportException.class, |
| 66 | + () -> client.prepareSearch().get()); |
| 67 | + assertEquals("No handler for action [indices:data/read/search]", ex.getMessage()); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void testEnsureWeReconnect() throws Exception { |
| 73 | + Settings remoteSettings = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "foo_bar_cluster").build(); |
| 74 | + try (MockTransportService remoteTransport = startTransport("remote_node", Collections.emptyList(), Version.CURRENT, threadPool, |
| 75 | + remoteSettings)) { |
| 76 | + DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode(); |
| 77 | + Settings localSettings = Settings.builder() |
| 78 | + .put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), true) |
| 79 | + .put("search.remote.test.seeds", remoteNode.getAddress().getAddress() + ":" + remoteNode.getAddress().getPort()).build(); |
| 80 | + try (MockTransportService service = MockTransportService.createNewService(localSettings, Version.CURRENT, threadPool, null)) { |
| 81 | + service.start(); |
| 82 | + service.acceptIncomingRequests(); |
| 83 | + service.disconnectFromNode(remoteNode); |
| 84 | + RemoteClusterService remoteClusterService = service.getRemoteClusterService(); |
| 85 | + assertBusy(() -> assertFalse(remoteClusterService.isRemoteNodeConnected("test", remoteNode))); |
| 86 | + Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test"); |
| 87 | + ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get(); |
| 88 | + assertNotNull(clusterStateResponse); |
| 89 | + assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments