Search in sources :

Example 1 with IndexClosedException

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

the class SyncedFlushService method getShardRoutingTable.

final IndexShardRoutingTable getShardRoutingTable(ShardId shardId, ClusterState state) {
    final IndexRoutingTable indexRoutingTable = state.routingTable().index(shardId.getIndexName());
    if (indexRoutingTable == null) {
        IndexMetaData index = state.getMetaData().index(shardId.getIndex());
        if (index != null && index.getState() == IndexMetaData.State.CLOSE) {
            throw new IndexClosedException(shardId.getIndex());
        }
        throw new IndexNotFoundException(shardId.getIndexName());
    }
    final IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId.id());
    if (shardRoutingTable == null) {
        throw new ShardNotFoundException(shardId);
    }
    return shardRoutingTable;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 2 with IndexClosedException

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

the class SimpleIndexStateIT method testSimpleOpenClose.

public void testSimpleOpenClose() {
    logger.info("--> creating test index");
    createIndex("test");
    logger.info("--> waiting for green status");
    ensureGreen();
    NumShards numShards = getNumShards("test");
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(numShards.numPrimaries));
    assertEquals(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), numShards.totalNumShards);
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
    logger.info("--> closing test index...");
    CloseIndexResponse closeIndexResponse = client().admin().indices().prepareClose("test").get();
    assertThat(closeIndexResponse.isAcknowledged(), equalTo(true));
    stateResponse = client().admin().cluster().prepareState().get();
    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").get();
        fail();
    } catch (IndexClosedException e) {
    // all is well
    }
    logger.info("--> opening index...");
    OpenIndexResponse openIndexResponse = client().admin().indices().prepareOpen("test").get();
    assertThat(openIndexResponse.isAcknowledged(), equalTo(true));
    logger.info("--> waiting for green status");
    ensureGreen();
    stateResponse = client().admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(numShards.numPrimaries));
    assertEquals(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), numShards.totalNumShards);
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
}
Also used : CloseIndexResponse(org.elasticsearch.action.admin.indices.close.CloseIndexResponse) IndexClosedException(org.elasticsearch.indices.IndexClosedException) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) OpenIndexResponse(org.elasticsearch.action.admin.indices.open.OpenIndexResponse)

Example 3 with IndexClosedException

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

the class IndexNameExpressionResolverTests method testIndexOptionsFailClosedIndicesAndAliases.

public void testIndexOptionsFailClosedIndicesAndAliases() {
    MetaData.Builder mdBuilder = MetaData.builder().put(indexBuilder("foo1-closed").state(IndexMetaData.State.CLOSE).putAlias(AliasMetaData.builder("foobar1-closed")).putAlias(AliasMetaData.builder("foobar2-closed"))).put(indexBuilder("foo2-closed").state(IndexMetaData.State.CLOSE).putAlias(AliasMetaData.builder("foobar2-closed"))).put(indexBuilder("foo3").putAlias(AliasMetaData.builder("foobar2-closed")));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metaData(mdBuilder).build();
    IndexNameExpressionResolver.Context context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictExpandOpenAndForbidClosed());
    try {
        indexNameExpressionResolver.concreteIndexNames(context, "foo1-closed");
        fail("foo1-closed should be closed, but it is open");
    } catch (IndexClosedException e) {
    // expected
    }
    try {
        indexNameExpressionResolver.concreteIndexNames(context, "foobar1-closed");
        fail("foo1-closed should be closed, but it is open");
    } catch (IndexClosedException e) {
    // expected
    }
    context = new IndexNameExpressionResolver.Context(state, IndicesOptions.fromOptions(true, context.getOptions().allowNoIndices(), context.getOptions().expandWildcardsOpen(), context.getOptions().expandWildcardsClosed(), context.getOptions()));
    String[] results = indexNameExpressionResolver.concreteIndexNames(context, "foo1-closed");
    assertThat(results, emptyArray());
    results = indexNameExpressionResolver.concreteIndexNames(context, "foobar1-closed");
    assertThat(results, emptyArray());
    context = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen());
    results = indexNameExpressionResolver.concreteIndexNames(context, "foo1-closed");
    assertThat(results, arrayWithSize(1));
    assertThat(results, arrayContaining("foo1-closed"));
    results = indexNameExpressionResolver.concreteIndexNames(context, "foobar1-closed");
    assertThat(results, arrayWithSize(1));
    assertThat(results, arrayContaining("foo1-closed"));
    // testing an alias pointing to three indices:
    context = new IndexNameExpressionResolver.Context(state, IndicesOptions.strictExpandOpenAndForbidClosed());
    try {
        indexNameExpressionResolver.concreteIndexNames(context, "foobar2-closed");
        fail("foo2-closed should be closed, but it is open");
    } catch (IndexClosedException e) {
    // expected
    }
    context = new IndexNameExpressionResolver.Context(state, IndicesOptions.fromOptions(true, context.getOptions().allowNoIndices(), context.getOptions().expandWildcardsOpen(), context.getOptions().expandWildcardsClosed(), context.getOptions()));
    results = indexNameExpressionResolver.concreteIndexNames(context, "foobar2-closed");
    assertThat(results, arrayWithSize(1));
    assertThat(results, arrayContaining("foo3"));
    context = new IndexNameExpressionResolver.Context(state, IndicesOptions.lenientExpandOpen());
    results = indexNameExpressionResolver.concreteIndexNames(context, "foobar2-closed");
    assertThat(results, arrayWithSize(3));
    assertThat(results, arrayContainingInAnyOrder("foo1-closed", "foo2-closed", "foo3"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexClosedException(org.elasticsearch.indices.IndexClosedException) ClusterName(org.elasticsearch.cluster.ClusterName) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with IndexClosedException

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

the class TransportBulkAction method addFailureIfIndexIsUnavailable.

private boolean addFailureIfIndexIsUnavailable(DocWriteRequest request, BulkRequest bulkRequest, AtomicArray<BulkItemResponse> responses, int idx, final ConcreteIndices concreteIndices, final MetaData metaData) {
    Index concreteIndex = concreteIndices.getConcreteIndex(request.index());
    Exception unavailableException = null;
    if (concreteIndex == null) {
        try {
            concreteIndex = concreteIndices.resolveIfAbsent(request);
        } catch (IndexClosedException | IndexNotFoundException ex) {
            // Fix for issue where bulk request references an index that
            // cannot be auto-created see issue #8125
            unavailableException = ex;
        }
    }
    if (unavailableException == null) {
        IndexMetaData indexMetaData = metaData.getIndexSafe(concreteIndex);
        if (indexMetaData.getState() == IndexMetaData.State.CLOSE) {
            unavailableException = new IndexClosedException(concreteIndex);
        }
    }
    if (unavailableException != null) {
        BulkItemResponse.Failure failure = new BulkItemResponse.Failure(request.index(), request.type(), request.id(), unavailableException);
        BulkItemResponse bulkItemResponse = new BulkItemResponse(idx, request.opType(), failure);
        responses.set(idx, bulkItemResponse);
        // make sure the request gets never processed again
        bulkRequest.requests.set(idx, null);
        return true;
    }
    return false;
}
Also used : IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) AutoCreateIndex(org.elasticsearch.action.support.AutoCreateIndex) Index(org.elasticsearch.index.Index) IndexClosedException(org.elasticsearch.indices.IndexClosedException) NodeClosedException(org.elasticsearch.node.NodeClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 5 with IndexClosedException

use of org.elasticsearch.indices.IndexClosedException in project graylog2-server by Graylog2.

the class Indices method indexRangeStatsOfIndex.

/**
     * Calculate min and max message timestamps in the given index.
     *
     * @param index Name of the index to query.
     * @return the timestamp stats in the given index, or {@code null} if they couldn't be calculated.
     * @see org.elasticsearch.search.aggregations.metrics.stats.Stats
     */
public IndexRangeStats indexRangeStatsOfIndex(String index) {
    final FilterAggregationBuilder builder = AggregationBuilders.filter("agg").filter(QueryBuilders.existsQuery("timestamp")).subAggregation(AggregationBuilders.min("ts_min").field("timestamp")).subAggregation(AggregationBuilders.max("ts_max").field("timestamp")).subAggregation(AggregationBuilders.terms("streams").field("streams"));
    final SearchRequestBuilder srb = c.prepareSearch().setIndices(index).setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0).addAggregation(builder);
    final SearchResponse response;
    try {
        final SearchRequest request = srb.request();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Index range query: _search/{}: {}", index, XContentHelper.convertToJson(request.source(), false));
        }
        response = c.search(request).actionGet();
    } catch (IndexClosedException e) {
        throw e;
    } catch (org.elasticsearch.index.IndexNotFoundException e) {
        LOG.error("Error while calculating timestamp stats in index <" + index + ">", e);
        throw e;
    } catch (ElasticsearchException e) {
        LOG.error("Error while calculating timestamp stats in index <" + index + ">", e);
        throw new org.elasticsearch.index.IndexNotFoundException("Index " + index + " not found", e);
    } catch (IOException e) {
        // the index range aggregation query on DEBUG (via XContentHelper)
        throw new RuntimeException(e);
    }
    final Filter f = response.getAggregations().get("agg");
    if (f.getDocCount() == 0L) {
        LOG.debug("No documents with attribute \"timestamp\" found in index <{}>", index);
        return IndexRangeStats.EMPTY;
    }
    final Min minAgg = f.getAggregations().get("ts_min");
    final DateTime min = new DateTime((long) minAgg.getValue(), DateTimeZone.UTC);
    final Max maxAgg = f.getAggregations().get("ts_max");
    final DateTime max = new DateTime((long) maxAgg.getValue(), DateTimeZone.UTC);
    // make sure we return an empty list, so we can differentiate between old indices that don't have this information
    // and newer ones that simply have no streams.
    ImmutableList.Builder<String> streamIds = ImmutableList.builder();
    final Terms streams = f.getAggregations().get("streams");
    if (!streams.getBuckets().isEmpty()) {
        streamIds.addAll(streams.getBuckets().stream().map(Terms.Bucket::getKeyAsString).collect(toSet()));
    }
    return IndexRangeStats.create(min, max, streamIds.build());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Max(org.elasticsearch.search.aggregations.metrics.max.Max) ImmutableList(com.google.common.collect.ImmutableList) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) Min(org.elasticsearch.search.aggregations.metrics.min.Min) IndexClosedException(org.elasticsearch.indices.IndexClosedException) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter)

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