SchedulerUtils { public static void normalizeAndValidateRequest(ResourceRequest resReq, Resource maximumAllocation, String queueName, YarnScheduler scheduler, boolean isRecovery, RMContext rmContext, QueueInfo queueInfo) throws InvalidResourceRequestException { Configuration conf = rmContext.getYarnConfiguration(); if (null != conf && !YarnConfiguration.areNodeLabelsEnabled(conf)) { String labelExp = resReq.getNodeLabelExpression(); if (!(RMNodeLabelsManager.NO_LABEL.equals(labelExp) || null == labelExp)) { String message = "NodeLabel is not enabled in cluster, but resource" + " request contains a label expression."; LOG.warn(message); if (!isRecovery) { throw new InvalidLabelResourceRequestException( "Invalid resource request, node label not enabled " + "but request contains label expression"); } } } if (null == queueInfo) { try { queueInfo = scheduler.getQueueInfo(queueName, false, false); } catch (IOException e) { } } SchedulerUtils.normalizeNodeLabelExpressionInRequest(resReq, queueInfo); if (!isRecovery) { validateResourceRequest(resReq, maximumAllocation, queueInfo, rmContext); } }  static ContainerStatus createAbnormalContainerStatus(
      ContainerId containerId, String diagnostics); static ContainerStatus createKilledContainerStatus(
      ContainerId containerId, String diagnostics); static ContainerStatus createPreemptedContainerStatus(
      ContainerId containerId, String diagnostics); @VisibleForTesting static void normalizeRequest(
    ResourceRequest ask,
    ResourceCalculator resourceCalculator,
    Resource minimumResource,
    Resource maximumResource); static Resource getNormalizedResource(
      Resource ask,
      ResourceCalculator resourceCalculator,
      Resource minimumResource,
      Resource maximumResource,
      Resource incrementResource); static void normalizeAndValidateRequest(ResourceRequest resReq,
      Resource maximumAllocation, String queueName, YarnScheduler scheduler,
      boolean isRecovery, RMContext rmContext, QueueInfo queueInfo); static void normalizeAndValidateRequest(ResourceRequest resReq,
      Resource maximumAllocation, String queueName, YarnScheduler scheduler,
      RMContext rmContext, QueueInfo queueInfo); static MaxResourceValidationResult validateResourceRequestsAgainstQueueMaxResource(
      ResourceRequest resReq, Resource availableResource); static boolean checkQueueLabelExpression(Set<String> queueLabels,
      String labelExpression, RMContext rmContext); static AccessType toAccessType(QueueACL acl); @Private static boolean hasPendingResourceRequest(ResourceCalculator rc,
      ResourceUsage usage, String nodePartition, Resource cluster,
      SchedulingMode schedulingMode); static RMContainer createOpportunisticRmContainer(RMContext rmContext,
      Container container, boolean isRemotelyAllocated); static final String RELEASED_CONTAINER; static final String UPDATED_CONTAINER; static final String LOST_CONTAINER; static final String PREEMPTED_CONTAINER; static final String COMPLETED_APPLICATION; static final String EXPIRED_CONTAINER; static final String UNRESERVED_CONTAINER; }
@Test(timeout = 30000) public void testValidateResourceRequestWithErrorLabelsPermission() throws IOException { ResourceScheduler scheduler = mock(ResourceScheduler.class); Set<String> queueAccessibleNodeLabels = Sets.newHashSet(); QueueInfo queueInfo = mock(QueueInfo.class); when(queueInfo.getQueueName()).thenReturn("queue"); when(queueInfo.getAccessibleNodeLabels()) .thenReturn(queueAccessibleNodeLabels); when(scheduler.getQueueInfo(any(String.class), anyBoolean(), anyBoolean())) .thenReturn(queueInfo); when(rmContext.getScheduler()).thenReturn(scheduler); Resource maxResource = Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays.asList("x", "y")); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"), NodeLabel.newInstance("y"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression("y"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(""); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(" "); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when request labels is a subset of queue labels"); } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x", "y")); } try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays.asList("x", "y")); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays.asList("x", "y")); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"), NodeLabel.newInstance("y"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("z"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x", "y")); } try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays.asList("x", "y")); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"), NodeLabel.newInstance("y"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x && y"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x", "y")); } queueAccessibleNodeLabels.clear(); try { queueAccessibleNodeLabels.clear(); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(""); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(" "); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when request labels is empty"); } boolean invalidlabelexception = false; try { queueAccessibleNodeLabels.clear(); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidLabelResourceRequestException e) { invalidlabelexception = true; } catch (InvalidResourceRequestException e) { } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x")); } Assert.assertTrue("InvalidLabelResourceRequestException expected", invalidlabelexception); try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.add(RMNodeLabelsManager.ANY); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"), NodeLabel.newInstance("y"), NodeLabel.newInstance("z"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression("y"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); resReq.setNodeLabelExpression("z"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when queue can access any labels"); } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x", "y", "z")); } try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.add(RMNodeLabelsManager.ANY); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays.asList("x", "y")); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"), NodeLabel.newInstance("y"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), "rack", resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x", "y")); } try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays .asList(CommonNodeLabelsManager.ANY)); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), "rack", resource, 1); resReq.setNodeLabelExpression("x"); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x")); } try { Resource resource = Resources.createResource(0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq1 = BuilderUtils .newResourceRequest(mock(Priority.class), "*", resource, 1, "x"); normalizeAndvalidateRequest(resReq1, "queue", scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { assertEquals("Invalid label resource request, cluster do not contain , " + "label= x", e.getMessage()); } try { rmContext.getYarnConfiguration() .set(YarnConfiguration.NODE_LABELS_ENABLED, "false"); Resource resource = Resources.createResource(0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq1 = BuilderUtils .newResourceRequest(mock(Priority.class), "*", resource, 1, "x"); normalizeAndvalidateRequest(resReq1, "queue", scheduler, rmContext, maxResource); Assert.assertEquals(RMNodeLabelsManager.NO_LABEL, resReq1.getNodeLabelExpression()); } catch (InvalidResourceRequestException e) { assertEquals("Invalid resource request, node label not enabled but " + "request contains label expression", e.getMessage()); } }
@Test(timeout = 30000) public void testNormalizeNodeLabelExpression() throws IOException { ResourceScheduler scheduler = mock(ResourceScheduler.class); Set<String> queueAccessibleNodeLabels = Sets.newHashSet(); QueueInfo queueInfo = mock(QueueInfo.class); when(queueInfo.getQueueName()).thenReturn("queue"); when(queueInfo.getAccessibleNodeLabels()).thenReturn(queueAccessibleNodeLabels); when(queueInfo.getDefaultNodeLabelExpression()).thenReturn(" x "); when(scheduler.getQueueInfo(any(String.class), anyBoolean(), anyBoolean())) .thenReturn(queueInfo); Resource maxResource = Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); when(rmContext.getScheduler()).thenReturn(scheduler); try { queueAccessibleNodeLabels.clear(); queueAccessibleNodeLabels.addAll(Arrays.asList("x", "y")); rmContext.getNodeLabelManager().addToCluserNodeLabels( ImmutableSet.of(NodeLabel.newInstance("x"), NodeLabel.newInstance("y"))); Resource resource = Resources.createResource( 0, YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); Assert.assertEquals("x", resReq.getNodeLabelExpression()); resReq.setNodeLabelExpression(" y "); normalizeAndvalidateRequest(resReq, "queue", scheduler, rmContext, maxResource); Assert.assertEquals("y", resReq.getNodeLabelExpression()); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when request labels is a subset of queue labels"); } finally { rmContext.getNodeLabelManager().removeFromClusterNodeLabels( Arrays.asList("x", "y")); } }