Skip to content
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

Give helpful message on remote connections disabled #53690

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,18 @@ public final class RemoteClusterService extends RemoteClusterAware implements Cl
(ns, key) -> boolSetting(key, TransportSettings.TRANSPORT_COMPRESS,
new RemoteConnectionEnabled<>(ns, key), Setting.Property.Dynamic, Setting.Property.NodeScope));

private final boolean enabled;

public boolean isEnabled() {
return enabled;
}

private final TransportService transportService;
private final Map<String, RemoteClusterConnection> remoteClusters = ConcurrentCollections.newConcurrentMap();

RemoteClusterService(Settings settings, TransportService transportService) {
super(settings);
this.enabled = ENABLE_REMOTE_CLUSTERS.get(settings);
this.transportService = transportService;
}

Expand Down Expand Up @@ -200,6 +207,9 @@ public Transport.Connection getConnection(String cluster) {
}

RemoteClusterConnection getRemoteClusterConnection(String cluster) {
if (enabled == false) {
throw new IllegalArgumentException("remote cluster service is not enabled");
}
RemoteClusterConnection connection = remoteClusters.get(cluster);
if (connection == null) {
throw new NoSuchRemoteClusterException(cluster);
Expand Down Expand Up @@ -336,6 +346,9 @@ public Stream<RemoteConnectionInfo> getRemoteConnectionInfos() {
* function on success.
*/
public void collectNodes(Set<String> clusters, ActionListener<BiFunction<String, String, DiscoveryNode>> listener) {
if (enabled == false) {
throw new IllegalArgumentException("remote cluster service is not enabled");
}
Map<String, RemoteClusterConnection> remoteClusters = this.remoteClusters;
for (String cluster : clusters) {
if (remoteClusters.containsKey(cluster) == false) {
Expand Down Expand Up @@ -379,6 +392,9 @@ public void onFailure(Exception e) {
* @throws IllegalArgumentException if the given clusterAlias doesn't exist
*/
public Client getRemoteClusterClient(ThreadPool threadPool, String clusterAlias) {
if (transportService.getRemoteClusterService().isEnabled() == false) {
throw new IllegalArgumentException("remote cluster service is not enabled");
}
if (transportService.getRemoteClusterService().getRemoteClusterNames().contains(clusterAlias) == false) {
throw new NoSuchRemoteClusterException(clusterAlias);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.transport.RemoteClusterConnectionTests.startTransport;
import static org.hamcrest.Matchers.equalTo;

public class RemoteClusterClientTests extends ESTestCase {
private final ThreadPool threadPool = new TestThreadPool(getClass().getName());
Expand Down Expand Up @@ -119,4 +120,17 @@ public void onNodeDisconnected(DiscoveryNode node, Transport.Connection connecti
}
}
}

public void testRemoteClusterServiceNotEnabled() {
final Settings settings = Settings.builder().put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), false).build();
try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) {
service.start();
service.acceptIncomingRequests();
final RemoteClusterService remoteClusterService = service.getRemoteClusterService();
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> remoteClusterService.getRemoteClusterClient(threadPool, "test"));
assertThat(e.getMessage(), equalTo("remote cluster service is not enabled"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -843,10 +844,34 @@ public void testSkipUnavailable() {
}
}

public void testRemoteClusterServiceNotEnabledGetRemoteClusterConnection() {
final Settings settings = Settings.builder().put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), false).build();
try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) {
service.start();
service.acceptIncomingRequests();
final IllegalArgumentException e =
expectThrows(IllegalArgumentException.class, () -> service.getRemoteClusterService().getRemoteClusterConnection("test"));
assertThat(e.getMessage(), equalTo("remote cluster service is not enabled"));
}
}

public void testRemoteClusterServiceNotEnabledGetCollectNodes() {
final Settings settings = Settings.builder().put(RemoteClusterService.ENABLE_REMOTE_CLUSTERS.getKey(), false).build();
try (MockTransportService service = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null)) {
service.start();
service.acceptIncomingRequests();
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> service.getRemoteClusterService().collectNodes(Set.of(), ActionListener.wrap(r -> {}, r -> {})));
assertThat(e.getMessage(), equalTo("remote cluster service is not enabled"));
}
}

private static Settings createSettings(String clusterAlias, List<String> seeds) {
Settings.Builder builder = Settings.builder();
builder.put(SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS.getConcreteSettingForNamespace(clusterAlias).getKey(),
Strings.collectionToCommaDelimitedString(seeds));
return builder.build();
}

}