You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2016/10/19 16:08:19 UTC
[06/32] ambari git commit: AMBARI-18495 - Remove Unnecessary Locks
Inside Of Cluster Business Object Implementations (jonathanhurley)
AMBARI-18495 - Remove Unnecessary Locks Inside Of Cluster Business Object Implementations (jonathanhurley)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d53c9e2e
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d53c9e2e
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d53c9e2e
Branch: refs/heads/trunk
Commit: d53c9e2ee99c046ae5b37b531419549397f4aea1
Parents: 817aed4
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Thu Sep 29 15:20:53 2016 -0400
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Mon Oct 3 13:03:59 2016 -0400
----------------------------------------------------------------------
ambari-project/pom.xml | 2 +-
.../ambari/server/controller/AmbariServer.java | 11 +-
.../internal/UpgradeResourceProvider.java | 2 +-
.../org/apache/ambari/server/state/Cluster.java | 5 -
.../apache/ambari/server/state/Clusters.java | 9 +
.../server/state/cluster/ClusterImpl.java | 1348 +++++++-----------
.../server/state/cluster/ClustersImpl.java | 629 ++++----
.../apache/ambari/server/utils/RetryHelper.java | 18 +-
.../ambari/server/agent/AgentResourceTest.java | 10 +-
.../server/agent/HeartbeatProcessorTest.java | 11 +-
.../server/agent/HeartbeatTestHelper.java | 15 +-
.../server/agent/TestHeartbeatHandler.java | 30 +-
.../server/api/services/ClusterServiceTest.java | 43 +-
.../configuration/RecoveryConfigHelperTest.java | 87 +-
.../server/controller/KerberosHelperTest.java | 78 +-
.../internal/ClusterResourceProviderTest.java | 7 +-
.../apache/ambari/server/orm/OrmTestHelper.java | 21 +-
.../server/orm/dao/ClusterVersionDAOTest.java | 12 +-
.../server/orm/dao/ConfigGroupDAOTest.java | 33 +-
.../orm/dao/RepositoryVersionDAOTest.java | 6 +-
.../ambari/server/orm/dao/SettingDAOTest.java | 19 +-
.../ambari/server/orm/dao/WidgetDAOTest.java | 8 +-
.../server/orm/dao/WidgetLayoutDAOTest.java | 13 +-
.../ambari/server/state/ConfigGroupTest.java | 8 +-
.../ambari/server/state/ConfigHelperTest.java | 9 +-
.../server/state/RequestExecutionTest.java | 9 +-
.../state/cluster/ClusterDeadlockTest.java | 35 +-
.../cluster/ClusterEffectiveVersionTest.java | 19 +-
.../server/state/cluster/ClusterTest.java | 34 +-
.../ambari/server/utils/StageUtilsTest.java | 3 +-
30 files changed, 1058 insertions(+), 1476 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/d53c9e2e/ambari-project/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-project/pom.xml b/ambari-project/pom.xml
index 4f045fe..3ef3086 100644
--- a/ambari-project/pom.xml
+++ b/ambari-project/pom.xml
@@ -114,7 +114,7 @@
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
- <version>10.9.1.0</version>
+ <version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
http://git-wip-us.apache.org/repos/asf/ambari/blob/d53c9e2e/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
index 5e498f0..a3c61df 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
@@ -25,17 +25,16 @@ import java.net.Authenticator;
import java.net.BindException;
import java.net.PasswordAuthentication;
import java.net.URL;
-import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Enumeration;
-import java.util.List;
import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.logging.LogManager;
import javax.crypto.BadPaddingException;
import javax.servlet.DispatcherType;
-import com.google.common.base.Joiner;
import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.StateRecoveryManager;
import org.apache.ambari.server.StaticallyInject;
@@ -149,6 +148,7 @@ import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
+import com.google.common.base.Joiner;
import com.google.common.util.concurrent.ServiceManager;
import com.google.gson.Gson;
import com.google.inject.Guice;
@@ -160,9 +160,6 @@ import com.google.inject.name.Named;
import com.google.inject.persist.Transactional;
import com.sun.jersey.spi.container.servlet.ServletContainer;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
@Singleton
public class AmbariServer {
@@ -880,7 +877,7 @@ public class AmbariServer {
BaseService.init(injector.getInstance(RequestAuditLogger.class));
- RetryHelper.init(configs.getOperationsRetryAttempts());
+ RetryHelper.init(injector.getInstance(Clusters.class), configs.getOperationsRetryAttempts());
}
/**
http://git-wip-us.apache.org/repos/asf/ambari/blob/d53c9e2e/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
index d37e32b..b3d23d9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/UpgradeResourceProvider.java
@@ -360,7 +360,7 @@ public class UpgradeResourceProvider extends AbstractControllerResourceProvider
// rollback
// As we operate inside with cluster data, any cache which belongs to
// cluster need to be flushed
- cluster.invalidateData();
+ clusters.get().invalidate(cluster);
throw e;
}
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/d53c9e2e/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
index d141df8..b1958ef 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
@@ -663,11 +663,6 @@ public interface Cluster {
void removeConfigurations(StackId stackId);
/**
- * Clear cluster caches and re-read data from database
- */
- void invalidateData();
-
- /**
* Returns whether this cluster was provisioned by a Blueprint or not.
* @return true if the cluster was deployed with a Blueprint otherwise false.
*/
http://git-wip-us.apache.org/repos/asf/ambari/blob/d53c9e2e/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
index 2d859b3..e2164c0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
@@ -278,4 +278,13 @@ public interface Clusters {
*/
int getClusterSize(String clusterName);
+ /**
+ * Invalidates the specified cluster by retrieving it from the database and
+ * refreshing all of the internal stateful collections.
+ *
+ * @param cluster
+ * the cluster to invalidate and refresh (not {@code null}).
+ */
+ void invalidate(Cluster cluster);
+
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/d53c9e2e/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index a6f0a3b..848036d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -30,8 +30,9 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
-import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
@@ -173,34 +174,32 @@ public class ClusterImpl implements Cluster {
private StackId desiredStackVersion;
- private volatile boolean desiredStackVersionSet = true;
-
- private volatile Map<String, Service> services = null;
+ private final ConcurrentSkipListMap<String, Service> services = new ConcurrentSkipListMap<>();
/**
* [ Config Type -> [ Config Version Tag -> Config ] ]
*/
- private volatile Map<String, Map<String, Config>> allConfigs;
+ private final ConcurrentMap<String, ConcurrentMap<String, Config>> allConfigs = new ConcurrentHashMap<>();
/**
* [ ServiceName -> [ ServiceComponentName -> [ HostName -> [ ... ] ] ] ]
*/
- private Map<String, Map<String, Map<String, ServiceComponentHost>>> serviceComponentHosts;
+ private final ConcurrentMap<String, ConcurrentMap<String, ConcurrentMap<String, ServiceComponentHost>>> serviceComponentHosts = new ConcurrentHashMap<>();
/**
* [ HostName -> [ ... ] ]
*/
- private Map<String, List<ServiceComponentHost>> serviceComponentHostsByHost;
+ private final ConcurrentMap<String, List<ServiceComponentHost>> serviceComponentHostsByHost = new ConcurrentHashMap<>();
/**
* Map of existing config groups
*/
- private volatile Map<Long, ConfigGroup> clusterConfigGroups;
+ private final Map<Long, ConfigGroup> clusterConfigGroups = new ConcurrentHashMap<>();
/**
* Map of Request schedules for this cluster
*/
- private volatile Map<Long, RequestExecution> requestExecutions;
+ private final Map<Long, RequestExecution> requestExecutions = new ConcurrentHashMap<>();
private final ReadWriteLock clusterGlobalLock = new ReentrantReadWriteLock();
@@ -287,8 +286,6 @@ public class ClusterImpl implements Cluster {
@Inject
private StackDAO stackDAO;
- private volatile boolean svcHostsLoaded = false;
-
private volatile Multimap<String, String> serviceConfigTypes;
/**
@@ -311,20 +308,21 @@ public class ClusterImpl implements Cluster {
private Map<String, String> m_clusterPropertyCache = new ConcurrentHashMap<>();
@Inject
- public ClusterImpl(@Assisted ClusterEntity clusterEntity,
- Injector injector, AmbariEventPublisher eventPublisher) throws AmbariException {
- injector.injectMembers(this);
+ public ClusterImpl(@Assisted ClusterEntity clusterEntity, Injector injector,
+ AmbariEventPublisher eventPublisher)
+ throws AmbariException {
clusterId = clusterEntity.getClusterId();
clusterName = clusterEntity.getClusterName();
- serviceComponentHosts = new HashMap<>();
-
- serviceComponentHostsByHost = new HashMap<>();
-
- desiredStackVersion = new StackId(clusterEntity.getDesiredStack());
+ injector.injectMembers(this);
+ loadStackVersion();
+ loadServices();
+ loadServiceHostComponents();
+ loadConfigGroups();
cacheConfigurations();
+ loadRequestExecutions();
if (desiredStackVersion != null && !StringUtils.isEmpty(desiredStackVersion.getStackName()) && !
StringUtils.isEmpty(desiredStackVersion.getStackVersion())) {
@@ -376,301 +374,195 @@ public class ClusterImpl implements Cluster {
* Make sure we load all the service host components.
* We need this for live status checks.
*/
- public void loadServiceHostComponents() {
- loadServices();
- if (svcHostsLoaded) {
- return;
- }
-
- clusterGlobalLock.writeLock().lock();
-
- try {
- LOG.info("Loading Service Host Components");
- if (svcHostsLoaded) {
- return;
- }
- if (services != null) {
- for (Entry<String, Service> serviceKV : services.entrySet()) {
- /* get all the service component hosts **/
- Service service = serviceKV.getValue();
- if (!serviceComponentHosts.containsKey(service.getName())) {
- serviceComponentHosts.put(service.getName(),
- new HashMap<String, Map<String, ServiceComponentHost>>());
+ private void loadServiceHostComponents() {
+ for (Entry<String, Service> serviceKV : services.entrySet()) {
+ /* get all the service component hosts **/
+ Service service = serviceKV.getValue();
+ if (!serviceComponentHosts.containsKey(service.getName())) {
+ serviceComponentHosts.put(service.getName(),
+ new ConcurrentHashMap<String, ConcurrentMap<String, ServiceComponentHost>>());
+ }
+
+ for (Entry<String, ServiceComponent> svcComponent : service.getServiceComponents().entrySet()) {
+ ServiceComponent comp = svcComponent.getValue();
+ String componentName = svcComponent.getKey();
+ if (!serviceComponentHosts.get(service.getName()).containsKey(componentName)) {
+ serviceComponentHosts.get(service.getName()).put(componentName,
+ new ConcurrentHashMap<String, ServiceComponentHost>());
+ }
+
+ /** Get Service Host Components **/
+ for (Entry<String, ServiceComponentHost> svchost : comp.getServiceComponentHosts().entrySet()) {
+ String hostname = svchost.getKey();
+ ServiceComponentHost svcHostComponent = svchost.getValue();
+ if (!serviceComponentHostsByHost.containsKey(hostname)) {
+ serviceComponentHostsByHost.put(hostname, new ArrayList<ServiceComponentHost>());
}
- for (Entry<String, ServiceComponent> svcComponent : service.getServiceComponents().entrySet()) {
- ServiceComponent comp = svcComponent.getValue();
- String componentName = svcComponent.getKey();
- if (!serviceComponentHosts.get(service.getName()).containsKey(
- componentName)) {
- serviceComponentHosts.get(service.getName()).put(componentName,
- new HashMap<String, ServiceComponentHost>());
- }
- /** Get Service Host Components **/
- for (Entry<String, ServiceComponentHost> svchost : comp.getServiceComponentHosts().entrySet()) {
- String hostname = svchost.getKey();
- ServiceComponentHost svcHostComponent = svchost.getValue();
- if (!serviceComponentHostsByHost.containsKey(hostname)) {
- serviceComponentHostsByHost.put(hostname,
- new ArrayList<ServiceComponentHost>());
- }
- List<ServiceComponentHost> compList = serviceComponentHostsByHost.get(hostname);
- compList.add(svcHostComponent);
- if (!serviceComponentHosts.get(service.getName()).get(
- componentName).containsKey(hostname)) {
- serviceComponentHosts.get(service.getName()).get(componentName).put(
- hostname, svcHostComponent);
- }
- }
+ List<ServiceComponentHost> compList = serviceComponentHostsByHost.get(hostname);
+ compList.add(svcHostComponent);
+
+ if (!serviceComponentHosts.get(service.getName()).get(componentName).containsKey(
+ hostname)) {
+ serviceComponentHosts.get(service.getName()).get(componentName).put(hostname,
+ svcHostComponent);
}
}
}
- svcHostsLoaded = true;
- } finally {
- clusterGlobalLock.writeLock().unlock();
}
}
private void loadServices() {
- if (services == null) {
- clusterGlobalLock.writeLock().lock();
+ ClusterEntity clusterEntity = getClusterEntity();
+ if (clusterEntity.getClusterServiceEntities().isEmpty()) {
+ return;
+ }
+ for (ClusterServiceEntity serviceEntity : clusterEntity.getClusterServiceEntities()) {
+ StackId stackId = getCurrentStackVersion();
try {
- if (services == null) {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- services = new TreeMap<String, Service>();
-
- if (!clusterEntity.getClusterServiceEntities().isEmpty()) {
- for (ClusterServiceEntity serviceEntity : clusterEntity.getClusterServiceEntities()) {
- StackId stackId = getCurrentStackVersion();
- try {
- if (ambariMetaInfo.getService(stackId.getStackName(),
- stackId.getStackVersion(), serviceEntity.getServiceName()) != null) {
- services.put(serviceEntity.getServiceName(),
- serviceFactory.createExisting(this, serviceEntity));
- }
- } catch (AmbariException e) {
- LOG.error(String.format(
- "Can not get service info: stackName=%s, stackVersion=%s, serviceName=%s",
- stackId.getStackName(), stackId.getStackVersion(),
- serviceEntity.getServiceName()));
- }
- }
- }
- }
+ if (ambariMetaInfo.getService(stackId.getStackName(),
+ stackId.getStackVersion(), serviceEntity.getServiceName()) != null) {
+ services.put(serviceEntity.getServiceName(),
+ serviceFactory.createExisting(this, serviceEntity));
}
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ } catch (AmbariException e) {
+ LOG.error(String.format(
+ "Can not get service info: stackName=%s, stackVersion=%s, serviceName=%s",
+ stackId.getStackName(), stackId.getStackVersion(),
+ serviceEntity.getServiceName()));
}
}
}
private void loadConfigGroups() {
- if (clusterConfigGroups == null) {
- clusterGlobalLock.writeLock().lock();
-
- try {
- if (clusterConfigGroups == null) {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- clusterConfigGroups = new HashMap<Long, ConfigGroup>();
- if (!clusterEntity.getConfigGroupEntities().isEmpty()) {
- for (ConfigGroupEntity configGroupEntity : clusterEntity.getConfigGroupEntities()) {
- clusterConfigGroups.put(configGroupEntity.getGroupId(),
- configGroupFactory.createExisting(this, configGroupEntity));
- }
- }
- }
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ ClusterEntity clusterEntity = getClusterEntity();
+ if (!clusterEntity.getConfigGroupEntities().isEmpty()) {
+ for (ConfigGroupEntity configGroupEntity : clusterEntity.getConfigGroupEntities()) {
+ clusterConfigGroups.put(configGroupEntity.getGroupId(),
+ configGroupFactory.createExisting(this, configGroupEntity));
}
}
}
private void loadRequestExecutions() {
- if (requestExecutions == null) {
- clusterGlobalLock.writeLock().lock();
- try {
- if (requestExecutions == null) {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- requestExecutions = new HashMap<Long, RequestExecution>();
- if (!clusterEntity.getRequestScheduleEntities().isEmpty()) {
- for (RequestScheduleEntity scheduleEntity : clusterEntity.getRequestScheduleEntities()) {
- requestExecutions.put(scheduleEntity.getScheduleId(),
- requestExecutionFactory.createExisting(this, scheduleEntity));
- }
- }
- }
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ ClusterEntity clusterEntity = getClusterEntity();
+ if (!clusterEntity.getRequestScheduleEntities().isEmpty()) {
+ for (RequestScheduleEntity scheduleEntity : clusterEntity.getRequestScheduleEntities()) {
+ requestExecutions.put(scheduleEntity.getScheduleId(),
+ requestExecutionFactory.createExisting(this, scheduleEntity));
}
}
}
@Override
public void addConfigGroup(ConfigGroup configGroup) throws AmbariException {
- loadConfigGroups();
- clusterGlobalLock.writeLock().lock();
- try {
- String hostList = "";
- if(LOG.isDebugEnabled()) {
- if (configGroup.getHosts() != null) {
- for (Host host : configGroup.getHosts().values()) {
- hostList += host.getHostName() + ", ";
- }
+ String hostList = "";
+ if(LOG.isDebugEnabled()) {
+ if (configGroup.getHosts() != null) {
+ for (Host host : configGroup.getHosts().values()) {
+ hostList += host.getHostName() + ", ";
}
}
+ }
- LOG.debug("Adding a new Config group" + ", clusterName = "
- + getClusterName() + ", groupName = " + configGroup.getName()
- + ", tag = " + configGroup.getTag() + " with hosts " + hostList);
-
- if (clusterConfigGroups.containsKey(configGroup.getId())) {
- // The loadConfigGroups will load all groups to memory
- LOG.debug("Config group already exists" + ", clusterName = "
- + getClusterName() + ", groupName = " + configGroup.getName()
- + ", groupId = " + configGroup.getId() + ", tag = "
- + configGroup.getTag());
- } else {
- clusterConfigGroups.put(configGroup.getId(), configGroup);
- }
+ LOG.debug("Adding a new Config group" + ", clusterName = "
+ + getClusterName() + ", groupName = " + configGroup.getName()
+ + ", tag = " + configGroup.getTag() + " with hosts " + hostList);
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ if (clusterConfigGroups.containsKey(configGroup.getId())) {
+ // The loadConfigGroups will load all groups to memory
+ LOG.debug("Config group already exists" + ", clusterName = "
+ + getClusterName() + ", groupName = " + configGroup.getName()
+ + ", groupId = " + configGroup.getId() + ", tag = "
+ + configGroup.getTag());
+ } else {
+ clusterConfigGroups.put(configGroup.getId(), configGroup);
}
}
@Override
public Map<Long, ConfigGroup> getConfigGroups() {
- loadConfigGroups();
- clusterGlobalLock.readLock().lock();
- try {
- return Collections.unmodifiableMap(clusterConfigGroups);
- } finally {
- clusterGlobalLock.readLock().unlock();
- }
+ return Collections.unmodifiableMap(clusterConfigGroups);
}
@Override
public Map<Long, ConfigGroup> getConfigGroupsByHostname(String hostname)
throws AmbariException {
- loadConfigGroups();
Map<Long, ConfigGroup> configGroups = new HashMap<Long, ConfigGroup>();
- clusterGlobalLock.readLock().lock();
- try {
- for (Entry<Long, ConfigGroup> groupEntry : clusterConfigGroups.entrySet()) {
- Long id = groupEntry.getKey();
- ConfigGroup group = groupEntry.getValue();
- for (Host host : group.getHosts().values()) {
- if (StringUtils.equals(hostname, host.getHostName())) {
- configGroups.put(id, group);
- break;
- }
+ for (Entry<Long, ConfigGroup> groupEntry : clusterConfigGroups.entrySet()) {
+ Long id = groupEntry.getKey();
+ ConfigGroup group = groupEntry.getValue();
+ for (Host host : group.getHosts().values()) {
+ if (StringUtils.equals(hostname, host.getHostName())) {
+ configGroups.put(id, group);
+ break;
}
}
- } finally {
- clusterGlobalLock.readLock().unlock();
}
return configGroups;
}
@Override
public void addRequestExecution(RequestExecution requestExecution) throws AmbariException {
- loadRequestExecutions();
- clusterGlobalLock.writeLock().lock();
- try {
- LOG.info("Adding a new request schedule" + ", clusterName = "
- + getClusterName() + ", id = " + requestExecution.getId()
- + ", description = " + requestExecution.getDescription());
-
- if (requestExecutions.containsKey(requestExecution.getId())) {
- LOG.debug("Request schedule already exists" + ", clusterName = "
- + getClusterName() + ", id = " + requestExecution.getId()
- + ", description = " + requestExecution.getDescription());
- } else {
- requestExecutions.put(requestExecution.getId(), requestExecution);
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ LOG.info("Adding a new request schedule" + ", clusterName = " + getClusterName() + ", id = "
+ + requestExecution.getId() + ", description = " + requestExecution.getDescription());
+
+ if (requestExecutions.containsKey(requestExecution.getId())) {
+ LOG.debug(
+ "Request schedule already exists" + ", clusterName = " + getClusterName() + ", id = "
+ + requestExecution.getId() + ", description = " + requestExecution.getDescription());
+ } else {
+ requestExecutions.put(requestExecution.getId(), requestExecution);
}
}
@Override
public Map<Long, RequestExecution> getAllRequestExecutions() {
- loadRequestExecutions();
- clusterGlobalLock.readLock().lock();
- try {
- return Collections.unmodifiableMap(requestExecutions);
- } finally {
- clusterGlobalLock.readLock().unlock();
- }
+ return Collections.unmodifiableMap(requestExecutions);
}
@Override
public void deleteRequestExecution(Long id) throws AmbariException {
- loadRequestExecutions();
- clusterGlobalLock.writeLock().lock();
- try {
- RequestExecution requestExecution = requestExecutions.get(id);
- if (requestExecution == null) {
- throw new AmbariException("Request schedule does not exists, "
- + "id = " + id);
- }
- LOG.info("Deleting request schedule" + ", clusterName = "
- + getClusterName() + ", id = " + requestExecution.getId()
- + ", description = " + requestExecution.getDescription());
-
- requestExecution.delete();
- requestExecutions.remove(id);
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ RequestExecution requestExecution = requestExecutions.get(id);
+ if (requestExecution == null) {
+ throw new AmbariException("Request schedule does not exists, " + "id = " + id);
}
+ LOG.info("Deleting request schedule" + ", clusterName = " + getClusterName() + ", id = "
+ + requestExecution.getId() + ", description = " + requestExecution.getDescription());
+
+ requestExecution.delete();
+ requestExecutions.remove(id);
}
@Override
public void deleteConfigGroup(Long id) throws AmbariException, AuthorizationException {
- loadConfigGroups();
- clusterGlobalLock.writeLock().lock();
- try {
- ConfigGroup configGroup = clusterConfigGroups.get(id);
- if (configGroup == null) {
- throw new ConfigGroupNotFoundException(getClusterName(), id.toString());
- }
+ ConfigGroup configGroup = clusterConfigGroups.get(id);
+ if (configGroup == null) {
+ throw new ConfigGroupNotFoundException(getClusterName(), id.toString());
+ }
- LOG.debug("Deleting Config group" + ", clusterName = " + getClusterName()
- + ", groupName = " + configGroup.getName() + ", groupId = "
- + configGroup.getId() + ", tag = " + configGroup.getTag());
+ LOG.debug("Deleting Config group" + ", clusterName = " + getClusterName()
+ + ", groupName = " + configGroup.getName() + ", groupId = "
+ + configGroup.getId() + ", tag = " + configGroup.getTag());
- configGroup.delete();
- clusterConfigGroups.remove(id);
- } finally {
- clusterGlobalLock.writeLock().unlock();
- }
+ configGroup.delete();
+ clusterConfigGroups.remove(id);
}
public ServiceComponentHost getServiceComponentHost(String serviceName,
String serviceComponentName, String hostname) throws AmbariException {
- loadServiceHostComponents();
- clusterGlobalLock.readLock().lock();
- try {
- if (!serviceComponentHosts.containsKey(serviceName)
- || !serviceComponentHosts.get(serviceName).containsKey(
- serviceComponentName)
- || !serviceComponentHosts.get(serviceName).get(serviceComponentName).containsKey(
- hostname)) {
- throw new ServiceComponentHostNotFoundException(getClusterName(),
- serviceName, serviceComponentName, hostname);
- }
- return serviceComponentHosts.get(serviceName).get(serviceComponentName).get(
- hostname);
- } finally {
- clusterGlobalLock.readLock().unlock();
+ if (!serviceComponentHosts.containsKey(serviceName)
+ || !serviceComponentHosts.get(serviceName).containsKey(
+ serviceComponentName)
+ || !serviceComponentHosts.get(serviceName).get(serviceComponentName).containsKey(
+ hostname)) {
+ throw new ServiceComponentHostNotFoundException(getClusterName(),
+ serviceName, serviceComponentName, hostname);
}
+ return serviceComponentHosts.get(serviceName).get(serviceComponentName).get(
+ hostname);
}
@Override
@@ -681,21 +573,14 @@ public class ClusterImpl implements Cluster {
@Override
public void setClusterName(String clusterName) {
String oldName = null;
- clusterGlobalLock.writeLock().lock();
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- oldName = clusterEntity.getClusterName();
- clusterEntity.setClusterName(clusterName);
+ ClusterEntity clusterEntity = getClusterEntity();
+ oldName = clusterEntity.getClusterName();
+ clusterEntity.setClusterName(clusterName);
- // RollbackException possibility if UNIQUE constraint violated
- clusterEntity = clusterDAO.merge(clusterEntity);
- clusters.updateClusterName(oldName, clusterName);
- this.clusterName = clusterName;
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
- }
+ // RollbackException possibility if UNIQUE constraint violated
+ clusterEntity = clusterDAO.merge(clusterEntity);
+ clusters.updateClusterName(oldName, clusterName);
+ this.clusterName = clusterName;
// if the name changed, fire an event
if (!StringUtils.equals(oldName, clusterName)) {
@@ -707,32 +592,26 @@ public class ClusterImpl implements Cluster {
@Override
public Long getResourceId() {
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- ResourceEntity resourceEntity = clusterEntity.getResource();
- if (resourceEntity == null) {
- LOG.warn("There is no resource associated with this cluster:\n\tCluster Name: {}\n\tCluster ID: {}",
+
+ ResourceEntity resourceEntity = clusterEntity.getResource();
+ if (resourceEntity == null) {
+ LOG.warn(
+ "There is no resource associated with this cluster:\n\tCluster Name: {}\n\tCluster ID: {}",
getClusterName(), getClusterId());
- return null;
- } else {
- return resourceEntity.getId();
- }
+ return null;
+ } else {
+ return resourceEntity.getId();
}
- return null;
}
@Override
public void addServiceComponentHosts(Collection<ServiceComponentHost> serviceComponentHosts) throws AmbariException {
- clusterGlobalLock.writeLock().lock();
- try {
- for (ServiceComponentHost serviceComponentHost : serviceComponentHosts) {
- Service service = getService(serviceComponentHost.getServiceName());
- ServiceComponent serviceComponent = service.getServiceComponent(serviceComponentHost.getServiceComponentName());
- serviceComponent.addServiceComponentHost(serviceComponentHost);
- }
- persistServiceComponentHosts(serviceComponentHosts);
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ for (ServiceComponentHost serviceComponentHost : serviceComponentHosts) {
+ Service service = getService(serviceComponentHost.getServiceName());
+ ServiceComponent serviceComponent = service.getServiceComponent(serviceComponentHost.getServiceComponentName());
+ serviceComponent.addServiceComponentHost(serviceComponentHost);
}
+ persistServiceComponentHosts(serviceComponentHosts);
}
@Transactional
@@ -750,68 +629,61 @@ public class ClusterImpl implements Cluster {
svcCompHost.getHostName());
}
- loadServiceHostComponents();
-
final String hostname = svcCompHost.getHostName();
final String serviceName = svcCompHost.getServiceName();
final String componentName = svcCompHost.getServiceComponentName();
Set<Cluster> cs = clusters.getClustersForHost(hostname);
- clusterGlobalLock.writeLock().lock();
-
- try {
- boolean clusterFound = false;
- Iterator<Cluster> iter = cs.iterator();
- while (iter.hasNext()) {
- Cluster c = iter.next();
- if (c.getClusterId() == getClusterId()) {
- clusterFound = true;
- break;
- }
- }
-
- if (!clusterFound) {
- throw new AmbariException("Host does not belong this cluster"
- + ", hostname=" + hostname + ", clusterName=" + getClusterName()
- + ", clusterId=" + getClusterId());
+ boolean clusterFound = false;
+ Iterator<Cluster> iter = cs.iterator();
+ while (iter.hasNext()) {
+ Cluster c = iter.next();
+ if (c.getClusterId() == getClusterId()) {
+ clusterFound = true;
+ break;
}
+ }
- if (!serviceComponentHosts.containsKey(serviceName)) {
- serviceComponentHosts.put(serviceName,
- new HashMap<String, Map<String, ServiceComponentHost>>());
- }
+ if (!clusterFound) {
+ throw new AmbariException("Host does not belong this cluster"
+ + ", hostname=" + hostname + ", clusterName=" + getClusterName()
+ + ", clusterId=" + getClusterId());
+ }
- if (!serviceComponentHosts.get(serviceName).containsKey(componentName)) {
- serviceComponentHosts.get(serviceName).put(componentName,
- new HashMap<String, ServiceComponentHost>());
- }
+ if (!serviceComponentHosts.containsKey(serviceName)) {
+ serviceComponentHosts.put(serviceName,
+ new ConcurrentHashMap<String, ConcurrentMap<String, ServiceComponentHost>>());
+ }
- if (serviceComponentHosts.get(serviceName).get(componentName).containsKey(
- hostname)) {
- throw new AmbariException("Duplicate entry for ServiceComponentHost"
- + ", serviceName=" + serviceName + ", serviceComponentName"
- + componentName + ", hostname= " + hostname);
- }
+ if (!serviceComponentHosts.get(serviceName).containsKey(componentName)) {
+ serviceComponentHosts.get(serviceName).put(componentName,
+ new ConcurrentHashMap<String, ServiceComponentHost>());
+ }
- if (!serviceComponentHostsByHost.containsKey(hostname)) {
- serviceComponentHostsByHost.put(hostname,
- new ArrayList<ServiceComponentHost>());
- }
+ if (serviceComponentHosts.get(serviceName).get(componentName).containsKey(
+ hostname)) {
+ throw new AmbariException("Duplicate entry for ServiceComponentHost"
+ + ", serviceName=" + serviceName + ", serviceComponentName"
+ + componentName + ", hostname= " + hostname);
+ }
- if (LOG.isDebugEnabled()) {
- LOG.debug("Adding a new ServiceComponentHost" + ", clusterName="
- + getClusterName() + ", clusterId=" + getClusterId()
- + ", serviceName=" + serviceName + ", serviceComponentName"
- + componentName + ", hostname= " + hostname);
- }
+ if (!serviceComponentHostsByHost.containsKey(hostname)) {
+ serviceComponentHostsByHost.put(hostname,
+ new ArrayList<ServiceComponentHost>());
+ }
- serviceComponentHosts.get(serviceName).get(componentName).put(hostname,
- svcCompHost);
- serviceComponentHostsByHost.get(hostname).add(svcCompHost);
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Adding a new ServiceComponentHost" + ", clusterName="
+ + getClusterName() + ", clusterId=" + getClusterId()
+ + ", serviceName=" + serviceName + ", serviceComponentName"
+ + componentName + ", hostname= " + hostname);
}
+
+ serviceComponentHosts.get(serviceName).get(componentName).put(hostname,
+ svcCompHost);
+
+ serviceComponentHostsByHost.get(hostname).add(svcCompHost);
}
@Override
@@ -824,76 +696,69 @@ public class ClusterImpl implements Cluster {
svcCompHost.getHostName());
}
- loadServiceHostComponents();
-
final String hostname = svcCompHost.getHostName();
final String serviceName = svcCompHost.getServiceName();
final String componentName = svcCompHost.getServiceComponentName();
Set<Cluster> cs = clusters.getClustersForHost(hostname);
- clusterGlobalLock.writeLock().lock();
- try {
- boolean clusterFound = false;
- Iterator<Cluster> iter = cs.iterator();
- while (iter.hasNext()) {
- Cluster c = iter.next();
- if (c.getClusterId() == getClusterId()) {
- clusterFound = true;
- break;
- }
+ boolean clusterFound = false;
+ Iterator<Cluster> iter = cs.iterator();
+ while (iter.hasNext()) {
+ Cluster c = iter.next();
+ if (c.getClusterId() == getClusterId()) {
+ clusterFound = true;
+ break;
}
+ }
- if (!clusterFound) {
- throw new AmbariException("Host does not belong this cluster"
- + ", hostname=" + hostname + ", clusterName=" + getClusterName()
- + ", clusterId=" + getClusterId());
- }
+ if (!clusterFound) {
+ throw new AmbariException("Host does not belong this cluster"
+ + ", hostname=" + hostname + ", clusterName=" + getClusterName()
+ + ", clusterId=" + getClusterId());
+ }
- if (!serviceComponentHosts.containsKey(serviceName)
- || !serviceComponentHosts.get(serviceName).containsKey(componentName)
- || !serviceComponentHosts.get(serviceName).get(componentName).containsKey(
- hostname)) {
- throw new AmbariException("Invalid entry for ServiceComponentHost"
- + ", serviceName=" + serviceName + ", serviceComponentName"
- + componentName + ", hostname= " + hostname);
- }
+ if (!serviceComponentHosts.containsKey(serviceName)
+ || !serviceComponentHosts.get(serviceName).containsKey(componentName)
+ || !serviceComponentHosts.get(serviceName).get(componentName).containsKey(
+ hostname)) {
+ throw new AmbariException("Invalid entry for ServiceComponentHost"
+ + ", serviceName=" + serviceName + ", serviceComponentName"
+ + componentName + ", hostname= " + hostname);
+ }
- if (!serviceComponentHostsByHost.containsKey(hostname)) {
- throw new AmbariException("Invalid host entry for ServiceComponentHost"
- + ", serviceName=" + serviceName + ", serviceComponentName"
- + componentName + ", hostname= " + hostname);
- }
+ if (!serviceComponentHostsByHost.containsKey(hostname)) {
+ throw new AmbariException("Invalid host entry for ServiceComponentHost"
+ + ", serviceName=" + serviceName + ", serviceComponentName"
+ + componentName + ", hostname= " + hostname);
+ }
- ServiceComponentHost schToRemove = null;
- for (ServiceComponentHost sch : serviceComponentHostsByHost.get(hostname)) {
- if (sch.getServiceName().equals(serviceName)
- && sch.getServiceComponentName().equals(componentName)
- && sch.getHostName().equals(hostname)) {
- schToRemove = sch;
- break;
- }
+ ServiceComponentHost schToRemove = null;
+ for (ServiceComponentHost sch : serviceComponentHostsByHost.get(hostname)) {
+ if (sch.getServiceName().equals(serviceName)
+ && sch.getServiceComponentName().equals(componentName)
+ && sch.getHostName().equals(hostname)) {
+ schToRemove = sch;
+ break;
}
+ }
- if (schToRemove == null) {
- LOG.warn("Unavailable in per host cache. ServiceComponentHost"
- + ", serviceName=" + serviceName
- + ", serviceComponentName" + componentName
- + ", hostname= " + hostname);
- }
+ if (schToRemove == null) {
+ LOG.warn("Unavailable in per host cache. ServiceComponentHost"
+ + ", serviceName=" + serviceName
+ + ", serviceComponentName" + componentName
+ + ", hostname= " + hostname);
+ }
- if (LOG.isDebugEnabled()) {
- LOG.debug("Removing a ServiceComponentHost" + ", clusterName="
- + getClusterName() + ", clusterId=" + getClusterId()
- + ", serviceName=" + serviceName + ", serviceComponentName"
- + componentName + ", hostname= " + hostname);
- }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Removing a ServiceComponentHost" + ", clusterName="
+ + getClusterName() + ", clusterId=" + getClusterId()
+ + ", serviceName=" + serviceName + ", serviceComponentName"
+ + componentName + ", hostname= " + hostname);
+ }
- serviceComponentHosts.get(serviceName).get(componentName).remove(hostname);
- if (schToRemove != null) {
- serviceComponentHostsByHost.get(hostname).remove(schToRemove);
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ serviceComponentHosts.get(serviceName).get(componentName).remove(hostname);
+ if (schToRemove != null) {
+ serviceComponentHostsByHost.get(hostname).remove(schToRemove);
}
}
@@ -905,19 +770,13 @@ public class ClusterImpl implements Cluster {
}
@Override
- public List<ServiceComponentHost> getServiceComponentHosts(
- String hostname) {
- loadServiceHostComponents();
- clusterGlobalLock.readLock().lock();
- try {
- if (serviceComponentHostsByHost.containsKey(hostname)) {
- return new CopyOnWriteArrayList<ServiceComponentHost>(
- serviceComponentHostsByHost.get(hostname));
- }
- return new ArrayList<ServiceComponentHost>();
- } finally {
- clusterGlobalLock.readLock().unlock();
+ public List<ServiceComponentHost> getServiceComponentHosts(String hostname) {
+ List<ServiceComponentHost> serviceComponentHosts = serviceComponentHostsByHost.get(hostname);
+ if (null != serviceComponentHosts) {
+ return new CopyOnWriteArrayList<ServiceComponentHost>(serviceComponentHosts);
}
+
+ return new ArrayList<ServiceComponentHost>();
}
@Override
@@ -961,21 +820,16 @@ public class ClusterImpl implements Cluster {
public List<ServiceComponentHost> getServiceComponentHosts(String serviceName, String componentName) {
ArrayList<ServiceComponentHost> foundItems = new ArrayList<ServiceComponentHost>();
- loadServiceHostComponents();
- clusterGlobalLock.readLock().lock();
- try {
- Map<String, Map<String, ServiceComponentHost>> foundByService = serviceComponentHosts.get(serviceName);
- if (foundByService != null) {
- if (componentName == null) {
- for(Map<String, ServiceComponentHost> foundByComponent :foundByService.values()) {
- foundItems.addAll(foundByComponent.values());
- }
- } else if (foundByService.containsKey(componentName)) {
- foundItems.addAll(foundByService.get(componentName).values());
+ ConcurrentMap<String, ConcurrentMap<String, ServiceComponentHost>> foundByService = serviceComponentHosts.get(
+ serviceName);
+ if (foundByService != null) {
+ if (componentName == null) {
+ for (Map<String, ServiceComponentHost> foundByComponent : foundByService.values()) {
+ foundItems.addAll(foundByComponent.values());
}
+ } else if (foundByService.containsKey(componentName)) {
+ foundItems.addAll(foundByService.get(componentName).values());
}
- } finally {
- clusterGlobalLock.readLock().unlock();
}
return foundItems;
@@ -983,76 +837,44 @@ public class ClusterImpl implements Cluster {
@Override
public void addService(Service service) {
- loadServices();
- clusterGlobalLock.writeLock().lock();
- try {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Adding a new Service" + ", clusterName=" + getClusterName()
- + ", clusterId=" + getClusterId() + ", serviceName="
- + service.getName());
- }
- services.put(service.getName(), service);
- } finally {
- clusterGlobalLock.writeLock().unlock();
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Adding a new Service" + ", clusterName=" + getClusterName() + ", clusterId="
+ + getClusterId() + ", serviceName=" + service.getName());
}
+ services.put(service.getName(), service);
}
@Override
public Service addService(String serviceName) throws AmbariException {
- loadServices();
- clusterGlobalLock.writeLock().lock();
- try {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Adding a new Service" + ", clusterName=" + getClusterName()
+ if (services.containsKey(serviceName)) {
+ throw new AmbariException("Service already exists" + ", clusterName=" + getClusterName()
+ ", clusterId=" + getClusterId() + ", serviceName=" + serviceName);
- }
- if (services.containsKey(serviceName)) {
- throw new AmbariException("Service already exists" + ", clusterName="
- + getClusterName() + ", clusterId=" + getClusterId()
- + ", serviceName=" + serviceName);
- }
- Service s = serviceFactory.createNew(this, serviceName);
- services.put(s.getName(), s);
- return s;
- } finally {
- clusterGlobalLock.writeLock().unlock();
}
+
+ Service service = serviceFactory.createNew(this, serviceName);
+ addService(service);
+
+ return service;
}
@Override
public Service getService(String serviceName) throws AmbariException {
- loadServices();
- clusterGlobalLock.readLock().lock();
- try {
- if (!services.containsKey(serviceName)) {
- throw new ServiceNotFoundException(getClusterName(), serviceName);
- }
- return services.get(serviceName);
- } finally {
- clusterGlobalLock.readLock().unlock();
+ Service service = services.get(serviceName);
+ if (null == service) {
+ throw new ServiceNotFoundException(getClusterName(), serviceName);
}
+
+ return service;
}
@Override
public Map<String, Service> getServices() {
- loadServices();
- clusterGlobalLock.readLock().lock();
- try {
- return new HashMap<String, Service>(services);
- } finally {
- clusterGlobalLock.readLock().unlock();
- }
+ return new HashMap<String, Service>(services);
}
@Override
public StackId getDesiredStackVersion() {
- loadStackVersion();
- clusterGlobalLock.readLock().lock();
- try {
- return desiredStackVersion;
- } finally {
- clusterGlobalLock.readLock().unlock();
- }
+ return desiredStackVersion;
}
@Override
@@ -1075,25 +897,24 @@ public class ClusterImpl implements Cluster {
StackEntity stackEntity = stackDAO.find(stackId.getStackName(), stackId.getStackVersion());
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- clusterEntity.setDesiredStack(stackEntity);
- clusterEntity = clusterDAO.merge(clusterEntity);
- if (cascade) {
- for (Service service : getServices().values()) {
- service.setDesiredStackVersion(stackId);
+ clusterEntity.setDesiredStack(stackEntity);
+ clusterEntity = clusterDAO.merge(clusterEntity);
+
+ if (cascade) {
+ for (Service service : getServices().values()) {
+ service.setDesiredStackVersion(stackId);
- for (ServiceComponent sc : service.getServiceComponents().values()) {
- sc.setDesiredStackVersion(stackId);
+ for (ServiceComponent sc : service.getServiceComponents().values()) {
+ sc.setDesiredStackVersion(stackId);
- for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
- sch.setDesiredStackVersion(stackId);
- }
+ for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
+ sch.setDesiredStackVersion(stackId);
}
}
}
- loadServiceConfigTypes();
}
+ loadServiceConfigTypes();
} finally {
clusterGlobalLock.writeLock().unlock();
}
@@ -1101,89 +922,55 @@ public class ClusterImpl implements Cluster {
@Override
public StackId getCurrentStackVersion() {
- clusterGlobalLock.readLock().lock();
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- ClusterStateEntity clusterStateEntity = clusterEntity.getClusterStateEntity();
- if (clusterStateEntity != null) {
- StackEntity currentStackEntity = clusterStateEntity.getCurrentStack();
- return new StackId(currentStackEntity);
- }
- }
+ ClusterEntity clusterEntity = getClusterEntity();
- return null;
- } finally {
- clusterGlobalLock.readLock().unlock();
+ ClusterStateEntity clusterStateEntity = clusterEntity.getClusterStateEntity();
+ if (clusterStateEntity != null) {
+ StackEntity currentStackEntity = clusterStateEntity.getCurrentStack();
+ return new StackId(currentStackEntity);
}
+
+ return null;
}
@Override
public State getProvisioningState() {
- clusterGlobalLock.readLock().lock();
State provisioningState = null;
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- provisioningState = clusterEntity.getProvisioningState();
-
- if (null == provisioningState) {
- provisioningState = State.INIT;
- }
- }
+ ClusterEntity clusterEntity = getClusterEntity();
+ provisioningState = clusterEntity.getProvisioningState();
- return provisioningState;
- } finally {
- clusterGlobalLock.readLock().unlock();
+ if (null == provisioningState) {
+ provisioningState = State.INIT;
}
+
+ return provisioningState;
}
@Override
public void setProvisioningState(State provisioningState) {
- clusterGlobalLock.writeLock().lock();
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- clusterEntity.setProvisioningState(provisioningState);
- clusterEntity = clusterDAO.merge(clusterEntity);
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
- }
+ ClusterEntity clusterEntity = getClusterEntity();
+ clusterEntity.setProvisioningState(provisioningState);
+ clusterEntity = clusterDAO.merge(clusterEntity);
}
@Override
public SecurityType getSecurityType() {
- clusterGlobalLock.readLock().lock();
SecurityType securityType = null;
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- securityType = clusterEntity.getSecurityType();
-
- if (null == securityType) {
- securityType = SecurityType.NONE;
- }
- }
+ ClusterEntity clusterEntity = getClusterEntity();
+ securityType = clusterEntity.getSecurityType();
- return securityType;
- } finally {
- clusterGlobalLock.readLock().unlock();
+ if (null == securityType) {
+ securityType = SecurityType.NONE;
}
+
+ return securityType;
}
@Override
public void setSecurityType(SecurityType securityType) {
- clusterGlobalLock.writeLock().lock();
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- clusterEntity.setSecurityType(securityType);
- clusterEntity = clusterDAO.merge(clusterEntity);
- }
- } finally {
- clusterGlobalLock.writeLock().unlock();
- }
+ ClusterEntity clusterEntity = getClusterEntity();
+ clusterEntity.setSecurityType(securityType);
+ clusterEntity = clusterDAO.merge(clusterEntity);
}
/**
@@ -1859,129 +1646,127 @@ public class ClusterImpl implements Cluster {
clusterGlobalLock.writeLock().lock();
try {
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- ClusterVersionEntity existingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
+ ClusterVersionEntity existingClusterVersion = clusterVersionDAO.findByClusterAndStackAndVersion(
getClusterName(), stackId, version);
- if (existingClusterVersion == null) {
- throw new AmbariException(
- "Existing cluster version not found for cluster="
- + getClusterName() + ", stack=" + stackId + ", version="
- + version);
- }
+ if (existingClusterVersion == null) {
+ throw new AmbariException("Existing cluster version not found for cluster="
+ + getClusterName() + ", stack=" + stackId + ", version=" + version);
+ }
- // NOOP
- if (existingClusterVersion.getState() == state) {
- return;
- }
+ // NOOP
+ if (existingClusterVersion.getState() == state) {
+ return;
+ }
- switch (existingClusterVersion.getState()) {
- case CURRENT:
- // If CURRENT state is changed here cluster will not have CURRENT
- // state.
- // CURRENT state will be changed to INSTALLED when another CURRENT
- // state is added.
- // allowedStates.add(RepositoryVersionState.INSTALLED);
- break;
- case INSTALLING:
- allowedStates.add(RepositoryVersionState.INSTALLED);
- allowedStates.add(RepositoryVersionState.INSTALL_FAILED);
- allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
- if (clusterVersionDAO.findByClusterAndStateCurrent(getClusterName()) == null) {
- allowedStates.add(RepositoryVersionState.CURRENT);
- }
- break;
- case INSTALL_FAILED:
- allowedStates.add(RepositoryVersionState.INSTALLING);
- break;
- case INSTALLED:
- allowedStates.add(RepositoryVersionState.INSTALLING);
- allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
- allowedStates.add(RepositoryVersionState.CURRENT);
- break;
- case OUT_OF_SYNC:
- allowedStates.add(RepositoryVersionState.INSTALLING);
- break;
- case INIT:
+ switch (existingClusterVersion.getState()) {
+ case CURRENT:
+ // If CURRENT state is changed here cluster will not have CURRENT
+ // state.
+ // CURRENT state will be changed to INSTALLED when another CURRENT
+ // state is added.
+ // allowedStates.add(RepositoryVersionState.INSTALLED);
+ break;
+ case INSTALLING:
+ allowedStates.add(RepositoryVersionState.INSTALLED);
+ allowedStates.add(RepositoryVersionState.INSTALL_FAILED);
+ allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
+ if (clusterVersionDAO.findByClusterAndStateCurrent(getClusterName()) == null) {
allowedStates.add(RepositoryVersionState.CURRENT);
- break;
- }
+ }
+ break;
+ case INSTALL_FAILED:
+ allowedStates.add(RepositoryVersionState.INSTALLING);
+ break;
+ case INSTALLED:
+ allowedStates.add(RepositoryVersionState.INSTALLING);
+ allowedStates.add(RepositoryVersionState.OUT_OF_SYNC);
+ allowedStates.add(RepositoryVersionState.CURRENT);
+ break;
+ case OUT_OF_SYNC:
+ allowedStates.add(RepositoryVersionState.INSTALLING);
+ break;
+ case INIT:
+ allowedStates.add(RepositoryVersionState.CURRENT);
+ break;
+ }
- if (!allowedStates.contains(state)) {
- throw new AmbariException("Invalid cluster version transition from "
+ if (!allowedStates.contains(state)) {
+ throw new AmbariException("Invalid cluster version transition from "
+ existingClusterVersion.getState() + " to " + state);
- }
+ }
- // There must be at most one cluster version whose state is CURRENT at
- // all times.
- if (state == RepositoryVersionState.CURRENT) {
- ClusterVersionEntity currentVersion = clusterVersionDAO.findByClusterAndStateCurrent(getClusterName());
- if (currentVersion != null) {
- currentVersion.setState(RepositoryVersionState.INSTALLED);
- currentVersion = clusterVersionDAO.merge(currentVersion);
- }
+ // There must be at most one cluster version whose state is CURRENT at
+ // all times.
+ if (state == RepositoryVersionState.CURRENT) {
+ ClusterVersionEntity currentVersion = clusterVersionDAO.findByClusterAndStateCurrent(
+ getClusterName());
+ if (currentVersion != null) {
+ currentVersion.setState(RepositoryVersionState.INSTALLED);
+ currentVersion = clusterVersionDAO.merge(currentVersion);
}
+ }
- existingClusterVersion.setState(state);
- existingClusterVersion.setEndTime(System.currentTimeMillis());
- existingClusterVersion = clusterVersionDAO.merge(existingClusterVersion);
+ existingClusterVersion.setState(state);
+ existingClusterVersion.setEndTime(System.currentTimeMillis());
+ existingClusterVersion = clusterVersionDAO.merge(existingClusterVersion);
- if (state == RepositoryVersionState.CURRENT) {
- for (HostEntity hostEntity : clusterEntity.getHostEntities()) {
- if (hostHasReportables(existingClusterVersion.getRepositoryVersion(), hostEntity)) {
- continue;
- }
+ if (state == RepositoryVersionState.CURRENT) {
+ for (HostEntity hostEntity : clusterEntity.getHostEntities()) {
+ if (hostHasReportables(existingClusterVersion.getRepositoryVersion(), hostEntity)) {
+ continue;
+ }
- Collection<HostVersionEntity> versions = hostVersionDAO.findByHost(hostEntity.getHostName());
+ Collection<HostVersionEntity> versions = hostVersionDAO.findByHost(
+ hostEntity.getHostName());
- HostVersionEntity target = null;
- if (null != versions) {
- // Set anything that was previously marked CURRENT as INSTALLED, and
- // the matching version as CURRENT
- for (HostVersionEntity entity : versions) {
- if (entity.getRepositoryVersion().getId().equals(
+ HostVersionEntity target = null;
+ if (null != versions) {
+ // Set anything that was previously marked CURRENT as INSTALLED, and
+ // the matching version as CURRENT
+ for (HostVersionEntity entity : versions) {
+ if (entity.getRepositoryVersion().getId().equals(
existingClusterVersion.getRepositoryVersion().getId())) {
- target = entity;
- target.setState(state);
- target = hostVersionDAO.merge(target);
- } else if (entity.getState() == RepositoryVersionState.CURRENT) {
- entity.setState(RepositoryVersionState.INSTALLED);
- entity = hostVersionDAO.merge(entity);
- }
+ target = entity;
+ target.setState(state);
+ target = hostVersionDAO.merge(target);
+ } else if (entity.getState() == RepositoryVersionState.CURRENT) {
+ entity.setState(RepositoryVersionState.INSTALLED);
+ entity = hostVersionDAO.merge(entity);
}
}
+ }
- if (null == target) {
- // If no matching version was found, create one with the desired
- // state
- HostVersionEntity hve = new HostVersionEntity(hostEntity,
+ if (null == target) {
+ // If no matching version was found, create one with the desired
+ // state
+ HostVersionEntity hve = new HostVersionEntity(hostEntity,
existingClusterVersion.getRepositoryVersion(), state);
- LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
- hve.getHostName(), hve.getState(),
- hve.getRepositoryVersion().getVersion(), hve.getRepositoryVersion().getId());
+ LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})",
+ hve.getHostName(), hve.getState(), hve.getRepositoryVersion().getVersion(),
+ hve.getRepositoryVersion().getId());
- hostVersionDAO.create(hve);
- }
+ hostVersionDAO.create(hve);
}
+ }
- // when setting the cluster's state to current, we must also
- // bring the desired stack and current stack in line with each other
- StackEntity desiredStackEntity = clusterEntity.getDesiredStack();
- StackId desiredStackId = new StackId(desiredStackEntity);
+ // when setting the cluster's state to current, we must also
+ // bring the desired stack and current stack in line with each other
+ StackEntity desiredStackEntity = clusterEntity.getDesiredStack();
+ StackId desiredStackId = new StackId(desiredStackEntity);
- // if the desired stack ID doesn't match the target when setting the
- // cluster to CURRENT, then there's a problem
- if (!desiredStackId.equals(stackId)) {
- String message = MessageFormat.format(
+ // if the desired stack ID doesn't match the target when setting the
+ // cluster to CURRENT, then there's a problem
+ if (!desiredStackId.equals(stackId)) {
+ String message = MessageFormat.format(
"The desired stack ID {0} must match {1} when transitioning the cluster''s state to {2}",
desiredStackId, stackId, RepositoryVersionState.CURRENT);
- throw new AmbariException(message);
- }
-
- setCurrentStackVersion(stackId);
+ throw new AmbariException(message);
}
+
+ setCurrentStackVersion(stackId);
}
} catch (RollbackException e) {
String message = MessageFormat.format(
@@ -2030,22 +1815,21 @@ public class ClusterImpl implements Cluster {
stackId.getStackVersion());
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- ClusterStateEntity clusterStateEntity = clusterStateDAO.findByPK(clusterEntity.getClusterId());
- if (clusterStateEntity == null) {
- clusterStateEntity = new ClusterStateEntity();
- clusterStateEntity.setClusterId(clusterEntity.getClusterId());
- clusterStateEntity.setCurrentStack(stackEntity);
- clusterStateEntity.setClusterEntity(clusterEntity);
- clusterStateDAO.create(clusterStateEntity);
- clusterStateEntity = clusterStateDAO.merge(clusterStateEntity);
- clusterEntity.setClusterStateEntity(clusterStateEntity);
- clusterEntity = clusterDAO.merge(clusterEntity);
- } else {
- clusterStateEntity.setCurrentStack(stackEntity);
- clusterStateEntity = clusterStateDAO.merge(clusterStateEntity);
- clusterEntity = clusterDAO.merge(clusterEntity);
- }
+ ClusterStateEntity clusterStateEntity = clusterStateDAO.findByPK(
+ clusterEntity.getClusterId());
+ if (clusterStateEntity == null) {
+ clusterStateEntity = new ClusterStateEntity();
+ clusterStateEntity.setClusterId(clusterEntity.getClusterId());
+ clusterStateEntity.setCurrentStack(stackEntity);
+ clusterStateEntity.setClusterEntity(clusterEntity);
+ clusterStateDAO.create(clusterStateEntity);
+ clusterStateEntity = clusterStateDAO.merge(clusterStateEntity);
+ clusterEntity.setClusterStateEntity(clusterStateEntity);
+ clusterEntity = clusterDAO.merge(clusterEntity);
+ } else {
+ clusterStateEntity.setCurrentStack(stackEntity);
+ clusterStateEntity = clusterStateDAO.merge(clusterStateEntity);
+ clusterEntity = clusterDAO.merge(clusterEntity);
}
} catch (RollbackException e) {
LOG.warn("Unable to set version " + stackId + " for cluster "
@@ -2059,7 +1843,6 @@ public class ClusterImpl implements Cluster {
@Override
public Map<String, Config> getConfigsByType(String configType) {
- loadConfigurations();
clusterGlobalLock.readLock().lock();
try {
if (!allConfigs.containsKey(configType)) {
@@ -2074,7 +1857,6 @@ public class ClusterImpl implements Cluster {
@Override
public Config getConfig(String configType, String versionTag) {
- loadConfigurations();
clusterGlobalLock.readLock().lock();
try {
if (!allConfigs.containsKey(configType)
@@ -2094,11 +1876,13 @@ public class ClusterImpl implements Cluster {
if (!allConfigs.containsKey(configType)) {
return null;
}
- for(Map.Entry<String, Config> entry: allConfigs.get(configType).entrySet()) {
- if(entry.getValue().getVersion().equals(configVersion)) {
+
+ for (Map.Entry<String, Config> entry : allConfigs.get(configType).entrySet()) {
+ if (entry.getValue().getVersion().equals(configVersion)) {
return entry.getValue();
}
}
+
return null;
} finally {
clusterGlobalLock.readLock().unlock();
@@ -2107,14 +1891,14 @@ public class ClusterImpl implements Cluster {
@Override
public void addConfig(Config config) {
- loadConfigurations();
+ if (config.getType() == null || config.getType().isEmpty()) {
+ throw new IllegalArgumentException("Config type cannot be empty");
+ }
+
clusterGlobalLock.writeLock().lock();
try {
- if (config.getType() == null || config.getType().isEmpty()) {
- throw new IllegalArgumentException("Config type cannot be empty");
- }
if (!allConfigs.containsKey(config.getType())) {
- allConfigs.put(config.getType(), new HashMap<String, Config>());
+ allConfigs.put(config.getType(), new ConcurrentHashMap<String, Config>());
}
allConfigs.get(config.getType()).put(config.getTag(), config);
@@ -2125,11 +1909,10 @@ public class ClusterImpl implements Cluster {
@Override
public Collection<Config> getAllConfigs() {
- loadConfigurations();
clusterGlobalLock.readLock().lock();
try {
List<Config> list = new ArrayList<Config>();
- for (Entry<String, Map<String, Config>> entry : allConfigs.entrySet()) {
+ for (Entry<String, ConcurrentMap<String, Config>> entry : allConfigs.entrySet()) {
for (Config config : entry.getValue().values()) {
list.add(config);
}
@@ -2143,44 +1926,31 @@ public class ClusterImpl implements Cluster {
@Override
public ClusterResponse convertToResponse()
throws AmbariException {
- loadStackVersion();
String clusterName = getClusterName();
Map<String, Host> hosts = clusters.getHostsForCluster(clusterName);
- clusterGlobalLock.readLock().lock();
- try {
- return new ClusterResponse(getClusterId(), clusterName,
- getProvisioningState(), getSecurityType(), hosts.keySet(),
- hosts.size(), getDesiredStackVersion().getStackId(),
- getClusterHealthReport(hosts));
- } finally {
- clusterGlobalLock.readLock().unlock();
- }
+
+ return new ClusterResponse(getClusterId(), clusterName,
+ getProvisioningState(), getSecurityType(), hosts.keySet(),
+ hosts.size(), getDesiredStackVersion().getStackId(),
+ getClusterHealthReport(hosts));
}
@Override
public void debugDump(StringBuilder sb) {
- loadServices();
- loadStackVersion();
- clusterGlobalLock.readLock().lock();
- try {
- sb.append("Cluster={ clusterName=").append(getClusterName()).append(
- ", clusterId=").append(getClusterId()).append(
- ", desiredStackVersion=").append(desiredStackVersion.getStackId()).append(
- ", services=[ ");
- boolean first = true;
- for (Service s : services.values()) {
- if (!first) {
- sb.append(" , ");
- }
- first = false;
- sb.append("\n ");
- s.debugDump(sb);
- sb.append(' ');
+ sb.append("Cluster={ clusterName=").append(getClusterName()).append(", clusterId=").append(
+ getClusterId()).append(", desiredStackVersion=").append(
+ desiredStackVersion.getStackId()).append(", services=[ ");
+ boolean first = true;
+ for (Service s : services.values()) {
+ if (!first) {
+ sb.append(" , ");
}
- sb.append(" ] }");
- } finally {
- clusterGlobalLock.readLock().unlock();
+ first = false;
+ sb.append("\n ");
+ s.debugDump(sb);
+ sb.append(' ');
}
+ sb.append(" ] }");
}
@Override
@@ -2198,7 +1968,6 @@ public class ClusterImpl implements Cluster {
@Override
@Transactional
public void deleteAllServices() throws AmbariException {
- loadServices();
clusterGlobalLock.writeLock().lock();
try {
LOG.info("Deleting all services for cluster" + ", clusterName="
@@ -2224,7 +1993,6 @@ public class ClusterImpl implements Cluster {
@Override
public void deleteService(String serviceName)
throws AmbariException {
- loadServices();
clusterGlobalLock.writeLock().lock();
try {
Service service = getService(serviceName);
@@ -2273,7 +2041,6 @@ public class ClusterImpl implements Cluster {
@Override
public boolean canBeRemoved() {
- loadServices();
clusterGlobalLock.readLock().lock();
try {
boolean safeToRemove = true;
@@ -2388,7 +2155,6 @@ public class ClusterImpl implements Cluster {
* @return a map of type-to-configuration information.
*/
private Map<String, Set<DesiredConfig>> getDesiredConfigs(boolean allVersions) {
- loadConfigurations();
clusterGlobalLock.readLock().lock();
try {
Map<String, Set<DesiredConfig>> map = new HashMap<>();
@@ -2474,37 +2240,38 @@ public class ClusterImpl implements Cluster {
clusterGlobalLock.writeLock().lock();
try {
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- // set config group
- if (configGroup != null) {
- serviceConfigEntity.setGroupId(configGroup.getId());
- Collection<Config> configs = configGroup.getConfigurations().values();
- List<ClusterConfigEntity> configEntities = new ArrayList<ClusterConfigEntity>(configs.size());
- for (Config config : configs) {
- configEntities.add(clusterDAO.findConfig(getClusterId(), config.getType(), config.getTag()));
- }
-
- serviceConfigEntity.setClusterConfigEntities(configEntities);
- } else {
- List<ClusterConfigEntity> configEntities = getClusterConfigEntitiesByService(serviceName);
- serviceConfigEntity.setClusterConfigEntities(configEntities);
+ // set config group
+ if (configGroup != null) {
+ serviceConfigEntity.setGroupId(configGroup.getId());
+ Collection<Config> configs = configGroup.getConfigurations().values();
+ List<ClusterConfigEntity> configEntities = new ArrayList<ClusterConfigEntity>(
+ configs.size());
+ for (Config config : configs) {
+ configEntities.add(
+ clusterDAO.findConfig(getClusterId(), config.getType(), config.getTag()));
}
+ serviceConfigEntity.setClusterConfigEntities(configEntities);
+ } else {
+ List<ClusterConfigEntity> configEntities = getClusterConfigEntitiesByService(serviceName);
+ serviceConfigEntity.setClusterConfigEntities(configEntities);
+ }
- long nextServiceConfigVersion = serviceConfigDAO.findNextServiceConfigVersion(clusterId, serviceName);
- serviceConfigEntity.setServiceName(serviceName);
- serviceConfigEntity.setClusterEntity(clusterEntity);
- serviceConfigEntity.setVersion(nextServiceConfigVersion);
- serviceConfigEntity.setUser(user);
- serviceConfigEntity.setNote(note);
- serviceConfigEntity.setStack(clusterEntity.getDesiredStack());
+ long nextServiceConfigVersion = serviceConfigDAO.findNextServiceConfigVersion(clusterId,
+ serviceName);
- serviceConfigDAO.create(serviceConfigEntity);
- if (configGroup != null) {
- serviceConfigEntity.setHostIds(new ArrayList<Long>(configGroup.getHosts().keySet()));
- serviceConfigEntity = serviceConfigDAO.merge(serviceConfigEntity);
- }
+ serviceConfigEntity.setServiceName(serviceName);
+ serviceConfigEntity.setClusterEntity(clusterEntity);
+ serviceConfigEntity.setVersion(nextServiceConfigVersion);
+ serviceConfigEntity.setUser(user);
+ serviceConfigEntity.setNote(note);
+ serviceConfigEntity.setStack(clusterEntity.getDesiredStack());
+
+ serviceConfigDAO.create(serviceConfigEntity);
+ if (configGroup != null) {
+ serviceConfigEntity.setHostIds(new ArrayList<Long>(configGroup.getHosts().keySet()));
+ serviceConfigEntity = serviceConfigDAO.merge(serviceConfigEntity);
}
} finally {
clusterGlobalLock.writeLock().unlock();
@@ -2597,7 +2364,6 @@ public class ClusterImpl implements Cluster {
@Override
public List<ServiceConfigVersionResponse> getServiceConfigVersions() {
- loadConfigurations();
clusterGlobalLock.readLock().lock();
try {
List<ServiceConfigVersionResponse> serviceConfigVersionResponses = new ArrayList<ServiceConfigVersionResponse>();
@@ -2926,35 +2692,26 @@ public class ClusterImpl implements Cluster {
@Override
public Config getDesiredConfigByType(String configType) {
- loadConfigurations();
- clusterGlobalLock.readLock().lock();
- try {
- for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId())) {
- if (e.isSelected() > 0 && e.getType().equals(configType)) {
- return getConfig(e.getType(), e.getTag());
- }
+ for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(
+ getClusterId())) {
+ if (e.isSelected() > 0 && e.getType().equals(configType)) {
+ return getConfig(e.getType(), e.getTag());
}
-
- return null;
- } finally {
- clusterGlobalLock.readLock().unlock();
}
+
+ return null;
}
@Override
public boolean isConfigTypeExists(String configType) {
- clusterGlobalLock.readLock().lock();
- try {
- for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId())) {
- if (e.getType().equals(configType)) {
- return true;
- }
+ for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(
+ getClusterId())) {
+ if (e.getType().equals(configType)) {
+ return true;
}
-
- return false;
- } finally {
- clusterGlobalLock.readLock().unlock();
}
+
+ return false;
}
@Override
@@ -3125,13 +2882,13 @@ public class ClusterImpl implements Cluster {
*/
@Override
public Set<String> getHosts(String serviceName, String componentName) {
- Map<String, Service> services = getServices();
+ Map<String, Service> clusterServices = getServices();
- if (!services.containsKey(serviceName)) {
+ if (!clusterServices.containsKey(serviceName)) {
return Collections.emptySet();
}
- Service service = services.get(serviceName);
+ Service service = clusterServices.get(serviceName);
Map<String, ServiceComponent> components = service.getServiceComponents();
if (!components.containsKey(componentName) ||
@@ -3250,16 +3007,14 @@ public class ClusterImpl implements Cluster {
@Override
public boolean checkPermission(PrivilegeEntity privilegeEntity, boolean readOnly) {
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- ResourceEntity resourceEntity = clusterEntity.getResource();
- if (resourceEntity != null) {
- Integer permissionId = privilegeEntity.getPermission().getId();
- // CLUSTER.USER or CLUSTER.ADMINISTRATOR for the given cluster resource.
- if (privilegeEntity.getResource().equals(resourceEntity)) {
- if ((readOnly && permissionId.equals(PermissionEntity.CLUSTER_USER_PERMISSION)) ||
- permissionId.equals(PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION)) {
- return true;
- }
+ ResourceEntity resourceEntity = clusterEntity.getResource();
+ if (resourceEntity != null) {
+ Integer permissionId = privilegeEntity.getPermission().getId();
+ // CLUSTER.USER or CLUSTER.ADMINISTRATOR for the given cluster resource.
+ if (privilegeEntity.getResource().equals(resourceEntity)) {
+ if ((readOnly && permissionId.equals(PermissionEntity.CLUSTER_USER_PERMISSION))
+ || permissionId.equals(PermissionEntity.CLUSTER_ADMINISTRATOR_PERMISSION)) {
+ return true;
}
}
}
@@ -3525,19 +3280,16 @@ public class ClusterImpl implements Cluster {
* Caches all of the {@link ClusterConfigEntity}s in {@link #allConfigs}.
*/
private void cacheConfigurations() {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- if (null == allConfigs) {
- allConfigs = new HashMap<String, Map<String, Config>>();
- }
-
+ clusterGlobalLock.writeLock().lock();
+ try {
+ ClusterEntity clusterEntity = getClusterEntity();
allConfigs.clear();
if (!clusterEntity.getClusterConfigEntities().isEmpty()) {
for (ClusterConfigEntity entity : clusterEntity.getClusterConfigEntities()) {
if (!allConfigs.containsKey(entity.getType())) {
- allConfigs.put(entity.getType(), new HashMap<String, Config>());
+ allConfigs.put(entity.getType(), new ConcurrentHashMap<String, Config>());
}
Config config = configFactory.createExisting(this, entity);
@@ -3545,80 +3297,23 @@ public class ClusterImpl implements Cluster {
allConfigs.get(entity.getType()).put(entity.getTag(), config);
}
}
- }
- }
-
- private void loadConfigurations() {
- if (allConfigs != null) {
- return;
- }
- clusterGlobalLock.writeLock().lock();
- try {
- if (allConfigs != null) {
- return;
- }
- cacheConfigurations();
-
} finally {
clusterGlobalLock.writeLock().unlock();
}
}
private void loadStackVersion() {
- if (desiredStackVersionSet) {
- return;
- }
- clusterGlobalLock.writeLock().lock();
- try {
-
- if (desiredStackVersionSet) {
- return;
- }
+ desiredStackVersion = new StackId(getClusterEntity().getDesiredStack());
- desiredStackVersion = new StackId(getClusterEntity().getDesiredStack());
-
- if (!StringUtils.isEmpty(desiredStackVersion.getStackName()) && !
- StringUtils.isEmpty(desiredStackVersion.getStackVersion())) {
- try {
- loadServiceConfigTypes();
- } catch (AmbariException e) {
- //TODO recheck wrapping exception here, required for lazy loading after invalidation
- throw new RuntimeException(e);
- }
+ if (!StringUtils.isEmpty(desiredStackVersion.getStackName())
+ && !StringUtils.isEmpty(desiredStackVersion.getStackVersion())) {
+ try {
+ loadServiceConfigTypes();
+ } catch (AmbariException e) {
+ // TODO recheck wrapping exception here, required for lazy loading after
+ // invalidation
+ throw new RuntimeException(e);
}
-
- desiredStackVersionSet = true;
-
- } finally {
- clusterGlobalLock.writeLock().unlock();
- }
-
- }
-
- /**
- * Purpose of this method is to clear all cached data to re-read it from database.
- * To be used in case of desync.
- */
- @Override
- public void invalidateData() {
- clusterGlobalLock.writeLock().lock();
- try {
- allConfigs = null;
- services = null;
- desiredStackVersionSet = false;
-
- serviceComponentHosts.clear();
- serviceComponentHostsByHost.clear();
- svcHostsLoaded = false;
-
- clusterConfigGroups = null;
-
- //TODO investigate reset request executions, it has separate api which is not too heavy
-
- refresh();
-
- } finally {
- clusterGlobalLock.writeLock().unlock();
}
}
@@ -3668,17 +3363,8 @@ public class ClusterImpl implements Cluster {
*/
@Override
public UpgradeEntity getUpgradeEntity() {
- clusterGlobalLock.readLock().lock();
- try {
- ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- return clusterEntity.getUpgradeEntity();
- } else {
- return null;
- }
- } finally {
- clusterGlobalLock.readLock().unlock();
- }
+ ClusterEntity clusterEntity = getClusterEntity();
+ return clusterEntity.getUpgradeEntity();
}
/**
@@ -3687,20 +3373,15 @@ public class ClusterImpl implements Cluster {
@Override
@Transactional
public void setUpgradeEntity(UpgradeEntity upgradeEntity) throws AmbariException {
- clusterGlobalLock.writeLock().lock();
try {
ClusterEntity clusterEntity = getClusterEntity();
- if (clusterEntity != null) {
- clusterEntity.setUpgradeEntity(upgradeEntity);
- clusterDAO.merge(clusterEntity);
- }
+ clusterEntity.setUpgradeEntity(upgradeEntity);
+ clusterDAO.merge(clusterEntity);
} catch (RollbackException e) {
String msg = "Unable to set upgrade entiry " + upgradeEntity + " for cluster "
+ getClusterName();
LOG.warn(msg);
throw new AmbariException(msg, e);
- } finally {
- clusterGlobalLock.writeLock().unlock();
}
}
@@ -3772,4 +3453,5 @@ public class ClusterImpl implements Cluster {
m_clusterPropertyCache.clear();
}
+
}