kylin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kangkai...@apache.org
Subject [45/50] [abbrv] kylin git commit: KYLIN-2814 code cleanup in RestClient
Date Mon, 04 Sep 2017 13:44:56 GMT
KYLIN-2814 code cleanup in RestClient


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

Branch: refs/heads/2622-2764
Commit: 2e4474e46cb563096635d0b749ee023e0fcb66d0
Parents: ae311b1
Author: Li Yang <liyang@apache.org>
Authored: Sun Sep 3 18:38:15 2017 +0800
Committer: Li Yang <liyang@apache.org>
Committed: Sun Sep 3 18:38:15 2017 +0800

----------------------------------------------------------------------
 .../kylin/common/restclient/RestClient.java     | 144 ++++++++++++-------
 1 file changed, 90 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/2e4474e4/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
----------------------------------------------------------------------
diff --git a/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
b/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
index b0ca5f7..95aa626 100644
--- a/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
+++ b/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
@@ -27,7 +27,8 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
+import javax.xml.bind.DatatypeConverter;
+
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.auth.AuthScope;
@@ -45,20 +46,17 @@ import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.util.EntityUtils;
 import org.apache.kylin.common.util.JsonUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import javax.xml.bind.DatatypeConverter;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 /**
  * @author yangli9
  */
 public class RestClient {
 
-    protected String host;
-    protected int port;
-    protected String baseUrl;
-    protected String userName;
-    protected String password;
-    protected DefaultHttpClient client;
+    private static final Logger logger = LoggerFactory.getLogger(RestClient.class);
 
     protected static Pattern fullRestPattern = Pattern.compile("(?:([^:]+)[:]([^@]+)[@])?([^:]+)(?:[:](\\d+))?");
 
@@ -74,9 +72,17 @@ public class RestClient {
         return m.matches();
     }
 
+    // ============================================================================
+
+    protected String host;
+    protected int port;
+    protected String baseUrl;
+    protected String userName;
+    protected String password;
+    protected DefaultHttpClient client;
+
     /**
-     * @param uri
-     *            "user:pwd@host:port"
+     * @param uri "user:pwd@host:port"
      */
     public RestClient(String uri) {
         Matcher m = fullRestPattern.matcher(uri);
@@ -127,32 +133,30 @@ public class RestClient {
 
             if (response.getStatusLine().getStatusCode() != 200) {
                 String msg = EntityUtils.toString(response.getEntity());
-                throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+ " with cache wipe url " + url + "\n" + msg);
+                throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+                        + " with cache wipe url " + url + "\n" + msg);
             }
-        } catch (Exception ex) {
-            throw new IOException(ex);
         } finally {
-            if (response != null) {
-                EntityUtils.consume(response.getEntity());
-            }
-            request.releaseConnection();
+            cleanup(request, response);
         }
     }
 
     public String getKylinProperties() throws IOException {
         String url = baseUrl + "/admin/config";
         HttpGet request = new HttpGet(url);
+        HttpResponse response = null;
         try {
-            HttpResponse response = client.execute(request);
+            response = client.execute(request);
             String msg = EntityUtils.toString(response.getEntity());
             Map<String, String> map = JsonUtil.readValueAsMap(msg);
             msg = map.get("config");
 
             if (response.getStatusLine().getStatusCode() != 200)
-                throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+ " with cache wipe url " + url + "\n" + msg);
+                throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+                        + " with cache wipe url " + url + "\n" + msg);
             return msg;
         } finally {
-            request.releaseConnection();
+            cleanup(request, response);
         }
     }
 
@@ -167,18 +171,24 @@ public class RestClient {
     public boolean buildCube(String cubeName, long startTime, long endTime, String buildType)
throws Exception {
         String url = baseUrl + "/cubes/" + cubeName + "/build";
         HttpPut put = newPut(url);
-        HashMap<String, String> paraMap = new HashMap<String, String>();
-        paraMap.put("startTime", startTime + "");
-        paraMap.put("endTime", endTime + "");
-        paraMap.put("buildType", buildType);
-        String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
-        put.setEntity(new StringEntity(jsonMsg, "UTF-8"));
-        HttpResponse response = client.execute(put);
-        String result = getContent(response);
-        if (response.getStatusLine().getStatusCode() != 200) {
-            throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+ " with build cube url " + url + "\n" + jsonMsg);
-        } else {
-            return true;
+        HttpResponse response = null;
+        try {
+            HashMap<String, String> paraMap = new HashMap<String, String>();
+            paraMap.put("startTime", startTime + "");
+            paraMap.put("endTime", endTime + "");
+            paraMap.put("buildType", buildType);
+            String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
+            put.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+            response = client.execute(put);
+            getContent(response);
+            if (response.getStatusLine().getStatusCode() != 200) {
+                throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+                        + " with build cube url " + url + "\n" + jsonMsg);
+            } else {
+                return true;
+            }
+        } finally {
+            cleanup(put, response);
         }
     }
 
@@ -197,22 +207,33 @@ public class RestClient {
     public HashMap getCube(String cubeName) throws Exception {
         String url = baseUrl + "/cubes/" + cubeName;
         HttpGet get = newGet(url);
-        get.setURI(new URI(url));
-        HttpResponse response = client.execute(get);
-        return dealResponse(response);
+        HttpResponse response = null;
+        try {
+            get.setURI(new URI(url));
+            response = client.execute(get);
+            return dealResponse(response);
+        } finally {
+            cleanup(get, response);
+        }
     }
 
     private boolean changeCubeStatus(String url) throws Exception {
         HttpPut put = newPut(url);
-        HashMap<String, String> paraMap = new HashMap<String, String>();
-        String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
-        put.setEntity(new StringEntity(jsonMsg, "UTF-8"));
-        HttpResponse response = client.execute(put);
-        String result = getContent(response);
-        if (response.getStatusLine().getStatusCode() != 200) {
-            throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+ " with url " + url + "\n" + jsonMsg);
-        } else {
-            return true;
+        HttpResponse response = null;
+        try {
+            HashMap<String, String> paraMap = new HashMap<String, String>();
+            String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
+            put.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+            response = client.execute(put);
+            getContent(response);
+            if (response.getStatusLine().getStatusCode() != 200) {
+                throw new IOException("Invalid response " + response.getStatusLine().getStatusCode()
+ " with url "
+                        + url + "\n" + jsonMsg);
+            } else {
+                return true;
+            }
+        } finally {
+            cleanup(put, response);
         }
     }
 
@@ -265,16 +286,21 @@ public class RestClient {
     private boolean setCache(boolean flag) throws IOException {
         String url = baseUrl + "/admin/config";
         HttpPut put = newPut(url);
-        HashMap<String, String> paraMap = new HashMap<String, String>();
-        paraMap.put("key", "kylin.query.cache-enabled");
-        paraMap.put("value", flag + "");
-        put.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(paraMap), "UTF-8"));
-        HttpResponse response = client.execute(put);
-        EntityUtils.consume(response.getEntity());
-        if (response.getStatusLine().getStatusCode() != 200) {
-            return false;
-        } else {
-            return true;
+        HttpResponse response = null;
+        try {
+            HashMap<String, String> paraMap = new HashMap<String, String>();
+            paraMap.put("key", "kylin.query.cache-enabled");
+            paraMap.put("value", flag + "");
+            put.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(paraMap),
"UTF-8"));
+            response = client.execute(put);
+            EntityUtils.consume(response.getEntity());
+            if (response.getStatusLine().getStatusCode() != 200) {
+                return false;
+            } else {
+                return true;
+            }
+        } finally {
+            cleanup(put, response);
         }
     }
 
@@ -296,4 +322,14 @@ public class RestClient {
         return result.toString();
     }
 
+    private void cleanup(HttpRequestBase request, HttpResponse response) {
+        try {
+            if (response != null)
+                EntityUtils.consume(response.getEntity());
+        } catch (Exception ex) {
+            logger.error("Error during HTTP connection cleanup", ex);
+        }
+        request.releaseConnection();
+    }
+
 }


Mime
View raw message