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