Updated Branches: refs/heads/branch-1.4.3 b0704544b -> 10127a259
AMBARI-4215. Enable caching in ConfigGroupHostMappingDAO. (Oleksandr Diachenko via swagle) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/10127a25 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/10127a25 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/10127a25 Branch: refs/heads/branch-1.4.3 Commit: 10127a259d1063ec4911a36c78445bfa1a676cb1 Parents: b070454 Author: Siddharth Wagle <[email protected]> Authored: Thu Jan 2 15:51:28 2014 -0800 Committer: Siddharth Wagle <[email protected]> Committed: Thu Jan 2 15:51:28 2014 -0800 ---------------------------------------------------------------------- .../orm/cache/ConfigGroupHostMapping.java | 35 +++ .../orm/cache/ConfigGroupHostMappingImpl.java | 121 +++++++++ .../server/orm/cache/HostConfigMapping.java | 47 ++++ .../server/orm/cache/HostConfigMappingImpl.java | 187 +++++++++++++ .../orm/dao/ConfigGroupHostMappingDAO.java | 270 ++++++++++++++++--- .../server/orm/dao/HostConfigMappingDAO.java | 6 +- .../ambari/server/state/HostConfigMapping.java | 47 ---- .../server/state/HostConfigMappingImpl.java | 186 ------------- .../server/state/cluster/ClusterImpl.java | 7 +- .../server/state/configgroup/ConfigGroup.java | 3 - .../state/configgroup/ConfigGroupFactory.java | 1 - .../state/configgroup/ConfigGroupImpl.java | 4 - .../ambari/server/state/host/HostImpl.java | 4 +- .../server/orm/dao/ConfigGroupDAOTest.java | 13 +- .../orm/dao/HostConfigMappingDAOTest.java | 4 +- 15 files changed, 650 insertions(+), 285 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 new file mode 100644 index 0000000..5c26a6c --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMapping.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.orm.cache; + +import org.apache.ambari.server.state.Host; +import org.apache.ambari.server.state.configgroup.ConfigGroup; + +public interface ConfigGroupHostMapping { + + public Long getConfigGroupId(); + public String getHostname(); + public Host getHost(); + public ConfigGroup getConfigGroup(); + + public void setConfigGroupId(Long configGroupId); + public void setHostname(String hostname); + public void setHost(Host host); + public void setConfigGroup(ConfigGroup configGroup); +} http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 new file mode 100644 index 0000000..54e1ca0 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/ConfigGroupHostMappingImpl.java @@ -0,0 +1,121 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.orm.cache; + +import org.apache.ambari.server.state.Host; +import org.apache.ambari.server.state.configgroup.ConfigGroup; + +public class ConfigGroupHostMappingImpl implements ConfigGroupHostMapping { + + private Long configGroupId; + private String hostname; + private Host host; + private ConfigGroup configGroup; + + @Override + public Long getConfigGroupId() { + return configGroupId; + } + + @Override + public String getHostname() { + return hostname; + } + + @Override + public Host getHost() { + return host; + } + + @Override + public ConfigGroup getConfigGroup() { + return configGroup; + } + + @Override + public void setConfigGroupId(Long configGroupId) { + this.configGroupId = configGroupId; + + } + + @Override + public void setHostname(String hostname) { + this.hostname = hostname; + + } + + @Override + public void setHost(Host host) { + this.host = host; + + } + + @Override + public void setConfigGroup(ConfigGroup configGroup) { + this.configGroup = configGroup; + + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = + prime * result + ((configGroup == null) ? 0 : configGroup.hashCode()); + result = + prime * result + + ((configGroupId == null) ? 0 : configGroupId.hashCode()); + result = prime * result + ((host == null) ? 0 : host.hashCode()); + result = prime * result + ((hostname == null) ? 0 : hostname.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + 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; + return true; + } + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 new file mode 100644 index 0000000..269daa9 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMapping.java @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.orm.cache; + +public interface HostConfigMapping { + + public Long getClusterId(); + public void setClusterId(Long clusterId); + + public String getHostName(); + public void setHostName(String hostName); + + public String getType(); + public void setType(String type); + + public Long getCreateTimestamp(); + public void setCreateTimestamp(Long createTimestamp); + + public String getVersion(); + public void setVersion(String version); + + public String getServiceName(); + public void setServiceName(String serviceName); + + public String getUser(); + public void setUser(String user); + + public Integer getSelected(); + public void setSelected(Integer selected); + + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 new file mode 100644 index 0000000..407aeb6 --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/cache/HostConfigMappingImpl.java @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.orm.cache; + + +public class HostConfigMappingImpl implements HostConfigMapping { + + private Long clusterId; + private String hostName; + private String type; + private Long createTimestamp; + private String version; + private String serviceName; + private String user; + private Integer selected; + + + + public HostConfigMappingImpl(HostConfigMapping entry) { + setClusterId(entry.getClusterId()); + setHostName(entry.getHostName()); + setType(entry.getType()); + setCreateTimestamp(entry.getCreateTimestamp()); + setVersion(entry.getVersion()); + setServiceName(entry.getServiceName()); + setUser(entry.getUser()); + setSelected(entry.getSelected()); + } + + public HostConfigMappingImpl() { + } + + @Override + public Long getClusterId() { + return clusterId; + } + + @Override + public void setClusterId(Long clusterId) { + if (clusterId == null) + throw new RuntimeException("ClusterId couldn't be null"); + this.clusterId = clusterId; + } + + @Override + public String getHostName() { + return hostName; + } + + @Override + public void setHostName(String hostName) { + if (hostName == null) + throw new RuntimeException("HostName couldn't be null"); + this.hostName = hostName; + } + + @Override + public String getType() { + return type; + } + + @Override + public void setType(String type) { + if (type == null) + throw new RuntimeException("Type couldn't be null"); + this.type = type; + } + + @Override + public Long getCreateTimestamp() { + return createTimestamp; + } + + @Override + public void setCreateTimestamp(Long createTimestamp) { + if (createTimestamp == null) + throw new RuntimeException("CreateTimestamp couldn't be null"); + this.createTimestamp = createTimestamp; + } + + @Override + public String getVersion() { + return version; + } + + @Override + public void setVersion(String version) { + if (version == null) + throw new RuntimeException("Version couldn't be null"); + this.version = version; + } + public String getServiceName() { + return serviceName; + } + + @Override + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + @Override + public String getUser() { + return user; + } + + @Override + public void setUser(String user) { + if (user == null) + throw new RuntimeException("User couldn't be null"); + this.user = user; + } + + @Override + public Integer getSelected() { + return selected; + } + + @Override + public void setSelected(Integer selected) { + if (selected == null) + throw new RuntimeException("Selected couldn't be null"); + this.selected = selected; + } + + @Override + public int hashCode() { + 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 + ((type == null) ? 0 : type.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + 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; + return true; + } + + +} http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 5ca155f..3612a07 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 @@ -21,12 +21,33 @@ import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.Singleton; import com.google.inject.persist.Transactional; + +import org.apache.ambari.server.orm.cache.ConfigGroupHostMapping; +import org.apache.ambari.server.orm.cache.ConfigGroupHostMappingImpl; +import org.apache.ambari.server.orm.cache.HostConfigMapping; +import org.apache.ambari.server.orm.entities.ConfigGroupEntity; import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntity; import org.apache.ambari.server.orm.entities.ConfigGroupHostMappingEntityPK; +import org.apache.ambari.server.orm.entities.HostEntity; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Host; +import org.apache.ambari.server.state.cluster.ClusterFactory; +import org.apache.ambari.server.state.configgroup.ConfigGroup; +import org.apache.ambari.server.state.configgroup.ConfigGroupFactory; +import org.apache.ambari.server.state.host.HostFactory; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.Predicate; + import javax.persistence.EntityManager; -import javax.persistence.NoResultException; import javax.persistence.TypedQuery; + +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; @Singleton public class ConfigGroupHostMappingDAO { @@ -34,75 +55,218 @@ public class ConfigGroupHostMappingDAO { Provider<EntityManager> entityManagerProvider; @Inject DaoUtils daoUtils; + @Inject + private ConfigGroupFactory configGroupFactory; + @Inject + private ClusterFactory clusterFactory; + @Inject + private HostFactory hostFactory; + + private final ReadWriteLock gl = new ReentrantReadWriteLock(); + + private Map<String, Set<ConfigGroupHostMapping>> configGroupHostMappingByHost; + + private volatile boolean cacheLoaded; + + + private void populateCache() { + + if (!cacheLoaded) { + gl.writeLock().lock(); + try { + if (configGroupHostMappingByHost == null) { + configGroupHostMappingByHost = new ConcurrentHashMap<String, Set<ConfigGroupHostMapping>>(); + + TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider.get().createQuery( + "SELECT entity FROM ConfigGroupHostMappingEntity entity", + ConfigGroupHostMappingEntity.class); + List<ConfigGroupHostMappingEntity> configGroupHostMappingEntities = daoUtils.selectList(query); + + for (ConfigGroupHostMappingEntity configGroupHostMappingEntity : configGroupHostMappingEntities) { + + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get((configGroupHostMappingEntity.getHostname())); + + if (setByHost == null) { + setByHost = new HashSet<ConfigGroupHostMapping>(); + configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), setByHost); + } + + ConfigGroupHostMapping configGroupHostMapping = buildConfigGroupHostMapping(configGroupHostMappingEntity); + setByHost.add(configGroupHostMapping); + } + } + } finally { + gl.writeLock().unlock(); + } + + cacheLoaded = true; + + } + + } + @Transactional - public ConfigGroupHostMappingEntity findByPK(ConfigGroupHostMappingEntityPK + public ConfigGroupHostMapping findByPK(final ConfigGroupHostMappingEntityPK configGroupHostMappingEntityPK) { - return entityManagerProvider.get().find(ConfigGroupHostMappingEntity - .class, configGroupHostMappingEntityPK); + + populateCache(); + + if (!configGroupHostMappingByHost.containsKey(configGroupHostMappingEntityPK.getHostname())) + return null; + + Set<ConfigGroupHostMapping> set = + new HashSet<ConfigGroupHostMapping>(configGroupHostMappingByHost.get(configGroupHostMappingEntityPK.getHostname())); + + ConfigGroupHostMapping itemByPk = (ConfigGroupHostMapping) CollectionUtils.find(set, new Predicate() { + + @Override + public boolean evaluate(Object arg0) { + return ((ConfigGroupHostMapping) arg0).getConfigGroupId(). + equals(configGroupHostMappingEntityPK.getConfigGroupId()); + } + }); + + return itemByPk; } @Transactional - public List<ConfigGroupHostMappingEntity> findByHost(String hostname) { - TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider - .get().createNamedQuery("groupsByHost", ConfigGroupHostMappingEntity - .class); - - query.setParameter("hostname", hostname); - try { - return query.getResultList(); - } catch (NoResultException ignored) { - } - return null; + public Set<ConfigGroupHostMapping> findByHost(String hostname) { + + populateCache(); + + if (!configGroupHostMappingByHost.containsKey(hostname)) + return null; + + Set<ConfigGroupHostMapping> set = new HashSet<ConfigGroupHostMapping>(configGroupHostMappingByHost.get(hostname)); + + return set; + } @Transactional - public List<ConfigGroupHostMappingEntity> findByGroup(Long groupId) { - TypedQuery<ConfigGroupHostMappingEntity> query = entityManagerProvider - .get().createNamedQuery("hostsByGroup", ConfigGroupHostMappingEntity - .class); - - query.setParameter("groupId", groupId); - try { - return query.getResultList(); - } catch (NoResultException ignored) { + public Set<ConfigGroupHostMapping> findByGroup(final Long groupId) { + + populateCache(); + + Set<ConfigGroupHostMapping> result = new HashSet<ConfigGroupHostMapping>(); + + for (Set<ConfigGroupHostMapping> item : configGroupHostMappingByHost.values()) { + + Set<ConfigGroupHostMapping> setByHost = new HashSet<ConfigGroupHostMapping>(item); + + CollectionUtils.filter(setByHost, new Predicate() { + + @Override + public boolean evaluate(Object arg0) { + return ((ConfigGroupHostMapping) arg0).getConfigGroupId().equals(groupId); + } + }); + + result.addAll(setByHost); + } - return null; + + return result; + } @Transactional public void create(ConfigGroupHostMappingEntity configGroupHostMappingEntity) { + + populateCache(); + entityManagerProvider.get().persist(configGroupHostMappingEntity); + + //create in cache + Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname()); + if (set == null){ + set = new HashSet<ConfigGroupHostMapping>(); + configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), set); + } + + set.add(buildConfigGroupHostMapping(configGroupHostMappingEntity)); + } + + @Transactional - public ConfigGroupHostMappingEntity merge(ConfigGroupHostMappingEntity - configGroupHostMappingEntity) { + public ConfigGroupHostMappingEntity merge(ConfigGroupHostMappingEntity configGroupHostMappingEntity) { + + populateCache(); + + Set<ConfigGroupHostMapping> set = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname()); + if (set == null){ + set = new HashSet<ConfigGroupHostMapping>(); + configGroupHostMappingByHost.put(configGroupHostMappingEntity.getHostname(), set); + } + + //Update object in set + set.remove(buildConfigGroupHostMapping(configGroupHostMappingEntity)); + set.add(buildConfigGroupHostMapping(configGroupHostMappingEntity)); + + return entityManagerProvider.get().merge(configGroupHostMappingEntity); } @Transactional public void refresh(ConfigGroupHostMappingEntity configGroupHostMappingEntity) { + cacheLoaded = false; + populateCache(); + entityManagerProvider.get().refresh(configGroupHostMappingEntity); } @Transactional - public void remove(ConfigGroupHostMappingEntity + public void remove(final ConfigGroupHostMappingEntity configGroupHostMappingEntity) { + + populateCache(); + entityManagerProvider.get().remove(merge(configGroupHostMappingEntity)); + + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntity.getHostname()); + + if (setByHost != null) { + CollectionUtils.filter(setByHost, new Predicate() { + + @Override + public boolean evaluate(Object arg0) { + return !((ConfigGroupHostMapping) arg0).getConfigGroupId(). + equals(configGroupHostMappingEntity.getConfigGroupId()); + } + }); + } } @Transactional - public void removeByPK(ConfigGroupHostMappingEntityPK + public void removeByPK(final ConfigGroupHostMappingEntityPK configGroupHostMappingEntityPK) { + populateCache(); + entityManagerProvider.get().remove(findByPK (configGroupHostMappingEntityPK)); + + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(configGroupHostMappingEntityPK.getHostname()); + + if (setByHost != null) { + CollectionUtils.filter(setByHost, new Predicate() { + + @Override + public boolean evaluate(Object arg0) { + return !((ConfigGroupHostMapping) arg0).getConfigGroupId(). + equals(configGroupHostMappingEntityPK.getConfigGroupId()); + } + }); + } + } @Transactional - public void removeAllByGroup(Long groupId) { + public void removeAllByGroup(final Long groupId) { TypedQuery<Long> query = entityManagerProvider.get().createQuery ("DELETE FROM ConfigGroupHostMappingEntity confighosts WHERE " + "confighosts.configGroupId = ?1", Long.class); @@ -111,6 +275,18 @@ public class ConfigGroupHostMappingDAO { // Flush to current transaction required in order to avoid Eclipse link // from re-ordering delete entityManagerProvider.get().flush(); + + for (Set<ConfigGroupHostMapping> setByHost : configGroupHostMappingByHost.values()) { + + CollectionUtils.filter(setByHost, new Predicate() { + + @Override + public boolean evaluate(Object arg0) { + return !((ConfigGroupHostMapping) arg0).getConfigGroupId().equals(groupId); + } + }); + } + } @Transactional @@ -120,5 +296,39 @@ public class ConfigGroupHostMappingDAO { "confighosts.hostname = ?1", String.class); daoUtils.executeUpdate(query, hostname); + + + Set<ConfigGroupHostMapping> setByHost = configGroupHostMappingByHost.get(hostname); + + setByHost.clear(); + + + } + + private ConfigGroupHostMapping buildConfigGroupHostMapping( + ConfigGroupHostMappingEntity configGroupHostMappingEntity) { + + ConfigGroupHostMappingImpl configGroupHostMapping = new ConfigGroupHostMappingImpl(); + configGroupHostMapping.setConfigGroup(buildConfigGroup(configGroupHostMappingEntity.getConfigGroupEntity())); + configGroupHostMapping.setConfigGroupId(configGroupHostMappingEntity.getConfigGroupId()); + configGroupHostMapping.setHost(buildHost(configGroupHostMappingEntity.getHostEntity())); + configGroupHostMapping.setHostname(configGroupHostMappingEntity.getHostname()); + + return configGroupHostMapping; + } + + private ConfigGroup buildConfigGroup(ConfigGroupEntity configGroupEntity) { + + Cluster cluster = clusterFactory.create(configGroupEntity.getClusterEntity()); + ConfigGroup configGroup = configGroupFactory.createExisting(cluster, configGroupEntity); + + return configGroup; + } + + private Host buildHost(HostEntity hostEntity) { + + Host host = hostFactory.create(hostEntity, false); + + return host; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 980cfda..e95a240 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 @@ -27,9 +27,9 @@ import javax.persistence.TypedQuery; import com.google.inject.Singleton; +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.state.HostConfigMapping; -import org.apache.ambari.server.state.HostConfigMappingImpl; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; @@ -71,7 +71,7 @@ public class HostConfigMappingDAO { for (HostConfigMappingEntity hostConfigMappingEntity : hostConfigMappingEntities) { - Set<HostConfigMapping> setByHost = hostConfigMappingByHost.get((hostConfigMappingEntity.getType())); + Set<HostConfigMapping> setByHost = hostConfigMappingByHost.get((hostConfigMappingEntity.getHostName())); if (setByHost == null) { setByHost = new HashSet<HostConfigMapping>(); http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMapping.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMapping.java b/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMapping.java deleted file mode 100644 index 4ad25db..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMapping.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ambari.server.state; - -public interface HostConfigMapping { - - public Long getClusterId(); - public void setClusterId(Long clusterId); - - public String getHostName(); - public void setHostName(String hostName); - - public String getType(); - public void setType(String type); - - public Long getCreateTimestamp(); - public void setCreateTimestamp(Long createTimestamp); - - public String getVersion(); - public void setVersion(String version); - - public String getServiceName(); - public void setServiceName(String serviceName); - - public String getUser(); - public void setUser(String user); - - public Integer getSelected(); - public void setSelected(Integer selected); - - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMappingImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMappingImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMappingImpl.java deleted file mode 100644 index 6905e27..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/HostConfigMappingImpl.java +++ /dev/null @@ -1,186 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.server.state; - -public class HostConfigMappingImpl implements HostConfigMapping { - - private Long clusterId; - private String hostName; - private String type; - private Long createTimestamp; - private String version; - private String serviceName; - private String user; - private Integer selected; - - - - public HostConfigMappingImpl(HostConfigMapping entry) { - setClusterId(entry.getClusterId()); - setHostName(entry.getHostName()); - setType(entry.getType()); - setCreateTimestamp(entry.getCreateTimestamp()); - setVersion(entry.getVersion()); - setServiceName(entry.getServiceName()); - setUser(entry.getUser()); - setSelected(entry.getSelected()); - } - - public HostConfigMappingImpl() { - } - - @Override - public Long getClusterId() { - return clusterId; - } - - @Override - public void setClusterId(Long clusterId) { - if (clusterId == null) - throw new RuntimeException("ClusterId couldn't be null"); - this.clusterId = clusterId; - } - - @Override - public String getHostName() { - return hostName; - } - - @Override - public void setHostName(String hostName) { - if (hostName == null) - throw new RuntimeException("HostName couldn't be null"); - this.hostName = hostName; - } - - @Override - public String getType() { - return type; - } - - @Override - public void setType(String type) { - if (type == null) - throw new RuntimeException("Type couldn't be null"); - this.type = type; - } - - @Override - public Long getCreateTimestamp() { - return createTimestamp; - } - - @Override - public void setCreateTimestamp(Long createTimestamp) { - if (createTimestamp == null) - throw new RuntimeException("CreateTimestamp couldn't be null"); - this.createTimestamp = createTimestamp; - } - - @Override - public String getVersion() { - return version; - } - - @Override - public void setVersion(String version) { - if (version == null) - throw new RuntimeException("Version couldn't be null"); - this.version = version; - } - public String getServiceName() { - return serviceName; - } - - @Override - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - } - - @Override - public String getUser() { - return user; - } - - @Override - public void setUser(String user) { - if (user == null) - throw new RuntimeException("User couldn't be null"); - this.user = user; - } - - @Override - public Integer getSelected() { - return selected; - } - - @Override - public void setSelected(Integer selected) { - if (selected == null) - throw new RuntimeException("Selected couldn't be null"); - this.selected = selected; - } - - @Override - public int hashCode() { - 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 + ((type == null) ? 0 : type.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - 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; - return true; - } - - -} http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 486779b..b19d239 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 @@ -27,6 +27,8 @@ import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.ServiceComponentHostNotFoundException; import org.apache.ambari.server.ServiceNotFoundException; import org.apache.ambari.server.controller.ClusterResponse; +import org.apache.ambari.server.orm.cache.ConfigGroupHostMapping; +import org.apache.ambari.server.orm.cache.HostConfigMapping; import org.apache.ambari.server.orm.dao.ClusterDAO; import org.apache.ambari.server.orm.dao.ClusterStateDAO; import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO; @@ -43,7 +45,6 @@ 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.DesiredConfig; -import org.apache.ambari.server.state.HostConfigMapping; import org.apache.ambari.server.state.Service; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; @@ -332,11 +333,11 @@ public class ClusterImpl implements Cluster { try { readLock.lock(); try { - List<ConfigGroupHostMappingEntity> hostMappingEntities = + Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO.findByHost(hostname); if (hostMappingEntities != null && !hostMappingEntities.isEmpty()) { - for (ConfigGroupHostMappingEntity entity : hostMappingEntities) { + for (ConfigGroupHostMapping entity : hostMappingEntities) { ConfigGroup configGroup = configGroupMap.get(entity.getConfigGroupId()); if (configGroup != null && !configGroups.containsKey(configGroup.getId())) { configGroups.put(configGroup.getId(), configGroup); http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 f8ec21d..8ec257e 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 @@ -20,13 +20,10 @@ package org.apache.ambari.server.state.configgroup; import com.google.inject.persist.Transactional; import org.apache.ambari.server.AmbariException; -import org.apache.ambari.server.DuplicateResourceException; import org.apache.ambari.server.controller.ConfigGroupResponse; -import org.apache.ambari.server.orm.entities.ConfigGroupEntity; import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.Host; -import java.util.List; import java.util.Map; /** http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 4ba569d..d4597af 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 @@ -24,7 +24,6 @@ import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.configgroup.ConfigGroup; -import java.util.List; import java.util.Map; public interface ConfigGroupFactory { http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 0162b80..61ad928 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 @@ -26,9 +26,7 @@ import com.google.inject.persist.Transactional; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.DuplicateResourceException; import org.apache.ambari.server.controller.ConfigGroupResponse; -import org.apache.ambari.server.controller.internal.ConfigGroupResourceProvider; import org.apache.ambari.server.controller.internal.ConfigurationResourceProvider; -import org.apache.ambari.server.controller.utilities.PropertyHelper; import org.apache.ambari.server.orm.dao.ClusterDAO; import org.apache.ambari.server.orm.dao.ConfigGroupConfigMappingDAO; import org.apache.ambari.server.orm.dao.ConfigGroupDAO; @@ -47,14 +45,12 @@ 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.eclipse.persistence.sessions.UnitOfWork; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.concurrent.locks.ReadWriteLock; http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/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 78f18fa..f8bf36e 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 @@ -31,6 +31,8 @@ import org.apache.ambari.server.agent.AgentEnv; import org.apache.ambari.server.agent.DiskInfo; import org.apache.ambari.server.agent.HostInfo; import org.apache.ambari.server.controller.HostResponse; +import org.apache.ambari.server.orm.cache.HostConfigMapping; +import org.apache.ambari.server.orm.cache.HostConfigMappingImpl; import org.apache.ambari.server.orm.dao.ClusterDAO; import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO; import org.apache.ambari.server.orm.dao.HostConfigMappingDAO; @@ -46,8 +48,6 @@ import org.apache.ambari.server.state.Config; import org.apache.ambari.server.state.DesiredConfig; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.HostConfig; -import org.apache.ambari.server.state.HostConfigMapping; -import org.apache.ambari.server.state.HostConfigMappingImpl; import org.apache.ambari.server.state.HostEvent; import org.apache.ambari.server.state.HostEventType; import org.apache.ambari.server.state.HostHealthStatus; http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java index 8424082..dd0b1f9 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/ConfigGroupDAOTest.java @@ -24,6 +24,7 @@ import junit.framework.Assert; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.cache.ConfigGroupHostMapping; import org.apache.ambari.server.orm.entities.ClusterConfigEntity; import org.apache.ambari.server.orm.entities.ClusterEntity; import org.apache.ambari.server.orm.entities.ConfigGroupConfigMappingEntity; @@ -36,6 +37,7 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; +import java.util.Set; public class ConfigGroupDAOTest { private Injector injector; @@ -191,13 +193,16 @@ public class ConfigGroupDAOTest { Assert.assertNotNull(configGroupEntity .getConfigGroupHostMappingEntities().iterator().next()); - List<ConfigGroupHostMappingEntity> hostMappingEntities = configGroupHostMappingDAO + Set<ConfigGroupHostMapping> hostMappingEntities = configGroupHostMappingDAO .findByHost("h1"); Assert.assertNotNull(hostMappingEntities); - Assert.assertEquals("h1", hostMappingEntities.get(0).getHostname()); - Assert.assertEquals("centOS", hostMappingEntities.get(0).getHostEntity() - .getOsType()); + + for (ConfigGroupHostMapping hostMappingEntity : hostMappingEntities) { + + Assert.assertEquals("h1", hostMappingEntity.getHostname()); + Assert.assertEquals("centOS", hostMappingEntity.getHost().getOsType()); + } } @Test http://git-wip-us.apache.org/repos/asf/ambari/blob/10127a25/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java index 50b070e..ec1289a 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/HostConfigMappingDAOTest.java @@ -24,8 +24,8 @@ import junit.framework.Assert; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; -import org.apache.ambari.server.state.HostConfigMapping; -import org.apache.ambari.server.state.HostConfigMappingImpl; +import org.apache.ambari.server.orm.cache.HostConfigMapping; +import org.apache.ambari.server.orm.cache.HostConfigMappingImpl; import org.junit.After; import org.junit.Before; import org.junit.Test;
