Search in sources :

Example 31 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class ClusterAllocationExplainIT method testAllocationFilteringPreventsShardMove.

public void testAllocationFilteringPreventsShardMove() throws Exception {
    logger.info("--> starting 2 nodes");
    internalCluster().startNodes(2);
    logger.info("--> creating an index with 1 primary and 0 replicas");
    createIndexAndIndexData(1, 0);
    logger.info("--> setting up allocation filtering to prevent allocation to both nodes");
    client().admin().indices().prepareUpdateSettings("idx").setSettings(Settings.builder().put("index.routing.allocation.include._name", "non_existent_node")).get();
    boolean includeYesDecisions = randomBoolean();
    boolean includeDiskInfo = randomBoolean();
    ClusterAllocationExplanation explanation = runExplain(true, includeYesDecisions, includeDiskInfo);
    ShardId shardId = explanation.getShard();
    boolean isPrimary = explanation.isPrimary();
    ShardRoutingState shardRoutingState = explanation.getShardState();
    DiscoveryNode currentNode = explanation.getCurrentNode();
    UnassignedInfo unassignedInfo = explanation.getUnassignedInfo();
    ClusterInfo clusterInfo = explanation.getClusterInfo();
    AllocateUnassignedDecision allocateDecision = explanation.getShardAllocationDecision().getAllocateDecision();
    MoveDecision moveDecision = explanation.getShardAllocationDecision().getMoveDecision();
    // verify shard info
    assertEquals("idx", shardId.getIndexName());
    assertEquals(0, shardId.getId());
    assertTrue(isPrimary);
    // verify current node info
    assertEquals(ShardRoutingState.STARTED, shardRoutingState);
    assertNotNull(currentNode);
    // verify unassigned info
    assertNull(unassignedInfo);
    // verify cluster info
    verifyClusterInfo(clusterInfo, includeDiskInfo, 2);
    // verify decision object
    assertFalse(allocateDecision.isDecisionTaken());
    assertTrue(moveDecision.isDecisionTaken());
    assertEquals(AllocationDecision.NO, moveDecision.getAllocationDecision());
    assertEquals("cannot move shard to another node, even though it is not allowed to remain on its current node", moveDecision.getExplanation());
    assertFalse(moveDecision.canRemain());
    assertFalse(moveDecision.forceMove());
    assertFalse(moveDecision.canRebalanceCluster());
    assertNull(moveDecision.getClusterRebalanceDecision());
    assertNull(moveDecision.getTargetNode());
    assertEquals(0, moveDecision.getCurrentNodeRanking());
    // verifying can remain decision object
    assertNotNull(moveDecision.getCanRemainDecision());
    assertEquals(Decision.Type.NO, moveDecision.getCanRemainDecision().type());
    for (Decision d : moveDecision.getCanRemainDecision().getDecisions()) {
        if (d.label().equals("filter")) {
            assertEquals(Decision.Type.NO, d.type());
            assertEquals("node does not match index setting [index.routing.allocation.include] filters [_name:\"non_existent_node\"]", d.getExplanation());
        } else {
            assertEquals(Decision.Type.YES, d.type());
            assertNotNull(d.getExplanation());
        }
    }
    // verify node decisions
    assertEquals(1, moveDecision.getNodeDecisions().size());
    NodeAllocationResult result = moveDecision.getNodeDecisions().get(0);
    assertNotNull(result.getNode());
    assertEquals(1, result.getWeightRanking());
    assertEquals(AllocationDecision.NO, result.getNodeDecision());
    if (includeYesDecisions) {
        assertThat(result.getCanAllocateDecision().getDecisions().size(), greaterThan(1));
    } else {
        assertEquals(1, result.getCanAllocateDecision().getDecisions().size());
    }
    for (Decision d : result.getCanAllocateDecision().getDecisions()) {
        if (d.label().equals("filter")) {
            assertEquals(Decision.Type.NO, d.type());
            assertEquals("node does not match index setting [index.routing.allocation.include] filters [_name:\"non_existent_node\"]", d.getExplanation());
        } else {
            assertEquals(Decision.Type.YES, d.type());
            assertNotNull(d.getExplanation());
        }
    }
    // verify JSON output
    try (XContentParser parser = getParser(explanation)) {
        verifyShardInfo(parser, true, includeDiskInfo, ShardRoutingState.STARTED);
        parser.nextToken();
        assertEquals("can_remain_on_current_node", parser.currentName());
        parser.nextToken();
        assertEquals(AllocationDecision.NO.toString(), parser.text());
        parser.nextToken();
        assertEquals("can_remain_decisions", parser.currentName());
        verifyDeciders(parser, AllocationDecision.NO);
        parser.nextToken();
        assertEquals("can_move_to_other_node", parser.currentName());
        parser.nextToken();
        assertEquals(AllocationDecision.NO.toString(), parser.text());
        parser.nextToken();
        assertEquals("move_explanation", parser.currentName());
        parser.nextToken();
        assertEquals("cannot move shard to another node, even though it is not allowed to remain on its current node", parser.text());
        verifyNodeDecisions(parser, allNodeDecisions(AllocationDecision.NO, true), includeYesDecisions, false);
        assertEquals(Token.END_OBJECT, parser.nextToken());
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) MoveDecision(org.elasticsearch.cluster.routing.allocation.MoveDecision) AllocateUnassignedDecision(org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) MoveDecision(org.elasticsearch.cluster.routing.allocation.MoveDecision) AllocationDecision(org.elasticsearch.cluster.routing.allocation.AllocationDecision) AllocateUnassignedDecision(org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) NodeAllocationResult(org.elasticsearch.cluster.routing.allocation.NodeAllocationResult) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 32 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class AliasActionsTests method testParseRemoveIndex.

public void testParseRemoveIndex() throws IOException {
    String[] indices = randomBoolean() ? new String[] { randomAsciiOfLength(5) } : generateRandomStringArray(10, 5, false, false);
    XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
    b.startObject();
    {
        b.startObject("remove_index");
        {
            if (indices.length > 1 || randomBoolean()) {
                b.array("indices", indices);
            } else {
                b.field("index", indices[0]);
            }
        }
        b.endObject();
    }
    b.endObject();
    b = shuffleXContent(b);
    try (XContentParser parser = createParser(b)) {
        AliasActions action = AliasActions.PARSER.apply(parser, null);
        assertEquals(AliasActions.Type.REMOVE_INDEX, action.actionType());
        assertArrayEquals(indices, action.indices());
        assertThat(action.aliases(), arrayWithSize(0));
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)

Example 33 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class AliasActionsTests method testParseAddDefaultRouting.

public void testParseAddDefaultRouting() throws IOException {
    String index = randomAsciiOfLength(5);
    String alias = randomAsciiOfLength(5);
    Object searchRouting = randomRouting();
    Object indexRouting = randomRouting();
    XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
    b.startObject();
    {
        b.startObject("add");
        {
            b.field("index", index);
            b.field("alias", alias);
            if (randomBoolean()) {
                b.field("routing", searchRouting);
                b.field("index_routing", indexRouting);
            } else {
                b.field("search_routing", searchRouting);
                b.field("routing", indexRouting);
            }
        }
        b.endObject();
    }
    b.endObject();
    b = shuffleXContent(b);
    try (XContentParser parser = createParser(b)) {
        AliasActions action = AliasActions.PARSER.apply(parser, null);
        assertEquals(AliasActions.Type.ADD, action.actionType());
        assertThat(action.indices(), arrayContaining(index));
        assertThat(action.aliases(), arrayContaining(alias));
        assertEquals(searchRouting.toString(), action.searchRouting());
        assertEquals(indexRouting.toString(), action.indexRouting());
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)

Example 34 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class AliasActionsTests method testParseAdd.

public void testParseAdd() throws IOException {
    String[] indices = generateRandomStringArray(10, 5, false, false);
    String[] aliases = generateRandomStringArray(10, 5, false, false);
    Map<String, Object> filter = randomBoolean() ? randomMap(5) : null;
    Object searchRouting = randomBoolean() ? randomRouting() : null;
    Object indexRouting = randomBoolean() ? randomBoolean() ? searchRouting : randomRouting() : null;
    XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
    b.startObject();
    {
        b.startObject("add");
        {
            if (indices.length > 1 || randomBoolean()) {
                b.array("indices", indices);
            } else {
                b.field("index", indices[0]);
            }
            if (aliases.length > 1 || randomBoolean()) {
                b.array("aliases", aliases);
            } else {
                b.field("alias", aliases[0]);
            }
            if (filter != null) {
                b.field("filter", filter);
            }
            if (searchRouting != null) {
                if (searchRouting.equals(indexRouting)) {
                    b.field("routing", searchRouting);
                } else {
                    b.field("search_routing", searchRouting);
                }
            }
            if (indexRouting != null && false == indexRouting.equals(searchRouting)) {
                b.field("index_routing", indexRouting);
            }
        }
        b.endObject();
    }
    b.endObject();
    b = shuffleXContent(b, "filter");
    try (XContentParser parser = createParser(b)) {
        AliasActions action = AliasActions.PARSER.apply(parser, null);
        assertEquals(AliasActions.Type.ADD, action.actionType());
        assertThat(action.indices(), equalTo(indices));
        assertThat(action.aliases(), equalTo(aliases));
        if (filter == null || filter.isEmpty()) {
            assertNull(action.filter());
        } else {
            assertEquals(XContentFactory.contentBuilder(XContentType.JSON).map(filter).string(), action.filter());
        }
        assertEquals(Objects.toString(searchRouting, null), action.searchRouting());
        assertEquals(Objects.toString(indexRouting, null), action.indexRouting());
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) AliasActions(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions)

Example 35 with XContentParser

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentParser in project elasticsearch by elastic.

the class AliasActionsTests method testParseIndexAndIndicesThrowsError.

public void testParseIndexAndIndicesThrowsError() throws IOException {
    XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
    b.startObject();
    {
        b.startObject(randomFrom("add", "remove"));
        {
            b.field("index", randomAsciiOfLength(5));
            b.array("indices", generateRandomStringArray(10, 5, false, false));
            b.field("alias", randomAsciiOfLength(5));
        }
        b.endObject();
    }
    b.endObject();
    try (XContentParser parser = createParser(b)) {
        Exception e = expectThrows(ParsingException.class, () -> AliasActions.PARSER.apply(parser, null));
        assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
        assertEquals("Only one of [index] and [indices] is supported", e.getCause().getCause().getMessage());
    }
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException)

Aggregations

XContentParser (org.elasticsearch.common.xcontent.XContentParser)463 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)147 ParsingException (org.elasticsearch.common.ParsingException)110 IOException (java.io.IOException)77 BytesReference (org.elasticsearch.common.bytes.BytesReference)63 ArrayList (java.util.ArrayList)59 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)56 XContentType (org.elasticsearch.common.xcontent.XContentType)47 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)44 HashMap (java.util.HashMap)33 Map (java.util.Map)27 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Settings (org.elasticsearch.common.settings.Settings)18 ToXContent (org.elasticsearch.common.xcontent.ToXContent)16 ShardId (org.elasticsearch.index.shard.ShardId)16 Script (org.elasticsearch.script.Script)16 XContent (org.elasticsearch.common.xcontent.XContent)15 ElasticsearchException (org.elasticsearch.ElasticsearchException)14 NodeClient (org.elasticsearch.client.node.NodeClient)14