incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r1178907 - in /incubator/ambari/trunk: CHANGES.txt controller/src/main/java/org/apache/ambari/resource/statemachine/ClusterImpl.java controller/src/main/java/org/apache/ambari/resource/statemachine/ServiceImpl.java
Date Tue, 04 Oct 2011 19:04:16 GMT
Author: ddas
Date: Tue Oct  4 19:04:15 2011
New Revision: 1178907

URL: http://svn.apache.org/viewvc?rev=1178907&view=rev
Log:
AMBARI-35. Replaces the counters for keeping track of service/role start/stop with iterators.
Contributed by Devaraj Das.

Modified:
    incubator/ambari/trunk/CHANGES.txt
    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/ServiceImpl.java

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1178907&r1=1178906&r2=1178907&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Tue Oct  4 19:04:15 2011
@@ -2,6 +2,9 @@ Ambari Change log
 
 Release 0.1.0 - unreleased
 
+  AMBARI-35. Replaces the counters for keeping track of service/role start/stop
+  with iterators. (ddas)
+
   AMBARI-34. Address the start cluster part of the statemachine implementation,
   and handle the heartbeat. (ddas)
 

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=1178907&r1=1178906&r2=1178907&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
Tue Oct  4 19:04:15 2011
@@ -17,8 +17,10 @@
 */
 package org.apache.ambari.resource.statemachine;
 
+import java.util.ArrayList;
 import java.util.EnumSet;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -81,19 +83,23 @@ public class ClusterImpl implements Clus
   private List<Service> services;
   private StateMachine<ClusterState, ClusterEventType, ClusterEvent> 
           stateMachine;
-  private int numServicesStarted;
   private int totalEnabledServices;
   private Lock readLock;
   private Lock writeLock;
-  private short roleCount; 
   private String clusterName;
+  private Iterator<Service> iterator;
     
-  public ClusterImpl(String name) {
+  public ClusterImpl(String name, List<Service> services) {
     this.clusterName = name;
     ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
     this.readLock = readWriteLock.readLock();
     this.writeLock = readWriteLock.writeLock();
     this.stateMachine = stateMachineFactory.make(this);
+    this.services = services;
+  }
+  
+  public ClusterImpl(String name) {
+    this(name, new ArrayList<Service>());
   }
 
   @Override
@@ -121,29 +127,20 @@ public class ClusterImpl implements Clus
     this.services.addAll(services);
   }
   
-  public Service getNextService() {
-    if (++roleCount <= services.size()) {
-      return services.get(roleCount - 1);
+  private Service getFirstService() {
+    //this call should reset the iterator
+    iterator = services.iterator();
+    if (iterator.hasNext()) {
+      return iterator.next();
     }
     return null;
   }
   
-  private void incrStartedServiceCount() {
-    try {
-      writeLock.lock();
-      numServicesStarted++;
-    } finally {
-      writeLock.unlock();
-    }
-  }
-  
-  private int getStartedServiceCount() {
-    try {
-      readLock.lock();
-      return numServicesStarted;
-    } finally {
-      readLock.unlock();
+  private Service getNextService() {
+    if (iterator.hasNext()) {
+      return iterator.next();
     }
+    return null;
   }
   
   private int getTotalServiceCount() {
@@ -155,9 +152,8 @@ public class ClusterImpl implements Clus
 
     @Override
     public void transition(ClusterImpl operand, ClusterEvent event) {
-      Service service = operand.getNextService();
+      Service service = operand.getFirstService();
       if (service != null) {
-              //start the first service (plugin)
         StateMachineInvoker.getAMBARIEventHandler().handle(
             new ServiceEvent(ServiceEventType.START, service));
       }
@@ -171,12 +167,13 @@ public class ClusterImpl implements Clus
     public ClusterState transition(ClusterImpl operand, ClusterEvent event) {
       //check whether all services started, and if not remain in the STARTING
       //state, else move to the ACTIVE state
-      if (operand.getStartedServiceCount() == operand.getTotalServiceCount()) {
-        return ClusterState.ACTIVE;
+      Service service = operand.getNextService();
+      if (service != null) {
+        StateMachineInvoker.getAMBARIEventHandler().handle(new ServiceEvent(
+            ServiceEventType.START, service));
+        return ClusterState.STARTING;
       }
-      operand.incrStartedServiceCount();
-      //TODO: start the next service (plugin)
-      return ClusterState.STARTING;
+      return ClusterState.ACTIVE;
     }
     
   }

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=1178907&r1=1178906&r2=1178907&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
Tue Oct  4 19:04:15 2011
@@ -20,6 +20,7 @@ package org.apache.ambari.resource.state
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.EnumSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -67,7 +68,7 @@ public class ServiceImpl implements Serv
              ServiceEventType.ROLE_STARTED)
              
          .addTransition(ServiceState.ACTIVE, ServiceState.STOPPING, 
-             ServiceEventType.STOP)
+             ServiceEventType.STOP, new StopServiceTransition())
              
          .addTransition(ServiceState.STOPPING, 
              EnumSet.of(ServiceState.INACTIVE, ServiceState.STOPPING),
@@ -91,8 +92,8 @@ public class ServiceImpl implements Serv
   private final StateMachine<ServiceState, ServiceEventType, ServiceEvent>
       stateMachine;
   private final List<Role> serviceRoles = new ArrayList<Role>();
+  private Iterator<Role> iterator;
   private final String serviceName;
-  private short roleCount;
   
   public ServiceImpl(Cluster cluster, String serviceName) throws IOException {
     this.cluster = cluster;
@@ -105,6 +106,7 @@ public class ServiceImpl implements Serv
       RoleImpl roleImpl = new RoleImpl(this, role);
       serviceRoles.add(roleImpl);
     }
+    
     stateMachine = stateMachineFactory.make(this);
   }
     
@@ -136,9 +138,18 @@ public class ServiceImpl implements Serv
     this.serviceRoles.addAll(roles);
   }
   
-  public Role getNextRole() {
-    if (++roleCount <= serviceRoles.size()) {
-      return serviceRoles.get(roleCount - 1);  
+  private Role getFirstRole() {
+    //this call should reset the iterator
+    iterator = serviceRoles.iterator();
+    if (iterator.hasNext()) {
+      return iterator.next();
+    }
+    return null;
+  }
+  
+  private Role getNextRole() {
+    if (iterator.hasNext()) {
+      return iterator.next();
     }
     return null;
   }
@@ -148,13 +159,25 @@ public class ServiceImpl implements Serv
 
     @Override
     public void transition(ServiceImpl operand, ServiceEvent event) {
-      Role firstRole = operand.getNextRole();
+      Role firstRole = operand.getFirstRole();
       if (firstRole != null) {
         StateMachineInvoker.getAMBARIEventHandler().handle(
                             new RoleEvent(RoleEventType.START, firstRole));
       }
-    }
+    } 
+  }
+  
+  static class StopServiceTransition implements 
+  SingleArcTransition<ServiceImpl, ServiceEvent>  {
     
+    @Override
+    public void transition(ServiceImpl operand, ServiceEvent event) {
+      Role firstRole = operand.getFirstRole();
+      if (firstRole != null) {
+        StateMachineInvoker.getAMBARIEventHandler().handle(
+                            new RoleEvent(RoleEventType.STOP, firstRole));
+      }
+    }
   }
   
   static class RoleStartedTransition 
@@ -191,7 +214,6 @@ public class ServiceImpl implements Serv
         return ServiceState.INACTIVE;
       }
     }
-    
   }
 
   @Override



Mime
View raw message