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

**URL:** <https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713>\
**Category:** Elasticsearch\
**Created:** [July 29, 2016, 9:54am UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713 "2016-07-29T09:54:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anishm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anishm/32/28796_2.png) [@anishm](https://discuss.elastic.co/u/anishm)\
**Post date:** [July 29, 2016, 9:54am UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713/1 "2016-07-29T09:54:05Z")

</div>

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(IndexShard.java:557)  
> at org.elasticsearch.action.index.TransportIndexAction.prepareIndexOperationOnPrimary(TransportIndexAction.java:212)  
> at org.elasticsearch.action.index.TransportIndexAction.executeIndexRequestOnPrimary(TransportIndexAction.java:224)  
> at org.elasticsearch.action.bulk.TransportShardBulkAction.shardIndexOperation(TransportShardBulkAction.java:326)  
> at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:119)  
> at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:68)  
> at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryPhase.doRun(TransportReplicationAction.java:639)  
> at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)  
> at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler.messageReceived(TransportReplicationAction.java:279)  
> at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler.messageReceived(TransportReplicationAction.java:271)  
> at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:75)  
> at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:376)  
> at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)  
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
> at java.lang.Thread.run(Thread.java:745)

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

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 29, 2016, 10:07am UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713/2 "2016-07-29T10:07:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [July 29, 2016, 10:10am UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713/3 "2016-07-29T10:10:14Z")

</div>

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()).

---

<div class="post-metadata">

**Author:** ![anishm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anishm/32/28796_2.png) [@anishm](https://discuss.elastic.co/u/anishm)\
**Post date:** [July 29, 2016, 10:47am UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713/4 "2016-07-29T10:47:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [July 29, 2016, 12:27pm UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713/5 "2016-07-29T12:27:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:31pm UTC](https://discuss.elastic.co/t/how-to-recover-in-java-api-due-to-currentstate-started-shard-is-not-a-primary/56713/6 "2017-07-05T22:31:45Z")

</div>


