luoyuan3471 commented on code in PR #8752:
URL: https://github.com/apache/hadoop/pull/8752#discussion_r4657275087


##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/allocator/TestRegularContainerAllocator.java:
##########
@@ -0,0 +1,227 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.allocator;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.apache.hadoop.yarn.api.records.NodeId;
+import org.apache.hadoop.yarn.api.records.Resource;
+import org.apache.hadoop.yarn.api.records.ResourceRequest;
+import org.apache.hadoop.yarn.conf.YarnConfiguration;
+import org.apache.hadoop.yarn.server.resourcemanager.RMContext;
+import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.AppSchedulingInfo;
+import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceLimits;
+import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceUsage;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.AbstractLeafQueue;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.SchedulingMode;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.PendingAsk;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.fica.FiCaSchedulerApp;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.fica.FiCaSchedulerNode;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.placement.AppPlacementAllocator;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.placement.CandidateNodeSet;
+import org.apache.hadoop.yarn.server.scheduler.SchedulerRequestKey;
+import org.apache.hadoop.yarn.util.resource.DefaultResourceCalculator;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class TestRegularContainerAllocator {
+
+  private FiCaSchedulerApp application;
+  private RMContext rmContext;
+  private SchedulerRequestKey schedulerKey;
+  private CandidateNodeSet<FiCaSchedulerNode> candidates;
+  private ResourceLimits resourceLimits;
+  private AppSchedulingInfo appSchedulingInfo;
+
+  @BeforeEach
+  public void setUp() {
+    application = mock(FiCaSchedulerApp.class);
+    rmContext = mock(RMContext.class);
+    when(rmContext.getYarnConfiguration()).thenReturn(new YarnConfiguration());
+
+    schedulerKey = mock(SchedulerRequestKey.class);
+    candidates = mock(CandidateNodeSet.class);
+    resourceLimits = mock(ResourceLimits.class);
+    appSchedulingInfo = mock(AppSchedulingInfo.class);
+
+    when(application.getAppSchedulingInfo()).thenReturn(appSchedulingInfo);
+    when(application.getPendingAsk(schedulerKey, ResourceRequest.ANY))
+        .thenReturn(new PendingAsk(Resource.newInstance(1024, 1), 1));
+    when(application.getOutstandingAsksCount(schedulerKey)).thenReturn(1);
+
+    AbstractLeafQueue leafQueue = mock(AbstractLeafQueue.class);
+    when(leafQueue.isReservationsContinueLooking()).thenReturn(true);
+    when(application.getCSLeafQueue()).thenReturn(leafQueue);
+    when(application.getAppAttemptResourceUsage()).thenReturn(new 
ResourceUsage());
+    when(resourceLimits.getHeadroom())
+        .thenReturn(Resource.newInstance(100 * 1024, 100));
+    when(candidates.getPartition()).thenReturn("");
+  }
+
+  @Test
+  public void testReservationFallbackUsesReservationNode() throws Exception {
+    FiCaSchedulerNode reservationNode = mock(FiCaSchedulerNode.class);
+    FiCaSchedulerNode skippedNode = mock(FiCaSchedulerNode.class);
+    when(reservationNode.getNodeID())
+        .thenReturn(NodeId.newInstance("reservation", 1234));
+    when(skippedNode.getNodeID())
+        .thenReturn(NodeId.newInstance("skipped", 1234));
+
+    mockPlacement(reservationNode, skippedNode);
+    allowNodePrecheck(reservationNode);
+
+    ContainerAllocation reservation = new ContainerAllocation(null,
+        Resource.newInstance(1024, 1), AllocationState.RESERVED);
+    TestAllocator allocator =
+        new TestAllocator(application, rmContext, reservation);
+
+    ContainerAllocation result = allocate(allocator);
+
+    assertEquals(AllocationState.RESERVED, result.getAllocationState());
+    assertSame(reservationNode, allocator.getLastDoAllocationNode());
+  }
+
+  @Test
+  public void testLaterReservationReplacesEarlierReservation()
+      throws Exception {
+    FiCaSchedulerNode firstReservationNode = mock(FiCaSchedulerNode.class);
+    FiCaSchedulerNode secondReservationNode = mock(FiCaSchedulerNode.class);
+    FiCaSchedulerNode skippedNode = mock(FiCaSchedulerNode.class);
+    when(firstReservationNode.getNodeID())
+        .thenReturn(NodeId.newInstance("reservation1", 1234));
+    when(secondReservationNode.getNodeID())
+        .thenReturn(NodeId.newInstance("reservation2", 1234));
+    when(skippedNode.getNodeID())
+        .thenReturn(NodeId.newInstance("skipped", 1234));
+
+    mockPlacement(firstReservationNode, secondReservationNode, skippedNode);
+    allowNodePrecheck(firstReservationNode);
+    allowNodePrecheck(secondReservationNode);
+
+    ContainerAllocation firstReservation = new ContainerAllocation(null,
+        Resource.newInstance(1024, 1), AllocationState.RESERVED);
+    ContainerAllocation secondReservation = new ContainerAllocation(null,
+        Resource.newInstance(2048, 1), AllocationState.RESERVED);
+    TestAllocator allocator = new TestAllocator(application, rmContext,
+        firstReservation, secondReservation);
+
+    ContainerAllocation result = allocate(allocator);
+
+    assertSame(secondReservation, result);
+    assertSame(secondReservationNode, allocator.getLastDoAllocationNode());
+  }
+
+  @Test
+  public void testFailedAllocationPreventsReservationFallback()

Review Comment:
   I added a comment explaining what the existing failure test preserves. The 
new success test checks that `doAllocation` runs exactly once, on the later 
node. A MockRM test also verifies that the AM receives the container on h2 and 
no reservation is left on h1.



##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/allocator/RegularContainerAllocator.java:
##########
@@ -840,14 +840,15 @@ ContainerAllocation doAllocation(ContainerAllocation 
allocationResult,
     return allocationResult;
   }
 
-  private ContainerAllocation allocate(Resource clusterResource,
+  ContainerAllocation allocate(Resource clusterResource,

Review Comment:
   Thanks, I added `@VisibleForTesting` to `allocate()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to