|
|
|
@ -21,17 +21,17 @@ import java.io.IOException;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class TransportClientHelper {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LogManager.getLogger(TransportClientHelper.class.getName());
|
|
|
|
|
|
|
|
|
|
private static ElasticsearchClient client;
|
|
|
|
|
|
|
|
|
|
private static Object configurationObject;
|
|
|
|
|
|
|
|
|
|
private final Object lock = new Object();
|
|
|
|
|
private static final Map<String, ElasticsearchClient> clientMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public ElasticsearchClient createClient(Settings settings, Object object) {
|
|
|
|
|
if (configurationObject == null && object != null) {
|
|
|
|
@ -40,46 +40,17 @@ public class TransportClientHelper {
|
|
|
|
|
if (configurationObject instanceof ElasticsearchClient) {
|
|
|
|
|
return (ElasticsearchClient) configurationObject;
|
|
|
|
|
}
|
|
|
|
|
if (client == null) {
|
|
|
|
|
synchronized (lock) {
|
|
|
|
|
String systemIdentifier = System.getProperty("os.name")
|
|
|
|
|
+ " " + System.getProperty("java.vm.name")
|
|
|
|
|
+ " " + System.getProperty("java.vm.vendor")
|
|
|
|
|
+ " " + System.getProperty("java.vm.version")
|
|
|
|
|
+ " Elasticsearch " + Version.CURRENT.toString();
|
|
|
|
|
Settings effectiveSettings = Settings.builder()
|
|
|
|
|
// for thread pool size
|
|
|
|
|
.put("processors",
|
|
|
|
|
settings.getAsInt("processors", Runtime.getRuntime().availableProcessors()))
|
|
|
|
|
.put("client.transport.sniff", false) // do not sniff
|
|
|
|
|
.put("client.transport.nodes_sampler_interval", "1m") // do not ping
|
|
|
|
|
.put("client.transport.ping_timeout", "1m") // wait for unresponsive nodes a very long time before disconnect
|
|
|
|
|
.put("client.transport.ignore_cluster_name", true) // connect to any cluster
|
|
|
|
|
// custom settings may override defaults
|
|
|
|
|
.put(settings)
|
|
|
|
|
.build();
|
|
|
|
|
logger.info("creating transport client on {} with custom settings {} and effective settings {}",
|
|
|
|
|
systemIdentifier, settings.getAsMap(), effectiveSettings.getAsMap());
|
|
|
|
|
|
|
|
|
|
// we need to disable dead lock check because we may have mixed node/transport clients
|
|
|
|
|
DefaultChannelFuture.setUseDeadLockChecker(false);
|
|
|
|
|
client = TransportClient.builder().settings(effectiveSettings).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return client;
|
|
|
|
|
return clientMap.computeIfAbsent(settings.get("cluster.name"),
|
|
|
|
|
key -> innerCreateClient(settings));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void closeClient() {
|
|
|
|
|
synchronized (lock) {
|
|
|
|
|
if (client != null) {
|
|
|
|
|
if (client instanceof Client) {
|
|
|
|
|
((Client) client).close();
|
|
|
|
|
}
|
|
|
|
|
if (client != null) {
|
|
|
|
|
client.threadPool().shutdownNow();
|
|
|
|
|
}
|
|
|
|
|
client = null;
|
|
|
|
|
public void closeClient(Settings settings) {
|
|
|
|
|
ElasticsearchClient client = clientMap.remove(settings.get("cluster.name"));
|
|
|
|
|
if (client != null) {
|
|
|
|
|
if (client instanceof Client) {
|
|
|
|
|
((Client) client).close();
|
|
|
|
|
}
|
|
|
|
|
client.threadPool().shutdownNow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -143,4 +114,29 @@ public class TransportClientHelper {
|
|
|
|
|
transportClient.addTransportAddress(discoveryNode.getAddress());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ElasticsearchClient innerCreateClient(Settings settings) {
|
|
|
|
|
String systemIdentifier = System.getProperty("os.name")
|
|
|
|
|
+ " " + System.getProperty("java.vm.name")
|
|
|
|
|
+ " " + System.getProperty("java.vm.vendor")
|
|
|
|
|
+ " " + System.getProperty("java.vm.version")
|
|
|
|
|
+ " Elasticsearch " + Version.CURRENT.toString();
|
|
|
|
|
Settings effectiveSettings = Settings.builder()
|
|
|
|
|
// for thread pool size
|
|
|
|
|
.put("processors",
|
|
|
|
|
settings.getAsInt("processors", Runtime.getRuntime().availableProcessors()))
|
|
|
|
|
.put("client.transport.sniff", false) // do not sniff
|
|
|
|
|
.put("client.transport.nodes_sampler_interval", "1m") // do not ping
|
|
|
|
|
.put("client.transport.ping_timeout", "1m") // wait for unresponsive nodes a very long time before disconnect
|
|
|
|
|
.put("client.transport.ignore_cluster_name", true) // connect to any cluster
|
|
|
|
|
// custom settings may override defaults
|
|
|
|
|
.put(settings)
|
|
|
|
|
.build();
|
|
|
|
|
logger.info("creating transport client on {} with custom settings {} and effective settings {}",
|
|
|
|
|
systemIdentifier, settings.getAsMap(), effectiveSettings.getAsMap());
|
|
|
|
|
|
|
|
|
|
// we need to disable dead lock check because we may have mixed node/transport clients
|
|
|
|
|
DefaultChannelFuture.setUseDeadLockChecker(false);
|
|
|
|
|
return TransportClient.builder().settings(effectiveSettings).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|