AMBARI-10679. Full Delete of Host : Switch config related tables to use host_id instead of host_name column (alejandro)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/290276c6 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/290276c6 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/290276c6 Branch: refs/heads/trunk Commit: 290276c62152f331e0859c1d81534cc160471327 Parents: 9096775 Author: Alejandro Fernandez <afernan...@hortonworks.com> Authored: Wed Apr 22 19:06:43 2015 -0700 Committer: Alejandro Fernandez <afernan...@hortonworks.com> Committed: Thu Apr 23 16:16:30 2015 -0700 ---------------------------------------------------------------------- .../ServiceConfigVersionResponse.java | 17 +- .../internal/ConfigGroupResourceProvider.java | 35 ++- .../internal/UpgradeResourceProvider.java | 25 ++- .../orm/cache/ConfigGroupHostMapping.java | 4 +- .../orm/cache/ConfigGroupHostMappingImpl.java | 43 ++-- .../server/orm/cache/HostConfigMapping.java | 8 +- .../server/orm/cache/HostConfigMappingImpl.java | 54 ++--- .../orm/dao/ConfigGroupHostMappingDAO.java | 46 ++-- .../server/orm/dao/HostConfigMappingDAO.java | 193 +++++++++-------- .../apache/ambari/server/orm/dao/HostDAO.java | 11 + .../entities/ConfigGroupHostMappingEntity.java | 24 ++- .../ConfigGroupHostMappingEntityPK.java | 16 +- .../orm/entities/HostConfigMappingEntity.java | 18 +- .../orm/entities/HostConfigMappingEntityPK.java | 20 +- .../orm/entities/ServiceConfigEntity.java | 12 +- .../org/apache/ambari/server/state/Cluster.java | 6 +- .../apache/ambari/server/state/Clusters.java | 17 ++ .../server/state/cluster/ClusterImpl.java | 77 ++++--- .../server/state/cluster/ClustersImpl.java | 54 ++++- .../server/state/configgroup/ConfigGroup.java | 6 +- .../state/configgroup/ConfigGroupFactory.java | 2 +- .../state/configgroup/ConfigGroupImpl.java | 38 ++-- .../ambari/server/state/host/HostImpl.java | 31 ++- .../server/upgrade/UpgradeCatalog210.java | 51 ++++- .../main/resources/Ambari-DDL-MySQL-CREATE.sql | 27 +-- .../main/resources/Ambari-DDL-Oracle-CREATE.sql | 25 +-- .../resources/Ambari-DDL-Postgres-CREATE.sql | 25 +-- .../Ambari-DDL-Postgres-EMBEDDED-CREATE.sql | 25 +-- .../resources/Ambari-DDL-SQLServer-CREATE.sql | 21 +- .../AmbariManagementControllerTest.java | 10 +- .../ConfigGroupResourceProviderTest.java | 98 +++++++-- .../server/orm/dao/ConfigGroupDAOTest.java | 22 +- .../orm/dao/HostConfigMappingDAOTest.java | 87 ++++---- .../ambari/server/state/ConfigGroupTest.java | 9 +- .../ambari/server/state/ConfigHelperTest.java | 6 +- .../ambari/server/state/UpgradeHelperTest.java | 5 +- .../server/state/cluster/ClusterTest.java | 97 ++++++--- .../svccomphost/ServiceComponentHostTest.java | 214 ++++++++++--------- .../ambari/server/upgrade/UpgradeTest.java | 13 +- 39 files changed, 867 insertions(+), 625 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 9a07a29..c02bb6e 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 @@ -21,6 +21,9 @@ package org.apache.ambari.server.controller; import java.util.List; +import com.google.inject.Inject; +import org.apache.ambari.server.StaticallyInject; +import org.apache.ambari.server.orm.dao.HostDAO; import org.apache.ambari.server.orm.entities.ClusterEntity; import org.apache.ambari.server.orm.entities.ServiceConfigEntity; import org.apache.ambari.server.orm.entities.StackEntity; @@ -28,6 +31,7 @@ import org.apache.ambari.server.state.StackId; import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.map.annotate.JsonSerialize; +@StaticallyInject public class ServiceConfigVersionResponse { @JsonProperty("cluster_name") @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) @@ -72,15 +76,14 @@ public class ServiceConfigVersionResponse { @JsonProperty("hosts") private final List<String> hosts; + @Inject + private static HostDAO hostDAO; + /** * Constructor. * - * @param clusterName - * @param serviceName - * @param version - * @param isCurrent - * @param isCompatibleWithCurrentStack - * @param configurations + * @param serviceConfigEntity + * @param configGroupName */ public ServiceConfigVersionResponse(ServiceConfigEntity serviceConfigEntity, String configGroupName) { @@ -95,7 +98,7 @@ public class ServiceConfigVersionResponse { note = serviceConfigEntity.getNote(); groupId = (null != serviceConfigEntity.getGroupId() ? serviceConfigEntity.getGroupId(): -1L); groupName = configGroupName; - hosts = serviceConfigEntity.getHostNames(); + hosts = hostDAO.getHostNamesByHostIds(serviceConfigEntity.getHostIds()); StackEntity serviceConfigStackEntity = serviceConfigEntity.getStack(); StackEntity clusterStackEntity = clusterEntity.getClusterStateEntity().getCurrentStack(); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 3fcb84b..68bad38 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 @@ -17,12 +17,14 @@ */ package org.apache.ambari.server.controller.internal; +import com.google.inject.Inject; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.ClusterNotFoundException; import org.apache.ambari.server.ConfigGroupNotFoundException; import org.apache.ambari.server.DuplicateResourceException; import org.apache.ambari.server.HostNotFoundException; import org.apache.ambari.server.ParentObjectNotFoundException; +import org.apache.ambari.server.StaticallyInject; import org.apache.ambari.server.controller.AmbariManagementController; import org.apache.ambari.server.controller.ConfigGroupRequest; import org.apache.ambari.server.controller.ConfigGroupResponse; @@ -37,6 +39,8 @@ import org.apache.ambari.server.controller.spi.ResourcePredicateEvaluator; import org.apache.ambari.server.controller.spi.SystemException; import org.apache.ambari.server.controller.spi.UnsupportedPropertyException; import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.apache.ambari.server.orm.dao.HostDAO; +import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Config; @@ -48,13 +52,16 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; +@StaticallyInject public class ConfigGroupResourceProvider extends AbstractControllerResourceProvider implements ResourcePredicateEvaluator { @@ -86,6 +93,9 @@ public class ConfigGroupResourceProvider extends private static Set<String> pkPropertyIds = new HashSet<String>(Arrays .asList(new String[] { CONFIGGROUP_ID_PROPERTY_ID })); + @Inject + private static HostDAO hostDAO; + /** * Create a new resource provider for the given management controller. * @@ -343,10 +353,11 @@ public class ConfigGroupResourceProvider extends // Has tag if (configGroup.getTag().equals(request.getTag())) { // Has a match with hosts - Set<String> groupHosts = new HashSet<String>(configGroup - .getHosts().keySet()); - groupHosts.retainAll(request.getHosts()); - if (!groupHosts.isEmpty()) { + List<Long> groupHostIds = new ArrayList<Long>(configGroup.getHosts().keySet()); + Set<String> groupHostNames = new HashSet<String>(hostDAO.getHostNamesByHostIds(groupHostIds)); + + groupHostNames.retainAll(request.getHosts()); + if (!groupHostNames.isEmpty()) { responses.add(configGroup.convertToResponse()); } } @@ -362,7 +373,7 @@ public class ConfigGroupResourceProvider extends return responses; } - private void verifyHostList(Cluster cluster, Map<String, Host> hosts, + private void verifyHostList(Cluster cluster, Map<Long, Host> hosts, ConfigGroupRequest request) throws AmbariException { Map<Long, ConfigGroup> configGroupMap = cluster.getConfigGroups(); @@ -472,14 +483,15 @@ public class ConfigGroupResourceProvider extends } // Find hosts - Map<String, Host> hosts = new HashMap<String, Host>(); + Map<Long, Host> hosts = new HashMap<Long, Host>(); if (request.getHosts() != null && !request.getHosts().isEmpty()) { for (String hostname : request.getHosts()) { Host host = clusters.getHost(hostname); - if (host == null) { + HostEntity hostEntity = hostDAO.findByName(hostname); + if (host == null || hostEntity == null) { throw new HostNotFoundException(hostname); } - hosts.put(hostname, host); + hosts.put(hostEntity.getHostId(), host); } } @@ -568,14 +580,15 @@ public class ConfigGroupResourceProvider extends } // Update hosts - Map<String, Host> hosts = new HashMap<String, Host>(); + Map<Long, Host> hosts = new HashMap<Long, Host>(); if (request.getHosts() != null && !request.getHosts().isEmpty()) { for (String hostname : request.getHosts()) { Host host = clusters.getHost(hostname); - if (host == null) { + HostEntity hostEntity = hostDAO.findByName(hostname); + if (host == null || hostEntity == null) { throw new HostNotFoundException(hostname); } - hosts.put(hostname, host); + hosts.put(hostEntity.getHostId(), host); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java index 730fba5..7de7348 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java @@ -23,6 +23,7 @@ import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.VERSION; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -59,11 +60,13 @@ import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.controller.spi.ResourceAlreadyExistsException; import org.apache.ambari.server.controller.spi.SystemException; import org.apache.ambari.server.controller.spi.UnsupportedPropertyException; +import org.apache.ambari.server.orm.dao.HostDAO; import org.apache.ambari.server.orm.dao.HostRoleCommandDAO; import org.apache.ambari.server.orm.dao.HostRoleCommandStatusSummaryDTO; import org.apache.ambari.server.orm.dao.RepositoryVersionDAO; import org.apache.ambari.server.orm.dao.RequestDAO; import org.apache.ambari.server.orm.dao.UpgradeDAO; +import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.orm.entities.RequestEntity; import org.apache.ambari.server.orm.entities.StackEntity; @@ -170,6 +173,10 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider @Inject private static HostRoleCommandDAO s_hostRoleCommandDAO = null; + @Inject + private static HostDAO s_hostDAO = null; + + /** * Used to generated the correct tasks and stages during an upgrade. */ @@ -901,6 +908,19 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider String itemDetail = entity.getText(); String stageText = StringUtils.abbreviate(entity.getText(), 255); + String hostName = null; + Collection<Long> hostIds = cluster.getAllHostsDesiredConfigs().keySet(); + if (!hostIds.isEmpty()) { + Long hostId = hostIds.iterator().next(); + HostEntity hostEntity = s_hostDAO.findById(hostId); + if (hostEntity != null) { + hostName = hostEntity.getHostName(); + } + } + if (StringUtils.isBlank(hostName)) { + throw new AmbariException("Could not retrieve an arbitrary host name to use for the server-side command."); + } + switch (task.getType()) { case MANUAL: { ManualTask mt = (ManualTask) task; @@ -980,15 +1000,12 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider stage.setStageId(stageId); entity.setStageId(Long.valueOf(stageId)); - // !!! hack hack hack - String host = cluster.getAllHostsDesiredConfigs().keySet().iterator().next(); - stage.addServerActionCommand(task.getImplementationClass(), getManagementController().getAuthName(), Role.AMBARI_SERVER_ACTION, RoleCommand.EXECUTE, cluster.getClusterName(), - host, + hostName, new ServiceComponentHostServerActionEvent(StageUtils.getHostName(), System.currentTimeMillis()), commandParams, itemDetail, http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java index 5c26a6c..ae3f076 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java @@ -24,12 +24,12 @@ import org.apache.ambari.server.state.configgroup.ConfigGroup; public interface ConfigGroupHostMapping { public Long getConfigGroupId(); - public String getHostname(); + public Long getHostId(); public Host getHost(); public ConfigGroup getConfigGroup(); public void setConfigGroupId(Long configGroupId); - public void setHostname(String hostname); + public void setHostId(Long setHostId); public void setHost(Host host); public void setConfigGroup(ConfigGroup configGroup); } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java index 54e1ca0..741b02e 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java @@ -24,7 +24,7 @@ import org.apache.ambari.server.state.configgroup.ConfigGroup; public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping { private Long configGroupId; - private String hostname; + private Long hostId; private Host host; private ConfigGroup configGroup; @@ -34,8 +34,8 @@ public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping { } @Override - public String getHostname() { - return hostname; + public Long getHostId() { + return hostId; } @Override @@ -51,25 +51,21 @@ public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping { @Override public void setConfigGroupId(Long configGroupId) { this.configGroupId = configGroupId; - } @Override - public void setHostname(String hostname) { - this.hostname = hostname; - + public void setHostId(Long hostId) { + this.hostId = hostId; } @Override public void setHost(Host host) { this.host = host; - } @Override public void setConfigGroup(ConfigGroup configGroup) { this.configGroup = configGroup; - } @Override @@ -82,7 +78,7 @@ public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping { prime * result + ((configGroupId == null) ? 0 : configGroupId.hashCode()); result = prime * result + ((host == null) ? 0 : host.hashCode()); - result = prime * result + ((hostname == null) ? 0 : hostname.hashCode()); + result = prime * result + ((hostId == null) ? 0 : hostId.hashCode()); return result; } @@ -95,27 +91,12 @@ public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping { if (getClass() != obj.getClass()) return false; ConfigGroupHostMappingImpl other = (ConfigGroupHostMappingImpl) obj; - if (configGroup == null) { - if (other.configGroup != null) - return false; - } else if (!configGroup.equals(other.configGroup)) - return false; - if (configGroupId == null) { - if (other.configGroupId != null) - return false; - } else if (!configGroupId.equals(other.configGroupId)) - return false; - if (host == null) { - if (other.host != null) - return false; - } else if (!host.equals(other.host)) - return false; - if (hostname == null) { - if (other.hostname != null) - return false; - } else if (!hostname.equals(other.hostname)) - return false; + + if (configGroup != null ? !configGroup.equals(other.configGroup) : other.configGroup != null) return false; + if (configGroupId != null ? !configGroupId.equals(other.configGroupId) : other.configGroupId != null) return false; + if (host != null ? !host.equals(other.host) : other.host != null) return false; + if (hostId != null ? !hostId.equals(other.hostId) : other.hostId != null) return false; + return true; } - } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 269daa9..d5afc15 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 @@ -21,9 +21,9 @@ public interface HostConfigMapping { public Long getClusterId(); public void setClusterId(Long clusterId); - - public String getHostName(); - public void setHostName(String hostName); + + public Long getHostId(); + public void setHostId(Long hostId); public String getType(); public void setType(String type); @@ -42,6 +42,4 @@ public interface HostConfigMapping { public Integer getSelected(); public void setSelected(Integer selected); - - } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 407aeb6..ab86c9b 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 @@ -22,7 +22,7 @@ package org.apache.ambari.server.orm.cache; public class HostConfigMappingImpl implements HostConfigMapping { private Long clusterId; - private String hostName; + private Long hostId; private String type; private Long createTimestamp; private String version; @@ -34,7 +34,7 @@ public class HostConfigMappingImpl implements HostConfigMapping { public HostConfigMappingImpl(HostConfigMapping entry) { setClusterId(entry.getClusterId()); - setHostName(entry.getHostName()); + setHostId(entry.getHostId()); setType(entry.getType()); setCreateTimestamp(entry.getCreateTimestamp()); setVersion(entry.getVersion()); @@ -57,17 +57,17 @@ public class HostConfigMappingImpl implements HostConfigMapping { throw new RuntimeException("ClusterId couldn't be null"); this.clusterId = clusterId; } - + @Override - public String getHostName() { - return hostName; + public Long getHostId() { + return hostId; } - + @Override - public void setHostName(String hostName) { - if (hostName == null) - throw new RuntimeException("HostName couldn't be null"); - this.hostName = hostName; + public void setHostId(Long hostId) { + if (hostId == null) + throw new RuntimeException("HostId couldn't be null"); + this.hostId = hostId; } @Override @@ -143,10 +143,8 @@ public class HostConfigMappingImpl implements HostConfigMapping { final int prime = 31; int result = 1; result = prime * result + ((clusterId == null) ? 0 : clusterId.hashCode()); - result = - prime * result - + ((createTimestamp == null) ? 0 : createTimestamp.hashCode()); - result = prime * result + ((hostName == null) ? 0 : hostName.hashCode()); + result = prime * result + ((createTimestamp == null) ? 0 : createTimestamp.hashCode()); + result = prime * result + ((hostId == null) ? 0 : hostId.hashCode()); result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; } @@ -159,29 +157,13 @@ public class HostConfigMappingImpl implements HostConfigMapping { return false; if (getClass() != obj.getClass()) return false; + HostConfigMappingImpl other = (HostConfigMappingImpl) obj; - if (clusterId == null) { - if (other.clusterId != null) - return false; - } else if (!clusterId.equals(other.clusterId)) - return false; - if (createTimestamp == null) { - if (other.createTimestamp != null) - return false; - } else if (!createTimestamp.equals(other.createTimestamp)) - return false; - if (hostName == null) { - if (other.hostName != null) - return false; - } else if (!hostName.equals(other.hostName)) - return false; - if (type == null) { - if (other.type != null) - return false; - } else if (!type.equals(other.type)) - return false; + if (clusterId != null ? !clusterId.equals(other.clusterId) : other.clusterId != null) return false; + if (createTimestamp != null ? !createTimestamp.equals(other.createTimestamp) : other.createTimestamp != null) return false; + if (hostId != null ? !hostId.equals(other.hostId) : other.hostId != null) return false; + if (type != null ? !type.equals(other.type) : other.type != null) return false; + return true; } - - } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ConfigGroupHostMappingDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ConfigGroupHostMappingDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ConfigGroupHostMappingDAO.java index 592679e..4628a4c 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ConfigGroupHostMappingDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ConfigGroupHostMappingDAO.java @@ -64,7 +64,7 @@ public class ConfigGroupHostMappingDAO { private final ReadWriteLock gl = new ReentrantReadWriteLock(); - private Map<String, Set<ConfigGroupHostMapping>> configGroupHostMappingByHost; + private Map<Long, Set<ConfigGroupHostMapping>> configGroupHostMappingByHost; private volatile boolean cacheLoaded; @@ -75,7 +75,7 @@ public class ConfigGroupHostMappingDAO { gl.writeLock().lock(); try { if (configGroupHostMappingByHost == null) { - configGroupHostMappingByHost = new WeakHashMap<String, Set<ConfigGroupHostMapping>>(); + configGroupHostMappingByHost = new WeakHashMap<Long, Set<ConfigGroupHostMapping>>(); TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider.get().createQuery( "SELECT entity FROM ConfigGroupHostMappingEntity entity", @@ -85,11 +85,11 @@ public class ConfigGroupHostMappingDAO { for (ConfigGroupHostMappingEntity configGroupHostMappingEntity : configGroupHostMappingEntities) { - Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get((configGroupHostMappingEntity.getHostname())); + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get((configGroupHostMappingEntity.getHostId())); if (setByHost == null) { setByHost = new HashSet<ConfigGroupHostMapping>(); - configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), setByHost); + configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostId(), setByHost); } ConfigGroupHostMapping configGroupHostMapping = buildConfigGroupHostMapping(configGroupHostMappingEntity); @@ -121,14 +121,14 @@ public class ConfigGroupHostMappingDAO { } @RequiresSession - public Set<ConfigGroupHostMapping> findByHost(String hostname) { + public Set<ConfigGroupHostMapping> findByHostId(Long hostId) { populateCache(); - if (!configGroupHostMappingByHost.containsKey(hostname)) + if (!configGroupHostMappingByHost.containsKey(hostId)) return null; - Set<ConfigGroupHostMapping> set = new HashSet<ConfigGroupHostMapping>(configGroupHostMappingByHost.get(hostname)); + Set<ConfigGroupHostMapping> set = new HashSet<ConfigGroupHostMapping>(configGroupHostMappingByHost.get(hostId)); return set; @@ -169,33 +169,29 @@ public class ConfigGroupHostMappingDAO { @Transactional public void create(ConfigGroupHostMappingEntity configGroupHostMappingEntity) { - populateCache(); entityManagerProvider.get().persist(configGroupHostMappingEntity); //create in cache - Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname()); + Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostId()); if (set == null){ set = new HashSet<ConfigGroupHostMapping>(); - configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), set); + configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostId(), set); } set.add(buildConfigGroupHostMapping(configGroupHostMappingEntity)); - } - - @Transactional public ConfigGroupHostMappingEntity merge(ConfigGroupHostMappingEntity configGroupHostMappingEntity) { populateCache(); - Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname()); + Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostId()); if (set == null){ set = new HashSet<ConfigGroupHostMapping>(); - configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), set); + configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostId(), set); } //Update object in set @@ -223,7 +219,7 @@ public class ConfigGroupHostMappingDAO { entityManagerProvider.get().remove(merge(configGroupHostMappingEntity)); - Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname()); + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostId()); if (setByHost != null) { CollectionUtils.filter(setByHost, new Predicate() { @@ -244,8 +240,7 @@ public class ConfigGroupHostMappingDAO { entityManagerProvider.get().remove(findByPK(configGroupHostMappingEntityPK)); - Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost - .get(configGroupHostMappingEntityPK.getHostname()); + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntityPK.getHostId()); if (setByHost != null) { CollectionUtils.filter(setByHost, new Predicate() { @@ -287,19 +282,17 @@ public class ConfigGroupHostMappingDAO { } @Transactional - public void removeAllByHost(String hostname) { + public void removeAllByHost(Long hostId) { TypedQuery<String> query = entityManagerProvider.get().createQuery ("DELETE FROM ConfigGroupHostMappingEntity confighosts WHERE " + - "confighosts.hostname = ?1", String.class); + "confighosts.hostId = ?1", String.class); - daoUtils.executeUpdate(query, hostname); + daoUtils.executeUpdate(query, hostId); - Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(hostname); + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(hostId); setByHost.clear(); - - } private ConfigGroupHostMapping buildConfigGroupHostMapping( @@ -309,13 +302,12 @@ public class ConfigGroupHostMappingDAO { configGroupHostMapping.setConfigGroup(buildConfigGroup(configGroupHostMappingEntity.getConfigGroupEntity())); configGroupHostMapping.setConfigGroupId(configGroupHostMappingEntity.getConfigGroupId()); configGroupHostMapping.setHost(buildHost(configGroupHostMappingEntity.getHostEntity())); - configGroupHostMapping.setHostname(configGroupHostMappingEntity.getHostname()); + configGroupHostMapping.setHostId(configGroupHostMappingEntity.getHostId()); return configGroupHostMapping; } private ConfigGroup buildConfigGroup(ConfigGroupEntity configGroupEntity) { - Cluster cluster = clusterFactory.create(configGroupEntity.getClusterEntity()); ConfigGroup configGroup = configGroupFactory.createExisting(cluster, configGroupEntity); @@ -323,9 +315,7 @@ public class ConfigGroupHostMappingDAO { } private Host buildHost(HostEntity hostEntity) { - Host host = hostFactory.create(hostEntity, false); - return host; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 9bc1235..34d0e3c 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 @@ -17,7 +17,15 @@ */ package org.apache.ambari.server.orm.dao; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -30,6 +38,7 @@ import org.apache.ambari.server.orm.RequiresSession; import org.apache.ambari.server.orm.cache.HostConfigMapping; import org.apache.ambari.server.orm.cache.HostConfigMappingImpl; import org.apache.ambari.server.orm.entities.HostConfigMappingEntity; +import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; @@ -43,13 +52,16 @@ import com.google.inject.persist.Transactional; @Singleton public class HostConfigMappingDAO { @Inject - Provider<EntityManager> entityManagerProvider; + private Provider<EntityManager> entityManagerProvider; + @Inject - DaoUtils daoUtils; - + private DaoUtils daoUtils; + + @Inject + private HostDAO hostDAO; private final ReadWriteLock gl = new ReentrantReadWriteLock(); - private Map<String, Set<HostConfigMapping>> hostConfigMappingByHost; + private Map<Long, Set<HostConfigMapping>> hostConfigMappingByHost; private volatile boolean cacheLoaded; @@ -61,7 +73,7 @@ public class HostConfigMappingDAO { gl.writeLock().lock(); try { if (hostConfigMappingByHost == null) { - hostConfigMappingByHost = new WeakHashMap<String, Set<HostConfigMapping>>(); + hostConfigMappingByHost = new WeakHashMap<Long, Set<HostConfigMapping>>(); TypedQuery<HostConfigMappingEntity> query = entityManagerProvider.get().createQuery( "SELECT entity FROM HostConfigMappingEntity entity", @@ -70,12 +82,18 @@ public class HostConfigMappingDAO { List<HostConfigMappingEntity> hostConfigMappingEntities = daoUtils.selectList(query); for (HostConfigMappingEntity hostConfigMappingEntity : hostConfigMappingEntities) { + Long hostId = hostConfigMappingEntity.getHostId(); - Set<HostConfigMapping> setByHost = hostConfigMappingByHost.get((hostConfigMappingEntity.getHostName())); - - if (setByHost == null) { + if (hostId == null) { + continue; + } + + Set<HostConfigMapping> setByHost; + if (hostConfigMappingByHost.containsKey(hostId)) { + setByHost = hostConfigMappingByHost.get(hostId); + } else { setByHost = new HashSet<HostConfigMapping>(); - hostConfigMappingByHost.put(hostConfigMappingEntity.getHostName(), setByHost); + hostConfigMappingByHost.put(hostId, setByHost); } HostConfigMapping hostConfigMapping = buildHostConfigMapping(hostConfigMappingEntity); @@ -90,65 +108,70 @@ public class HostConfigMappingDAO { } cacheLoaded = true; - } - } @Transactional public void create(HostConfigMapping hostConfigMapping) { - populateCache(); //create in db entityManagerProvider.get().persist(buildHostConfigMappingEntity(hostConfigMapping)); //create in cache - Set<HostConfigMapping> set = hostConfigMappingByHost.get(hostConfigMapping.getHostName()); - if (set == null){ - set = new HashSet<HostConfigMapping>(); - hostConfigMappingByHost.put(hostConfigMapping.getHostName(), set); + Long hostId = hostConfigMapping.getHostId(); + + if (hostId != null) { + Set<HostConfigMapping> set; + if (hostConfigMappingByHost.containsKey(hostId)) { + set = hostConfigMappingByHost.get(hostId); + } else { + set = new HashSet<HostConfigMapping>(); + hostConfigMappingByHost.put(hostId, set); + } + + set.add(hostConfigMapping); } - - set.add(hostConfigMapping); } @Transactional public HostConfigMapping merge(HostConfigMapping hostConfigMapping) { - populateCache(); - - Set<HostConfigMapping> set = hostConfigMappingByHost.get(hostConfigMapping.getHostName()); - if (set == null){ - set = new HashSet<HostConfigMapping>(); - hostConfigMappingByHost.put(hostConfigMapping.getHostName(), set); + + Long hostId = hostConfigMapping.getHostId(); + if (hostId != null) { + Set<HostConfigMapping> set; + if (hostConfigMappingByHost.containsKey(hostId)) { + set = hostConfigMappingByHost.get(hostId); + } else { + set = new HashSet<HostConfigMapping>(); + hostConfigMappingByHost.put(hostId, set); + } + + //Update object in set + set.remove(hostConfigMapping); + set.add(hostConfigMapping); + + entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); } - - //Update object in set - set.remove(hostConfigMapping); - set.add(hostConfigMapping); - - entityManagerProvider.get().merge(buildHostConfigMappingEntity(hostConfigMapping)); - + return hostConfigMapping; } @RequiresSession - public Set<HostConfigMapping> findByType(final long clusterId, String hostName, final String type) { - + public Set<HostConfigMapping> findByType(final long clusterId, Long hostId, final String type) { populateCache(); - if (!hostConfigMappingByHost.containsKey(hostName)) + if (!hostConfigMappingByHost.containsKey(hostId)) return Collections.emptySet(); - Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostName)); + Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostId)); CollectionUtils.filter(set, new Predicate() { @Override public boolean evaluate(Object arg0) { - return ((HostConfigMapping) arg0).getClusterId().equals(clusterId) && ((HostConfigMapping) arg0).getType().equals(type); } @@ -159,41 +182,35 @@ public class HostConfigMappingDAO { @RequiresSession public HostConfigMapping findSelectedByType(final long clusterId, - String hostName, final String type) { - + Long hostId, final String type) { populateCache(); - if (!hostConfigMappingByHost.containsKey(hostName)) + if (!hostConfigMappingByHost.containsKey(hostId)) return null; - Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostName)); + Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostId)); HostConfigMapping result = (HostConfigMapping) CollectionUtils.find(set, new Predicate() { @Override public boolean evaluate(Object arg0) { - return ((HostConfigMapping) arg0).getClusterId().equals(clusterId) && ((HostConfigMapping) arg0).getType().equals(type) && ((HostConfigMapping) arg0).getSelected() > 0; } }); - return result; - } @RequiresSession - public Set<HostConfigMapping> findSelected(final long clusterId, String hostName) { - + public Set<HostConfigMapping> findSelected(final long clusterId, Long hostId) { populateCache(); - if (!hostConfigMappingByHost.containsKey(hostName)) + if (!hostConfigMappingByHost.containsKey(hostId)) return Collections.emptySet(); - - Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostName)); + Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostId)); CollectionUtils.filter(set, new Predicate() { @@ -208,31 +225,26 @@ public class HostConfigMappingDAO { } @RequiresSession - public Set<HostConfigMapping> findSelectedByHosts(long clusterId, Collection<String> hostNames) { - + public Set<HostConfigMapping> findSelectedByHosts(Collection<Long> hostIds) { populateCache(); - if (hostNames == null || hostNames.isEmpty()) { + if (hostIds == null || hostIds.isEmpty()) { return Collections.emptySet(); } - HashSet<HostConfigMapping> result = new HashSet<HostConfigMapping>(); - - - for (final String hostName : hostNames) { - - if (!hostConfigMappingByHost.containsKey(hostName)) + for (final Long hostId : hostIds) { + if (!hostConfigMappingByHost.containsKey(hostId)) continue; - Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostName)); + Set<HostConfigMapping> set = new HashSet<HostConfigMapping>(hostConfigMappingByHost.get(hostId)); CollectionUtils.filter(set, new Predicate() { @Override public boolean evaluate(Object arg0) { - return ((HostConfigMapping) arg0).getHostName().equals(hostName) && + return ((HostConfigMapping) arg0).getHostId().equals(hostId) && ((HostConfigMapping) arg0).getSelected() > 0; } }); @@ -247,9 +259,7 @@ public class HostConfigMappingDAO { @RequiresSession public Map<String, List<HostConfigMapping>> findSelectedHostsByTypes(final long clusterId, Collection<String> types) { - populateCache(); - Map<String, List<HostConfigMapping>> mappingsByType = new HashMap<String, List<HostConfigMapping>>(); @@ -268,7 +278,6 @@ public class HostConfigMappingDAO { if (types.contains(entry.getType()) && entry.getClusterId().equals(clusterId)) mappings.add(new HostConfigMappingImpl(entry)); - } } @@ -276,8 +285,6 @@ public class HostConfigMappingDAO { mappingsByType.get(mapping.getType()).add(mapping); } } - - return mappingsByType; } @@ -293,42 +300,40 @@ public class HostConfigMappingDAO { */ @Transactional public void removeHost(final long clusterId, String hostName) { - populateCache(); - - Set<HostConfigMapping> set = hostConfigMappingByHost.get(hostName); - - //Remove from cache items with clusterId - CollectionUtils.filter(set, new Predicate() { - - @Override - public boolean evaluate(Object arg0) { - return !((HostConfigMapping) arg0).getClusterId().equals(clusterId); + + if (hostConfigMappingByHost.containsKey(hostName)) { + Set<HostConfigMapping> set = hostConfigMappingByHost.get(hostName); + + //Remove from cache items with clusterId + CollectionUtils.filter(set, new Predicate() { + @Override + public boolean evaluate(Object arg0) { + return !((HostConfigMapping) arg0).getClusterId().equals(clusterId); + } + }); + + //delete from db + TypedQuery<HostConfigMappingEntity> query = entityManagerProvider.get().createQuery( + "SELECT entity FROM HostConfigMappingEntity entity " + + "WHERE entity.clusterId = ?1 AND entity.hostEntity.hostName = ?2", + HostConfigMappingEntity.class); + + List<HostConfigMappingEntity> list = daoUtils.selectList(query, Long.valueOf(clusterId), hostName); + + for (HostConfigMappingEntity entity : list) { + entityManagerProvider.get().remove(entity); } - }); - - //delete from db - TypedQuery<HostConfigMappingEntity> query = entityManagerProvider.get().createQuery( - "SELECT entity FROM HostConfigMappingEntity entity " + - "WHERE entity.clusterId = ?1 AND entity.hostName = ?2", - HostConfigMappingEntity.class); - - List<HostConfigMappingEntity> list = daoUtils.selectList(query, Long.valueOf(clusterId), hostName); - - for (HostConfigMappingEntity entity : list) { - entityManagerProvider.get().remove(entity); } - - } public HostConfigMappingEntity buildHostConfigMappingEntity(HostConfigMapping hostConfigMapping) { - + HostEntity hostEntity = hostDAO.findById(hostConfigMapping.getHostId()); HostConfigMappingEntity hostConfigMappingEntity = new HostConfigMappingEntity(); - + hostConfigMappingEntity.setClusterId(hostConfigMapping.getClusterId()); hostConfigMappingEntity.setCreateTimestamp(hostConfigMapping.getCreateTimestamp()); - hostConfigMappingEntity.setHostName(hostConfigMapping.getHostName()); + hostConfigMappingEntity.setHostId(hostEntity.getHostId()); hostConfigMappingEntity.setSelected(hostConfigMapping.getSelected()); hostConfigMappingEntity.setServiceName(hostConfigMapping.getServiceName()); hostConfigMappingEntity.setType(hostConfigMapping.getType()); @@ -340,12 +345,11 @@ public class HostConfigMappingDAO { public HostConfigMapping buildHostConfigMapping( HostConfigMappingEntity hostConfigMappingEntity) { - HostConfigMapping hostConfigMapping = new HostConfigMappingImpl(); hostConfigMapping.setClusterId(hostConfigMappingEntity.getClusterId()); hostConfigMapping.setCreateTimestamp(hostConfigMappingEntity.getCreateTimestamp()); - hostConfigMapping.setHostName(hostConfigMappingEntity.getHostName()); + hostConfigMapping.setHostId(hostConfigMappingEntity.getHostId()); hostConfigMapping.setServiceName(hostConfigMappingEntity.getServiceName()); hostConfigMapping.setType(hostConfigMappingEntity.getType()); hostConfigMapping.setUser(hostConfigMappingEntity.getUser()); @@ -354,5 +358,4 @@ public class HostConfigMappingDAO { return hostConfigMapping; } - } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostDAO.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostDAO.java index 6442bf5..ebd29e3 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/HostDAO.java @@ -29,6 +29,7 @@ import org.apache.ambari.server.orm.entities.StageEntity; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.TypedQuery; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -117,4 +118,14 @@ public class HostDAO { remove(findByName(hostName)); } + public List<String> getHostNamesByHostIds(List<Long> hostIds) { + List<String> hostNames = new ArrayList<String>(); + if (hostIds != null) { + for (Long hostId : hostIds) { + HostEntity hostEntity = findById(hostId); + hostNames.add(hostEntity.getHostName()); + } + } + return hostNames; + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntity.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntity.java index 261bbe8..a9a2d40 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntity.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntity.java @@ -34,7 +34,7 @@ import javax.persistence.Table; @NamedQueries({ @NamedQuery(name = "groupsByHost", query = "SELECT confighosts FROM ConfigGroupHostMappingEntity confighosts " + - "WHERE confighosts.hostname=:hostname"), + "WHERE confighosts.hostEntity.hostName=:hostname"), @NamedQuery(name = "hostsByGroup", query = "SELECT confighosts FROM ConfigGroupHostMappingEntity confighosts " + "WHERE confighosts.configGroupId=:groupId") @@ -46,12 +46,12 @@ public class ConfigGroupHostMappingEntity { private Long configGroupId; @Id - @Column(name = "host_name", nullable = false, insertable = true, updatable = true) - private String hostname; + @Column(name = "host_id", nullable = false, insertable = true, updatable = true) + private Long hostId; @ManyToOne @JoinColumns({ - @JoinColumn(name = "host_name", referencedColumnName = "host_name", nullable = false, insertable = false, updatable = false) }) + @JoinColumn(name = "host_id", referencedColumnName = "host_id", nullable = false, insertable = false, updatable = false) }) private HostEntity hostEntity; @ManyToOne @@ -67,12 +67,16 @@ public class ConfigGroupHostMappingEntity { this.configGroupId = configGroupId; } - public String getHostname() { - return hostname; + public Long getHostId() { + return hostId; + } + + public void setHostId(Long hostId) { + this.hostId = hostId; } - public void setHostname(String hostname) { - this.hostname = hostname; + public String getHostname() { + return hostEntity != null ? hostEntity.getHostName() : null; } public HostEntity getHostEntity() { @@ -99,7 +103,7 @@ public class ConfigGroupHostMappingEntity { ConfigGroupHostMappingEntity that = (ConfigGroupHostMappingEntity) o; if (!configGroupId.equals(that.configGroupId)) return false; - if (!hostname.equals(that.hostname)) return false; + if (!hostEntity.equals(that.hostEntity)) return false; return true; } @@ -107,7 +111,7 @@ public class ConfigGroupHostMappingEntity { @Override public int hashCode() { int result = configGroupId.hashCode(); - result = 31 * result + hostname.hashCode(); + result = 31 * result + hostEntity.hashCode(); return result; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntityPK.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntityPK.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntityPK.java index 3ee2b6c..e7ed99e 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntityPK.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ConfigGroupHostMappingEntityPK.java @@ -23,7 +23,7 @@ import java.io.Serializable; public class ConfigGroupHostMappingEntityPK implements Serializable { private Long configGroupId; - private String hostname; + private Long hostId; @Id @Column(name = "config_group_id", nullable = false, insertable = true, updatable = true) @@ -36,13 +36,13 @@ public class ConfigGroupHostMappingEntityPK implements Serializable { } @Id - @Column(name = "host_name", nullable = false, insertable = true, updatable = true) - public String getHostname() { - return hostname; + @Column(name = "host_id", nullable = false, insertable = true, updatable = true) + public Long getHostId() { + return hostId; } - public void setHostname(String hostname) { - this.hostname = hostname; + public void setHostId(Long hostId) { + this.hostId = hostId; } @Override @@ -53,7 +53,7 @@ public class ConfigGroupHostMappingEntityPK implements Serializable { ConfigGroupHostMappingEntityPK that = (ConfigGroupHostMappingEntityPK) o; if (!configGroupId.equals(that.configGroupId)) return false; - if (!hostname.equals(that.hostname)) return false; + if (!hostId.equals(that.hostId)) return false; return true; } @@ -61,7 +61,7 @@ public class ConfigGroupHostMappingEntityPK implements Serializable { @Override public int hashCode() { int result = configGroupId.hashCode(); - result = 31 * result + hostname.hashCode(); + result = 31 * result + hostId.hashCode(); return result; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 1411a67..915b05f 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 @@ -36,8 +36,8 @@ public class HostConfigMappingEntity { private Long clusterId; @Id - @Column(name = "host_name", insertable = true, updatable = false, nullable = false) - private String hostName; + @Column(name = "host_id", insertable = true, updatable = false, nullable=false) + private Long hostId; @Id @Column(name = "type_name", insertable = true, updatable = false, nullable = false) @@ -58,7 +58,7 @@ public class HostConfigMappingEntity { @Column(name = "user_name", insertable = true, updatable = true, nullable = false) private String user = null; - + public Long getClusterId() { return clusterId; } @@ -67,12 +67,12 @@ public class HostConfigMappingEntity { clusterId = id; } - public String getHostName() { - return hostName; + public Long getHostId() { + return hostId; } - public void setHostName(String name) { - hostName = name; + public void setHostId(Long hostId) { + this.hostId = hostId; } public String getType() { @@ -140,7 +140,7 @@ public class HostConfigMappingEntity { if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId != null) return false; if (createTimestamp != null ? !createTimestamp.equals(that.createTimestamp) : that.createTimestamp != null) return false; - if (hostName != null ? !hostName.equals(that.hostName) : that.hostName != 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 (type != null ? !type.equals(that.type) : that.type != null) return false; if (user != null ? !user.equals(that.user) : that.user != null) return false; @@ -152,7 +152,7 @@ public class HostConfigMappingEntity { @Override public int hashCode() { int result = clusterId != null ? clusterId.hashCode() : 0; - result = 31 * result + (hostName != null ? hostName.hashCode() : 0); + result = 31 * result + (hostId != null ? hostId.hashCode() : 0); result = 31 * result + (type != null ? type.hashCode() : 0); result = 31 * result + (createTimestamp != null ? createTimestamp.hashCode() : 0); result = 31 * result + (versionTag != null ? versionTag.hashCode() : 0); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntityPK.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntityPK.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntityPK.java index 16111fb..61039bb 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntityPK.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/HostConfigMappingEntityPK.java @@ -27,7 +27,7 @@ import java.io.Serializable; */ public class HostConfigMappingEntityPK implements Serializable { private Long clusterId; - private String hostName; + private Long hostId; private String type; private Long createTimestamp; @@ -41,14 +41,14 @@ public class HostConfigMappingEntityPK implements Serializable { clusterId = id; } - @Column(name = "host_name", insertable = true, updatable = true, nullable = false) + @Column(name = "host_id", insertable = true, updatable = true, nullable = false) @Id - public String getHostName() { - return hostName; + public Long getHostId() { + return hostId; } - public void setHostName(String name) { - hostName = name; + public void setHostId(Long hostId) { + this.hostId = hostId; } @Column(name = "type_name", insertable = true, updatable = true, nullable = false) @@ -81,7 +81,7 @@ public class HostConfigMappingEntityPK implements Serializable { HostConfigMappingEntityPK that = (HostConfigMappingEntityPK) o; if (clusterId != null ? !clusterId.equals(that.clusterId) : that.clusterId != null) return false; - if (hostName != null ? !hostName.equals(that.hostName) : that.hostName != null) return false; + if (hostId != null ? !hostId.equals(that.hostId) : that.hostId != null) return false; if (type != null ? !type.equals(that.type) : that.type != null) return false; if (createTimestamp != null ? !createTimestamp.equals (that.createTimestamp) : that.createTimestamp != null) return false; @@ -92,10 +92,8 @@ public class HostConfigMappingEntityPK implements Serializable { public int hashCode() { int result = clusterId !=null ? clusterId.intValue() : 0; result = 31 * result + (type != null ? type.hashCode() : 0); - result = 31 * result + (hostName != null ? hostName.hashCode() : 0); + result = 31 * result + (hostId != null ? hostId.hashCode() : 0); result = 31 * result + createTimestamp.intValue(); return result; - } - - + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 1a31252..cd3bb54 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 @@ -83,8 +83,8 @@ public class ServiceConfigEntity { @ElementCollection() @CollectionTable(name = "serviceconfighosts", joinColumns = {@JoinColumn(name = "service_config_id")}) - @Column(name = "hostname") - private List<String> hostNames; + @Column(name = "host_id") + private List<Long> hostIds; @ManyToMany @JoinTable( @@ -185,12 +185,12 @@ public class ServiceConfigEntity { this.groupId = groupId; } - public List<String> getHostNames() { - return hostNames; + public List<Long> getHostIds() { + return hostIds; } - public void setHostNames(List<String> hostNames) { - this.hostNames = hostNames; + public void setHostIds(List<Long> hostIds) { + this.hostIds = hostIds; } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 855bb3f..209293f 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 @@ -406,16 +406,16 @@ public interface Cluster { /** * Fetch desired configs for list of hosts in cluster - * @param hostnames + * @param hostIds * @return */ - Map<String, Map<String, DesiredConfig>> getHostsDesiredConfigs(Collection<String> hostnames); + Map<Long, Map<String, DesiredConfig>> getHostsDesiredConfigs(Collection<Long> hostIds); /** * Fetch desired configs for all hosts in cluster * @return */ - Map<String, Map<String, DesiredConfig>> getAllHostsDesiredConfigs(); + Map<Long, Map<String, DesiredConfig>> getAllHostsDesiredConfigs(); /** * Add a new config group to the set of Config groups associated with this http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java index 80ac6a7..6edf7d9 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java @@ -80,6 +80,14 @@ public interface Clusters { public Host getHost(String hostname) throws AmbariException; /** + * Get a Host object managed by this server + * @param hostId Host Id from the {@link org.apache.ambari.server.orm.entities.HostEntity} objecty + * @return Host object + * @throws AmbariException + */ + public Host getHostById(Long hostId) throws AmbariException; + + /** * Add a Host object to be managed by this server * @param hostname Host to be added * @throws AmbariException @@ -137,6 +145,15 @@ public interface Clusters { throws AmbariException; /** + * Gets all the host Ids associated with the cluster + * @param clusterName The name of the cluster + * @return <code>Map</code> containing host id and <code>Host</code> + * @throws AmbariException + */ + public Map<Long, Host> getHostIdsForCluster(String clusterName) + throws AmbariException; + + /** * Deletes the cluster identified by the name * @param clusterName The name of the cluster * @throws AmbariException http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 2558de8..bb6f6c1 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 @@ -290,7 +290,6 @@ public class ClusterImpl implements Cluster { return clusterGlobalLock; } - private void loadServiceConfigTypes() throws AmbariException { try { serviceConfigTypes = collectServiceConfigTypesMapping(); @@ -501,22 +500,24 @@ public class ClusterImpl implements Cluster { clusterGlobalLock.readLock().lock(); try { - Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO.findByHost(hostname); - - if (hostMappingEntities != null && !hostMappingEntities.isEmpty()) { - for (ConfigGroupHostMapping entity : hostMappingEntities) { - ConfigGroup configGroup = configGroupMap.get(entity.getConfigGroupId()); - if (configGroup != null - && !configGroups.containsKey(configGroup.getId())) { - configGroups.put(configGroup.getId(), configGroup); + HostEntity hostEntity = hostDAO.findByName(hostname); + if (hostEntity != null) { + Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO.findByHostId(hostEntity.getHostId()); + + if (hostMappingEntities != null && !hostMappingEntities.isEmpty()) { + for (ConfigGroupHostMapping entity : hostMappingEntities) { + ConfigGroup configGroup = configGroupMap.get(entity.getConfigGroupId()); + if (configGroup != null + && !configGroups.containsKey(configGroup.getId())) { + configGroups.put(configGroup.getId(), configGroup); + } } } } - return configGroups; - } finally { clusterGlobalLock.readLock().unlock(); } + return configGroups; } @Override @@ -1964,6 +1965,9 @@ public class ClusterImpl implements Cluster { } } + // TODO AMBARI-10679, need efficient caching from hostId to hostName + Map<Long, String> hostIdToName = new HashMap<Long, String>(); + if (!map.isEmpty()) { Map<String, List<HostConfigMapping>> hostMappingsByType = hostConfigMappingDAO.findSelectedHostsByTypes( clusterEntity.getClusterId(), types); @@ -1971,8 +1975,14 @@ public class ClusterImpl implements Cluster { for (Entry<String, DesiredConfig> entry : map.entrySet()) { List<DesiredConfig.HostOverride> hostOverrides = new ArrayList<DesiredConfig.HostOverride>(); for (HostConfigMapping mappingEntity : hostMappingsByType.get(entry.getKey())) { + + if (!hostIdToName.containsKey(mappingEntity.getHostId())) { + HostEntity hostEntity = hostDAO.findById(mappingEntity.getHostId()); + hostIdToName.put(mappingEntity.getHostId(), hostEntity.getHostName()); + } + hostOverrides.add(new DesiredConfig.HostOverride( - mappingEntity.getHostName(), mappingEntity.getVersion())); + hostIdToName.get(mappingEntity.getHostId()), mappingEntity.getVersion())); } entry.getValue().setHostOverrides(hostOverrides); } @@ -2002,7 +2012,7 @@ public class ClusterImpl implements Cluster { } serviceConfigEntity.setClusterConfigEntities(configEntities); - serviceConfigEntity.setHostNames(new ArrayList<String>(configGroup.getHosts().keySet())); + serviceConfigEntity.setHostIds(new ArrayList<Long>(configGroup.getHosts().keySet())); } else { List<ClusterConfigEntity> configEntities = getClusterConfigEntitiesByService(serviceName); @@ -2248,13 +2258,15 @@ public class ClusterImpl implements Cluster { } configGroup.setConfigurations(groupDesiredConfigs); - Map<String, Host> groupDesiredHosts = new HashMap<String, Host>(); - for (String hostname : serviceConfigEntity.getHostNames()) { - Host host = clusters.getHost(hostname); - if (host != null) { - groupDesiredHosts.put(hostname, host); - } else { - LOG.warn("Host {} doesn't exist anymore, skipping", hostname); + Map<Long, Host> groupDesiredHosts = new HashMap<Long, Host>(); + if (serviceConfigEntity.getHostIds() != null) { + for (Long hostId : serviceConfigEntity.getHostIds()) { + Host host = clusters.getHostById(hostId); + if (host != null) { + groupDesiredHosts.put(hostId, host); + } else { + LOG.warn("Host with id {} doesn't exist anymore, skipping", hostId); + } } } configGroup.setHosts(groupDesiredHosts); @@ -2275,7 +2287,7 @@ public class ClusterImpl implements Cluster { serviceConfigEntityClone.setStack(serviceConfigEntity.getStack()); serviceConfigEntityClone.setClusterConfigEntities(serviceConfigEntity.getClusterConfigEntities()); serviceConfigEntityClone.setClusterId(serviceConfigEntity.getClusterId()); - serviceConfigEntityClone.setHostNames(serviceConfigEntity.getHostNames()); + serviceConfigEntityClone.setHostIds(serviceConfigEntity.getHostIds()); serviceConfigEntityClone.setGroupId(serviceConfigEntity.getGroupId()); serviceConfigEntityClone.setNote(serviceConfigVersionNote); serviceConfigEntityClone.setVersion(nextServiceConfigVersion); @@ -2388,21 +2400,20 @@ public class ClusterImpl implements Cluster { } } - @Override - public Map<String, Map<String, DesiredConfig>> getHostsDesiredConfigs(Collection<String> hostnames) { + public Map<Long, Map<String, DesiredConfig>> getHostsDesiredConfigs(Collection<Long> hostIds) { - if (hostnames == null || hostnames.isEmpty()) { + if (hostIds == null || hostIds.isEmpty()) { return Collections.emptyMap(); } Set<HostConfigMapping> mappingEntities = - hostConfigMappingDAO.findSelectedByHosts(clusterEntity.getClusterId(), hostnames); + hostConfigMappingDAO.findSelectedByHosts(hostIds); - Map<String, Map<String, DesiredConfig>> desiredConfigsByHost = new HashMap<String, Map<String, DesiredConfig>>(); + Map<Long, Map<String, DesiredConfig>> desiredConfigsByHost = new HashMap<Long, Map<String, DesiredConfig>>(); - for (String hostname : hostnames) { - desiredConfigsByHost.put(hostname, new HashMap<String, DesiredConfig>()); + for (Long hostId : hostIds) { + desiredConfigsByHost.put(hostId, new HashMap<String, DesiredConfig>()); } for (HostConfigMapping mappingEntity : mappingEntities) { @@ -2411,23 +2422,23 @@ public class ClusterImpl implements Cluster { desiredConfig.setServiceName(mappingEntity.getServiceName()); desiredConfig.setUser(mappingEntity.getUser()); - desiredConfigsByHost.get(mappingEntity.getHostName()).put(mappingEntity.getType(), desiredConfig); + desiredConfigsByHost.get(mappingEntity.getHostId()).put(mappingEntity.getType(), desiredConfig); } return desiredConfigsByHost; } @Override - public Map<String, Map<String, DesiredConfig>> getAllHostsDesiredConfigs() { + public Map<Long, Map<String, DesiredConfig>> getAllHostsDesiredConfigs() { - Collection<String> hostnames; + Collection<Long> hostIds; try { - hostnames = clusters.getHostsForCluster(clusterEntity.getClusterName()).keySet(); + hostIds = clusters.getHostIdsForCluster(clusterEntity.getClusterName()).keySet(); } catch (AmbariException ignored) { return Collections.emptyMap(); } - return getHostsDesiredConfigs(hostnames); + return getHostsDesiredConfigs(hostIds); } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java index 70788ff..f8066ca 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java @@ -89,6 +89,7 @@ public class ClustersImpl implements Clusters { private ConcurrentHashMap<String, Cluster> clusters; private ConcurrentHashMap<Long, Cluster> clustersById; private ConcurrentHashMap<String, Host> hosts; + private ConcurrentHashMap<Long, Host> hostsById; private ConcurrentHashMap<String, Set<Cluster>> hostClusterMap; private ConcurrentHashMap<String, Set<Host>> clusterHostMap; @@ -138,6 +139,7 @@ public class ClustersImpl implements Clusters { clusters = new ConcurrentHashMap<String, Cluster>(); clustersById = new ConcurrentHashMap<Long, Cluster>(); hosts = new ConcurrentHashMap<String, Host>(); + hostsById = new ConcurrentHashMap<Long, Host>(); hostClusterMap = new ConcurrentHashMap<String, Set<Cluster>>(); clusterHostMap = new ConcurrentHashMap<String, Set<Host>>(); @@ -172,6 +174,7 @@ public class ClustersImpl implements Clusters { for (HostEntity hostEntity : hostDAO.findAll()) { Host host = hostFactory.create(hostEntity, true); hosts.put(hostEntity.getHostName(), host); + hostsById.put(hostEntity.getHostId(), host); Set<Cluster> cSet = Collections.newSetFromMap(new ConcurrentHashMap<Cluster, Boolean>()); hostClusterMap.put(hostEntity.getHostName(), cSet); @@ -328,6 +331,17 @@ public class ClustersImpl implements Clusters { return hosts.get(hostname); } + @Override + public Host getHostById(Long hostId) throws AmbariException { + checkLoaded(); + + if (!hosts.containsKey(hostId)) { + throw new HostNotFoundException("Host Id = " + hostId); + } + + return hosts.get(hostId); + } + /** * Register a host by creating a {@link HostEntity} object in the database and setting its state to * {@link HostState#INIT}. This does not add the host the cluster. @@ -614,6 +628,27 @@ public class ClustersImpl implements Clusters { } @Override + public Map<Long, Host> getHostIdsForCluster(String clusterName) + throws AmbariException { + + checkLoaded(); + r.lock(); + + try { + Map<Long, Host> hosts = new HashMap<Long, Host>(); + + for (Host h : clusterHostMap.get(clusterName)) { + HostEntity hostEntity = hostDAO.findByName(h.getHostName()); + hosts.put(hostEntity.getHostId(), h); + } + + return hosts; + } finally { + r.unlock(); + } + } + + @Override public void deleteCluster(String clusterName) throws AmbariException { checkLoaded(); @@ -649,6 +684,7 @@ public class ClustersImpl implements Clusters { throws AmbariException { Host host = null; Cluster cluster = null; + HostEntity hostEntity = null; checkLoaded(); @@ -656,6 +692,7 @@ public class ClustersImpl implements Clusters { try { host = getHost(hostname); cluster = getCluster(clusterName); + hostEntity = hostDAO.findByName(hostname); } finally { r.unlock(); } @@ -677,7 +714,7 @@ public class ClustersImpl implements Clusters { host.refresh(); cluster.refresh(); - deleteConfigGroupHostMapping(hostname); + deleteConfigGroupHostMapping(hostEntity.getHostId()); // Remove mapping of principals to the unmapped host kerberosPrincipalHostDAO.removeByHost(hostname); @@ -699,11 +736,11 @@ public class ClustersImpl implements Clusters { } @Transactional - private void deleteConfigGroupHostMapping(String hostname) throws AmbariException { + private void deleteConfigGroupHostMapping(Long hostId) throws AmbariException { // Remove Config group mapping for (Cluster cluster : clusters.values()) { for (ConfigGroup configGroup : cluster.getConfigGroups().values()) { - configGroup.removeHost(hostname); + configGroup.removeHost(hostId); } } } @@ -719,17 +756,23 @@ public class ClustersImpl implements Clusters { w.lock(); try { - deleteConfigGroupHostMapping(hostname); + HostEntity entity = hostDAO.findByName(hostname); + + if (entity == null) { + return; + } + + deleteConfigGroupHostMapping(entity.getHostId()); Collection<HostVersionEntity> hostVersions = hosts.get(hostname).getAllHostVersions(); for (HostVersionEntity hostVersion : hostVersions) { hostVersionDAO.remove(hostVersion); } - HostEntity entity = hostDAO.findByName(hostname); hostDAO.refresh(entity); hostDAO.remove(entity); hosts.remove(hostname); + hostsById.remove(entity.getHostId()); // Remove mapping of principals to deleted host kerberosPrincipalHostDAO.removeByHost(hostname); @@ -743,7 +786,6 @@ public class ClustersImpl implements Clusters { } finally { w.unlock(); } - } @Override http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 a4cc6ac..4c806e5 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 @@ -84,7 +84,7 @@ public interface ConfigGroup { * List of hosts to which configs are applied * @return */ - public Map<String, Host> getHosts(); + public Map<Long, Host> getHosts(); /** * List of @Config objects @@ -137,7 +137,7 @@ public interface ConfigGroup { * Reassign the set of hosts associated with this config group * @param hosts */ - public void setHosts(Map<String, Host> hosts); + public void setHosts(Map<Long, Host> hosts); /** * Reassign the set of configs associated with this config group @@ -148,7 +148,7 @@ public interface ConfigGroup { /** * Remove host mapping */ - public void removeHost(String hostname) throws AmbariException; + public void removeHost(Long hostId) throws AmbariException; String getServiceName(); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 d4597af..9abadf3 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 @@ -32,7 +32,7 @@ public interface ConfigGroupFactory { @Assisted("tag") String tag, @Assisted("description") String description, @Assisted("configs") Map<String, Config> configs, - @Assisted("hosts") Map<String, Host> hosts); + @Assisted("hosts") Map<Long, Host> hosts); ConfigGroup createExisting(Cluster cluster, ConfigGroupEntity entity); } http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 ffa085a..4c5d016 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 @@ -62,7 +62,7 @@ public class ConfigGroupImpl implements ConfigGroup { private Cluster cluster; private ConfigGroupEntity configGroupEntity; - private Map<String, Host> hosts; + private Map<Long, Host> hosts; private Map<String, Config> configurations; private volatile boolean isPersisted = false; @@ -89,7 +89,7 @@ public class ConfigGroupImpl implements ConfigGroup { @Assisted("tag") String tag, @Assisted("description") String description, @Assisted("configs") Map<String, Config> configs, - @Assisted("hosts") Map<String, Host> hosts, + @Assisted("hosts") Map<Long, Host> hosts, Injector injector) { injector.injectMembers(this); this.cluster = cluster; @@ -103,7 +103,7 @@ public class ConfigGroupImpl implements ConfigGroup { if (hosts != null) { this.hosts = hosts; } else { - this.hosts = new HashMap<String, Host>(); + this.hosts = new HashMap<Long, Host>(); } if (configs != null) { @@ -122,7 +122,7 @@ public class ConfigGroupImpl implements ConfigGroup { this.configGroupEntity = configGroupEntity; configurations = new HashMap<String, Config>(); - hosts = new HashMap<String, Host>(); + hosts = new HashMap<Long, Host>(); // Populate configs for (ConfigGroupConfigMappingEntity configMappingEntity : configGroupEntity @@ -147,8 +147,9 @@ public class ConfigGroupImpl implements ConfigGroup { try { Host host = clusters.getHost(hostMappingEntity.getHostname()); - if (host != null) { - hosts.put(host.getHostName(), host); + HostEntity hostEntity = hostMappingEntity.getHostEntity(); + if (host != null && hostEntity != null) { + hosts.put(hostEntity.getHostId(), host); } } catch (AmbariException e) { String msg = "Host seems to be deleted but Config group mapping still " + @@ -235,7 +236,7 @@ public class ConfigGroupImpl implements ConfigGroup { } @Override - public Map<String, Host> getHosts() { + public Map<Long, Host> getHosts() { readWriteLock.readLock().lock(); try { return Collections.unmodifiableMap(hosts); @@ -260,7 +261,7 @@ public class ConfigGroupImpl implements ConfigGroup { * @param hosts */ @Override - public void setHosts(Map<String, Host> hosts) { + public void setHosts(Map<Long, Host> hosts) { readWriteLock.writeLock().lock(); try { this.hosts = hosts; @@ -287,23 +288,25 @@ public class ConfigGroupImpl implements ConfigGroup { @Override @Transactional - public void removeHost(String hostname) throws AmbariException { + public void removeHost(Long hostId) throws AmbariException { readWriteLock.writeLock().lock(); try { - if (hosts.containsKey(hostname)) { - LOG.info("Removing host from config group, hostname = " + hostname); - hosts.remove(hostname); + if (hosts.containsKey(hostId)) { + String hostName = hosts.get(hostId).getHostName(); + LOG.info("Removing host from config group, hostid = " + hostId + ", hostname = " + hostName); + hosts.remove(hostId); try { ConfigGroupHostMappingEntityPK hostMappingEntityPK = new ConfigGroupHostMappingEntityPK(); - hostMappingEntityPK.setHostname(hostname); + hostMappingEntityPK.setHostId(hostId); hostMappingEntityPK.setConfigGroupId(configGroupEntity.getGroupId()); configGroupHostMappingDAO.removeByPK(hostMappingEntityPK); } catch (Exception e) { LOG.error("Failed to delete config group host mapping" + ", clusterName = " + getClusterName() + ", id = " + getId() - + ", hostname = " + hostname, e); + + ", hostid = " + hostId + + ", hostname = " + hostName, e); throw new AmbariException(e.getMessage()); } } @@ -366,7 +369,7 @@ public class ConfigGroupImpl implements ConfigGroup { if (hostEntity != null) { ConfigGroupHostMappingEntity hostMappingEntity = new ConfigGroupHostMappingEntity(); - hostMappingEntity.setHostname(host.getHostName()); + hostMappingEntity.setHostId(hostEntity.getHostId()); hostMappingEntity.setHostEntity(hostEntity); hostMappingEntity.setConfigGroupEntity(configGroupEntity); hostMappingEntity.setConfigGroupId(configGroupEntity.getGroupId()); @@ -487,7 +490,10 @@ public class ConfigGroupImpl implements ConfigGroup { configGroupEntity.getGroupName()); } } - hosts.put(host.getHostName(), host); + HostEntity hostEntity = hostDAO.findByName(host.getHostName()); + if (hostEntity != null) { + hosts.put(hostEntity.getHostId(), host); + } } } finally { readWriteLock.writeLock().unlock(); http://git-wip-us.apache.org/repos/asf/ambari/blob/290276c6/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 41cfee7..50d762e 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 @@ -1069,6 +1069,29 @@ public class HostImpl implements Host { } @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Host that = (Host) o; + + return this.getHostName().equals(that.getHostName()); + } + + @Override + public int hashCode() { + return (null == getHostName() ? 0 : getHostName().hashCode()); + } + + public int compareTo(HostEntity other) { + return getHostName().compareTo(other.getHostName()); + } + + @Override public HostResponse convertToResponse() { try { readLock.lock(); @@ -1200,7 +1223,7 @@ public class HostImpl implements Host { try { // set all old mappings for this type to empty for (HostConfigMapping e : hostConfigMappingDAO.findByType(clusterId, - hostEntity.getHostName(), config.getType())) { + hostEntity.getHostId(), config.getType())) { e.setSelected(0); hostConfigMappingDAO.merge(e); } @@ -1208,7 +1231,7 @@ public class HostImpl implements Host { HostConfigMapping hostConfigMapping = new HostConfigMappingImpl(); hostConfigMapping.setClusterId(clusterId); hostConfigMapping.setCreateTimestamp(System.currentTimeMillis()); - hostConfigMapping.setHostName(hostEntity.getHostName()); + hostConfigMapping.setHostId(hostEntity.getHostId()); hostConfigMapping.setSelected(1); hostConfigMapping.setUser(user); hostConfigMapping.setType(config.getType()); @@ -1230,7 +1253,7 @@ public class HostImpl implements Host { Map<String, DesiredConfig> map = new HashMap<String, DesiredConfig>(); for (HostConfigMapping e : hostConfigMappingDAO.findSelected( - clusterId, hostEntity.getHostName())) { + clusterId, hostEntity.getHostId())) { DesiredConfig dc = new DesiredConfig(); dc.setTag(e.getVersion()); @@ -1289,7 +1312,7 @@ public class HostImpl implements Host { } private HostConfigMapping getDesiredConfigEntity(long clusterId, String type) { - return hostConfigMappingDAO.findSelectedByType(clusterId, hostEntity.getHostName(), type); + return hostConfigMappingDAO.findSelectedByType(clusterId, hostEntity.getHostId(), type); } private void ensureMaintMap() {