Return-Path: X-Original-To: apmail-incubator-ambari-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-ambari-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 32341D248 for ; Tue, 16 Oct 2012 06:39:49 +0000 (UTC) Received: (qmail 21023 invoked by uid 500); 16 Oct 2012 06:39:49 -0000 Delivered-To: apmail-incubator-ambari-commits-archive@incubator.apache.org Received: (qmail 20993 invoked by uid 500); 16 Oct 2012 06:39:48 -0000 Mailing-List: contact ambari-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ambari-dev@incubator.apache.org Delivered-To: mailing list ambari-commits@incubator.apache.org Received: (qmail 20978 invoked by uid 99); 16 Oct 2012 06:39:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Oct 2012 06:39:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Oct 2012 06:39:45 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 622792388AA6; Tue, 16 Oct 2012 06:38:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1398669 [4/5] - in /incubator/ambari/branches/AMBARI-666: ./ ambari-project/ ambari-server/ ambari-server/src/main/java/org/apache/ambari/server/actionmanager/ ambari-server/src/main/java/org/apache/ambari/server/agent/ ambari-server/src/m... Date: Tue, 16 Oct 2012 06:38:26 -0000 To: ambari-commits@incubator.apache.org From: mahadev@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121016063854.622792388AA6@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java Tue Oct 16 06:38:23 2012 @@ -26,17 +26,25 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.Singleton; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.ClusterNotFoundException; import org.apache.ambari.server.HostNotFoundException; +import org.apache.ambari.server.orm.dao.ClusterDAO; +import org.apache.ambari.server.orm.dao.HostDAO; +import org.apache.ambari.server.orm.entities.ClusterEntity; +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.Host; +import org.apache.ambari.server.state.host.HostFactory; import org.apache.ambari.server.state.host.HostImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.inject.Singleton; +import javax.persistence.RollbackException; @Singleton public class ClustersImpl implements Clusters { @@ -49,7 +57,21 @@ public class ClustersImpl implements Clu private Map hosts; private Map> hostClusterMap; - public ClustersImpl() { + private Injector injector; + private ClusterDAO clusterDAO; + private HostDAO hostDAO; + private ClusterFactory clusterFactory; + private HostFactory hostFactory; + + @Inject + public ClustersImpl(Injector injector) { + this.injector = injector; + + this.clusterDAO = injector.getInstance(ClusterDAO.class); + this.hostDAO = injector.getInstance(HostDAO.class); + this.clusterFactory = injector.getInstance(ClusterFactory.class); + this.hostFactory = injector.getInstance(HostFactory.class); + clusters = new HashMap(); clustersById = new HashMap(); hosts = new HashMap(); @@ -64,17 +86,21 @@ public class ClustersImpl implements Clu throw new AmbariException("Duplicate entry for Cluster" + ", clusterName= " + clusterName); } - // TODO persist cluster into DB + // retrieve new cluster id // add cluster id -> cluster mapping into clustersById - long clusterId = clusterName.hashCode(); - Cluster impl = new ClusterImpl(this, clusterId, clusterName); - clusters.put(clusterName, impl); - clustersById.put(clusterId, impl); - if (LOG.isDebugEnabled()) { - LOG.debug("Adding a new cluster" - + ", clusterName=" + clusterName - + ", clusterId=" + clusterId); + ClusterEntity clusterEntity = new ClusterEntity(); + clusterEntity.setClusterName(clusterName); + try { + clusterDAO.create(clusterEntity); + clusterEntity = clusterDAO.merge(clusterEntity); + Cluster cluster = clusterFactory.create(clusterEntity); + + clusters.put(clusterName, cluster); + clustersById.put(cluster.getClusterId(), cluster); + } catch (RollbackException e) { + LOG.warn("Unable to create cluster " + clusterName, e); + throw new AmbariException("Unable to create cluster " + clusterName, e); } } @@ -82,21 +108,54 @@ public class ClustersImpl implements Clu public synchronized Cluster getCluster(String clusterName) throws AmbariException { if (!clusters.containsKey(clusterName)) { - throw new ClusterNotFoundException(clusterName); + ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); + if (clusterEntity != null) { + return getClusterById(clusterEntity.getClusterId()); + } else { + throw new ClusterNotFoundException(clusterName); + } } return clusters.get(clusterName); } @Override + public synchronized Cluster getClusterById(long id) throws AmbariException { + if (!clustersById.containsKey(id)) { + ClusterEntity clusterEntity = clusterDAO.findById(id); + if (clusterEntity != null) { + Cluster cluster = clusterFactory.create(clusterEntity); + clustersById.put(cluster.getClusterId(), cluster); + clusters.put(clusterEntity.getClusterName(), cluster); + } else { + throw new ClusterNotFoundException("clusterID=" + id); + } + } + return clustersById.get(id); + } + + @Override public synchronized List getHosts() { - return new ArrayList(hosts.values()); + List hostList = new ArrayList(hosts.size()); + hostList.addAll(hosts.values()); + + for (HostEntity hostEntity : hostDAO.findAll()) { + if (!hosts.containsKey(hostEntity.getHostName())) { + try { + hostList.add(getHost(hostEntity.getHostName())); + } catch (AmbariException ignored) { + LOG.error("Database externally modified?"); + } + } + } + + return hostList; } @Override public synchronized Set getClustersForHost(String hostname) throws AmbariException { if (!hostClusterMap.containsKey(hostname)) { - throw new HostNotFoundException(hostname); + getHost(hostname); } return Collections.unmodifiableSet(hostClusterMap.get(hostname)); } @@ -104,7 +163,24 @@ public class ClustersImpl implements Clu @Override public synchronized Host getHost(String hostname) throws AmbariException { if (!hosts.containsKey(hostname)) { - throw new HostNotFoundException(hostname); + HostEntity hostEntity = hostDAO.findByName(hostname); + if (hostEntity != null) { + Host host = hostFactory.create(hostEntity, true); + + Set cSet = new HashSet(); + for (ClusterEntity clusterEntity : hostEntity.getClusterEntities()) { + if (clustersById.containsKey(clusterEntity.getClusterId())) { + cSet.add(clustersById.get(clusterEntity.getClusterId())); + } else { + cSet.add(getClusterById(clusterEntity.getClusterId())); + } + } + + hosts.put(hostname, host); + hostClusterMap.put(hostname, cSet); + } else { + throw new HostNotFoundException(hostname); + } } return hosts.get(hostname); } @@ -115,7 +191,13 @@ public class ClustersImpl implements Clu throw new AmbariException("Duplicate entry for Host" + ", hostName= " + hostname); } - hosts.put(hostname, new HostImpl(hostname)); + HostEntity hostEntity = new HostEntity(); + hostEntity.setHostName(hostname); + hostEntity.setClusterEntities(new ArrayList()); + //not stored to DB + Host host = hostFactory.create(hostEntity, false); + + hosts.put(hostname, host); hostClusterMap.put(hostname, new HashSet()); if (LOG.isDebugEnabled()) { LOG.debug("Adding a host to Clusters" @@ -125,44 +207,53 @@ public class ClustersImpl implements Clu @Override public synchronized void mapHostToCluster(String hostname, - String clusterName) throws AmbariException { - Cluster c = getCluster(clusterName); - getHost(hostname); + String clusterName) throws AmbariException { + ClusterImpl cluster = (ClusterImpl) getCluster(clusterName); + HostImpl host = (HostImpl) getHost(hostname); + if (!hostClusterMap.containsKey(hostname)) { throw new HostNotFoundException(hostname); } - hostClusterMap.get(hostname).add(c); + + host.addToCluster(cluster); + + hostClusterMap.get(hostname).add(cluster); if (LOG.isDebugEnabled()) { LOG.debug("Mapping a host to a cluster" + ", clusterName=" + clusterName - + ", clusterId=" + c.getClusterId() + + ", clusterId=" + cluster.getClusterId() + ", hostname=" + hostname); } } @Override public synchronized Map getClusters() { + for (ClusterEntity clusterEntity : clusterDAO.findAll()) { + try { + if (!clustersById.containsKey(clusterEntity.getClusterId())) { + getClusterById(clusterEntity.getClusterId()); + } + } catch (AmbariException ignored) { + + } + } return Collections.unmodifiableMap(clusters); } @Override public synchronized void mapHostsToCluster(Set hostnames, - String clusterName) throws AmbariException { - Cluster c = getCluster(clusterName); + String clusterName) throws AmbariException { for (String hostname : hostnames) { - if (!hostClusterMap.containsKey(hostname)) { - throw new HostNotFoundException(hostname); - } - hostClusterMap.get(hostname).add(c); - if (LOG.isDebugEnabled()) { - LOG.debug("Mapping a host to a cluster" - + ", clusterName=" + clusterName - + ", clusterId=" + c.getClusterId() - + ", hostname=" + hostname); - } + mapHostToCluster(hostname, clusterName); } } + @Override + public synchronized void updateClusterName(String oldName, String newName) { + clusters.put(newName, clusters.remove(oldName)); + } + + public void debugDump(StringBuilder sb) { sb.append("Clusters=[ "); boolean first = true; Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostFactory.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostFactory.java?rev=1398669&view=auto ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostFactory.java (added) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostFactory.java Tue Oct 16 06:38:23 2012 @@ -0,0 +1,25 @@ +/** + * 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.host; + +import org.apache.ambari.server.orm.entities.HostEntity; +import org.apache.ambari.server.state.Host; + +public interface HostFactory { + Host create(HostEntity hostEntity, boolean persisted); +} Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java Tue Oct 16 06:38:23 2012 @@ -19,119 +19,58 @@ package org.apache.ambari.server.state.host; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.lang.reflect.Type; +import java.util.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.assistedinject.Assisted; +import com.google.inject.persist.Transactional; +import org.apache.ambari.server.AmbariException; 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.state.AgentVersion; -import org.apache.ambari.server.state.Host; -import org.apache.ambari.server.state.HostEvent; -import org.apache.ambari.server.state.HostEventType; -import org.apache.ambari.server.state.HostHealthStatus; -import org.apache.ambari.server.state.HostState; +import org.apache.ambari.server.orm.dao.ClusterDAO; +import org.apache.ambari.server.orm.dao.HostStateDAO; +import org.apache.ambari.server.orm.entities.ClusterEntity; +import org.apache.ambari.server.orm.entities.HostStateEntity; +import org.apache.ambari.server.state.*; import org.apache.ambari.server.state.HostHealthStatus.HealthStatus; import org.apache.ambari.server.state.fsm.InvalidStateTransitonException; import org.apache.ambari.server.state.fsm.SingleArcTransition; import org.apache.ambari.server.state.fsm.StateMachine; import org.apache.ambari.server.state.fsm.StateMachineFactory; import org.apache.ambari.server.state.job.Job; +import org.apache.ambari.server.orm.dao.HostDAO; +import org.apache.ambari.server.orm.entities.HostEntity; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class HostImpl implements Host { private static final Log LOG = LogFactory.getLog(HostImpl.class); + private final Gson gson; + + private static final Type diskInfoType = new TypeToken>() {}.getType(); + private static final Type hostAttributesType = new TypeToken>() {}.getType(); private final Lock readLock; private final Lock writeLock; - /** - * Host hostname - */ - private String hostName; - - /** - * Host IP if ipv4 interface available - */ - private String ipv4; - - /** - * Host IP if ipv6 interface available - */ - private String ipv6; - - /** - * Count of cores on Host - */ - private int cpuCount; - - /** - * Os Architecture - */ - private String osArch; - - /** - * OS Type - */ - private String osType; - - /** - * OS Information - */ - private String osInfo; - - /** - * Amount of available memory for the Host - */ - private long availableMemBytes; - - /** - * Amount of physical memory for the Host - */ - private long totalMemBytes; - - /** - * Disks mounted on the Host - */ - private List disksInfo; + private HostEntity hostEntity; + private HostStateEntity hostStateEntity; + private Injector injector; + private HostDAO hostDAO; + private HostStateDAO hostStateDAO; + private ClusterDAO clusterDAO; + private Clusters clusters; - /** - * Last heartbeat timestamp from the Host - */ - private long lastHeartbeatTime; - - /** - * Last registration timestamp for the Host - */ - private long lastRegistrationTime; - - /** - * Rack to which the Host belongs to - */ - private String rackInfo; - - /** - * Additional Host attributes - */ - private Map hostAttributes; - - /** - * Version of agent running on the Host - */ - private AgentVersion agentVersion; - - /** - * Host Health Status - */ - private HostHealthStatus healthStatus; + private boolean persisted = false; private static final StateMachineFactory @@ -212,30 +151,47 @@ public class HostImpl implements Host { private final StateMachine stateMachine; - public HostImpl(String hostName) { - super(); - this.hostName = hostName; + @Inject + public HostImpl(@Assisted HostEntity hostEntity, @Assisted boolean persisted, Injector injector) { this.stateMachine = stateMachineFactory.make(this); ReadWriteLock rwLock = new ReentrantReadWriteLock(); this.readLock = rwLock.readLock(); this.writeLock = rwLock.writeLock(); - this.healthStatus = new HostHealthStatus(HealthStatus.UNKNOWN, ""); - this.ipv4 = ""; - this.ipv6 = ""; - this.cpuCount = 0; - this.osArch = ""; - this.osType = ""; - this.osInfo = ""; - this.availableMemBytes = 0; - this.totalMemBytes = 0; - this.disksInfo = new ArrayList(); - this.lastHeartbeatTime = 0; - this.lastRegistrationTime = 0; - this.rackInfo = ""; - this.hostAttributes = new HashMap(); - this.agentVersion = new AgentVersion(""); + + this.hostEntity = hostEntity; + this.injector = injector; + this.persisted = persisted; + this.hostDAO = injector.getInstance(HostDAO.class); + this.hostStateDAO = injector.getInstance(HostStateDAO.class); + this.gson = injector.getInstance(Gson.class); + this.clusterDAO = injector.getInstance(ClusterDAO.class); + this.clusters = injector.getInstance(Clusters.class); + + hostStateEntity = hostEntity.getHostStateEntity(); + if (hostStateEntity == null) { + hostStateEntity = new HostStateEntity(); + hostStateEntity.setHostEntity(hostEntity); + hostEntity.setHostStateEntity(hostStateEntity); + setHealthStatus(new HostHealthStatus(HealthStatus.UNKNOWN, "")); + if (persisted) { + persist(); + } + } else { + this.stateMachine.setCurrentState(hostStateEntity.getCurrentState()); + } + } +// //TODO remove +// public HostImpl(String hostname) { +// this.stateMachine = stateMachineFactory.make(this); +// ReadWriteLock rwLock = new ReentrantReadWriteLock(); +// this.readLock = rwLock.readLock(); +// this.writeLock = rwLock.writeLock(); +// setHostName(hostname); +// setHealthStatus(new HostHealthStatus(HealthStatus.UNKNOWN, "")); +// } + static class HostRegistrationReceived implements SingleArcTransition { @@ -258,7 +214,7 @@ public class HostImpl implements Host { LOG.debug("Host transition to host status updates received state" + ", host=" + e.getHostName() + ", heartbeatTime=" + e.getTimestamp()); - host.getHealthStatus().setHealthStatus(HealthStatus.HEALTHY); + host.setHealthStatus(new HostHealthStatus(HealthStatus.HEALTHY, host.getHealthStatus().getHealthReport())); } } @@ -296,9 +252,9 @@ public class HostImpl implements Host { host.setLastHeartbeatTime(e.getHeartbeatTime()); // TODO Audit logs LOG.debug("Host transitioned to a healthy state" - + ", host=" + e.getHostName() - + ", heartbeatTime=" + e.getHeartbeatTime()); - host.getHealthStatus().setHealthStatus(HealthStatus.HEALTHY); + + ", host=" + e.getHostName() + + ", heartbeatTime=" + e.getHeartbeatTime()); + host.setHealthStatus(new HostHealthStatus(HealthStatus.HEALTHY, host.getHealthStatus().getHealthReport())); } } @@ -328,20 +284,24 @@ public class HostImpl implements Host { LOG.debug("Host transitioned to heartbeat lost state" + ", host=" + e.getHostName() + ", lastHeartbeatTime=" + host.getLastHeartbeatTime()); - host.getHealthStatus().setHealthStatus(HealthStatus.UNKNOWN); + host.setHealthStatus(new HostHealthStatus(HealthStatus.UNKNOWN, host.getHealthStatus().getHealthReport())); } } - void importHostInfo(HostInfo hostInfo) { + @Override + public void importHostInfo(HostInfo hostInfo) { try { writeLock.lock(); - this.hostName = hostInfo.getHostName(); - this.availableMemBytes = hostInfo.getFreeMemory(); - this.totalMemBytes = hostInfo.getMemoryTotal(); - this.cpuCount = hostInfo.getProcessorCount(); - this.osArch = hostInfo.getArchitecture(); - this.osType = hostInfo.getOS(); - this.disksInfo = hostInfo.getMounts(); + persisted = false; + setHostName(hostInfo.getHostName()); + setCpuCount(hostInfo.getProcessorCount()); + setTotalMemBytes(hostInfo.getMemoryTotal()); + setOsArch(hostInfo.getArchitecture()); + setOsType(hostInfo.getOS()); + setDisksInfo(hostInfo.getMounts()); + setAvailableMemBytes(hostInfo.getFreeMemory()); + + saveIfPersisted(); } finally { writeLock.unlock(); @@ -364,6 +324,8 @@ public class HostImpl implements Host { try { writeLock.lock(); stateMachine.setCurrentState(state); + hostStateEntity.setCurrentState(state); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -410,9 +372,8 @@ public class HostImpl implements Host { public String getHostName() { try { readLock.lock(); - return hostName; - } - finally { + return hostEntity.getHostName(); + } finally { readLock.unlock(); } } @@ -421,9 +382,12 @@ public class HostImpl implements Host { public void setHostName(String hostName) { try { writeLock.lock(); - this.hostName = hostName; - } - finally { + if (!isPersisted()) { + hostEntity.setHostName(hostName); + } else { + throw new UnsupportedOperationException("PK of persisted entity cannot be modified"); + } + } finally { writeLock.unlock(); } } @@ -432,9 +396,8 @@ public class HostImpl implements Host { public String getIPv4() { try { readLock.lock(); - return ipv4; - } - finally { + return hostEntity.getIpv4(); + } finally { readLock.unlock(); } } @@ -443,9 +406,9 @@ public class HostImpl implements Host { public void setIPv4(String ip) { try { writeLock.lock(); - this.ipv4 = ip; - } - finally { + hostEntity.setIpv4(ip); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -454,9 +417,8 @@ public class HostImpl implements Host { public String getIPv6() { try { readLock.lock(); - return ipv6; - } - finally { + return hostEntity.getIpv6(); + } finally { readLock.unlock(); } } @@ -465,9 +427,9 @@ public class HostImpl implements Host { public void setIPv6(String ip) { try { writeLock.lock(); - this.ipv6 = ip; - } - finally { + hostEntity.setIpv6(ip); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -476,9 +438,8 @@ public class HostImpl implements Host { public int getCpuCount() { try { readLock.lock(); - return cpuCount; - } - finally { + return hostEntity.getCpuCount(); + } finally { readLock.unlock(); } } @@ -487,9 +448,9 @@ public class HostImpl implements Host { public void setCpuCount(int cpuCount) { try { writeLock.lock(); - this.cpuCount = cpuCount; - } - finally { + hostEntity.setCpuCount(cpuCount); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -498,9 +459,8 @@ public class HostImpl implements Host { public long getTotalMemBytes() { try { readLock.lock(); - return totalMemBytes; - } - finally { + return hostEntity.getTotalMem(); + } finally { readLock.unlock(); } } @@ -509,9 +469,9 @@ public class HostImpl implements Host { public void setTotalMemBytes(long totalMemBytes) { try { writeLock.lock(); - this.totalMemBytes = totalMemBytes; - } - finally { + hostEntity.setTotalMem(totalMemBytes); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -520,7 +480,7 @@ public class HostImpl implements Host { public long getAvailableMemBytes() { try { readLock.lock(); - return availableMemBytes; + return hostStateEntity.getAvailableMem(); } finally { readLock.unlock(); @@ -531,7 +491,8 @@ public class HostImpl implements Host { public void setAvailableMemBytes(long availableMemBytes) { try { writeLock.lock(); - this.availableMemBytes = availableMemBytes; + hostStateEntity.setAvailableMem(availableMemBytes); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -542,9 +503,8 @@ public class HostImpl implements Host { public String getOsArch() { try { readLock.lock(); - return osArch; - } - finally { + return hostEntity.getOsArch(); + } finally { readLock.unlock(); } } @@ -553,9 +513,9 @@ public class HostImpl implements Host { public void setOsArch(String osArch) { try { writeLock.lock(); - this.osArch = osArch; - } - finally { + hostEntity.setOsArch(osArch); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -564,9 +524,8 @@ public class HostImpl implements Host { public String getOsInfo() { try { readLock.lock(); - return osInfo; - } - finally { + return hostEntity.getOsInfo(); + } finally { readLock.unlock(); } } @@ -575,9 +534,9 @@ public class HostImpl implements Host { public void setOsInfo(String osInfo) { try { writeLock.lock(); - this.osInfo = osInfo; - } - finally { + hostEntity.setOsInfo(osInfo); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -586,9 +545,8 @@ public class HostImpl implements Host { public String getOsType() { try { readLock.lock(); - return osType; - } - finally { + return hostEntity.getOsType(); + } finally { readLock.unlock(); } } @@ -597,9 +555,9 @@ public class HostImpl implements Host { public void setOsType(String osType) { try { writeLock.lock(); - this.osType = osType; - } - finally { + hostEntity.setOsType(osType); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -608,9 +566,8 @@ public class HostImpl implements Host { public List getDisksInfo() { try { readLock.lock(); - return Collections.unmodifiableList(disksInfo); - } - finally { + return Collections.unmodifiableList(gson.>fromJson(hostEntity.getDisksInfo(), diskInfoType)); + } finally { readLock.unlock(); } } @@ -619,9 +576,9 @@ public class HostImpl implements Host { public void setDisksInfo(List disksInfo) { try { writeLock.lock(); - this.disksInfo = disksInfo; - } - finally { + hostEntity.setDisksInfo(gson.toJson(disksInfo, diskInfoType)); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -630,9 +587,8 @@ public class HostImpl implements Host { public HostHealthStatus getHealthStatus() { try { readLock.lock(); - return healthStatus; - } - finally { + return gson.fromJson(hostStateEntity.getHealthStatus(), HostHealthStatus.class); + } finally { readLock.unlock(); } } @@ -641,9 +597,9 @@ public class HostImpl implements Host { public void setHealthStatus(HostHealthStatus healthStatus) { try { writeLock.lock(); - this.healthStatus = healthStatus; - } - finally { + hostStateEntity.setHealthStatus(gson.toJson(healthStatus)); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -652,9 +608,9 @@ public class HostImpl implements Host { public Map getHostAttributes() { try { readLock.lock(); - return Collections.unmodifiableMap(hostAttributes); - } - finally { + return Collections.unmodifiableMap( + gson.>fromJson(hostEntity.getHostAttributes(), hostAttributesType)); + } finally { readLock.unlock(); } } @@ -663,9 +619,10 @@ public class HostImpl implements Host { public void setHostAttributes(Map hostAttributes) { try { writeLock.lock(); - this.hostAttributes.putAll(hostAttributes); - } - finally { + //TODO should this add attributes and not replace them? + hostEntity.setHostAttributes(gson.toJson(hostAttributes, hostAttributesType)); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -674,9 +631,8 @@ public class HostImpl implements Host { public String getRackInfo() { try { readLock.lock(); - return rackInfo; - } - finally { + return hostEntity.getRackInfo(); + } finally { readLock.unlock(); } } @@ -685,9 +641,9 @@ public class HostImpl implements Host { public void setRackInfo(String rackInfo) { try { writeLock.lock(); - this.rackInfo = rackInfo; - } - finally { + hostEntity.setRackInfo(rackInfo); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -696,9 +652,8 @@ public class HostImpl implements Host { public long getLastRegistrationTime() { try { readLock.lock(); - return lastRegistrationTime; - } - finally { + return hostEntity.getLastRegistrationTime(); + } finally { readLock.unlock(); } } @@ -707,9 +662,9 @@ public class HostImpl implements Host { public void setLastRegistrationTime(long lastRegistrationTime) { try { writeLock.lock(); - this.lastRegistrationTime = lastRegistrationTime; - } - finally { + this.hostEntity.setLastRegistrationTime(lastRegistrationTime); + saveIfPersisted(); + } finally { writeLock.unlock(); } } @@ -718,7 +673,7 @@ public class HostImpl implements Host { public long getLastHeartbeatTime() { try { readLock.lock(); - return lastHeartbeatTime; + return hostStateEntity.getLastHeartbeatTime(); } finally { readLock.unlock(); @@ -729,7 +684,8 @@ public class HostImpl implements Host { public void setLastHeartbeatTime(long lastHeartbeatTime) { try { writeLock.lock(); - this.lastHeartbeatTime = lastHeartbeatTime; + hostStateEntity.setLastHeartbeatTime(lastHeartbeatTime); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -740,7 +696,7 @@ public class HostImpl implements Host { public AgentVersion getAgentVersion() { try { readLock.lock(); - return agentVersion; + return gson.fromJson(hostStateEntity.getAgentVersion(), AgentVersion.class); } finally { readLock.unlock(); @@ -751,7 +707,8 @@ public class HostImpl implements Host { public void setAgentVersion(AgentVersion agentVersion) { try { writeLock.lock(); - this.agentVersion = agentVersion; + hostStateEntity.setAgentVersion(gson.toJson(agentVersion)); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -771,26 +728,26 @@ public class HostImpl implements Host { } @Override - public synchronized HostResponse convertToResponse() { - HostResponse r = new HostResponse(hostName); + public HostResponse convertToResponse() { try { readLock.lock(); + HostResponse r = new HostResponse(getHostName()); - r.setAgentVersion(agentVersion); - r.setAvailableMemBytes(availableMemBytes); - r.setCpuCount(cpuCount); + r.setAgentVersion(getAgentVersion()); + r.setAvailableMemBytes(getAvailableMemBytes()); + r.setCpuCount(getCpuCount()); r.setDisksInfo(getDisksInfo()); - r.setHealthStatus(healthStatus); + r.setHealthStatus(getHealthStatus()); r.setHostAttributes(getHostAttributes()); - r.setIpv4(ipv4); - r.setIpv6(ipv6); - r.setLastHeartbeatTime(lastHeartbeatTime); - r.setLastRegistrationTime(lastRegistrationTime); - r.setOsArch(osArch); - r.setOsInfo(osInfo); - r.setOsType(osType); - r.setRackInfo(rackInfo); - r.setTotalMemBytes(totalMemBytes); + r.setIpv4(getIPv4()); + r.setIpv6(getIPv6()); + r.setLastHeartbeatTime(getLastHeartbeatTime()); + r.setLastRegistrationTime(getLastRegistrationTime()); + r.setOsArch(getOsArch()); + r.setOsInfo(getOsInfo()); + r.setOsType(getOsType()); + r.setRackInfo(getRackInfo()); + r.setTotalMemBytes(getTotalMemBytes()); return r; } @@ -799,4 +756,93 @@ public class HostImpl implements Host { } } + @Transactional + public void addToCluster(Cluster cluster) { + ClusterEntity clusterEntity = clusterDAO.findById(cluster.getClusterId()); + if (isPersisted()) { + hostEntity.getClusterEntities().add(clusterEntity); + clusterEntity.getHostEntities().add(hostEntity); + clusterDAO.merge(clusterEntity); + hostDAO.merge(hostEntity); + cluster.refresh(); + } else { + Collection clusters = hostEntity.getClusterEntities(); + if (clusters == null) { + hostEntity.setClusterEntities(new ArrayList()); + } + hostEntity.getClusterEntities().add(clusterEntity); + } + } + + /** + * Shows if Host is persisted to database + * + * @return true if persisted + */ + @Override + public boolean isPersisted() { + try { + readLock.lock(); + return persisted; + } finally { + readLock.unlock(); + } + } + + /** + * Save host to database and make all changes to be saved afterwards + */ + @Override + @Transactional + public void persist() { + try { + writeLock.lock(); + if (!persisted) { + hostDAO.create(hostEntity); + hostStateDAO.create(hostStateEntity); + if (!hostEntity.getClusterEntities().isEmpty()) { + for (ClusterEntity clusterEntity : hostEntity.getClusterEntities()) { + clusterEntity.getHostEntities().add(hostEntity); + clusterDAO.merge(clusterEntity); + try { + clusters.getClusterById(clusterEntity.getClusterId()).refresh(); + } catch (AmbariException e) { + LOG.error(e); + throw new RuntimeException("Cluster '" + clusterEntity.getClusterId() + "' was removed", e); + } + } + } + hostEntity = hostDAO.merge(hostEntity); + hostStateEntity = hostStateDAO.merge(hostStateEntity); + persisted = true; + } else { + hostDAO.merge(hostEntity); + hostStateDAO.merge(hostStateEntity); + } + } finally { + writeLock.unlock(); + } + } + + @Override + public void refresh() { + try { + writeLock.lock(); + if (isPersisted()) { + hostEntity = hostDAO.findByName(hostEntity.getHostName()); + hostStateEntity = hostEntity.getHostStateEntity(); + hostDAO.refresh(hostEntity); + hostStateDAO.refresh(hostStateEntity); + } + } finally { + writeLock.unlock(); + } + } + + private void saveIfPersisted() { + if (isPersisted()) { + hostDAO.merge(hostEntity); + hostStateDAO.merge(hostStateEntity); + } + } } Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java Tue Oct 16 06:38:23 2012 @@ -26,14 +26,17 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import com.google.gson.Gson; +import com.google.inject.Inject; +import com.google.inject.Injector; +import com.google.inject.assistedinject.Assisted; +import com.google.inject.assistedinject.AssistedInject; +import com.google.inject.persist.Transactional; +import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.controller.ServiceComponentHostResponse; -import org.apache.ambari.server.state.Config; -import org.apache.ambari.server.state.State; -import org.apache.ambari.server.state.ServiceComponent; -import org.apache.ambari.server.state.ServiceComponentHost; -import org.apache.ambari.server.state.ServiceComponentHostEvent; -import org.apache.ambari.server.state.ServiceComponentHostEventType; -import org.apache.ambari.server.state.StackVersion; +import org.apache.ambari.server.orm.dao.*; +import org.apache.ambari.server.orm.entities.*; +import org.apache.ambari.server.state.*; import org.apache.ambari.server.state.fsm.InvalidStateTransitonException; import org.apache.ambari.server.state.fsm.SingleArcTransition; import org.apache.ambari.server.state.fsm.StateMachine; @@ -53,14 +56,27 @@ public class ServiceComponentHostImpl im private final Lock writeLock; private final ServiceComponent serviceComponent; - private final String hostName; + private final Host host; + private boolean persisted = false; + + @Inject + Gson gson; + @Inject + HostComponentStateDAO hostComponentStateDAO; + @Inject + HostComponentDesiredStateDAO hostComponentDesiredStateDAO; + @Inject + HostDAO hostDAO; + @Inject + ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO; + @Inject + Clusters clusters; + + private HostComponentStateEntity stateEntity; + private HostComponentDesiredStateEntity desiredStateEntity; private Map configs; private Map desiredConfigs; - private StackVersion stackVersion; - private StackVersion desiredStackVersion; - - private State desiredState; private long lastOpStartTime; private long lastOpEndTime; @@ -409,25 +425,70 @@ public class ServiceComponentHostImpl im } } - public ServiceComponentHostImpl(ServiceComponent serviceComponent, - String hostName, boolean isClient) { + @AssistedInject + public ServiceComponentHostImpl(@Assisted ServiceComponent serviceComponent, + @Assisted String hostName, @Assisted boolean isClient, Injector injector) { super(); + injector.injectMembers(this); + if (isClient) { this.stateMachine = clientStateMachineFactory.make(this); } else { this.stateMachine = daemonStateMachineFactory.make(this); } + ReadWriteLock rwLock = new ReentrantReadWriteLock(); this.readLock = rwLock.readLock(); this.writeLock = rwLock.writeLock(); this.serviceComponent = serviceComponent; - this.hostName = hostName; + + stateEntity = new HostComponentStateEntity(); + stateEntity.setCurrentState(stateMachine.getCurrentState()); + stateEntity.setCurrentStackVersion(gson.toJson(new StackVersion(""))); + + desiredStateEntity = new HostComponentDesiredStateEntity(); + desiredStateEntity.setDesiredStackVersion(gson.toJson(new StackVersion(""))); + + + try { + this.host = clusters.getHost(hostName); + } catch (AmbariException e) { + //TODO exception? + LOG.error("Host '{}' was not found", hostName); + throw new RuntimeException(e); + } this.resetLastOpInfo(); this.desiredConfigs = new HashMap(); - this.desiredStackVersion = new StackVersion(""); - this.desiredState = State.INIT; this.configs = new HashMap(); - this.stackVersion = new StackVersion(""); + } + + @AssistedInject + public ServiceComponentHostImpl(@Assisted ServiceComponent serviceComponent, + @Assisted HostComponentStateEntity stateEntity, + @Assisted HostComponentDesiredStateEntity desiredStateEntity, + Injector injector) { + injector.injectMembers(this); + ReadWriteLock rwLock = new ReentrantReadWriteLock(); + this.readLock = rwLock.readLock(); + this.writeLock = rwLock.writeLock(); + this.serviceComponent = serviceComponent; + + + this.desiredStateEntity = desiredStateEntity; + this.stateEntity = stateEntity; + //TODO implement State Machine init as now type choosing is hardcoded in above code + this.stateMachine = clientStateMachineFactory.make(this); + this.stateMachine.setCurrentState(stateEntity.getCurrentState()); + + try { + this.host = clusters.getHost(stateEntity.getHostName()); + } catch (AmbariException e) { + //TODO exception? impossible due to database restrictions + LOG.error("Host '{}' was not found", stateEntity.getHostName()); + throw new RuntimeException(e); + } + + persisted = true; } @Override @@ -446,6 +507,8 @@ public class ServiceComponentHostImpl im try { writeLock.lock(); stateMachine.setCurrentState(state); + stateEntity.setCurrentState(state); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -506,7 +569,7 @@ public class ServiceComponentHostImpl im @Override public String getHostName() { - return hostName; + return host.getHostName(); } /** @@ -623,7 +686,7 @@ public class ServiceComponentHostImpl im public StackVersion getStackVersion() { try { readLock.lock(); - return stackVersion; + return gson.fromJson(stateEntity.getCurrentStackVersion(), StackVersion.class); } finally { readLock.unlock(); @@ -634,7 +697,8 @@ public class ServiceComponentHostImpl im public void setStackVersion(StackVersion stackVersion) { try { writeLock.lock(); - this.stackVersion = stackVersion; + stateEntity.setCurrentStackVersion(gson.toJson(stackVersion)); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -646,7 +710,7 @@ public class ServiceComponentHostImpl im public State getDesiredState() { try { readLock.lock(); - return desiredState; + return desiredStateEntity.getDesiredState(); } finally { readLock.unlock(); @@ -657,7 +721,8 @@ public class ServiceComponentHostImpl im public void setDesiredState(State state) { try { writeLock.lock(); - this.desiredState = state; + desiredStateEntity.setDesiredState(state); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -690,7 +755,7 @@ public class ServiceComponentHostImpl im public StackVersion getDesiredStackVersion() { try { readLock.lock(); - return desiredStackVersion; + return gson.fromJson(desiredStateEntity.getDesiredStackVersion(), StackVersion.class); } finally { readLock.unlock(); @@ -701,7 +766,8 @@ public class ServiceComponentHostImpl im public void setDesiredStackVersion(StackVersion stackVersion) { try { writeLock.lock(); - this.desiredStackVersion = stackVersion; + desiredStateEntity.setDesiredStackVersion(gson.toJson(stackVersion)); + saveIfPersisted(); } finally { writeLock.unlock(); @@ -730,7 +796,7 @@ public class ServiceComponentHostImpl im serviceComponent.getClusterName(), serviceComponent.getServiceName(), serviceComponent.getName(), - hostName, + getHostName(), getConfigVersions(), getState().toString(), getStackVersion().getStackVersion(), @@ -751,13 +817,13 @@ public class ServiceComponentHostImpl im public void debugDump(StringBuilder sb) { try { readLock.lock(); - sb.append("ServiceComponentHost={ hostname=" + hostName + sb.append("ServiceComponentHost={ hostname=" + getHostName() + ", serviceComponentName=" + serviceComponent.getName() + ", clusterName=" + serviceComponent.getClusterName() + ", serviceName=" + serviceComponent.getServiceName() - + ", desiredStackVersion=" + desiredStackVersion - + ", desiredState=" + desiredState - + ", stackVersion=" + stackVersion + + ", desiredStackVersion=" + getDesiredStackVersion() + + ", desiredState=" + getDesiredState() + + ", stackVersion=" + getStackVersion() + ", state=" + getState() + " }"); } @@ -766,4 +832,84 @@ public class ServiceComponentHostImpl im } } + @Override + public boolean isPersisted() { + try { + readLock.lock(); + return persisted; + } finally { + readLock.unlock(); + } + } + + @Override + @Transactional + public void persist() { + try { + writeLock.lock(); + if (!persisted) { + serviceComponent.persist(); //TODO is this correct? + HostEntity hostEntity = hostDAO.findByName(getHostName()); + hostEntity.getHostComponentStateEntities().add(stateEntity); + hostEntity.getHostComponentDesiredStateEntities().add(desiredStateEntity); + + ServiceComponentDesiredStateEntityPK dpk = new ServiceComponentDesiredStateEntityPK(); + dpk.setClusterId(serviceComponent.getClusterId()); + dpk.setServiceName(serviceComponent.getServiceName()); + dpk.setComponentName(serviceComponent.getName()); + + ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity = serviceComponentDesiredStateDAO.findByPK(dpk); + serviceComponentDesiredStateEntity.getHostComponentDesiredStateEntities().add(desiredStateEntity); + + desiredStateEntity.setServiceComponentDesiredStateEntity(serviceComponentDesiredStateEntity); + desiredStateEntity.setHostEntity(hostEntity); + stateEntity.setServiceComponentDesiredStateEntity(serviceComponentDesiredStateEntity); + stateEntity.setHostEntity(hostEntity); + + hostComponentStateDAO.create(stateEntity); + hostComponentDesiredStateDAO.create(desiredStateEntity); + + serviceComponentDesiredStateDAO.merge(serviceComponentDesiredStateEntity); + hostDAO.merge(hostEntity); + stateEntity = hostComponentStateDAO.merge(stateEntity); + desiredStateEntity = hostComponentDesiredStateDAO.merge(desiredStateEntity); + + host.refresh(); + serviceComponent.refresh(); + persisted = true; + } else { + saveIfPersisted(); + } + } finally { + writeLock.unlock(); + } + } + + @Override + public synchronized void refresh() { + if (isPersisted()) { + HostComponentStateEntityPK pk = new HostComponentStateEntityPK(); + HostComponentDesiredStateEntityPK dpk = new HostComponentDesiredStateEntityPK(); + pk.setClusterId(getClusterId()); + pk.setComponentName(getServiceComponentName()); + pk.setServiceName(getServiceName()); + pk.setHostName(getHostName()); + dpk.setClusterId(getClusterId()); + dpk.setComponentName(getServiceComponentName()); + dpk.setServiceName(getServiceName()); + dpk.setHostName(getHostName()); + stateEntity = hostComponentStateDAO.findByPK(pk); + desiredStateEntity = hostComponentDesiredStateDAO.findByPK(dpk); + hostComponentStateDAO.refresh(stateEntity); + hostComponentDesiredStateDAO.refresh(desiredStateEntity); + } + } + + private void saveIfPersisted() { + if (isPersisted()) { + hostComponentStateDAO.merge(stateEntity); + hostComponentDesiredStateDAO.merge(desiredStateEntity); + } + } + } Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java?rev=1398669&view=auto ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java (added) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/utils/ShellCommandUtil.java Tue Oct 16 06:38:23 2012 @@ -0,0 +1,72 @@ +/** + * 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.utils; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Logs OpenSsl command exit code with description + */ +public class ShellCommandUtil { + private static final Log LOG = LogFactory.getLog(ShellCommandUtil.class); + /* + public static String LogAndReturnOpenSslExitCode(String command, int exitCode) { + LogOpenSslExitCode(command, exitCode); + return getOpenSslCommandResult(command, exitCode); + } + */ + public static void LogOpenSslExitCode(String command, int exitCode) { + if (exitCode == 0) { + LOG.info(getOpenSslCommandResult(command, exitCode)); + } else { + LOG.warn(getOpenSslCommandResult(command, exitCode)); + } + + } + + public static String getOpenSslCommandResult(String command, int exitCode) { + return new StringBuilder().append("Command ").append(command).append(" was finished with exit code: ") + .append(exitCode).append(" - ").append(getOpenSslExitCodeDescription(exitCode)).toString(); + } + + private static String getOpenSslExitCodeDescription(int exitCode) { + switch (exitCode) { + case 0: { + return "the operation was completely successfully."; + } + case 1: { + return "an error occurred parsing the command options."; + } + case 2: { + return "one of the input files could not be read."; + } + case 3: { + return "an error occurred creating the PKCS#7 file or when reading the MIME message."; + } + case 4: { + return "an error occurred decrypting or verifying the message."; + } + case 5: { + return "the message was verified correctly but an error occurred writing out the signers certificates."; + } + default: + return "unsupported code"; + } + } +} Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/resources/META-INF/persistence.xml Tue Oct 16 06:38:23 2012 @@ -22,17 +22,16 @@ org.apache.ambari.server.orm.entities.ClusterEntity org.apache.ambari.server.orm.entities.ClusterServiceEntity org.apache.ambari.server.orm.entities.ClusterStateEntity - org.apache.ambari.server.orm.entities.ComponentHostDesiredStateEntity + org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity org.apache.ambari.server.orm.entities.HostComponentStateEntity org.apache.ambari.server.orm.entities.HostComponentMappingEntity org.apache.ambari.server.orm.entities.HostEntity org.apache.ambari.server.orm.entities.HostStateEntity org.apache.ambari.server.orm.entities.ServiceComponentConfigEntity + org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity org.apache.ambari.server.orm.entities.ServiceComponentHostConfigEntity - org.apache.ambari.server.orm.entities.ServiceComponentStateEntity org.apache.ambari.server.orm.entities.ServiceConfigEntity org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity - org.apache.ambari.server.orm.entities.ServiceStateEntity org.apache.ambari.server.orm.entities.RoleEntity org.apache.ambari.server.orm.entities.UserEntity @@ -49,17 +48,16 @@ org.apache.ambari.server.orm.entities.ClusterEntity org.apache.ambari.server.orm.entities.ClusterServiceEntity org.apache.ambari.server.orm.entities.ClusterStateEntity - org.apache.ambari.server.orm.entities.ComponentHostDesiredStateEntity + org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity org.apache.ambari.server.orm.entities.HostComponentStateEntity org.apache.ambari.server.orm.entities.HostComponentMappingEntity org.apache.ambari.server.orm.entities.HostEntity org.apache.ambari.server.orm.entities.HostStateEntity org.apache.ambari.server.orm.entities.ServiceComponentConfigEntity + org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity org.apache.ambari.server.orm.entities.ServiceComponentHostConfigEntity - org.apache.ambari.server.orm.entities.ServiceComponentStateEntity org.apache.ambari.server.orm.entities.ServiceConfigEntity org.apache.ambari.server.orm.entities.ServiceDesiredStateEntity - org.apache.ambari.server.orm.entities.ServiceStateEntity org.apache.ambari.server.orm.entities.RoleEntity org.apache.ambari.server.orm.entities.UserEntity Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionManager.java Tue Oct 16 06:38:23 2012 @@ -22,25 +22,48 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; +import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.Role; import org.apache.ambari.server.RoleCommand; import org.apache.ambari.server.agent.ActionQueue; import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.cluster.ClustersImpl; import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent; import org.apache.ambari.server.utils.StageUtils; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class TestActionManager { private long requestId = 23; private long stageId = 31; - + private Injector injector; + private Clusters clusters; + + @Before + public void setup() throws AmbariException { + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); + } + + @After + public void teardown() throws AmbariException { + injector.getInstance(PersistService.class).stop(); + } + @Test public void testActionResponse() { ActionDBAccessor db = new ActionDBInMemoryImpl(); ActionManager am = new ActionManager(5000, 1200000, new ActionQueue(), - new ClustersImpl(), db); + clusters, db); String hostname = "host1"; populateActionDB(db, hostname); List reports = new ArrayList(); Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java Tue Oct 16 06:38:23 2012 @@ -20,20 +20,42 @@ package org.apache.ambari.server.agent; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.actionmanager.ActionDBInMemoryImpl; import org.apache.ambari.server.actionmanager.ActionManager; import org.apache.ambari.server.agent.HostStatus.Status; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Host; import org.apache.ambari.server.state.HostState; import org.apache.ambari.server.state.cluster.ClustersImpl; import org.apache.ambari.server.state.fsm.InvalidStateTransitonException; import org.apache.ambari.server.state.host.HostImpl; +import org.junit.After; +import org.junit.Before; import org.junit.Test; public class TestHeartbeatHandler { + private Injector injector; + private Clusters clusters; + + @Before + public void setup() throws AmbariException{ + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); + } + + @After + public void teardown() throws AmbariException { + injector.getInstance(PersistService.class).stop(); + } + @Test public void testHeartbeat() throws AmbariException { ActionManager am = new ActionManager(0, 0, null, null, @@ -49,7 +71,8 @@ public class TestHeartbeatHandler { HeartBeat hb = new HeartBeat(); hb.setNodeStatus(new HostStatus(Status.HEALTHY, "I am ok")); hb.setHostname(hostname); - Host hostObject = new HostImpl(hostname); + clusters.addHost(hostname); + Host hostObject = clusters.getHost(hostname); hostObject.setState(HostState.UNHEALTHY); when(fsm.getHost(hostname)).thenReturn(hostObject); handler.handleHeartBeat(hb); @@ -65,7 +88,8 @@ public class TestHeartbeatHandler { Clusters fsm = mock(Clusters.class); String hostname = "host1"; HeartBeatHandler handler = new HeartBeatHandler(fsm, new ActionQueue(), am); - Host hostObject = new HostImpl(hostname); + clusters.addHost(hostname); + Host hostObject = clusters.getHost(hostname); when(fsm.getHost(hostname)).thenReturn(hostObject); Register reg = new Register(); @@ -82,7 +106,7 @@ public class TestHeartbeatHandler { public void testRegisterNewNode() throws AmbariException, InvalidStateTransitonException { ActionManager am = new ActionManager(0, 0, null, null, new ActionDBInMemoryImpl()); - Clusters fsm = new ClustersImpl(); + Clusters fsm = clusters; String hostname = "host1"; fsm.addHost(hostname); HeartBeatHandler handler = new HeartBeatHandler(fsm, new ActionQueue(), am); Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java Tue Oct 16 06:38:23 2012 @@ -20,12 +20,11 @@ package org.apache.ambari.server.control import static org.junit.Assert.fail; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; @@ -34,14 +33,17 @@ import org.apache.ambari.server.actionma import org.apache.ambari.server.actionmanager.ActionManager; import org.apache.ambari.server.actionmanager.Stage; import org.apache.ambari.server.agent.ActionQueue; +import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.ServiceComponent; import org.apache.ambari.server.state.ServiceComponentHost; import org.apache.ambari.server.state.State; -import org.apache.ambari.server.state.cluster.ClustersImpl; import org.apache.ambari.server.utils.StageUtils; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,21 +56,22 @@ public class AmbariManagementControllerT private AmbariManagementController controller; private Clusters clusters; private ActionDBAccessor db; + private Injector injector; @Before public void setup() { - clusters = new ClustersImpl(); - db = new ActionDBInMemoryImpl(); - ActionManager am = new ActionManager(5000, 1200000, new ActionQueue(), - clusters, db); - controller = new AmbariManagementControllerImpl(am, clusters); + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); +// ActionManager am = new ActionManager(5000, 1200000, new ActionQueue(), +// clusters, db); + db = injector.getInstance(ActionDBAccessor.class); + controller = injector.getInstance(AmbariManagementController.class); } @After public void teardown() { - controller = null; - clusters = null; - db = null; + injector.getInstance(PersistService.class).stop(); } private void createCluster(String clusterName) throws AmbariException { @@ -247,8 +250,11 @@ public class AmbariManagementControllerT State.INIT); String host1 = "h1"; + clusters.addHost(host1); + clusters.getHost("h1").persist(); String host2 = "h2"; - + clusters.addHost(host2); + clusters.getHost("h2").persist(); try { createServiceComponentHost(clusterName, serviceName, componentName1, host1, State.INSTALLING); Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java?rev=1398669&view=auto ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java (added) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java Tue Oct 16 06:38:23 2012 @@ -0,0 +1,16 @@ +package org.apache.ambari.server.orm; + +import com.google.inject.AbstractModule; +import org.apache.ambari.server.configuration.Configuration; +import org.apache.ambari.server.controller.ControllerModule; + +import java.util.Properties; + +public class InMemoryDefaultTestModule extends AbstractModule { + @Override + protected void configure() { + Properties properties = new Properties(); + properties.setProperty(Configuration.PERSISTENCE_IN_MEMORY_KEY, "true"); + install(new ControllerModule(properties)); + } +} Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java Tue Oct 16 06:38:23 2012 @@ -27,6 +27,7 @@ import org.apache.ambari.server.orm.dao. import org.apache.ambari.server.orm.dao.RoleDAO; import org.apache.ambari.server.orm.dao.UserDAO; import org.apache.ambari.server.orm.entities.*; +import org.apache.ambari.server.state.HostState; import org.springframework.security.crypto.password.PasswordEncoder; import javax.persistence.EntityManager; @@ -66,9 +67,9 @@ public class OrmTestHelper { host1.setHostName("test_host1"); host2.setHostName("test_host2"); host3.setHostName("test_host3"); - host1.setIp("192.168.0.1"); - host2.setIp("192.168.0.2"); - host3.setIp("192.168.0.3"); + host1.setIpv4("192.168.0.1"); + host2.setIpv4("192.168.0.2"); + host3.setIpv4("192.168.0.3"); List hostEntities = new ArrayList(); hostEntities.add(host1); @@ -77,14 +78,14 @@ public class OrmTestHelper { clusterEntity.setHostEntities(hostEntities); //both sides of relation should be set when modifying in runtime - host1.setClusterEntity(clusterEntity); - host2.setClusterEntity(clusterEntity); + host1.setClusterEntities(Arrays.asList(clusterEntity)); + host2.setClusterEntities(Arrays.asList(clusterEntity)); HostStateEntity hostStateEntity1 = new HostStateEntity(); - hostStateEntity1.setCurrentState("TEST_STATE1"); + hostStateEntity1.setCurrentState(HostState.HEARTBEAT_LOST); hostStateEntity1.setHostEntity(host1); HostStateEntity hostStateEntity2 = new HostStateEntity(); - hostStateEntity2.setCurrentState("TEST_STATE2"); + hostStateEntity2.setCurrentState(HostState.HEALTHY); hostStateEntity2.setHostEntity(host2); host1.setHostStateEntity(hostStateEntity1); host2.setHostStateEntity(hostStateEntity2); @@ -145,7 +146,7 @@ public class OrmTestHelper { public int getClusterSizeByHostName(String hostName) { Query query = getEntityManager().createQuery( - "SELECT host2 from HostEntity host join host.clusterEntity cluster join cluster.hostEntities host2 where host.hostName=:hostName"); + "SELECT host2 from HostEntity host join host.clusterEntities clusters join clusters.hostEntities host2 where host.hostName=:hostName"); query.setParameter("hostName", hostName); Collection hosts = query.getResultList(); Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/orm/TestOrmImpl.java Tue Oct 16 06:38:23 2012 @@ -35,6 +35,7 @@ import org.junit.Test; import javax.persistence.EntityManager; import javax.persistence.RollbackException; +import java.util.Collection; import java.util.Date; import java.util.List; @@ -90,7 +91,7 @@ public class TestOrmImpl extends Assert entityManager.persist(clusterEntity); entityManager.getTransaction().rollback(); - assertNull("transaction was not rolled back", entityManager.find(ClusterEntity.class, testClusterName)); + assertNull("transaction was not rolled back", injector.getInstance(ClusterDAO.class).findByName(testClusterName)); } /** @@ -132,8 +133,7 @@ public class TestOrmImpl extends Assert clusterServiceDAO.remove(clusterServiceEntity); - assertNull(clusterServiceDAO - .findByClusterAndServiceNames(clusterName, serviceName)); + assertNull(clusterServiceDAO.findByClusterAndServiceNames(clusterName, serviceName)); } @@ -151,15 +151,14 @@ public class TestOrmImpl extends Assert clusterServiceDAO.create(clusterServiceEntity); clusterDAO.merge(cluster); - clusterServiceEntity = clusterServiceDAO - .findByClusterAndServiceNames(clusterName, serviceName); + clusterServiceEntity = clusterServiceDAO.findByClusterAndServiceNames(clusterName, serviceName); assertNotNull(clusterServiceEntity); ServiceConfigEntity serviceConfigEntity = new ServiceConfigEntity(); serviceConfigEntity.setConfigSnapshotTime(currentTime); serviceConfigEntity.setClusterServiceEntity(clusterServiceEntity); - List list = clusterServiceEntity.getServiceConfigEntities(); + Collection list = clusterServiceEntity.getServiceConfigEntities(); list.add(serviceConfigEntity); clusterServiceEntity.setServiceConfigEntities(list); Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/security/SslExecutionTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/security/SslExecutionTest.java?rev=1398669&view=auto ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/security/SslExecutionTest.java (added) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/security/SslExecutionTest.java Tue Oct 16 06:38:23 2012 @@ -0,0 +1,113 @@ +/** + * 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.security; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.Injector; +import org.apache.ambari.server.configuration.Configuration; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.*; +import org.junit.rules.TemporaryFolder; +import static org.junit.Assert.assertTrue; + +import java.io.*; +import java.lang.reflect.Constructor; +import java.util.Properties; + +public class SslExecutionTest { + + private static Log LOG = LogFactory.getLog(SslExecutionTest.class); + public TemporaryFolder temp = new TemporaryFolder(); + + Injector injector; + + private static CertificateManager certMan; + + @Inject + static void init(CertificateManager instance) { + certMan = instance; + } + + + private class SecurityModule extends AbstractModule { + @Override + protected void configure() { + bind(Properties.class).toInstance(buildTestProperties()); + bind(Configuration.class).toConstructor(getConfigurationConstructor()); + requestStaticInjection(SslExecutionTest.class); + } + } + + protected Properties buildTestProperties() { + try { + temp.create(); + } catch (IOException e) { + e.printStackTrace(); + } + Properties properties = new Properties(); + properties.setProperty(Configuration.SRVR_KSTR_DIR_KEY, temp.getRoot().getAbsolutePath()); + + return properties; + } + + protected Constructor getConfigurationConstructor() { + try { + return Configuration.class.getConstructor(Properties.class); + } catch (NoSuchMethodException e) { + throw new RuntimeException("Expected constructor not found in Configuration.java", e); + } + } + + @Before + public void setUp() throws IOException { + + injector = Guice.createInjector(new SecurityModule()); + certMan = injector.getInstance(CertificateManager.class); + + certMan.initRootCert(); + + } + + @After + public void tearDown() throws IOException { + temp.delete(); + } + + @Test + public void testSslLogging() throws Exception { + LOG.info("Testing sign"); + + certMan.configs.getConfigsMap().put(Configuration.PASSPHRASE_KEY, "123123"); + + LOG.info("key dir = " + certMan.configs.getConfigsMap().get(Configuration.SRVR_KSTR_DIR_KEY)); + + SignCertResponse signAgentCrt = certMan.signAgentCrt("somehost", "gdfgdfg", "123123"); + LOG.info("-------------RESPONCE-------------"); + LOG.info("-------------MESSAGE--------------"); + LOG.info(signAgentCrt.getMessage()); + LOG.info("---------------------------------"); + LOG.info("-------------RESULT--------------"); + LOG.info(signAgentCrt.getResult()); + LOG.info("---------------------------------"); + assertTrue(SignCertResponse.ERROR_STATUS.equals(signAgentCrt.getResult())); + } + +} Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java Tue Oct 16 06:38:23 2012 @@ -18,9 +18,14 @@ package org.apache.ambari.server.state; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; 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.cluster.ClustersImpl; import org.junit.After; import org.junit.Before; @@ -31,29 +36,33 @@ public class ServiceTest { private Clusters clusters; private Cluster cluster; private String clusterName; + private Injector injector; + private ServiceFactory serviceFactory; @Before public void setup() throws AmbariException { - clusters = new ClustersImpl(); + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); + serviceFactory = injector.getInstance(ServiceFactory.class); clusterName = "foo"; clusters.addCluster(clusterName); cluster = clusters.getCluster(clusterName); Assert.assertNotNull(cluster); } - + @After public void teardown() throws AmbariException { - clusters = null; - cluster = null; + injector.getInstance(PersistService.class).stop(); } - + @Test public void testCreateService() throws AmbariException { String serviceName = "s1"; - Service s = new ServiceImpl(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName); cluster.addService(s); Service service = cluster.getService(serviceName); - + Assert.assertNotNull(service); Assert.assertEquals(serviceName, service.getName()); Assert.assertEquals(cluster.getClusterId(), @@ -68,7 +77,7 @@ public class ServiceTest { @Test public void testGetAndSetServiceInfo() throws AmbariException { String serviceName = "s1"; - Service s = new ServiceImpl(cluster, serviceName); + Service s = serviceFactory.createNew(cluster, serviceName); cluster.addService(s); Service service = cluster.getService(serviceName); Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java Tue Oct 16 06:38:23 2012 @@ -24,11 +24,16 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.agent.DiskInfo; import org.apache.ambari.server.agent.HostInfo; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.controller.ClusterResponse; import org.apache.ambari.server.state.AgentVersion; import org.apache.ambari.server.state.Cluster; @@ -53,10 +58,13 @@ public class ClusterTest { String h1 = "h1"; String s1 = "s1"; String sc1 = "sc1"; + private Injector injector; @Before public void setup() throws AmbariException { - clusters = new ClustersImpl(); + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); clusters.addCluster("c1"); c1 = clusters.getCluster("c1"); Assert.assertEquals("c1", c1.getClusterName()); @@ -66,8 +74,7 @@ public class ClusterTest { @After public void teardown() { - clusters = null; - c1 = null; + injector.getInstance(PersistService.class).stop(); } @Test @@ -151,12 +158,12 @@ public class ClusterTest { Assert.assertNotNull(c2); - Assert.assertEquals(clusterName.hashCode(), c2.getClusterId()); +// Assert.assertEquals(clusterName.hashCode(), c2.getClusterId()); This is not true Assert.assertEquals(clusterName, c2.getClusterName()); c2.setClusterName("foo2"); Assert.assertEquals("foo2", c2.getClusterName()); - Assert.assertEquals(clusterName.hashCode(), c2.getClusterId()); +// Assert.assertEquals(clusterName.hashCode(), c2.getClusterId()); Assert.assertNotNull(c2.getDesiredStackVersion()); Assert.assertEquals("", c2.getDesiredStackVersion().getStackVersion()); Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersTest.java Tue Oct 16 06:38:23 2012 @@ -24,14 +24,18 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; import junit.framework.Assert; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.ClusterNotFoundException; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.Host; -import org.apache.ambari.server.state.cluster.ClustersImpl; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,15 +43,18 @@ import org.junit.Test; public class ClustersTest { private Clusters clusters; + private Injector injector; @Before public void setup() { - clusters = new ClustersImpl(); + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); } @After public void teardown() { - clusters = null; + injector.getInstance(PersistService.class).stop(); } @Test Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostImplTest.java URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostImplTest.java?rev=1398669&r1=1398668&r2=1398669&view=diff ============================================================================== --- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostImplTest.java (original) +++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostImplTest.java Tue Oct 16 06:38:23 2012 @@ -23,11 +23,15 @@ import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.List; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.persist.PersistService; +import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.agent.DiskInfo; import org.apache.ambari.server.agent.HostInfo; -import org.apache.ambari.server.state.AgentVersion; -import org.apache.ambari.server.state.HostHealthStatus; -import org.apache.ambari.server.state.HostState; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.state.*; import org.apache.ambari.server.state.HostHealthStatus.HealthStatus; import org.apache.ambari.server.state.host.HostHealthyHeartbeatEvent; import org.apache.ambari.server.state.host.HostHeartbeatLostEvent; @@ -35,13 +39,30 @@ import org.apache.ambari.server.state.ho import org.apache.ambari.server.state.host.HostRegistrationRequestEvent; import org.apache.ambari.server.state.host.HostStatusUpdatesReceivedEvent; import org.apache.ambari.server.state.host.HostUnhealthyHeartbeatEvent; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; public class HostImplTest { + private Injector injector; + private Clusters clusters; + + @Before + public void setup() throws AmbariException{ + injector = Guice.createInjector(new InMemoryDefaultTestModule()); + injector.getInstance(GuiceJpaInitializer.class); + clusters = injector.getInstance(Clusters.class); + } + + @After + public void teardown() throws AmbariException { + injector.getInstance(PersistService.class).stop(); + } + @Test - public void testHostInfoImport() { + public void testHostInfoImport() throws AmbariException{ HostInfo info = new HostInfo(); info.setMemorySize(100); info.setProcessorCount(10); @@ -56,7 +77,9 @@ public class HostImplTest { info.setOS("os_type"); info.setMemoryTotal(10); - HostImpl host = new HostImpl("foo"); + clusters.addHost("foo"); + Host host = clusters.getHost("foo"); + host.importHostInfo(info); Assert.assertEquals(info.getHostName(), host.getHostName()); @@ -68,7 +91,7 @@ public class HostImplTest { Assert.assertEquals(info.getOS(), host.getOsType()); } - private void registerHost(HostImpl host) throws Exception { + private void registerHost(Host host) throws Exception { HostInfo info = new HostInfo(); info.setMemorySize(100); info.setProcessorCount(10); @@ -93,24 +116,24 @@ public class HostImplTest { Assert.assertEquals(currentTime, host.getLastRegistrationTime()); } - private void ensureHostUpdatesReceived(HostImpl host) throws Exception { + private void ensureHostUpdatesReceived(Host host) throws Exception { HostStatusUpdatesReceivedEvent e = new HostStatusUpdatesReceivedEvent(host.getHostName(), 1); host.handleEvent(e); } - private void verifyHostState(HostImpl host, HostState state) { + private void verifyHostState(Host host, HostState state) { Assert.assertEquals(state, host.getState()); } - private void sendHealthyHeartbeat(HostImpl host, long counter) + private void sendHealthyHeartbeat(Host host, long counter) throws Exception { HostHealthyHeartbeatEvent e = new HostHealthyHeartbeatEvent( host.getHostName(), counter); host.handleEvent(e); } - private void sendUnhealthyHeartbeat(HostImpl host, long counter) + private void sendUnhealthyHeartbeat(Host host, long counter) throws Exception { HostHealthStatus healthStatus = new HostHealthStatus(HealthStatus.UNHEALTHY, "Unhealthy server"); @@ -119,21 +142,23 @@ public class HostImplTest { host.handleEvent(e); } - private void timeoutHost(HostImpl host) throws Exception { + private void timeoutHost(Host host) throws Exception { HostHeartbeatLostEvent e = new HostHeartbeatLostEvent( host.getHostName()); host.handleEvent(e); } @Test - public void testHostFSMInit() { - HostImpl host = new HostImpl("foo"); + public void testHostFSMInit() throws AmbariException{ + clusters.addHost("foo"); + Host host = clusters.getHost("foo"); verifyHostState(host, HostState.INIT); } @Test public void testHostRegistrationFlow() throws Exception { - HostImpl host = new HostImpl("foo"); + clusters.addHost("foo"); + Host host = clusters.getHost("foo"); registerHost(host); verifyHostState(host, HostState.WAITING_FOR_HOST_STATUS_UPDATES); @@ -165,7 +190,8 @@ public class HostImplTest { @Test public void testHostHeartbeatFlow() throws Exception { - HostImpl host = new HostImpl("foo"); + clusters.addHost("foo"); + Host host = clusters.getHost("foo"); registerHost(host); ensureHostUpdatesReceived(host); @@ -232,7 +258,8 @@ public class HostImplTest { @Test public void testHostRegistrationsInAnyState() throws Exception { - HostImpl host = new HostImpl("foo"); + clusters.addHost("foo"); + Host host = clusters.getHost("foo");; long counter = 0; registerHost(host);