From 97171b9b1833f45e123eb100362a5518c5112b6e Mon Sep 17 00:00:00 2001 From: Inigo Goiri Date: Wed, 13 May 2020 10:04:12 -0700 Subject: [PATCH] YARN-8942. PriorityBasedRouterPolicy throws exception if all sub-cluster weights have negative value. Contributed by Bilwa S T. (cherry picked from commit 108ecf992f0004dd64a7143d1c400de1361b13f3) --- .../policies/router/PriorityRouterPolicy.java | 5 ++++ .../router/TestPriorityRouterPolicy.java | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/router/PriorityRouterPolicy.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/router/PriorityRouterPolicy.java index a1f7666a9f..b81ca07b42 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/router/PriorityRouterPolicy.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/policies/router/PriorityRouterPolicy.java @@ -24,6 +24,7 @@ import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext; import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.server.federation.policies.FederationPolicyUtils; +import org.apache.hadoop.yarn.server.federation.policies.exceptions.FederationPolicyException; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterId; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterIdInfo; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterInfo; @@ -65,6 +66,10 @@ public SubClusterId getHomeSubcluster( chosen = id; } } + if (chosen == null) { + throw new FederationPolicyException( + "No Active Subcluster with weight vector greater than zero"); + } return chosen; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/federation/policies/router/TestPriorityRouterPolicy.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/federation/policies/router/TestPriorityRouterPolicy.java index 3c036c1812..e1799d3210 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/federation/policies/router/TestPriorityRouterPolicy.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/federation/policies/router/TestPriorityRouterPolicy.java @@ -16,6 +16,7 @@ */ package org.apache.hadoop.yarn.server.federation.policies.router; +import static org.apache.hadoop.test.LambdaTestUtils.intercept; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -24,6 +25,7 @@ import org.apache.hadoop.yarn.exceptions.YarnException; import org.apache.hadoop.yarn.server.federation.policies.dao.WeightedPolicyInfo; +import org.apache.hadoop.yarn.server.federation.policies.exceptions.FederationPolicyException; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterId; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterIdInfo; import org.apache.hadoop.yarn.server.federation.store.records.SubClusterInfo; @@ -82,4 +84,31 @@ public void testPickLowestWeight() throws YarnException { Assert.assertEquals("sc5", chosen.getId()); } + @Test + public void testZeroSubClustersWithPositiveWeight() throws Exception { + Map routerWeights = new HashMap<>(); + Map amrmWeights = new HashMap<>(); + // Set negative value to all subclusters + for (int i = 0; i < 5; i++) { + SubClusterIdInfo sc = new SubClusterIdInfo("sc" + i); + + SubClusterInfo sci = mock(SubClusterInfo.class); + when(sci.getState()).thenReturn(SubClusterState.SC_RUNNING); + when(sci.getSubClusterId()).thenReturn(sc.toId()); + getActiveSubclusters().put(sc.toId(), sci); + routerWeights.put(sc, 0.0f); + amrmWeights.put(sc, -1.0f); + } + getPolicyInfo().setRouterPolicyWeights(routerWeights); + getPolicyInfo().setAMRMPolicyWeights(amrmWeights); + FederationPoliciesTestUtil.initializePolicyContext(getPolicy(), + getPolicyInfo(), getActiveSubclusters()); + + intercept(FederationPolicyException.class, + "No Active Subcluster with weight vector greater than zero", + () -> ((FederationRouterPolicy) getPolicy()) + .getHomeSubcluster(getApplicationSubmissionContext(), null)); + } + + }