Skip to content

Commit

Permalink
Ensure notify when proxy connections disconnect (elastic#65697)
Browse files Browse the repository at this point in the history
TransportService doesn't respond to the pending requests of proxy 
connections when the underlying connections get disconnected because
proxy connections do not override the getCacheKey method. Some CCS
requests would never be completed because of this bug.
  • Loading branch information
dnhatn committed Dec 2, 2020
1 parent 667908e commit 26350df
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 94 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.ccs;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.index.IndexModule;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.shard.SearchOperationListener;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractMultiClustersTestCase;
import org.elasticsearch.test.InternalTestCluster;
import org.elasticsearch.test.NodeRoles;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.junit.Before;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;

public class CrossClusterSearchIT extends AbstractMultiClustersTestCase {

@Override
protected Collection<String> remoteClusterAlias() {
return List.of("cluster_a");
}

private int indexDocs(Client client, String index) {
int numDocs = between(1, 10);
for (int i = 0; i < numDocs; i++) {
client.prepareIndex(index).setSource("f", "v").get();
}
client.admin().indices().prepareRefresh(index).get();
return numDocs;
}

public void testRemoteClusterClientRole() throws Exception {
assertAcked(client(LOCAL_CLUSTER).admin().indices().prepareCreate("demo"));
final int demoDocs = indexDocs(client(LOCAL_CLUSTER), "demo");
assertAcked(client("cluster_a").admin().indices().prepareCreate("prod"));
final int prodDocs = indexDocs(client("cluster_a"), "prod");
final InternalTestCluster localCluster = cluster(LOCAL_CLUSTER);
final String pureDataNode = randomBoolean() ? localCluster.startDataOnlyNode() : null;
final String nodeWithoutRemoteClusterClientRole = localCluster.startNode(NodeRoles.onlyRole(DiscoveryNodeRole.DATA_ROLE));
ElasticsearchAssertions.assertFutureThrows(
localCluster.client(nodeWithoutRemoteClusterClientRole)
.prepareSearch("demo", "cluster_a:prod")
.setQuery(new MatchAllQueryBuilder())
.setAllowPartialSearchResults(false)
.setSize(1000)
.execute(),
IllegalArgumentException.class,
RestStatus.BAD_REQUEST,
"node [" + nodeWithoutRemoteClusterClientRole + "] does not have the remote cluster client role enabled"
);

final String nodeWithRemoteClusterClientRole = randomFrom(
StreamSupport.stream(localCluster.clusterService().state().nodes().spliterator(), false)
.map(DiscoveryNode::getName)
.filter(nodeName -> nodeWithoutRemoteClusterClientRole.equals(nodeName) == false)
.filter(nodeName -> nodeName.equals(pureDataNode) == false)
.collect(Collectors.toList()));

final SearchResponse resp = localCluster.client(nodeWithRemoteClusterClientRole)
.prepareSearch("demo", "cluster_a:prod")
.setQuery(new MatchAllQueryBuilder())
.setAllowPartialSearchResults(false)
.setSize(1000)
.get();
assertHitCount(resp, demoDocs + prodDocs);
}

public void testProxyConnectionDisconnect() throws Exception {
assertAcked(client(LOCAL_CLUSTER).admin().indices().prepareCreate("demo"));
indexDocs(client(LOCAL_CLUSTER), "demo");
final String remoteNode = cluster("cluster_a").startDataOnlyNode();
assertAcked(client("cluster_a").admin().indices().prepareCreate("prod")
.setSettings(Settings.builder().put("index.routing.allocation.require._name", remoteNode)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build()));
indexDocs(client("cluster_a"), "prod");
SearchListenerPlugin.blockQueryPhase();
try {
PlainActionFuture<SearchResponse> future = new PlainActionFuture<>();
SearchRequest searchRequest = new SearchRequest("demo", "cluster_a:prod");
searchRequest.allowPartialSearchResults(false);
searchRequest.setCcsMinimizeRoundtrips(false);
searchRequest.source(new SearchSourceBuilder().query(new MatchAllQueryBuilder()).size(1000));
client(LOCAL_CLUSTER).search(searchRequest, future);
SearchListenerPlugin.waitSearchStarted();
disconnectFromRemoteClusters();
assertBusy(() -> assertTrue(future.isDone()));
configureAndConnectsToRemoteClusters();
} finally {
SearchListenerPlugin.allowQueryPhase();
}
}

@Override
protected Collection<Class<? extends Plugin>> nodePlugins(String clusterAlias) {
if (clusterAlias.equals(LOCAL_CLUSTER)) {
return super.nodePlugins(clusterAlias);
} else {
return CollectionUtils.appendToCopy(super.nodePlugins(clusterAlias), SearchListenerPlugin.class);
}
}

@Before
public void resetSearchListenerPlugin() throws Exception {
SearchListenerPlugin.reset();
}

public static class SearchListenerPlugin extends Plugin {
private static final AtomicReference<CountDownLatch> startedLatch = new AtomicReference<>();
private static final AtomicReference<CountDownLatch> queryLatch = new AtomicReference<>();

static void reset() {
startedLatch.set(new CountDownLatch(1));
}

static void blockQueryPhase() {
queryLatch.set(new CountDownLatch(1));
}

static void allowQueryPhase() {
final CountDownLatch latch = queryLatch.get();
if (latch != null) {
latch.countDown();
}
}

static void waitSearchStarted() throws InterruptedException {
assertTrue(startedLatch.get().await(60, TimeUnit.SECONDS));
}

@Override
public void onIndexModule(IndexModule indexModule) {
indexModule.addSearchOperationListener(new SearchOperationListener() {
@Override
public void onPreQueryPhase(SearchContext searchContext) {
startedLatch.get().countDown();
final CountDownLatch latch = queryLatch.get();
if (latch != null) {
try {
assertTrue(latch.await(60, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
}
});
super.onIndexModule(indexModule);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,10 @@ public boolean isClosed() {
public Version getVersion() {
return connection.getVersion();
}

@Override
public Object getCacheKey() {
return connection.getCacheKey();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.transport.RemoteConnectionInfo;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.transport.nio.MockNioTransportPlugin;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;

Expand All @@ -49,6 +50,7 @@

import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING;
import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;

Expand Down Expand Up @@ -108,7 +110,15 @@ public final void startClusters() throws Exception {
clusters.put(clusterAlias, cluster);
}
clusterGroup = new ClusterGroup(clusters);
configureRemoteClusters();
configureAndConnectsToRemoteClusters();
}

@After
public void assertAfterTest() throws Exception {
for (InternalTestCluster cluster : clusters().values()) {
cluster.wipe(Set.of());
cluster.assertAfterTest();
}
}

@AfterClass
Expand All @@ -117,7 +127,23 @@ public static void stopClusters() throws IOException {
clusterGroup = null;
}

private void configureRemoteClusters() throws Exception {
protected void disconnectFromRemoteClusters() throws Exception {
Settings.Builder settings = Settings.builder();
final Set<String> clusterAliases = clusterGroup.clusterAliases();
for (String clusterAlias : clusterAliases) {
if (clusterAlias.equals(LOCAL_CLUSTER) == false) {
settings.putNull("cluster.remote." + clusterAlias + ".seeds");
}
}
client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings).get();
assertBusy(() -> {
for (TransportService transportService : cluster(LOCAL_CLUSTER).getInstances(TransportService.class)) {
assertThat(transportService.getRemoteClusterService().getRegisteredRemoteClusterNames(), empty());
}
});
}

protected void configureAndConnectsToRemoteClusters() throws Exception {
Map<String, List<String>> seedNodes = new HashMap<>();
for (String clusterAlias : clusterGroup.clusterAliases()) {
if (clusterAlias.equals(LOCAL_CLUSTER) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2335,7 +2335,7 @@ public synchronized void assertAfterTest() throws Exception {
}
}

private void assertRequestsFinished() {
public void assertRequestsFinished() {
assert Thread.holdsLock(this);
if (size() > 0) {
for (NodeAndClient nodeAndClient : nodes.values()) {
Expand Down

0 comments on commit 26350df

Please sign in to comment.