Created
May 10, 2012 09:06
-
-
Save jprante/2652024 to your computer and use it in GitHub Desktop.
A tiny step towards reproducing StackOverflowError
This file contains hidden or 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
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.net.InetAddress; | |
import java.net.NetworkInterface; | |
import java.net.URI; | |
import java.net.UnknownHostException; | |
import java.util.Collections; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Properties; | |
import java.util.Set; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import org.elasticsearch.Version; | |
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; | |
import org.elasticsearch.client.Client; | |
import org.elasticsearch.client.transport.TransportClient; | |
import org.elasticsearch.cluster.node.DiscoveryNode; | |
import org.elasticsearch.common.settings.ImmutableSettings; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.common.transport.InetSocketTransportAddress; | |
/* | |
javac -cp lib/elasticsearch-0.19.3.jar ElasticsearchClient.java | |
java -cp .:lib/elasticsearch-0.19.3.jar ElasticsearchClient | |
*/ | |
public class ElasticsearchClient { | |
private static final Logger logger = Logger.getLogger(ElasticsearchClient.class.getName()); | |
public final static String DEFAULT_CLUSTER_NAME = "elasticsearch"; | |
private final static URI DEFAULT_URI = URI.create("es://interfaces:9300"); | |
private final static Map<URI, ElasticsearchClient> instances = new HashMap(); | |
private final Set<InetSocketTransportAddress> addresses = new HashSet(); | |
private TransportClient client; | |
private URI uri; | |
private String clusterName; | |
private ElasticsearchClient(URI uri, String clusterName) { | |
this.uri = uri; | |
this.clusterName = clusterName; | |
this.client = createClient(); | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
System.err.println("shutdown hook: closing client"); | |
try { | |
client.close(); | |
} catch (Exception e) { | |
System.err.println("shutdown hook: exception while client close"); | |
e.printStackTrace(); | |
} | |
System.err.println("shutdown hook: closing client thread pool"); | |
try { | |
client.threadPool().shutdownNow(); | |
} catch (Exception e) { | |
System.err.println("shutdown hook: exception while client thread pool close"); | |
e.printStackTrace(); | |
} | |
System.err.println("shutdown hook: done"); | |
} | |
}); | |
} | |
public static ElasticsearchClient getInstance() { | |
return getInstance(DEFAULT_URI, DEFAULT_CLUSTER_NAME); | |
} | |
public static ElasticsearchClient getInstance(String clusterName) { | |
return getInstance(DEFAULT_URI, clusterName); | |
} | |
public static ElasticsearchClient getInstance(URI uri, String clusterName) { | |
if (!instances.containsKey(uri)) { | |
instances.put(uri, new ElasticsearchClient(uri, clusterName)); | |
} | |
return instances.get(uri); | |
} | |
public void open() throws IOException { | |
String hostname = uri.getHost(); | |
int port = uri.getPort(); // beware: 9300, not 9200 | |
boolean newnodes = false; | |
if ("es".equals(uri.getScheme())) { | |
if ("hostname".equals(uri.getHost())) { | |
InetSocketTransportAddress address = new InetSocketTransportAddress(getHostname(), port); | |
if (!addresses.contains(address)) { | |
logger.log(Level.INFO, "adding hostname address for transport client = {0}", address); | |
client.addTransportAddress(address); | |
logger.log(Level.INFO, "hostname address added"); | |
addresses.add(address); | |
newnodes = true; | |
} | |
} | |
else if ("interfaces".equals(uri.getHost())) { | |
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); | |
for (NetworkInterface netint : Collections.list(nets)) { | |
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); | |
for (InetAddress addr : Collections.list(inetAddresses)) { | |
InetSocketTransportAddress address = new InetSocketTransportAddress(addr, port); | |
if (!addresses.contains(address)) { | |
logger.log(Level.INFO, "adding interface address for transport client = {0}", address); | |
client.addTransportAddress(address); | |
addresses.add(address); | |
newnodes = true; | |
} | |
} | |
} | |
} | |
} else { | |
InetSocketTransportAddress address = new InetSocketTransportAddress(hostname, port); | |
if (!addresses.contains(address)) { | |
logger.log(Level.INFO, "adding custom address for transport client = {0}", address); | |
client.addTransportAddress(address); | |
addresses.add(address); | |
newnodes = true; | |
} | |
} | |
logger.log(Level.INFO, "addresses = {0}", addresses); | |
if (newnodes) { | |
List<DiscoveryNode> nodes = client.connectedNodes().asList(); | |
logger.log(Level.INFO, "number of connected nodes = {0}", nodes); | |
for (DiscoveryNode node : nodes) { | |
logger.log(Level.INFO, "new connection to {0} {1}", new Object[]{node.getId(), node.getName()}); | |
} | |
} | |
isReady(); | |
} | |
private TransportClient createClient() { | |
logger.log(Level.INFO, "starting discovery for cluster = {0}", clusterName); | |
Settings settings = ImmutableSettings.settingsBuilder() | |
.put("cluster.name", clusterName) | |
.put("client.transport.sniff", true) | |
.build(); | |
this.client = new TransportClient(settings); | |
return client; | |
} | |
public Client getClient() { | |
return client; | |
} | |
public void isReady() throws IOException { | |
ClusterHealthResponse healthResponse = | |
client.admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout("30s").execute().actionGet(); | |
if (healthResponse.isTimedOut()) { | |
throw new IOException("cluster not ready, cowardly refusing to continue"); | |
} | |
} | |
public String getHostname() throws UnknownHostException { | |
InetAddress addr = InetAddress.getLocalHost(); | |
return addr.getHostName(); | |
} | |
public static void main(String[] args) throws Exception { | |
final String clusterName = args.length > 0 ? args[0] : "elasticsearch"; | |
for (int i = 0; i < 12; i++) { | |
Thread t = new Thread() { | |
public void run() { | |
ElasticsearchClient client = ElasticsearchClient.getInstance(DEFAULT_URI, clusterName); | |
try { | |
client.open(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
}; | |
t.run(); | |
} | |
Thread.sleep(10000L); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment