You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sm...@apache.org on 2013/06/04 20:33:32 UTC

svn commit: r1489550 [3/3] - in /incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state: ./ cluster/ svccomphost/

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java?rev=1489550&r1=1489549&r2=1489550&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java Tue Jun  4 18:33:30 2013
@@ -64,6 +64,7 @@ public class ServiceComponentHostImpl im
 
   // FIXME need more debug logs
 
+  private final ReadWriteLock clusterGlobalLock;
   private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
   private final Lock readLock = readWriteLock.readLock();
   private final Lock writeLock = readWriteLock.writeLock();
@@ -548,46 +549,56 @@ public class ServiceComponentHostImpl im
 
 
   private void resetLastOpInfo() {
+    clusterGlobalLock.readLock().lock();
     try {
-      writeLock.lock();
-      setLastOpStartTime(-1);
-      setLastOpLastUpdateTime(-1);
-      setLastOpEndTime(-1);
-    }
-    finally {
-      writeLock.unlock();
+      try {
+        writeLock.lock();
+        setLastOpStartTime(-1);
+        setLastOpLastUpdateTime(-1);
+        setLastOpEndTime(-1);
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   private void updateLastOpInfo(ServiceComponentHostEventType eventType,
       long time) {
+    clusterGlobalLock.readLock().lock();
     try {
-      writeLock.lock();
-      switch (eventType) {
-        case HOST_SVCCOMP_INSTALL:
-        case HOST_SVCCOMP_START:
-        case HOST_SVCCOMP_STOP:
-        case HOST_SVCCOMP_UNINSTALL:
-        case HOST_SVCCOMP_WIPEOUT:
-        case HOST_SVCCOMP_OP_RESTART:
-          resetLastOpInfo();
-          setLastOpStartTime(time);
-          break;
-        case HOST_SVCCOMP_OP_FAILED:
-        case HOST_SVCCOMP_OP_SUCCEEDED:
-        case HOST_SVCCOMP_STOPPED:
-        case HOST_SVCCOMP_STARTED:
-          setLastOpLastUpdateTime(time);
-          setLastOpEndTime(time);
-          break;
-        case HOST_SVCCOMP_OP_IN_PROGRESS:
-          setLastOpLastUpdateTime(time);
-          break;
+      try {
+        writeLock.lock();
+        switch (eventType) {
+          case HOST_SVCCOMP_INSTALL:
+          case HOST_SVCCOMP_START:
+          case HOST_SVCCOMP_STOP:
+          case HOST_SVCCOMP_UNINSTALL:
+          case HOST_SVCCOMP_WIPEOUT:
+          case HOST_SVCCOMP_OP_RESTART:
+            resetLastOpInfo();
+            setLastOpStartTime(time);
+            break;
+          case HOST_SVCCOMP_OP_FAILED:
+          case HOST_SVCCOMP_OP_SUCCEEDED:
+          case HOST_SVCCOMP_STOPPED:
+          case HOST_SVCCOMP_STARTED:
+            setLastOpLastUpdateTime(time);
+            setLastOpEndTime(time);
+            break;
+          case HOST_SVCCOMP_OP_IN_PROGRESS:
+            setLastOpLastUpdateTime(time);
+            break;
+        }
+      } finally {
+        writeLock.unlock();
       }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
-    finally {
-      writeLock.unlock();
-    }
+
   }
 
   @AssistedInject
@@ -602,6 +613,7 @@ public class ServiceComponentHostImpl im
     }
 
     this.serviceComponent = serviceComponent;
+    this.clusterGlobalLock = serviceComponent.getClusterGlobalLock();
 
     stateEntity = new HostComponentStateEntity();
     stateEntity.setClusterId(serviceComponent.getClusterId());
@@ -640,7 +652,7 @@ public class ServiceComponentHostImpl im
                                   Injector injector) {
     injector.injectMembers(this);
     this.serviceComponent = serviceComponent;
-
+    this.clusterGlobalLock = serviceComponent.getClusterGlobalLock();
 
     this.desiredStateEntity = desiredStateEntity;
     this.stateEntity = stateEntity;
@@ -673,26 +685,36 @@ public class ServiceComponentHostImpl im
 
   @Override
   public State getState() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return stateMachine.getCurrentState();
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return stateMachine.getCurrentState();
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void setState(State state) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      stateMachine.setCurrentState(state);
-      stateEntity.setCurrentState(state);
-      saveIfPersisted();
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        stateMachine.setCurrentState(state);
+        stateEntity.setCurrentState(state);
+        saveIfPersisted();
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
@@ -705,27 +727,32 @@ public class ServiceComponentHostImpl im
           + ", event=" + event.toString());
     }
     State oldState = getState();
+    clusterGlobalLock.readLock().lock();
     try {
-      writeLock.lock();
       try {
-        stateMachine.doTransition(event.getType(), event);
-        stateEntity.setCurrentState(stateMachine.getCurrentState());
-        saveIfPersisted();
-        // TODO Audit logs
-      } catch (InvalidStateTransitionException e) {
-        LOG.error("Can't handle ServiceComponentHostEvent event at"
-            + " current state"
-            + ", serviceComponentName=" + this.getServiceComponentName()
-            + ", hostName=" + this.getHostName()
-            + ", currentState=" + oldState
-            + ", eventType=" + event.getType()
-            + ", event=" + event);
-        throw e;
+        writeLock.lock();
+        try {
+          stateMachine.doTransition(event.getType(), event);
+          stateEntity.setCurrentState(stateMachine.getCurrentState());
+          saveIfPersisted();
+          // TODO Audit logs
+        } catch (InvalidStateTransitionException e) {
+          LOG.error("Can't handle ServiceComponentHostEvent event at"
+              + " current state"
+              + ", serviceComponentName=" + this.getServiceComponentName()
+              + ", hostName=" + this.getHostName()
+              + ", currentState=" + oldState
+              + ", eventType=" + event.getType()
+              + ", event=" + event);
+          throw e;
+        }
+      } finally {
+        writeLock.unlock();
       }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
-    finally {
-      writeLock.unlock();
-    }
+
     if (!oldState.equals(getState())) {
       if (LOG.isDebugEnabled()) {
         LOG.debug("ServiceComponentHost transitioned to a new state"
@@ -741,182 +768,276 @@ public class ServiceComponentHostImpl im
 
   @Override
   public String getServiceComponentName() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return serviceComponent.getName();
+      readLock.lock();
+      try {
+        return serviceComponent.getName();
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
+
   }
 
   @Override
   public String getHostName() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return host.getHostName();
+      readLock.lock();
+      try {
+        return host.getHostName();
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
+
   }
 
   /**
    * @return the lastOpStartTime
    */
   public long getLastOpStartTime() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return lastOpStartTime;
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return lastOpStartTime;
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   /**
    * @param lastOpStartTime the lastOpStartTime to set
    */
   public void setLastOpStartTime(long lastOpStartTime) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      this.lastOpStartTime = lastOpStartTime;
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        this.lastOpStartTime = lastOpStartTime;
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   /**
    * @return the lastOpEndTime
    */
   public long getLastOpEndTime() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return lastOpEndTime;
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return lastOpEndTime;
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   /**
    * @param lastOpEndTime the lastOpEndTime to set
    */
   public void setLastOpEndTime(long lastOpEndTime) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      this.lastOpEndTime = lastOpEndTime;
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        this.lastOpEndTime = lastOpEndTime;
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   /**
    * @return the lastOpLastUpdateTime
    */
   public long getLastOpLastUpdateTime() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return lastOpLastUpdateTime;
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return lastOpLastUpdateTime;
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   /**
    * @param lastOpLastUpdateTime the lastOpLastUpdateTime to set
    */
   public void setLastOpLastUpdateTime(long lastOpLastUpdateTime) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      this.lastOpLastUpdateTime = lastOpLastUpdateTime;
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        this.lastOpLastUpdateTime = lastOpLastUpdateTime;
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public long getClusterId() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return serviceComponent.getClusterId();
+      readLock.lock();
+      try {
+        return serviceComponent.getClusterId();
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
+
   }
 
   @Override
   public String getServiceName() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return serviceComponent.getServiceName();
+      readLock.lock();
+      try {
+        return serviceComponent.getServiceName();
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
+
   }
 
   Map<String, String> getConfigVersions() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      if (this.configs != null) {
-        return Collections.unmodifiableMap(configs);
-      } else {
-        return new HashMap<String, String>();
+      readLock.lock();
+      try {
+        if (this.configs != null) {
+          return Collections.unmodifiableMap(configs);
+        } else {
+          return new HashMap<String, String>();
+        }
+      } finally {
+        readLock.unlock();
       }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
-    finally {
-      readLock.unlock();
-    }
+
 
   }
 
   @Override
   public Map<String, Config> getConfigs() throws AmbariException {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      Map<String, Config> map = new HashMap<String, Config>();
-      Cluster cluster = clusters.getClusterById(getClusterId());
-      for (Entry<String, String> entry : configs.entrySet()) {
-        Config config = cluster.getConfig(
-            entry.getKey(), entry.getValue());
-        if (null != config) {
-          map.put(entry.getKey(), config);
+      readLock.lock();
+      try {
+        Map<String, Config> map = new HashMap<String, Config>();
+        Cluster cluster = clusters.getClusterById(getClusterId());
+        for (Entry<String, String> entry : configs.entrySet()) {
+          Config config = cluster.getConfig(
+              entry.getKey(), entry.getValue());
+          if (null != config) {
+            map.put(entry.getKey(), config);
+          }
         }
+        return map;
+      } finally {
+        readLock.unlock();
       }
-      return map;
-    }
-    finally {
-      readLock.unlock();
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Transactional
   void setConfigs(Map<String, String> configs) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
+      writeLock.lock();
+      try {
 
-      Set<String> deletedTypes = new HashSet<String>();
-      for (String type : this.configs.keySet()) {
-        if (!configs.containsKey(type)) {
-          deletedTypes.add(type);
+        Set<String> deletedTypes = new HashSet<String>();
+        for (String type : this.configs.keySet()) {
+          if (!configs.containsKey(type)) {
+            deletedTypes.add(type);
+          }
         }
-      }
 
-      long now = Long.valueOf(new java.util.Date().getTime());
+        long now = Long.valueOf(new Date().getTime());
+
+        for (Entry<String, String> entry : configs.entrySet()) {
 
-      for (Entry<String,String> entry : configs.entrySet()) {
+          boolean contains = false;
+          for (HostComponentConfigMappingEntity mappingEntity : stateEntity.getHostComponentConfigMappingEntities()) {
+            if (entry.getKey().equals(mappingEntity.getConfigType())) {
+              if (LOG.isDebugEnabled()) {
+                LOG.debug("Updating live config to ServiceComponentHost"
+                    + ", clusterId=" + stateEntity.getClusterId()
+                    + ", serviceName=" + stateEntity.getServiceName()
+                    + ", componentName=" + stateEntity.getComponentName()
+                    + ", hostname=" + stateEntity.getHostName()
+                    + ", configType=" + entry.getKey()
+                    + ", configVersionTag=" + entry.getValue());
+              }
+              contains = true;
+              mappingEntity.setVersionTag(entry.getValue());
+              mappingEntity.setTimestamp(now);
+              hostComponentConfigMappingDAO.merge(mappingEntity);
+              break;
+            }
+          }
+
+          if (!contains) {
+            HostComponentConfigMappingEntity newEntity =
+                new HostComponentConfigMappingEntity();
+            newEntity.setClusterId(stateEntity.getClusterId());
+            newEntity.setServiceName(stateEntity.getServiceName());
+            newEntity.setComponentName(stateEntity.getComponentName());
+            newEntity.setHostName(stateEntity.getHostName());
+            newEntity.setConfigType(entry.getKey());
+            newEntity.setVersionTag(entry.getValue());
+            newEntity.setTimestamp(now);
 
-        boolean contains = false;
-        for (HostComponentConfigMappingEntity mappingEntity : stateEntity.getHostComponentConfigMappingEntities()) {
-          if (entry.getKey().equals(mappingEntity.getConfigType())) {
             if (LOG.isDebugEnabled()) {
-              LOG.debug("Updating live config to ServiceComponentHost"
+              LOG.debug("Adding new live config to ServiceComponentHost"
                   + ", clusterId=" + stateEntity.getClusterId()
                   + ", serviceName=" + stateEntity.getServiceName()
                   + ", componentName=" + stateEntity.getComponentName()
@@ -924,312 +1045,359 @@ public class ServiceComponentHostImpl im
                   + ", configType=" + entry.getKey()
                   + ", configVersionTag=" + entry.getValue());
             }
-            contains = true;
-            mappingEntity.setVersionTag(entry.getValue());
-            mappingEntity.setTimestamp(now);
-            hostComponentConfigMappingDAO.merge(mappingEntity);
-            break;
+            stateEntity.getHostComponentConfigMappingEntities().add(newEntity);
+            newEntity.setHostComponentStateEntity(stateEntity);
+            hostComponentConfigMappingDAO.create(newEntity);
           }
         }
 
-        if (!contains) {
-          HostComponentConfigMappingEntity newEntity =
-              new HostComponentConfigMappingEntity();
-          newEntity.setClusterId(stateEntity.getClusterId());
-          newEntity.setServiceName(stateEntity.getServiceName());
-          newEntity.setComponentName(stateEntity.getComponentName());
-          newEntity.setHostName(stateEntity.getHostName());
-          newEntity.setConfigType(entry.getKey());
-          newEntity.setVersionTag(entry.getValue());
-          newEntity.setTimestamp(now);
-
-          if (LOG.isDebugEnabled()) {
-            LOG.debug("Adding new live config to ServiceComponentHost"
-                + ", clusterId=" + stateEntity.getClusterId()
-                + ", serviceName=" + stateEntity.getServiceName()
-                + ", componentName=" + stateEntity.getComponentName()
-                + ", hostname=" + stateEntity.getHostName()
-                + ", configType=" + entry.getKey()
-                + ", configVersionTag=" + entry.getValue());
-          }
-          stateEntity.getHostComponentConfigMappingEntities().add(newEntity);
-          newEntity.setHostComponentStateEntity(stateEntity);
-          hostComponentConfigMappingDAO.create(newEntity);
-        }
-      }
-
-      if (!deletedTypes.isEmpty()) {
-        List<HostComponentConfigMappingEntity> deleteEntities =
-            hostComponentConfigMappingDAO.findByHostComponentAndType(
-                stateEntity.getClusterId(), stateEntity.getServiceName(),
-                stateEntity.getComponentName(),
-                stateEntity.getHostName(), deletedTypes);
-        for (HostComponentConfigMappingEntity deleteEntity : deleteEntities) {
-          if (LOG.isDebugEnabled()) {
-            LOG.debug("Deleting live config to ServiceComponentHost"
-                + ", clusterId="  + stateEntity.getClusterId()
-                + ", serviceName=" + stateEntity.getServiceName()
-                + ", componentName=" + stateEntity.getComponentName()
-                + ", hostname=" + stateEntity.getHostName()
-                + ", configType=" + deleteEntity.getConfigType()
-                + ", configVersionTag=" + deleteEntity.getVersionTag());
-          }
-          stateEntity.getHostComponentConfigMappingEntities().remove(
-              deleteEntity);
-          if (persisted) {
-            hostComponentConfigMappingDAO.remove(deleteEntity);
+        if (!deletedTypes.isEmpty()) {
+          List<HostComponentConfigMappingEntity> deleteEntities =
+              hostComponentConfigMappingDAO.findByHostComponentAndType(
+                  stateEntity.getClusterId(), stateEntity.getServiceName(),
+                  stateEntity.getComponentName(),
+                  stateEntity.getHostName(), deletedTypes);
+          for (HostComponentConfigMappingEntity deleteEntity : deleteEntities) {
+            if (LOG.isDebugEnabled()) {
+              LOG.debug("Deleting live config to ServiceComponentHost"
+                  + ", clusterId=" + stateEntity.getClusterId()
+                  + ", serviceName=" + stateEntity.getServiceName()
+                  + ", componentName=" + stateEntity.getComponentName()
+                  + ", hostname=" + stateEntity.getHostName()
+                  + ", configType=" + deleteEntity.getConfigType()
+                  + ", configVersionTag=" + deleteEntity.getVersionTag());
+            }
+            stateEntity.getHostComponentConfigMappingEntities().remove(
+                deleteEntity);
+            if (persisted) {
+              hostComponentConfigMappingDAO.remove(deleteEntity);
+            }
           }
         }
+        this.configs = configs;
+        saveIfPersisted();
+      } finally {
+        writeLock.unlock();
       }
-      this.configs = configs;
-      saveIfPersisted();
     } finally {
-      writeLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public StackId getStackVersion() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return gson.fromJson(stateEntity.getCurrentStackVersion(), StackId.class);
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return gson.fromJson(stateEntity.getCurrentStackVersion(), StackId.class);
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void setStackVersion(StackId stackVersion) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      stateEntity.setCurrentStackVersion(gson.toJson(stackVersion));
-      saveIfPersisted();
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        stateEntity.setCurrentStackVersion(gson.toJson(stackVersion));
+        saveIfPersisted();
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
 
   @Override
   public State getDesiredState() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return desiredStateEntity.getDesiredState();
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return desiredStateEntity.getDesiredState();
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void setDesiredState(State state) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      desiredStateEntity.setDesiredState(state);
-      saveIfPersisted();
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        desiredStateEntity.setDesiredState(state);
+        saveIfPersisted();
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public Map<String, String> getDesiredConfigVersionsRecursive() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      Map<String, String> fullDesiredConfigVersions =
-          new HashMap<String, String>();
-      Map<String, Config> desiredConfs = getDesiredConfigs();
-      for (Config c : desiredConfs.values()) {
-        fullDesiredConfigVersions.put(c.getType(), c.getVersionTag());
+      readLock.lock();
+      try {
+        Map<String, String> fullDesiredConfigVersions =
+            new HashMap<String, String>();
+        Map<String, Config> desiredConfs = getDesiredConfigs();
+        for (Config c : desiredConfs.values()) {
+          fullDesiredConfigVersions.put(c.getType(), c.getVersionTag());
+        }
+        return fullDesiredConfigVersions;
+      } finally {
+        readLock.unlock();
       }
-      return fullDesiredConfigVersions;
-    }
-    finally {
-      readLock.unlock();
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
 
   @Override
   public Map<String, Config> getDesiredConfigs() {
-    Map<String, Config> map = new HashMap<String, Config>();
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      for (Entry<String, String> entry : desiredConfigs.entrySet()) {
-        Config config = clusters.getClusterById(getClusterId()).getConfig(
-            entry.getKey(), entry.getValue());
-        if (null != config) {
-          map.put(entry.getKey(), config);
+      Map<String, Config> map = new HashMap<String, Config>();
+      readLock.lock();
+      try {
+        for (Entry<String, String> entry : desiredConfigs.entrySet()) {
+          Config config = clusters.getClusterById(getClusterId()).getConfig(
+              entry.getKey(), entry.getValue());
+          if (null != config) {
+            map.put(entry.getKey(), config);
+          }
         }
+      } catch (AmbariException e) {
+        // TODO do something
+        return null;
+      } finally {
+        readLock.unlock();
       }
-    }
-    catch (AmbariException e) {
-      // TODO do something
-      return null;
-    }
-    finally {
-      readLock.unlock();
-    }
-    // do a union with component level configs
-    Map<String, Config> compConfigs = serviceComponent.getDesiredConfigs();
-    for (Entry<String, Config> entry : compConfigs.entrySet()) {
-      if (!map.containsKey(entry.getKey())) {
-        map.put(entry.getKey(), entry.getValue());
+      // do a union with component level configs
+      Map<String, Config> compConfigs = serviceComponent.getDesiredConfigs();
+      for (Entry<String, Config> entry : compConfigs.entrySet()) {
+        if (!map.containsKey(entry.getKey())) {
+          map.put(entry.getKey(), entry.getValue());
+        }
       }
+      return Collections.unmodifiableMap(map);
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
-    return Collections.unmodifiableMap(map);
+
   }
 
   @Override
   @Transactional
   public void updateDesiredConfigs(Map<String, Config> configs) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
+      writeLock.lock();
+      try {
 
-      for (Entry<String,Config> entry : configs.entrySet()) {
+        for (Entry<String, Config> entry : configs.entrySet()) {
 
-        boolean contains = false;
-        for (HostComponentDesiredConfigMappingEntity desiredConfigMappingEntity : desiredStateEntity.getHostComponentDesiredConfigMappingEntities()) {
-          if (entry.getKey().equals(desiredConfigMappingEntity.getConfigType())) {
-            contains = true;
-            desiredConfigMappingEntity.setVersionTag(entry.getValue().getVersionTag());
-            desiredConfigMappingEntity.setTimestamp(new Date().getTime());
-            hostComponentDesiredConfigMappingDAO.merge(desiredConfigMappingEntity);
-            break;
+          boolean contains = false;
+          for (HostComponentDesiredConfigMappingEntity desiredConfigMappingEntity : desiredStateEntity.getHostComponentDesiredConfigMappingEntities()) {
+            if (entry.getKey().equals(desiredConfigMappingEntity.getConfigType())) {
+              contains = true;
+              desiredConfigMappingEntity.setVersionTag(entry.getValue().getVersionTag());
+              desiredConfigMappingEntity.setTimestamp(new Date().getTime());
+              hostComponentDesiredConfigMappingDAO.merge(desiredConfigMappingEntity);
+              break;
+            }
+          }
+
+          if (!contains) {
+            HostComponentDesiredConfigMappingEntity newEntity = new HostComponentDesiredConfigMappingEntity();
+            newEntity.setClusterId(desiredStateEntity.getClusterId());
+            newEntity.setServiceName(desiredStateEntity.getServiceName());
+            newEntity.setComponentName(desiredStateEntity.getComponentName());
+            newEntity.setHostName(desiredStateEntity.getHostName());
+            newEntity.setConfigType(entry.getKey());
+            newEntity.setVersionTag(entry.getValue().getVersionTag());
+            newEntity.setTimestamp(new Date().getTime());
+            newEntity.setHostComponentDesiredStateEntity(desiredStateEntity);
+            desiredStateEntity.getHostComponentDesiredConfigMappingEntities().add(newEntity);
+            hostComponentDesiredConfigMappingDAO.create(newEntity);
           }
-        }
 
-        if (!contains) {
-          HostComponentDesiredConfigMappingEntity newEntity = new HostComponentDesiredConfigMappingEntity();
-          newEntity.setClusterId(desiredStateEntity.getClusterId());
-          newEntity.setServiceName(desiredStateEntity.getServiceName());
-          newEntity.setComponentName(desiredStateEntity.getComponentName());
-          newEntity.setHostName(desiredStateEntity.getHostName());
-          newEntity.setConfigType(entry.getKey());
-          newEntity.setVersionTag(entry.getValue().getVersionTag());
-          newEntity.setTimestamp(new Date().getTime());
-          newEntity.setHostComponentDesiredStateEntity(desiredStateEntity);
-          desiredStateEntity.getHostComponentDesiredConfigMappingEntities().add(newEntity);
-          hostComponentDesiredConfigMappingDAO.create(newEntity);
+          this.desiredConfigs.put(entry.getKey(), entry.getValue().getVersionTag());
         }
 
-        this.desiredConfigs.put(entry.getKey(), entry.getValue().getVersionTag());
+        saveIfPersisted();
+      } finally {
+        writeLock.unlock();
       }
-
-      saveIfPersisted();
-    }
-    finally {
-      writeLock.unlock();
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public StackId getDesiredStackVersion() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return gson.fromJson(desiredStateEntity.getDesiredStackVersion(), StackId.class);
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        return gson.fromJson(desiredStateEntity.getDesiredStackVersion(), StackId.class);
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void setDesiredStackVersion(StackId stackVersion) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      desiredStateEntity.setDesiredStackVersion(gson.toJson(stackVersion));
-      saveIfPersisted();
-    }
-    finally {
-      writeLock.unlock();
+      writeLock.lock();
+      try {
+        desiredStateEntity.setDesiredStackVersion(gson.toJson(stackVersion));
+        saveIfPersisted();
+      } finally {
+        writeLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public ServiceComponentHostResponse convertToResponse() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      ServiceComponentHostResponse r = new ServiceComponentHostResponse(
-          serviceComponent.getClusterName(),
-          serviceComponent.getServiceName(),
-          serviceComponent.getName(),
-          getHostName(),
-          configs,
-          desiredConfigs,
-          getState().toString(),
-          getStackVersion().getStackId(),
-          getDesiredState().toString(),
-          getDesiredStackVersion().getStackId());
-      
-      r.setHa_status(ha_status);
-      r.setActualConfigs(actualConfigs);
-      return r;
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        ServiceComponentHostResponse r = new ServiceComponentHostResponse(
+            serviceComponent.getClusterName(),
+            serviceComponent.getServiceName(),
+            serviceComponent.getName(),
+            getHostName(),
+            configs,
+            desiredConfigs,
+            getState().toString(),
+            getStackVersion().getStackId(),
+            getDesiredState().toString(),
+            getDesiredStackVersion().getStackId());
+
+        r.setHa_status(ha_status);
+        r.setActualConfigs(actualConfigs);
+        return r;
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public String getClusterName() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return serviceComponent.getClusterName();
+      readLock.lock();
+      try {
+        return serviceComponent.getClusterName();
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
   }
 
   @Override
   public void debugDump(StringBuilder sb) {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      sb.append("ServiceComponentHost={ hostname=" + getHostName()
-          + ", serviceComponentName=" + serviceComponent.getName()
-          + ", clusterName=" + serviceComponent.getClusterName()
-          + ", serviceName=" + serviceComponent.getServiceName()
-          + ", desiredStackVersion=" + getDesiredStackVersion()
-          + ", desiredState=" + getDesiredState()
-          + ", stackVersion=" + getStackVersion()
-          + ", state=" + getState()
-          + " }");
-    }
-    finally {
-      readLock.unlock();
+      readLock.lock();
+      try {
+        sb.append("ServiceComponentHost={ hostname=" + getHostName()
+            + ", serviceComponentName=" + serviceComponent.getName()
+            + ", clusterName=" + serviceComponent.getClusterName()
+            + ", serviceName=" + serviceComponent.getServiceName()
+            + ", desiredStackVersion=" + getDesiredStackVersion()
+            + ", desiredState=" + getDesiredState()
+            + ", stackVersion=" + getStackVersion()
+            + ", state=" + getState()
+            + " }");
+      } finally {
+        readLock.unlock();
+      }
+    } finally {
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public boolean isPersisted() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return persisted;
+      readLock.lock();
+      try {
+        return persisted;
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void persist() {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      if (!persisted) {
-        persistEntities();
-        refresh();
-        host.refresh();
-        serviceComponent.refresh();
-        persisted = true;
-      } else {
-        saveIfPersisted();
+      writeLock.lock();
+      try {
+        if (!persisted) {
+          persistEntities();
+          refresh();
+          host.refresh();
+          serviceComponent.refresh();
+          persisted = true;
+        } else {
+          saveIfPersisted();
+        }
+      } finally {
+        writeLock.unlock();
       }
     } finally {
-      writeLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Transactional
@@ -1261,26 +1429,31 @@ public class ServiceComponentHostImpl im
   @Override
   @Transactional
   public void refresh() {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      if (isPersisted()) {
-        HostComponentStateEntityPK pk = new HostComponentStateEntityPK();
-        HostComponentDesiredStateEntityPK dpk = new HostComponentDesiredStateEntityPK();
-        pk.setClusterId(getClusterId());
-        pk.setComponentName(getServiceComponentName());
-        pk.setServiceName(getServiceName());
-        pk.setHostName(getHostName());
-        dpk.setClusterId(getClusterId());
-        dpk.setComponentName(getServiceComponentName());
-        dpk.setServiceName(getServiceName());
-        dpk.setHostName(getHostName());
-        stateEntity = hostComponentStateDAO.findByPK(pk);
-        desiredStateEntity = hostComponentDesiredStateDAO.findByPK(dpk);
-        hostComponentStateDAO.refresh(stateEntity);
-        hostComponentDesiredStateDAO.refresh(desiredStateEntity);
+      writeLock.lock();
+      try {
+        if (isPersisted()) {
+          HostComponentStateEntityPK pk = new HostComponentStateEntityPK();
+          HostComponentDesiredStateEntityPK dpk = new HostComponentDesiredStateEntityPK();
+          pk.setClusterId(getClusterId());
+          pk.setComponentName(getServiceComponentName());
+          pk.setServiceName(getServiceName());
+          pk.setHostName(getHostName());
+          dpk.setClusterId(getClusterId());
+          dpk.setComponentName(getServiceComponentName());
+          dpk.setServiceName(getServiceName());
+          dpk.setHostName(getHostName());
+          stateEntity = hostComponentStateDAO.findByPK(pk);
+          desiredStateEntity = hostComponentDesiredStateDAO.findByPK(dpk);
+          hostComponentStateDAO.refresh(stateEntity);
+          hostComponentDesiredStateDAO.refresh(desiredStateEntity);
+        }
+      } finally {
+        writeLock.unlock();
       }
     } finally {
-      writeLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
   }
@@ -1295,41 +1468,58 @@ public class ServiceComponentHostImpl im
 
   @Override
   public boolean canBeRemoved() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
+      readLock.lock();
+      try {
 
-      return (getDesiredState().isRemovableState() &&
-              getState().isRemovableState());
+        return (getDesiredState().isRemovableState() &&
+            getState().isRemovableState());
 
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void deleteDesiredConfigs(Set<String> configTypes) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      hostComponentDesiredConfigMappingDAO.removeByType(configTypes);
-      for (String configType : configTypes) {
-        desiredConfigs.remove(configType);
+      writeLock.lock();
+      try {
+        hostComponentDesiredConfigMappingDAO.removeByType(configTypes);
+        for (String configType : configTypes) {
+          desiredConfigs.remove(configType);
+        }
+      } finally {
+        writeLock.unlock();
       }
     } finally {
-      writeLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
+
   }
 
   @Override
   public void delete() {
-    writeLock.lock();
+    clusterGlobalLock.writeLock().lock();
     try {
-      if (persisted) {
-        removeEntities();
-        persisted = false;
+      writeLock.lock();
+      try {
+        if (persisted) {
+          removeEntities();
+          persisted = false;
+        }
+        desiredConfigs.clear();
+      } finally {
+        writeLock.unlock();
       }
-      desiredConfigs.clear();
     } finally {
-      writeLock.unlock();
+      clusterGlobalLock.writeLock().unlock();
     }
 
   }
@@ -1355,52 +1545,67 @@ public class ServiceComponentHostImpl im
   
   @Override
   public void updateActualConfigs(Map<String, Map<String, String>> configTags) {
-    writeLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      actualConfigs = new HashMap<String, DesiredConfig>();
+      writeLock.lock();
+      try {
+        actualConfigs = new HashMap<String, DesiredConfig>();
 
-      String hostName = getHostName();
+        String hostName = getHostName();
 
-      for (Entry<String, Map<String, String>> entry : configTags.entrySet()) {
-        String type = entry.getKey();
-        Map<String, String> values = entry.getValue();
-
-        String tag = values.get("tag");
-        String hostTag = values.get("host_override_tag");
-
-        DesiredConfig dc = new DesiredConfig();
-        dc.setVersion(tag);
-        actualConfigs.put(type, dc);
-        if (null != hostTag && null != hostName) {
-          List<HostOverride> list = new ArrayList<HostOverride>();
-          list.add(new HostOverride(hostName, hostTag));
-          dc.setHostOverrides(list);
+        for (Entry<String, Map<String, String>> entry : configTags.entrySet()) {
+          String type = entry.getKey();
+          Map<String, String> values = entry.getValue();
+
+          String tag = values.get("tag");
+          String hostTag = values.get("host_override_tag");
+
+          DesiredConfig dc = new DesiredConfig();
+          dc.setVersion(tag);
+          actualConfigs.put(type, dc);
+          if (null != hostTag && null != hostName) {
+            List<HostOverride> list = new ArrayList<HostOverride>();
+            list.add(new HostOverride(hostName, hostTag));
+            dc.setHostOverrides(list);
+          }
         }
+      } finally {
+        writeLock.unlock();
       }
     } finally {
-      writeLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
   }
   
   @Override
   public Map<String, DesiredConfig> getActualConfigs() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return actualConfigs;
+      readLock.lock();
+      try {
+        return actualConfigs;
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
   }
 
   @Override
   public HostState getHostState() {
-    readLock.lock();
+    clusterGlobalLock.readLock().lock();
     try {
-      return host.getState();
+      readLock.lock();
+      try {
+        return host.getState();
+      } finally {
+        readLock.unlock();
+      }
     } finally {
-      readLock.unlock();
+      clusterGlobalLock.readLock().unlock();
     }
 
   }