# Junit issue with node(local): No shard available

**URL:** https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943
**Category:** Elasticsearch
**Created:** [March 8, 2012, 6:32am UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943 "2012-03-08T06:32:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Christian\_von\_Wendt\_](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@Christian\_von\_Wendt\_](https://discuss.elastic.co/u/Christian_von_Wendt_)
#### Post date: [March 8, 2012, 6:32am UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943/1 "2012-03-08T06:32:12Z")

</div>

Hi,

I'm having a junit setup with a local node with data, and on  
"@BeforeClass", I initialize the node like this:

```
@BeforeClass
public static void init()
{
    node = nodeBuilder().local(true).data(true).node();
    client = node.client();
    service = new ElasticSearchServiceImpl();
    service.setClient(client);
}

```

Before each test, I create an index:

```
@Before
public void before()
{
    createIndex();
}

private static void createIndex() {

```

client.admin().indices().prepareCreate("lists").execute().actionGet();  
client.admin().indices().refresh(new  
RefreshRequest("lists")).actionGet();  
}

After each test, I delete the index:

```
@AfterClass
public static void cleanup() {
    client.close();
    node.close();
}

private static void deleteIndex() {

```

client.admin().indices().prepareDelete("lists").execute().actionGet();  
}

The issue arises when I execute my test:

...  
GetResponse response = client.prepareGet("lists",  
Document.Type.INTERCHANGE.name(), originId).execute().actionGet();  
...

This call results in the following stacktrace:

org.elasticsearch.action.NoShardAvailableActionException: [lists][3] No  
shard available for [[lists][INTERCHANGE][id:1]: routing [null]]  
at  
org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$AsyncSingleAction.perform(TransportShardSingleOperationAction.java:140)  
at  
org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$AsyncSingleAction.start(TransportShardSingleOperationAction.java:125)  
at  
org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction.doExecute(TransportShardSingleOperationAction.java:72)  
at  
org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction.doExecute(TransportShardSingleOperationAction.java:47)  
at  
org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:61)  
at org.elasticsearch.client.node.NodeClient.execute(NodeClient.java:83)  
at  
org.elasticsearch.client.support.AbstractClient.get(AbstractClient.java:171)  
at  
org.elasticsearch.action.get.GetRequestBuilder.doExecute(GetRequestBuilder.java:126)  
at  
org.elasticsearch.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:53)  
at  
org.elasticsearch.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:47)  
at  
dk.cooldev.elasticsearch.service.impl.ElasticSearchServiceImpl.findById(ElasticSearchServiceImpl.java:19)

I'm wondering why I am able to create and delete the index, but not use it  
to find document? Is there some setting, I'm missing?

---

<div class="post-metadata">

### Author: ![Paul\_Loy](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@Paul\_Loy](https://discuss.elastic.co/u/Paul_Loy)
#### Post date: [March 8, 2012, 6:51am UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943/2 "2012-03-08T06:51:30Z")

</div>

It may be a timing issue. You can use the admin client to check if the  
index exists before starting the test phase. We ran ES embedded and had to  
do that before indexing or searching for anything.

On Wed, Mar 7, 2012 at 10:32 PM, Christian von Wendt-Jensen \<  
[csj.the.man@gmail.com](mailto:csj.the.man@gmail.com)\> wrote:

> Hi,
> 
> I'm having a junit setup with a local node with data, and on  
> "@BeforeClass", I initialize the node like this:
> 
> ```
> @BeforeClass
> public static void init()
> {
> node = nodeBuilder().local(true).data(true).node();
> client = node.client();
> service = new ElasticSearchServiceImpl();
> service.setClient(client);
> }
> 
> ```
> 
> Before each test, I create an index:
> 
> ```
> @Before
> public void before()
> {
> createIndex();
> }
> 
> private static void createIndex() {
> 
> ```
> 
> client.admin().indices().prepareCreate("lists").execute().actionGet();  
> client.admin().indices().refresh(new  
> RefreshRequest("lists")).actionGet();  
> }
> 
> After each test, I delete the index:
> 
> ```
> @AfterClass
> public static void cleanup() {
> client.close();
> node.close();
> }
> 
> private static void deleteIndex() {
> 
> ```
> 
> client.admin().indices().prepareDelete("lists").execute().actionGet();  
> }
> 
> The issue arises when I execute my test:
> 
> ...  
> GetResponse response = client.prepareGet("lists",  
> Document.Type.INTERCHANGE.name(), originId).execute().actionGet();  
> ...
> 
> This call results in the following stacktrace:
> 
> org.elasticsearch.action.NoShardAvailableActionException: [lists][3] No  
> shard available for [[lists][INTERCHANGE][id:1]: routing [null]]  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$AsyncSingleAction.perform(TransportShardSingleOperationAction.java:140)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction$AsyncSingleAction.start(TransportShardSingleOperationAction.java:125)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction.doExecute(TransportShardSingleOperationAction.java:72)  
> at  
> org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction.doExecute(TransportShardSingleOperationAction.java:47)  
> at  
> org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:61)  
> at org.elasticsearch.client.node.NodeClient.execute(NodeClient.java:83)  
> at  
> org.elasticsearch.client.support.AbstractClient.get(AbstractClient.java:171)  
> at  
> org.elasticsearch.action.get.GetRequestBuilder.doExecute(GetRequestBuilder.java:126)  
> at  
> org.elasticsearch.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:53)  
> at  
> org.elasticsearch.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:47)  
> at  
> dk.cooldev.elasticsearch.service.impl.ElasticSearchServiceImpl.findById(ElasticSearchServiceImpl.java:19)
> 
> I'm wondering why I am able to create and delete the index, but not use it  
> to find document? Is there some setting, I'm missing?

## --

Paul Loy  
[paul@keteracel.com](mailto:paul@keteracel.com)  
[http://uk.linkedin.com/in/paulloy](http://uk.linkedin.com/in/paulloy)

---

<div class="post-metadata">

### Author: ![Christian\_von\_Wendt\_](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@Christian\_von\_Wendt\_](https://discuss.elastic.co/u/Christian_von_Wendt_)
#### Post date: [March 8, 2012, 7:19am UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943/3 "2012-03-08T07:19:52Z")

</div>

The funny thing is that I'm actually trying to ensure that the index exists  
beforehand by calling:

```
    client.admin().indices().refresh(new 

```

RefreshRequest("lists")).actionGet();

And when I'm deleting the index, no exception rises.

Also, I've tried to set a breakpoint before the call to

```
    GetResponse response = client.prepareGet("lists", 

```

Document.Type.INTERCHANGE.name [http://document.type.interchange.name/](http://document.type.interchange.name/)(),  
originId).execute().actionGet();

but no matter how long I wait, the exception will arise. Right after the  
test fails, the index is deleted with no fuss...

---

<div class="post-metadata">

### Author: ![Christian\_von\_Wendt\_](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@Christian\_von\_Wendt\_](https://discuss.elastic.co/u/Christian_von_Wendt_)
#### Post date: [March 8, 2012, 9:05am UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943/4 "2012-03-08T09:05:45Z")

</div>

Hi,

I've found the solution by digging into other posts with similar issues. As  
keteracel suggests, it is a timing issue. I thought that my index was ready  
when I called:

```
client.admin().indices().prepareCreate("lists").execute().actionGet();

```

- client.admin().indices().refresh(new  
RefreshRequest("lists")).actionGet();
- 

But it seems that it is not. What I had to do was to wait for the first  
shard to become available. I do this with the following call:

```
node.client().admin().cluster().health(new 

```

ClusterHealthRequest("lists").waitForActiveShards(1)).actionGet();

Hereafter, I can do all the good stuff with the index.

---

<div class="post-metadata">

### Author: ![Frederic](https://avatars.discourse-cdn.com/v4/letter/f/4491bb/32.png) [@Frederic](https://discuss.elastic.co/u/Frederic)
#### Post date: [March 8, 2012, 3:16pm UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943/5 "2012-03-08T15:16:26Z")

</div>

Hi,

You may also use

getClient().admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();

(either Green or Yellow) to be sure the index is initialized

Cheers,

Frederic

On Thursday, 8 March 2012 06:05:45 UTC-3, Christian von Wendt-Jensen wrote:

> Hi,
> 
> I've found the solution by digging into other posts with similar issues.  
> As keteracel suggests, it is a timing issue. I thought that my index was  
> ready when I called:
> 
> ```
> client.admin().indices().prepareCreate("lists").execute().actionGet();
> 
> ```
> 
> - client.admin().indices().refresh(new  
> RefreshRequest("lists")).actionGet();
> - 
> 
> But it seems that it is not. What I had to do was to wait for the first  
> shard to become available. I do this with the following call:
> 
> ```
> node.client().admin().cluster().health(new 
> 
> ```
> 
> ClusterHealthRequest("lists").waitForActiveShards(1)).actionGet();
> 
> Hereafter, I can do all the good stuff with the index.

---

<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 6, 2017, 3:36am UTC](https://discuss.elastic.co/t/junit-issue-with-node-local-no-shard-available/6943/6 "2017-07-06T03:36:39Z")

</div>


