Simplify ES client creation (Node, Local, Transport), and default to Node

Changed default client to be NodeClient so we can listen to IP transport.
This commit is contained in:
Peter Dietz
2012-08-21 17:55:49 -04:00
committed by Peter Dietz
parent f6e495797c
commit 21fdd99bb9

View File

@@ -68,7 +68,9 @@ public class ElasticSearchLogger {
private static Client client; private static Client client;
private static boolean havingTroubles = false; public static enum ClientType {
NODE, LOCAL, TRANSPORT
}
public ElasticSearchLogger() { public ElasticSearchLogger() {
@@ -269,7 +271,7 @@ public class ElasticSearchLogger {
} }
log.info("DSpace ElasticSearchLogger Initialized Successfully (I suppose)"); log.info("DSpace ElasticSearchLogger Initialized Successfully (I suppose)");
havingTroubles=false;
} catch (Exception e) { } catch (Exception e) {
log.info("Elastic Search crashed during init. " + e.getMessage()); log.info("Elastic Search crashed during init. " + e.getMessage());
} }
@@ -405,11 +407,9 @@ public class ElasticSearchLogger {
} catch (RuntimeException re) { } catch (RuntimeException re) {
log.error("RunTimer in ESL:\n" + ExceptionUtils.getStackTrace(re)); log.error("RunTimer in ESL:\n" + ExceptionUtils.getStackTrace(re));
havingTroubles=true;
throw re; throw re;
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
havingTroubles=true;
} finally { } finally {
client.close(); client.close();
} }
@@ -525,10 +525,17 @@ public class ElasticSearchLogger {
} }
// Transport Client will talk to another server on 9300 // Transport Client will talk to another server on 9300
public void createTransportClient(boolean skipCheck) { public void createTransportClient() {
if(havingTroubles && !skipCheck) { // Configurable values for all elasticsearch connection constants
initializeElasticSearch(); clusterName = getConfigurationStringWithFallBack("statistics.elasticsearch.clusterName", clusterName);
} indexName = getConfigurationStringWithFallBack("statistics.elasticsearch.indexName", indexName);
indexType = getConfigurationStringWithFallBack("statistics.elasticsearch.indexType", indexType);
address = getConfigurationStringWithFallBack("statistics.elasticsearch.address", address);
port = ConfigurationManager.getIntProperty("statistics.elasticsearch.port", port);
storeData = ConfigurationManager.getBooleanProperty("statistics.elasticsearch.storeData", storeData);
log.info("Creating TransportClient to [Address:" + address + "] [Port:" + port + "] [cluster.name:" + clusterName + "]");
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build(); Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build();
client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(address, port)); client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(address, port));
} }
@@ -536,45 +543,51 @@ public class ElasticSearchLogger {
public void createElasticClient() { public void createElasticClient() {
//createElasticClient(true); //createElasticClient(true);
} }
public Client getClient() {
//Get an available client, otherwise new default is NODE.
return getClient(ClientType.NODE);
}
// Get the already available client, otherwise we will create a new client. // Get the already available client, otherwise we will create a new client.
// TODO Allow for config to determine which architecture / topology to use. // TODO Allow for config to determine which architecture / topology to use.
// - Local Node, store Data // - Local Node, store Data
// - Node Client, must discover a master within ES cluster // - Node Client, must discover a master within ES cluster
// - Transport Client, specify IP address of server running ES. // - Transport Client, specify IP address of server running ES.
public Client getClient() { public Client getClient(ClientType clientType) {
if(client == null) { if(client == null) {
log.error("getClient reports null client"); log.error("getClient reports null client");
createNodeClient();
if(clientType == ClientType.TRANSPORT) {
createTransportClient();
//createElasticClient(); } else {
createNodeClient(clientType);
}
} }
return client; return client;
} }
// Node Client will discover other ES nodes running in local JVM // Node Client will discover other ES nodes running in local JVM
public void createNodeClient() { public Client createNodeClient(ClientType clientType) {
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().clusterName(clusterName); String dspaceDir = ConfigurationManager.getProperty("dspace.dir");
Settings settings = ImmutableSettings.settingsBuilder().put("path.data", dspaceDir + "/elasticsearch/").build();
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().clusterName(clusterName).data(true).settings(settings);
if(storeData) { if(clientType == ClientType.LOCAL) {
String dspaceDir = ConfigurationManager.getProperty("dspace.dir"); log.info("Create a Local Node.");
Settings settings = ImmutableSettings.settingsBuilder().put("path.data", dspaceDir + "/elasticsearch/").build(); nodeBuilder = nodeBuilder.local(true);
log.info("Create a Local Node that will store data."); } else if(clientType == ClientType.NODE) {
nodeBuilder = nodeBuilder.data(true).local(true).settings(settings); log.info("Create a nodeClient, allows transport clients to connect");
} else { nodeBuilder = nodeBuilder.local(false);
log.info("Create a nodeClient, that looks for a neighbor to be master.");
nodeBuilder = nodeBuilder.client(true);
} }
Node node = nodeBuilder.node(); Node node = nodeBuilder.node();
log.info("Got node"); log.info("Got node");
client = node.client(); client = node.client();
log.info("Created new node client"); log.info("Created new node client");
} return client;
public void createElasticClient(boolean skipCheck) {
log.info("Creating a new elastic-client");
//createTransportClient(skipCheck);
} }
public String getConfigurationStringWithFallBack(String configurationKey, String defaultFallbackValue) { public String getConfigurationStringWithFallBack(String configurationKey, String defaultFallbackValue) {