Repository: ambari
Updated Branches:
  refs/heads/branch-2.2 1beedcf4a -> 46434ed19


AMBARI-16270. Blueprint processor should create ConfigGroup even with only one 
host registered. (stoader)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/46434ed1
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/46434ed1
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/46434ed1

Branch: refs/heads/branch-2.2
Commit: 46434ed196cd16f73358a4e292933103a1a1a452
Parents: 1beedcf
Author: Toader, Sebastian <[email protected]>
Authored: Fri May 6 16:42:39 2016 +0200
Committer: Toader, Sebastian <[email protected]>
Committed: Fri May 6 16:43:02 2016 +0200

----------------------------------------------------------------------
 .../ambari/server/topology/AmbariContext.java   | 26 +++++++++-
 .../server/topology/AmbariContextTest.java      | 52 ++++++++++++++++++++
 2 files changed, 76 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/46434ed1/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
index cf1a6ac..3907a43 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/topology/AmbariContext.java
@@ -60,6 +60,7 @@ import org.apache.ambari.server.utils.RetryHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.annotation.Nullable;
 import javax.inject.Inject;
 import java.util.Collection;
 import java.util.Collections;
@@ -73,6 +74,8 @@ import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicLong;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
 
 /**
  * Provides topology related information as well as access to the core Ambari 
functionality.
@@ -609,9 +612,28 @@ public class AmbariContext {
       groupHosts = topology.getHostGroupInfo().
           get(groupName).getHostNames();
 
+      // remove hosts that are not assigned to the cluster yet
+      String clusterName = null;
+      try {
+        clusterName = getClusterName(topology.getClusterId());
+      } catch (AmbariException e) {
+        LOG.error("Cannot get cluster name for clusterId = " + 
topology.getClusterId(), e);
+        throw new RuntimeException(e);
+      }
+
+      final Map<String, Host> clusterHosts = 
getController().getClusters().getHostsForCluster(clusterName);
+      Iterable<String> filteredGroupHosts = Iterables.filter(groupHosts, new 
com.google.common.base.Predicate<String>() {
+        @Override
+        public boolean apply(@Nullable String groupHost) {
+          return clusterHosts.containsKey(groupHost);
+        }
+      });
+
+
+
       ConfigGroupRequest request = new ConfigGroupRequest(
-          null, getClusterName(topology.getClusterId()), absoluteGroupName, 
service, "Host Group Configuration",
-          new HashSet<String>(groupHosts), serviceConfigs);
+          null, clusterName, absoluteGroupName, service, "Host Group 
Configuration",
+        Sets.newHashSet(filteredGroupHosts), serviceConfigs);
 
       // get the config group provider and create config group resource
       ConfigGroupResourceProvider configGroupProvider = 
(ConfigGroupResourceProvider)

http://git-wip-us.apache.org/repos/asf/ambari/blob/46434ed1/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
index 1613d11..7d25fd5 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/topology/AmbariContextTest.java
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.topology;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import org.apache.ambari.server.controller.AmbariManagementController;
@@ -199,6 +200,9 @@ public class AmbariContextTest {
     expect(clusters.getHost(HOST1)).andReturn(host1).anyTimes();
     expect(clusters.getHost(HOST2)).andReturn(host2).anyTimes();
 
+    Map<String, Host> clusterHosts = ImmutableMap.of(HOST1, host1, HOST2, 
host2);
+    
expect(clusters.getHostsForCluster(CLUSTER_NAME)).andReturn(clusterHosts).anyTimes();
+
     expect(cluster.getClusterId()).andReturn(CLUSTER_ID).anyTimes();
     expect(cluster.getClusterName()).andReturn(CLUSTER_NAME).anyTimes();
 
@@ -358,6 +362,54 @@ public class AmbariContextTest {
   }
 
   @Test
+  public void 
testRegisterHostWithConfigGroup_createNewConfigGroupWithPendingHosts() throws 
Exception {
+    // test specific expectations
+    expect(cluster.getConfigGroups()).andReturn(Collections.<Long, 
ConfigGroup>emptyMap()).once();
+    
expect(clusterController.ensureResourceProvider(Resource.Type.ConfigGroup)).andReturn(configGroupResourceProvider).once();
+    //todo: for now not using return value so just returning null
+    
expect(configGroupResourceProvider.createResources(capture(configGroupRequestCapture))).andReturn(null).once();
+    configHelper.moveDeprecatedGlobals(stackId, 
group1Configuration.getFullProperties(1), CLUSTER_NAME);
+
+    reset(group1Info);
+    
expect(group1Info.getConfiguration()).andReturn(group1Configuration).anyTimes();
+    Collection<String> groupHosts = ImmutableList.of(HOST1, HOST2, 
"pending_host"); // pending_host is not registered with the cluster
+    expect(group1Info.getHostNames()).andReturn(groupHosts).anyTimes(); // 
there are 3 hosts for the host group
+    // replay all mocks
+    replayAll();
+
+    // test
+    context.registerHostWithConfigGroup(HOST1, topology, HOST_GROUP_1);
+
+    // assertions
+    Set<ConfigGroupRequest> configGroupRequests = 
configGroupRequestCapture.getValue();
+    assertEquals(1, configGroupRequests.size());
+    ConfigGroupRequest configGroupRequest = 
configGroupRequests.iterator().next();
+    assertEquals(CLUSTER_NAME, configGroupRequest.getClusterName());
+    assertEquals("testBP:group1", configGroupRequest.getGroupName());
+    assertEquals("service1", configGroupRequest.getTag());
+    assertEquals("Host Group Configuration", 
configGroupRequest.getDescription());
+    Collection<String> requestHosts = configGroupRequest.getHosts();
+
+    // we expect only HOST1 and HOST2 in the config group request as the third 
host "pending_host" hasn't registered yet with the cluster
+    assertEquals(2, requestHosts.size());
+    assertTrue(requestHosts.contains(HOST1));
+    assertTrue(requestHosts.contains(HOST2));
+
+    Map<String, Config> requestConfig = configGroupRequest.getConfigs();
+    assertEquals(1, requestConfig.size());
+    Config type1Config = requestConfig.get("type1");
+    //todo: other properties such as cluster name are not currently being 
explicitly set on config
+    assertEquals("type1", type1Config.getType());
+    assertEquals("group1", type1Config.getTag());
+    Map<String, String> requestProps = type1Config.getProperties();
+    assertEquals(3, requestProps.size());
+    // 1.2 is overridden value
+    assertEquals("val1.2", requestProps.get("prop1"));
+    assertEquals("val2", requestProps.get("prop2"));
+    assertEquals("val3", requestProps.get("prop3"));
+  }
+
+  @Test
   public void 
testRegisterHostWithConfigGroup_registerWithExistingConfigGroup() throws 
Exception {
     // test specific expectations
     expect(cluster.getConfigGroups()).andReturn(configGroups).once();

Reply via email to