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;
}
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();
}
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"));
}
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;
}
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());
}
Aggregations