You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by bi...@apache.org on 2017/08/31 07:38:47 UTC
[4/4] kylin git commit: KYLIN-2814 close response for RestClient's
request
KYLIN-2814 close response for RestClient's request
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/3f80488e
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/3f80488e
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/3f80488e
Branch: refs/heads/KYLIN-2814
Commit: 3f80488e35d6c9b971833d64257040b938b689fd
Parents: 5c1aab1
Author: Billy Liu <bi...@apache.org>
Authored: Sun Aug 27 12:47:33 2017 +0800
Committer: Billy Liu <bi...@apache.org>
Committed: Thu Aug 31 15:38:31 2017 +0800
----------------------------------------------------------------------
.../kylin/common/restclient/RestClient.java | 112 +++--------------
.../kylin/restclient/ITRestClientTest.java | 120 ++++++++++++++++++-
2 files changed, 135 insertions(+), 97 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/kylin/blob/3f80488e/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 efd0438..6a89b9d 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
@@ -37,7 +37,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.entity.StringEntity;
+import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
@@ -129,12 +129,8 @@ public class RestClient {
String msg = EntityUtils.toString(response.getEntity());
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());
- }
+ HttpClientUtils.closeQuietly(response);
request.releaseConnection();
}
}
@@ -142,8 +138,10 @@ public class RestClient {
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");
@@ -152,83 +150,27 @@ public class RestClient {
throw new IOException("Invalid response " + response.getStatusLine().getStatusCode() + " with cache wipe url " + url + "\n" + msg);
return msg;
} finally {
+ HttpClientUtils.closeQuietly(response);
request.releaseConnection();
}
}
- public boolean enableCache() throws IOException {
- return setCache(true);
- }
-
- public boolean disableCache() throws IOException {
- return setCache(false);
- }
-
- 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;
- }
- }
-
- public boolean disableCube(String cubeName) throws Exception {
- return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/disable");
- }
-
- public boolean enableCube(String cubeName) throws Exception {
- return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/enable");
- }
-
- public boolean purgeCube(String cubeName) throws Exception {
- return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/purge");
- }
-
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);
- }
-
- 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 {
+ response = client.execute(get);
+ return dealResponse(response);
+ } finally {
+ HttpClientUtils.closeQuietly(response);
+ get.releaseConnection();
}
- }
- public HttpResponse query(String sql, String project) throws IOException {
- String url = baseUrl + "/query";
- HttpPost post = newPost(url);
- HashMap<String, String> paraMap = new HashMap<String, String>();
- paraMap.put("sql", sql);
- paraMap.put("project", project);
- String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
- post.setEntity(new StringEntity(jsonMsg, "UTF-8"));
- HttpResponse response = client.execute(post);
- return response;
}
- private HashMap dealResponse(HttpResponse response) throws IOException {
+ protected HashMap dealResponse(HttpResponse response) throws IOException {
if (response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Invalid response " + response.getStatusLine().getStatusCode());
}
@@ -237,48 +179,32 @@ public class RestClient {
return resultMap;
}
- private void addHttpHeaders(HttpRequestBase method) {
+ protected void addHttpHeaders(HttpRequestBase method) {
method.addHeader("Accept", "application/json, text/plain, */*");
method.addHeader("Content-Type", "application/json");
String basicAuth = DatatypeConverter.printBase64Binary((this.userName + ":" + this.password).getBytes());
method.addHeader("Authorization", "Basic " + basicAuth);
}
- private HttpPost newPost(String url) {
+ protected HttpPost newPost(String url) {
HttpPost post = new HttpPost(url);
addHttpHeaders(post);
return post;
}
- private HttpPut newPut(String url) {
+ protected HttpPut newPut(String url) {
HttpPut put = new HttpPut(url);
addHttpHeaders(put);
return put;
}
- private HttpGet newGet(String url) {
+ protected HttpGet newGet(String url) {
HttpGet get = new HttpGet();
addHttpHeaders(get);
return get;
}
- 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;
- }
- }
-
- private String getContent(HttpResponse response) throws IOException {
+ protected String getContent(HttpResponse response) throws IOException {
InputStreamReader reader = null;
BufferedReader rd = null;
StringBuffer result = new StringBuffer();
http://git-wip-us.apache.org/repos/asf/kylin/blob/3f80488e/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java b/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
index c97f08b..c4ac763 100644
--- a/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
+++ b/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
@@ -22,11 +22,18 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.io.IOException;
import java.util.HashMap;
import java.util.Random;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.utils.HttpClientUtils;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
import org.apache.kylin.common.restclient.RestClient;
import org.apache.kylin.common.util.HBaseMetadataTestCase;
import org.eclipse.jetty.server.Server;
@@ -77,28 +84,28 @@ public class ITRestClientTest extends HBaseMetadataTestCase {
@Test
public void testGetCube() throws Exception {
- RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+ ITRestClient client = new ITRestClient(HOST, PORT, USERNAME, PASSWD);
HashMap result = client.getCube(CUBE_NAME);
assertEquals("READY", result.get("status"));
}
@Test
public void testChangeCubeStatus() throws Exception {
- RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+ ITRestClient client = new ITRestClient(HOST, PORT, USERNAME, PASSWD);
assertTrue(client.disableCube(CUBE_NAME));
assertTrue(client.enableCube(CUBE_NAME));
}
@Test
public void testChangeCache() throws Exception {
- RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+ ITRestClient client = new ITRestClient(HOST, PORT, USERNAME, PASSWD);
assertTrue(client.disableCache());
assertTrue(client.enableCache());
}
@Test
public void testQuery() throws Exception {
- RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+ ITRestClient client = new ITRestClient(HOST, PORT, USERNAME, PASSWD);
String sql = "select count(*) from TEST_KYLIN_FACT; ";
HttpResponse result = client.query(sql, PROJECT_NAME);
}
@@ -148,4 +155,109 @@ public class ITRestClientTest extends HBaseMetadataTestCase {
backup.clear();
}
}
+
+ public class ITRestClient extends RestClient{
+
+ public ITRestClient(String uri) {
+ super(uri);
+ }
+
+ public ITRestClient(String host, int port, String username, String passwd) {
+ super(host, port, username, passwd);
+ }
+
+ boolean enableCache() throws IOException {
+ return setCache(true);
+ }
+
+ boolean disableCache() throws IOException {
+ return setCache(false);
+ }
+
+ boolean disableCube(String cubeName) throws Exception {
+ return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/disable");
+ }
+
+ boolean enableCube(String cubeName) throws Exception {
+ return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/enable");
+ }
+
+ boolean purgeCube(String cubeName) throws Exception {
+ return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/purge");
+ }
+
+ 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 = null;
+
+ try {
+ 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;
+ }
+ } finally {
+ HttpClientUtils.closeQuietly(response);
+ put.releaseConnection();
+ }
+ }
+
+ 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;
+ }
+ }
+
+ boolean buildCube(String cubeName, long startTime, long endTime, String buildType) throws Exception {
+ String url = baseUrl + "/cubes/" + cubeName + "/build";
+ HttpPut request = 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);
+ request.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+
+ HttpResponse response = null;
+ try {
+ response = client.execute(request);
+ 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;
+ }
+ } finally {
+ HttpClientUtils.closeQuietly(response);
+ request.releaseConnection();
+ }
+ }
+
+ public HttpResponse query(String sql, String project) throws IOException {
+ String url = baseUrl + "/query";
+ HttpPost post = newPost(url);
+ HashMap<String, String> paraMap = new HashMap<String, String>();
+ paraMap.put("sql", sql);
+ paraMap.put("project", project);
+ String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
+ post.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+ HttpResponse response = client.execute(post);
+ return response;
+ }
+ }
}