You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ds...@apache.org on 2017/11/03 12:37:54 UTC

[1/2] ambari git commit: AMBARI-22309 Update db schema to use service_id instead of service_name (configs part) (dsen)

Repository: ambari
Updated Branches:
  refs/heads/branch-feature-AMBARI-14714 72bac1f7f -> 527c4a2d7


http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index 881f172..755a80f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -289,7 +289,7 @@ public class ClusterImpl implements Cluster {
   @Inject
   private StackDAO stackDAO;
 
-  private volatile Multimap<String, String> serviceConfigTypes;
+  private volatile Multimap<Long, String> serviceConfigTypes;
 
   /**
    * Used to publish events relating to cluster CRUD operations and to receive
@@ -348,9 +348,8 @@ public class ClusterImpl implements Cluster {
 
     loadRequestExecutions();
 
-    if (desiredStackVersion != null && !StringUtils.isEmpty(desiredStackVersion.getStackName()) && !
-      StringUtils.isEmpty(desiredStackVersion.getStackVersion())) {
-      loadServiceConfigTypes();
+    if (servicesById != null && !servicesById.isEmpty()) {
+      loadServicesConfigTypes(servicesById.values());
     }
 
     // register to receive stuff
@@ -358,41 +357,42 @@ public class ClusterImpl implements Cluster {
     this.eventPublisher = eventPublisher;
   }
 
-  private void loadServiceConfigTypes() throws AmbariException {
+  private void loadServicesConfigTypes(Collection<Service> services) throws AmbariException {
+    for (Service service : services) {
+      loadServiceConfigTypes(service);
+    }
+  }
+
+  private void loadServiceConfigTypes(Service service) throws AmbariException {
+    if (serviceConfigTypes == null) {
+      serviceConfigTypes = HashMultimap.create();
+    } else {
+      serviceConfigTypes.removeAll(service.getServiceId());
+    }
     try {
-      serviceConfigTypes = collectServiceConfigTypesMapping();
+      serviceConfigTypes.putAll(service.getServiceId(), collectServiceConfigTypes(service));
     } catch (AmbariException e) {
-      LOG.error("Cannot load stack info:", e);
+      LOG.error("Cannot load service info:", e);
       throw e;
     }
-    LOG.info("Service config types loaded: {}", serviceConfigTypes);
+    LOG.info("Updated service config types: {}", serviceConfigTypes);
   }
 
   /**
-   * Construct config type to service name mapping
+   * Construct config types for concrete service
    *
-   * @throws AmbariException when stack or its part not found
+   * @throws AmbariException when stack or service not found
    */
-  private Multimap<String, String> collectServiceConfigTypesMapping() throws AmbariException {
-    Multimap<String, String> serviceConfigTypes = HashMultimap.create();
-
-    Map<String, ServiceInfo> serviceInfoMap = null;
+  private Collection<String> collectServiceConfigTypes(Service service) throws AmbariException {
+    ServiceInfo serviceInfo;
     try {
-      serviceInfoMap = ambariMetaInfo.getServices(desiredStackVersion.getStackName(), desiredStackVersion.getStackVersion());
+      serviceInfo = ambariMetaInfo.getService(service);
     } catch (ParentObjectNotFoundException e) {
       LOG.error("Service config versioning disabled due to exception: ", e);
-      return serviceConfigTypes;
-    }
-    for (Entry<String, ServiceInfo> entry : serviceInfoMap.entrySet()) {
-      String serviceName = entry.getKey();
-      ServiceInfo serviceInfo = entry.getValue();
-      Set<String> configTypes = serviceInfo.getConfigTypeAttributes().keySet();
-      for (String configType : configTypes) {
-        serviceConfigTypes.put(serviceName, configType);
-      }
+      throw e;
     }
 
-    return serviceConfigTypes;
+    return serviceInfo.getConfigTypeAttributes().keySet();
   }
 
   /**
@@ -448,9 +448,10 @@ public class ClusterImpl implements Cluster {
       try {
         if (ambariMetaInfo.getService(stackId.getStackName(),
           stackId.getStackVersion(), serviceEntity.getServiceType()) != null) {
-          services.put(serviceEntity.getServiceName(),
-            serviceFactory.createExisting(this, getServiceGroup(serviceEntity.getServiceGroupId()), serviceEntity));
-           stackId = getService(serviceEntity.getServiceName()).getDesiredStackId();
+          Service service = serviceFactory.createExisting(this, getServiceGroup(serviceEntity.getServiceGroupId()), serviceEntity);
+          services.put(serviceEntity.getServiceName(), service);
+          stackId = getService(serviceEntity.getServiceName()).getDesiredStackId();
+          servicesById.put(serviceEntity.getServiceId(), service);
         }
 
       } catch (AmbariException e) {
@@ -874,7 +875,15 @@ public class ClusterImpl implements Cluster {
       LOG.debug("Adding a new Service, clusterName={}, clusterId={}, serviceName={} serviceType={}",
                  getClusterName(), getClusterId(), service.getName(), service.getServiceType());
     }
+    //TODO get rid of services map
     services.put(service.getName(), service);
+
+    servicesById.put(service.getServiceId(), service);
+    try {
+      loadServiceConfigTypes(service);
+    } catch (AmbariException e) {
+      e.printStackTrace();
+    }
   }
 
   /**
@@ -1047,15 +1056,53 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
+  public Service getService(String serviceGroupName, String serviceName) throws AmbariException {
+    Service service = null;
+    //TODO use serviceIds instead of service name (remove this if)
+    if (serviceGroupName == null) {
+      service = getService(serviceName);
+      if (null == service) {
+        throw new ServiceNotFoundException(getClusterName(), serviceName);
+      }
+    } else {
+      for (Service serviceCandidate : servicesById.values()) {
+        if (serviceCandidate.getClusterId().equals(this.getClusterId())
+            && StringUtils.equals(serviceCandidate.getServiceGroupName(), serviceGroupName)
+            && StringUtils.equals(serviceCandidate.getName(), serviceName)) {
+          if (service == null) {
+            service = serviceCandidate;
+          } else {
+            LOG.error("Two services entities found for same serviceGroup and serviceName : service1 {%s}, service2 {%s}", service, serviceCandidate);
+            throw new AmbariException(String.format("Two services entities found for same serviceGroup and serviceName : service1 {%s}, service2 {%s}", service, serviceCandidate));
+          }
+        }
+      }
+    }
+
+    if (null == service) {
+      throw new ServiceNotFoundException(getClusterName(), serviceName);
+    }
+
+    return service;
+  }
+
+  @Override
   public Service getService(Long serviceId) throws AmbariException {
     Service service = servicesById.get(serviceId);
     if (null == service) {
       throw new ServiceNotFoundException(getClusterName(), serviceId);
     }
-
     return service;
   }
 
+  private Service getServiceOrNull(Long serviceId) {
+    try {
+      return getService(serviceId);
+    } catch (AmbariException e) {
+      return null;
+    }
+  }
+
   @Override
   public Map<String, Service> getServices() {
     return new HashMap<>(services);
@@ -1126,8 +1173,6 @@ public class ClusterImpl implements Cluster {
 
       clusterEntity.setDesiredStack(stackEntity);
       clusterEntity = clusterDAO.merge(clusterEntity);
-
-      loadServiceConfigTypes();
     } finally {
       clusterGlobalLock.writeLock().unlock();
     }
@@ -1569,6 +1614,9 @@ public class ClusterImpl implements Cluster {
     service.delete();
 
     serviceComponentHosts.remove(serviceName);
+    services.remove(serviceName);
+    servicesById.remove(service.getServiceId());
+    serviceConfigTypes.removeAll(service.getServiceId());
 
     for (List<ServiceComponentHost> serviceComponents : serviceComponentHostsByHost.values()) {
       Iterables.removeIf(serviceComponents, new Predicate<ServiceComponentHost>() {
@@ -1639,12 +1687,12 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
-  public ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs) {
+  public ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs) throws AmbariException {
     return addDesiredConfig(user, configs, null);
   }
 
   @Override
-  public ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs, String serviceConfigVersionNote) {
+  public ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs, String serviceConfigVersionNote) throws AmbariException {
     if (null == user) {
       throw new NullPointerException("User must be specified.");
     }
@@ -1721,7 +1769,8 @@ public class ClusterImpl implements Cluster {
       for (ClusterConfigEntity configEntity : entities) {
         if (allVersions || configEntity.isSelected()) {
           DesiredConfig desiredConfig = new DesiredConfig();
-          desiredConfig.setServiceName(null);
+          desiredConfig.setServiceId(null);
+          desiredConfig.setServiceGroupId(null);
           desiredConfig.setTag(configEntity.getTag());
 
           if (!allConfigs.containsKey(configEntity.getType())) {
@@ -1787,7 +1836,7 @@ public class ClusterImpl implements Cluster {
 
   @Override
   public ServiceConfigVersionResponse createServiceConfigVersion(
-    String serviceName, String user, String note, ConfigGroup configGroup) {
+    Long serviceId, String user, String note, ConfigGroup configGroup) throws AmbariException {
 
     // Create next service config version
     ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
@@ -1808,24 +1857,23 @@ public class ClusterImpl implements Cluster {
 
         serviceConfigEntity.setClusterConfigEntities(configEntities);
       } else {
-        List<ClusterConfigEntity> configEntities = getClusterConfigEntitiesByService(serviceName);
+        List<ClusterConfigEntity> configEntities = getClusterConfigEntitiesByService(serviceId);
         serviceConfigEntity.setClusterConfigEntities(configEntities);
       }
 
 
       long nextServiceConfigVersion = serviceConfigDAO.findNextServiceConfigVersion(clusterId,
-        serviceName);
+        serviceId);
 
-      // get the correct stack ID to use when creating the service config
-      StackEntity stackEntity = clusterEntity.getDesiredStack();
-      Service service = services.get(serviceName);
-      if (null != service) {
-        StackId serviceStackId = service.getDesiredStackId();
-        stackEntity = stackDAO.find(serviceStackId);
-      }
+      Service service = getService(serviceId);
+      StackId serviceStackId = service.getDesiredStackId();
+      StackEntity stackEntity = stackDAO.find(serviceStackId);
+      ClusterServiceEntity clusterServiceEntity = clusterServiceDAO.findById(clusterId, service.getServiceGroupId(), service.getServiceId());
 
-      serviceConfigEntity.setServiceName(serviceName);
+      serviceConfigEntity.setServiceId(serviceId);
       serviceConfigEntity.setClusterEntity(clusterEntity);
+      serviceConfigEntity.setServiceGroupId(service.getServiceGroupId());
+      serviceConfigEntity.setClusterServiceEntity(clusterServiceEntity);
       serviceConfigEntity.setVersion(nextServiceConfigVersion);
       serviceConfigEntity.setUser(user);
       serviceConfigEntity.setNote(note);
@@ -1843,7 +1891,7 @@ public class ClusterImpl implements Cluster {
     String configGroupName = configGroup == null ? ServiceConfigVersionResponse.DEFAULT_CONFIG_GROUP_NAME : configGroup.getName();
     configChangeLog.info("(configchange) Creating config version. cluster: '{}', changed by: '{}', " +
         "service_name: '{}', config_group: '{}', config_group_id: '{}', version: '{}', create_timestamp: '{}', note: '{}'",
-      getClusterName(), user, serviceName, configGroupName,
+      getClusterName(), user, getService(serviceId).getName(), configGroupName,
       configGroup == null ? "null" : configGroup.getId(), serviceConfigEntity.getVersion(), serviceConfigEntity.getCreateTimestamp(),
       serviceConfigEntity.getNote());
 
@@ -1854,42 +1902,47 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
-  public String getServiceForConfigTypes(Collection<String> configTypes) {
+  public Long getServiceForConfigTypes(Collection<String> configTypes) {
     //debug
     LOG.info("Looking for service for config types {}", configTypes);
-    String serviceName = null;
+    Long serviceId = null;
     for (String configType : configTypes) {
-      for (Entry<String, String> entry : serviceConfigTypes.entries()) {
+      for (Entry<Long, String> entry : serviceConfigTypes.entries()) {
         if (StringUtils.equals(entry.getValue(), configType)) {
-          if (serviceName != null) {
-            if (entry.getKey() != null && !StringUtils.equals(serviceName, entry.getKey())) {
+          if (serviceId != null) {
+            if (entry.getKey() != null && !serviceId.equals(entry.getKey())) {
               throw new IllegalArgumentException(String.format("Config type %s belongs to %s service, " +
-                "but also qualified for %s", configType, serviceName, entry.getKey()));
+                "but also qualified for %s", configType, getServiceOrNull(serviceId), getServiceOrNull(entry.getKey())));
             }
           } else {
-            serviceName = entry.getKey();
+            serviceId = entry.getKey();
           }
         }
       }
     }
-    LOG.info("Service {} returning", serviceName);
-    return serviceName;
+    if (serviceId == null) {
+      LOG.warn("Can't find serviceId for {}, there is a problem if there's no cluster-env", configTypes);
+    } else {
+      LOG.info("Service {} returning", getServiceOrNull(serviceId));
+    }
+
+    return serviceId;
   }
 
   @Override
-  public String getServiceByConfigType(String configType) {
-    for (Entry<String, String> entry : serviceConfigTypes.entries()) {
-      String serviceName = entry.getKey();
+  public Service getServiceByConfigType(String configType) {
+    for (Entry<Long, String> entry : serviceConfigTypes.entries()) {
+      Long serviceId = entry.getKey();
       String type = entry.getValue();
       if (StringUtils.equals(type, configType)) {
-        return serviceName;
+        return getServiceOrNull(serviceId);
       }
     }
     return null;
   }
 
   @Override
-  public ServiceConfigVersionResponse setServiceConfigVersion(String serviceName, Long version, String user, String note) throws AmbariException {
+  public ServiceConfigVersionResponse setServiceConfigVersion(Long serviceId, Long version, String user, String note) throws AmbariException {
     if (null == user) {
       throw new NullPointerException("User must be specified.");
     }
@@ -1897,7 +1950,7 @@ public class ClusterImpl implements Cluster {
     clusterGlobalLock.writeLock().lock();
     try {
       ServiceConfigVersionResponse serviceConfigVersionResponse = applyServiceConfigVersion(
-        serviceName, version, user, note);
+          serviceId, version, user, note);
       return serviceConfigVersionResponse;
     } finally {
       clusterGlobalLock.writeLock().unlock();
@@ -2008,12 +2061,12 @@ public class ClusterImpl implements Cluster {
   }
 
   @Override
-  public List<ServiceConfigVersionResponse> getActiveServiceConfigVersionResponse(String serviceName) {
+  public List<ServiceConfigVersionResponse> getActiveServiceConfigVersionResponse(Long serviceId) {
     clusterGlobalLock.readLock().lock();
     try {
       List<ServiceConfigEntity> activeServiceConfigVersionEntities = new ArrayList<>();
       List<ServiceConfigVersionResponse> activeServiceConfigVersionResponses = new ArrayList<>();
-      activeServiceConfigVersionEntities.addAll(serviceConfigDAO.getLastServiceConfigsForService(getClusterId(), serviceName));
+      activeServiceConfigVersionEntities.addAll(serviceConfigDAO.getLastServiceConfigsForService(getClusterId(), serviceId));
       for (ServiceConfigEntity serviceConfigEntity : activeServiceConfigVersionEntities) {
         ServiceConfigVersionResponse serviceConfigVersionResponse = getServiceConfigVersionResponseWithConfig(convertToServiceConfigVersionResponse(serviceConfigEntity), serviceConfigEntity);
         serviceConfigVersionResponse.setIsCurrent(true);
@@ -2044,17 +2097,6 @@ public class ClusterImpl implements Cluster {
     return serviceConfigVersionResponse;
   }
 
-
-  @RequiresSession
-  ServiceConfigVersionResponse getActiveServiceConfigVersion(String serviceName) {
-    ServiceConfigEntity lastServiceConfig = serviceConfigDAO.getLastServiceConfig(getClusterId(), serviceName);
-    if (lastServiceConfig == null) {
-      LOG.debug("No service config version found for service {}", serviceName);
-      return null;
-    }
-    return convertToServiceConfigVersionResponse(lastServiceConfig);
-  }
-
   @RequiresSession
   ServiceConfigVersionResponse convertToServiceConfigVersionResponse(ServiceConfigEntity serviceConfigEntity) {
     Long groupId = serviceConfigEntity.getGroupId();
@@ -2082,11 +2124,11 @@ public class ClusterImpl implements Cluster {
   }
 
   @Transactional
-  ServiceConfigVersionResponse applyServiceConfigVersion(String serviceName, Long serviceConfigVersion, String user,
+  ServiceConfigVersionResponse applyServiceConfigVersion(Long serviceId, Long serviceConfigVersion, String user,
                                                          String serviceConfigVersionNote) throws AmbariException {
-    ServiceConfigEntity serviceConfigEntity = serviceConfigDAO.findByServiceAndVersion(serviceName, serviceConfigVersion);
+    ServiceConfigEntity serviceConfigEntity = serviceConfigDAO.findByServiceAndVersion(serviceId, serviceConfigVersion);
     if (serviceConfigEntity == null) {
-      throw new ObjectNotFoundException("Service config version with serviceName={} and version={} not found");
+      throw new ObjectNotFoundException("Service config version with serviceId={} and version={} not found");
     }
 
     // disable all configs related to service
@@ -2096,7 +2138,7 @@ public class ClusterImpl implements Cluster {
       // In that case eclipselink will revert changes to cached, if entity has fluchGroup and it
       // needs to be refreshed. Actually we don't need to change same antities in few steps, so i
       // decided to filter out. duplicates and do not change them. It will be better for performance and bug will be fixed.
-      Collection<String> configTypes = serviceConfigTypes.get(serviceName);
+      Collection<String> configTypes = serviceConfigTypes.get(serviceId);
       List<ClusterConfigEntity> enabledConfigs = clusterDAO.getEnabledConfigsByTypes(clusterId, configTypes);
       List<ClusterConfigEntity> serviceConfigEntities = serviceConfigEntity.getClusterConfigEntities();
       ArrayList<ClusterConfigEntity> duplicatevalues = new ArrayList<>(serviceConfigEntities);
@@ -2145,12 +2187,14 @@ public class ClusterImpl implements Cluster {
 
     ClusterEntity clusterEntity = getClusterEntity();
     long nextServiceConfigVersion = serviceConfigDAO.findNextServiceConfigVersion(
-      clusterEntity.getClusterId(), serviceName);
+      clusterEntity.getClusterId(), serviceId);
 
     ServiceConfigEntity serviceConfigEntityClone = new ServiceConfigEntity();
     serviceConfigEntityClone.setCreateTimestamp(System.currentTimeMillis());
     serviceConfigEntityClone.setUser(user);
-    serviceConfigEntityClone.setServiceName(serviceName);
+    serviceConfigEntityClone.setServiceId(serviceId);
+    serviceConfigEntityClone.setServiceGroupId(serviceConfigEntity.getServiceGroupId());
+    serviceConfigEntityClone.setClusterServiceEntity(serviceConfigEntity.getClusterServiceEntity());
     serviceConfigEntityClone.setClusterEntity(clusterEntity);
     serviceConfigEntityClone.setStack(serviceConfigEntity.getStack());
     serviceConfigEntityClone.setClusterConfigEntities(serviceConfigEntity.getClusterConfigEntities());
@@ -2166,21 +2210,21 @@ public class ClusterImpl implements Cluster {
   }
 
   @Transactional
-  ServiceConfigVersionResponse applyConfigs(Set<Config> configs, String user, String serviceConfigVersionNote) {
+  ServiceConfigVersionResponse applyConfigs(Set<Config> configs, String user, String serviceConfigVersionNote) throws AmbariException {
 
-    String serviceName = null;
+    Long serviceId = null;
     for (Config config : configs) {
-      for (Entry<String, String> entry : serviceConfigTypes.entries()) {
+      for (Entry<Long, String> entry : serviceConfigTypes.entries()) {
         if (StringUtils.equals(entry.getValue(), config.getType())) {
-          if (serviceName == null) {
-            serviceName = entry.getKey();
+          if (serviceId == null) {
+            serviceId = entry.getKey();
             break;
-          } else if (!serviceName.equals(entry.getKey())) {
+          } else if (!serviceId.equals(entry.getKey())) {
             String error = String.format("Updating configs for multiple services by a " +
                 "single API request isn't supported. Conflicting services %s and %s for %s",
-              serviceName, entry.getKey(), config.getType());
+                getServiceOrNull(serviceId).getName(), getServiceOrNull(entry.getKey()).getName(), config.getType());
             IllegalArgumentException exception = new IllegalArgumentException(error);
-            LOG.error(error + ", config version not created for {}", serviceName);
+            LOG.error(error + ", config version not created for {}", getServiceOrNull(serviceId).getName());
             throw exception;
           } else {
             break;
@@ -2208,7 +2252,7 @@ public class ClusterImpl implements Cluster {
 
     clusterEntity = clusterDAO.merge(clusterEntity);
 
-    if (serviceName == null) {
+    if (serviceId == null) {
       ArrayList<String> configTypes = new ArrayList<>();
       for (Config config : configs) {
         configTypes.add(config.getType());
@@ -2216,19 +2260,19 @@ public class ClusterImpl implements Cluster {
       LOG.error("No service found for config types '{}', service config version not created", configTypes);
       return null;
     } else {
-      return createServiceConfigVersion(serviceName, user, serviceConfigVersionNote);
+      return createServiceConfigVersion(serviceId, user, serviceConfigVersionNote);
     }
 
   }
 
-  private ServiceConfigVersionResponse createServiceConfigVersion(String serviceName, String user,
-                                                                  String serviceConfigVersionNote) {
+  private ServiceConfigVersionResponse createServiceConfigVersion(Long serviceId, String user,
+                                                                  String serviceConfigVersionNote) throws AmbariException {
     //create next service config version
-    return createServiceConfigVersion(serviceName, user, serviceConfigVersionNote, null);
+    return createServiceConfigVersion(serviceId, user, serviceConfigVersionNote, null);
   }
 
-  private List<ClusterConfigEntity> getClusterConfigEntitiesByService(String serviceName) {
-    Collection<String> configTypes = serviceConfigTypes.get(serviceName);
+  private List<ClusterConfigEntity> getClusterConfigEntitiesByService(Long serviceId) {
+    Collection<String> configTypes = serviceConfigTypes.get(serviceId);
     return clusterDAO.getEnabledConfigsByTypes(getClusterId(), new ArrayList<>(configTypes));
   }
 
@@ -2267,7 +2311,8 @@ public class ClusterImpl implements Cluster {
     for (HostConfigMapping mappingEntity : mappingEntities) {
       DesiredConfig desiredConfig = new DesiredConfig();
       desiredConfig.setTag(mappingEntity.getVersion());
-      desiredConfig.setServiceName(mappingEntity.getServiceName());
+      desiredConfig.setServiceId(mappingEntity.getServiceId());
+      desiredConfig.setServiceGroupId(mappingEntity.getServiceGroupId());
 
       desiredConfigsByHost.get(mappingEntity.getHostId()).put(mappingEntity.getType(), desiredConfig);
     }
@@ -2642,7 +2687,7 @@ public class ClusterImpl implements Cluster {
    */
   @Override
   @Transactional
-  public void applyLatestConfigurations(StackId stackId, String serviceName) {
+  public void applyLatestConfigurations(StackId stackId, Long serviceId) {
     clusterGlobalLock.writeLock().lock();
 
     try {
@@ -2655,7 +2700,7 @@ public class ClusterImpl implements Cluster {
       // find the latest configurations for the service
       Set<String> configTypesForService = new HashSet<>();
       List<ServiceConfigEntity> latestServiceConfigs = serviceConfigDAO.getLastServiceConfigsForService(
-        getClusterId(), serviceName);
+        getClusterId(), serviceId);
 
       // process the current service configurations
       for (ServiceConfigEntity serviceConfig : latestServiceConfigs) {
@@ -2704,7 +2749,7 @@ public class ClusterImpl implements Cluster {
 
       LOG.info(
         "Applied latest configurations for {} on stack {}. The the following types were modified: {}",
-        serviceName, stackId, StringUtils.join(configTypesForService, ','));
+          getServiceOrNull(serviceId), stackId, StringUtils.join(configTypesForService, ','));
 
     } finally {
       clusterGlobalLock.writeLock().unlock();
@@ -2739,12 +2784,12 @@ public class ClusterImpl implements Cluster {
    *
    * @param stackId
    *          the stack to remove configurations for (not {@code null}).
-   * @param serviceName
-   *          the service name (not {@code null}).
+   * @param serviceId
+   *          the service ID (not {@code null}).
    * @see #clusterGlobalLock
    */
   @Transactional
-  void removeAllConfigsForStack(StackId stackId, String serviceName) {
+  void removeAllConfigsForStack(StackId stackId, Long serviceId) {
     ClusterEntity clusterEntity = getClusterEntity();
 
     // make sure the entity isn't stale in the current unit of work.
@@ -2764,7 +2809,7 @@ public class ClusterImpl implements Cluster {
 
     // get the service configs only for the service
     List<ServiceConfigEntity> serviceConfigs = serviceConfigDAO.getServiceConfigsForServiceAndStack(
-      clusterId, stackId, serviceName);
+      clusterId, stackId, serviceId);
 
     // remove all service configurations and associated configs
     for (ServiceConfigEntity serviceConfig : serviceConfigs) {
@@ -2784,7 +2829,7 @@ public class ClusterImpl implements Cluster {
     clusterEntity.setClusterConfigEntities(allClusterConfigEntities);
     clusterEntity = clusterDAO.merge(clusterEntity);
 
-    LOG.info("Removed the following configuration types for {} on stack {}: {}", serviceName,
+    LOG.info("Removed the following configuration types for {} on stack {}: {}", getServiceOrNull(serviceId).getName(),
       stackId, StringUtils.join(removedConfigurationTypes, ','));
   }
 
@@ -2792,10 +2837,10 @@ public class ClusterImpl implements Cluster {
    * {@inheritDoc}
    */
   @Override
-  public void removeConfigurations(StackId stackId, String serviceName) {
+  public void removeConfigurations(StackId stackId, Long serviceId) {
     clusterGlobalLock.writeLock().lock();
     try {
-      removeAllConfigsForStack(stackId, serviceName);
+      removeAllConfigsForStack(stackId, serviceId);
       cacheConfigurations();
     } finally {
       clusterGlobalLock.writeLock().unlock();
@@ -2830,17 +2875,6 @@ public class ClusterImpl implements Cluster {
 
   private void loadStackVersion() {
     desiredStackVersion = new StackId(getClusterEntity().getDesiredStack());
-
-    if (!StringUtils.isEmpty(desiredStackVersion.getStackName())
-      && !StringUtils.isEmpty(desiredStackVersion.getStackVersion())) {
-      try {
-        loadServiceConfigTypes();
-      } catch (AmbariException e) {
-        // TODO recheck wrapping exception here, required for lazy loading after
-        // invalidation
-        throw new RuntimeException(e);
-      }
-    }
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroup.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroup.java b/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroup.java
index 0393145..324f43c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroup.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroup.java
@@ -134,7 +134,14 @@ public interface ConfigGroup {
   /**
    * Name of service which config group is wired to
    */
-  String getServiceName();
+  Long getServiceId();
 
-  void setServiceName(String serviceName);
+  void setServiceId(Long serviceId);
+
+  /**
+   * Id of service group which config group is wired to
+   */
+  Long getServiceGroupId();
+
+  void setServiceGroupId(Long serviceGroupId);
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupFactory.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupFactory.java
index f6fecce..06f9e6d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupFactory.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupFactory.java
@@ -33,7 +33,8 @@ public interface ConfigGroupFactory {
    * Creates and saves a new {@link ConfigGroup}.
    */
   ConfigGroup createNew(@Assisted("cluster") Cluster cluster,
-      @Assisted("serviceName") @Nullable String serviceName, @Assisted("name") String name,
+      @Assisted("serviceGroupId") @Nullable Long serviceGroupId,
+      @Assisted("serviceId") @Nullable Long serviceId, @Assisted("name") String name,
       @Assisted("tag") String tag, @Assisted("description") String description,
       @Assisted("configs") Map<String, Config> configs, @Assisted("hosts") Map<Long, Host> hosts);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java
index d1fbf1d..33902af 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/configgroup/ConfigGroupImpl.java
@@ -36,12 +36,14 @@ import org.apache.ambari.server.controller.ConfigGroupResponse;
 import org.apache.ambari.server.controller.internal.ConfigurationResourceProvider;
 import org.apache.ambari.server.logging.LockFactory;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
+import org.apache.ambari.server.orm.dao.ClusterServiceDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupDAO;
 import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupEntity;
 import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity;
@@ -67,7 +69,8 @@ public class ConfigGroupImpl implements ConfigGroup {
   private ConcurrentMap<Long, Host> m_hosts;
   private ConcurrentMap<String, Config> m_configurations;
   private String configGroupName;
-  private String serviceName;
+  private Long serviceId;
+  private Long serviceGroupId;
   private long configGroupId;
 
   /**
@@ -91,17 +94,20 @@ public class ConfigGroupImpl implements ConfigGroup {
 
   private final ClusterDAO clusterDAO;
 
+  private final ClusterServiceDAO clusterServiceDAO;
+
   private final ConfigFactory configFactory;
 
   @AssistedInject
   public ConfigGroupImpl(@Assisted("cluster") Cluster cluster,
-      @Assisted("serviceName") @Nullable String serviceName, @Assisted("name") String name,
-      @Assisted("tag") String tag, @Assisted("description") String description,
-      @Assisted("configs") Map<String, Config> configurations,
-      @Assisted("hosts") Map<Long, Host> hosts, Clusters clusters, ConfigFactory configFactory,
-      ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
-      ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
-      ConfigGroupHostMappingDAO configGroupHostMappingDAO, LockFactory lockFactory)
+                         @Assisted("serviceGroupId") @Nullable Long serviceGroupId,
+                         @Assisted("serviceId") @Nullable Long serviceId, @Assisted("name") String name,
+                         @Assisted("tag") String tag, @Assisted("description") String description,
+                         @Assisted("configs") Map<String, Config> configurations,
+                         @Assisted("hosts") Map<Long, Host> hosts, Clusters clusters, ConfigFactory configFactory,
+                         ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
+                         ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
+                         ConfigGroupHostMappingDAO configGroupHostMappingDAO, ClusterServiceDAO clusterServiceDAO, LockFactory lockFactory)
       throws AmbariException {
 
     this.configFactory = configFactory;
@@ -110,11 +116,13 @@ public class ConfigGroupImpl implements ConfigGroup {
     this.configGroupDAO = configGroupDAO;
     this.configGroupConfigMappingDAO = configGroupConfigMappingDAO;
     this.configGroupHostMappingDAO = configGroupHostMappingDAO;
+    this.clusterServiceDAO = clusterServiceDAO;
 
     hostLock = lockFactory.newReadWriteLock(hostLockLabel);
 
     this.cluster = cluster;
-    this.serviceName = serviceName;
+    this.serviceGroupId = serviceGroupId;
+    this.serviceId = serviceId;
     configGroupName = name;
 
     ConfigGroupEntity configGroupEntity = new ConfigGroupEntity();
@@ -122,7 +130,10 @@ public class ConfigGroupImpl implements ConfigGroup {
     configGroupEntity.setGroupName(name);
     configGroupEntity.setTag(tag);
     configGroupEntity.setDescription(description);
-    configGroupEntity.setServiceName(serviceName);
+    configGroupEntity.setServiceId(serviceId);
+    configGroupEntity.setServiceGroupId(serviceGroupId);
+    ClusterServiceEntity clusterServiceEntity = clusterServiceDAO.findById(cluster.getClusterId(), serviceGroupId, serviceId);
+    configGroupEntity.setClusterServiceEntity(clusterServiceEntity);
 
     m_hosts = hosts == null ? new ConcurrentHashMap<>()
         : new ConcurrentHashMap<>(hosts);
@@ -140,7 +151,7 @@ public class ConfigGroupImpl implements ConfigGroup {
       Clusters clusters, ConfigFactory configFactory,
       ClusterDAO clusterDAO, HostDAO hostDAO, ConfigGroupDAO configGroupDAO,
       ConfigGroupConfigMappingDAO configGroupConfigMappingDAO,
-      ConfigGroupHostMappingDAO configGroupHostMappingDAO, LockFactory lockFactory) {
+      ConfigGroupHostMappingDAO configGroupHostMappingDAO, ClusterServiceDAO clusterServiceDAO, LockFactory lockFactory) {
 
     this.configFactory = configFactory;
     this.clusterDAO = clusterDAO;
@@ -148,13 +159,15 @@ public class ConfigGroupImpl implements ConfigGroup {
     this.configGroupDAO = configGroupDAO;
     this.configGroupConfigMappingDAO = configGroupConfigMappingDAO;
     this.configGroupHostMappingDAO = configGroupHostMappingDAO;
+    this.clusterServiceDAO = clusterServiceDAO;
 
     hostLock = lockFactory.newReadWriteLock(hostLockLabel);
 
     this.cluster = cluster;
     configGroupId = configGroupEntity.getGroupId();
     configGroupName = configGroupEntity.getGroupName();
-    serviceName = configGroupEntity.getServiceName();
+    serviceId = configGroupEntity.getServiceId();
+    serviceGroupId = configGroupEntity.getServiceGroupId();
 
     m_configurations = new ConcurrentHashMap<>();
     m_hosts = new ConcurrentHashMap<>();
@@ -347,6 +360,10 @@ public class ConfigGroupImpl implements ConfigGroup {
     ClusterEntity clusterEntity = clusterDAO.findById(cluster.getClusterId());
     configGroupEntity.setClusterEntity(clusterEntity);
     configGroupEntity.setTimestamp(System.currentTimeMillis());
+    configGroupEntity.setServiceGroupId(serviceGroupId);
+    configGroupEntity.setServiceId(serviceId);
+    ClusterServiceEntity clusterServiceEntity = clusterServiceDAO.findById(cluster.getClusterId(), serviceGroupId, serviceId);
+    configGroupEntity.setClusterServiceEntity(clusterServiceEntity);
     configGroupDAO.create(configGroupEntity);
 
     configGroupId = configGroupEntity.getGroupId();
@@ -416,8 +433,7 @@ public class ConfigGroupImpl implements ConfigGroup {
           (cluster.getClusterId(), config.getType(), config.getTag());
 
         if (clusterConfigEntity == null) {
-          String serviceName = getServiceName();
-          Service service = cluster.getService(serviceName);
+          Service service = cluster.getService(serviceId);
 
           config = configFactory.createNew(service.getDesiredStackId(), cluster, config.getType(),
               config.getTag(), config.getProperties(), config.getPropertiesAttributes());
@@ -507,17 +523,30 @@ public class ConfigGroupImpl implements ConfigGroup {
   }
 
   @Override
-  public String getServiceName() {
-    return serviceName;
+  public Long getServiceId() {
+    return serviceId;
+  }
+
+  @Override
+  public void setServiceId(Long serviceId) {
+    ConfigGroupEntity configGroupEntity = getConfigGroupEntity();
+    configGroupEntity.setServiceId(serviceId);
+    configGroupDAO.merge(configGroupEntity);
+
+    this.serviceId = serviceId;
+  }
+  @Override
+  public Long getServiceGroupId() {
+    return serviceGroupId;
   }
 
   @Override
-  public void setServiceName(String serviceName) {
+  public void setServiceGroupId(Long serviceGroupId) {
     ConfigGroupEntity configGroupEntity = getConfigGroupEntity();
-    configGroupEntity.setServiceName(serviceName);
+    configGroupEntity.setServiceGroupId(serviceGroupId);
     configGroupDAO.merge(configGroupEntity);
 
-    this.serviceName = serviceName;
+    this.serviceId = serviceGroupId;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
index 4285ba9..7ec3112 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
@@ -1021,7 +1021,8 @@ public class HostImpl implements Host {
 
       DesiredConfig dc = new DesiredConfig();
       dc.setTag(e.getVersion());
-      dc.setServiceName(e.getServiceName());
+      dc.setServiceGroupId(e.getServiceGroupId());
+      dc.setServiceId(e.getServiceId());
       map.put(e.getType(), dc);
 
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/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 e5eb11c..8a32265 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
@@ -712,7 +712,7 @@ public class AmbariContext {
       });
 
       ConfigGroupRequest request = new ConfigGroupRequest(null, clusterName,
-        absoluteGroupName, service.getName(), service.getName(), "Host Group Configuration",
+        absoluteGroupName, service.getName(), service.getServiceGroupName(), service.getName(), "Host Group Configuration",
         Sets.newHashSet(filteredGroupHosts), serviceConfigs);
 
       // get the config group provider and create config group resource

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/topology/Service.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/topology/Service.java b/ambari-server/src/main/java/org/apache/ambari/server/topology/Service.java
index 46c75c5..317e29f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/topology/Service.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/topology/Service.java
@@ -149,4 +149,11 @@ public class Service implements Configurable {
       ", stackId='" + stackId + '\'' +
       '}';
   }
+
+  public String getServiceGroupName() {
+    if (serviceGroup != null) {
+      return serviceGroup.getName();
+    }
+    return null;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
index a2e2f6f..5a2b92a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/AbstractUpgradeCatalog.java
@@ -555,7 +555,7 @@ public abstract class AbstractUpgradeCatalog implements UpgradeCatalog {
         }
 
         Multimap<ConfigUpdateType, Entry<String, String>> propertiesToLog = ArrayListMultimap.create();
-        String serviceName = cluster.getServiceByConfigType(configType);
+        String serviceName = cluster.getServiceByConfigType(configType).getName();
 
         Map<String, String> mergedProperties =
           mergeProperties(oldConfigProperties, properties, updateIfExists, propertiesToLog);

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
index 36ff3b7..e1e7c9e 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Derby-CREATE.sql
@@ -122,20 +122,6 @@ CREATE TABLE ambari_configuration (
   CONSTRAINT FK_ambari_conf_conf_base FOREIGN KEY (id) REFERENCES configuration_base (id)
 );
 
-CREATE TABLE serviceconfig (
-  service_config_id BIGINT NOT NULL,
-  cluster_id BIGINT NOT NULL,
-  service_name VARCHAR(255) NOT NULL,
-  version BIGINT NOT NULL,
-  create_timestamp BIGINT NOT NULL,
-  stack_id BIGINT NOT NULL,
-  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
-  group_id BIGINT,
-  note VARCHAR(3000),
-  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
-  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
-  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
-
 CREATE TABLE hosts (
   host_id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
@@ -156,20 +142,6 @@ CREATE TABLE hosts (
   CONSTRAINT PK_hosts PRIMARY KEY (host_id),
   CONSTRAINT UQ_hosts_host_name UNIQUE (host_name));
 
-CREATE TABLE serviceconfighosts (
-  service_config_id BIGINT NOT NULL,
-  host_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
-  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
-  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
-CREATE TABLE serviceconfigmapping (
-  service_config_id BIGINT NOT NULL,
-  config_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
-  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
-  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
 CREATE TABLE servicegroups (
   id BIGINT NOT NULL,
   service_group_name VARCHAR(255) NOT NULL,
@@ -207,6 +179,36 @@ CREATE TABLE servicedependencies (
   CONSTRAINT FK_servicedependencies_service_group_cluster_id FOREIGN KEY (service_id, service_group_id, service_cluster_id) REFERENCES clusterservices  (id, service_group_id, cluster_id));
   CONSTRAINT FK_servicedependencies_dependent_service_group_cluster_id FOREIGN KEY (dependent_service_id, dependent_service_group_id, dependent_service_cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id));
 
+CREATE TABLE serviceconfig (
+  service_config_id BIGINT NOT NULL,
+  cluster_id BIGINT NOT NULL,
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
+  version BIGINT NOT NULL,
+  create_timestamp BIGINT NOT NULL,
+  stack_id BIGINT NOT NULL,
+  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
+  group_id BIGINT,
+  note VARCHAR(3000),
+  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
+  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
+  CONSTRAINT FK_serviceconfig_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
+  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_id, version));
+
+CREATE TABLE serviceconfighosts (
+  service_config_id BIGINT NOT NULL,
+  host_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
+  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
+  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
+CREATE TABLE serviceconfigmapping (
+  service_config_id BIGINT NOT NULL,
+  config_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
+  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
+  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
 CREATE TABLE clusterstate (
   cluster_id BIGINT NOT NULL,
   current_cluster_state VARCHAR(255) NOT NULL,
@@ -499,12 +501,14 @@ CREATE TABLE hostconfigmapping (
   host_id BIGINT NOT NULL,
   type_name VARCHAR(255) NOT NULL,
   version_tag VARCHAR(255) NOT NULL,
-  service_name VARCHAR(255),
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
   create_timestamp BIGINT NOT NULL,
   selected INTEGER NOT NULL DEFAULT 0,
   user_name VARCHAR(255) NOT NULL DEFAULT '_db',
   CONSTRAINT PK_hostconfigmapping PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp),
   CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id),
+  CONSTRAINT FK_hostconfmapping_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id));
 
 CREATE TABLE metainfo (
@@ -525,8 +529,10 @@ CREATE TABLE configgroup (
   tag VARCHAR(1024) NOT NULL,
   description VARCHAR(1024),
   create_timestamp BIGINT NOT NULL,
-  service_name VARCHAR(255),
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
   CONSTRAINT PK_configgroup PRIMARY KEY (group_id),
+  CONSTRAINT FK_configgroup_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id));
 
 CREATE TABLE confgroupclusterconfigmapping (

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
index 70a6543..b438e69 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-MySQL-CREATE.sql
@@ -141,20 +141,6 @@ CREATE TABLE ambari_configuration (
   CONSTRAINT FK_ambari_conf_conf_base FOREIGN KEY (id) REFERENCES configuration_base (id)
 );
 
-CREATE TABLE serviceconfig (
-  service_config_id BIGINT NOT NULL,
-  cluster_id BIGINT NOT NULL,
-  service_name VARCHAR(255) NOT NULL,
-  version BIGINT NOT NULL,
-  create_timestamp BIGINT NOT NULL,
-  stack_id BIGINT NOT NULL,
-  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
-  group_id BIGINT,
-  note LONGTEXT,
-  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
-  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
-  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
-
 CREATE TABLE hosts (
   host_id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
@@ -175,20 +161,6 @@ CREATE TABLE hosts (
   CONSTRAINT PK_hosts PRIMARY KEY (host_id),
   CONSTRAINT UQ_hosts_host_name UNIQUE (host_name));
 
-CREATE TABLE serviceconfighosts (
-  service_config_id BIGINT NOT NULL,
-  host_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
-  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
-  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
-CREATE TABLE serviceconfigmapping (
-  service_config_id BIGINT NOT NULL,
-  config_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
-  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
-  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
 CREATE TABLE servicegroups (
   id BIGINT NOT NULL,
   service_group_name VARCHAR(255) NOT NULL,
@@ -226,6 +198,36 @@ CREATE TABLE servicedependencies (
   CONSTRAINT FK_servicedependencies_service_group_cluster_id FOREIGN KEY (service_id, service_group_id, service_cluster_id) REFERENCES clusterservices  (id, service_group_id, cluster_id));
   CONSTRAINT FK_servicedependencies_dependent_service_group_cluster_id FOREIGN KEY (dependent_service_id, dependent_service_group_id, dependent_service_cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id));
 
+CREATE TABLE serviceconfig (
+  service_config_id BIGINT NOT NULL,
+  cluster_id BIGINT NOT NULL,
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
+  version BIGINT NOT NULL,
+  create_timestamp BIGINT NOT NULL,
+  stack_id BIGINT NOT NULL,
+  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
+  group_id BIGINT,
+  note LONGTEXT,
+  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
+  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
+  CONSTRAINT FK_serviceconfig_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
+  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_id, version));
+
+CREATE TABLE serviceconfighosts (
+  service_config_id BIGINT NOT NULL,
+  host_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
+  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
+  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
+CREATE TABLE serviceconfigmapping (
+  service_config_id BIGINT NOT NULL,
+  config_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
+  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
+  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
 CREATE TABLE clusterstate (
   cluster_id BIGINT NOT NULL,
   current_cluster_state VARCHAR(255) NOT NULL,
@@ -511,11 +513,13 @@ CREATE TABLE hostconfigmapping (
   cluster_id BIGINT NOT NULL,
   type_name VARCHAR(255) NOT NULL,
   selected INTEGER NOT NULL DEFAULT 0,
-  service_name VARCHAR(255),
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
   version_tag VARCHAR(255) NOT NULL,
   user_name VARCHAR(255) NOT NULL DEFAULT '_db',
   CONSTRAINT PK_hostconfigmapping PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name),
   CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id),
+  CONSTRAINT FK_hostconfmapping_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id));
 
 CREATE TABLE metainfo (
@@ -542,8 +546,10 @@ CREATE TABLE configgroup (
   tag VARCHAR(1024) NOT NULL,
   description VARCHAR(1024),
   create_timestamp BIGINT NOT NULL,
-  service_name VARCHAR(255),
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
   CONSTRAINT PK_configgroup PRIMARY KEY (group_id),
+  CONSTRAINT FK_configgroup_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id));
 
 CREATE TABLE confgroupclusterconfigmapping (

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
index 7c5f0fd..aab7f95 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Oracle-CREATE.sql
@@ -122,21 +122,6 @@ CREATE TABLE ambari_configuration (
   CONSTRAINT FK_ambari_conf_conf_base FOREIGN KEY (id) REFERENCES configuration_base (id)
 );
 
-CREATE TABLE serviceconfig (
-  service_config_id NUMBER(19) NOT NULL,
-  cluster_id NUMBER(19) NOT NULL,
-  service_name VARCHAR(255) NOT NULL,
-  version NUMBER(19) NOT NULL,
-  create_timestamp NUMBER(19) NOT NULL,
-  stack_id NUMBER(19) NOT NULL,
-  user_name VARCHAR(255) DEFAULT '_db' NOT NULL,
-  group_id NUMBER(19),
-  note CLOB,
-  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
-  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
-  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
-
-
 CREATE TABLE hosts (
   host_id NUMBER(19) NOT NULL,
   host_name VARCHAR2(255) NOT NULL,
@@ -157,20 +142,6 @@ CREATE TABLE hosts (
   CONSTRAINT PK_hosts PRIMARY KEY (host_id),
   CONSTRAINT UQ_hosts_host_name UNIQUE (host_name));
 
-CREATE TABLE serviceconfighosts (
-  service_config_id NUMBER(19) NOT NULL,
-  host_id NUMBER(19) NOT NULL,
-  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
-  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
-  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
-CREATE TABLE serviceconfigmapping (
-  service_config_id NUMBER(19) NOT NULL,
-  config_id NUMBER(19) NOT NULL,
-  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
-  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
-  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
 CREATE TABLE servicegroups (
   id NUMBER(19) NOT NULL,
   service_group_name VARCHAR2(255) NOT NULL,
@@ -208,6 +179,36 @@ CREATE TABLE servicedependencies (
   CONSTRAINT FK_servicedependencies_service_group_cluster_id FOREIGN KEY (service_id, service_group_id, service_cluster_id) REFERENCES clusterservices  (id, service_group_id, cluster_id));
   CONSTRAINT FK_servicedependencies_dependent_service_group_cluster_id FOREIGN KEY (dependent_service_id, dependent_service_group_id, dependent_service_cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id));
 
+CREATE TABLE serviceconfig (
+  service_config_id NUMBER(19) NOT NULL,
+  cluster_id NUMBER(19) NOT NULL,
+  service_id NUMBER(19) NOT NULL,
+  service_group_id NUMBER(19) NOT NULL,
+  version NUMBER(19) NOT NULL,
+  create_timestamp NUMBER(19) NOT NULL,
+  stack_id NUMBER(19) NOT NULL,
+  user_name VARCHAR(255) DEFAULT '_db' NOT NULL,
+  group_id NUMBER(19),
+  note CLOB,
+  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
+  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
+  CONSTRAINT FK_serviceconfig_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
+  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_id, version));
+
+CREATE TABLE serviceconfighosts (
+  service_config_id NUMBER(19) NOT NULL,
+  host_id NUMBER(19) NOT NULL,
+  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
+  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
+  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
+CREATE TABLE serviceconfigmapping (
+  service_config_id NUMBER(19) NOT NULL,
+  config_id NUMBER(19) NOT NULL,
+  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
+  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
+  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
 CREATE TABLE clusterstate (
   cluster_id NUMBER(19) NOT NULL,
   current_cluster_state VARCHAR2(255) NULL,
@@ -493,11 +494,13 @@ CREATE TABLE hostconfigmapping (
   cluster_id NUMBER(19) NOT NULL,
   type_name VARCHAR2(255) NOT NULL,
   selected NUMBER(10) NOT NULL,
-  service_name VARCHAR2(255) NULL,
+  service_id NUMBER(19) NOT NULL,
+  service_group_id NUMBER(19) NOT NULL,
   version_tag VARCHAR2(255) NOT NULL,
   user_name VARCHAR(255) DEFAULT '_db',
   CONSTRAINT PK_hostconfigmapping PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name),
   CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id),
+  CONSTRAINT FK_hostconfmapping_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id));
 
 CREATE TABLE metainfo (
@@ -524,8 +527,10 @@ CREATE TABLE configgroup (
   tag VARCHAR2(1024) NOT NULL,
   description VARCHAR2(1024),
   create_timestamp NUMBER(19) NOT NULL,
-  service_name VARCHAR(255),
+  service_id NUMBER(19) NOT NULL,
+  service_group_id NUMBER(19) NOT NULL,
   CONSTRAINT PK_configgroup PRIMARY KEY (group_id),
+  CONSTRAINT FK_configgroup_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id));
 
 CREATE TABLE confgroupclusterconfigmapping (

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
index dfef1ce..8832818 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-Postgres-CREATE.sql
@@ -123,20 +123,6 @@ CREATE TABLE clusterconfig (
   CONSTRAINT UQ_config_type_tag UNIQUE (cluster_id, type_name, version_tag),
   CONSTRAINT UQ_config_type_version UNIQUE (cluster_id, type_name, version));
 
-CREATE TABLE serviceconfig (
-  service_config_id BIGINT NOT NULL,
-  cluster_id BIGINT NOT NULL,
-  service_name VARCHAR(255) NOT NULL,
-  version BIGINT NOT NULL,
-  create_timestamp BIGINT NOT NULL,
-  stack_id BIGINT NOT NULL,
-  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
-  group_id BIGINT,
-  note TEXT,
-  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
-  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
-  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
-
 CREATE TABLE hosts (
   host_id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
@@ -157,20 +143,6 @@ CREATE TABLE hosts (
   CONSTRAINT PK_hosts PRIMARY KEY (host_id),
   CONSTRAINT UQ_hosts_host_name UNIQUE (host_name));
 
-CREATE TABLE serviceconfighosts (
-  service_config_id BIGINT NOT NULL,
-  host_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
-  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
-  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
-CREATE TABLE serviceconfigmapping (
-  service_config_id BIGINT NOT NULL,
-  config_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
-  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
-  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
 CREATE TABLE servicegroups (
   id BIGINT NOT NULL,
   service_group_name VARCHAR(255) NOT NULL,
@@ -208,6 +180,36 @@ CREATE TABLE servicedependencies (
   CONSTRAINT FK_servicedependencies_service_group_cluster_id FOREIGN KEY (service_id, service_group_id, service_cluster_id) REFERENCES clusterservices  (id, service_group_id, cluster_id));
   CONSTRAINT FK_servicedependencies_dependent_service_group_cluster_id FOREIGN KEY (dependent_service_id, dependent_service_group_id, dependent_service_cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id));
 
+CREATE TABLE serviceconfig (
+  service_config_id BIGINT NOT NULL,
+  cluster_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
+  service_id BIGINT NOT NULL,
+  version BIGINT NOT NULL,
+  create_timestamp BIGINT NOT NULL,
+  stack_id BIGINT NOT NULL,
+  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
+  group_id BIGINT,
+  note TEXT,
+  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
+  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
+  CONSTRAINT FK_serviceconfig_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
+  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_id, version ));
+
+CREATE TABLE serviceconfighosts (
+  service_config_id BIGINT NOT NULL,
+  host_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
+  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
+  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
+CREATE TABLE serviceconfigmapping (
+  service_config_id BIGINT NOT NULL,
+  config_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
+  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
+  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
 CREATE TABLE clusterstate (
   cluster_id BIGINT NOT NULL,
   current_cluster_state VARCHAR(255) NOT NULL,
@@ -499,12 +501,14 @@ CREATE TABLE hostconfigmapping (
   host_id BIGINT NOT NULL,
   type_name VARCHAR(255) NOT NULL,
   version_tag VARCHAR(255) NOT NULL,
-  service_name VARCHAR(255),
+  service_group_id BIGINT NOT NULL,
+  service_id BIGINT NOT NULL,
   create_timestamp BIGINT NOT NULL,
   selected INTEGER NOT NULL DEFAULT 0,
   user_name VARCHAR(255) NOT NULL DEFAULT '_db',
   CONSTRAINT PK_hostconfigmapping PRIMARY KEY (cluster_id, host_id, type_name, create_timestamp),
   CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id),
+  CONSTRAINT FK_hostconfmapping_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id));
 
 CREATE TABLE metainfo (
@@ -524,8 +528,10 @@ CREATE TABLE configgroup (
   tag VARCHAR(1024) NOT NULL,
   description VARCHAR(1024),
   create_timestamp BIGINT NOT NULL,
-  service_name VARCHAR(255),
+  service_group_id BIGINT NOT NULL,
+  service_id BIGINT NOT NULL,
   CONSTRAINT PK_configgroup PRIMARY KEY (group_id),
+  CONSTRAINT FK_configgroup_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id));
 
 CREATE TABLE confgroupclusterconfigmapping (

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
index 870555d..135707b 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-SQLAnywhere-CREATE.sql
@@ -121,20 +121,6 @@ CREATE TABLE ambari_configuration (
   CONSTRAINT FK_ambari_conf_conf_base FOREIGN KEY (id) REFERENCES configuration_base (id)
 );
 
-CREATE TABLE serviceconfig (
-  service_config_id NUMERIC(19) NOT NULL,
-  cluster_id NUMERIC(19) NOT NULL,
-  service_name VARCHAR(255) NOT NULL,
-  version NUMERIC(19) NOT NULL,
-  create_timestamp NUMERIC(19) NOT NULL,
-  stack_id NUMERIC(19) NOT NULL,
-  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
-  group_id NUMERIC(19),
-  note TEXT,
-  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
-  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
-  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
-
 CREATE TABLE hosts (
   host_id NUMERIC(19) NOT NULL,
   host_name VARCHAR(255) NOT NULL,
@@ -155,20 +141,6 @@ CREATE TABLE hosts (
   CONSTRAINT PK_hosts PRIMARY KEY (host_id),
   CONSTRAINT UQ_hosts_host_name UNIQUE (host_name));
 
-CREATE TABLE serviceconfighosts (
-  service_config_id NUMERIC(19) NOT NULL,
-  host_id NUMERIC(19) NOT NULL,
-  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
-  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
-  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
-CREATE TABLE serviceconfigmapping (
-  service_config_id NUMERIC(19) NOT NULL,
-  config_id NUMERIC(19) NOT NULL,
-  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
-  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
-  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
 CREATE TABLE servicegroups (
   id NUMERIC(19) NOT NULL,
   service_group_name VARCHAR(255) NOT NULL,
@@ -206,6 +178,36 @@ CREATE TABLE servicedependencies (
   CONSTRAINT FK_servicedependencies_service_group_cluster_id FOREIGN KEY (service_id, service_group_id, service_cluster_id) REFERENCES clusterservices  (id, service_group_id, cluster_id));
   CONSTRAINT FK_servicedependencies_dependent_service_group_cluster_id FOREIGN KEY (dependent_service_id, dependent_service_group_id, dependent_service_cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id));
 
+CREATE TABLE serviceconfig (
+  service_config_id NUMERIC(19) NOT NULL,
+  cluster_id NUMERIC(19) NOT NULL,
+  service_id NUMERIC(19) NOT NULL,
+  service_group_id NUMERIC(19) NOT NULL,
+  version NUMERIC(19) NOT NULL,
+  create_timestamp NUMERIC(19) NOT NULL,
+  stack_id NUMERIC(19) NOT NULL,
+  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
+  group_id NUMERIC(19),
+  note TEXT,
+  CONSTRAINT PK_serviceconfig PRIMARY KEY (service_config_id),
+  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
+  CONSTRAINT FK_serviceconfig_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
+  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_id, version));
+
+CREATE TABLE serviceconfighosts (
+  service_config_id NUMERIC(19) NOT NULL,
+  host_id NUMERIC(19) NOT NULL,
+  CONSTRAINT PK_serviceconfighosts PRIMARY KEY (service_config_id, host_id),
+  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
+  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
+CREATE TABLE serviceconfigmapping (
+  service_config_id NUMERIC(19) NOT NULL,
+  config_id NUMERIC(19) NOT NULL,
+  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY (service_config_id, config_id),
+  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
+  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
 CREATE TABLE clusterstate (
   cluster_id NUMERIC(19) NOT NULL,
   current_cluster_state VARCHAR(255) NOT NULL,
@@ -489,11 +491,13 @@ CREATE TABLE hostconfigmapping (
   cluster_id NUMERIC(19) NOT NULL,
   type_name VARCHAR(255) NOT NULL,
   selected INTEGER NOT NULL DEFAULT 0,
-  service_name VARCHAR(255),
+  service_id NUMERIC(19) NOT NULL,
+  service_group_id NUMERIC(19) NOT NULL,
   version_tag VARCHAR(255) NOT NULL,
   user_name VARCHAR(255) NOT NULL DEFAULT '_db',
   CONSTRAINT PK_hostconfigmapping PRIMARY KEY (create_timestamp, host_id, cluster_id, type_name),
   CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id),
+  CONSTRAINT FK_hostconfmapping_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id));
 
 CREATE TABLE metainfo (
@@ -520,8 +524,10 @@ CREATE TABLE configgroup (
   tag VARCHAR(1024) NOT NULL,
   description VARCHAR(1024),
   create_timestamp NUMERIC(19) NOT NULL,
-  service_name VARCHAR(255),
+  service_id NUMERIC(19) NOT NULL,
+  service_group_id NUMERIC(19) NOT NULL,
   CONSTRAINT PK_configgroup PRIMARY KEY (group_id),
+  CONSTRAINT FK_configgroup_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id));
 
 CREATE TABLE confgroupclusterconfigmapping (

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
index 641b959..7d93aad 100644
--- a/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
+++ b/ambari-server/src/main/resources/Ambari-DDL-SQLServer-CREATE.sql
@@ -135,20 +135,6 @@ CREATE TABLE ambari_configuration (
   CONSTRAINT FK_ambari_conf_conf_base FOREIGN KEY (id) REFERENCES configuration_base (id)
 );
 
-CREATE TABLE serviceconfig (
-  service_config_id BIGINT NOT NULL,
-  cluster_id BIGINT NOT NULL,
-  service_name VARCHAR(255) NOT NULL,
-  version BIGINT NOT NULL,
-  create_timestamp BIGINT NOT NULL,
-  stack_id BIGINT NOT NULL,
-  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
-  group_id BIGINT,
-  note VARCHAR(MAX),
-  CONSTRAINT PK_serviceconfig PRIMARY KEY CLUSTERED (service_config_id),
-  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
-  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
-
 CREATE TABLE hosts (
   host_id BIGINT NOT NULL,
   host_name VARCHAR(255) NOT NULL,
@@ -169,20 +155,6 @@ CREATE TABLE hosts (
   CONSTRAINT PK_hosts PRIMARY KEY CLUSTERED (host_id),
   CONSTRAINT UQ_hosts_host_name UNIQUE (host_name));
 
-CREATE TABLE serviceconfighosts (
-  service_config_id BIGINT NOT NULL,
-  host_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfighosts PRIMARY KEY CLUSTERED (service_config_id, host_id),
-  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
-  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
-CREATE TABLE serviceconfigmapping (
-  service_config_id BIGINT NOT NULL,
-  config_id BIGINT NOT NULL,
-  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY CLUSTERED (service_config_id, config_id),
-  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
-  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
-
 CREATE TABLE servicegroups (
   id BIGINT NOT NULL,
   service_group_name VARCHAR(255) NOT NULL,
@@ -220,6 +192,36 @@ CREATE TABLE servicedependencies (
   CONSTRAINT FK_servicedependencies_service_group_cluster_id FOREIGN KEY (service_id, service_group_id, service_cluster_id) REFERENCES clusterservices  (id, service_group_id, cluster_id));
   CONSTRAINT FK_servicedependencies_dependent_service_group_cluster_id FOREIGN KEY (dependent_service_id, dependent_service_group_id, dependent_service_cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id));
 
+CREATE TABLE serviceconfig (
+  service_config_id BIGINT NOT NULL,
+  cluster_id BIGINT NOT NULL,
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
+  version BIGINT NOT NULL,
+  create_timestamp BIGINT NOT NULL,
+  stack_id BIGINT NOT NULL,
+  user_name VARCHAR(255) NOT NULL DEFAULT '_db',
+  group_id BIGINT,
+  note VARCHAR(MAX),
+  CONSTRAINT PK_serviceconfig PRIMARY KEY CLUSTERED (service_config_id),
+  CONSTRAINT FK_serviceconfig_stack_id FOREIGN KEY (stack_id) REFERENCES stack(stack_id),
+  CONSTRAINT FK_serviceconfig_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
+  CONSTRAINT UQ_scv_service_version UNIQUE (cluster_id, service_name, version));
+
+CREATE TABLE serviceconfighosts (
+  service_config_id BIGINT NOT NULL,
+  host_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfighosts PRIMARY KEY CLUSTERED (service_config_id, host_id),
+  CONSTRAINT FK_scvhosts_host_id FOREIGN KEY (host_id) REFERENCES hosts(host_id),
+  CONSTRAINT FK_scvhosts_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
+CREATE TABLE serviceconfigmapping (
+  service_config_id BIGINT NOT NULL,
+  config_id BIGINT NOT NULL,
+  CONSTRAINT PK_serviceconfigmapping PRIMARY KEY CLUSTERED (service_config_id, config_id),
+  CONSTRAINT FK_scvm_config FOREIGN KEY (config_id) REFERENCES clusterconfig(config_id),
+  CONSTRAINT FK_scvm_scv FOREIGN KEY (service_config_id) REFERENCES serviceconfig(service_config_id));
+
 CREATE TABLE clusterstate (
   cluster_id BIGINT NOT NULL,
   current_cluster_state VARCHAR(255) NOT NULL,
@@ -502,12 +504,14 @@ CREATE TABLE hostconfigmapping (
   host_id BIGINT NOT NULL,
   type_name VARCHAR(255) NOT NULL,
   version_tag VARCHAR(255) NOT NULL,
-  service_name VARCHAR(255),
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
   create_timestamp BIGINT NOT NULL,
   selected INTEGER NOT NULL DEFAULT 0,
   user_name VARCHAR(255) NOT NULL DEFAULT '_db',
   CONSTRAINT PK_hostconfigmapping PRIMARY KEY CLUSTERED (cluster_id, host_id, type_name, create_timestamp),
   CONSTRAINT FK_hostconfmapping_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id),
+  CONSTRAINT FK_hostconfmapping_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_hostconfmapping_host_id FOREIGN KEY (host_id) REFERENCES hosts (host_id));
 
 CREATE TABLE metainfo (
@@ -528,8 +532,10 @@ CREATE TABLE configgroup (
   tag VARCHAR(1024) NOT NULL,
   description VARCHAR(1024),
   create_timestamp BIGINT NOT NULL,
-  service_name VARCHAR(255),
+  service_id BIGINT NOT NULL,
+  service_group_id BIGINT NOT NULL,
   CONSTRAINT PK_configgroup PRIMARY KEY CLUSTERED (group_id),
+  CONSTRAINT FK_configgroup_cluster_service FOREIGN KEY (service_id, service_group_id, cluster_id) REFERENCES clusterservices (id, service_group_id, cluster_id),
   CONSTRAINT FK_configgroup_cluster_id FOREIGN KEY (cluster_id) REFERENCES clusters (cluster_id));
 
 CREATE TABLE confgroupclusterconfigmapping (

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/test/java/org/apache/ambari/server/checks/ServiceCheckValidityCheckTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/ServiceCheckValidityCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/ServiceCheckValidityCheckTest.java
index 7485257..2ff4d32 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/checks/ServiceCheckValidityCheckTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/ServiceCheckValidityCheckTest.java
@@ -55,6 +55,8 @@ public class ServiceCheckValidityCheckTest {
   private static final String CLUSTER_NAME = "cluster1";
   private static final long CLUSTER_ID = 1L;
   private static final String SERVICE_NAME = "HDFS";
+  private static final long SERVICE_ID = 2L;
+  private static final long SERVICE_GROUP_ID = 3L;
   private static final long CONFIG_CREATE_TIMESTAMP = 1461518722202L;
   private static final long SERVICE_CHECK_START_TIME = CONFIG_CREATE_TIMESTAMP - 2000L;
   private static final String SERVICE_COMPONENT_NAME = "service component";
@@ -132,13 +134,14 @@ public class ServiceCheckValidityCheckTest {
     when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent));
 
     ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
-    serviceConfigEntity.setServiceName(SERVICE_NAME);
+    serviceConfigEntity.setServiceId(SERVICE_ID);
+    serviceConfigEntity.setServiceGroupId(SERVICE_GROUP_ID);
     serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP);
 
     LastServiceCheckDTO lastServiceCheckDTO1 = new LastServiceCheckDTO(Role.ZOOKEEPER_QUORUM_SERVICE_CHECK.name(), SERVICE_CHECK_START_TIME);
     LastServiceCheckDTO lastServiceCheckDTO2 = new LastServiceCheckDTO(Role.HDFS_SERVICE_CHECK.name(), SERVICE_CHECK_START_TIME);
 
-    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity);
+    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_ID))).thenReturn(serviceConfigEntity);
     when(hostRoleCommandDAO.getLatestServiceChecksByRole(any(Long.class))).thenReturn(asList(lastServiceCheckDTO1, lastServiceCheckDTO2));
 
     PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);
@@ -159,12 +162,13 @@ public class ServiceCheckValidityCheckTest {
     when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent));
 
     ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
-    serviceConfigEntity.setServiceName(SERVICE_NAME);
+    serviceConfigEntity.setServiceId(SERVICE_ID);
+    serviceConfigEntity.setServiceGroupId(SERVICE_GROUP_ID);
     serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP);
 
     LastServiceCheckDTO lastServiceCheckDTO = new LastServiceCheckDTO(Role.HDFS_SERVICE_CHECK.name(), SERVICE_CHECK_START_TIME);
 
-    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity);
+    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_ID))).thenReturn(serviceConfigEntity);
     when(hostRoleCommandDAO.getLatestServiceChecksByRole(any(Long.class))).thenReturn(singletonList(lastServiceCheckDTO));
 
     PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);
@@ -182,10 +186,11 @@ public class ServiceCheckValidityCheckTest {
     when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent));
 
     ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
-    serviceConfigEntity.setServiceName(SERVICE_NAME);
+    serviceConfigEntity.setServiceId(SERVICE_ID);
+    serviceConfigEntity.setServiceGroupId(SERVICE_GROUP_ID);
     serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP);
 
-    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity);
+    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_ID))).thenReturn(serviceConfigEntity);
     when(hostRoleCommandDAO.getLatestServiceChecksByRole(any(Long.class))).thenReturn(Collections.<LastServiceCheckDTO>emptyList());
 
     PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);
@@ -202,13 +207,14 @@ public class ServiceCheckValidityCheckTest {
     when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent));
 
     ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
-    serviceConfigEntity.setServiceName(SERVICE_NAME);
+    serviceConfigEntity.setServiceId(SERVICE_ID);
+    serviceConfigEntity.setServiceGroupId(SERVICE_GROUP_ID);
     serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP);
 
     LastServiceCheckDTO lastServiceCheckDTO1 = new LastServiceCheckDTO(Role.HDFS_SERVICE_CHECK.name(), SERVICE_CHECK_START_TIME);
     LastServiceCheckDTO lastServiceCheckDTO2 = new LastServiceCheckDTO(Role.HDFS_SERVICE_CHECK.name(), CONFIG_CREATE_TIMESTAMP - 1L);
 
-    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity);
+    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_ID))).thenReturn(serviceConfigEntity);
     when(hostRoleCommandDAO.getLatestServiceChecksByRole(any(Long.class))).thenReturn(asList(lastServiceCheckDTO1, lastServiceCheckDTO2));
 
     PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);
@@ -235,7 +241,8 @@ public class ServiceCheckValidityCheckTest {
     when(service.getServiceComponents()).thenReturn(ImmutableMap.of(SERVICE_COMPONENT_NAME, serviceComponent));
 
     ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity();
-    serviceConfigEntity.setServiceName(SERVICE_NAME);
+    serviceConfigEntity.setServiceId(SERVICE_ID);
+    serviceConfigEntity.setServiceGroupId(SERVICE_GROUP_ID);
     serviceConfigEntity.setCreateTimestamp(CONFIG_CREATE_TIMESTAMP);
 
     String hdfsRole = Role.HDFS_SERVICE_CHECK.name();
@@ -244,7 +251,7 @@ public class ServiceCheckValidityCheckTest {
     LastServiceCheckDTO lastServiceCheckDTO1 = new LastServiceCheckDTO(hdfsRole, SERVICE_CHECK_START_TIME);
     LastServiceCheckDTO lastServiceCheckDTO2 = new LastServiceCheckDTO(hdfs2Role, CONFIG_CREATE_TIMESTAMP - 1L);
 
-    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_NAME))).thenReturn(serviceConfigEntity);
+    when(serviceConfigDAO.getLastServiceConfig(eq(CLUSTER_ID), eq(SERVICE_ID))).thenReturn(serviceConfigEntity);
     when(hostRoleCommandDAO.getLatestServiceChecksByRole(any(Long.class))).thenReturn(asList(lastServiceCheckDTO1, lastServiceCheckDTO2));
 
     PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);


[2/2] ambari git commit: AMBARI-22309 Update db schema to use service_id instead of service_name (configs part) (dsen)

Posted by ds...@apache.org.
AMBARI-22309 Update db schema to use service_id instead of service_name (configs part) (dsen)


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

Branch: refs/heads/branch-feature-AMBARI-14714
Commit: 527c4a2d74e7f03b44d9412bd418c73a6f155032
Parents: 72bac1f
Author: Dmytro Sen <ds...@apache.org>
Authored: Fri Nov 3 14:35:10 2017 +0200
Committer: Dmytro Sen <ds...@apache.org>
Committed: Fri Nov 3 14:35:10 2017 +0200

----------------------------------------------------------------------
 .../checks/ServiceCheckValidityCheck.java       |   2 +-
 .../AmbariManagementControllerImpl.java         |  16 +-
 .../server/controller/ConfigGroupRequest.java   |  12 +-
 .../controller/ServiceConfigVersionRequest.java |  14 +-
 .../ServiceConfigVersionResponse.java           |  24 ++
 .../internal/ClusterResourceProvider.java       |   6 +
 .../internal/ConfigGroupResourceProvider.java   |  48 ++--
 .../ServiceConfigVersionResourceProvider.java   |   7 +-
 .../server/orm/cache/HostConfigMapping.java     |   7 +-
 .../server/orm/cache/HostConfigMappingImpl.java |  30 ++-
 .../server/orm/dao/HostConfigMappingDAO.java    |   8 +-
 .../ambari/server/orm/dao/ServiceConfigDAO.java |  46 ++--
 .../orm/entities/ClusterServiceEntity.java      |   6 +
 .../server/orm/entities/ConfigGroupEntity.java  |  41 ++-
 .../orm/entities/HostConfigMappingEntity.java   |  59 +++-
 .../orm/entities/ServiceConfigEntity.java       |  66 ++++-
 .../serveraction/upgrades/ConfigureAction.java  |   2 +-
 .../upgrades/FinalizeUpgradeAction.java         |   4 +-
 .../upgrades/PreconfigureKerberosAction.java    |   2 +-
 .../org/apache/ambari/server/state/Cluster.java |  35 +--
 .../ambari/server/state/ConfigHelper.java       |   4 +-
 .../ambari/server/state/DesiredConfig.java      |  51 +++-
 .../apache/ambari/server/state/ServiceImpl.java |  13 +-
 .../ambari/server/state/UpgradeHelper.java      |   7 +-
 .../server/state/cluster/ClusterImpl.java       | 268 +++++++++++--------
 .../server/state/configgroup/ConfigGroup.java   |  11 +-
 .../state/configgroup/ConfigGroupFactory.java   |   3 +-
 .../state/configgroup/ConfigGroupImpl.java      |  67 +++--
 .../ambari/server/state/host/HostImpl.java      |   3 +-
 .../ambari/server/topology/AmbariContext.java   |   2 +-
 .../apache/ambari/server/topology/Service.java  |   7 +
 .../server/upgrade/AbstractUpgradeCatalog.java  |   2 +-
 .../main/resources/Ambari-DDL-Derby-CREATE.sql  |  66 ++---
 .../main/resources/Ambari-DDL-MySQL-CREATE.sql  |  66 ++---
 .../main/resources/Ambari-DDL-Oracle-CREATE.sql |  67 ++---
 .../resources/Ambari-DDL-Postgres-CREATE.sql    |  66 ++---
 .../resources/Ambari-DDL-SQLAnywhere-CREATE.sql |  66 ++---
 .../resources/Ambari-DDL-SQLServer-CREATE.sql   |  66 ++---
 .../checks/ServiceCheckValidityCheckTest.java   |  27 +-
 39 files changed, 829 insertions(+), 468 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/checks/ServiceCheckValidityCheck.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/ServiceCheckValidityCheck.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/ServiceCheckValidityCheck.java
index a4c2430..2582c71 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/ServiceCheckValidityCheck.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/ServiceCheckValidityCheck.java
@@ -106,7 +106,7 @@ public class ServiceCheckValidityCheck extends AbstractCheckDescriptor {
         LOG.info(String.format("%s in %s version %s does not have customizable configurations. Skip checking service configuration history.", service.getName(), stackId.getStackName(), stackId.getStackVersion()));
       } else {
         LOG.info(String.format("%s in %s version %s has customizable configurations. Check service configuration history.", service.getName(), stackId.getStackName(), stackId.getStackVersion()));
-        ServiceConfigEntity lastServiceConfig = serviceConfigDAO.getLastServiceConfig(clusterId, service.getName());
+        ServiceConfigEntity lastServiceConfig = serviceConfigDAO.getLastServiceConfig(clusterId, service.getServiceId());
         lastServiceConfigUpdates.put(service.getName(), lastServiceConfig.getCreateTimestamp());
       }
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
index 370f735..7f5c051 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
@@ -883,7 +883,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
 
     // If the config type is for a service, then allow a user with SERVICE_MODIFY_CONFIGS to
     // update, else ensure the user has CLUSTER_MODIFY_CONFIGS
-    String service = null;
+    Long service = null;
 
     try {
       service = cluster.getServiceForConfigTypes(Collections.singleton(configType));
@@ -897,7 +897,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     // recovery mode setting.
     Map<String, String[]> propertyChanges = getPropertyChanges(cluster, request);
 
-    if(StringUtils.isEmpty(service)) {
+    if(service == null) {
       // If the configuration is not attached to a specific service, it is a cluster-wide configuration
       // type. For example, cluster-env.
 
@@ -1961,6 +1961,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
       }
 
       ServiceConfigVersionRequest serviceConfigVersionRequest = request.getServiceConfigVersionRequest();
+      //TODO add serviceGroupName validation when the UI is updated to use them
       if (StringUtils.isEmpty(serviceConfigVersionRequest.getServiceName()) ||
           null == serviceConfigVersionRequest.getVersion()) {
         String msg = "Service name and version should be specified in service config version";
@@ -1968,7 +1969,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
         throw new IllegalArgumentException(msg);
       }
 
-      serviceConfigVersionResponse = cluster.setServiceConfigVersion(serviceConfigVersionRequest.getServiceName(),
+      serviceConfigVersionResponse = cluster.setServiceConfigVersion(
+          cluster.getService(serviceConfigVersionRequest.getServiceGroupName(), serviceConfigVersionRequest.getServiceName()).getServiceId(),
           serviceConfigVersionRequest.getVersion(), getAuthName(),
           serviceConfigVersionRequest.getNote());
     }
@@ -3985,7 +3987,13 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     List<ServiceConfigVersionResponse> serviceConfigVersionResponses =  new ArrayList<>();
 
     if (Boolean.TRUE.equals(request.getIsCurrent()) && serviceName != null) {
-      serviceConfigVersionResponses.addAll(cluster.getActiveServiceConfigVersionResponse(serviceName));
+      //TODO add serviceGroupName validation when the UI is updated to use them
+      try {
+        Long serviceId = cluster.getService(request.getServiceGroupName(), request.getServiceName()).getServiceId();
+        serviceConfigVersionResponses.addAll(cluster.getActiveServiceConfigVersionResponse(serviceId));
+      } catch (ServiceNotFoundException e) {
+        LOG.warn("Service {} not found on cluster {}", serviceName, cluster.getClusterName());
+      }
     } else {
       serviceConfigVersionResponses.addAll(cluster.getServiceConfigVersions());
     }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/ConfigGroupRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ConfigGroupRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ConfigGroupRequest.java
index babdf10..fb8d2c9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ConfigGroupRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ConfigGroupRequest.java
@@ -28,18 +28,20 @@ public class ConfigGroupRequest {
   private String groupName;
   private String tag;
   private String serviceName;
+  private String serviceGroupName;
   private String description;
   private String serviceConfigVersionNote;
   private Set<String> hosts;
   private Map<String, Config> configs;
 
   public ConfigGroupRequest(Long id, String clusterName, String groupName,
-                            String tag, String serviceName, String description,
+                            String tag, String serviceGroupName, String serviceName, String description,
                             Set<String> hosts, Map<String, Config> configs) {
     this.id = id;
     this.clusterName = clusterName;
     this.groupName = groupName;
     this.tag = tag;
+    this.serviceGroupName = serviceGroupName;
     this.serviceName = serviceName;
     this.description = description;
     this.hosts = hosts;
@@ -117,4 +119,12 @@ public class ConfigGroupRequest {
   public void setServiceConfigVersionNote(String serviceConfigVersionNote) {
     this.serviceConfigVersionNote = serviceConfigVersionNote;
   }
+
+  public String getServiceGroupName() {
+    return serviceGroupName;
+  }
+
+  public void setServiceGroupName(String serviceGroupName) {
+    this.serviceGroupName = serviceGroupName;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionRequest.java
index c47a5e0..29a7a42 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionRequest.java
@@ -21,6 +21,7 @@ package org.apache.ambari.server.controller;
 public class ServiceConfigVersionRequest {
   private String clusterName;
   private String serviceName;
+  private String serviceGroupName;
   private Long version;
   private Long createTime;
   private Long applyTime;
@@ -31,8 +32,9 @@ public class ServiceConfigVersionRequest {
   public ServiceConfigVersionRequest() {
   }
 
-  public ServiceConfigVersionRequest(String clusterName, String serviceName, Long version, Long createTime, Long applyTime, String userName, Boolean isCurrent) {
+  public ServiceConfigVersionRequest(String clusterName, String serviceGroupName, String serviceName, Long version, Long createTime, Long applyTime, String userName, Boolean isCurrent) {
     this.clusterName = clusterName;
+    this.serviceGroupName = serviceGroupName;
     this.serviceName = serviceName;
     this.version = version;
     this.createTime = createTime;
@@ -105,10 +107,20 @@ public class ServiceConfigVersionRequest {
     this.isCurrent = isCurrent;
   }
 
+  public String getServiceGroupName() {
+    return serviceGroupName;
+  }
+
+  public void setServiceGroupName(String serviceGroupName) {
+    this.serviceGroupName = serviceGroupName;
+  }
+
+
   @Override
   public String toString() {
     return "ServiceConfigVersionRequest{" +
         "clusterName='" + clusterName + '\'' +
+        ", serviceGroupName='" + serviceGroupName + '\'' +
         ", serviceName='" + serviceName + '\'' +
         ", version=" + version +
         ", createTime=" + createTime +

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionResponse.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionResponse.java
index 275d703..0d5b99e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionResponse.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceConfigVersionResponse.java
@@ -54,6 +54,15 @@ public class ServiceConfigVersionResponse {
   @JsonProperty("service_name")
   private final String serviceName;
 
+  @JsonProperty("service_id")
+  private final Long serviceId;
+
+  @JsonProperty("service_group_name")
+  private final String serviceGroupName;
+
+  @JsonProperty("service_group_id")
+  private final Long serviceGroupId;
+
   @JsonProperty("service_config_version")
   private final Long version;
 
@@ -106,6 +115,9 @@ public class ServiceConfigVersionResponse {
 
     clusterName = clusterEntity.getClusterName();
     serviceName = serviceConfigEntity.getServiceName();
+    serviceId = serviceConfigEntity.getServiceId();
+    serviceGroupName = serviceConfigEntity.getServiceGroupName();
+    serviceGroupId = serviceConfigEntity.getServiceGroupId();
     version = serviceConfigEntity.getVersion();
     userName = serviceConfigEntity.getUser();
     createTime = serviceConfigEntity.getCreateTimestamp();
@@ -234,5 +246,17 @@ public class ServiceConfigVersionResponse {
       .append(hosts)
       .toHashCode();
   }
+
+  public String getServiceGroupName() {
+    return serviceGroupName;
+  }
+
+  public Long getServiceId() {
+    return serviceId;
+  }
+
+  public Long getServiceGroupId() {
+    return serviceGroupId;
+  }
 }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
index ed80291..61e0f99 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
@@ -328,8 +328,14 @@ public class ClusterResourceProvider extends AbstractControllerResourceProvider
           for (Collection<ServiceConfigVersionResponse> scvCollection : serviceConfigVersions.values()) {
             for (ServiceConfigVersionResponse serviceConfigVersionResponse : scvCollection) {
               Resource resource = new ResourceImpl(Resource.Type.ServiceConfigVersion);
+              resource.setProperty(ServiceConfigVersionResourceProvider.SERVICE_CONFIG_VERSION_SERVICE_GROUP_NAME_PROPERTY_ID,
+                serviceConfigVersionResponse.getServiceGroupName());
+              resource.setProperty(ServiceConfigVersionResourceProvider.SERVICE_CONFIG_VERSION_SERVICE_GROUP_ID_PROPERTY_ID,
+                  serviceConfigVersionResponse.getServiceGroupId());
               resource.setProperty(ServiceConfigVersionResourceProvider.SERVICE_CONFIG_VERSION_SERVICE_NAME_PROPERTY_ID,
                 serviceConfigVersionResponse.getServiceName());
+              resource.setProperty(ServiceConfigVersionResourceProvider.SERVICE_CONFIG_VERSION_SERVICE_ID_PROPERTY_ID,
+                serviceConfigVersionResponse.getServiceId());
               resource.setProperty(ServiceConfigVersionResourceProvider.SERVICE_CONFIG_VERSION_PROPERTY_ID,
                 serviceConfigVersionResponse.getVersion());
               resource.setProperty(ServiceConfigVersionResourceProvider.SERVICE_CONFIG_VERSION_NOTE_PROPERTY_ID,

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProvider.java
index 737bfa4..332b1d2 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ConfigGroupResourceProvider.java
@@ -60,6 +60,7 @@ import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.Config;
 import org.apache.ambari.server.state.ConfigFactory;
 import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.Service;
 import org.apache.ambari.server.state.configgroup.ConfigGroup;
 import org.apache.ambari.server.state.configgroup.ConfigGroupFactory;
 import org.apache.commons.collections.MapUtils;
@@ -85,6 +86,8 @@ public class ConfigGroupResourceProvider extends
     .getPropertyId("ConfigGroup", "group_name");
   protected static final String CONFIGGROUP_TAG_PROPERTY_ID = PropertyHelper
     .getPropertyId("ConfigGroup", "tag");
+  protected static final String CONFIGGROUP_SERVICEGROUPNAME_PROPERTY_ID = PropertyHelper
+    .getPropertyId("ConfigGroup", "service_group_name");
   protected static final String CONFIGGROUP_SERVICENAME_PROPERTY_ID = PropertyHelper
     .getPropertyId("ConfigGroup", "service_name");
   protected static final String CONFIGGROUP_DESC_PROPERTY_ID = PropertyHelper
@@ -475,7 +478,7 @@ public class ConfigGroupResourceProvider extends
       throw new ConfigGroupNotFoundException(cluster.getClusterName(), request.getId().toString());
     }
 
-    if (StringUtils.isEmpty(configGroup.getServiceName())) {
+    if (configGroup.getServiceId() == null) {
       if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, cluster.getResourceId(),
         RoleAuthorization.CLUSTER_MANAGE_CONFIG_GROUPS)) {
         throw new AuthorizationException("The authenticated user is not authorized to delete config groups");
@@ -564,18 +567,20 @@ public class ConfigGroupResourceProvider extends
       }
 
       verifyHostList(cluster, hosts, request);
-
-      String serviceName = request.getServiceName();
-      if (serviceName == null && !MapUtils.isEmpty(request.getConfigs())) {
+      Service service = cluster.getService(request.getServiceGroupName(), request.getServiceName());
+      Long serviceId = service.getServiceId();
+      Long serviceGroupId = service.getServiceGroupId();
+      if (serviceId == null && !MapUtils.isEmpty(request.getConfigs())) {
         try {
-          serviceName = cluster.getServiceForConfigTypes(request.getConfigs().keySet());
+          serviceId = cluster.getServiceForConfigTypes(request.getConfigs().keySet());
+          serviceGroupId = cluster.getService(serviceId).getServiceGroupId();
         } catch (IllegalArgumentException e) {
           // Ignore this since we may have hit a config type that spans multiple services. This may
           // happen in unit test cases but should not happen with later versions of stacks.
         }
       }
 
-      if (StringUtils.isEmpty(serviceName)) {
+      if (serviceId == null) {
         if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, cluster.getResourceId(),
             RoleAuthorization.CLUSTER_MANAGE_CONFIG_GROUPS)) {
           throw new AuthorizationException("The authenticated user is not authorized to create config groups");
@@ -592,14 +597,14 @@ public class ConfigGroupResourceProvider extends
 
       verifyConfigs(request.getConfigs(), cluster.getClusterName());
 
-      ConfigGroup configGroup = configGroupFactory.createNew(cluster, serviceName,
+      ConfigGroup configGroup = configGroupFactory.createNew(cluster, serviceGroupId, serviceId,
         request.getGroupName(),
         request.getTag(), request.getDescription(),
         request.getConfigs(), hosts);
 
       cluster.addConfigGroup(configGroup);
-      if (serviceName != null) {
-        cluster.createServiceConfigVersion(serviceName, getManagementController().getAuthName(),
+      if (serviceGroupId != null && serviceId != null) {
+        cluster.createServiceConfigVersion(serviceId, getManagementController().getAuthName(),
           request.getServiceConfigVersionNote(), configGroup);
       } else {
         LOG.warn("Could not determine service name for config group {}, service config version not created",
@@ -648,10 +653,10 @@ public class ConfigGroupResourceProvider extends
                                  + ", groupId = " + request.getId());
       }
 
-      String serviceName = configGroup.getServiceName();
-      String requestServiceName = cluster.getServiceForConfigTypes(request.getConfigs().keySet());
+      Long serviceId = configGroup.getServiceId();
+      Long requestServiceId = cluster.getServiceForConfigTypes(request.getConfigs().keySet());
 
-      if (StringUtils.isEmpty(serviceName) && StringUtils.isEmpty(requestServiceName)) {
+      if (serviceId == null && requestServiceId == null) {
         if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, cluster.getResourceId(),
             RoleAuthorization.CLUSTER_MANAGE_CONFIG_GROUPS)) {
           throw new AuthorizationException("The authenticated user is not authorized to update config groups");
@@ -663,20 +668,20 @@ public class ConfigGroupResourceProvider extends
         }
       }
 
-      if (serviceName != null && requestServiceName != null && !StringUtils.equals(serviceName, requestServiceName)) {
+      if (serviceId != null && requestServiceId != null && !serviceId.equals(requestServiceId)) {
         throw new IllegalArgumentException("Config group " + configGroup.getId() +
-            " is mapped to service " + serviceName + ", " +
-            "but request contain configs from service " + requestServiceName);
-      } else if (serviceName == null && requestServiceName != null) {
-        configGroup.setServiceName(requestServiceName);
-        serviceName = requestServiceName;
+            " is mapped to service " + serviceId + ", " +
+            "but request contain configs from service " + requestServiceId);
+      } else if (serviceId == null && requestServiceId != null) {
+        configGroup.setServiceId(requestServiceId);
+        serviceId = requestServiceId;
       }
 
       int numHosts = (null != configGroup.getHosts()) ? configGroup.getHosts().size() : 0;
       configLogger.info("(configchange) Updating configuration group host membership or config value. cluster: '{}', changed by: '{}', " +
               "service_name: '{}', config group: '{}', tag: '{}', num hosts in config group: '{}', note: '{}'",
           cluster.getClusterName(), getManagementController().getAuthName(),
-          serviceName, request.getGroupName(), request.getTag(), numHosts, request.getServiceConfigVersionNote());
+          serviceId, request.getGroupName(), request.getTag(), numHosts, request.getServiceConfigVersionNote());
 
       if (!request.getConfigs().isEmpty()) {
         List<String> affectedConfigTypeList = new ArrayList(request.getConfigs().keySet());
@@ -718,8 +723,8 @@ public class ConfigGroupResourceProvider extends
       configGroup.setDescription(request.getDescription());
       configGroup.setTag(request.getTag());
 
-      if (serviceName != null) {
-        cluster.createServiceConfigVersion(serviceName, getManagementController().getAuthName(),
+      if (serviceId != null) {
+        cluster.createServiceConfigVersion(serviceId, getManagementController().getAuthName(),
                 request.getServiceConfigVersionNote(), configGroup);
 
         ConfigGroupResponse configGroupResponse = new ConfigGroupResponse(configGroup.getId(), cluster.getClusterName(), configGroup.getName(),
@@ -754,6 +759,7 @@ public class ConfigGroupResourceProvider extends
       (String) properties.get(CONFIGGROUP_CLUSTER_NAME_PROPERTY_ID),
       (String) properties.get(CONFIGGROUP_NAME_PROPERTY_ID),
       (String) properties.get(CONFIGGROUP_TAG_PROPERTY_ID),
+      (String) properties.get(CONFIGGROUP_SERVICEGROUPNAME_PROPERTY_ID),
       (String) properties.get(CONFIGGROUP_SERVICENAME_PROPERTY_ID),
       (String) properties.get(CONFIGGROUP_DESC_PROPERTY_ID),
       null,

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceConfigVersionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceConfigVersionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceConfigVersionResourceProvider.java
index e7dbbc0..cbc08eb 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceConfigVersionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ServiceConfigVersionResourceProvider.java
@@ -52,7 +52,10 @@ public class ServiceConfigVersionResourceProvider extends
 
   public static final String SERVICE_CONFIG_VERSION_CLUSTER_NAME_PROPERTY_ID = PropertyHelper.getPropertyId(null, "cluster_name");
   public static final String SERVICE_CONFIG_VERSION_PROPERTY_ID = PropertyHelper.getPropertyId(null, "service_config_version");
+  public static final String SERVICE_CONFIG_VERSION_SERVICE_GROUP_NAME_PROPERTY_ID = PropertyHelper.getPropertyId(null, "service_group_name");
+  public static final String SERVICE_CONFIG_VERSION_SERVICE_GROUP_ID_PROPERTY_ID = PropertyHelper.getPropertyId(null, "service_group_id");
   public static final String SERVICE_CONFIG_VERSION_SERVICE_NAME_PROPERTY_ID = PropertyHelper.getPropertyId(null, "service_name");
+  public static final String SERVICE_CONFIG_VERSION_SERVICE_ID_PROPERTY_ID = PropertyHelper.getPropertyId(null, "service_id");
   public static final String SERVICE_CONFIG_VERSION_CREATE_TIME_PROPERTY_ID = PropertyHelper.getPropertyId(null, "createtime");
   public static final String SERVICE_CONFIG_VERSION_USER_PROPERTY_ID = PropertyHelper.getPropertyId(null, "user");
   public static final String SERVICE_CONFIG_VERSION_NOTE_PROPERTY_ID = PropertyHelper.getPropertyId(null, "service_config_version_note");
@@ -194,6 +197,7 @@ public class ServiceConfigVersionResourceProvider extends
 
     for (String propertyId : propertyIds) {
       if (!propertyId.equals("cluster_name") && !propertyId.equals("service_config_version") &&
+          !propertyId.equals("service_group_name")&&
           !propertyId.equals("service_name") && !propertyId.equals("createtime") &&
           !propertyId.equals("appliedtime") && !propertyId.equals("user") &&
           !propertyId.equals("service_config_version_note") &&
@@ -214,13 +218,14 @@ public class ServiceConfigVersionResourceProvider extends
 
   private ServiceConfigVersionRequest createRequest(Map<String, Object> properties) {
     String clusterName = (String) properties.get(SERVICE_CONFIG_VERSION_CLUSTER_NAME_PROPERTY_ID);
+    String serviceGroupName = (String) properties.get(SERVICE_CONFIG_VERSION_SERVICE_GROUP_NAME_PROPERTY_ID);
     String serviceName = (String) properties.get(SERVICE_CONFIG_VERSION_SERVICE_NAME_PROPERTY_ID);
     String user = (String) properties.get(SERVICE_CONFIG_VERSION_USER_PROPERTY_ID);
     Boolean isCurrent = Boolean.valueOf((String) properties.get(SERVICE_CONFIG_VERSION_IS_CURRENT_PROPERTY_ID));
     Object versionObject = properties.get(SERVICE_CONFIG_VERSION_PROPERTY_ID);
     Long version = versionObject == null ? null : Long.valueOf(versionObject.toString());
 
-    return new ServiceConfigVersionRequest(clusterName, serviceName, version, null, null, user, isCurrent);
+    return new ServiceConfigVersionRequest(clusterName, serviceGroupName, serviceName, version, null, null, user, isCurrent);
   }
 
   private List<Map<String, Object>> convertToSubResources(final String clusterName, List<ConfigurationResponse> configs) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java
index 8f6c9b7..9ead794 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java
@@ -34,8 +34,11 @@ public interface HostConfigMapping {
   String getVersion();
   void setVersion(String version);
   
-  String getServiceName();
-  void setServiceName(String serviceName);
+  Long getServiceId();
+  void setServiceId(Long serviceId);
+
+  Long getServiceGroupId();
+  void setServiceGroupId(Long serviceGroupId);
   
   String getUser();
   void setUser(String user);

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java
index 5da7f95..f28841f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java
@@ -26,7 +26,8 @@ public class HostConfigMappingImpl implements HostConfigMapping {
   private String type;
   private Long createTimestamp;
   private String version;
-  private String serviceName;
+  private Long serviceId;
+  private Long serviceGroupId;
   private String user;
   private Integer selected;
   
@@ -38,7 +39,8 @@ public class HostConfigMappingImpl implements HostConfigMapping {
     setType(entry.getType());
     setCreateTimestamp(entry.getCreateTimestamp());
     setVersion(entry.getVersion());
-    setServiceName(entry.getServiceName());
+    setServiceGroupId(entry.getServiceGroupId());
+    setServiceId(entry.getServiceId());
     setUser(entry.getUser());
     setSelected(entry.getSelected());
   }
@@ -105,15 +107,27 @@ public class HostConfigMappingImpl implements HostConfigMapping {
       throw new RuntimeException("Version couldn't be null");
     this.version = version;
   }
-  public String getServiceName() {
-    return serviceName;
+
+  @Override
+  public Long getServiceId() {
+    return serviceId;
   }
-  
+
   @Override
-  public void setServiceName(String serviceName) {
-    this.serviceName = serviceName;
+  public void setServiceId(Long serviceId) {
+    this.serviceId = serviceId;
   }
-  
+
+  @Override
+  public Long getServiceGroupId() {
+    return serviceGroupId;
+  }
+
+  @Override
+  public void setServiceGroupId(Long serviceGroupId) {
+    this.serviceGroupId = serviceGroupId;
+  }
+
   @Override
   public String getUser() {
     return user;

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAO.java
index 5e43473..b80e0e7 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAO.java
@@ -351,7 +351,8 @@ public class HostConfigMappingDAO {
     hostConfigMappingEntity.setCreateTimestamp(hostConfigMapping.getCreateTimestamp());
     hostConfigMappingEntity.setHostId(hostEntity.getHostId());
     hostConfigMappingEntity.setSelected(hostConfigMapping.getSelected());
-    hostConfigMappingEntity.setServiceName(hostConfigMapping.getServiceName());
+    hostConfigMappingEntity.setServiceGroupId(hostConfigMapping.getServiceGroupId());
+    hostConfigMappingEntity.setServiceId(hostConfigMapping.getServiceId());
     hostConfigMappingEntity.setType(hostConfigMapping.getType());
     hostConfigMappingEntity.setUser(hostConfigMapping.getUser());
     hostConfigMappingEntity.setVersion(hostConfigMapping.getVersion());
@@ -362,11 +363,12 @@ public class HostConfigMappingDAO {
   public HostConfigMapping buildHostConfigMapping(
       HostConfigMappingEntity hostConfigMappingEntity) {
     HostConfigMapping hostConfigMapping = new HostConfigMappingImpl();
-    
+
     hostConfigMapping.setClusterId(hostConfigMappingEntity.getClusterId());
     hostConfigMapping.setCreateTimestamp(hostConfigMappingEntity.getCreateTimestamp());
     hostConfigMapping.setHostId(hostConfigMappingEntity.getHostId());
-    hostConfigMapping.setServiceName(hostConfigMappingEntity.getServiceName());
+    hostConfigMapping.setServiceGroupId(hostConfigMappingEntity.getServiceGroupId());
+    hostConfigMapping.setServiceId(hostConfigMappingEntity.getServiceId());
     hostConfigMapping.setType(hostConfigMappingEntity.getType());
     hostConfigMapping.setUser(hostConfigMappingEntity.getUser());
     hostConfigMapping.setSelected(hostConfigMappingEntity.isSelected());

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
index 6bfad54..d00ff67 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ServiceConfigDAO.java
@@ -59,19 +59,19 @@ public class ServiceConfigDAO {
   }
 
   @RequiresSession
-  public ServiceConfigEntity findByServiceAndVersion(String serviceName, Long version) {
+  public ServiceConfigEntity findByServiceAndVersion(Long serviceId, Long version) {
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().
         createQuery("SELECT scv FROM ServiceConfigEntity scv " +
-            "WHERE scv.serviceName=?1 AND scv.version=?2", ServiceConfigEntity.class);
-    return daoUtils.selectOne(query, serviceName, version);
+            "WHERE scv.serviceId=?1 AND scv.version=?2", ServiceConfigEntity.class);
+    return daoUtils.selectOne(query, serviceId, version);
   }
 
   @RequiresSession
-  public List<ServiceConfigEntity> findByService(Long clusterId, String serviceName) {
+  public List<ServiceConfigEntity> findByService(Long clusterId, Long serviceId) {
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().
         createQuery("SELECT scv FROM ServiceConfigEntity scv " +
-            "WHERE scv.clusterId=?1 AND scv.serviceName=?2", ServiceConfigEntity.class);
-    return daoUtils.selectList(query, clusterId, serviceName);
+            "WHERE scv.clusterId=?1 AND scv.serviceId=?2", ServiceConfigEntity.class);
+    return daoUtils.selectList(query, clusterId, serviceId);
   }
 
   @RequiresSession
@@ -128,18 +128,18 @@ public class ServiceConfigDAO {
    *  Gets the latest service config versions of all config groups for a service
    * @param clusterId
    *          the cluster (not {@code null}).
-   * @param serviceName
-   *          Name of the service whose latest service config versions needs to be retrieved .
+   * @param serviceId
+   *          ID of the service whose latest service config versions needs to be retrieved .
    * @return all service configurations for the cluster and service.
    */
   @RequiresSession
-  public List<ServiceConfigEntity> getLastServiceConfigsForService(Long clusterId, String serviceName) {
+  public List<ServiceConfigEntity> getLastServiceConfigsForService(Long clusterId, Long serviceId) {
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().createNamedQuery(
         "ServiceConfigEntity.findLatestServiceConfigsByService",
         ServiceConfigEntity.class);
 
     query.setParameter("clusterId", clusterId);
-    query.setParameter("serviceName", serviceName);
+    query.setParameter("serviceId", serviceId);
 
     return daoUtils.selectList(query);
   }
@@ -157,7 +157,7 @@ public class ServiceConfigDAO {
    */
   @RequiresSession
   public List<ServiceConfigEntity> getServiceConfigsForServiceAndStack(Long clusterId,
-      StackId stackId, String serviceName) {
+      StackId stackId, Long serviceId) {
 
     StackEntity stackEntity = stackDAO.find(stackId.getStackName(),
         stackId.getStackVersion());
@@ -168,7 +168,7 @@ public class ServiceConfigDAO {
 
     query.setParameter("clusterId", clusterId);
     query.setParameter("stack", stackEntity);
-    query.setParameter("serviceName", serviceName);
+    query.setParameter("serviceId", serviceId);
 
     return daoUtils.selectList(query);
   }
@@ -200,24 +200,24 @@ public class ServiceConfigDAO {
   }
 
   @RequiresSession
-  public ServiceConfigEntity getLastServiceConfig(Long clusterId, String serviceName) {
+  public ServiceConfigEntity getLastServiceConfig(Long clusterId, Long serviceId) {
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().
         createQuery("SELECT scv FROM ServiceConfigEntity scv " +
-          "WHERE scv.clusterId = ?1 AND scv.serviceName = ?2 " +
+          "WHERE scv.clusterId = ?1 AND scv.serviceId = ?2 " +
           "ORDER BY scv.createTimestamp DESC",
           ServiceConfigEntity.class);
 
-    return daoUtils.selectOne(query, clusterId, serviceName);
+    return daoUtils.selectOne(query, clusterId, serviceId);
   }
 
   @RequiresSession
-  public ServiceConfigEntity findMaxVersion(Long clusterId, String serviceName) {
+  public ServiceConfigEntity findMaxVersion(Long clusterId, Long serviceId) {
     TypedQuery<ServiceConfigEntity> query = entityManagerProvider.get().createQuery("SELECT scv FROM ServiceConfigEntity scv " +
-      "WHERE scv.clusterId=?1 AND scv.serviceName=?2 AND scv.version = (" +
+      "WHERE scv.clusterId=?1 AND scv.serviceId=?2 AND scv.version = (" +
       "SELECT max(scv2.version) FROM ServiceConfigEntity scv2 " +
-      "WHERE scv2.clusterId=?1 AND scv2.serviceName=?2)", ServiceConfigEntity.class);
+      "WHERE scv2.clusterId=?1 AND scv2.serviceId=?2)", ServiceConfigEntity.class);
 
-    return daoUtils.selectSingle(query, clusterId, serviceName);
+    return daoUtils.selectSingle(query, clusterId, serviceId);
   }
 
   /**
@@ -250,17 +250,17 @@ public class ServiceConfigDAO {
    *
    * @param clusterId
    *          the cluster that the service is a part of.
-   * @param serviceName
-   *          the name of the service (not {@code null}).
+   * @param serviceId
+   *          the ID of the service (not {@code null}).
    * @return the maximum version value + 1
    */
   @RequiresSession
-  public Long findNextServiceConfigVersion(long clusterId, String serviceName) {
+  public Long findNextServiceConfigVersion(long clusterId, Long serviceId) {
     TypedQuery<Number> query = entityManagerProvider.get().createNamedQuery(
         "ServiceConfigEntity.findNextServiceConfigVersion", Number.class);
 
     query.setParameter("clusterId", clusterId);
-    query.setParameter("serviceName", serviceName);
+    query.setParameter("serviceId", serviceId);
 
     return daoUtils.selectSingle(query).longValue();
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java
index b6c60be..976bb67 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterServiceEntity.java
@@ -235,4 +235,10 @@ public class ClusterServiceEntity {
     this.serviceComponentDesiredStateEntities = serviceComponentDesiredStateEntities;
   }
 
+  public String getServiceGroupName() {
+    if (serviceGroupEntity != null) {
+      return serviceGroupEntity.getServiceGroupName();
+    }
+    return null;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupEntity.java
index 065a073..225b2cc 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupEntity.java
@@ -26,6 +26,7 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
 import javax.persistence.ManyToOne;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
@@ -73,8 +74,20 @@ public class ConfigGroupEntity {
   @Column(name = "create_timestamp", nullable=false, insertable=true, updatable=false)
   private long timestamp;
 
-  @Column(name = "service_name")
-  private String serviceName;
+  @Column(name = "service_id", nullable = false, insertable = false, updatable = false)
+  private Long serviceId;
+
+  @Column(name = "service_group_id", nullable = false, insertable = false, updatable = false)
+  private Long serviceGroupId;
+
+  @ManyToOne
+  @JoinColumns(
+      {
+          @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false, insertable = false, updatable = false),
+          @JoinColumn(name = "service_group_id", referencedColumnName = "service_group_id", nullable = false),
+          @JoinColumn(name = "service_id", referencedColumnName = "id", nullable = false)
+      })
+  private ClusterServiceEntity clusterServiceEntity;
 
   @ManyToOne
   @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false)
@@ -182,11 +195,27 @@ public class ConfigGroupEntity {
     return result;
   }
 
-  public String getServiceName() {
-    return serviceName;
+  public Long getServiceId() {
+    return serviceId;
+  }
+
+  public void setServiceId(Long serviceId) {
+    this.serviceId = serviceId;
+  }
+
+  public Long getServiceGroupId() {
+    return serviceGroupId;
+  }
+
+  public void setServiceGroupId(Long serviceGroupId) {
+    this.serviceGroupId = serviceGroupId;
+  }
+
+  public ClusterServiceEntity getClusterServiceEntity() {
+    return clusterServiceEntity;
   }
 
-  public void setServiceName(String serviceName) {
-    this.serviceName = serviceName;
+  public void setClusterServiceEntity(ClusterServiceEntity clusterServiceEntity) {
+    this.clusterServiceEntity = clusterServiceEntity;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntity.java
index eea4a40..76708f9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntity.java
@@ -21,6 +21,9 @@ import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.IdClass;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.ManyToOne;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.Table;
@@ -40,7 +43,7 @@ import javax.persistence.Table;
 public class HostConfigMappingEntity {
 
   @Id
-  @Column(name = "cluster_id", insertable = true, updatable = false, nullable = false)
+  @Column(name = "cluster_id", insertable = false, updatable = false, nullable = false)
   private Long clusterId;
 
   @Id
@@ -58,8 +61,20 @@ public class HostConfigMappingEntity {
   @Column(name = "version_tag", insertable = true, updatable = false, nullable = false)
   private String versionTag;
 
-  @Column(name = "service_name", insertable = true, updatable = true)
-  private String serviceName;
+  @Column(name = "service_id", insertable = false, updatable = false, nullable = false)
+  private Long serviceId;
+
+  @Column(name = "service_group_id", insertable = false, updatable = false, nullable = false)
+  private Long serviceGroupId;
+
+  @ManyToOne
+  @JoinColumns(
+      {
+          @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false),
+          @JoinColumn(name = "service_group_id", referencedColumnName = "service_group_id", nullable = false),
+          @JoinColumn(name = "service_id", referencedColumnName = "id", nullable = false)
+      })
+  private ClusterServiceEntity clusterServiceEntity;
 
   @Column(name = "selected", insertable = true, updatable = true, nullable = false)
   private int selected = 0;
@@ -115,14 +130,6 @@ public class HostConfigMappingEntity {
     this.selected = selected;
   }
 
-  public String getServiceName() {
-    return serviceName;
-  }
-
-  public void setServiceName(String name) {
-    serviceName = name;
-  }
-  
   /**
    * @return the user
    */
@@ -149,7 +156,8 @@ public class HostConfigMappingEntity {
     if (createTimestamp != null ? !createTimestamp.equals(that.createTimestamp) : that.createTimestamp != null)
       return false;
     if (hostId != null ? !hostId.equals(that.hostId) : that.hostId != null) return false;
-    if (serviceName != null ? !serviceName.equals(that.serviceName) : that.serviceName != null) return false;
+    if (serviceGroupId != null ? !serviceGroupId.equals(that.serviceGroupId) : that.serviceGroupId != null) return false;
+    if (serviceId != null ? !serviceId.equals(that.serviceId) : that.serviceId != null) return false;
     if (type != null ? !type.equals(that.type) : that.type != null) return false;
     if (user != null ? !user.equals(that.user) : that.user != null) return false;
     if (versionTag != null ? !versionTag.equals(that.versionTag) : that.versionTag != null) return false;
@@ -164,9 +172,34 @@ public class HostConfigMappingEntity {
     result = 31 * result + (type != null ? type.hashCode() : 0);
     result = 31 * result + (createTimestamp != null ? createTimestamp.hashCode() : 0);
     result = 31 * result + (versionTag != null ? versionTag.hashCode() : 0);
-    result = 31 * result + (serviceName != null ? serviceName.hashCode() : 0);
+    result = 31 * result + (serviceGroupId != null ? serviceGroupId.hashCode() : 0);
+    result = 31 * result + (serviceId != null ? serviceId.hashCode() : 0);
     result = 31 * result + selected;
     result = 31 * result + (user != null ? user.hashCode() : 0);
     return result;
   }
+
+  public Long getServiceId() {
+    return serviceId;
+  }
+
+  public void setServiceId(Long serviceId) {
+    this.serviceId = serviceId;
+  }
+
+  public Long getServiceGroupId() {
+    return serviceGroupId;
+  }
+
+  public void setServiceGroupId(Long serviceGroupId) {
+    this.serviceGroupId = serviceGroupId;
+  }
+
+  public ClusterServiceEntity getClusterServiceEntity() {
+    return clusterServiceEntity;
+  }
+
+  public void setClusterServiceEntity(ClusterServiceEntity clusterServiceEntity) {
+    this.clusterServiceEntity = clusterServiceEntity;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceConfigEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceConfigEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceConfigEntity.java
index b1409ed..7cd7e98 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceConfigEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ServiceConfigEntity.java
@@ -29,6 +29,7 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
 import javax.persistence.ManyToOne;
@@ -51,19 +52,19 @@ import javax.persistence.TableGenerator;
         query = "SELECT serviceConfig FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.clusterId=:clusterId ORDER BY serviceConfig.version DESC"),
     @NamedQuery(
         name = "ServiceConfigEntity.findNextServiceConfigVersion",
-        query = "SELECT COALESCE(MAX(serviceConfig.version), 0) + 1 AS nextVersion FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.serviceName=:serviceName AND serviceConfig.clusterId=:clusterId"),
+        query = "SELECT COALESCE(MAX(serviceConfig.version), 0) + 1 AS nextVersion FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.serviceId=:serviceId AND serviceConfig.clusterId=:clusterId"),
     @NamedQuery(
         name = "ServiceConfigEntity.findServiceConfigsByStack",
-        query = "SELECT serviceConfig FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.clusterId=:clusterId AND serviceConfig.stack=:stack AND serviceConfig.serviceName=:serviceName"),
+        query = "SELECT serviceConfig FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.clusterId=:clusterId AND serviceConfig.stack=:stack AND serviceConfig.serviceId=:serviceId"),
     @NamedQuery(
         name = "ServiceConfigEntity.findLatestServiceConfigsByStack",
-        query = "SELECT serviceConfig FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.clusterId = :clusterId AND (serviceConfig.groupId = null OR serviceConfig.groupId IN (SELECT cg.groupId from ConfigGroupEntity cg)) AND serviceConfig.version = (SELECT MAX(serviceConfig2.version) FROM ServiceConfigEntity serviceConfig2 WHERE serviceConfig2.clusterId= :clusterId AND serviceConfig2.stack = :stack AND serviceConfig2.serviceName = serviceConfig.serviceName)"),
+        query = "SELECT serviceConfig FROM ServiceConfigEntity serviceConfig WHERE serviceConfig.clusterId = :clusterId AND (serviceConfig.groupId = null OR serviceConfig.groupId IN (SELECT cg.groupId from ConfigGroupEntity cg)) AND serviceConfig.version = (SELECT MAX(serviceConfig2.version) FROM ServiceConfigEntity serviceConfig2 WHERE serviceConfig2.clusterId= :clusterId AND serviceConfig2.stack = :stack AND serviceConfig2.serviceId = serviceConfig.serviceId)"),
     @NamedQuery(
         name = "ServiceConfigEntity.findLatestServiceConfigsByService",
-        query = "SELECT scv FROM ServiceConfigEntity scv WHERE scv.clusterId = :clusterId AND scv.serviceName = :serviceName AND (scv.groupId = null OR scv.groupId IN (SELECT cg.groupId from ConfigGroupEntity cg)) AND scv.version = (SELECT MAX(scv2.version) FROM ServiceConfigEntity scv2 WHERE (scv2.serviceName = :serviceName AND scv2.clusterId = :clusterId) AND (scv2.groupId = scv.groupId OR (scv2.groupId IS NULL AND scv.groupId IS NULL)))"),
+        query = "SELECT scv FROM ServiceConfigEntity scv WHERE scv.clusterId = :clusterId AND scv.serviceId = :serviceId AND (scv.groupId = null OR scv.groupId IN (SELECT cg.groupId from ConfigGroupEntity cg)) AND scv.version = (SELECT MAX(scv2.version) FROM ServiceConfigEntity scv2 WHERE (scv2.serviceId = :serviceId AND scv2.clusterId = :clusterId) AND (scv2.groupId = scv.groupId OR (scv2.groupId IS NULL AND scv.groupId IS NULL)))"),
     @NamedQuery(
         name = "ServiceConfigEntity.findLatestServiceConfigsByCluster",
-        query = "SELECT scv FROM ServiceConfigEntity scv WHERE scv.clusterId = :clusterId AND scv.serviceConfigId IN (SELECT MAX(scv1.serviceConfigId) FROM ServiceConfigEntity scv1 WHERE (scv1.clusterId = :clusterId) AND (scv1.groupId IS NULL) GROUP BY scv1.serviceName)") })
+        query = "SELECT scv FROM ServiceConfigEntity scv WHERE scv.clusterId = :clusterId AND scv.serviceConfigId IN (SELECT MAX(scv1.serviceConfigId) FROM ServiceConfigEntity scv1 WHERE (scv1.clusterId = :clusterId) AND (scv1.groupId IS NULL) GROUP BY scv1.serviceId)") })
 public class ServiceConfigEntity {
   @Id
   @Column(name = "service_config_id")
@@ -75,8 +76,21 @@ public class ServiceConfigEntity {
   private Long clusterId;
 
   @Basic
-  @Column(name = "service_name", nullable = false)
-  private String serviceName;
+  @Column(name = "service_id", nullable = false, insertable = false, updatable = false)
+  private Long serviceId;
+
+  @Basic
+  @Column(name = "service_group_id", nullable = false, insertable = false, updatable = false)
+  private Long serviceGroupId;
+
+  @ManyToOne
+  @JoinColumns(
+      {
+          @JoinColumn(name = "cluster_id", referencedColumnName = "cluster_id", nullable = false, insertable = false, updatable = false),
+          @JoinColumn(name = "service_group_id", referencedColumnName = "service_group_id", nullable = false),
+          @JoinColumn(name = "service_id", referencedColumnName = "id", nullable = false)
+      })
+  private ClusterServiceEntity clusterServiceEntity;
 
   @Basic
   @Column(name = "group_id", nullable = true)
@@ -135,12 +149,12 @@ public class ServiceConfigEntity {
     this.serviceConfigId = serviceConfigId;
   }
 
-  public String getServiceName() {
-    return serviceName;
+  public Long getServiceId() {
+    return serviceId;
   }
 
-  public void setServiceName(String serviceName) {
-    this.serviceName = serviceName;
+  public void setServiceId(Long serviceId) {
+    this.serviceId = serviceId;
   }
 
   public Long getVersion() {
@@ -275,4 +289,34 @@ public class ServiceConfigEntity {
     }
     return true;
   }
+
+  public Long getServiceGroupId() {
+    return serviceGroupId;
+  }
+
+  public void setServiceGroupId(Long serviceGroupId) {
+    this.serviceGroupId = serviceGroupId;
+  }
+
+  public ClusterServiceEntity getClusterServiceEntity() {
+    return clusterServiceEntity;
+  }
+
+  public void setClusterServiceEntity(ClusterServiceEntity clusterServiceEntity) {
+    this.clusterServiceEntity = clusterServiceEntity;
+  }
+
+  public String getServiceGroupName() {
+    if (clusterServiceEntity != null) {
+      return clusterServiceEntity.getServiceGroupName();
+    }
+    return null;
+  }
+
+  public String getServiceName() {
+    if (clusterServiceEntity != null) {
+      return clusterServiceEntity.getServiceName();
+    }
+    return null;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
index f15a507..6bb248e 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/ConfigureAction.java
@@ -181,7 +181,7 @@ public class ConfigureAction extends AbstractUpgradeServerAction {
 
     // such as hdfs-site or hbase-env
     String configType = commandParameters.get(ConfigureTask.PARAMETER_CONFIG_TYPE);
-    String serviceName = cluster.getServiceByConfigType(configType);
+    String serviceName = cluster.getServiceByConfigType(configType).getName();
 
     // !!! we couldn't get the service based on its config type, so try the associated
     if (StringUtils.isBlank(serviceName)) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
index f0ae0c8..7380cba 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FinalizeUpgradeAction.java
@@ -325,8 +325,8 @@ public class FinalizeUpgradeAction extends AbstractUpgradeServerAction {
           outSB.append(
               String.format("Removing %s configurations for %s", sourceStackId,
                   serviceName)).append(System.lineSeparator());
-
-          cluster.removeConfigurations(sourceStackId, serviceName);
+          //TODO pass serviceGroupName
+          cluster.removeConfigurations(sourceStackId, cluster.getService(null, serviceName).getServiceId());
         }
       }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/PreconfigureKerberosAction.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/PreconfigureKerberosAction.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/PreconfigureKerberosAction.java
index 5af7c6b..02cb70a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/PreconfigureKerberosAction.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/PreconfigureKerberosAction.java
@@ -462,7 +462,7 @@ public class PreconfigureKerberosAction extends AbstractUpgradeServerAction {
       for (Map.Entry<String, Map<String, String>> entry : kerberosConfigurations.entrySet()) {
         String configType = entry.getKey();
 
-        String service = cluster.getServiceByConfigType(configType);
+        String service = cluster.getServiceByConfigType(configType).getName();
         Set<String> allowedProperties = propertyFilter.get(configType);
 
         // Update properties for services that are installed and not filtered out

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
index 2193c3a..27d37e3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
@@ -98,6 +98,7 @@ public interface Cluster {
 
   ServiceGroup addServiceGroupDependency(String serviceGroupName, String dependencyServiceGroupName) throws AmbariException;
 
+  //TODO remove when UI starts using service groups
   /**
    * Get a service
    *
@@ -106,6 +107,8 @@ public interface Cluster {
    */
   Service getService(String serviceName) throws AmbariException;
 
+  Service getService(String serviceGroupName, String serviceName) throws AmbariException;
+
   Service getService(Long serviceId) throws AmbariException;
 
   /**
@@ -383,7 +386,7 @@ public interface Cluster {
    * @return <code>true</code> if the config was added, or <code>false</code>
    * if the config is already set as the current
    */
-  ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs);
+  ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs) throws AmbariException;
 
   /**
    * Adds and sets a DESIRED configuration to be applied to a cluster.  There
@@ -395,24 +398,24 @@ public interface Cluster {
    * @return <code>true</code> if the config was added, or <code>false</code>
    * if the config is already set as the current
    */
-  ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs, String serviceConfigVersionNote);
+  ServiceConfigVersionResponse addDesiredConfig(String user, Set<Config> configs, String serviceConfigVersionNote) throws AmbariException;
 
-  ServiceConfigVersionResponse createServiceConfigVersion(String serviceName, String user, String note,
-                                                          ConfigGroup configGroup);
+  ServiceConfigVersionResponse createServiceConfigVersion(Long serviceId, String user, String note,
+                                                          ConfigGroup configGroup) throws AmbariException;
 
-  String getServiceForConfigTypes(Collection<String> configTypes);
+  Long getServiceForConfigTypes(Collection<String> configTypes);
 
   /**
    * Apply specified service config version (rollback)
    *
-   * @param serviceName service name
+   * @param serviceId   service Id
    * @param version     service config version
    * @param user        the user making the change for audit purposes
    * @param note
    * @return service config version created
    * @throws AmbariException
    */
-  ServiceConfigVersionResponse setServiceConfigVersion(String serviceName, Long version, String user, String note) throws AmbariException;
+  ServiceConfigVersionResponse setServiceConfigVersion(Long serviceId, Long version, String user, String note) throws AmbariException;
 
   /**
    * Get currently active service config versions for stack services
@@ -424,10 +427,10 @@ public interface Cluster {
   /**
    * Get active service config version responses for all config groups of a service
    *
-   * @param serviceName service name
+   * @param serviceId service ID
    * @return
    */
-  List<ServiceConfigVersionResponse> getActiveServiceConfigVersionResponse(String serviceName);
+  List<ServiceConfigVersionResponse> getActiveServiceConfigVersionResponse(Long serviceId);
 
   /**
    * Get service config version history
@@ -670,18 +673,18 @@ public interface Cluster {
    *
    * @param stackId     the stack to use when finding the latest configurations (not
    *                    {@code null}).
-   * @param serviceName the service to modify configurations for (not {@code null}).
+   * @param serviceId the service to modify configurations for (not {@code null}).
    */
-  void applyLatestConfigurations(StackId stackId, String serviceName);
+  void applyLatestConfigurations(StackId stackId, Long serviceId);
 
   /**
    * Removes all configurations for the specified service and stack.
    *
    * @param stackId     the stack to use when finding the configurations to remove (not
    *                    {@code null}).
-   * @param serviceName the service to rmeove configurations for (not {@code null}).
+   * @param serviceId the service to remove configurations for (not {@code null}).
    */
-  void removeConfigurations(StackId stackId, String serviceName);
+  void removeConfigurations(StackId stackId, Long serviceId);
 
   /**
    * Returns whether this cluster was provisioned by a Blueprint or not.
@@ -717,13 +720,13 @@ public interface Cluster {
   boolean isUpgradeSuspended();
 
   /**
-   * Returns the name of the service that the passed config type belongs to.
+   * Returns the service that the passed config type belongs to.
    *
    * @param configType the config type to look up the service by
-   * @return returns the name of the service that the config type belongs to if
+   * @return returns the service that the config type belongs to if
    * there is any otherwise returns null.
    */
-  String getServiceByConfigType(String configType);
+  Service getServiceByConfigType(String configType);
 
   /**
    * Gets the most recent value of {@code cluster-env/propertyName} where

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
index d729cb8..8c0e656 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ConfigHelper.java
@@ -1117,7 +1117,7 @@ public class ConfigHelper {
       AmbariManagementController controller, Map<String, Map<String, String>> batchProperties,
       String authenticatedUserName, String serviceVersionNote) throws AmbariException {
 
-    Map<String, Set<Config>> serviceMapped = new HashMap<>();
+    Map<Long, Set<Config>> serviceMapped = new HashMap<>();
 
     for (Map.Entry<String, Map<String, String>> entry : batchProperties.entrySet()) {
       String type = entry.getKey();
@@ -1128,7 +1128,7 @@ public class ConfigHelper {
 
       if (null != baseConfig) {
         try {
-          String service = cluster.getServiceForConfigTypes(Collections.singleton(type));
+          Long service = cluster.getServiceForConfigTypes(Collections.singleton(type));
           if (!serviceMapped.containsKey(service)) {
             serviceMapped.put(service, new HashSet<>());
           }

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/DesiredConfig.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/DesiredConfig.java b/ambari-server/src/main/java/org/apache/ambari/server/state/DesiredConfig.java
index 4fad21d..9c0d32d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/DesiredConfig.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/DesiredConfig.java
@@ -33,7 +33,8 @@ import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
 public class DesiredConfig {
 
   private String tag;
-  private String serviceName;
+  private Long serviceId;
+  private Long serviceGroupId;
   private Long version;
   private List<HostOverride> hostOverrides = new ArrayList<>();
 
@@ -55,21 +56,39 @@ public class DesiredConfig {
   }
 
   /**
-   * Gets the service name (if any) for the desired config.
-   * @return the service name
+   * Gets the service id (if any) for the desired config.
+   * @return the service id
    */
   @JsonSerialize(include = Inclusion.NON_NULL)
-  @JsonProperty("service_name")
-  public String getServiceName() {
-    return serviceName;
+  @JsonProperty("service_id")
+  public Long getServiceId() {
+    return serviceId;
   }
 
   /**
-   * Sets the service name (if any) for the desired config.
-   * @param name the service name
+   * Sets the service id (if any) for the desired config.
+   * @param serviceId the service id
    */
-  public void setServiceName(String name) {
-    serviceName = name;
+  public void setServiceId(Long serviceId) {
+    this.serviceId = serviceId;
+  }
+
+  /**
+   * Gets the service group id (if any) for the desired config.
+   * @return the service group id
+   */
+  @JsonSerialize(include = Inclusion.NON_NULL)
+  @JsonProperty("service_group_id")
+  public Long getServiceGroupId() {
+    return serviceGroupId;
+  }
+
+  /**
+   * Sets the service group id (if any) for the desired config.
+   * @param serviceGroupId the service group id
+   */
+  public void setServiceGroupId(Long serviceGroupId) {
+    this.serviceGroupId = serviceGroupId;
   }
   
   /**
@@ -160,8 +179,10 @@ public class DesiredConfig {
     StringBuilder sb = new StringBuilder();
     sb.append("{");
     sb.append("tag=").append(tag);
-    if (null != serviceName)
-      sb.append(", service=").append(serviceName);
+    if (null != serviceGroupId)
+      sb.append(", serviceGroupId=").append(serviceGroupId);
+    if (null != serviceId)
+      sb.append(", serviceId=").append(serviceId);
     if (null != hostOverrides && hostOverrides.size() > 0) {
       sb.append(", hosts=[");
       int i = 0;
@@ -189,7 +210,8 @@ public class DesiredConfig {
 
     return new EqualsBuilder()
       .append(tag, that.tag)
-      .append(serviceName, that.serviceName)
+      .append(serviceGroupId, that.serviceGroupId)
+      .append(serviceId, that.serviceId)
       .append(version, that.version)
       .append(hostOverrides, that.hostOverrides)
       .isEquals();
@@ -199,7 +221,8 @@ public class DesiredConfig {
   public int hashCode() {
     return new HashCodeBuilder(17, 37)
       .append(tag)
-      .append(serviceName)
+      .append(serviceGroupId)
+      .append(serviceId)
       .append(version)
       .append(hostOverrides)
       .toHashCode();

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
index 0321701..bfb04be 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
@@ -512,6 +512,15 @@ public class ServiceImpl implements Service {
     return isCredentialStoreRequired;
   }
 
+  @Override
+  public String toString() {
+    return "ServiceImpl{" +
+        "serviceId=" + serviceId +
+        ", serviceName='" + serviceName + '\'' +
+        ", displayName='" + displayName + '\'' +
+        ", serviceType='" + serviceType + '\'' +
+        '}';
+  }
 
   /**
    * Get a true or false value specifying whether
@@ -635,7 +644,7 @@ public class ServiceImpl implements Service {
   @Transactional
   void deleteAllServiceConfigs() throws AmbariException {
     long clusterId = getClusterId();
-    ServiceConfigEntity lastServiceConfigEntity = serviceConfigDAO.findMaxVersion(clusterId, getName());
+    ServiceConfigEntity lastServiceConfigEntity = serviceConfigDAO.findMaxVersion(clusterId, getServiceId());
     // de-select every configuration from the service
     if (lastServiceConfigEntity != null) {
       for (ClusterConfigEntity serviceConfigEntity : lastServiceConfigEntity.getClusterConfigEntities()) {
@@ -649,7 +658,7 @@ public class ServiceImpl implements Service {
     LOG.info("Deleting all configuration associations for {} on cluster {}", getName(), cluster.getClusterName());
 
     List<ServiceConfigEntity> serviceConfigEntities =
-      serviceConfigDAO.findByService(cluster.getClusterId(), getName());
+      serviceConfigDAO.findByService(cluster.getClusterId(), getServiceId());
 
     for (ServiceConfigEntity serviceConfigEntity : serviceConfigEntities) {
       // Only delete the historical version information and not original

http://git-wip-us.apache.org/repos/asf/ambari/blob/527c4a2d/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
index 510072a..a6c6507 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/UpgradeHelper.java
@@ -972,7 +972,8 @@ public class UpgradeHelper {
 
       // downgrade is easy - just remove the new and make the old current
       if (direction == Direction.DOWNGRADE) {
-        cluster.applyLatestConfigurations(targetStackId, serviceName);
+        //TODO pass serviceGroupName
+        cluster.applyLatestConfigurations(targetStackId, cluster.getService(null, serviceName).getServiceId());
         continue;
       }
 
@@ -1001,8 +1002,10 @@ public class UpgradeHelper {
 
       // find the current, existing configurations for the service
       List<Config> existingServiceConfigs = new ArrayList<>();
+
       List<ServiceConfigEntity> latestServiceConfigs = m_serviceConfigDAO.getLastServiceConfigsForService(
-          cluster.getClusterId(), serviceName);
+          //TODO pass serviceGroupName
+          cluster.getClusterId(), cluster.getService(null, serviceName).getServiceId());
 
       for (ServiceConfigEntity serviceConfig : latestServiceConfigs) {
         List<ClusterConfigEntity> existingConfigurations = serviceConfig.getClusterConfigEntities();