How to know whether elastic is down or not

I have a requirement where i need to apply patches, create indices through resthighlevelclient and use elastic if elastic is up , otherwise switch to postgres.

let's suppose elastic service is down. when i try to apply patch , through resthighlevelclient it gives connection refused error and fails. Before applying patches or creating indices ,can we somehow know that elastic is down, through resthighlevelclient?

I'm calling info() for that.

@dadoonet
info is also giving connection refused exception
image .
It is not returning any information.

Please don't post images of text as they are hard to read, may not display correctly for everyone, and are not searchable.

Instead, paste the text and format it with </> icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

Here most likely I'd wrap the info() call in its own try - catch.

@dadoonet i have tried with separate try catch , same result : java.net.ConnectException: Connection refused

RestClientBuilder clientBuilder = getConnection(dataSource);
try (RestHighLevelClient client = new RestHighLevelClient(clientBuilder)) {
try {
MainResponse rs=client.info( RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
applyPatchToElasticsearch(client, clusterName);
} catch (IOException e) {
logger.error("PATCH :Unable to Apply Patch for cluster {0}", e, clusterName);
throw new PatchManagerException("Unable to apply Patch", e);
} catch (ElasticSearchPatchException e) {
logger.error("PATCH :Unable to apply patch for cluster : {0}", e, clusterName);
throw new PatchManagerException("Unable to apply Patch", e);
} catch (Exception e) {
logger.error("PATCH :Unable to apply patch for cluster : {0}", e, clusterName);
throw new PatchManagerException("Unable to apply Patch", e);
}

And what's wrong with that?

It indicates that the cluster is not reachable. What kind of other information are you expecting?

Also please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.

@dadoonet In this way, through exception handling, we need to understand whether the elastic cluster is up or not.
Is there a way without exception handling?

What I'd do to answer this question is:

try {
        MainResponse info = this.esClient.info(RequestOptions.DEFAULT);
        logger.info("Connected to {} running version {}", clusterUrl, info.getVersion().getNumber());
        try {
            this.esClient.indices().create(new CreateIndexRequest("myindex"), RequestOptions.DEFAULT);
            logger.info("New index myindex has been created");
        } catch (ElasticsearchStatusException e) {
            if (e.status().getStatus() != 400) {
                logger.warn("can not create index and mappings", e);
            } else {
                logger.debug("Index myindex was already existing. Skipping creating it again.");
            }
        }
} catch (IOException e) {
        // Fail the process here
}

If you need something else, could you clarify the exact use case?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.