Bladeren bron

AMBARI-5803. Implement Slider Apps View status endpoint - direct API. (srimanth)

Srimanth Gunturi 11 jaren geleden
bovenliggende
commit
e03d242834

+ 8 - 0
contrib/views/slider/pom.xml

@@ -93,12 +93,19 @@
 		<dependency>
 			<groupId>org.apache.ambari</groupId>
 			<artifactId>ambari-views</artifactId>
+			<scope>provided</scope>
 		</dependency>
 		<dependency>
 			<groupId>com.google.code.gson</groupId>
 			<artifactId>gson</artifactId>
 			<version>2.2.2</version>
 		</dependency>
+		<dependency>
+			<groupId>org.apache.ambari</groupId>
+			<artifactId>ambari-server</artifactId>
+			<version>${ambari.version}</version>
+			<scope>provided</scope>
+		</dependency>
 	</dependencies>
 
 	<repositories>
@@ -114,6 +121,7 @@
 		<nodejs.directory>${basedir}/target/nodejs</nodejs.directory>
 		<npm.version>1.4.3</npm.version>
 		<ui.directory>${basedir}/src/main/resources/ui</ui.directory>
+		<ambari.version>1.3.0-SNAPSHOT</ambari.version>
 	</properties>
 	<build>
 		<plugins>

+ 37 - 44
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/SliderAppsViewControllerImpl.java

@@ -23,10 +23,11 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.ambari.view.ViewContext;
-import org.apache.ambari.view.slider.rest.client.AmbariCluster;
-import org.apache.ambari.view.slider.rest.client.AmbariClusterInfo;
-import org.apache.ambari.view.slider.rest.client.AmbariHttpClient;
-import org.apache.ambari.view.slider.rest.client.AmbariService;
+import org.apache.ambari.view.slider.clients.AmbariClient;
+import org.apache.ambari.view.slider.clients.AmbariCluster;
+import org.apache.ambari.view.slider.clients.AmbariClusterInfo;
+import org.apache.ambari.view.slider.clients.AmbariServiceInfo;
+import org.apache.log4j.Logger;
 
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
@@ -34,63 +35,55 @@ import com.google.inject.Singleton;
 @Singleton
 public class SliderAppsViewControllerImpl implements SliderAppsViewController {
 
+	private static final Logger logger = Logger
+	    .getLogger(SliderAppsViewControllerImpl.class);
 	@Inject
 	private ViewContext viewContext;
-	private AmbariHttpClient ambariClient;
-
-	private AmbariHttpClient getAmbariClient() {
-		// TODO Calculate Ambari location dynamically
-		if (ambariClient == null)
-			ambariClient = new AmbariHttpClient("http://localhost:8080",
-			    viewContext.getUsername(), "admin");
-		return ambariClient;
-	}
+	@Inject
+	private AmbariClient ambariClient;
 
 	@Override
 	public ViewStatus getViewStatus() {
 		ViewStatus status = new ViewStatus();
 		List<String> viewErrors = new ArrayList<String>();
 
-		AmbariHttpClient client = getAmbariClient();
-		AmbariClusterInfo clusterInfo = client.getClusterInfo();
+		AmbariClusterInfo clusterInfo = ambariClient.getClusterInfo();
 		if (clusterInfo != null) {
-			AmbariCluster cluster = client.getCluster(clusterInfo);
-			List<String> services = cluster.getServices();
-			if (services != null && services.size() > 0) {
-				boolean zkFound = services.indexOf("ZOOKEEPER") > -1;
-				boolean hdfsFound = services.indexOf("HDFS") > -1;
-				boolean yarnFound = services.indexOf("YARN") > -1;
-				if (!hdfsFound) {
+			AmbariCluster cluster = ambariClient.getCluster(clusterInfo);
+			List<AmbariServiceInfo> services = cluster.getServices();
+			if (services != null && !services.isEmpty()) {
+				AmbariServiceInfo hdfsService = null, yarnService = null, zkService = null;
+				for (AmbariServiceInfo service : services) {
+					if ("HDFS".equals(service.getId())) {
+						hdfsService = service;
+					} else if ("YARN".equals(service.getId())) {
+						yarnService = service;
+					} else if ("ZOOKEEPER".equals(service.getId())) {
+						zkService = service;
+					}
+				}
+				if (hdfsService == null) {
 					viewErrors.add("Slider applications view requires HDFS service");
 				} else {
-					AmbariService service = client.getService(clusterInfo, "HDFS");
-					if (service != null) {
-						if (!service.isStarted()) {
-							viewErrors
-							    .add("Slider applications view requires HDFS service to be started");
-						}
+					if (!hdfsService.isStarted()) {
+						viewErrors
+						    .add("Slider applications view requires HDFS service to be started");
 					}
 				}
-				if (!yarnFound) {
+				if (yarnService == null) {
 					viewErrors.add("Slider applications view requires YARN service");
 				} else {
-					AmbariService service = client.getService(clusterInfo, "YARN");
-					if (service != null) {
-						if (!service.isStarted()) {
-							viewErrors
-							    .add("Slider applications view requires YARN service to be started");
-						}
+					if (!yarnService.isStarted()) {
+						viewErrors
+						    .add("Slider applications view requires YARN service to be started");
 					}
 				}
-				if (!zkFound) {
+				if (zkService == null) {
 					viewErrors.add("Slider applications view requires ZooKeeper service");
 				} else {
-					AmbariService service = client.getService(clusterInfo, "ZOOKEEPER");
-					if (service != null) {
-						if (!service.isStarted()) {
-							viewErrors
-							    .add("Slider applications view requires ZooKeeper service to be started");
-						}
+					if (!zkService.isStarted()) {
+						viewErrors
+						    .add("Slider applications view requires ZooKeeper service to be started");
 					}
 				}
 			} else {
@@ -100,8 +93,8 @@ public class SliderAppsViewControllerImpl implements SliderAppsViewController {
 			// Check security
 			if (cluster.getDesiredConfigs() != null
 			    && cluster.getDesiredConfigs().containsKey("global")) {
-				Map<String, String> globalConfig = client.getConfiguration(clusterInfo,
-				    "global", cluster.getDesiredConfigs().get("global"));
+				Map<String, String> globalConfig = ambariClient.getConfiguration(
+				    clusterInfo, "global", cluster.getDesiredConfigs().get("global"));
 				if (globalConfig != null
 				    && globalConfig.containsKey("security_enabled")) {
 					String securityValue = globalConfig.get("security_enabled");

+ 39 - 0
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/clients/AmbariClient.java

@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.slider.clients;
+
+import java.util.Map;
+
+import com.google.inject.ImplementedBy;
+
+@ImplementedBy(AmbariInternalClient.class)
+public interface AmbariClient {
+
+	public AmbariCluster getCluster(AmbariClusterInfo clusterInfo);
+
+	/**
+	 * Provides the first cluster defined on this Ambari server.
+	 * 
+	 * @return
+	 */
+	public AmbariClusterInfo getClusterInfo();
+
+	public Map<String, String> getConfiguration(AmbariClusterInfo cluster,
+	    String configType, String configTag);
+}

+ 7 - 7
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/AmbariCluster.java → contrib/views/slider/src/main/java/org/apache/ambari/view/slider/clients/AmbariCluster.java

@@ -16,15 +16,15 @@
  * limitations under the License.
  */
 
-package org.apache.ambari.view.slider.rest.client;
+package org.apache.ambari.view.slider.clients;
 
 import java.util.List;
 import java.util.Map;
 
 public class AmbariCluster extends AmbariClusterInfo {
 	private Map<String, String> desiredConfigs;
-	private List<String> services;
-	private List<String> hosts;
+	private List<AmbariServiceInfo> services;
+	private List<AmbariHostInfo> hosts;
 
 	public Map<String, String> getDesiredConfigs() {
 		return desiredConfigs;
@@ -34,19 +34,19 @@ public class AmbariCluster extends AmbariClusterInfo {
 		this.desiredConfigs = desiredConfigs;
 	}
 
-	public List<String> getServices() {
+	public List<AmbariServiceInfo> getServices() {
 		return services;
 	}
 
-	public void setServices(List<String> services) {
+	public void setServices(List<AmbariServiceInfo> services) {
 		this.services = services;
 	}
 
-	public List<String> getHosts() {
+	public List<AmbariHostInfo> getHosts() {
 		return hosts;
 	}
 
-	public void setHosts(List<String> hosts) {
+	public void setHosts(List<AmbariHostInfo> hosts) {
 		this.hosts = hosts;
 	}
 }

+ 1 - 1
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/AmbariClusterInfo.java → contrib/views/slider/src/main/java/org/apache/ambari/view/slider/clients/AmbariClusterInfo.java

@@ -16,7 +16,7 @@
  * limitations under the License.
  */
 
-package org.apache.ambari.view.slider.rest.client;
+package org.apache.ambari.view.slider.clients;
 
 public class AmbariClusterInfo {
 	private String name;

+ 32 - 0
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/clients/AmbariHostInfo.java

@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.slider.clients;
+
+public class AmbariHostInfo {
+
+	private String hostName;
+
+	public String getHostName() {
+		return hostName;
+	}
+
+	public void setHostName(String hostName) {
+		this.hostName = hostName;
+	}
+}

+ 209 - 0
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/clients/AmbariInternalClient.java

@@ -0,0 +1,209 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.slider.clients;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.ambari.server.controller.predicate.AndPredicate;
+import org.apache.ambari.server.controller.predicate.EqualsPredicate;
+import org.apache.ambari.server.controller.spi.ClusterController;
+import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
+import org.apache.ambari.server.controller.spi.NoSuchResourceException;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.spi.SystemException;
+import org.apache.ambari.server.controller.spi.UnsupportedPropertyException;
+import org.apache.ambari.server.controller.utilities.ClusterControllerHelper;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.apache.ambari.server.state.DesiredConfig;
+import org.apache.ambari.server.state.State;
+import org.apache.log4j.Logger;
+
+import com.google.inject.Singleton;
+
+@Singleton
+public class AmbariInternalClient implements AmbariClient {
+
+	private static final Logger logger = Logger
+	    .getLogger(AmbariInternalClient.class);
+
+	@Override
+	public AmbariCluster getCluster(AmbariClusterInfo clusterInfo) {
+		ClusterController clusterController = ClusterControllerHelper
+		    .getClusterController();
+		try {
+			EqualsPredicate<String> clusterPredicate = new EqualsPredicate<String>(
+			    "Clusters/cluster_name", clusterInfo.getName());
+			Set<Resource> clusterResources = clusterController.getResources(
+			    Resource.Type.Cluster, PropertyHelper.getReadRequest(),
+			    clusterPredicate);
+			if (!clusterResources.isEmpty()) {
+				Resource clusterResource = clusterResources.iterator().next();
+				AmbariCluster cluster = new AmbariCluster();
+				cluster.setName(clusterResource.getPropertyValue(
+				    "Clusters/cluster_name").toString());
+				cluster.setVersion(clusterResource.getPropertyValue("Clusters/version")
+				    .toString());
+				Map<String, String> desiredConfigsMap = new HashMap<String, String>();
+				Map<String, Object> desiredConfigsMapResource = clusterResource
+				    .getPropertiesMap().get("Clusters/desired_configs");
+				for (Map.Entry<String, Object> siteEntry : desiredConfigsMapResource
+				    .entrySet()) {
+					desiredConfigsMap.put(siteEntry.getKey(),
+					    ((DesiredConfig) siteEntry.getValue()).getVersion());
+				}
+				cluster.setDesiredConfigs(desiredConfigsMap);
+
+				EqualsPredicate<String> serviceClusterPredicate = new EqualsPredicate<String>(
+				    "ServiceInfo/cluster_name", cluster.getName());
+				EqualsPredicate<String> hostClusterPredicate = new EqualsPredicate<String>(
+				    "Hosts/cluster_name", cluster.getName());
+				Set<Resource> serviceResources = clusterController.getResources(
+				    Resource.Type.Service, PropertyHelper.getReadRequest(),
+				    serviceClusterPredicate);
+				Set<Resource> hostResources = clusterController.getResources(
+				    Resource.Type.Host, PropertyHelper.getReadRequest(),
+				    hostClusterPredicate);
+				List<AmbariServiceInfo> servicesList = new ArrayList<AmbariServiceInfo>();
+				List<AmbariHostInfo> hostsList = new ArrayList<AmbariHostInfo>();
+				for (Resource serviceResource : serviceResources) {
+					AmbariServiceInfo service = new AmbariServiceInfo();
+					service.setId(serviceResource.getPropertyValue(
+					    "ServiceInfo/service_name").toString());
+					service.setStarted(State.STARTED.toString().equals(
+					    serviceResource.getPropertyValue("ServiceInfo/state")));
+					servicesList.add(service);
+				}
+				for (Resource hostResource : hostResources) {
+					AmbariHostInfo host = new AmbariHostInfo();
+					host.setHostName(hostResource.getPropertyValue("Hosts/host_name")
+					    .toString());
+					hostsList.add(host);
+				}
+				cluster.setServices(servicesList);
+				cluster.setHosts(hostsList);
+				return cluster;
+			}
+		} catch (UnsupportedPropertyException e) {
+			logger.warn(
+			    "Unable to determine Ambari cluster details - "
+			        + clusterInfo.getName(), e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (NoSuchResourceException e) {
+			logger.warn(
+			    "Unable to determine Ambari cluster details - "
+			        + clusterInfo.getName(), e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (NoSuchParentResourceException e) {
+			logger.warn(
+			    "Unable to determine Ambari cluster details - "
+			        + clusterInfo.getName(), e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (SystemException e) {
+			logger.warn(
+			    "Unable to determine Ambari cluster details - "
+			        + clusterInfo.getName(), e);
+			throw new RuntimeException(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	@Override
+	public AmbariClusterInfo getClusterInfo() {
+		ClusterController clusterController = ClusterControllerHelper
+		    .getClusterController();
+		try {
+			Set<Resource> resources = clusterController.getResources(
+			    Resource.Type.Cluster, PropertyHelper.getReadRequest(), null);
+			if (resources.size() > 0) {
+				Resource clusterResource = resources.iterator().next();
+				AmbariClusterInfo clusterInfo = new AmbariClusterInfo();
+				clusterInfo.setName(clusterResource.getPropertyValue(
+				    "Clusters/cluster_name").toString());
+				clusterInfo.setVersion(clusterResource.getPropertyValue(
+				    "Clusters/version").toString());
+				return clusterInfo;
+			}
+		} catch (UnsupportedPropertyException e) {
+			logger.warn("Unable to determine Ambari cluster", e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (NoSuchResourceException e) {
+			logger.warn("Unable to determine Ambari cluster", e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (NoSuchParentResourceException e) {
+			logger.warn("Unable to determine Ambari cluster", e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (SystemException e) {
+			logger.warn("Unable to determine Ambari cluster", e);
+			throw new RuntimeException(e.getMessage(), e);
+		}
+		return null;
+	}
+
+	@Override
+	public Map<String, String> getConfiguration(AmbariClusterInfo cluster,
+	    String configType, String configTag) {
+		ClusterController clusterController = ClusterControllerHelper
+		    .getClusterController();
+		try {
+			EqualsPredicate<String> clusterPredicate = new EqualsPredicate<String>(
+			    "Config/cluster_name", cluster.getName());
+			EqualsPredicate<String> typePredicate = new EqualsPredicate<String>(
+			    "type", configType);
+			EqualsPredicate<String> tagPredicate = new EqualsPredicate<String>("tag",
+			    configTag);
+			AndPredicate typeTagPredicate = new AndPredicate(typePredicate,
+			    tagPredicate);
+			AndPredicate configsPredicate = new AndPredicate(clusterPredicate,
+			    typeTagPredicate);
+
+			Set<Resource> configResources = clusterController.getResources(
+			    Resource.Type.Configuration, PropertyHelper.getReadRequest(),
+			    configsPredicate);
+			if (!configResources.isEmpty()) {
+				Resource configResource = configResources.iterator().next();
+				Map<String, String> configs = new HashMap<String, String>();
+				Object props = configResource.getPropertiesMap().get("properties");
+				if (props instanceof Map) {
+					@SuppressWarnings("unchecked")
+					Map<String, String> propsMap = (Map<String, String>) props;
+					configs.putAll(propsMap);
+				}
+				return configs;
+			}
+		} catch (UnsupportedPropertyException e) {
+			logger.warn("Unable to determine Ambari cluster configuration", e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (NoSuchResourceException e) {
+			logger.warn("Unable to determine Ambari cluster configuration", e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (NoSuchParentResourceException e) {
+			logger.warn("Unable to determine Ambari cluster configuration", e);
+			throw new RuntimeException(e.getMessage(), e);
+		} catch (SystemException e) {
+			logger.warn("Unable to determine Ambari cluster configuration", e);
+			throw new RuntimeException(e.getMessage(), e);
+		}
+		return null;
+	}
+
+}

+ 2 - 2
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/AmbariService.java → contrib/views/slider/src/main/java/org/apache/ambari/view/slider/clients/AmbariServiceInfo.java

@@ -16,9 +16,9 @@
  * limitations under the License.
  */
 
-package org.apache.ambari.view.slider.rest.client;
+package org.apache.ambari.view.slider.clients;
 
-public class AmbariService {
+public class AmbariServiceInfo {
 
 	private String id;
 	private boolean started;

+ 8 - 41
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/AmbariHttpClient.java

@@ -24,6 +24,11 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.ambari.view.slider.clients.AmbariClient;
+import org.apache.ambari.view.slider.clients.AmbariCluster;
+import org.apache.ambari.view.slider.clients.AmbariClusterInfo;
+import org.apache.ambari.view.slider.clients.AmbariHostInfo;
+import org.apache.ambari.view.slider.clients.AmbariServiceInfo;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.log4j.Logger;
 
@@ -31,7 +36,7 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 
-public class AmbariHttpClient extends BaseHttpClient {
+public class AmbariHttpClient extends BaseHttpClient implements AmbariClient {
 
 	private static final Logger logger = Logger.getLogger(AmbariHttpClient.class);
 
@@ -86,22 +91,10 @@ public class AmbariHttpClient extends BaseHttpClient {
 					}
 					cluster.setDesiredConfigs(desiredConfigs);
 					// services
-					List<String> services = new ArrayList<String>();
-					JsonArray servicesArray = jsonElement.getAsJsonObject()
-					    .get("services").getAsJsonArray();
-					for (JsonElement entry : servicesArray) {
-						services.add(entry.getAsJsonObject().get("ServiceInfo")
-						    .getAsJsonObject().get("service_name").getAsString());
-					}
+					List<AmbariServiceInfo> services = new ArrayList<AmbariServiceInfo>();
 					cluster.setServices(services);
 					// hosts
-					List<String> hosts = new ArrayList<String>();
-					JsonArray hostsArray = jsonElement.getAsJsonObject().get("hosts")
-					    .getAsJsonArray();
-					for (JsonElement entry : hostsArray) {
-						hosts.add(entry.getAsJsonObject().get("Hosts").getAsJsonObject()
-						    .get("host_name").getAsString());
-					}
+					List<AmbariHostInfo> hosts = new ArrayList<AmbariHostInfo>();
 					cluster.setHosts(hosts);
 					return cluster;
 				}
@@ -118,32 +111,6 @@ public class AmbariHttpClient extends BaseHttpClient {
 		return null;
 	}
 
-	public AmbariService getService(AmbariClusterInfo clusterInfo,
-	    String serviceId) {
-		if (clusterInfo != null) {
-			try {
-				JsonElement jsonElement = doGetJson("/api/v1/clusters/"
-				    + clusterInfo.getName() + "/services/" + serviceId);
-				if (jsonElement != null) {
-					AmbariService service = new AmbariService();
-					String serviceState = jsonElement.getAsJsonObject()
-					    .get("ServiceInfo").getAsJsonObject().get("state").getAsString();
-					service.setStarted("STARTED".equals(serviceState));
-					return service;
-				}
-			} catch (HttpException e) {
-				logger.warn(
-				    "Unable to determine Ambari service details - " + serviceId, e);
-				throw new RuntimeException(e.getMessage(), e);
-			} catch (IOException e) {
-				logger.warn(
-				    "Unable to determine Ambari cluster details - " + serviceId, e);
-				throw new RuntimeException(e.getMessage(), e);
-			}
-		}
-		return null;
-	}
-
 	public Map<String, String> getConfiguration(AmbariClusterInfo cluster,
 	    String configType, String configTag) {
 		if (cluster != null && configType != null && configTag != null) {