TransportClient: Mark transport client as such when instantiating

This allows plugins to load/inject specific classes, when the client started
is a transport client (compared to being a node client).

Closes #7552
This commit is contained in:
Alexander Reelsen 2014-09-03 12:29:52 +02:00
parent 07d741c2cb
commit 8b8cc80ba8
5 changed files with 80 additions and 13 deletions

View File

@ -82,6 +82,8 @@ import org.elasticsearch.common.settings.Settings;
*/ */
public interface Client extends ElasticsearchClient<Client>, Releasable { public interface Client extends ElasticsearchClient<Client>, Releasable {
String CLIENT_TYPE_SETTING = "client.type";
/** /**
* The admin client that can be used to perform administrative operations. * The admin client that can be used to perform administrative operations.
*/ */

View File

@ -98,20 +98,16 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde
*/ */
public class TransportClient extends AbstractClient { public class TransportClient extends AbstractClient {
private static final String CLIENT_TYPE = "transport";
final Injector injector; final Injector injector;
private final Settings settings; private final Settings settings;
private final Environment environment; private final Environment environment;
private final PluginsService pluginsService; private final PluginsService pluginsService;
private final TransportClientNodesService nodesService; private final TransportClientNodesService nodesService;
private final InternalTransportClient internalClient; private final InternalTransportClient internalClient;
/** /**
* Constructs a new transport client with settings loaded either from the classpath or the file system (the * Constructs a new transport client with settings loaded either from the classpath or the file system (the
* <tt>elasticsearch.(yml|json)</tt> files optionally prefixed with <tt>config/</tt>). * <tt>elasticsearch.(yml|json)</tt> files optionally prefixed with <tt>config/</tt>).
@ -163,6 +159,7 @@ public class TransportClient extends AbstractClient {
Settings settings = settingsBuilder().put(tuple.v1()) Settings settings = settingsBuilder().put(tuple.v1())
.put("network.server", false) .put("network.server", false)
.put("node.client", true) .put("node.client", true)
.put(CLIENT_TYPE_SETTING, CLIENT_TYPE)
.build(); .build();
this.environment = tuple.v2(); this.environment = tuple.v2();

View File

@ -103,28 +103,29 @@ import org.elasticsearch.watcher.ResourceWatcherService;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
/** /**
* *
*/ */
public final class InternalNode implements Node { public final class InternalNode implements Node {
private static final String CLIENT_TYPE = "node";
private final Lifecycle lifecycle = new Lifecycle(); private final Lifecycle lifecycle = new Lifecycle();
private final Injector injector; private final Injector injector;
private final Settings settings; private final Settings settings;
private final Environment environment; private final Environment environment;
private final PluginsService pluginsService; private final PluginsService pluginsService;
private final Client client; private final Client client;
public InternalNode() throws ElasticsearchException { public InternalNode() throws ElasticsearchException {
this(ImmutableSettings.Builder.EMPTY_SETTINGS, true); this(ImmutableSettings.Builder.EMPTY_SETTINGS, true);
} }
public InternalNode(Settings pSettings, boolean loadConfigSettings) throws ElasticsearchException { public InternalNode(Settings preparedSettings, boolean loadConfigSettings) throws ElasticsearchException {
final Settings pSettings = settingsBuilder().put(preparedSettings)
.put(Client.CLIENT_TYPE_SETTING, CLIENT_TYPE).build();
Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettings(pSettings, loadConfigSettings); Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettings(pSettings, loadConfigSettings);
tuple = new Tuple<>(TribeService.processSettings(tuple.v1()), tuple.v2()); tuple = new Tuple<>(TribeService.processSettings(tuple.v1()), tuple.v2());

View File

@ -0,0 +1,49 @@
/*
* 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.client.node;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
import static org.hamcrest.Matchers.is;
/**
*
*/
@ClusterScope(scope = Scope.SUITE)
public class NodeClientTests extends ElasticsearchIntegrationTest {
@Override
protected Settings nodeSettings(int nodeOrdinal) {
return settingsBuilder().put(super.nodeSettings(nodeOrdinal)).put(Client.CLIENT_TYPE_SETTING, "anything").build();
}
@Test
public void testThatClientTypeSettingCannotBeChanged() {
for (Settings settings : internalCluster().getInstances(Settings.class)) {
assertThat(settings.get(Client.CLIENT_TYPE_SETTING), is("node"));
}
}
}

View File

@ -20,8 +20,10 @@
package org.elasticsearch.client.transport; package org.elasticsearch.client.transport;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.node.Node; import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.node.NodeBuilder;
@ -31,6 +33,8 @@ import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
import org.junit.Test; import org.junit.Test;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope; import static org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -50,7 +54,7 @@ public class TransportClientTests extends ElasticsearchIntegrationTest {
public void testNodeVersionIsUpdated() { public void testNodeVersionIsUpdated() {
TransportClient client = (TransportClient) internalCluster().client(); TransportClient client = (TransportClient) internalCluster().client();
TransportClientNodesService nodeService = client.nodeService(); TransportClientNodesService nodeService = client.nodeService();
Node node = NodeBuilder.nodeBuilder().data(false).settings(ImmutableSettings.builder() Node node = nodeBuilder().data(false).settings(ImmutableSettings.builder()
.put(internalCluster().getDefaultSettings()) .put(internalCluster().getDefaultSettings())
.put("node.name", "testNodeVersionIsUpdated") .put("node.name", "testNodeVersionIsUpdated")
.put("http.enabled", false) .put("http.enabled", false)
@ -80,4 +84,18 @@ public class TransportClientTests extends ElasticsearchIntegrationTest {
node.close(); node.close();
} }
} }
@Test
public void testThatTransportClientSettingIsSet() {
TransportClient client = (TransportClient) internalCluster().client();
Settings settings = client.injector.getInstance(Settings.class);
assertThat(settings.get(Client.CLIENT_TYPE_SETTING), is("transport"));
}
@Test
public void testThatTransportClientSettingCannotBeChanged() {
TransportClient client = new TransportClient(settingsBuilder().put(Client.CLIENT_TYPE_SETTING, "anything"));
Settings settings = client.injector.getInstance(Settings.class);
assertThat(settings.get(Client.CLIENT_TYPE_SETTING), is("transport"));
}
} }