-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add remote cluster client #29495
Merged
Merged
Add remote cluster client #29495
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8cb37a4
Add remote cluster client
s1monw 13fd567
revert unrelated change
s1monw 6fed374
remove stale imports
s1monw c2084c5
fix line len
s1monw a46c4f5
fix alias check
s1monw efd34c0
also test a failure
s1monw ac49d1b
fix javadocs
s1monw 47e148b
fix more javadocs
s1monw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
server/src/main/java/org/elasticsearch/transport/RemoteClusterAwareClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.transport; | ||
|
||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.ActionListenerResponseHandler; | ||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestBuilder; | ||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.client.support.AbstractClient; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
final class RemoteClusterAwareClient extends AbstractClient { | ||
|
||
private final TransportService service; | ||
private final String clusterAlias; | ||
private final RemoteClusterService remoteClusterService; | ||
|
||
RemoteClusterAwareClient(Settings settings, ThreadPool threadPool, TransportService service, String clusterAlias) { | ||
super(settings, threadPool); | ||
this.service = service; | ||
this.clusterAlias = clusterAlias; | ||
this.remoteClusterService = service.getRemoteClusterService(); | ||
} | ||
|
||
@Override | ||
protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends | ||
ActionRequestBuilder<Request, Response, RequestBuilder>> | ||
void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) { | ||
remoteClusterService.ensureConnected(clusterAlias, ActionListener.wrap(res -> { | ||
Transport.Connection connection = remoteClusterService.getConnection(clusterAlias); | ||
service.sendRequest(connection, action.name(), request, TransportRequestOptions.EMPTY, | ||
new ActionListenerResponseHandler<>(listener, action::newResponse)); | ||
}, | ||
listener::onFailure)); | ||
} | ||
|
||
|
||
@Override | ||
public void close() { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public Client getRemoteClusterClient(String clusterAlias) { | ||
return remoteClusterService.getRemoteClusterClient(threadPool(), clusterAlias); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
*/ | ||
package org.elasticsearch.transport; | ||
|
||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.core.internal.io.IOUtils; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.ActionListener; | ||
|
@@ -36,6 +37,7 @@ | |
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.common.util.concurrent.CountDown; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
|
@@ -398,4 +400,16 @@ public void onFailure(Exception e) { | |
}); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a client to the remote cluster if the given cluster alias exists. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while at it, shall we mention that it throws if cluster alias is not found (rather than for instance returning null) |
||
* @param threadPool the {@link ThreadPool} for the client | ||
* @param clusterAlias the cluster alias the remote cluster is registred under | ||
*/ | ||
public Client getRemoteClusterClient(ThreadPool threadPool, String clusterAlias) { | ||
if (transportService.getRemoteClusterService().getRemoteClusterNames().contains(clusterAlias) == false) { | ||
throw new IllegalArgumentException("unknown cluster alias [" + clusterAlias + "]"); | ||
} | ||
return new RemoteClusterAwareClient(settings, threadPool, transportService, clusterAlias); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
server/src/test/java/org/elasticsearch/transport/RemoteClusterClientTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.transport; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.transport.MockTransportService; | ||
import org.elasticsearch.threadpool.TestThreadPool; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.util.Collections; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.elasticsearch.transport.RemoteClusterConnectionTests.startTransport; | ||
|
||
public class RemoteClusterClientTests extends ESTestCase { | ||
private final ThreadPool threadPool = new TestThreadPool(getClass().getName()); | ||
|
||
@Override | ||
public void tearDown() throws Exception { | ||
super.tearDown(); | ||
ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS); | ||
} | ||
|
||
public void testConnectAndExecuteRequest() throws Exception { | ||
Settings remoteSettings = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "foo_bar_cluster").build(); | ||
try (MockTransportService remoteTransport = startTransport("remote_node", Collections.emptyList(), Version.CURRENT, threadPool, | ||
remoteSettings)) { | ||
DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode(); | ||
|
||
Settings localSettings = Settings.builder() | ||
.put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), true) | ||
.put("search.remote.test.seeds", remoteNode.getAddress().getAddress() + ":" + remoteNode.getAddress().getPort()).build(); | ||
try (MockTransportService service = MockTransportService.createNewService(localSettings, Version.CURRENT, threadPool, null)) { | ||
service.start(); | ||
service.acceptIncomingRequests(); | ||
RemoteClusterService remoteClusterService = service.getRemoteClusterService(); | ||
assertTrue(remoteClusterService.isRemoteNodeConnected("test", remoteNode)); | ||
Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test"); | ||
ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get(); | ||
assertNotNull(clusterStateResponse); | ||
assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value()); | ||
// also test a failure, there is no handler for search registered | ||
ActionNotFoundTransportException ex = expectThrows(ActionNotFoundTransportException.class, | ||
() -> client.prepareSearch().get()); | ||
assertEquals("No handler for action [indices:data/read/search]", ex.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
public void testEnsureWeReconnect() throws Exception { | ||
Settings remoteSettings = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "foo_bar_cluster").build(); | ||
try (MockTransportService remoteTransport = startTransport("remote_node", Collections.emptyList(), Version.CURRENT, threadPool, | ||
remoteSettings)) { | ||
DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode(); | ||
Settings localSettings = Settings.builder() | ||
.put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), true) | ||
.put("search.remote.test.seeds", remoteNode.getAddress().getAddress() + ":" + remoteNode.getAddress().getPort()).build(); | ||
try (MockTransportService service = MockTransportService.createNewService(localSettings, Version.CURRENT, threadPool, null)) { | ||
service.start(); | ||
service.acceptIncomingRequests(); | ||
service.disconnectFromNode(remoteNode); | ||
RemoteClusterService remoteClusterService = service.getRemoteClusterService(); | ||
assertBusy(() -> assertFalse(remoteClusterService.isRemoteNodeConnected("test", remoteNode))); | ||
Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test"); | ||
ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get(); | ||
assertNotNull(clusterStateResponse); | ||
assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value()); | ||
} | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh one thing s/optinoal/optional :P