Gonfi den Tschal
Gonfi den Tschal

Reputation: 1764

ElasticSearch embedded in JVM: wait until ready

What is the proper way of running an embedded ES in version 2? I'm playing with version 2.3.4 and I ran into some troubles, it feels fragile.

This blog http://geekabyte.blogspot.ro/2015/08/embedding-elasticsearch-in-spring.html which was for version 1 works mostly. A configuration now looks something like this:

Settings.Builder settingsBuilder = Settings.builder();
settingsBuilder.put("node.name", "embedded-node");
settingsBuilder.put("path.home", "/existing/dummy/path/");
settingsBuilder.put("path.data", "/path/where/my/data/will/be/stored/");
settingsBuilder.put("http.enabled", false);

New is the requirement for the "path.home". I don't understand why, it's embedded. I've seen people setting it to an ES installation base folder, but then it throws with "Jar hell". Setting it to an existing, empty folder works right now. Question 1: Is there a better way?

Other config options I've copy-pasted from the net are:

settingsBuilder.put("client.transport.sniff", false);
settingsBuilder.put("index.number_of_replicas", 0);
settingsBuilder.put("index.number_of_shards", 1);
settingsBuilder.put("action.write_consistency", "one");

And finally the code to create the node:

NodeBuilder.nodeBuilder()
            .settings(settings)
            .clusterName("embedded-cluster")
            .data(true).local(true).node();

Question 2: Do I need to explicitly tell ES to shut down?

I have seen Difference between close and shutdown a node in elasticsearch? but that seems to be for version 1, in my API there are no such methods.

After indexing data I wait with

this.node.client().admin().indices().refresh(new RefreshRequest(indexName)).actionGet();

to make sure it is properly indexed and available. It was mentioned on Integration test elastic search, timing issue, document not found


After JVM shutdown, and restart with an indexed embedded data store, ES is not available for querying immediately. I get one of these error:

ClusterBlockException[blocked by: [SERVICE_UNAVAILABLE/1/state not recovered / initialized];]

or

Failed to execute phase [query_fetch], all shards failed

My current solution is to wait for 3 seconds between starting the node and querying it:

NodeBuilder.nodeBuilder()...node();
Thread.sleep(3000);
node.client().prepareSearch()...

Question 3: How do I programmatically wait for ES to be ready to serve requests?

Upvotes: 1

Views: 1245

Answers (1)

Gonfi den Tschal
Gonfi den Tschal

Reputation: 1764

See comment from Andrei Stefan:

Before running your search try node.client().admin().cluster().prepareHealth().setWaitForGreenStatus() .execute().actionGet();

In newer Elasticsearch versions, especially from version 6 on, the embedded ES mode is not functional anymore. For an automated test setup this works for me: https://github.com/allegro/embedded-elasticsearch

Upvotes: 1

Related Questions