liuml07 commented on a change in pull request #2223:
URL: https://github.com/apache/hadoop/pull/2223#discussion_r487367770



##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be one statement (shorter and clearer)
   ```
      policies.forEach((k,v) -> 
sb.append(",").append(k).append(DELIM).append(v));
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.

Review comment:
       Since this JIRA has been moved from YARN to MAPREDUCE project, should we 
replace the `YARN-10398` in comment with the new JIRA number `MAPREDUCE-7294`?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
       // If no policy is provided, we will reset the config by setting an 
empty string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       Also, given the code is so simple, maybe we can save one extra private 
helper method, and move the logic back to `setSharedCacheUploadPolicies()` 
method, which itself has only several lines of code.

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1);
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1); // do we need this, or it is just fine?
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
   // If no policy is provided, we will reset the config by setting an empty 
string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be one statement (shorter and clearer)
   ```
      policies.forEach((k,v) -> 
sb.append(",").append(k).append(DELIM).append(v));
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.

Review comment:
       Since this JIRA has been moved from YARN to MAPREDUCE project, should we 
replace the `YARN-10398` in comment with the new JIRA number `MAPREDUCE-7294`?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
       // If no policy is provided, we will reset the config by setting an 
empty string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       Also, given the code is so simple, maybe we can save one extra private 
helper method, and move the logic back to `setSharedCacheUploadPolicies()` 
method, which itself has only several lines of code.

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1);
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1); // do we need this, or it is just fine?
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
   // If no policy is provided, we will reset the config by setting an empty 
string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be one statement (shorter and clearer)
   ```
      policies.forEach((k,v) -> 
sb.append(",").append(k).append(DELIM).append(v));
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.

Review comment:
       Since this JIRA has been moved from YARN to MAPREDUCE project, should we 
replace the `YARN-10398` in comment with the new JIRA number `MAPREDUCE-7294`?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
       // If no policy is provided, we will reset the config by setting an 
empty string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       Also, given the code is so simple, maybe we can save one extra private 
helper method, and move the logic back to `setSharedCacheUploadPolicies()` 
method, which itself has only several lines of code.

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1);
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1); // do we need this, or it is just fine?
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
   // If no policy is provided, we will reset the config by setting an empty 
string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be one statement (shorter and clearer)
   ```
      policies.forEach((k,v) -> 
sb.append(",").append(k).append(DELIM).append(v));
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.

Review comment:
       Since this JIRA has been moved from YARN to MAPREDUCE project, should we 
replace the `YARN-10398` in comment with the new JIRA number `MAPREDUCE-7294`?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
       // If no policy is provided, we will reset the config by setting an 
empty string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       Also, given the code is so simple, maybe we can save one extra private 
helper method, and move the logic back to `setSharedCacheUploadPolicies()` 
method, which itself has only several lines of code.

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1);
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1); // do we need this, or it is just fine?
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
   // If no policy is provided, we will reset the config by setting an empty 
string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be one statement (shorter and clearer)
   ```
      policies.forEach((k,v) -> 
sb.append(",").append(k).append(DELIM).append(v));
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.

Review comment:
       Since this JIRA has been moved from YARN to MAPREDUCE project, should we 
replace the `YARN-10398` in comment with the new JIRA number `MAPREDUCE-7294`?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
       // If no policy is provided, we will reset the config by setting an 
empty string value.
   ```

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       Also, given the code is so simple, maybe we can save one extra private 
helper method, and move the logic back to `setSharedCacheUploadPolicies()` 
method, which itself has only several lines of code.

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1);
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.
+    // In other words, cleaning up existing policies. This is useful when we
+    // try to clean up shared cache upload policies for non-application
+    // master tasks. See YARN-10398 for details.
+    if (policies == null || policies.size() == 0) {
+      return "";
+    }
+    StringBuilder sb = new StringBuilder();

Review comment:
       I know following code was mostly borrowed from the existing code, but 
since we are in Java 8 for Hadoop 3, should we simplify this a bit using this 
chance?
   
   ```
       Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
       Map.Entry<String, Boolean> e;
       if (it.hasNext()) {
         e = it.next();
         sb.append(e.getKey() + DELIM + e.getValue());
       }
       while (it.hasNext()) {
         e = it.next();
         sb.append("," + e.getKey() + DELIM + e.getValue());
       }
   ```
   can be shorter and clearer statement, for e.g.
   ```
       policies.forEach((k,v) -> 
sb.append(k).append(DELIM).append(v).append(","));
       sb.deleteCharAt(sb.length() - 1); // do we need this, or it is just fine?
   ```
   
   Thoughts?

##########
File path: 
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
##########
@@ -1450,26 +1450,33 @@ public static void 
setArchiveSharedCacheUploadPolicies(Configuration conf,
    */
   private static void setSharedCacheUploadPolicies(Configuration conf,
       Map<String, Boolean> policies, boolean areFiles) {
-    if (policies != null) {
-      StringBuilder sb = new StringBuilder();
-      Iterator<Map.Entry<String, Boolean>> it = policies.entrySet().iterator();
-      Map.Entry<String, Boolean> e;
-      if (it.hasNext()) {
-        e = it.next();
-        sb.append(e.getKey() + DELIM + e.getValue());
-      } else {
-        // policies is an empty map, just skip setting the parameter
-        return;
-      }
-      while (it.hasNext()) {
-        e = it.next();
-        sb.append("," + e.getKey() + DELIM + e.getValue());
-      }
-      String confParam =
-          areFiles ? MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES
-              : MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
-      conf.set(confParam, sb.toString());
+    String confParam = areFiles ?
+        MRJobConfig.CACHE_FILES_SHARED_CACHE_UPLOAD_POLICIES :
+        MRJobConfig.CACHE_ARCHIVES_SHARED_CACHE_UPLOAD_POLICIES;
+    conf.set(confParam, populateSharedCacheUploadPolicies(policies));
+  }
+
+  private static String populateSharedCacheUploadPolicies(
+      Map<String, Boolean> policies) {
+    // If policies are an empty map or null, we will set EMPTY_STRING.

Review comment:
       nit: this sentence can be:
   ```
   // If no policy is provided, we will reset the config by setting an empty 
string value.
   ```




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to