Junit issue with node(local): No shard available

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?

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> 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
http://uk.linkedin.com/in/paulloy

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

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.

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.