How to recover in Java API due to CurrentState[STARTED] shard is not a primary

I am using Java API to push BulkRequests to Elasticsearch. For testing availability, I killed a data node and got the following DEBUG message in Elasticsearch Data Node log:

[myindex][[myindex][1]] IllegalIndexShardStateException[CurrentState[STARTED] shard is not a primary]
at org.elasticsearch.index.shard.IndexShard.prepareIndexOnPrimary(
at org.elasticsearch.action.index.TransportIndexAction.prepareIndexOperationOnPrimary(
at org.elasticsearch.action.index.TransportIndexAction.executeIndexRequestOnPrimary(
at org.elasticsearch.action.bulk.TransportShardBulkAction.shardIndexOperation(
at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(
at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(
at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(
at org.elasticsearch.transport.TransportService$4.doRun(
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$

However, I did not get any exception in the Java API. How do I recover from such errors?

If you look at the BulkResponse object, you probably got back a lot of individual errors, no?

also, for some of these errors, we do wait until shard is in the right state and do a retry, which means that the document could still have been successfully indexed. As @dadoonet said, you have to check the BulkResponse object for failures (e.g. using method hasFailures()).

I do check for errors by iterating over the array of BulkItemResponse for all responses to detect errors such as MapperParsingExceptions. But, I haven't found any errors that correspond to the one I am seeing. Am I doing something wrong?

As I've said, all is good then. The log message (which is at DEBUG level) just indicates that an index request which was part of the bulk request could not be written right away to the shard (as it wasn't promoted to primary yet). The indexing request was, however, internally retried shortly after and succeeded before the BulkResponse was returned.

1 Like