Search code examples
elasticsearchelasticsearch-jest

How can I make Jest handle an ElasticSearch server being unavailable?


I currently configure Jest by giving it a list of server URIs. Like this:

 public JestClient jestClient() {
    final JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig
            .Builder(esServerUris)
            .build());
    final JestClient jestClient = factory.getObject();
    return jestClient;
}

If one of my ElasticSearch servers go offline (e.g. failure or maintenance), then a percentage of my Jest queries fail. Jest doesn't seem to do any kind of intelligent connection management by default.It must do something like round-robin through the servers or pick a server at random.

Is there a better way to handle this?


Solution

  • You need to enable discovery in order for the client factory to find another server in case one goes down. Something like this should do:

    public JestClient jestClient() {
        final JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig
                .Builder(esServerUris)
                .discoveryEnabled(true)
                .discoveryFrequency(500l, TimeUnit.MILLISECONDS)
                .build());
        final JestClient jestClient = factory.getObject();
        return jestClient;
    }
    

    You can also see how they're testing this in JestClientFactoryIntegrationTest.java, i.e. they start with a single node, then add 3 more nodes, then bring one node down and assert that the client still has a valid connection to a node that is up.