Search in sources :

Example 1 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class SimpleRoutingIT method testRequiredRoutingCrudApis.

public void testRequiredRoutingCrudApis() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    logger.info("--> indexing with id [1], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> verifying get with no routing, should fail");
    logger.info("--> indexing with id [1], with no routing, should fail");
    try {
        client().prepareIndex(indexOrAlias(), "type1", "1").setSource("field", "value1").get();
        fail("index with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    logger.info("--> verifying get with routing, should find");
    for (int i = 0; i < 5; i++) {
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    logger.info("--> deleting with no routing, should fail");
    try {
        client().prepareDelete(indexOrAlias(), "type1", "1").get();
        fail("delete with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail("get with missing routing when routing is required should fail");
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").execute().actionGet();
        fail("update with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").get();
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        GetResponse getResponse = client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet();
        assertThat(getResponse.isExists(), equalTo(true));
        assertThat(getResponse.getSourceAsMap().get("field"), equalTo("value2"));
    }
    client().prepareDelete(indexOrAlias(), "type1", "1").setRouting("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(false));
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Example 2 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class TransportExplainAction method resolveRequest.

@Override
protected void resolveRequest(ClusterState state, InternalRequest request) {
    final AliasFilter aliasFilter = searchService.buildAliasFilter(state, request.concreteIndex(), request.request().index());
    request.request().filteringAlias(aliasFilter);
    // Fail fast on the node that received the request.
    if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) {
        throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id());
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Example 3 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFromXContentWithHeadersAndMetadata.

public void testFromXContentWithHeadersAndMetadata() throws IOException {
    RoutingMissingException routing = new RoutingMissingException("_test", "_type", "_id");
    ElasticsearchException baz = new ElasticsearchException("baz", routing);
    baz.addHeader("baz_0", "baz0");
    baz.addMetadata("es.baz_1", "baz1");
    baz.addHeader("baz_2", "baz2");
    baz.addMetadata("es.baz_3", "baz3");
    ElasticsearchException bar = new ElasticsearchException("bar", baz);
    bar.addMetadata("es.bar_0", "bar0");
    bar.addHeader("bar_1", "bar1");
    bar.addMetadata("es.bar_2", "bar2");
    ElasticsearchException foo = new ElasticsearchException("foo", bar);
    foo.addMetadata("es.foo_0", "foo0");
    foo.addHeader("foo_1", "foo1");
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    XContentBuilder builder = XContentBuilder.builder(xContent).startObject().value(foo).endObject();
    ElasticsearchException parsed;
    try (XContentParser parser = createParser(builder)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsed = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertNotNull(parsed);
    assertEquals(parsed.getMessage(), "Elasticsearch exception [type=exception, reason=foo]");
    assertThat(parsed.getHeaderKeys(), hasSize(1));
    assertThat(parsed.getHeader("foo_1"), hasItem("foo1"));
    assertThat(parsed.getMetadataKeys(), hasSize(1));
    assertThat(parsed.getMetadata("es.foo_0"), hasItem("foo0"));
    ElasticsearchException cause = (ElasticsearchException) parsed.getCause();
    assertEquals(cause.getMessage(), "Elasticsearch exception [type=exception, reason=bar]");
    assertThat(cause.getHeaderKeys(), hasSize(1));
    assertThat(cause.getHeader("bar_1"), hasItem("bar1"));
    assertThat(cause.getMetadataKeys(), hasSize(2));
    assertThat(cause.getMetadata("es.bar_0"), hasItem("bar0"));
    assertThat(cause.getMetadata("es.bar_2"), hasItem("bar2"));
    cause = (ElasticsearchException) cause.getCause();
    assertEquals(cause.getMessage(), "Elasticsearch exception [type=exception, reason=baz]");
    assertThat(cause.getHeaderKeys(), hasSize(2));
    assertThat(cause.getHeader("baz_0"), hasItem("baz0"));
    assertThat(cause.getHeader("baz_2"), hasItem("baz2"));
    assertThat(cause.getMetadataKeys(), hasSize(2));
    assertThat(cause.getMetadata("es.baz_1"), hasItem("baz1"));
    assertThat(cause.getMetadata("es.baz_3"), hasItem("baz3"));
    cause = (ElasticsearchException) cause.getCause();
    assertEquals(cause.getMessage(), "Elasticsearch exception [type=routing_missing_exception, reason=routing is required for [_test]/[_type]/[_id]]");
    assertThat(cause.getHeaderKeys(), hasSize(0));
    assertThat(cause.getMetadataKeys(), hasSize(2));
    assertThat(cause.getMetadata("es.index"), hasItem("_test"));
    assertThat(cause.getMetadata("es.index_uuid"), hasItem("_na_"));
}
Also used : XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class ElasticsearchExceptionTests method testFailureToAndFromXContentWithDetails.

public void testFailureToAndFromXContentWithDetails() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    Exception failure;
    Throwable failureCause;
    ElasticsearchException expected;
    ElasticsearchException expectedCause;
    ElasticsearchException suppressed;
    switch(randomIntBetween(0, 6)) {
        case // Simple elasticsearch exception without cause
        0:
            failure = new NoNodeAvailableException("A");
            expected = new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=no_node_available_exception, reason=A]"));
            break;
        case // Simple elasticsearch exception with headers (other metadata of type number are not parsed)
        1:
            failure = new CircuitBreakingException("B", 5_000, 2_000);
            ((ElasticsearchException) failure).addHeader("header_name", "0", "1");
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            expected.addHeader("header_name", "0", "1");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=B]");
            suppressed.addHeader("header_name", "0", "1");
            expected.addSuppressed(suppressed);
            break;
        case // Elasticsearch exception with a cause, headers and parsable metadata
        2:
            failureCause = new NullPointerException("var is null");
            failure = new ScriptException("C", failureCause, singletonList("stack"), "test", "painless");
            ((ElasticsearchException) failure).addHeader("script_name", "my_script");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, reason=var is null]");
            expected = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]", expectedCause);
            expected.addHeader("script_name", "my_script");
            expected.addMetadata("es.lang", "painless");
            expected.addMetadata("es.script", "test");
            expected.addMetadata("es.script_stack", "stack");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=script_exception, reason=C]");
            suppressed.addHeader("script_name", "my_script");
            suppressed.addMetadata("es.lang", "painless");
            suppressed.addMetadata("es.script", "test");
            suppressed.addMetadata("es.script_stack", "stack");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception without cause
        3:
            failure = new IllegalStateException("D");
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            suppressed = new ElasticsearchException("Elasticsearch exception [type=illegal_state_exception, reason=D]");
            expected.addSuppressed(suppressed);
            break;
        case // JDK exception with cause
        4:
            failureCause = new RoutingMissingException("idx", "type", "id");
            failure = new RuntimeException("E", failureCause);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=routing_missing_exception, " + "reason=routing is required for [idx]/[type]/[id]]");
            expectedCause.addMetadata("es.index", "idx");
            expectedCause.addMetadata("es.index_uuid", "_na_");
            expected = new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=E]", expectedCause);
            suppressed = new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=E]");
            expected.addSuppressed(suppressed);
            break;
        case // Wrapped exception with cause
        5:
            failureCause = new FileAlreadyExistsException("File exists");
            failure = new BroadcastShardOperationFailedException(new ShardId("_index", "_uuid", 5), "F", failureCause);
            expected = new ElasticsearchException("Elasticsearch exception [type=file_already_exists_exception, reason=File exists]");
            // strangely, the wrapped exception appears as the root cause...
            suppressed = new ElasticsearchException("Elasticsearch exception [type=broadcast_shard_operation_failed_exception, " + "reason=F]");
            expected.addSuppressed(suppressed);
            break;
        case // SearchPhaseExecutionException with cause and multiple failures
        6:
            DiscoveryNode node = new DiscoveryNode("node_g", buildNewFakeTransportAddress(), Version.CURRENT);
            failureCause = new NodeClosedException(node);
            failureCause = new NoShardAvailableActionException(new ShardId("_index_g", "_uuid_g", 6), "node_g", failureCause);
            ShardSearchFailure[] shardFailures = new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(0, 0, "Parsing g", null), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 61))), new ShardSearchFailure(new RepositoryException("repository_g", "Repo"), new SearchShardTarget("node_g", new ShardId(new Index("_index_g", "_uuid_g"), 62))), new ShardSearchFailure(new SearchContextMissingException(0L), null) };
            failure = new SearchPhaseExecutionException("phase_g", "G", failureCause, shardFailures);
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=node_closed_exception, " + "reason=node closed " + node + "]");
            expectedCause = new ElasticsearchException("Elasticsearch exception [type=no_shard_available_action_exception, " + "reason=node_g]", expectedCause);
            expectedCause.addMetadata("es.index", "_index_g");
            expectedCause.addMetadata("es.index_uuid", "_uuid_g");
            expectedCause.addMetadata("es.shard", "6");
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=G]", expectedCause);
            expected.addMetadata("es.phase", "phase_g");
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=parsing_exception, reason=Parsing g]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=repository_exception, " + "reason=[repository_g] Repo]"));
            expected.addSuppressed(new ElasticsearchException("Elasticsearch exception [type=search_context_missing_exception, " + "reason=No search context found for id [0]]"));
            break;
        default:
            throw new UnsupportedOperationException("Failed to generate randomized failure");
    }
    Exception finalFailure = failure;
    BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> {
        ElasticsearchException.generateFailureXContent(builder, params, finalFailure, true);
        return builder;
    }, xContent.type(), randomBoolean());
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        failureBytes = shuffleXContent(parser, randomBoolean()).bytes();
    }
    ElasticsearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = ElasticsearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertDeepEquals(expected, parsedFailure);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) ScriptException(org.elasticsearch.script.ScriptException) XContent(org.elasticsearch.common.xcontent.XContent) ToXContent(org.elasticsearch.common.xcontent.ToXContent) ParsingException(org.elasticsearch.common.ParsingException) NodeClosedException(org.elasticsearch.node.NodeClosedException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BytesReference(org.elasticsearch.common.bytes.BytesReference) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) RepositoryException(org.elasticsearch.repositories.RepositoryException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) SearchParseException(org.elasticsearch.search.SearchParseException) NodeClosedException(org.elasticsearch.node.NodeClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) BroadcastShardOperationFailedException(org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException) RepositoryException(org.elasticsearch.repositories.RepositoryException) QueryShardException(org.elasticsearch.index.query.QueryShardException) SearchContextMissingException(org.elasticsearch.search.SearchContextMissingException) ScriptException(org.elasticsearch.script.ScriptException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) ParsingException(org.elasticsearch.common.ParsingException) IndexShardRecoveringException(org.elasticsearch.index.shard.IndexShardRecoveringException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) NoShardAvailableActionException(org.elasticsearch.action.NoShardAvailableActionException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 5 with RoutingMissingException

use of org.elasticsearch.action.RoutingMissingException in project elasticsearch by elastic.

the class TransportGetAction method resolveRequest.

@Override
protected void resolveRequest(ClusterState state, InternalRequest request) {
    IndexMetaData indexMeta = state.getMetaData().index(request.concreteIndex());
    if (// if the realtime flag is set
    request.request().realtime && // the preference flag is not already set
    request.request().preference() == null && // and we have the index
    indexMeta != null && indexMeta.isIndexUsingShadowReplicas()) {
        // and the index uses shadow replicas
        // set the preference for the request to use "_primary" automatically
        request.request().preference(Preference.PRIMARY.type());
    }
    // update the routing (request#index here is possibly an alias)
    request.request().routing(state.metaData().resolveIndexRouting(request.request().parent(), request.request().routing(), request.request().index()));
    // Fail fast on the node that received the request.
    if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) {
        throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id());
    }
}
Also used : RoutingMissingException(org.elasticsearch.action.RoutingMissingException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

RoutingMissingException (org.elasticsearch.action.RoutingMissingException)8 ToXContent (org.elasticsearch.common.xcontent.ToXContent)3 XContent (org.elasticsearch.common.xcontent.XContent)3 XContentParser (org.elasticsearch.common.xcontent.XContentParser)3 Alias (org.elasticsearch.action.admin.indices.alias.Alias)2 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 NoShardAvailableActionException (org.elasticsearch.action.NoShardAvailableActionException)1 ExplainResponse (org.elasticsearch.action.explain.ExplainResponse)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)1 MultiTermVectorsResponse (org.elasticsearch.action.termvectors.MultiTermVectorsResponse)1