|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.cluster.routing.allocation.decider; |
| 21 | + |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.cluster.ClusterName; |
| 24 | +import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 26 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 27 | +import org.elasticsearch.cluster.routing.RecoverySource; |
| 28 | +import org.elasticsearch.cluster.routing.RoutingNode; |
| 29 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 30 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
| 31 | +import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; |
| 32 | +import org.elasticsearch.index.shard.ShardId; |
| 33 | +import org.elasticsearch.test.ESTestCase; |
| 34 | +import org.hamcrest.Matcher; |
| 35 | +import org.hamcrest.Matchers; |
| 36 | + |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +public class AllocationDecidersTests extends ESTestCase { |
| 41 | + |
| 42 | + public void testDebugMode() { |
| 43 | + verifyDebugMode(RoutingAllocation.DebugMode.ON, Matchers.hasSize(1)); |
| 44 | + } |
| 45 | + |
| 46 | + public void testNoDebugMode() { |
| 47 | + verifyDebugMode(RoutingAllocation.DebugMode.OFF, Matchers.empty()); |
| 48 | + } |
| 49 | + |
| 50 | + public void testDebugExcludeYesMode() { |
| 51 | + verifyDebugMode(RoutingAllocation.DebugMode.EXCLUDE_YES_DECISIONS, Matchers.empty()); |
| 52 | + } |
| 53 | + |
| 54 | + private void verifyDebugMode(RoutingAllocation.DebugMode mode, Matcher<Collection<? extends Decision>> matcher) { |
| 55 | + AllocationDeciders deciders = new AllocationDeciders(List.of(new AllocationDecider() { |
| 56 | + @Override |
| 57 | + public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { |
| 58 | + return Decision.YES; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { |
| 63 | + return Decision.YES; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { |
| 68 | + return Decision.YES; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocation) { |
| 73 | + return Decision.YES; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Decision canAllocate(IndexMetaData indexMetaData, RoutingNode node, RoutingAllocation allocation) { |
| 78 | + return Decision.YES; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Decision canAllocate(RoutingNode node, RoutingAllocation allocation) { |
| 83 | + return Decision.YES; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Decision shouldAutoExpandToNode(IndexMetaData indexMetaData, DiscoveryNode node, RoutingAllocation allocation) { |
| 88 | + return Decision.YES; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Decision canRebalance(RoutingAllocation allocation) { |
| 93 | + return Decision.YES; |
| 94 | + } |
| 95 | + })); |
| 96 | + |
| 97 | + ClusterState clusterState = ClusterState.builder(new ClusterName("test")).build(); |
| 98 | + final RoutingAllocation allocation = new RoutingAllocation(deciders, |
| 99 | + clusterState.getRoutingNodes(), clusterState, null, 0L); |
| 100 | + |
| 101 | + allocation.setDebugMode(mode); |
| 102 | + final UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "_message"); |
| 103 | + final ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId("test", "testUUID", 0), true, |
| 104 | + RecoverySource.ExistingStoreRecoverySource.INSTANCE, unassignedInfo); |
| 105 | + IndexMetaData idx = |
| 106 | + IndexMetaData.builder("idx").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build(); |
| 107 | + |
| 108 | + RoutingNode routingNode = new RoutingNode("testNode", null); |
| 109 | + verify(deciders.canAllocate(shardRouting, routingNode, allocation), matcher); |
| 110 | + verify(deciders.canAllocate(idx, routingNode, allocation), matcher); |
| 111 | + verify(deciders.canAllocate(shardRouting, allocation), matcher); |
| 112 | + verify(deciders.canAllocate(routingNode, allocation), matcher); |
| 113 | + verify(deciders.canRebalance(shardRouting, allocation), matcher); |
| 114 | + verify(deciders.canRebalance(allocation), matcher); |
| 115 | + verify(deciders.canRemain(shardRouting, routingNode, allocation), matcher); |
| 116 | + verify(deciders.canForceAllocatePrimary(shardRouting, routingNode, allocation), matcher); |
| 117 | + verify(deciders.shouldAutoExpandToNode(idx, null, allocation), matcher); |
| 118 | + } |
| 119 | + |
| 120 | + private void verify(Decision decision, Matcher<Collection<? extends Decision>> matcher) { |
| 121 | + assertThat(decision.type(), Matchers.equalTo(Decision.Type.YES)); |
| 122 | + assertThat(decision, Matchers.instanceOf(Decision.Multi.class)); |
| 123 | + Decision.Multi multi = (Decision.Multi) decision; |
| 124 | + assertThat(multi.getDecisions(), matcher); |
| 125 | + } |
| 126 | +} |
0 commit comments