# Can anyone explain this exception

**URL:** https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475
**Category:** Elasticsearch
**Created:** [September 29, 2011, 11:24am UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475 "2011-09-29T11:24:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Steff](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Steff](https://discuss.elastic.co/u/Steff)
#### Post date: [September 29, 2011, 11:24am UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/1 "2011-09-29T11:24:33Z")

</div>

I sometimes get the following exception when running one of my  
unittests against ES. The exception is returned to me in a  
ActionListener.onFailure object that I give as a  
parameter when I call execute on SearchRequestBuilder. I never get the  
exception when running the test on my local Mac, but when the test is  
run on our TeamCity machine (Ubuntu) the exception occurs 90% of the  
time.

org.elasticsearch.cluster.block.ClusterBlockException: blocked by: [1/  
state not recovered / initialized];[2/no master];  
at  
org.elasticsearch.cluster.block.ClusterBlocks.indexBlockedException(ClusterBlocks.java:  
131)  
at  
org.elasticsearch.cluster.block.ClusterBlocks.indexBlockedRaiseException(ClusterBlocks.java:  
115)  
at org.elasticsearch.action.search.type.TransportSearchTypeAction  
$BaseAsyncAction.(TransportSearchTypeAction.java:116)  
at  
org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction  
$AsyncAction.(TransportSearchQueryThenFetchAction.java:72)  
at  
org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction  
$AsyncAction.(TransportSearchQueryThenFetchAction.java:63)  
at  
org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction.doExecute(TransportSearchQueryThenFetchAction.java:  
60)  
at  
org.elasticsearch.action.search.type.TransportSearchQueryThenFetchAction.doExecute(TransportSearchQueryThenFetchAction.java:  
52)  
at  
org.elasticsearch.action.support.BaseAction.execute(BaseAction.java:  
61)  
at  
org.elasticsearch.action.search.TransportSearchAction.doExecute(TransportSearchAction.java:  
112)  
at  
org.elasticsearch.action.search.TransportSearchAction.doExecute(TransportSearchAction.java:  
49)  
at  
org.elasticsearch.action.support.BaseAction.execute(BaseAction.java:  
61)  
at org.elasticsearch.client.node.NodeClient.search(NodeClient.java:  
186)  
at  
org.elasticsearch.client.action.search.SearchRequestBuilder.doExecute(SearchRequestBuilder.java:  
589)  
at  
org.elasticsearch.client.action.support.BaseRequestBuilder.execute(BaseRequestBuilder.java:  
56)  
at ... my junit-test code ...

The test is running 10 threads doing indexing (inserting) and  
reindexing (updating) the on "the same" documents in ES, so it relying  
on the "optimistic locking based on versioning" and "unique constraint  
on \_id" to work perfectly (they probably are, but I am not able to  
interpret the exception here).

Regards, Per Steffensen

---

<div class="post-metadata">

### Author: ![Steff](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Steff](https://discuss.elastic.co/u/Steff)
#### Post date: [September 29, 2011, 12:00pm UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/2 "2011-09-29T12:00:03Z")

</div>

I imagine that it might be because the node hasnt completely started  
before I start using it. I do the following in a @Before in my  
unittest:  
Node node = nodeBuilder()  
.local(true)  
.settings(ImmutableSettings.settingsBuilder()  
.put("index.number\_of\_shards", shards)  
.put("index.number\_of\_replicas", replicas).build())  
.build()  
.start();

Can I somehow wait for the node to be completely started? Looking at  
the code I do not see a way to do that?

Regards, Per Steffensen

---

<div class="post-metadata">

### Author: ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)
#### Post date: [September 29, 2011, 1:38pm UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/3 "2011-09-29T13:38:05Z")

</div>

Wait for yellow would probably too expensive for every @Before

> <https://github.com/karussell/Jetwick/blob/master/src/main/java/de/jetwick/es/AbstractElasticSearch.java#L142>

So I suggest to do node initialization in @BeforeClass and then put a  
deleteIndex in @Before

In my unit tests I don't even need to wait for yellow state

On 29 Sep., 14:00, Steff [st...@designware.dk](mailto:st...@designware.dk) wrote:

> I imagine that it might be because the node hasnt completely started  
> before I start using it. I do the following in a @Before in my  
> unittest:  
> Node node = nodeBuilder()  
> .local(true)  
> .settings(ImmutableSettings.settingsBuilder()  
> .put("index.number\_of\_shards", shards)  
> .put("index.number\_of\_replicas", replicas).build())  
> .build()  
> .start();
> 
> Can I somehow wait for the node to be completely started? Looking at  
> the code I do not see a way to do that?
> 
> Regards, Per Steffensen

---

<div class="post-metadata">

### Author: ![Steff](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Steff](https://discuss.elastic.co/u/Steff)
#### Post date: [September 30, 2011, 7:03am UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/4 "2011-09-30T07:03:41Z")

</div>

I know it is not what Karussell suggested, but now I have the  
following in my unittest class, and it never fails on the first test  
but often on the second test. Guess it is due to the node not finished  
its close before another note is started. Can anyone be more detailed  
about what to do in @BeforeClass, @AfterClass, @Before and @After in  
unittests to write good stable tests against ES. Thanks!

```
@Before
public void before()
{
    node = Node node = nodeBuilder()
    .local(true)
    .settings(ImmutableSettings.settingsBuilder()
            .put("index.number_of_shards", 1)
            .put("index.number_of_replicas", 0).build())
    .build()
    .start();
}

@After
public void after()
{
	for (String indexName : createdIndices) {

```

client.admin().indices().delete(Requests.deleteIndexRequest(indexName)).actionGet();  
}

```
    if (node != null)
    {
        node.close();
        node = null;
    }
}

```

Karussell suggests to wait for yellow to be sure that the node has  
completely started. But waiting for yellow requires an index.  
Basically I just want to know when the node has completely started -  
even though there might be no indexes yet.

Regards, Per Steffensen

On 29 Sep., 15:38, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> Wait for yellow would probably too expensive for every @Before
> 
> [https://github.com/karussell/Jetwick/blob/master/src/main/java/de/jet](https://github.com/karussell/Jetwick/blob/master/src/main/java/de/jet)...
> 
> So I suggest to do node initialization in @BeforeClass and then put a  
> deleteIndex in @Before
> 
> In my unit tests I don't even need to wait for yellow state
> 
> On 29 Sep., 14:00, Steff [st...@designware.dk](mailto:st...@designware.dk) wrote:
> 
> > I imagine that it might be because the node hasnt completely started  
> > before I start using it. I do the following in a @Before in my  
> > unittest:  
> > Node node = nodeBuilder()  
> > .local(true)  
> > .settings(ImmutableSettings.settingsBuilder()  
> > .put("index.number\_of\_shards", shards)  
> > .put("index.number\_of\_replicas", replicas).build())  
> > .build()  
> > .start();
> 
> > Can I somehow wait for the node to be completely started? Looking at  
> > the code I do not see a way to do that?
> 
> > Regards, Per Steffensen

---

<div class="post-metadata">

### Author: ![Steff](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Steff](https://discuss.elastic.co/u/Steff)
#### Post date: [September 30, 2011, 7:24am UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/5 "2011-09-30T07:24:32Z")

</div>

On 30 Sep., 09:03, Steff [st...@designware.dk](mailto:st...@designware.dk) wrote:

> I know it is not what Karussell suggested, but now I have the  
> following in my unittest class, and it never fails on the first test  
> but often on the second test. Guess it is due to the node not finished  
> its close before another note is started. Can anyone be more detailed  
> about what to do in @BeforeClass, @AfterClass, @Before and @After in  
> unittests to write good stable tests against ES. Thanks!
> 
> ```
> @Before
> public void before()
> {
> node = Node node = nodeBuilder()
> .local(true)
> .settings(ImmutableSettings.settingsBuilder()
> .put("index.number_of_shards", 1)
> .put("index.number_of_replicas", 0).build())
> .build()
> .start();
> }
> 
> @After
> public void after()
> {
> for (String indexName : createdIndices) {
> 
> ```
> 
> client.admin().indices().delete(Requests.deleteIndexRequest(indexName)).actionGet();  
> }
> 
> ```
> if (node != null)
> {
> node.close();
> node = null;
> }
> }
> 
> ```
> 
> Karussell suggests to wait for yellow to be sure that the node has  
> completely started. But waiting for yellow requires an index.  
> Basically I just want to know when the node has completely started -  
> even though there might be no indexes yet.
> 
> Regards, Per Steffensen

Forgot to say that the tests themselves create the indices they need  
and add them to the list createdIndices, so that they will be deleted  
in after(). The tests use the following code to create the indices  
they need:

```
private void createIndex(String indexName) {
	if (!createdIndices.contains(indexName)) {
    	String mappingDescription = "{\"" + INDEX_TYPE + "\" : {" +

```

""properties" : {" + getESMappingStr() + "}}}";  
AdminClient admin = client.admin();  
Settings settings =  
ImmutableSettings.settingsBuilder().put("number\_of\_shards",  
1).put("number\_of\_replicas", 0).build();  
CreateIndexRequest request =  
Requests.createIndexRequest(indexName).settings(settings).mapping(INDEX\_TYPE,  
mappingDescription);  
CreateIndexResponse result =  
admin.indices().create(request).actionGet();  
// wait for ready  
admin.cluster().health(new  
ClusterHealthRequest(indexName).waitForYellowStatus()).actionGet();  
createdIndices.add(indexName);  
}  
}

Regards, Per Steffensen

---

<div class="post-metadata">

### Author: ![Steff](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Steff](https://discuss.elastic.co/u/Steff)
#### Post date: [September 30, 2011, 2:17pm UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/6 "2011-09-30T14:17:28Z")

</div>

My problem seemed to be due to the fact that all the threads in the  
test used their own node/client. I have changed so that they all share  
the same node/client, and now the test is constantly green. Guess  
conclusion is that you should not have more than one node/client in  
the same JVM 🙂

Regards, Per Steffensen

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [October 2, 2011, 12:56pm UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/7 "2011-10-02T12:56:12Z")

</div>

You can have more than one instance of a local node in the same JVM, but,  
they will discover each other (within the same classloader, using static  
vars). I am not sure what your tests do, but if you need them to use  
different "clusters", you can use a different cluster name for the node(s)  
you start per test.

On Fri, Sep 30, 2011 at 5:17 PM, Steff [steff@designware.dk](mailto:steff@designware.dk) wrote:

> My problem seemed to be due to the fact that all the threads in the  
> test used their own node/client. I have changed so that they all share  
> the same node/client, and now the test is constantly green. Guess  
> conclusion is that you should not have more than one node/client in  
> the same JVM 🙂
> 
> Regards, Per Steffensen

---

<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:53am UTC](https://discuss.elastic.co/t/can-anyone-explain-this-exception/5475/8 "2017-07-06T03:53:06Z")

</div>


