@@ -117,4 +117,130 @@ private void verify(Decision decision, Matcher<Collection<? extends Decision>> m
117117 Decision .Multi multi = (Decision .Multi ) decision ;
118118 assertThat (multi .getDecisions (), matcher );
119119 }
120+
121+ public void testEarlyTermination () {
122+ final Decision decisionOne = randomFrom (Decision .NO , Decision .single (Decision .Type .NO , "label1" , "explanation" ));
123+ final Decision decisionTwo = randomFrom (Decision .NO , Decision .single (Decision .Type .NO , "label2" , "explanation" ));
124+ final AllocationDeciders allocationDeciders = new AllocationDeciders (List .of (
125+ new AllocationDecider () {
126+ @ Override
127+ public Decision canAllocate (ShardRouting shardRouting , RoutingNode node , RoutingAllocation allocation ) {
128+ return decisionOne ;
129+ }
130+
131+ @ Override
132+ public Decision canRebalance (ShardRouting shardRouting , RoutingAllocation allocation ) {
133+ return decisionOne ;
134+ }
135+
136+ @ Override
137+ public Decision canRemain (ShardRouting shardRouting , RoutingNode node , RoutingAllocation allocation ) {
138+ return decisionOne ;
139+ }
140+
141+ @ Override
142+ public Decision canAllocate (ShardRouting shardRouting , RoutingAllocation allocation ) {
143+ return decisionOne ;
144+ }
145+
146+ @ Override
147+ public Decision canAllocate (IndexMetadata indexMetadata , RoutingNode node , RoutingAllocation allocation ) {
148+ return decisionOne ;
149+ }
150+
151+ @ Override
152+ public Decision shouldAutoExpandToNode (IndexMetadata indexMetadata , DiscoveryNode node , RoutingAllocation allocation ) {
153+ return decisionOne ;
154+ }
155+
156+ @ Override
157+ public Decision canRebalance (RoutingAllocation allocation ) {
158+ return decisionOne ;
159+ }
160+
161+ @ Override
162+ public Decision canForceAllocatePrimary (ShardRouting shardRouting , RoutingNode node , RoutingAllocation allocation ) {
163+ return decisionOne ;
164+ }
165+ }, new AllocationDecider () {
166+
167+ @ Override
168+ public Decision canAllocate (ShardRouting shardRouting , RoutingNode node , RoutingAllocation allocation ) {
169+ return decision (allocation );
170+ }
171+
172+ @ Override
173+ public Decision canRebalance (ShardRouting shardRouting , RoutingAllocation allocation ) {
174+ return decision (allocation );
175+ }
176+
177+ @ Override
178+ public Decision canRemain (ShardRouting shardRouting , RoutingNode node , RoutingAllocation allocation ) {
179+ return decision (allocation );
180+ }
181+
182+ @ Override
183+ public Decision canAllocate (ShardRouting shardRouting , RoutingAllocation allocation ) {
184+ return decision (allocation );
185+ }
186+
187+ @ Override
188+ public Decision canAllocate (IndexMetadata indexMetadata , RoutingNode node , RoutingAllocation allocation ) {
189+ return decision (allocation );
190+ }
191+
192+ @ Override
193+ public Decision shouldAutoExpandToNode (IndexMetadata indexMetadata , DiscoveryNode node , RoutingAllocation allocation ) {
194+ return decision (allocation );
195+ }
196+
197+ @ Override
198+ public Decision canRebalance (RoutingAllocation allocation ) {
199+ return decision (allocation );
200+ }
201+
202+ @ Override
203+ public Decision canForceAllocatePrimary (ShardRouting shardRouting , RoutingNode node , RoutingAllocation allocation ) {
204+ return decision (allocation );
205+ }
206+
207+ private Decision decision (RoutingAllocation allocation ) {
208+ if (allocation .debugDecision () == false ) {
209+ throw new AssertionError ("Should not be called" );
210+ }
211+ return decisionTwo ;
212+ }
213+ }));
214+
215+ // no debug should just short-circuit to no, no matter what kind of no type return the first decider returns
216+ final ShardRouting shardRouting = ShardRouting .newUnassigned (new ShardId ("test" , "testUUID" , 0 ), true ,
217+ RecoverySource .ExistingStoreRecoverySource .INSTANCE , new UnassignedInfo (UnassignedInfo .Reason .INDEX_CREATED , "_message" ));
218+ final RoutingNode routingNode = new RoutingNode ("testNode" , null );
219+ final ClusterState clusterState = ClusterState .builder (new ClusterName ("test" )).build ();
220+ final IndexMetadata indexMetadata =
221+ IndexMetadata .builder ("idx" ).settings (settings (Version .CURRENT )).numberOfShards (1 ).numberOfReplicas (0 ).build ();
222+
223+ final RoutingAllocation allocation = new RoutingAllocation (allocationDeciders ,
224+ clusterState .getRoutingNodes (), clusterState , null , null ,0L );
225+ assertSame (Decision .NO , allocationDeciders .canAllocate (shardRouting , routingNode , allocation ));
226+ assertSame (Decision .NO , allocationDeciders .canRebalance (shardRouting , allocation ));
227+ assertSame (Decision .NO , allocationDeciders .canRemain (shardRouting , routingNode , allocation ));
228+ assertSame (Decision .NO , allocationDeciders .canAllocate (shardRouting , allocation ));
229+ assertSame (Decision .NO , allocationDeciders .canAllocate (indexMetadata , routingNode , allocation ));
230+ assertSame (Decision .NO , allocationDeciders .shouldAutoExpandToNode (indexMetadata , null , allocation ));
231+ assertSame (Decision .NO , allocationDeciders .canRebalance (allocation ));
232+ assertSame (Decision .NO , allocationDeciders .canForceAllocatePrimary (shardRouting , routingNode , allocation ));
233+
234+ // debug decision should contain both individual decisions in a multi-decision
235+ allocation .debugDecision (true );
236+ final Decision expectedDebugDecision = new Decision .Multi ().add (decisionOne ).add (decisionTwo );
237+ assertEquals (expectedDebugDecision , allocationDeciders .canAllocate (shardRouting , routingNode , allocation ));
238+ assertEquals (expectedDebugDecision , allocationDeciders .canRebalance (shardRouting , allocation ));
239+ assertEquals (expectedDebugDecision , allocationDeciders .canRemain (shardRouting , routingNode , allocation ));
240+ assertEquals (expectedDebugDecision , allocationDeciders .canAllocate (shardRouting , allocation ));
241+ assertEquals (expectedDebugDecision , allocationDeciders .canAllocate (indexMetadata , routingNode , allocation ));
242+ assertEquals (expectedDebugDecision , allocationDeciders .shouldAutoExpandToNode (indexMetadata , null , allocation ));
243+ assertEquals (expectedDebugDecision , allocationDeciders .canRebalance (allocation ));
244+ assertEquals (expectedDebugDecision , allocationDeciders .canForceAllocatePrimary (shardRouting , routingNode , allocation ));
245+ }
120246}
0 commit comments