ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From srima...@apache.org
Subject ambari git commit: AMBARI-13612. Add RM and ATS Proxy Urls to Ambari Tez (DIPAYAN BHOWMICK via srimanth)
Date Fri, 30 Oct 2015 16:23:58 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 44baa24bf -> dd8bdd274


AMBARI-13612. Add RM and ATS Proxy Urls to Ambari Tez (DIPAYAN BHOWMICK via srimanth)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/dd8bdd27
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/dd8bdd27
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/dd8bdd27

Branch: refs/heads/trunk
Commit: dd8bdd2748b6ae276752b3601b64a5f36dade466
Parents: 44baa24
Author: Srimanth Gunturi <sgunturi@hortonworks.com>
Authored: Fri Oct 30 09:23:05 2015 -0700
Committer: Srimanth Gunturi <sgunturi@hortonworks.com>
Committed: Fri Oct 30 09:23:39 2015 -0700

----------------------------------------------------------------------
 .../apache/ambari/view/tez/ViewController.java  | 12 +++
 .../ambari/view/tez/ViewControllerImpl.java     | 20 ++--
 .../view/tez/exceptions/ProxyException.java     | 48 ++++++++++
 .../ambari/view/tez/rest/AtsProxyResource.java  | 47 ++++++++++
 .../ambari/view/tez/rest/BaseProxyResource.java | 57 ++++++++++++
 .../ambari/view/tez/rest/RMProxyResource.java   | 47 ++++++++++
 .../ambari/view/tez/utils/ProxyHelper.java      | 97 ++++++++++++++++++++
 contrib/views/tez/src/main/resources/view.xml   |  8 ++
 8 files changed, 329 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewController.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewController.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewController.java
index 70876db..16b470a 100644
--- a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewController.java
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewController.java
@@ -33,4 +33,16 @@ public interface ViewController {
    * @return Get the properties that any user is allowed to see, even non-admin users.
    */
   public ViewStatus getViewStatus();
+
+  /**
+   *
+   * @return The Active Application timeline server URL. Though, there is currently no
+   * HA in ATS, the ATS Url that is returned is considered as the Active one.
+   */
+  String getActiveATSUrl();
+
+  /**
+   * @return The active resource manager URL.
+   */
+  String getActiveRMUrl();
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewControllerImpl.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewControllerImpl.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewControllerImpl.java
index 3fad9e9..c1456bc 100644
--- a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewControllerImpl.java
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/ViewControllerImpl.java
@@ -42,7 +42,6 @@ public class ViewControllerImpl implements ViewController {
 
   private AmbariApi ambariApi;
 
-
   @Inject
   public ViewControllerImpl(ViewContext viewContext) {
     this.ambariApi = new AmbariApi(viewContext);
@@ -60,21 +59,28 @@ public class ViewControllerImpl implements ViewController {
 
     ViewStatus status = new ViewStatus();
     Map<String, String> parameters = new HashMap<String, String>();
+    parameters.put(ViewController.PARAM_YARN_ATS_URL, getActiveATSUrl());
+    parameters.put(ViewController.PARAM_YARN_RESOURCEMANAGER_URL, getActiveRMUrl());
+    status.setParameters(parameters);
+    return status;
+  }
+
+  @Override
+  public String getActiveATSUrl() {
     try {
-      parameters.put(ViewController.PARAM_YARN_ATS_URL, ambariApi.getServices().getTimelineServerUrl());
+      return ambariApi.getServices().getTimelineServerUrl();
     } catch (AmbariApiException ex) {
       throw new ATSUrlFetchException(ex);
     }
+  }
 
+  @Override
+  public String getActiveRMUrl() {
     try {
-      parameters.put(ViewController.PARAM_YARN_RESOURCEMANAGER_URL, ambariApi.getServices().getRMUrl());
+      return ambariApi.getServices().getRMUrl();
     } catch (AmbariApiException ex) {
       throw new ActiveRMFetchException(ex);
     }
-
-    status.setParameters(parameters);
-    return status;
   }
-
 }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/exceptions/ProxyException.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/exceptions/ProxyException.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/exceptions/ProxyException.java
new file mode 100644
index 0000000..aa006b2
--- /dev/null
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/exceptions/ProxyException.java
@@ -0,0 +1,48 @@
+/**
+ * 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.tez.exceptions;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Exception thrown by the proxy resources
+ */
+
+public class ProxyException extends WebApplicationException {
+
+  public ProxyException(String message, int status) {
+    this(message, status, null);
+  }
+
+  public ProxyException(String message, int status, String trace) {
+    super(toEntity(message, status, trace));
+  }
+
+  private static Response toEntity(String message, int status, String trace) {
+    Map<String, Object> json = new HashMap<>();
+    json.put("message", message);
+    json.put("status", status);
+    json.put("trace", trace);
+    return Response.status(status).entity(json).type(MediaType.APPLICATION_JSON).build();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
new file mode 100644
index 0000000..1dd822e
--- /dev/null
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/AtsProxyResource.java
@@ -0,0 +1,47 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.tez.rest;
+
+import com.google.inject.Inject;
+import org.apache.ambari.view.tez.ViewController;
+import org.apache.ambari.view.tez.utils.ProxyHelper;
+
+import javax.ws.rs.core.MultivaluedMap;
+
+/**
+ * Proxy class to query Application timeline server
+ */
+public class AtsProxyResource extends BaseProxyResource {
+
+  private ViewController viewController;
+  private ProxyHelper proxyHelper;
+
+  @Inject
+  public AtsProxyResource(ViewController viewController, ProxyHelper proxyHelper) {
+    super(proxyHelper);
+    this.viewController = viewController;
+    this.proxyHelper = proxyHelper;
+  }
+
+  @Override
+  public String getProxyUrl(String endpoint, MultivaluedMap<String, String> queryParams)
{
+    String atsUrl = viewController.getActiveATSUrl();
+    return String.format("%s/%s%s", atsUrl, endpoint, proxyHelper.getQueryParamsString(queryParams));
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/BaseProxyResource.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/BaseProxyResource.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/BaseProxyResource.java
new file mode 100644
index 0000000..3670a40
--- /dev/null
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/BaseProxyResource.java
@@ -0,0 +1,57 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.tez.rest;
+
+import com.google.inject.Inject;
+import org.apache.ambari.view.tez.utils.ProxyHelper;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.*;
+import java.util.HashMap;
+
+/**
+ * Base class for the proxy resources
+ */
+public abstract class BaseProxyResource {
+
+  private ProxyHelper proxyHelper;
+
+  @Inject
+  public BaseProxyResource(ProxyHelper proxyHelper) {
+    this.proxyHelper = proxyHelper;
+  }
+
+  @Path("/{endpoint:.+}")
+  @GET
+  @Produces({MediaType.APPLICATION_JSON})
+  public Response getData(@Context UriInfo uriInfo, @PathParam("endpoint") String endpoint)
{
+    String url = getProxyUrl(endpoint, uriInfo.getQueryParameters());
+    String response = proxyHelper.getResponse(url, new HashMap<String, String>());
+
+    JSONObject jsonObject = (JSONObject) JSONValue.parse(response);
+    return Response.ok(jsonObject).type(MediaType.APPLICATION_JSON).build();
+  }
+
+  public abstract String getProxyUrl(String endpoint, MultivaluedMap<String, String>
queryParams);
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
new file mode 100644
index 0000000..95a8705
--- /dev/null
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/rest/RMProxyResource.java
@@ -0,0 +1,47 @@
+/**
+ * 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.tez.rest;
+
+import com.google.inject.Inject;
+import org.apache.ambari.view.tez.ViewController;
+import org.apache.ambari.view.tez.utils.ProxyHelper;
+
+import javax.ws.rs.core.MultivaluedMap;
+
+/**
+ * Proxy class to query the Active RM server
+ */
+public class RMProxyResource extends BaseProxyResource {
+
+  private ViewController viewController;
+  private ProxyHelper proxyHelper;
+
+  @Inject
+  public RMProxyResource(ViewController viewController, ProxyHelper proxyHelper) {
+    super(proxyHelper);
+    this.viewController = viewController;
+    this.proxyHelper = proxyHelper;
+  }
+
+  @Override
+  public String getProxyUrl(String endpoint, MultivaluedMap<String, String> queryParams)
{
+    String activeRMUrl = viewController.getActiveRMUrl();
+    return String.format("%s/proxy/%s%s", activeRMUrl, endpoint, proxyHelper.getQueryParamsString(queryParams));
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
new file mode 100644
index 0000000..a471544
--- /dev/null
+++ b/contrib/views/tez/src/main/java/org/apache/ambari/view/tez/utils/ProxyHelper.java
@@ -0,0 +1,97 @@
+/**
+ * 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.tez.utils;
+
+
+import com.google.inject.Inject;
+import org.apache.ambari.view.URLConnectionProvider;
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.tez.exceptions.ProxyException;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.util.Map;
+import java.util.Set;
+
+public class ProxyHelper {
+
+  private ViewContext viewContext;
+
+  private static final Logger LOG = LoggerFactory.getLogger(ProxyHelper.class);
+
+  @Inject
+  public ProxyHelper(ViewContext viewContext) {
+    this.viewContext = viewContext;
+  }
+
+
+  public String getResponse(String url, Map<String, String> headers) {
+    LOG.debug("Fetching the result from the URL: {} using proxy", url);
+    InputStream inputStream = null;
+    try {
+      URLConnectionProvider provider = viewContext.getURLConnectionProvider();
+      HttpURLConnection connection = provider.getConnectionAsCurrent(url, "GET", (String)
null, headers);
+
+      if(connection.getResponseCode() != Response.Status.OK.getStatusCode()) {
+        LOG.error("Failure in fetching results for the URL: {}. Status: {}", url, connection.getResponseCode());
+        inputStream = connection.getErrorStream();
+        String trace = IOUtils.toString(inputStream);
+        throw new ProxyException("Failed to fetch results by the proxy from url: " + url,
connection.getResponseCode(), trace);
+      }
+
+      inputStream = connection.getInputStream();
+      return IOUtils.toString(inputStream);
+
+    } catch (IOException e) {
+      LOG.error("Cannot access the url: {}", url, e);
+      throw new ProxyException("Failed to fetch results by the proxy from url: " + url +
".Internal Error.",
+        Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
+    } finally {
+      if (inputStream != null) {
+        try {
+          inputStream.close();
+        } catch (IOException e) { /* Noting to do */ }
+      }
+    }
+  }
+
+  public String getQueryParamsString(MultivaluedMap<String, String> queryParameters)
{
+    Set<String> keySet = queryParameters.keySet();
+    StringBuilder builder = new StringBuilder();
+    if(keySet.size() > 0)
+      builder.append("?");
+
+    int count = 0;
+    for(String key: keySet) {
+      builder.append(key);
+      builder.append("=");
+      builder.append(queryParameters.getFirst(key));
+      if(count < keySet.size() - 1) {
+        builder.append("&");
+      }
+    }
+    return builder.toString();
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/dd8bdd27/contrib/views/tez/src/main/resources/view.xml
----------------------------------------------------------------------
diff --git a/contrib/views/tez/src/main/resources/view.xml b/contrib/views/tez/src/main/resources/view.xml
index 7d458b3..08f6f40 100644
--- a/contrib/views/tez/src/main/resources/view.xml
+++ b/contrib/views/tez/src/main/resources/view.xml
@@ -42,6 +42,14 @@ limitations under the License. Kerberos, LDAP, Custom. Binary/Htt
     <service-class>org.apache.ambari.view.tez.rest.ViewStatusResource</service-class>
   </resource>
   <resource>
+    <name>rmproxy</name>
+    <service-class>org.apache.ambari.view.tez.rest.RMProxyResource</service-class>
+  </resource>
+  <resource>
+    <name>atsproxy</name>
+    <service-class>org.apache.ambari.view.tez.rest.AtsProxyResource</service-class>
+  </resource>
+  <resource>
     <name>configurations</name>
     <service-class>org.apache.ambari.view.tez.ConfigurationService</service-class>
   </resource>


Mime
View raw message