Ben George
Ben George

Reputation: 1005

ElasticSearch why call close() on client or node?

Is there a reason why you would call close() on a client or node ? What are the impacts if you do not ?

Node node = nodeBuilder().client(true).node();
Client client = node.client();
// index some stuff
client.close(); // <-- why do this ?
node.close(); // <-- why do this ?

We have a single client that has a life-cycle matching our application. Am wondering if we need bother calling client.close() and or node.close() at application tear down ?

Upvotes: 0

Views: 4026

Answers (2)

Stephen C
Stephen C

Reputation: 719416

Consider this code snippet:

// on startup

Node node = nodeBuilder().node();
Client client = node.client();

// on shutdown

node.close();

According to the documentation, when you create a Node it joins the local elastic search cluster. That means that announces itself to the other nodes in in the cluster ... and presumably starts listening for requests from other nodes or clients.

When your application exits, and the JVM terminates, the node obviously cannot handle requests from other nodes anymore.

  • If your application calls node.close() as above, the node will tell the other nodes that it is going away, so that the other nodes will know not to send any more requests.

  • If your application doesn't call node.close(), then the other nodes won't know that this node has gone away, and may continue to send it requests. These requests will fail, potentially resulting in exceptions, log messages and so on. At the very least, sending requests to node that cannot respond is a waste of time / resources.

Obviously, this is moot if your application is the only node in the cluster. However, it is a bad idea to make that assumption, and it is probably worth the small overhead of calling close() on (at least) the current application's Node object.

Upvotes: 3

dadoonet
dadoonet

Reputation: 14512

It's definitely better if you can share the same client instance for your whole application.

So don't open a new node and a new client each time you need it. Share it (use a factory).

When you stop your application (or your JVM), it's better to close the node instead of having other nodes getting that information as an exception.

Not sure I answered to your question though!

Upvotes: 2

Related Questions