Skip to content

Commit b3e3eb2

Browse files
Fix bug
1 parent 54bb5e7 commit b3e3eb2

File tree

4 files changed

+51
-29
lines changed

4 files changed

+51
-29
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/placement/CSMappingPlacementRule.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ private VariableContext createVariableContext(
228228
ApplicationSubmissionContext asc, String user) {
229229
VariableContext vctx = new VariableContext();
230230

231-
vctx.put("%user", cleanName(user));
231+
String cleanedName = cleanName(user);
232+
if (!user.equals(cleanedName)) {
233+
vctx.putOriginal("%user", user);
234+
}
235+
vctx.put("%user", cleanedName);
232236
//If the specified matches the default it means NO queue have been specified
233237
//as per ClientRMService#submitApplication which sets the queue to default
234238
//when no queue is provided.
@@ -239,7 +243,7 @@ private VariableContext createVariableContext(
239243
//Adding specified as empty will prevent it to be undefined and it won't
240244
//try to place the application to a queue named '%specified', queue path
241245
//validation will reject the empty path or the path with empty parts,
242-
//so we sill still hit the fallback action of this rule if no queue
246+
//so we still hit the fallback action of this rule if no queue
243247
//is specified
244248
vctx.put("%specified", "");
245249
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/placement/VariableContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class VariableContext {
3939
* This is our actual variable store.
4040
*/
4141
private Map<String, String> variables = new HashMap<>();
42+
private Map<String, String> originalVariables = new HashMap<>();
4243

4344
/**
4445
* This is our conditional variable store.
@@ -124,6 +125,10 @@ public VariableContext put(String name, String value) {
124125
return this;
125126
}
126127

128+
public void putOriginal(String name, String value) {
129+
originalVariables.put(name, value);
130+
}
131+
127132
/**
128133
* This method is used to add a conditional variable to the variable context.
129134
* @param name Name of the variable
@@ -149,6 +154,11 @@ public String get(String name) {
149154
String ret = variables.get(name);
150155
return ret == null ? "" : ret;
151156
}
157+
158+
public String getOriginal(String name) {
159+
String ret = originalVariables.get(name);
160+
return ret;
161+
}
152162

153163
/**
154164
* Adds a set to the context, each name can only be added once. The extra

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/placement/csmappingrule/MappingRuleMatchers.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public boolean match(VariableContext variables) {
8787
}
8888

8989
String substituted = variables.replaceVariables(value);
90+
91+
String originalVariableValue = variables.getOriginal(variable);
92+
if (originalVariableValue != null) {
93+
return substituted.equals(originalVariableValue);
94+
}
95+
9096
return substituted.equals(variables.get(variable));
9197
}
9298

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/placement/csmappingrule/TestCSMappingPlacementRule.java

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerQueueManager;
3838
import org.apache.hadoop.yarn.util.Records;
3939
import org.apache.log4j.Level;
40+
import org.junit.Before;
4041
import org.junit.Rule;
4142
import org.junit.Test;
4243
import org.junit.rules.TemporaryFolder;
@@ -69,6 +70,11 @@ public class TestCSMappingPlacementRule {
6970

7071
@Rule
7172
public TemporaryFolder folder = new TemporaryFolder();
73+
74+
@Before
75+
public void setUp() {
76+
setLoggerToDebug();
77+
}
7278

7379
private Map<String, Set<String>> userGroups =
7480
ImmutableMap.<String, Set<String>>builder()
@@ -89,6 +95,7 @@ private void createQueueHierarchy(CapacitySchedulerQueueManager queueManager) {
8995
.withQueue("root.user.alice")
9096
.withQueue("root.user.bob")
9197
.withQueue("root.user.test_dot_user")
98+
.withQueue("root.user.testuser")
9299
.withQueue("root.groups.main_dot_grp")
93100
.withQueue("root.groups.sec_dot_test_dot_grp")
94101
.withQueue("root.secondaryTests.unique")
@@ -862,18 +869,12 @@ public List<MappingRule> getMappingRules() {
862869
}
863870

864871
@Test
865-
public void testUserNameSetDefaultAndPlaceWith2Rules() throws IOException {
866-
Log log = LogFactory.getLog("org.apache.hadoop.yarn.server.resourcemanager.placement");
867-
if (log instanceof Log4JLogger) {
868-
org.apache.log4j.Logger logger = ((Log4JLogger) log).getLogger();
869-
logger.setLevel(Level.DEBUG);
870-
}
871-
872+
public void testOriginalUserNameWithDotCanBeUsedInMatchExpression() throws IOException {
872873
List<MappingRule> rules = new ArrayList<>();
873874
rules.add(
874875
new MappingRule(
875876
MappingRuleMatchers.createUserMatcher("test.user"),
876-
(MappingRuleActions.createUpdateDefaultAction("root.user"))
877+
(MappingRuleActions.createUpdateDefaultAction("root.user.testuser"))
877878
.setFallbackSkip()));
878879
rules.add(new MappingRule(
879880
MappingRuleMatchers.createUserMatcher("test.user"),
@@ -884,34 +885,35 @@ public void testUserNameSetDefaultAndPlaceWith2Rules() throws IOException {
884885
ApplicationSubmissionContext app = createApp("app");
885886
assertPlace(
886887
"test.user should be placed to root.user",
887-
engine, app, "test.user", "root.user");
888+
engine, app, "test.user", "root.user.testuser");
888889
}
889890

890891
@Test
891-
public void testUserNameSetDefaultAndPlaceWith2RulesUsernameReplacedWithDot() throws IOException {
892-
Log log = LogFactory.getLog("org.apache.hadoop.yarn.server.resourcemanager.placement");
893-
if (log instanceof Log4JLogger) {
894-
org.apache.log4j.Logger logger = ((Log4JLogger) log).getLogger();
895-
logger.setLevel(Level.DEBUG);
896-
}
897-
898-
899-
ArrayList<MappingRule> rules = new ArrayList<>();
892+
public void testOriginalGroupNameWithDotCanBeUsedInMatchExpression() throws IOException {
893+
List<MappingRule> rules = new ArrayList<>();
900894
rules.add(
901-
new MappingRule(
902-
MappingRuleMatchers.createUserMatcher("test_dot_user"),
903-
(MappingRuleActions.createUpdateDefaultAction("root.user.bob"))
904-
.setFallbackSkip()));
895+
new MappingRule(
896+
MappingRuleMatchers.createUserGroupMatcher("sec.test.grp"),
897+
(MappingRuleActions.createUpdateDefaultAction("root.user.testuser"))
898+
.setFallbackSkip()));
905899
rules.add(new MappingRule(
906-
MappingRuleMatchers.createUserMatcher("test_dot_user"),
907-
(MappingRuleActions.createPlaceToDefaultAction())
908-
.setFallbackReject()));
900+
MappingRuleMatchers.createUserMatcher("test.user"),
901+
(MappingRuleActions.createPlaceToDefaultAction())
902+
.setFallbackReject()));
909903

910904
CSMappingPlacementRule engine = setupEngine(true, rules);
911905
ApplicationSubmissionContext app = createApp("app");
912906
assertPlace(
913-
"test.user should be placed to root.user.bob",
914-
engine, app, "test.user", "root.user.bob");
907+
"test.user should be placed to root.user",
908+
engine, app, "test.user", "root.user.testuser");
909+
}
910+
911+
private static void setLoggerToDebug() {
912+
Log log = LogFactory.getLog("org.apache.hadoop.yarn.server.resourcemanager.placement");
913+
if (log instanceof Log4JLogger) {
914+
org.apache.log4j.Logger logger = ((Log4JLogger) log).getLogger();
915+
logger.setLevel(Level.DEBUG);
916+
}
915917
}
916918

917919
private CSMappingPlacementRule initPlacementEngine(CapacityScheduler cs) throws IOException {

0 commit comments

Comments
 (0)