Search in sources :

Example 6 with IndexClosedException

use of org.elasticsearch.indices.IndexClosedException in project elasticsearch by elastic.

the class GatewayIndexStateIT method testSimpleOpenClose.

public void testSimpleOpenClose() throws Exception {
    logger.info("--> starting 2 nodes");
    internalCluster().startNodes(2);
    logger.info("--> creating test index");
    createIndex("test");
    NumShards test = getNumShards("test");
    logger.info("--> waiting for green status");
    ensureGreen();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
    logger.info("--> closing test index...");
    client().admin().indices().prepareClose("test").get();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
    logger.info("--> verifying that the state is green");
    ensureGreen();
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setTimeout("1s").execute().actionGet();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> creating another index (test2) by indexing into it");
    client().prepareIndex("test2", "type1", "1").setSource("field1", "value1").execute().actionGet();
    logger.info("--> verifying that the state is green");
    ensureGreen();
    logger.info("--> opening the first index again...");
    client().admin().indices().prepareOpen("test").execute().actionGet();
    logger.info("--> verifying that the state is green");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> trying to get the indexed document on the first index");
    GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    logger.info("--> closing test index...");
    client().admin().indices().prepareClose("test").execute().actionGet();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
    logger.info("--> restarting nodes...");
    internalCluster().fullRestart();
    logger.info("--> waiting for two nodes and green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test"), nullValue());
    logger.info("--> trying to index into a closed index ...");
    try {
        client().prepareIndex("test", "type1", "1").setSource("field1", "value1").setTimeout("1s").execute().actionGet();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> opening index...");
    client().admin().indices().prepareOpen("test").execute().actionGet();
    logger.info("--> waiting for green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().execute().actionGet();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(test.numPrimaries));
    assertThat(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), equalTo(test.totalNumShards));
    logger.info("--> trying to get the indexed document on the first round (before close and shutdown)");
    getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "2").setSource("field1", "value1").execute().actionGet();
}
Also used : IndexClosedException(org.elasticsearch.indices.IndexClosedException) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 7 with IndexClosedException

use of org.elasticsearch.indices.IndexClosedException in project elasticsearch by elastic.

the class IndexNameExpressionResolverTests method testIndexOptionsSingleIndexNoExpandWildcards.

public void testIndexOptionsSingleIndexNoExpandWildcards() {
    MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("foo").putAlias(AliasMetaData.builder("foofoobar"))).put(indexBuilder("foobar").putAlias(AliasMetaData.builder("foofoobar"))).put(indexBuilder("foofoo-closed").state(IndexMetaData.State.CLOSE)).put(indexBuilder("foofoo").putAlias(AliasMetaData.builder("barbaz")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
    try {
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictSingleIndexNoExpandForbidClosed());
        indexNameExpressionResolver.concreteIndexNames(context, "baz*");
        fail();
    } catch (IndexNotFoundException e) {
        assertThat(e.getIndex().getName(), equalTo("baz*"));
    }
    try {
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictSingleIndexNoExpandForbidClosed());
        indexNameExpressionResolver.concreteIndexNames(context, "foo", "baz*");
        fail();
    } catch (IndexNotFoundException e) {
        assertThat(e.getIndex().getName(), equalTo("baz*"));
    }
    try {
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictSingleIndexNoExpandForbidClosed());
        indexNameExpressionResolver.concreteIndexNames(context, "foofoobar");
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Alias [foofoobar] has more than one indices associated with it"));
    }
    try {
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictSingleIndexNoExpandForbidClosed());
        indexNameExpressionResolver.concreteIndexNames(context, "foo", "foofoobar");
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("Alias [foofoobar] has more than one indices associated with it"));
    }
    try {
        IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictSingleIndexNoExpandForbidClosed());
        indexNameExpressionResolver.concreteIndexNames(context, "foofoo-closed", "foofoobar");
        fail();
    } catch (IndexClosedException e) {
        assertThat(e.getMessage(), equalTo("closed"));
        assertEquals(e.getIndex().getName(), "foofoo-closed");
    }
    IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictSingleIndexNoExpandForbidClosed());
    String[] results = indexNameExpressionResolver.concreteIndexNames(context, "foo", "barbaz");
    assertEquals(2, results.length);
    assertThat(results, arrayContainingInAnyOrder("foo", "foofoo"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexClosedException(org.elasticsearch.indices.IndexClosedException) ClusterName(org.elasticsearch.cluster.ClusterName) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 8 with IndexClosedException

use of org.elasticsearch.indices.IndexClosedException in project elasticsearch by elastic.

the class IndexNameExpressionResolver method concreteIndices.

Index[] concreteIndices(Context context, String... indexExpressions) {
    if (indexExpressions == null || indexExpressions.length == 0) {
        indexExpressions = new String[] { MetaData.ALL };
    }
    MetaData metaData = context.getState().metaData();
    IndicesOptions options = context.getOptions();
    boolean failClosed = options.forbidClosedIndices() && options.ignoreUnavailable() == false;
    boolean failNoIndices = options.ignoreUnavailable() == false;
    // or multiple indices are specified yield different behaviour.
    if (indexExpressions.length == 1) {
        failNoIndices = options.allowNoIndices() == false;
    }
    List<String> expressions = Arrays.asList(indexExpressions);
    for (ExpressionResolver expressionResolver : expressionResolvers) {
        expressions = expressionResolver.resolve(context, expressions);
    }
    if (expressions.isEmpty()) {
        if (!options.allowNoIndices()) {
            IndexNotFoundException infe = new IndexNotFoundException((String) null);
            infe.setResources("index_expression", indexExpressions);
            throw infe;
        } else {
            return Index.EMPTY_ARRAY;
        }
    }
    final Set<Index> concreteIndices = new HashSet<>(expressions.size());
    for (String expression : expressions) {
        AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
        if (aliasOrIndex == null) {
            if (failNoIndices) {
                IndexNotFoundException infe = new IndexNotFoundException(expression);
                infe.setResources("index_expression", expression);
                throw infe;
            } else {
                continue;
            }
        }
        Collection<IndexMetaData> resolvedIndices = aliasOrIndex.getIndices();
        if (resolvedIndices.size() > 1 && !options.allowAliasesToMultipleIndices()) {
            String[] indexNames = new String[resolvedIndices.size()];
            int i = 0;
            for (IndexMetaData indexMetaData : resolvedIndices) {
                indexNames[i++] = indexMetaData.getIndex().getName();
            }
            throw new IllegalArgumentException("Alias [" + expression + "] has more than one indices associated with it [" + Arrays.toString(indexNames) + "], can't execute a single index op");
        }
        for (IndexMetaData index : resolvedIndices) {
            if (index.getState() == IndexMetaData.State.CLOSE) {
                if (failClosed) {
                    throw new IndexClosedException(index.getIndex());
                } else {
                    if (options.forbidClosedIndices() == false) {
                        concreteIndices.add(index.getIndex());
                    }
                }
            } else if (index.getState() == IndexMetaData.State.OPEN) {
                concreteIndices.add(index.getIndex());
            } else {
                throw new IllegalStateException("index state [" + index.getState() + "] not supported");
            }
        }
    }
    if (options.allowNoIndices() == false && concreteIndices.isEmpty()) {
        IndexNotFoundException infe = new IndexNotFoundException((String) null);
        infe.setResources("index_expression", indexExpressions);
        throw infe;
    }
    return concreteIndices.toArray(new Index[concreteIndices.size()]);
}
Also used : Index(org.elasticsearch.index.Index) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) HashSet(java.util.HashSet)

Aggregations

IndexClosedException (org.elasticsearch.indices.IndexClosedException)8 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)4 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)2 ClusterName (org.elasticsearch.cluster.ClusterName)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)2 Index (org.elasticsearch.index.Index)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)1 RoutingMissingException (org.elasticsearch.action.RoutingMissingException)1 CloseIndexResponse (org.elasticsearch.action.admin.indices.close.CloseIndexResponse)1 OpenIndexResponse (org.elasticsearch.action.admin.indices.open.OpenIndexResponse)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1