소스 검색

AMBARI-17310. Pig View- Show actual error message thrown by webhcat api. (Gaurav Nagar via dipayanb)

Dipayan Bhowmick 9 년 전
부모
커밋
0d9020c95c

+ 11 - 2
ambari-server/src/main/java/org/apache/ambari/server/view/ViewAmbariStreamProvider.java

@@ -25,6 +25,8 @@ import org.apache.ambari.server.proxy.ProxyService;
 import org.apache.ambari.view.AmbariHttpException;
 import org.apache.ambari.view.AmbariStreamProvider;
 import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -53,6 +55,8 @@ public class ViewAmbariStreamProvider implements AmbariStreamProvider {
    */
   private final AmbariManagementController controller;
 
+  private static Logger LOG = LoggerFactory.getLogger(ViewAmbariStreamProvider.class);
+
 
   // ----- Constructor -----------------------------------------------------
 
@@ -122,8 +126,13 @@ public class ViewAmbariStreamProvider implements AmbariStreamProvider {
 
   private InputStream getInputStream(HttpURLConnection connection) throws IOException, AmbariHttpException {
     int responseCode = connection.getResponseCode();
-    if(responseCode >= ProxyService.HTTP_ERROR_RANGE_START){
-      throw new AmbariHttpException(IOUtils.toString(connection.getErrorStream()),responseCode);
+    if (responseCode >= ProxyService.HTTP_ERROR_RANGE_START) {
+      String message = connection.getResponseMessage();
+      if (connection.getErrorStream() != null) {
+        message = IOUtils.toString(connection.getErrorStream());
+      }
+      LOG.error("Got error response for url {}. Response code:{}. {}", connection.getURL(), responseCode, message);
+      throw new AmbariHttpException(message, responseCode);
     }
     return connection.getInputStream();
   }

+ 7 - 3
contrib/views/pig/src/main/java/org/apache/ambari/view/pig/resources/jobs/JobResourceManager.java

@@ -29,6 +29,7 @@ import org.apache.ambari.view.pig.templeton.client.TempletonApiFactory;
 import org.apache.ambari.view.pig.utils.MisconfigurationFormattedException;
 import org.apache.ambari.view.pig.utils.ServiceFormattedException;
 import org.apache.ambari.view.pig.utils.UserLocalObjects;
+import org.apache.ambari.view.utils.ambari.AmbariApiException;
 import org.apache.ambari.view.utils.hdfs.HdfsApiException;
 import org.apache.hadoop.fs.FSDataOutputStream;
 import org.slf4j.Logger;
@@ -233,14 +234,17 @@ public class JobResourceManager extends PersonalCRUDResourceManager<PigJob> {
     TempletonApi.JobData data;
     try {
       data = UserLocalObjects.getTempletonApi(context).runPigQuery(new File(job.getPigScript()), statusdir, job.getTempletonArguments());
+      if (data.id != null) {
+        job.setJobId(data.id);
+        JobPolling.pollJob(this, job);
+      } else {
+        throw new AmbariApiException("Cannot get id for the Job.");
+      }
     } catch (IOException templetonBadResponse) {
       String msg = String.format("Templeton bad response: %s", templetonBadResponse.toString());
       LOG.debug(msg);
       throw new ServiceFormattedException(msg, templetonBadResponse);
     }
-    job.setJobId(data.id);
-
-    JobPolling.pollJob(this, job);
   }
 
   /**

+ 23 - 4
contrib/views/pig/src/main/java/org/apache/ambari/view/pig/templeton/client/JSONRequest.java

@@ -21,14 +21,18 @@ package org.apache.ambari.view.pig.templeton.client;
 import com.google.gson.Gson;
 import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.core.util.MultivaluedMapImpl;
+import org.apache.ambari.view.AmbariHttpException;
 import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.utils.ambari.AmbariApiException;
 import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriBuilder;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.HttpURLConnection;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -262,17 +266,32 @@ public class JSONRequest<RESPONSE> {
   }
 
   public InputStream readFrom(WebResource resource, String method, String body, Map<String, String> headers) throws IOException {
-    InputStream inputStream;
+    HttpURLConnection connection;
     resource = resource.queryParam("user.name", username)
         .queryParam("doAs", doAs);
 
+    String path = resource.toString();
     if (doAs == null) {
-      inputStream = context.getURLStreamProvider().readFrom(resource.toString(),
+      connection = context.getURLConnectionProvider().getConnection(path,
           method, body, headers);
     } else {
-      inputStream = context.getURLStreamProvider().readAs(resource.toString(),
+      connection = context.getURLConnectionProvider().getConnectionAs(path,
           method, body, headers, doAs);
     }
-    return inputStream;
+    return getInputStream(connection);
   }
+
+  private InputStream getInputStream(HttpURLConnection connection) throws IOException {
+    int responseCode = connection.getResponseCode();
+    if (responseCode >= Response.Status.BAD_REQUEST.getStatusCode()) {
+      String message = connection.getResponseMessage();
+      if (connection.getErrorStream() != null) {
+        message = IOUtils.toString(connection.getErrorStream());
+      }
+      LOG.error("Got error response for url {}. Response code:{}. {}", connection.getURL(), responseCode, message);
+      throw new AmbariApiException(message);
+    }
+    return connection.getInputStream();
+  }
+
 }

+ 2 - 2
contrib/views/pig/src/main/java/org/apache/ambari/view/pig/utils/ServiceFormattedException.java

@@ -88,10 +88,10 @@ public class ServiceFormattedException extends WebApplicationException {
     response.put("trace", trace);
     response.put("status", status);
 
-    if(message != null) {
+    if(message != null && status != 404) {
       LOG.error(message);
     }
-    if(trace != null) {
+    if(trace != null && status != 404) {
       LOG.error(trace);
     }
 

+ 5 - 0
contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/JobTest.java

@@ -116,6 +116,7 @@ public class JobTest extends BasePigTest {
     TempletonApi api = createNiceMock(TempletonApi.class);
     UserLocalObjects.setTempletonApi(api, context);
     TempletonApi.JobData data = api.new JobData();
+    data.id = "job_1466418324742_0005";
     expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data);
     replay(api);
 
@@ -150,6 +151,7 @@ public class JobTest extends BasePigTest {
     TempletonApi api = createNiceMock(TempletonApi.class);
     UserLocalObjects.setTempletonApi(api, context);
     TempletonApi.JobData data = api.new JobData();
+    data.id = "job_1466418324742_0005";
     expect(api.runPigQuery((File) anyObject(), anyString(), (String) isNull())).andReturn(data).anyTimes();
     replay(api);
 
@@ -190,6 +192,7 @@ public class JobTest extends BasePigTest {
     TempletonApi api = createNiceMock(TempletonApi.class);
     UserLocalObjects.setTempletonApi(api, context);
     TempletonApi.JobData data = api.new JobData();
+    data.id = "job_1466418324742_0005";
     expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data);
     replay(api);
 
@@ -215,6 +218,7 @@ public class JobTest extends BasePigTest {
     TempletonApi api = createNiceMock(TempletonApi.class);
     UserLocalObjects.setTempletonApi(api, context);
     TempletonApi.JobData data = api.new JobData();
+    data.id = "job_1466418324742_0005";
     expect(api.runPigQuery((File) anyObject(), anyString(), (String) isNull())).andReturn(data);
     replay(api);
 
@@ -273,6 +277,7 @@ public class JobTest extends BasePigTest {
     TempletonApi api = createNiceMock(TempletonApi.class);
     UserLocalObjects.setTempletonApi(api, context);
     TempletonApi.JobData data = api.new JobData();
+    data.id = "job_1466418324742_0005";
     expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data);
     replay(api);