Repository: ambari Updated Branches: refs/heads/trunk de420a88a -> 61cebe7c4
AMBARI-19866. Scale requests are failing after removing services from original components of a hostgroup (magyari_sandor) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/61cebe7c Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/61cebe7c Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/61cebe7c Branch: refs/heads/trunk Commit: 61cebe7c4ff8f3312d74d3c1e8dce84ec4934d6b Parents: de420a8 Author: Sandor Magyari <smagy...@hortonworks.com> Authored: Tue Feb 7 15:43:19 2017 +0100 Committer: Sandor Magyari <smagy...@hortonworks.com> Committed: Thu Feb 9 08:27:32 2017 +0100 ---------------------------------------------------------------------- .../ambari/server/topology/AmbariContext.java | 36 +++++----- .../server/topology/AmbariContextTest.java | 69 +++++++++++++++++++- 2 files changed, 87 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/61cebe7c/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 5e887d4..adca3a3 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 @@ -186,7 +186,7 @@ public class AmbariContext { }); } catch (AmbariException e) { - e.printStackTrace(); + LOG.error("Failed to create Cluster resource: ", e); throw new RuntimeException("Failed to create Cluster resource: " + e, e); } } @@ -231,14 +231,14 @@ public class AmbariContext { new RequestImpl(null, Collections.singleton(installProps), null, null), predicate); getServiceResourceProvider().updateResources( - new RequestImpl(null, Collections.singleton(startProps), null, null), predicate); + new RequestImpl(null, Collections.singleton(startProps), null, null), predicate); } catch (Exception e) { // just log as this won't prevent cluster from being provisioned correctly LOG.error("Unable to update state of services during cluster provision: " + e, e); } } - public void createAmbariHostResources(long clusterId, String hostName, Map<String, Collection<String>> components) { + public void createAmbariHostResources(long clusterId, String hostName, Map<String, Collection<String>> components) { Host host; try { host = getController().getClusters().getHost(hostName); @@ -248,13 +248,14 @@ public class AmbariContext { "Unable to obtain host instance '%s' when persisting host resources", hostName)); } - String clusterName = null; + Cluster cluster = null; try { - clusterName = getClusterName(clusterId); + cluster = getController().getClusters().getClusterById(clusterId); } catch (AmbariException e) { - LOG.error("Cannot get cluster name for clusterId = " + clusterId, e); + LOG.error("Cannot get cluster for clusterId = " + clusterId, e); throw new RuntimeException(e); } + String clusterName = cluster.getClusterName(); Map<String, Object> properties = new HashMap<String, Object>(); properties.put(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID, clusterName); @@ -264,18 +265,23 @@ public class AmbariContext { try { getHostResourceProvider().createHosts(new RequestImpl(null, Collections.singleton(properties), null, null)); } catch (AmbariException e) { - e.printStackTrace(); + LOG.error("Unable to create host component resource for host {}", hostName, e); throw new RuntimeException(String.format("Unable to create host resource for host '%s': %s", hostName, e.toString()), e); } final Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>(); + for (Map.Entry<String, Collection<String>> entry : components.entrySet()) { String service = entry.getKey(); for (String component : entry.getValue()) { //todo: handle this in a generic manner. These checks are all over the code - if (!component.equals("AMBARI_SERVER")) { - requests.add(new ServiceComponentHostRequest(clusterName, service, component, hostName, null)); + try { + if (cluster.getService(service) != null && !component.equals("AMBARI_SERVER")) { + requests.add(new ServiceComponentHostRequest(clusterName, service, component, hostName, null)); + } + } catch(AmbariException se) { + LOG.warn("Service already deleted from cluster: {}", service); } } } @@ -288,7 +294,7 @@ public class AmbariContext { } }); } catch (AmbariException e) { - e.printStackTrace(); + LOG.error("Unable to create host component resource for host {}", hostName, e); throw new RuntimeException(String.format("Unable to create host component resource for host '%s': %s", hostName, e.toString()), e); } @@ -328,7 +334,7 @@ public class AmbariContext { createConfigGroupsAndRegisterHost(topology, groupName); } } catch (Exception e) { - e.printStackTrace(); + LOG.error("Unable to register config group for host: ", e); throw new RuntimeException("Unable to register config group for host: " + hostName); } } @@ -392,7 +398,7 @@ public class AmbariContext { } }); } catch (AmbariException e) { - e.printStackTrace(); + LOG.error("Failed to set configurations on cluster: ", e); throw new RuntimeException("Failed to set configurations on cluster: " + e, e); } } @@ -436,7 +442,7 @@ public class AmbariContext { // sleep before checking the config again Thread.sleep(100); } catch (InterruptedException e) { - e.printStackTrace(); + LOG.warn("sleep interrupted"); } } } @@ -627,8 +633,6 @@ public class AmbariContext { } }); - - ConfigGroupRequest request = new ConfigGroupRequest( null, clusterName, absoluteGroupName, service, "Host Group Configuration", Sets.newHashSet(filteredGroupHosts), serviceConfigs); @@ -640,7 +644,7 @@ public class AmbariContext { try { configGroupProvider.createResources(Collections.singleton(request)); } catch (Exception e) { - e.printStackTrace(); + LOG.error("Failed to create new configuration group: " + e); throw new RuntimeException("Failed to create new configuration group: " + e, e); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/61cebe7c/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 b0c4729..80003e5 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 static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.capture; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.createNiceMock; @@ -32,6 +33,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -43,6 +45,7 @@ import java.util.Set; import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.ClusterRequest; import org.apache.ambari.server.controller.ConfigGroupRequest; +import org.apache.ambari.server.controller.ServiceComponentHostRequest; import org.apache.ambari.server.controller.ServiceComponentRequest; import org.apache.ambari.server.controller.ServiceRequest; import org.apache.ambari.server.controller.internal.ComponentResourceProvider; @@ -113,6 +116,7 @@ public class AmbariContextTest { private static final Host host1 = createNiceMock(Host.class); private static final Host host2 = createNiceMock(Host.class); private static final ConfigFactory configFactory = createNiceMock(ConfigFactory.class); + private static final Service mockService1 = createStrictMock(Service.class); private static final Collection<String> blueprintServices = new HashSet<String>(); private static final Map<String, Service> clusterServices = new HashMap<String, Service>(); @@ -259,8 +263,8 @@ public class AmbariContextTest { private void replayAll() { replay(controller, clusterController, hostResourceProvider, serviceResourceProvider, componentResourceProvider, - hostComponentResourceProvider, configGroupResourceProvider, topology, blueprint, stack, clusters, - cluster, group1Info, configHelper, configGroup1, configGroup2, host1, host2, configFactory); + hostComponentResourceProvider, configGroupResourceProvider, topology, blueprint, stack, clusters, + cluster, group1Info, configHelper, configGroup1, configGroup2, host1, host2, configFactory); } @Test @@ -352,6 +356,67 @@ public class AmbariContextTest { } @Test + public void testCreateAmbariHostResources() throws Exception { + // expectations + expect(cluster.getServices()).andReturn(clusterServices).anyTimes(); + + hostResourceProvider.createHosts(anyObject(Request.class)); + expectLastCall().once(); + expect(cluster.getService("service1")).andReturn(mockService1).times(2); + expect(cluster.getService("service2")).andReturn(mockService1).once(); + Capture<Set<ServiceComponentHostRequest>> requestsCapture = EasyMock.newCapture(); + + controller.createHostComponents(capture(requestsCapture)); + expectLastCall().once(); + + replayAll(); + + // test + Map<String, Collection<String>> componentsMap = new HashMap<>(); + Collection<String> components = new ArrayList<>(); + components.add("component1"); + components.add("component2"); + componentsMap.put("service1", components); + components = new ArrayList<>(); + components.add("component3"); + componentsMap.put("service2", components); + + context.createAmbariHostResources(CLUSTER_ID, "host1", componentsMap); + + assertEquals(requestsCapture.getValue().size(), 3); + } + + @Test + public void testCreateAmbariHostResourcesWithMissingService() throws Exception { + // expectations + expect(cluster.getServices()).andReturn(clusterServices).anyTimes(); + + hostResourceProvider.createHosts(anyObject(Request.class)); + expectLastCall().once(); + expect(cluster.getService("service1")).andReturn(mockService1).times(2); + Capture<Set<ServiceComponentHostRequest>> requestsCapture = EasyMock.newCapture(); + + controller.createHostComponents(capture(requestsCapture)); + expectLastCall().once(); + + replayAll(); + + // test + Map<String, Collection<String>> componentsMap = new HashMap<>(); + Collection<String> components = new ArrayList<>(); + components.add("component1"); + components.add("component2"); + componentsMap.put("service1", components); + components = new ArrayList<>(); + components.add("component3"); + componentsMap.put("service2", components); + + context.createAmbariHostResources(CLUSTER_ID, "host1", componentsMap); + + assertEquals(requestsCapture.getValue().size(), 2); + } + + @Test public void testRegisterHostWithConfigGroup_createNewConfigGroup() throws Exception { // test specific expectations expect(cluster.getConfigGroups()).andReturn(Collections.<Long, ConfigGroup>emptyMap()).once();