Repository: lens Updated Branches: refs/heads/master 84678c5e3 -> 62cafa448
LENS-1010: Make LensClient implement java.lang.AutoClosable Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/62cafa44 Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/62cafa44 Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/62cafa44 Branch: refs/heads/master Commit: 62cafa4484ffaebc4f6376f50ca2900936c1a0fc Parents: 84678c5 Author: Rajat Khandelwal <pro...@apache.org> Authored: Tue May 24 18:41:20 2016 +0530 Committer: Rajat Khandelwal <rajatgupt...@gmail.com> Committed: Tue May 24 18:41:20 2016 +0530 ---------------------------------------------------------------------- .../java/org/apache/lens/client/LensClient.java | 10 ++- .../org/apache/lens/client/LensConnection.java | 68 +++++++++++--------- .../org/apache/lens/client/TestLensClient.java | 5 +- .../src/main/resources/olap-query-conf.xml | 9 +++ .../org/apache/lens/server/BaseLensService.java | 2 +- .../org/apache/lens/server/LensServices.java | 8 +++ .../server/metastore/MetastoreResource.java | 5 +- .../org/apache/lens/server/TestServerMode.java | 3 - src/site/apt/user/olap-query-conf.apt | 12 ++-- 9 files changed, 73 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-client/src/main/java/org/apache/lens/client/LensClient.java ---------------------------------------------------------------------- diff --git a/lens-client/src/main/java/org/apache/lens/client/LensClient.java b/lens-client/src/main/java/org/apache/lens/client/LensClient.java index 480b6f8..439ca90 100644 --- a/lens-client/src/main/java/org/apache/lens/client/LensClient.java +++ b/lens-client/src/main/java/org/apache/lens/client/LensClient.java @@ -53,7 +53,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j -public class LensClient { +public class LensClient implements AutoCloseable { public static final String CLILOGGER = "cliLogger"; private static final String DEFAULT_PASSWORD = ""; @Getter @@ -403,9 +403,9 @@ public class LensClient { return this.connection.getConnectionParams(key); } - public APIResult closeConnection() { + public void closeConnection() { log.debug("Closing lens connection: {}", new LensConnectionParams(conf)); - return this.connection.close(); + this.connection.close(); } public APIResult addJarResource(String path) { @@ -719,4 +719,8 @@ public class LensClient { return mc.dropSegmentation(segName); } + @Override + public void close() { + closeConnection(); + } } http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-client/src/main/java/org/apache/lens/client/LensConnection.java ---------------------------------------------------------------------- diff --git a/lens-client/src/main/java/org/apache/lens/client/LensConnection.java b/lens-client/src/main/java/org/apache/lens/client/LensConnection.java index 4951866..bb15b23 100644 --- a/lens-client/src/main/java/org/apache/lens/client/LensConnection.java +++ b/lens-client/src/main/java/org/apache/lens/client/LensConnection.java @@ -22,7 +22,6 @@ import static org.apache.lens.client.LensClientConfig.*; import java.net.ConnectException; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -39,6 +38,7 @@ import org.apache.lens.api.APIResult; import org.apache.lens.api.LensSessionHandle; import org.apache.lens.api.StringList; import org.apache.lens.api.util.MoxyJsonConfigurationContextResolver; +import org.apache.lens.client.exceptions.LensClientException; import org.apache.lens.client.exceptions.LensClientServerConnectionException; import org.glassfish.jersey.client.ClientProperties; @@ -55,7 +55,7 @@ import lombok.extern.slf4j.Slf4j; * Top level client connection class which is used to connect to a lens server. */ @Slf4j -public class LensConnection { +public class LensConnection implements AutoCloseable { /** The params. */ private final LensConnectionParams params; @@ -66,6 +66,7 @@ public class LensConnection { /** The session handle. */ @Getter private LensSessionHandle sessionHandle; + private boolean closed = false; /** * Construct a connection to lens server specified by connection parameters. @@ -119,9 +120,8 @@ public class LensConnection { public Client buildClient() { ClientBuilder cb = ClientBuilder.newBuilder().register(MultiPartFeature.class).register(MoxyJsonFeature.class) .register(MoxyJsonConfigurationContextResolver.class); - Iterator<Class<?>> itr = params.getRequestFilters().iterator(); - while (itr.hasNext()) { - cb.register(itr.next()); + for (Class<?> aClass : params.getRequestFilters()) { + cb.register(aClass); } Client client = cb.build(); @@ -200,28 +200,43 @@ public class LensConnection { */ public APIResult attachDatabaseToSession() { WebTarget target = getMetastoreWebTarget(); - APIResult result = target.path("databases").path("current").queryParam("sessionid", this.sessionHandle) + return target.path("databases").path("current").queryParam("sessionid", this.sessionHandle) .request(MediaType.APPLICATION_XML_TYPE).put(Entity.xml(params.getDbName()), APIResult.class); - return result; } /** - * Close. - * - * @return the API result + * Close the connection. */ - public APIResult close() { + @Override + public synchronized void close() { + if (closed) { + log.warn("Session already closed. Ignoring the attempt to close again."); + return; + } WebTarget target = getSessionWebTarget(); - - APIResult result = target.queryParam("sessionid", this.sessionHandle).request().delete(APIResult.class); - if (result.getStatus() != APIResult.Status.SUCCEEDED) { - throw new IllegalStateException("Unable to close lens connection " + "with params " + params); + Response response = target.queryParam("sessionid", this.sessionHandle).request().delete(); + if (response == null) { + // Should never come here, just fool-proofing + throw new LensClientException("Null response from server while closing connection."); + } + switch(response.getStatus()){ + case 410: + log.warn("Session is already gone. Ignoring the attempt to close again."); + break; + case 200: + APIResult apiResult = response.readEntity(APIResult.class); + if (apiResult.getStatus() != APIResult.Status.SUCCEEDED) { + throw new LensClientException("Error closing lens connection: " + apiResult.getMessage()); + } + break; + default: + throw new LensClientException("Couldn't close session, error code: " + response.getStatus()); } - log.debug("Lens connection closed."); - return result; + closed = true; } + /** * Adds the resource to connection. * @@ -236,9 +251,8 @@ public class LensConnection { MediaType.APPLICATION_XML_TYPE)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("type").build(), type)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("path").build(), resourcePath)); - APIResult result = target.path("resources/add").request() + return target.path("resources/add").request() .put(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), APIResult.class); - return result; } /** @@ -255,9 +269,8 @@ public class LensConnection { MediaType.APPLICATION_XML_TYPE)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("type").build(), type)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("path").build(), resourcePath)); - APIResult result = target.path("resources/delete").request() + return target.path("resources/delete").request() .put(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), APIResult.class); - return result; } /** @@ -280,8 +293,7 @@ public class LensConnection { */ public Response getLogs(String logFile) { WebTarget target = getLogWebTarget(); - Response result = target.path(logFile).request(MediaType.APPLICATION_OCTET_STREAM).get(); - return result; + return target.path(logFile).request(MediaType.APPLICATION_OCTET_STREAM).get(); } /** @@ -299,9 +311,8 @@ public class LensConnection { mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("key").build(), key)); mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("value").build(), value)); log.debug("Setting connection params {}={}", key, value); - APIResult result = target.path("params").request() + return target.path("params").request() .put(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), APIResult.class); - return result; } public List<String> getConnectionParams() { @@ -326,7 +337,7 @@ public class LensConnection { public Map<String, String> getConnectionParamsAsMap() { List<String> params = getConnectionParams(); - Map<String, String> paramsMap = new HashMap<String, String>(params.size()); + Map<String, String> paramsMap = new HashMap<>(params.size()); String[] paramKeyAndValue; for (String param : params) { paramKeyAndValue = param.split("="); @@ -348,9 +359,6 @@ public class LensConnection { */ @Override public String toString() { - final StringBuilder sb = new StringBuilder("LensConnection{"); - sb.append("sessionHandle=").append(sessionHandle.getPublicId()); - sb.append('}'); - return sb.toString(); + return "LensConnection{" + "sessionHandle=" + sessionHandle.getPublicId() + '}'; } } http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java ---------------------------------------------------------------------- diff --git a/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java b/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java index 5cf7417..c49b5e8 100644 --- a/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java +++ b/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java @@ -130,8 +130,9 @@ public class TestLensClient extends LensAllApplicationJerseyTest { result = client.dropDatabase(TEST_DB, true); assertEquals(result.getStatus(), APIResult.Status.SUCCEEDED); - result = client.closeConnection(); - assertEquals(result.getStatus(), APIResult.Status.SUCCEEDED); + client.close(); + // Multiple close should be fine, no exceptions. + client.close(); super.tearDown(); } http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-cube/src/main/resources/olap-query-conf.xml ---------------------------------------------------------------------- diff --git a/lens-cube/src/main/resources/olap-query-conf.xml b/lens-cube/src/main/resources/olap-query-conf.xml index 6e9933e..0c888ca 100644 --- a/lens-cube/src/main/resources/olap-query-conf.xml +++ b/lens-cube/src/main/resources/olap-query-conf.xml @@ -229,4 +229,13 @@ applied on aggregated field. If set to false, aggregate will applied on top of the expression i.e. sub_str. </description> </property> + <property> + <name>lens.cube.query.rewrite.dim.filter.to.fact.filter</name> + <value>false</value> + <description>Flag specifies if dimension filter has to be rewritten as fact filter. for eg. + where dim.name in ('x', 'y') + will become + where fact.dimid in (select dim.id from dim where dim.name in ('x','y')) + </description> + </property> </configuration> http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-server/src/main/java/org/apache/lens/server/BaseLensService.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/BaseLensService.java b/lens-server/src/main/java/org/apache/lens/server/BaseLensService.java index 76c8d16..b96cdf0 100644 --- a/lens-server/src/main/java/org/apache/lens/server/BaseLensService.java +++ b/lens-server/src/main/java/org/apache/lens/server/BaseLensService.java @@ -295,7 +295,7 @@ public abstract class BaseLensService extends CompositeService implements Extern String publicId = sessionHandle.getPublicId().toString(); SESSION_MAP.remove(publicId); decrementSessionCountForUser(sessionHandle, userName); - } catch (Exception e) { + } catch (HiveSQLException e) { throw new LensException(e); } } http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-server/src/main/java/org/apache/lens/server/LensServices.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/LensServices.java b/lens-server/src/main/java/org/apache/lens/server/LensServices.java index 46bae24..10ec8b7 100644 --- a/lens-server/src/main/java/org/apache/lens/server/LensServices.java +++ b/lens-server/src/main/java/org/apache/lens/server/LensServices.java @@ -28,6 +28,7 @@ import java.util.concurrent.*; import org.apache.lens.api.error.ErrorCollection; import org.apache.lens.api.error.ErrorCollectionFactory; import org.apache.lens.server.api.ServiceProvider; +import org.apache.lens.server.api.error.LensException; import org.apache.lens.server.api.events.LensEventService; import org.apache.lens.server.api.metrics.MetricsService; import org.apache.lens.server.metrics.MetricsServiceImpl; @@ -140,6 +141,13 @@ public class LensServices extends CompositeService implements ServiceProvider { return getMetricService().getCounter(LensServices.class, counter); } + public static LensException processLensException(LensException exc) { + if (exc != null) { + exc.buildLensErrorTO(get().getErrorCollection()); + } + return exc; + } + /** * The Enum SERVICE_MODE. */ http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-server/src/main/java/org/apache/lens/server/metastore/MetastoreResource.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/metastore/MetastoreResource.java b/lens-server/src/main/java/org/apache/lens/server/metastore/MetastoreResource.java index 6f10d58..4a6d779 100644 --- a/lens-server/src/main/java/org/apache/lens/server/metastore/MetastoreResource.java +++ b/lens-server/src/main/java/org/apache/lens/server/metastore/MetastoreResource.java @@ -78,10 +78,7 @@ public class MetastoreResource { } private static LensException processLensException(LensException exc) { - if (exc != null) { - exc.buildLensErrorTO(LensServices.get().getErrorCollection()); - } - return exc; + return LensServices.processLensException(exc); } public enum Entity { http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/lens-server/src/test/java/org/apache/lens/server/TestServerMode.java ---------------------------------------------------------------------- diff --git a/lens-server/src/test/java/org/apache/lens/server/TestServerMode.java b/lens-server/src/test/java/org/apache/lens/server/TestServerMode.java index fce6e5f..caf968a 100644 --- a/lens-server/src/test/java/org/apache/lens/server/TestServerMode.java +++ b/lens-server/src/test/java/org/apache/lens/server/TestServerMode.java @@ -53,8 +53,6 @@ import org.testng.annotations.Test; @Test(alwaysRun = true, groups = "filter-test", dependsOnGroups = "restart-test") public class TestServerMode extends LensAllApplicationJerseyTest { - private LensSessionHandle lensSessionHandle; - /* * (non-Javadoc) * @@ -74,7 +72,6 @@ public class TestServerMode extends LensAllApplicationJerseyTest { */ @AfterTest public void tearDown() throws Exception { - RestAPITestUtil.closeSession(target(), lensSessionHandle, defaultMT); super.tearDown(); } http://git-wip-us.apache.org/repos/asf/lens/blob/62cafa44/src/site/apt/user/olap-query-conf.apt ---------------------------------------------------------------------- diff --git a/src/site/apt/user/olap-query-conf.apt b/src/site/apt/user/olap-query-conf.apt index 8533ca7..ce35ec4 100644 --- a/src/site/apt/user/olap-query-conf.apt +++ b/src/site/apt/user/olap-query-conf.apt @@ -68,16 +68,16 @@ OLAP query configuration *--+--+---+--+ |20|lens.cube.query.replace.timedim|true|Tells whether timedim attribute queried in the time range should be replaced with its corresponding partition column name.| *--+--+---+--+ -|21|lens.cube.query.time.range.writer.class|org.apache.lens.cube.parse.ORTimeRangeWriter|The timerange writer class which specifies how the resolved partitions in timeranges should be written in final query. Available writers are org.apache.lens.cube.parse.ORTimeRangeWriter and org.apache.lens.cube.parse.BetweenTimeRangeWriter| +|21|lens.cube.query.rewrite.dim.filter.to.fact.filter|false|Flag specifies if dimension filter has to be rewritten as fact filter. for eg. where dim.name in ('x', 'y') will become where fact.dimid in (select dim.id from dim where dim.name in ('x','y'))| *--+--+---+--+ -|22|lens.cube.query.valid.${cubename}.facttables| |List of comma separated fact tables that are valid for cube. If no value is specified, all fact tables are valid| +|22|lens.cube.query.time.range.writer.class|org.apache.lens.cube.parse.ORTimeRangeWriter|The timerange writer class which specifies how the resolved partitions in timeranges should be written in final query. Available writers are org.apache.lens.cube.parse.ORTimeRangeWriter and org.apache.lens.cube.parse.BetweenTimeRangeWriter| *--+--+---+--+ -|23|lens.cube.query.valid.dim.storgaetables| |List of comma separated dimension storage tables that are valid. If no value is specified, all tables are valid| +|23|lens.cube.query.valid.${cubename}.facttables| |List of comma separated fact tables that are valid for cube. If no value is specified, all fact tables are valid| *--+--+---+--+ -|24|lens.cube.query.valid.fact.${facttable}.storage.${storagename}.updateperiods| |List of comma separated update periods that are valid for a fact on a storage. If no value is specified, all update periods are valid| +|24|lens.cube.query.valid.dim.storgaetables| |List of comma separated dimension storage tables that are valid. If no value is specified, all tables are valid| *--+--+---+--+ -|25|lens.cube.query.valid.fact.${facttable}.storagetables| |List of comma separated storage tables that are valid for a fact. If no value is specified, all storage tables are valid| +|25|lens.cube.query.valid.fact.${facttable}.storage.${storagename}.updateperiods| |List of comma separated update periods that are valid for a fact on a storage. If no value is specified, all update periods are valid| *--+--+---+--+ -|26|lens.cube.query.rewrite.dim.filter.to.fact.filter|false|Falg specifies if dimension filter has to be rewritten as fact filter. for eg. where dim.name in ('x', 'y') will become where fact.dimid in (select dim.id from dim where dim.name in ('x','y'))| +|26|lens.cube.query.valid.fact.${facttable}.storagetables| |List of comma separated storage tables that are valid for a fact. If no value is specified, all storage tables are valid| *--+--+---+--+ The configuration parameters and their default values