incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hit...@apache.org
Subject svn commit: r1384984 [2/2] - in /incubator/ambari/branches/AMBARI-666: ./ ambari-server/src/main/java/org/apache/ambari/server/state/live/ ambari-server/src/main/java/org/apache/ambari/server/state/live/host/ ambari-server/src/main/java/org/apache/amba...
Date Sat, 15 Sep 2012 00:28:44 GMT
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostImpl.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostImpl.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostImpl.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,734 @@
+/**
+ * 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.live.host;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import org.apache.ambari.server.agent.DiskInfo;
+import org.apache.ambari.server.agent.HostInfo;
+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.live.AgentVersion;
+import org.apache.ambari.server.state.live.job.Job;
+import org.apache.ambari.server.state.live.host.HostHealthStatus.HealthStatus;
+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 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<DiskInfo> disksInfo;
+
+  /**
+   * 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<String, String> hostAttributes;
+
+  /**
+   * Version of agent running on the Host
+   */
+  private AgentVersion agentVersion;
+
+  /**
+   * Host Health Status
+   */
+  private HostHealthStatus healthStatus;
+
+  private static final StateMachineFactory
+    <HostImpl, HostState, HostEventType, HostEvent>
+      stateMachineFactory
+        = new StateMachineFactory<HostImpl, HostState, HostEventType, HostEvent>
+        (HostState.INIT)
+
+   // define the state machine of a Host
+
+   // Transition from INIT state
+   // when the initial registration request is received
+   .addTransition(HostState.INIT, HostState.WAITING_FOR_VERIFICATION,
+       HostEventType.HOST_REGISTRATION_REQUEST, new HostRegistrationReceived())
+
+   // Transition from WAITING_FOR_VERIFICATION state
+   // when the host is authenticated
+   .addTransition(HostState.WAITING_FOR_VERIFICATION, HostState.VERIFIED,
+       HostEventType.HOST_VERIFIED, new HostVerifiedTransition())
+
+   // Transitions from VERIFIED state
+   // when a normal heartbeat is received
+   .addTransition(HostState.VERIFIED, HostState.HEALTHY,
+       HostEventType.HOST_HEARTBEAT_HEALTHY,
+       new HostBecameHealthyTransition())
+   // when a heartbeat is not received within the configured timeout period
+   .addTransition(HostState.VERIFIED, HostState.HEARTBEAT_LOST,
+       HostEventType.HOST_HEARTBEAT_TIMED_OUT,
+       new HostHeartbeatTimedOutTransition())
+   // when a heartbeart denoting host as unhealthy is received
+   .addTransition(HostState.VERIFIED, HostState.UNHEALTHY,
+       HostEventType.HOST_HEARTBEAT_UNHEALTHY,
+       new HostBecameUnhealthyTransition())
+
+   // Transitions from HEALTHY state
+   // when a normal heartbeat is received
+   .addTransition(HostState.HEALTHY, HostState.HEALTHY,
+       HostEventType.HOST_HEARTBEAT_HEALTHY,
+       new HostHeartbeatReceivedTransition())
+   // when a heartbeat is not received within the configured timeout period
+   .addTransition(HostState.HEALTHY, HostState.HEARTBEAT_LOST,
+       HostEventType.HOST_HEARTBEAT_TIMED_OUT,
+       new HostHeartbeatTimedOutTransition())
+   // when a heartbeart denoting host as unhealthy is received
+   .addTransition(HostState.HEALTHY, HostState.UNHEALTHY,
+       HostEventType.HOST_HEARTBEAT_UNHEALTHY,
+       new HostBecameUnhealthyTransition())
+
+   // Transitions from UNHEALTHY state
+   // when a normal heartbeat is received
+   .addTransition(HostState.UNHEALTHY, HostState.HEALTHY,
+       HostEventType.HOST_HEARTBEAT_HEALTHY,
+       new HostBecameHealthyTransition())
+   // when a heartbeart denoting host as unhealthy is received
+   .addTransition(HostState.UNHEALTHY, HostState.UNHEALTHY,
+       HostEventType.HOST_HEARTBEAT_UNHEALTHY,
+       new HostHeartbeatReceivedTransition())
+   // when a heartbeat is not received within the configured timeout period
+   .addTransition(HostState.UNHEALTHY, HostState.HEARTBEAT_LOST,
+       HostEventType.HOST_HEARTBEAT_TIMED_OUT,
+       new HostHeartbeatTimedOutTransition())
+
+   // Transitions from HEARTBEAT_LOST state
+   // when a normal heartbeat is received
+   .addTransition(HostState.HEARTBEAT_LOST, HostState.HEALTHY,
+       HostEventType.HOST_HEARTBEAT_HEALTHY,
+       new HostBecameHealthyTransition())
+   // when a heartbeart denoting host as unhealthy is received
+   .addTransition(HostState.HEARTBEAT_LOST, HostState.UNHEALTHY,
+       HostEventType.HOST_HEARTBEAT_UNHEALTHY,
+       new HostBecameUnhealthyTransition())
+   // when a heartbeat is not received within the configured timeout period
+   .addTransition(HostState.HEARTBEAT_LOST, HostState.HEARTBEAT_LOST,
+       HostEventType.HOST_HEARTBEAT_TIMED_OUT)
+   .installTopology();
+
+  private final StateMachine<HostState, HostEventType, HostEvent> stateMachine;
+
+  public HostImpl() {
+    super();
+    this.stateMachine = stateMachineFactory.make(this);
+    ReadWriteLock rwLock = new ReentrantReadWriteLock();
+    this.readLock = rwLock.readLock();
+    this.writeLock = rwLock.writeLock();
+    this.healthStatus = new HostHealthStatus(HealthStatus.UNKNOWN, "");
+  }
+
+  static class HostRegistrationReceived
+      implements SingleArcTransition<HostImpl, HostEvent> {
+
+    @Override
+    public void transition(HostImpl host, HostEvent event) {
+      HostRegistrationRequestEvent e = (HostRegistrationRequestEvent) event;
+      host.importHostInfo(e.hostInfo);
+      host.setLastRegistrationTime(e.registrationTime);
+      host.setAgentVersion(e.agentVersion);
+    }
+  }
+
+  static class HostVerifiedTransition
+      implements SingleArcTransition<HostImpl, HostEvent> {
+
+    @Override
+    public void transition(HostImpl host, HostEvent event) {
+      // TODO Auto-generated method stub
+    }
+  }
+
+  static class HostHeartbeatReceivedTransition
+    implements SingleArcTransition<HostImpl, HostEvent> {
+
+    @Override
+    public void transition(HostImpl host, HostEvent event) {
+      long heartbeatTime = 0;
+      switch (event.getType()) {
+        case HOST_HEARTBEAT_HEALTHY:
+          heartbeatTime = ((HostHealthyHeartbeatEvent)event).getHeartbeatTime();
+          break;
+        case HOST_HEARTBEAT_UNHEALTHY:
+          heartbeatTime = ((HostUnhealthyHeartbeatEvent)event).getHeartbeatTime();
+          break;
+        default:
+          break;
+      }
+      if (0 == heartbeatTime) {
+        // TODO handle error
+      }
+      host.setLastHeartbeatTime(heartbeatTime);
+    }
+  }
+
+  static class HostBecameHealthyTransition
+      implements SingleArcTransition<HostImpl, HostEvent> {
+
+    @Override
+    public void transition(HostImpl host, HostEvent event) {
+      HostHealthyHeartbeatEvent e = (HostHealthyHeartbeatEvent) event;
+      host.setLastHeartbeatTime(e.getHeartbeatTime());
+      // TODO Audit logs
+      LOG.info("Host transitioned to a healthy state"
+          + ", host=" + e.hostName
+          + ", heartbeatTime=" + e.getHeartbeatTime());
+      host.getHealthStatus().setHealthStatus(HealthStatus.HEALTHY);
+    }
+  }
+
+  static class HostBecameUnhealthyTransition
+      implements SingleArcTransition<HostImpl, HostEvent> {
+
+    @Override
+    public void transition(HostImpl host, HostEvent event) {
+      HostUnhealthyHeartbeatEvent e = (HostUnhealthyHeartbeatEvent) event;
+      host.setLastHeartbeatTime(e.getHeartbeatTime());
+      // TODO Audit logs
+      LOG.info("Host transitioned to an unhealthy state"
+          + ", host=" + e.hostName
+          + ", heartbeatTime=" + e.getHeartbeatTime()
+          + ", healthStatus=" + e.getHealthStatus());
+      host.setHealthStatus(e.getHealthStatus());
+    }
+  }
+
+  static class HostHeartbeatTimedOutTransition
+      implements SingleArcTransition<HostImpl, HostEvent> {
+
+    @Override
+    public void transition(HostImpl host, HostEvent event) {
+      HostHeartbeatTimedOutEvent e = (HostHeartbeatTimedOutEvent) event;
+      // TODO Audit logs
+      LOG.info("Host transitioned to heartbeat timed out state"
+          + ", host=" + e.hostName
+          + ", lastHeartbeatTime=" + host.getLastHeartbeatTime());
+      host.getHealthStatus().setHealthStatus(HealthStatus.UNKNOWN);
+    }
+  }
+
+  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();
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public HostState getState() {
+    try {
+      readLock.lock();
+      return stateMachine.getCurrentState();
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setState(HostState state) {
+    try {
+      writeLock.lock();
+      stateMachine.setCurrentState(state);
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public void handleEvent(HostEvent event)
+      throws InvalidStateTransitonException {
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Handling Host event, eventType=" + event.getType().name()
+          + ", event=" + event.toString());
+    }
+    HostState oldState = getState();
+    try {
+      writeLock.lock();
+      try {
+        stateMachine.doTransition(event.getType(), event);
+      } catch (InvalidStateTransitonException e) {
+        LOG.error("Can't handle Host event at current state"
+            + ", host=" + this.getHostName()
+            + ", currentState=" + oldState
+            + ", eventType=" + event.getType()
+            + ", event=" + event);
+        throw e;
+      }
+    }
+    finally {
+      writeLock.unlock();
+    }
+    if (oldState != getState()) {
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("Host transitioned to a new state"
+            + ", host=" + this.getHostName()
+            + ", oldState=" + oldState
+            + ", currentState=" + getState()
+            + ", eventType=" + event.getType().name()
+            + ", event=" + event);
+      }
+    }
+  }
+
+  @Override
+  public String getHostName() {
+    try {
+      readLock.lock();
+      return hostName;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setHostName(String hostName) {
+    try {
+      writeLock.lock();
+      this.hostName = hostName;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public String getIPv4() {
+    try {
+      readLock.lock();
+      return ipv4;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setIPv4(String ip) {
+    try {
+      writeLock.lock();
+      this.ipv4 = ip;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public String getIPv6() {
+    try {
+      readLock.lock();
+      return ipv6;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setIPv6(String ip) {
+    try {
+      writeLock.lock();
+      this.ipv6 = ip;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public int getCpuCount() {
+    try {
+      readLock.lock();
+      return cpuCount;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setCpuCount(int cpuCount) {
+    try {
+      writeLock.lock();
+      this.cpuCount = cpuCount;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public long getTotalMemBytes() {
+    try {
+      readLock.lock();
+      return totalMemBytes;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setTotalMemBytes(long totalMemBytes) {
+    try {
+      writeLock.lock();
+      this.totalMemBytes = totalMemBytes;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public long getAvailableMemBytes() {
+    try {
+      readLock.lock();
+      return availableMemBytes;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setAvailableMemBytes(long availableMemBytes) {
+    try {
+      writeLock.lock();
+      this.availableMemBytes = availableMemBytes;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public String getOsArch() {
+    try {
+      readLock.lock();
+      return osArch;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setOsArch(String osArch) {
+    try {
+      writeLock.lock();
+      this.osArch = osArch;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public String getOsInfo() {
+    try {
+      readLock.lock();
+      return osInfo;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setOsInfo(String osInfo) {
+    try {
+      writeLock.lock();
+      this.osInfo = osInfo;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public String getOsType() {
+    try {
+      readLock.lock();
+      return osType;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setOsType(String osType) {
+    try {
+      writeLock.lock();
+      this.osType = osType;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public List<DiskInfo> getDisksInfo() {
+    try {
+      readLock.lock();
+      return Collections.unmodifiableList(disksInfo);
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setDisksInfo(List<DiskInfo> disksInfo) {
+    try {
+      writeLock.lock();
+      this.disksInfo = disksInfo;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public HostHealthStatus getHealthStatus() {
+    try {
+      readLock.lock();
+      return healthStatus;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setHealthStatus(HostHealthStatus healthStatus) {
+    try {
+      writeLock.lock();
+      this.healthStatus = healthStatus;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public Map<String, String> getHostAttributes() {
+    try {
+      readLock.lock();
+      return Collections.unmodifiableMap(hostAttributes);
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setHostAttributes(Map<String, String> hostAttributes) {
+    try {
+      writeLock.lock();
+      this.hostAttributes = hostAttributes;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public String getRackInfo() {
+    try {
+      readLock.lock();
+      return rackInfo;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setRackInfo(String rackInfo) {
+    try {
+      writeLock.lock();
+      this.rackInfo = rackInfo;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public long getLastRegistrationTime() {
+    try {
+      readLock.lock();
+      return lastRegistrationTime;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setLastRegistrationTime(long lastRegistrationTime) {
+    try {
+      writeLock.lock();
+      this.lastRegistrationTime = lastRegistrationTime;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public long getLastHeartbeatTime() {
+    try {
+      readLock.lock();
+      return lastHeartbeatTime;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setLastHeartbeatTime(long lastHeartbeatTime) {
+    try {
+      writeLock.lock();
+      this.lastHeartbeatTime = lastHeartbeatTime;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public AgentVersion getAgentVersion() {
+    try {
+      readLock.lock();
+      return agentVersion;
+    }
+    finally {
+      readLock.unlock();
+    }
+  }
+
+  @Override
+  public void setAgentVersion(AgentVersion agentVersion) {
+    try {
+      writeLock.lock();
+      this.agentVersion = agentVersion;
+    }
+    finally {
+      writeLock.unlock();
+    }
+  }
+
+  @Override
+  public List<Job> getJobs() {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostRegistrationRequestEvent.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostRegistrationRequestEvent.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostRegistrationRequestEvent.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostRegistrationRequestEvent.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,40 @@
+/**
+ * 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.live.host;
+
+import org.apache.ambari.server.agent.HostInfo;
+import org.apache.ambari.server.state.live.AgentVersion;
+
+public class HostRegistrationRequestEvent extends HostEvent {
+
+  final long registrationTime;
+
+  final HostInfo hostInfo;
+
+  final AgentVersion agentVersion;
+
+  public HostRegistrationRequestEvent(String hostName,
+      AgentVersion agentVersion, long registrationTime, HostInfo hostInfo) {
+    super(hostName, HostEventType.HOST_REGISTRATION_REQUEST);
+    this.registrationTime = registrationTime;
+    this.hostInfo = hostInfo;
+    this.agentVersion = agentVersion;
+  }
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostRegistrationRequestEvent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostState.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostState.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostState.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostState.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,50 @@
+/**
+ * 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.live.host;
+
+public enum HostState {
+  /**
+   * New host state
+   */
+  INIT,
+  /**
+   * State when a registration request is received from the Host but
+   * the host has not been verified/authenticated.
+   */
+  WAITING_FOR_VERIFICATION,
+  /**
+   * State when the host has been verified/authenticated
+   */
+  VERIFIED,
+  /**
+   * State when the server is receiving heartbeats regularly from the Host
+   * and the state of the Host is healthy
+   */
+  HEALTHY,
+  /**
+   * State when the server has not received a heartbeat from the Host in the
+   * configured heartbeat expiry window.
+   */
+  HEARTBEAT_LOST,
+  /**
+   * Host is in unhealthy state as reported either by the Host itself or via
+   * any other additional means ( monitoring layer )
+   */
+  UNHEALTHY
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostState.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostUnhealthyHeartbeatEvent.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostUnhealthyHeartbeatEvent.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostUnhealthyHeartbeatEvent.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostUnhealthyHeartbeatEvent.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,48 @@
+/**
+ * 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.live.host;
+
+public class HostUnhealthyHeartbeatEvent extends HostEvent {
+
+  private final long heartbeatTime;
+
+  private final HostHealthStatus healthStatus;
+
+  public HostUnhealthyHeartbeatEvent(String hostName, long heartbeatTime,
+      HostHealthStatus healthStatus) {
+    super(hostName, HostEventType.HOST_HEARTBEAT_UNHEALTHY);
+    this.heartbeatTime = heartbeatTime;
+    this.healthStatus = healthStatus;
+  }
+
+  /**
+   * @return the heartbeatTime
+   */
+  public long getHeartbeatTime() {
+    return heartbeatTime;
+  }
+
+  /**
+   * @return the healthStatus
+   */
+  public HostHealthStatus getHealthStatus() {
+    return healthStatus;
+  }
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostUnhealthyHeartbeatEvent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostVerifiedEvent.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostVerifiedEvent.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostVerifiedEvent.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostVerifiedEvent.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,29 @@
+/**
+ * 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.live.host;
+
+public class HostVerifiedEvent extends HostEvent {
+
+  // TODO need to add any additional information required for verification
+  // tracking
+  public HostVerifiedEvent(String hostName) {
+    super(hostName, HostEventType.HOST_VERIFIED);
+  }
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/host/HostVerifiedEvent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/job/Job.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/job/Job.java?rev=1384984&r1=1384983&r2=1384984&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/job/Job.java
(original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/live/job/Job.java
Sat Sep 15 00:28:42 2012
@@ -31,7 +31,7 @@ public interface Job {
   // TODO requires some form of JobType to ensure only one running
   // job per job type
   // There may be gotchas such as de-commissioning should be allowed to happen
-  // on more than one node at a time
+  // on more than one host at a time
 
 
   /**

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/TestServiceComponentHostImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/TestServiceComponentHostImpl.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/TestServiceComponentHostImpl.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/TestServiceComponentHostImpl.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,229 @@
+/**
+ * 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.live;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestServiceComponentHostImpl {
+
+  private ServiceComponentHostImpl createNewServiceComponentHost(String svcComponent,
+      String hostName, boolean isClient) {
+    ServiceComponentHostImpl impl = new ServiceComponentHostImpl(svcComponent,
+        hostName, isClient);
+    Assert.assertEquals(ServiceComponentHostLiveState.INIT,
+        impl.getState().getLiveState());
+    return impl;
+  }
+
+  @Test
+  public void testNewServiceComponentHostImpl() {
+    createNewServiceComponentHost("svcComp", "h1", false);
+    createNewServiceComponentHost("svcComp", "h1", true);
+  }
+
+  private ServiceComponentHostEvent createEvent(ServiceComponentHostImpl impl,
+      long timestamp, ServiceComponentHostEventType eventType) {
+    ServiceComponentHostEvent event = new ServiceComponentHostEvent(eventType,
+          impl.getServiceComponentName(), impl.getHostName(), timestamp);
+    return event;
+  }
+
+
+
+  private void runStateChanges(ServiceComponentHostImpl impl,
+      ServiceComponentHostEventType startEvent,
+      ServiceComponentHostLiveState startState,
+      ServiceComponentHostLiveState inProgressState,
+      ServiceComponentHostLiveState failedState,
+      ServiceComponentHostLiveState completedState)
+    throws Exception {
+    long timestamp = 0;
+
+    Assert.assertEquals(startState,
+        impl.getState().getLiveState());
+    ServiceComponentHostEvent installEvent = createEvent(impl, ++timestamp,
+        startEvent);
+
+    long startTime = timestamp;
+    impl.handleEvent(installEvent);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(-1, impl.getLastOpEndTime());
+    Assert.assertEquals(inProgressState,
+        impl.getState().getLiveState());
+
+    ServiceComponentHostEvent installEvent2 = createEvent(impl, ++timestamp,
+        startEvent);
+    boolean exceptionThrown = false;
+    try {
+      impl.handleEvent(installEvent2);
+    } catch (Exception e) {
+      exceptionThrown = true;
+    }
+    Assert.assertTrue("Exception not thrown on invalid event", exceptionThrown);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(-1, impl.getLastOpEndTime());
+    Assert.assertEquals(inProgressState,
+        impl.getState().getLiveState());
+
+    ServiceComponentHostOpInProgressEvent inProgressEvent1 = new
+        ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+            impl.getHostName(), ++timestamp);
+    impl.handleEvent(inProgressEvent1);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(-1, impl.getLastOpEndTime());
+    Assert.assertEquals(inProgressState,
+        impl.getState().getLiveState());
+
+    ServiceComponentHostOpInProgressEvent inProgressEvent2 = new
+        ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+            impl.getHostName(), ++timestamp);
+    impl.handleEvent(inProgressEvent2);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(-1, impl.getLastOpEndTime());
+    Assert.assertEquals(inProgressState,
+        impl.getState().getLiveState());
+
+
+    ServiceComponentHostOpFailedEvent failEvent = new
+        ServiceComponentHostOpFailedEvent(impl.getServiceComponentName(),
+            impl.getHostName(), ++timestamp);
+    long endTime = timestamp;
+    impl.handleEvent(failEvent);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(endTime, impl.getLastOpEndTime());
+    Assert.assertEquals(failedState,
+        impl.getState().getLiveState());
+
+    ServiceComponentHostOpRestartedEvent restartEvent = new
+        ServiceComponentHostOpRestartedEvent(impl.getServiceComponentName(),
+            impl.getHostName(), ++timestamp);
+    startTime = timestamp;
+    impl.handleEvent(restartEvent);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(-1, impl.getLastOpEndTime());
+    Assert.assertEquals(inProgressState,
+        impl.getState().getLiveState());
+
+    ServiceComponentHostOpInProgressEvent inProgressEvent3 = new
+        ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+            impl.getHostName(), ++timestamp);
+    impl.handleEvent(inProgressEvent3);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(-1, impl.getLastOpEndTime());
+    Assert.assertEquals(inProgressState,
+        impl.getState().getLiveState());
+
+    ServiceComponentHostOpSucceededEvent succeededEvent = new
+        ServiceComponentHostOpSucceededEvent(impl.getServiceComponentName(),
+            impl.getHostName(), ++timestamp);
+    endTime = timestamp;
+    impl.handleEvent(succeededEvent);
+    Assert.assertEquals(startTime, impl.getLastOpStartTime());
+    Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+    Assert.assertEquals(endTime, impl.getLastOpEndTime());
+    Assert.assertEquals(completedState,
+        impl.getState().getLiveState());
+
+  }
+
+  @Test
+  public void testClientStateFlow() throws Exception {
+    ServiceComponentHostImpl impl = createNewServiceComponentHost("svcComp",
+        "h1", true);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_INSTALL,
+        ServiceComponentHostLiveState.INIT,
+        ServiceComponentHostLiveState.INSTALLING,
+        ServiceComponentHostLiveState.INSTALL_FAILED,
+        ServiceComponentHostLiveState.INSTALLED);
+
+    boolean exceptionThrown = false;
+    try {
+      runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_START,
+        ServiceComponentHostLiveState.INSTALLED,
+        ServiceComponentHostLiveState.STARTING,
+        ServiceComponentHostLiveState.START_FAILED,
+        ServiceComponentHostLiveState.STARTED);
+    }
+    catch (Exception e) {
+      exceptionThrown = true;
+    }
+    Assert.assertTrue("Exception not thrown on invalid event", exceptionThrown);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_UNINSTALL,
+        ServiceComponentHostLiveState.INSTALLED,
+        ServiceComponentHostLiveState.UNINSTALLING,
+        ServiceComponentHostLiveState.UNINSTALL_FAILED,
+        ServiceComponentHostLiveState.UNINSTALLED);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_WIPEOUT,
+        ServiceComponentHostLiveState.UNINSTALLED,
+        ServiceComponentHostLiveState.WIPING_OUT,
+        ServiceComponentHostLiveState.WIPEOUT_FAILED,
+        ServiceComponentHostLiveState.INIT);
+
+  }
+
+  @Test
+  public void testDaemonStateFlow() throws Exception {
+    ServiceComponentHostImpl impl = createNewServiceComponentHost("svcComp",
+        "h1", false);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_INSTALL,
+        ServiceComponentHostLiveState.INIT,
+        ServiceComponentHostLiveState.INSTALLING,
+        ServiceComponentHostLiveState.INSTALL_FAILED,
+        ServiceComponentHostLiveState.INSTALLED);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_START,
+      ServiceComponentHostLiveState.INSTALLED,
+      ServiceComponentHostLiveState.STARTING,
+      ServiceComponentHostLiveState.START_FAILED,
+      ServiceComponentHostLiveState.STARTED);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_STOP,
+      ServiceComponentHostLiveState.STARTED,
+      ServiceComponentHostLiveState.STOPPING,
+      ServiceComponentHostLiveState.STOP_FAILED,
+      ServiceComponentHostLiveState.INSTALLED);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_UNINSTALL,
+        ServiceComponentHostLiveState.INSTALLED,
+        ServiceComponentHostLiveState.UNINSTALLING,
+        ServiceComponentHostLiveState.UNINSTALL_FAILED,
+        ServiceComponentHostLiveState.UNINSTALLED);
+
+    runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_WIPEOUT,
+        ServiceComponentHostLiveState.UNINSTALLED,
+        ServiceComponentHostLiveState.WIPING_OUT,
+        ServiceComponentHostLiveState.WIPEOUT_FAILED,
+        ServiceComponentHostLiveState.INIT);
+
+  }
+
+
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/TestServiceComponentHostImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/host/TestHostImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/host/TestHostImpl.java?rev=1384984&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/host/TestHostImpl.java
(added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/host/TestHostImpl.java
Sat Sep 15 00:28:42 2012
@@ -0,0 +1,224 @@
+/**
+ * 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.live.host;
+
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.ambari.server.agent.DiskInfo;
+import org.apache.ambari.server.agent.HostInfo;
+import org.apache.ambari.server.state.live.AgentVersion;
+import org.apache.ambari.server.state.live.host.HostHealthStatus.HealthStatus;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestHostImpl {
+
+  @Test
+  public void testHostInfoImport() {
+    HostInfo info = new HostInfo();
+    info.setMemorySize(100);
+    info.setProcessorCount(10);
+    List<DiskInfo> mounts = new ArrayList<DiskInfo>();
+    mounts.add(new DiskInfo("/dev/sda", "/mnt/disk1",
+        "5000000", "4000000", "10%", "size"));
+    info.setMounts(mounts);
+
+    info.setHostName("foo");
+    info.setInterfaces("fip_4");
+    info.setArchitecture("os_arch");
+    info.setOS("os_type");
+    info.setMemoryTotal(10);
+
+    HostImpl host = new HostImpl();
+    host.importHostInfo(info);
+
+    Assert.assertEquals(info.getHostName(), host.getHostName());
+    Assert.assertEquals(info.getFreeMemory(), host.getAvailableMemBytes());
+    Assert.assertEquals(info.getMemoryTotal(), host.getTotalMemBytes());
+    Assert.assertEquals(info.getProcessorCount(), host.getCpuCount());
+    Assert.assertEquals(info.getMounts().size(), host.getDisksInfo().size());
+    Assert.assertEquals(info.getArchitecture(), host.getOsArch());
+    Assert.assertEquals(info.getOS(), host.getOsType());
+  }
+
+  private void registerHost(HostImpl host) throws Exception {
+    HostInfo info = new HostInfo();
+    info.setMemorySize(100);
+    info.setProcessorCount(10);
+    List<DiskInfo> mounts = new ArrayList<DiskInfo>();
+    mounts.add(new DiskInfo("/dev/sda", "/mnt/disk1",
+        "5000000", "4000000", "10%", "size"));
+    info.setMounts(mounts);
+
+    info.setHostName("foo");
+    info.setInterfaces("fip_4");
+    info.setArchitecture("os_arch");
+    info.setOS("os_type");
+    info.setMemoryTotal(10);
+
+    AgentVersion agentVersion = null;
+    long currentTime = System.currentTimeMillis();
+
+    HostRegistrationRequestEvent e =
+        new HostRegistrationRequestEvent("foo", agentVersion, currentTime,
+            info);
+    host.handleEvent(e);
+    Assert.assertEquals(currentTime, host.getLastRegistrationTime());
+  }
+
+  private void verifyHost(HostImpl host) throws Exception {
+    HostVerifiedEvent e = new HostVerifiedEvent(host.getHostName());
+    host.handleEvent(e);
+  }
+
+  private void verifyHostState(HostImpl host, HostState state) {
+    Assert.assertEquals(state, host.getState());
+  }
+
+  private void sendHealthyHeartbeat(HostImpl host, long counter) throws Exception {
+    HostHealthyHeartbeatEvent e = new HostHealthyHeartbeatEvent(
+        host.getHostName(), counter);
+    host.handleEvent(e);
+  }
+
+  private void sendUnhealthyHeartbeat(HostImpl host, long counter) throws Exception {
+    HostHealthStatus healthStatus = new HostHealthStatus(HealthStatus.UNHEALTHY,
+        "Unhealthy server");
+    HostUnhealthyHeartbeatEvent e = new HostUnhealthyHeartbeatEvent(
+        host.getHostName(), counter, healthStatus);
+    host.handleEvent(e);
+  }
+
+  private void timeoutHost(HostImpl host) throws Exception {
+    HostHeartbeatTimedOutEvent e = new HostHeartbeatTimedOutEvent(
+        host.getHostName());
+    host.handleEvent(e);
+  }
+
+  @Test
+  public void testHostFSMInit() {
+    HostImpl host = new HostImpl();
+    verifyHostState(host, HostState.INIT);
+  }
+
+  @Test
+  public void testHostRegistrationFlow() throws Exception {
+    HostImpl host = new HostImpl();
+    registerHost(host);
+    verifyHostState(host, HostState.WAITING_FOR_VERIFICATION);
+
+    boolean exceptionThrown = false;
+    try {
+      registerHost(host);
+    } catch (Exception e) {
+      // Expected
+      exceptionThrown = true;
+    }
+    if (!exceptionThrown) {
+      fail("Expected invalid transition exception to be thrown");
+    }
+
+    verifyHost(host);
+    verifyHostState(host, HostState.VERIFIED);
+
+    exceptionThrown = false;
+    try {
+      verifyHost(host);
+    } catch (Exception e) {
+      // Expected
+      exceptionThrown = true;
+    }
+    if (!exceptionThrown) {
+      fail("Expected invalid transition exception to be thrown");
+    }
+  }
+
+  @Test
+  public void testHostHeartbeatFlow() throws Exception {
+    HostImpl host = new HostImpl();
+    registerHost(host);
+    verifyHost(host);
+
+    // TODO need to verify audit logs generated
+    // TODO need to verify health status updated properly
+
+    long counter = 0;
+    sendHealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.HEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+
+    sendHealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.HEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.HEALTHY,
+        host.getHealthStatus().getHealthStatus());
+
+    sendUnhealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.UNHEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.UNHEALTHY,
+        host.getHealthStatus().getHealthStatus());
+
+    sendUnhealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.UNHEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.UNHEALTHY,
+        host.getHealthStatus().getHealthStatus());
+
+    sendHealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.HEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.HEALTHY,
+        host.getHealthStatus().getHealthStatus());
+
+    timeoutHost(host);
+    verifyHostState(host, HostState.HEARTBEAT_LOST);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.UNKNOWN,
+        host.getHealthStatus().getHealthStatus());
+
+    timeoutHost(host);
+    verifyHostState(host, HostState.HEARTBEAT_LOST);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.UNKNOWN,
+        host.getHealthStatus().getHealthStatus());
+
+    sendUnhealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.UNHEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.UNHEALTHY,
+        host.getHealthStatus().getHealthStatus());
+
+    timeoutHost(host);
+    verifyHostState(host, HostState.HEARTBEAT_LOST);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.UNKNOWN,
+        host.getHealthStatus().getHealthStatus());
+
+    sendHealthyHeartbeat(host, ++counter);
+    verifyHostState(host, HostState.HEALTHY);
+    Assert.assertEquals(counter, host.getLastHeartbeatTime());
+    Assert.assertEquals(HealthStatus.HEALTHY,
+        host.getHealthStatus().getHealthStatus());
+
+  }
+}

Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/live/host/TestHostImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message