Bladeren bron

AMBARI-11612. Changing only 'final' flag for a config not being persisted by API (dlysnichenko)

Lisnichenko Dmitro 10 jaren geleden
bovenliggende
commit
3bb7a6dc3a

+ 49 - 0
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java

@@ -1285,14 +1285,22 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     if (request.getDesiredConfig() != null) {
       for (ConfigurationRequest desiredConfig : request.getDesiredConfig()) {
         Map<String, String> requestConfigProperties = desiredConfig.getProperties();
+        Map<String,Map<String,String>> requestConfigAttributes = desiredConfig.getPropertiesAttributes();
         Config clusterConfig = cluster.getDesiredConfigByType(desiredConfig.getType());
         Map<String, String> clusterConfigProperties = null;
+        Map<String,Map<String,String>> clusterConfigAttributes = null;
         if (clusterConfig != null) {
           clusterConfigProperties = clusterConfig.getProperties();
+          clusterConfigAttributes = clusterConfig.getPropertiesAttributes();
+          if (!isAttributeMapsEqual(requestConfigAttributes, clusterConfigAttributes)){
+            isConfigurationCreationNeeded = true;
+            break;            
+          }
         } else {
           isConfigurationCreationNeeded = true;
           break;
         }
+        
         if (requestConfigProperties == null || requestConfigProperties.isEmpty()) {
           Config existingConfig = cluster.getConfig(desiredConfig.getType(), desiredConfig.getVersionTag());
           if (existingConfig != null) {
@@ -1490,6 +1498,47 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     }
   }
 
+  /**
+   * Comparison of two attributes maps 
+   * @param requestConfigAttributes - attribute map sent from API
+   * @param clusterConfigAttributes - existed attribute map
+   * @return true if maps is equal (have the same attributes and their values) 
+   */
+  public boolean isAttributeMapsEqual(Map<String, Map<String, String>> requestConfigAttributes,
+          Map<String, Map<String, String>> clusterConfigAttributes) {
+    boolean isAttributesEqual = true;
+    if ((requestConfigAttributes != null && clusterConfigAttributes == null)
+            || (requestConfigAttributes == null && clusterConfigAttributes != null)
+            || (requestConfigAttributes != null && clusterConfigAttributes != null 
+            && !requestConfigAttributes.keySet().equals(clusterConfigAttributes.keySet()))) {
+      return false;
+    } else if (clusterConfigAttributes != null && requestConfigAttributes != null) {
+      for (Entry<String, Map<String, String>> ClusterEntrySet : clusterConfigAttributes.entrySet()) {
+        Map<String, String> clusterMapAttributes = ClusterEntrySet.getValue();
+        Map<String, String> requestMapAttributes = requestConfigAttributes.get(ClusterEntrySet.getKey());
+        if ((requestMapAttributes != null && clusterMapAttributes == null)
+                || (requestMapAttributes == null && clusterMapAttributes != null)
+                || (requestMapAttributes != null && clusterMapAttributes != null 
+                && !requestMapAttributes.keySet().equals(clusterMapAttributes.keySet()))) {
+          return false;
+        } else if (requestMapAttributes != null && clusterMapAttributes != null) {
+          for (Entry<String, String> requestPropertyEntrySet : requestMapAttributes.entrySet()) {
+            String requestPropertyValue = requestPropertyEntrySet.getValue();
+            String clusterPropertyValue = clusterMapAttributes.get(requestPropertyEntrySet.getKey());
+            if ((requestPropertyValue != null && clusterPropertyValue == null)
+                    || (requestPropertyValue == null && clusterPropertyValue != null)
+                    || (requestPropertyValue != null && clusterPropertyValue != null 
+                    && !requestPropertyValue.equals(clusterPropertyValue))) {
+              return false;
+            }
+          }
+        }
+
+      }
+    }
+    return isAttributesEqual;
+  } 
+  
   /**
    * Save cluster update results to retrieve later
    * @param clusterRequest   cluster request info

+ 22 - 0
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java

@@ -10035,6 +10035,28 @@ public class AmbariManagementControllerTest {
 
   }
 
+  @Test
+  public void testIsAttributeMapsEqual() {
+    AmbariManagementControllerImpl controllerImpl = null;
+    if (controller instanceof AmbariManagementControllerImpl){
+      controllerImpl = (AmbariManagementControllerImpl)controller;
+    }
+    Map<String, Map<String, String>> requestConfigAttributes = new HashMap<String, Map<String,String>>();
+    Map<String, Map<String, String>> clusterConfigAttributes = new HashMap<String, Map<String,String>>();
+    Assert.assertTrue(controllerImpl.isAttributeMapsEqual(requestConfigAttributes, clusterConfigAttributes));
+    requestConfigAttributes.put("final", new HashMap<String, String>());
+    requestConfigAttributes.get("final").put("c", "true");
+    clusterConfigAttributes.put("final", new HashMap<String, String>());
+    clusterConfigAttributes.get("final").put("c", "true");
+    Assert.assertTrue(controllerImpl.isAttributeMapsEqual(requestConfigAttributes, clusterConfigAttributes));
+    clusterConfigAttributes.put("final2", new HashMap<String, String>());
+    clusterConfigAttributes.get("final2").put("a", "true");
+    Assert.assertFalse(controllerImpl.isAttributeMapsEqual(requestConfigAttributes, clusterConfigAttributes));
+    requestConfigAttributes.put("final2", new HashMap<String, String>());
+    requestConfigAttributes.get("final2").put("a", "false"); 
+    Assert.assertFalse(controllerImpl.isAttributeMapsEqual(requestConfigAttributes, clusterConfigAttributes));
+  } 
+  
   @Test
   public void testEmptyConfigs() throws Exception {
     String clusterName = "c1";