You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by dd...@apache.org on 2011/12/11 20:20:14 UTC
svn commit: r1213043 - in /incubator/ambari/trunk: ./
controller/src/main/java/org/apache/ambari/controller/
controller/src/main/java/org/apache/ambari/datastore/
controller/src/main/java/org/apache/ambari/resource/statemachine/
controller/src/test/jav...
Author: ddas
Date: Sun Dec 11 19:20:13 2011
New Revision: 1213043
URL: http://svn.apache.org/viewvc?rev=1213043&view=rev
Log:
AMBARI-148. Refactors StateMachineInvoker
Added:
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriver.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriverInterface.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvokerInterface.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestFSMDriverImpl.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestModule.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestStateMachineInvokerImpl.java
Modified:
incubator/ambari/trunk/CHANGES.txt
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/ControllerModule.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/datastore/PersistentDataStore.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterFSM.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/RoleImpl.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ServiceImpl.java
incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvoker.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestHeartbeat.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImplServiceCreation.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestRoleImpl.java
incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestServiceImpl.java
Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Sun Dec 11 19:20:13 2011
@@ -2,6 +2,8 @@ Ambari Change log
Release 0.1.0 - unreleased
+ AMBARI-148. Refactors StateMachineInvoker (ddas)
+
AMBARI-151. Fix TestHardware when in offline mode. (omalley)
AMBARI-150. Simplifies states in controller state machine (thejas via ddas)
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/Clusters.java Sun Dec 11 19:20:13 2011
@@ -44,13 +44,11 @@ import org.apache.ambari.common.rest.ent
import org.apache.ambari.common.rest.entities.Stack;
import org.apache.ambari.datastore.PersistentDataStore;
import org.apache.ambari.resource.statemachine.ClusterFSM;
-import org.apache.ambari.resource.statemachine.StateMachineInvoker;
+import org.apache.ambari.resource.statemachine.FSMDriverInterface;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import com.google.inject.Guice;
import com.google.inject.Inject;
-import com.google.inject.Injector;
import com.google.inject.Singleton;
@Singleton
@@ -68,16 +66,19 @@ public class Clusters {
private final Nodes nodes;
private final ClusterFactory clusterFactory;
private final StackFlattener flattener;
+ private final FSMDriverInterface fsmDriver;
@Inject
private Clusters(Stacks stacks, Nodes nodes,
PersistentDataStore dataStore,
ClusterFactory clusterFactory,
- StackFlattener flattener) throws Exception {
+ StackFlattener flattener,
+ FSMDriverInterface fsmDriver) throws Exception {
this.stacks = stacks;
this.nodes = nodes;
this.dataStore = dataStore;
this.clusterFactory = clusterFactory;
+ this.fsmDriver = fsmDriver;
this.flattener = flattener;
recoverClustersStateAfterRestart();
}
@@ -332,11 +333,11 @@ public class Clusters {
* Invoke state machine event
*/
if(c.getGoalState().equals(ClusterState.CLUSTER_STATE_ACTIVE)) {
- StateMachineInvoker.startCluster(cls.getName());
+ fsmDriver.startCluster(cls.getName());
} else if(c.getGoalState().equals(ClusterState.CLUSTER_STATE_INACTIVE)) {
- StateMachineInvoker.stopCluster(cls.getName());
+ fsmDriver.stopCluster(cls.getName());
} else if(c.getGoalState().equals(ClusterState.CLUSTER_STATE_ATTIC)) {
- StateMachineInvoker.deleteCluster(cls.getName());
+ fsmDriver.deleteCluster(cls.getName());
}
return cls.getClusterDefinition(-1);
@@ -421,8 +422,7 @@ public class Clusters {
* TODO: Make sure createCluster is idempotent (i.e. if object already exists
* then return success)
*/
- ClusterFSM cs = StateMachineInvoker.createCluster(cls,cls.getLatestRevisionNumber(),
- cls.getClusterState());
+ ClusterFSM cs = fsmDriver.createCluster(cls,cls.getLatestRevisionNumber());
if(cdef.getGoalState().equals(ClusterDefinition.GOAL_STATE_ACTIVE)) {
cs.activate();
}
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/ControllerModule.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/ControllerModule.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/ControllerModule.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/ControllerModule.java Sun Dec 11 19:20:13 2011
@@ -22,9 +22,9 @@ import org.apache.ambari.controller.rest
import org.apache.ambari.controller.rest.resources.ClustersResource;
import org.apache.ambari.controller.rest.resources.NodesResource;
import org.apache.ambari.controller.rest.resources.StacksResource;
-import org.apache.ambari.datastore.PersistentDataStore;
-import org.apache.ambari.datastore.impl.ZookeeperDS;
-import org.apache.ambari.resource.statemachine.StateMachineInvoker;
+import org.apache.ambari.resource.statemachine.ClusterImpl;
+import org.apache.ambari.resource.statemachine.RoleImpl;
+import org.apache.ambari.resource.statemachine.ServiceImpl;
import com.google.inject.AbstractModule;
import com.google.inject.assistedinject.FactoryModuleBuilder;
@@ -34,12 +34,13 @@ public class ControllerModule extends Ab
@Override
protected void configure() {
install(new ComponentModule());
- bind(PersistentDataStore.class).to(ZookeeperDS.class);
requestStaticInjection(ClustersResource.class,
NodesResource.class,
StacksResource.class,
ControllerResource.class,
- StateMachineInvoker.class);
+ RoleImpl.class,
+ ServiceImpl.class,
+ ClusterImpl.class);
install(new FactoryModuleBuilder()
.implement(Cluster.class,Cluster.class)
.build(ClusterFactory.class));
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/HeartbeatHandler.java Sun Dec 11 19:20:13 2011
@@ -36,6 +36,7 @@ import org.apache.ambari.common.rest.age
import org.apache.ambari.common.rest.agent.HeartBeat;
import org.apache.ambari.components.ComponentPlugin;
import org.apache.ambari.resource.statemachine.ClusterFSM;
+import org.apache.ambari.resource.statemachine.FSMDriverInterface;
import org.apache.ambari.resource.statemachine.RoleFSM;
import org.apache.ambari.resource.statemachine.RoleEvent;
import org.apache.ambari.resource.statemachine.RoleEventType;
@@ -43,7 +44,7 @@ import org.apache.ambari.resource.statem
import org.apache.ambari.resource.statemachine.ServiceEventType;
import org.apache.ambari.resource.statemachine.ServiceFSM;
import org.apache.ambari.resource.statemachine.ServiceState;
-import org.apache.ambari.resource.statemachine.StateMachineInvoker;
+import org.apache.ambari.resource.statemachine.StateMachineInvokerInterface;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -59,11 +60,17 @@ public class HeartbeatHandler {
private static Log LOG = LogFactory.getLog(HeartbeatHandler.class);
private final Clusters clusters;
private final Nodes nodes;
-
+ private StateMachineInvokerInterface stateMachineInvoker;
+ private FSMDriverInterface driver;
+
@Inject
- HeartbeatHandler(Clusters clusters, Nodes nodes) {
+ HeartbeatHandler(Clusters clusters, Nodes nodes,
+ FSMDriverInterface driver,
+ StateMachineInvokerInterface stateMachineInvoker) {
this.clusters = clusters;
this.nodes = nodes;
+ this.driver = driver;
+ this.stateMachineInvoker = stateMachineInvoker;
}
public ControllerResponse processHeartBeat(HeartBeat heartbeat)
@@ -118,8 +125,8 @@ public class HeartbeatHandler {
//get the cluster object corresponding to the clusterId
Cluster cluster = clusters.getClusterByName(clusterName);
//get the state machine reference to the cluster
- ClusterFSM clusterFsm = StateMachineInvoker
- .getStateMachineClusterInstance(clusterName);
+ ClusterFSM clusterFsm =
+ driver.getFSMClusterInstance(clusterName);
//the state machine references to the services
List<ServiceFSM> clusterServices = clusterFsm.getServices();
@@ -147,7 +154,7 @@ public class HeartbeatHandler {
heartbeat)) {
//raise an event to the state machine for a successful
//role-start
- StateMachineInvoker.getAMBARIEventHandler()
+ stateMachineInvoker.getAMBARIEventHandler()
.handle(new RoleEvent(RoleEventType.START_SUCCESS, role));
}
}
@@ -161,7 +168,7 @@ public class HeartbeatHandler {
if (wasStopRoleSuccessful(clusterIdAndRev,
service.getServiceName(), role.getRoleName(), response,
heartbeat)) {
- StateMachineInvoker.getAMBARIEventHandler()
+ stateMachineInvoker.getAMBARIEventHandler()
.handle(new RoleEvent(RoleEventType.STOP_SUCCESS, role));
}
}
@@ -368,11 +375,11 @@ public class HeartbeatHandler {
//where the service is not available for a couple of checkservice
//invocations
if (result.getCommandResult().getExitCode() == 0) {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.AVAILABLE_CHECK_SUCCESS,
service));
} else {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.AVAILABLE_CHECK_FAILURE,
service));
}
@@ -396,11 +403,11 @@ public class HeartbeatHandler {
if (result != null) {
//this action ran
if (result.getCommandResult().getExitCode() == 0) {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.PRESTART_SUCCESS,
service));
} else {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.PRESTART_FAILURE,
service));
}
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/datastore/PersistentDataStore.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/datastore/PersistentDataStore.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/datastore/PersistentDataStore.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/datastore/PersistentDataStore.java Sun Dec 11 19:20:13 2011
@@ -23,10 +23,15 @@ import java.util.List;
import org.apache.ambari.common.rest.entities.ClusterState;
import org.apache.ambari.common.rest.entities.Stack;
import org.apache.ambari.common.rest.entities.ClusterDefinition;
+import org.apache.ambari.datastore.impl.StaticDataStore;
+import org.apache.ambari.datastore.impl.ZookeeperDS;
+
+import com.google.inject.ImplementedBy;
/**
* Abstraction that stores the Ambari state.
*/
+@ImplementedBy(ZookeeperDS.class)
public interface PersistentDataStore {
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterFSM.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterFSM.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterFSM.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterFSM.java Sun Dec 11 19:20:13 2011
@@ -20,13 +20,11 @@ package org.apache.ambari.resource.state
import java.util.List;
import java.util.Map;
-import org.apache.ambari.common.rest.entities.ClusterState;
-
public interface ClusterFSM {
public List<ServiceFSM> getServices();
public Map<String, String> getServiceStates();
public void terminate();
- public ClusterState getClusterState();
+ public String getClusterState();
public void activate();
public void deactivate();
}
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java Sun Dec 11 19:20:13 2011
@@ -28,7 +28,6 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
-import org.apache.ambari.common.rest.entities.ClusterState;
import org.apache.ambari.common.state.MultipleArcTransition;
import org.apache.ambari.common.state.SingleArcTransition;
import org.apache.ambari.common.state.StateMachine;
@@ -39,6 +38,8 @@ import org.apache.ambari.event.EventHand
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import com.google.inject.Inject;
+
public class ClusterImpl implements ClusterFSM, EventHandler<ClusterEvent> {
/* The state machine for the cluster looks like:
@@ -90,11 +91,13 @@ public class ClusterImpl implements Clus
private Lock readLock;
private Lock writeLock;
private Iterator<ServiceFSM> iterator;
- private ClusterState clusterState;
private static Log LOG = LogFactory.getLog(ClusterImpl.class);
-
- public ClusterImpl(Cluster cluster, int revision,
- ClusterState clusterState) throws IOException {
+ private static StateMachineInvokerInterface stateMachineInvoker;
+ @Inject
+ public static void setInvoker(StateMachineInvokerInterface sm) {
+ stateMachineInvoker = sm;
+ }
+ public ClusterImpl(Cluster cluster, int revision) throws IOException {
ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
this.readLock = readWriteLock.readLock();
this.writeLock = readWriteLock.writeLock();
@@ -112,7 +115,6 @@ public class ClusterImpl implements Clus
}
}
this.services = serviceImpls;
- this.clusterState = clusterState;
}
private static boolean hasActiveRoles(Cluster cluster, String serviceName)
@@ -157,8 +159,8 @@ public class ClusterImpl implements Clus
}
@Override
- public ClusterState getClusterState() {
- return clusterState;
+ public String getClusterState() {
+ return getState().toString();
}
static class StartClusterTransition implements
@@ -166,10 +168,9 @@ public class ClusterImpl implements Clus
@Override
public void transition(ClusterImpl operand, ClusterEvent event) {
- operand.getClusterState().setState(operand.getState().name());
ServiceFSM service = operand.getFirstService();
if (service != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.START, service));
}
}
@@ -181,11 +182,10 @@ public class ClusterImpl implements Clus
@Override
public void transition(ClusterImpl operand, ClusterEvent event) {
- operand.getClusterState().setState(operand.getState().name());
//TODO: do it in the reverse order of startup
ServiceFSM service = operand.getFirstService();
if (service != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.STOP, service));
}
}
@@ -196,12 +196,11 @@ public class ClusterImpl implements Clus
@Override
public ClusterStateFSM transition(ClusterImpl operand, ClusterEvent event) {
- operand.getClusterState().setState(operand.getState().name());
//check whether all services stopped, and if not remain in the STOPPING
//state, else move to the INACTIVE state
ServiceFSM service = operand.getNextService();
if (service != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(new ServiceEvent(
+ stateMachineInvoker.getAMBARIEventHandler().handle(new ServiceEvent(
ServiceEventType.STOP, service));
return ClusterStateFSM.STOPPING;
}
@@ -214,12 +213,11 @@ public class ClusterImpl implements Clus
MultipleArcTransition<ClusterImpl, ClusterEvent, ClusterStateFSM> {
@Override
public ClusterStateFSM transition(ClusterImpl operand, ClusterEvent event){
- operand.getClusterState().setState(operand.getState().name());
//check whether all services started, and if not remain in the STARTING
//state, else move to the ACTIVE state
ServiceFSM service = operand.getFirstService();
if (service != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(new ServiceEvent(
+ stateMachineInvoker.getAMBARIEventHandler().handle(new ServiceEvent(
ServiceEventType.START, service));
return ClusterStateFSM.STARTING;
}
@@ -239,19 +237,19 @@ public class ClusterImpl implements Clus
@Override
public void activate() {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ClusterEvent(ClusterEventType.START, this));
}
@Override
public void deactivate() {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ClusterEvent(ClusterEventType.STOP, this));
}
@Override
public void terminate() {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ClusterEvent(ClusterEventType.RELEASE_NODES, this));
}
Added: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriver.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriver.java?rev=1213043&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriver.java (added)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriver.java Sun Dec 11 19:20:13 2011
@@ -0,0 +1,66 @@
+/**
+* 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.resource.statemachine;
+
+import java.io.IOException;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.ambari.controller.Cluster;
+
+import com.google.inject.Singleton;
+
+@Singleton
+public class FSMDriver implements FSMDriverInterface {
+ private ConcurrentMap<String, ClusterFSM> clusters;
+
+ public ClusterFSM createCluster(Cluster cluster, int revision)
+ throws IOException {
+ ClusterFSM clusterFSM = new ClusterImpl(cluster, revision);
+ clusters.put(cluster.getName(), clusterFSM);
+ return clusterFSM;
+ }
+
+ public void startCluster(String clusterId) {
+ ClusterFSM clusterFSM = clusters.get(clusterId);
+ clusterFSM.activate();
+ }
+
+ public void stopCluster(String clusterId) {
+ ClusterFSM clusterFSM = clusters.get(clusterId);
+ clusterFSM.deactivate();
+ }
+
+ public void deleteCluster(String clusterId) {
+ ClusterFSM clusterFSM = clusters.get(clusterId);
+ clusterFSM.deactivate();
+ clusterFSM.terminate();
+ clusters.remove(clusterId);
+ }
+
+ public String getClusterState(String clusterId,
+ long clusterDefinitionRev) {
+ return clusters.get(clusterId).getClusterState();
+ }
+
+ @Override
+ public ClusterFSM getFSMClusterInstance(String clusterId) {
+ return clusters.get(clusterId);
+ }
+
+}
Added: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriverInterface.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriverInterface.java?rev=1213043&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriverInterface.java (added)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/FSMDriverInterface.java Sun Dec 11 19:20:13 2011
@@ -0,0 +1,43 @@
+/**
+* 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.resource.statemachine;
+
+import java.io.IOException;
+
+import org.apache.ambari.controller.Cluster;
+
+import com.google.inject.ImplementedBy;
+
+@ImplementedBy(FSMDriver.class)
+public interface FSMDriverInterface {
+ public ClusterFSM createCluster(Cluster cluster, int revision)
+ throws IOException;
+
+ public void startCluster(String clusterId);
+
+ public void stopCluster(String clusterId);
+
+ public void deleteCluster(String clusterId);
+
+ public ClusterFSM getFSMClusterInstance(String clusterId);
+
+ public String getClusterState(String clusterId,
+ long clusterDefinitionRev);
+
+}
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/RoleImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/RoleImpl.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/RoleImpl.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/RoleImpl.java Sun Dec 11 19:20:13 2011
@@ -22,11 +22,17 @@ import org.apache.ambari.common.state.St
import org.apache.ambari.common.state.StateMachineFactory;
import org.apache.ambari.event.EventHandler;
+import com.google.inject.Inject;
+
public class RoleImpl implements RoleFSM, EventHandler<RoleEvent> {
private final String roleName;
private final ServiceFSM service;
-
+ private static StateMachineInvokerInterface stateMachineInvoker;
+ @Inject
+ public static void setInvoker(StateMachineInvokerInterface sm) {
+ stateMachineInvoker = sm;
+ }
/* The state machine for the role looks like:
* (INACTIVE or FAIL) --S_START--> STARTING --S_START_SUCCESS--> ACTIVE
* --S_START_FAILURE--> FAIL
@@ -122,7 +128,7 @@ public class RoleImpl implements RoleFSM
ServiceFSM service = operand.getAssociatedService();
//if one instance of the role starts up fine, we consider the service
//as ready for the 'safe-mode' kinds of checks
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.ROLE_START_SUCCESS, service,
operand));
}
@@ -136,7 +142,7 @@ public class RoleImpl implements RoleFSM
ServiceFSM service = operand.getAssociatedService();
//if one instance of the role starts up fine, we consider the service
//as ready for the 'safe-mode' kinds of checks
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.ROLE_START_SUCCESS, service,
operand));
}
@@ -148,7 +154,7 @@ public class RoleImpl implements RoleFSM
@Override
public void transition(RoleImpl operand, RoleEvent event) {
ServiceFSM service = operand.getAssociatedService();
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.ROLE_STOP_SUCCESS, service,
operand));
}
@@ -156,13 +162,13 @@ public class RoleImpl implements RoleFSM
@Override
public void activate() {
- StateMachineInvoker.getAMBARIEventHandler()
+ stateMachineInvoker.getAMBARIEventHandler()
.handle(new RoleEvent(RoleEventType.START, this));
}
@Override
public void deactivate() {
- StateMachineInvoker.getAMBARIEventHandler()
+ stateMachineInvoker.getAMBARIEventHandler()
.handle(new RoleEvent(RoleEventType.STOP, this));
}
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ServiceImpl.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ServiceImpl.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/ServiceImpl.java Sun Dec 11 19:20:13 2011
@@ -29,6 +29,8 @@ import org.apache.ambari.common.state.St
import org.apache.ambari.common.state.StateMachineFactory;
import org.apache.ambari.event.EventHandler;
+import com.google.inject.Inject;
+
public class ServiceImpl implements ServiceFSM, EventHandler<ServiceEvent> {
private final ClusterFSM clusterFsm;
@@ -43,7 +45,11 @@ public class ServiceImpl implements Serv
* ACTIVE or FAIL --S_STOP--> STOPPING --S_STOP_SUCCESS--> INACTIVE
* --S_STOP_FAILURE--> FAIL
*/
-
+ private static StateMachineInvokerInterface stateMachineInvoker;
+ @Inject
+ public static void setInvoker(StateMachineInvokerInterface sm) {
+ stateMachineInvoker = sm;
+ }
private static final StateMachineFactory
<ServiceImpl, ServiceState, ServiceEventType, ServiceEvent>
stateMachineFactory
@@ -172,7 +178,7 @@ public class ServiceImpl implements Serv
public void transition(ServiceImpl operand, ServiceEvent event) {
RoleFSM firstRole = operand.getFirstRole();
if (firstRole != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new RoleEvent(RoleEventType.START, firstRole));
}
}
@@ -188,7 +194,7 @@ public class ServiceImpl implements Serv
//since we support starting services explicitly (without touching the
//associated cluster), we need to check what the cluster state is
//before sending it any event
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ClusterEvent(ClusterEventType.START_SUCCESS,
operand.getAssociatedCluster()));
}
@@ -205,7 +211,7 @@ public class ServiceImpl implements Serv
//since we support starting services explicitly (without touching the
//associated cluster), we need to check what the cluster state is
//before sending it any event
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ClusterEvent(ClusterEventType.START_FAILURE,
operand.getAssociatedCluster()));
}
@@ -219,7 +225,7 @@ public class ServiceImpl implements Serv
public void transition(ServiceImpl operand, ServiceEvent event) {
RoleFSM firstRole = operand.getFirstRole();
if (firstRole != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new RoleEvent(RoleEventType.STOP, firstRole));
}
}
@@ -234,7 +240,7 @@ public class ServiceImpl implements Serv
//state, else move to the STARTED state
RoleFSM role = operand.getNextRole();
if (role != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(new RoleEvent(
+ stateMachineInvoker.getAMBARIEventHandler().handle(new RoleEvent(
RoleEventType.START, role));
return ServiceState.STARTING;
} else {
@@ -252,7 +258,7 @@ public class ServiceImpl implements Serv
//state, else move to the INACTIVE state
RoleFSM role = operand.getNextRole();
if (role != null) {
- StateMachineInvoker.getAMBARIEventHandler().handle(new RoleEvent(
+ stateMachineInvoker.getAMBARIEventHandler().handle(new RoleEvent(
RoleEventType.STOP, role));
return ServiceState.STOPPING;
} else {
@@ -261,7 +267,7 @@ public class ServiceImpl implements Serv
//since we support stopping services explicitly (without stopping the
//associated cluster), we need to check what the cluster state is
//before sending it any event
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ClusterEvent(ClusterEventType.STOP_SUCCESS,
operand.getAssociatedCluster()));
}
@@ -272,13 +278,13 @@ public class ServiceImpl implements Serv
@Override
public void activate() {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.START, this));
}
@Override
public void deactivate() {
- StateMachineInvoker.getAMBARIEventHandler().handle(
+ stateMachineInvoker.getAMBARIEventHandler().handle(
new ServiceEvent(ServiceEventType.STOP, this));
}
Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvoker.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvoker.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvoker.java (original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvoker.java Sun Dec 11 19:20:13 2011
@@ -17,49 +17,33 @@
*/
package org.apache.ambari.resource.statemachine;
-import java.io.IOException;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-
-import org.apache.ambari.common.rest.entities.ClusterState;
-import org.apache.ambari.controller.Cluster;
import org.apache.ambari.event.AsyncDispatcher;
import org.apache.ambari.event.Dispatcher;
import org.apache.ambari.event.EventHandler;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import com.google.inject.Inject;
+import com.google.inject.Singleton;
-public class StateMachineInvoker {
+@Singleton
+class StateMachineInvoker implements StateMachineInvokerInterface {
+
+ private Dispatcher dispatcher;
@Inject
- public static void init(AsyncDispatcher d,
- ConcurrentHashMap<String, ClusterFSM> c) {
- clusters = c;
- dispatcher = d;
+ StateMachineInvoker() {
+ dispatcher = new AsyncDispatcher();
dispatcher.register(ClusterEventType.class, new ClusterEventDispatcher());
dispatcher.register(ServiceEventType.class, new ServiceEventDispatcher());
dispatcher.register(RoleEventType.class, new RoleEventDispatcher());
dispatcher.start();
}
- private static Dispatcher dispatcher;
-
- private static Log LOG = LogFactory.getLog(StateMachineInvoker.class);
- public Dispatcher getAMBARIDispatcher() {
- return dispatcher;
- }
-
- public static void setAMBARIDispatcher(Dispatcher dispatcher1) {
- dispatcher = dispatcher1;
- }
- public static EventHandler getAMBARIEventHandler() {
+ public EventHandler getAMBARIEventHandler() {
return dispatcher.getEventHandler();
}
- public static class ClusterEventDispatcher
+ private static class ClusterEventDispatcher
implements EventHandler<ClusterEvent> {
@Override
public void handle(ClusterEvent event) {
@@ -67,7 +51,7 @@ public class StateMachineInvoker {
}
}
- public static class ServiceEventDispatcher
+ private static class ServiceEventDispatcher
implements EventHandler<ServiceEvent> {
@Override
public void handle(ServiceEvent event) {
@@ -75,54 +59,11 @@ public class StateMachineInvoker {
}
}
- public static class RoleEventDispatcher
+ private static class RoleEventDispatcher
implements EventHandler<RoleEvent> {
@Override
public void handle(RoleEvent event) {
((EventHandler<RoleEvent>)event.getRole()).handle(event);
}
- }
-
- private static ConcurrentMap<String, ClusterFSM> clusters;
-
- public static ClusterFSM createCluster(Cluster cluster, int revision,
- ClusterState state) throws IOException {
- ClusterImpl clusterFSM = new ClusterImpl(cluster, revision, state);
- clusters.put(cluster.getName(), clusterFSM);
- return clusterFSM;
- }
-
- public static void startCluster(String clusterId) {
- ClusterFSM clusterFSM = clusters.get(clusterId);
- clusterFSM.activate();
- }
-
- public static void stopCluster(String clusterId) {
- ClusterFSM clusterFSM = clusters.get(clusterId);
- clusterFSM.deactivate();
- }
-
- public static void deleteCluster(String clusterId) {
- ClusterFSM clusterFSM = clusters.get(clusterId);
- clusterFSM.deactivate();
- clusterFSM.terminate();
- clusters.remove(clusterId);
- }
-
- public static ClusterFSM getStateMachineClusterInstance(String clusterId) {
- return clusters.get(clusterId);
- }
-
- public static void setStateMachineClusterInstance(String clusterId,
- ClusterFSM clusterFsm) {
- clusters.put(clusterId, clusterFsm);
- }
-
- public static ClusterState getClusterState(String clusterId,
- long clusterDefinitionRev) {
- return clusters.get(clusterId).getClusterState();
- }
-
-
-
+ }
}
Added: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvokerInterface.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvokerInterface.java?rev=1213043&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvokerInterface.java (added)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/resource/statemachine/StateMachineInvokerInterface.java Sun Dec 11 19:20:13 2011
@@ -0,0 +1,27 @@
+/**
+* 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.resource.statemachine;
+
+import org.apache.ambari.event.EventHandler;
+
+import com.google.inject.ImplementedBy;
+
+@ImplementedBy(StateMachineInvoker.class)
+public interface StateMachineInvokerInterface {
+ public EventHandler getAMBARIEventHandler();
+}
Added: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestFSMDriverImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestFSMDriverImpl.java?rev=1213043&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestFSMDriverImpl.java (added)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestFSMDriverImpl.java Sun Dec 11 19:20:13 2011
@@ -0,0 +1,69 @@
+/*
+ * 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.controller;
+
+import java.io.IOException;
+
+import org.apache.ambari.resource.statemachine.ClusterFSM;
+import org.apache.ambari.resource.statemachine.FSMDriverInterface;
+
+class TestFSMDriverImpl implements FSMDriverInterface {
+
+ ClusterFSM clusterFsm;
+ public void setClusterFsm(ClusterFSM clusterFsm) {
+ this.clusterFsm = clusterFsm;
+ }
+
+ @Override
+ public ClusterFSM createCluster(Cluster cluster, int revision)
+ throws IOException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void startCluster(String clusterId) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void stopCluster(String clusterId) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void deleteCluster(String clusterId) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public ClusterFSM getFSMClusterInstance(String clusterId) {
+ return clusterFsm;
+ }
+
+ @Override
+ public String getClusterState(String clusterId,
+ long clusterDefinitionRev) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
\ No newline at end of file
Modified: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestHeartbeat.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestHeartbeat.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestHeartbeat.java (original)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/controller/TestHeartbeat.java Sun Dec 11 19:20:13 2011
@@ -26,7 +26,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.ambari.common.rest.agent.Action;
import org.apache.ambari.common.rest.agent.Action.Kind;
@@ -42,9 +41,11 @@ import org.apache.ambari.common.rest.ent
import org.apache.ambari.components.ComponentPlugin;
import org.apache.ambari.controller.HeartbeatHandler.ClusterNameAndRev;
import org.apache.ambari.controller.HeartbeatHandler.SpecialServiceIDs;
-import org.apache.ambari.event.AsyncDispatcher;
+import org.apache.ambari.datastore.PersistentDataStore;
+import org.apache.ambari.datastore.impl.StaticDataStore;
import org.apache.ambari.event.EventHandler;
import org.apache.ambari.resource.statemachine.ClusterFSM;
+import org.apache.ambari.resource.statemachine.FSMDriverInterface;
import org.apache.ambari.resource.statemachine.RoleEvent;
import org.apache.ambari.resource.statemachine.RoleEventType;
import org.apache.ambari.resource.statemachine.RoleFSM;
@@ -53,11 +54,13 @@ import org.apache.ambari.resource.statem
import org.apache.ambari.resource.statemachine.ServiceEventType;
import org.apache.ambari.resource.statemachine.ServiceFSM;
import org.apache.ambari.resource.statemachine.ServiceState;
-import org.apache.ambari.resource.statemachine.StateMachineInvoker;
-import org.testng.annotations.AfterTest;
+import org.apache.ambari.resource.statemachine.StateMachineInvokerInterface;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
public class TestHeartbeat {
ComponentPlugin plugin;
@@ -67,15 +70,28 @@ public class TestHeartbeat {
Cluster cluster;
Nodes nodes;
Clusters clusters;
+ StateMachineInvokerInterface invoker;
+ FSMDriverInterface driver;
HeartBeat heartbeat;
Node node;
+ Injector injector;
final String script = "script-content";
final int scriptHash = script.hashCode();
- private static ConcurrentHashMap<String, ClusterFSM> c;
+ class TestModule extends ControllerModule {
+ @Override
+ protected void configure() {
+ super.configure();
+ bind(PersistentDataStore.class).to(StaticDataStore.class);
+ bind(FSMDriverInterface.class).to(TestFSMDriverImpl.class);
+ }
+ }
@BeforeMethod
public void setup() throws Exception {
+ injector = Guice.createInjector(new TestModule());
+ driver = injector.getInstance(FSMDriverInterface.class);
+ invoker = injector.getInstance(StateMachineInvokerInterface.class);
plugin = mock(ComponentPlugin.class);
when(plugin.getActiveRoles()).thenReturn(roles);
cdef = mock(ClusterDefinition.class);
@@ -114,20 +130,13 @@ public class TestHeartbeat {
heartbeat.setInstallScriptHash(-1);
heartbeat.setHostname("localhost");
heartbeat.setInstalledRoleStates(new ArrayList<AgentRoleState>());
- StateMachineInvoker.init(new AsyncDispatcher(),
- (c=new ConcurrentHashMap<String, ClusterFSM>()));
}
- @AfterTest
- public void teardown() {
- c.clear();
- }
-
@Test
public void testInstall() throws Exception {
//send a heartbeat and get a response with install/config action
- HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes);
-
+ HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes,
+ driver, invoker);
ControllerResponse response = handler.processHeartBeat(heartbeat);
List<Action> actions = response.getActions();
assert(actions.size() == 1);
@@ -142,7 +151,7 @@ public class TestHeartbeat {
TestClusterImpl clusterImpl = new TestClusterImpl(services,roles);
((TestRoleImpl)clusterImpl.getServices()
.get(0).getRoles().get(0)).setShouldStart(true);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
processHeartbeatAndGetResponse(true);
}
@@ -154,7 +163,7 @@ public class TestHeartbeat {
TestClusterImpl clusterImpl = new TestClusterImpl(services,roles);
((TestRoleImpl)clusterImpl.getServices()
.get(0).getRoles().get(0)).setShouldStart(false);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
processHeartbeatAndGetResponse(false);
}
@@ -163,7 +172,7 @@ public class TestHeartbeat {
//send a heartbeat with some role server start success,
//and then the role should be considered active
TestClusterImpl clusterImpl = new TestClusterImpl(services,roles);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
RoleFSM roleFsm = clusterImpl.getServices()
.get(0).getRoles().get(0);
heartbeat.setInstallScriptHash(scriptHash);
@@ -175,7 +184,8 @@ public class TestHeartbeat {
roleState.setComponentName("comp1");
installedRoleStates.add(roleState);
heartbeat.setInstalledRoleStates(installedRoleStates);
- HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes);
+ HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes,
+ driver, invoker);
ControllerResponse response = handler.processHeartBeat(heartbeat);
checkActions(response, true);
int i = 0;
@@ -194,7 +204,7 @@ public class TestHeartbeat {
ServiceFSM serviceImpl = clusterImpl.getServices().get(0);
((TestRoleImpl)clusterImpl.getServices().get(0).getRoles().get(0)).setShouldStart(false);
((TestServiceImpl)serviceImpl).setServiceState(ServiceState.PRESTART);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
checkSpecialAction(ServiceState.PRESTART, ServiceEventType.START,
SpecialServiceIDs.SERVICE_PRESTART_CHECK_ID);
}
@@ -204,7 +214,7 @@ public class TestHeartbeat {
TestClusterImpl clusterImpl = new TestClusterImpl(services,roles);
ServiceFSM serviceImpl = clusterImpl.getServices().get(0);
((TestServiceImpl)serviceImpl).setServiceState(ServiceState.STARTED);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
checkSpecialAction(ServiceState.STARTED, ServiceEventType.ROLE_START_SUCCESS,
SpecialServiceIDs.SERVICE_AVAILABILITY_CHECK_ID);
}
@@ -212,13 +222,13 @@ public class TestHeartbeat {
@Test
public void testServiceAvailableEvent() throws Exception {
TestClusterImpl clusterImpl = new TestClusterImpl(services,roles);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
heartbeat.setInstallScriptHash(scriptHash);
ServiceFSM serviceImpl = clusterImpl.getServices().get(0);
((TestServiceImpl)serviceImpl).setServiceState(ServiceState.STARTED);
ActionResult actionResult = new ActionResult();
actionResult.setKind(Kind.RUN_ACTION);
- ClusterNameAndRev clusterNameAndRev = new ClusterNameAndRev("cluster1", -1);
+ ClusterNameAndRev clusterNameAndRev = new ClusterNameAndRev("cluster1",-1);
String checkActionId = HeartbeatHandler.getSpecialActionID(
clusterNameAndRev, "comp1", "abc",
SpecialServiceIDs.SERVICE_AVAILABILITY_CHECK_ID);
@@ -230,7 +240,8 @@ public class TestHeartbeat {
List<ActionResult> actionResults = new ArrayList<ActionResult>();
actionResults.add(actionResult);
heartbeat.setActionResults(actionResults);
- HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes);
+ HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes,
+ driver, invoker);
handler.processHeartBeat(heartbeat);
int i = 0;
while (i++ < 10) {
@@ -245,7 +256,7 @@ public class TestHeartbeat {
@Test
public void testServiceReadyToStartEvent() throws Exception {
TestClusterImpl clusterImpl = new TestClusterImpl(services,roles);
- c.put("cluster1", clusterImpl);
+ updateTestFSMDriverImpl(clusterImpl);
heartbeat.setInstallScriptHash(scriptHash);
ServiceFSM serviceImpl = clusterImpl.getServices().get(0);
((TestServiceImpl)serviceImpl).setServiceState(ServiceState.PRESTART);
@@ -263,7 +274,8 @@ public class TestHeartbeat {
List<ActionResult> actionResults = new ArrayList<ActionResult>();
actionResults.add(actionResult);
heartbeat.setActionResults(actionResults);
- HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes);
+ HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes,
+ driver, invoker);
handler.processHeartBeat(heartbeat);
int i = 0;
while (i++ < 10) {
@@ -279,7 +291,8 @@ public class TestHeartbeat {
ServiceEventType serviceEventType,
SpecialServiceIDs serviceId) throws Exception {
heartbeat.setInstallScriptHash(scriptHash);
- HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes);
+ HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes,
+ driver, invoker);
ControllerResponse response = handler.processHeartBeat(heartbeat);
checkActions(response, ServiceState.STARTED == serviceState);
ClusterNameAndRev clusterNameAndRev = new ClusterNameAndRev("cluster1", -1);
@@ -297,10 +310,15 @@ public class TestHeartbeat {
assert(found != false);
}
+ private void updateTestFSMDriverImpl(TestClusterImpl clusterImpl) {
+ ((TestFSMDriverImpl)driver).setClusterFsm(clusterImpl);
+ }
+
private void processHeartbeatAndGetResponse(boolean shouldFindStart)
throws Exception {
heartbeat.setInstallScriptHash(scriptHash);
- HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes);
+ HeartbeatHandler handler = new HeartbeatHandler(clusters, nodes,
+ driver, invoker);
ControllerResponse response = handler.processHeartBeat(heartbeat);
checkActions(response, shouldFindStart);
}
@@ -352,7 +370,7 @@ public class TestHeartbeat {
}
@Override
- public ClusterState getClusterState() {
+ public String getClusterState() {
// TODO Auto-generated method stub
return null;
}
Modified: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java (original)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImpl.java Sun Dec 11 19:20:13 2011
@@ -9,11 +9,12 @@ import java.io.IOException;
import java.util.ArrayList;
import org.apache.ambari.common.rest.entities.ClusterDefinition;
-import org.apache.ambari.common.rest.entities.ClusterState;
import org.apache.ambari.controller.Cluster;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import com.google.inject.Guice;
+
/**
*Test ClusterImpl state transitions
*/
@@ -34,14 +35,12 @@ public class TestClusterImpl {
@BeforeMethod
public void setup() throws IOException{
- StateMachineInvoker.setAMBARIDispatcher(new NoOPDispatcher());
-
+ Guice.createInjector(new TestModule());
ClusterDefinition clusterDef = mock(ClusterDefinition.class);
when(clusterDef.getEnabledServices()).thenReturn(new ArrayList<String>());
Cluster cluster = mock(Cluster.class);
when(cluster.getClusterDefinition(anyInt())).thenReturn(clusterDef);
- ClusterState clusterState= new ClusterState();
- clusterImpl = new ClusterImpl(cluster, 1, clusterState);
+ clusterImpl = new ClusterImpl(cluster, 1);
}
/**
Modified: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImplServiceCreation.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImplServiceCreation.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImplServiceCreation.java (original)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestClusterImplServiceCreation.java Sun Dec 11 19:20:13 2011
@@ -78,7 +78,7 @@ public class TestClusterImplServiceCreat
when(cluster.getComponentDefinition("comp1")).thenReturn(componentPlugin1);
when(cluster.getComponentDefinition("comp2")).thenReturn(componentPlugin2);
- ClusterImpl clusterImpl = new ClusterImpl(cluster, 1, null);
+ ClusterImpl clusterImpl = new ClusterImpl(cluster, 1);
return clusterImpl;
}
Added: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestModule.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestModule.java?rev=1213043&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestModule.java (added)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestModule.java Sun Dec 11 19:20:13 2011
@@ -0,0 +1,31 @@
+/*
+ * 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.resource.statemachine;
+
+import com.google.inject.AbstractModule;
+
+class TestModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ bind(StateMachineInvokerInterface.class)
+ .to(TestStateMachineInvokerImpl.class);
+ requestStaticInjection(RoleImpl.class, ServiceImpl.class,
+ ClusterImpl.class);
+ }
+}
Modified: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestRoleImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestRoleImpl.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestRoleImpl.java (original)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestRoleImpl.java Sun Dec 11 19:20:13 2011
@@ -10,18 +10,18 @@ import org.apache.ambari.common.state.In
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import com.google.inject.Guice;
+
public class TestRoleImpl {
RoleImpl role;
-
@BeforeMethod
public void setup(){
- StateMachineInvoker.setAMBARIDispatcher(new NoOPDispatcher());
+ Guice.createInjector(new TestModule());
ServiceFSM service = mock(ServiceFSM.class);
role = new RoleImpl(service, "role1");
-
}
@Test
Modified: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestServiceImpl.java?rev=1213043&r1=1213042&r2=1213043&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestServiceImpl.java (original)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestServiceImpl.java Sun Dec 11 19:20:13 2011
@@ -10,6 +10,8 @@ import java.util.Arrays;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import com.google.inject.Guice;
+
/**
*
*/
@@ -31,10 +33,9 @@ public class TestServiceImpl {
ServiceState.ACTIVE
};
-
@BeforeMethod
public void setup() throws IOException{
- StateMachineInvoker.setAMBARIDispatcher(new NoOPDispatcher());
+ Guice.createInjector(new TestModule());
String roles[] = {"role1"};
ClusterImpl clusterImpl = mock(ClusterImpl.class);
service = new ServiceImpl(roles, clusterImpl, "service1");
Added: incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestStateMachineInvokerImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestStateMachineInvokerImpl.java?rev=1213043&view=auto
==============================================================================
--- incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestStateMachineInvokerImpl.java (added)
+++ incubator/ambari/trunk/controller/src/test/java/org/apache/ambari/resource/statemachine/TestStateMachineInvokerImpl.java Sun Dec 11 19:20:13 2011
@@ -0,0 +1,28 @@
+/*
+ * 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.resource.statemachine;
+
+import org.apache.ambari.event.EventHandler;
+
+class TestStateMachineInvokerImpl implements StateMachineInvokerInterface {
+
+ @Override
+ public EventHandler getAMBARIEventHandler() {
+ return new NoOPDispatcher().getEventHandler();
+ }
+}
\ No newline at end of file