Repository: lens Updated Branches: refs/heads/master 84831297c -> 2da114407
LENS-967 : Wrap SavedQueryService methods by session acquire release blocks Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/2da11440 Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/2da11440 Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/2da11440 Branch: refs/heads/master Commit: 2da1144070986900ad0891890faa53786262c8a6 Parents: 8483129 Author: Rajat Khandelwal <pro...@apache.org> Authored: Sat Feb 27 22:42:13 2016 +0530 Committer: Amareshwari Sriramadasu <amareshw...@apache.org> Committed: Sat Feb 27 22:42:13 2016 +0530 ---------------------------------------------------------------------- .../api/query/save/SavedQueryService.java | 30 +++++--- .../server/query/save/SavedQueryResource.java | 38 ++-------- .../query/save/SavedQueryServiceImpl.java | 76 ++++++++++++++++---- .../query/save/TestSavedQueryService.java | 16 +++-- 4 files changed, 100 insertions(+), 60 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/2da11440/lens-server-api/src/main/java/org/apache/lens/server/api/query/save/SavedQueryService.java ---------------------------------------------------------------------- diff --git a/lens-server-api/src/main/java/org/apache/lens/server/api/query/save/SavedQueryService.java b/lens-server-api/src/main/java/org/apache/lens/server/api/query/save/SavedQueryService.java index cba1df6..c34217a 100644 --- a/lens-server-api/src/main/java/org/apache/lens/server/api/query/save/SavedQueryService.java +++ b/lens-server-api/src/main/java/org/apache/lens/server/api/query/save/SavedQueryService.java @@ -20,6 +20,7 @@ package org.apache.lens.server.api.query.save; import javax.ws.rs.core.MultivaluedMap; +import org.apache.lens.api.LensSessionHandle; import org.apache.lens.api.query.save.ListResponse; import org.apache.lens.api.query.save.SavedQuery; import org.apache.lens.server.api.error.LensException; @@ -37,58 +38,67 @@ public interface SavedQueryService { * @return id of the created saved query. * @throws LensException */ - long save(SavedQuery query) throws LensException; + long save(LensSessionHandle handle, SavedQuery query) throws LensException; /** * Updates the specified saved query with the new object. + * @param handle session handle of the query * @param id id of the saved query. * @param query Saved query object. * @throws LensException */ - void update(long id, SavedQuery query) throws LensException; + void update(LensSessionHandle handle, long id, SavedQuery query) throws LensException; /** * Deletes the saved query specified. - * @param id id of the saved query. + * @param handle session handle of the query + * @param id id of the saved query. * @throws LensException */ - void delete(long id) throws LensException; + void delete(LensSessionHandle handle, long id) throws LensException; /** * Returns the saved query pointed out by the id. - * @param id id of the saved query. + * @param handle session handle of the query + * @param id id of the saved query. * @return saved query object. * @throws LensException */ - SavedQuery get(long id) throws LensException; + SavedQuery get(LensSessionHandle handle, long id) throws LensException; /** * List the saved query from {start} to {count} matching filter denoted by criteria. + * @param handle session handle of the query * @param criteria Multivalued map representing the criteria. - * @param start Displacement from the first matching record. + * @param start Displacement from the first matching record. * @param count Number of records to fetch. * @return list of queries. * @throws LensException */ - ListResponse list(MultivaluedMap<String, String> criteria, long start, long count) throws LensException; + ListResponse list(LensSessionHandle handle, MultivaluedMap<String, String> criteria, long start, + long count) throws LensException; /** * Grant permissions for users to do actions on the saved query. + * @param handle session handle of the query * @param id id of the query. * @param sharingUser User invoking this action. * @param targetUserPath Target users who have to get affected. * @param privileges Privileges to be granted. * @throws LensException */ - void grant(long id, String sharingUser, String targetUserPath, String[] privileges) throws LensException; + void grant(LensSessionHandle handle, long id, String sharingUser, String targetUserPath, + String[] privileges) throws LensException; /** * Revoke permissions from users to do actions on the saved query. + * @param handle session handle of the query * @param id id of the query. * @param sharingUser User invoking this action. * @param targetUserPath Target users who have to get affected. * @param privileges Privileges to be granted. * @throws LensException */ - void revoke(long id, String sharingUser, String targetUserPath, String[] privileges) throws LensException; + void revoke(LensSessionHandle handle, long id, String sharingUser, String targetUserPath, + String[] privileges) throws LensException; } http://git-wip-us.apache.org/repos/asf/lens/blob/2da11440/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryResource.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryResource.java b/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryResource.java index 02541c5..6ee1e34 100644 --- a/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryResource.java +++ b/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryResource.java @@ -18,7 +18,6 @@ */ package org.apache.lens.server.query.save; -import static org.apache.lens.api.error.LensCommonErrorCode.INVALID_XML_ERROR; import static org.apache.lens.api.query.save.ResourceModifiedResponse.Action.CREATED; import static org.apache.lens.api.query.save.ResourceModifiedResponse.Action.DELETED; import static org.apache.lens.api.query.save.ResourceModifiedResponse.Action.UPDATED; @@ -41,9 +40,7 @@ import org.apache.lens.api.query.save.ParameterParserResponse; import org.apache.lens.api.query.save.ResourceModifiedResponse; import org.apache.lens.api.query.save.SavedQuery; import org.apache.lens.api.result.LensAPIResult; -import org.apache.lens.cube.parse.HQLParser; import org.apache.lens.server.LensServices; -import org.apache.lens.server.api.LensErrorInfo; import org.apache.lens.server.api.error.LensException; import org.apache.lens.server.api.query.QueryExecutionService; import org.apache.lens.server.api.query.save.*; @@ -56,7 +53,6 @@ import org.apache.hadoop.hive.conf.HiveConf; import org.glassfish.grizzly.http.server.Response; import org.glassfish.jersey.media.multipart.FormDataParam; -import lombok.NonNull; @Path("/queryapi") /** @@ -115,7 +111,7 @@ public class SavedQueryResource { @QueryParam("count") String count) throws LensException { try { final int countVal = count == null? defaultCount: Integer.parseInt(count); - return savedQueryService.list(info.getQueryParameters(), start, countVal); + return savedQueryService.list(sessionid, info.getQueryParameters(), start, countVal); } catch (LensException e) { throw getWrapped(e); } @@ -136,7 +132,7 @@ public class SavedQueryResource { @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("id") long id) throws LensException { try { - return savedQueryService.get(id); + return savedQueryService.get(sessionid, id); } catch (LensException e) { throw getWrapped(e); } @@ -157,7 +153,7 @@ public class SavedQueryResource { @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("id") long id) throws LensException { try { - savedQueryService.delete(id); + savedQueryService.delete(sessionid, id); return new ResourceModifiedResponse(id, "saved_query", DELETED); } catch (LensException e) { throw getWrapped(e); @@ -184,8 +180,7 @@ public class SavedQueryResource { @Context final Response response) throws LensException, IOException { try { - validateSampleResolved(savedQuery); - long id = savedQueryService.save(savedQuery); + long id = savedQueryService.save(sessionid, savedQuery); response.setStatus(HttpServletResponse.SC_CREATED); response.flush(); return new ResourceModifiedResponse(id, "saved_query", CREATED); @@ -214,8 +209,7 @@ public class SavedQueryResource { SavedQuery savedQuery, @Context final Response response) throws LensException, IOException { try { - validateSampleResolved(savedQuery); - savedQueryService.update(id, savedQuery); + savedQueryService.update(sessionid, id, savedQuery); response.setStatus(HttpServletResponse.SC_CREATED); response.flush(); return new ResourceModifiedResponse(id, "saved_query", UPDATED); @@ -260,7 +254,7 @@ public class SavedQueryResource { @FormDataParam("conf") LensConf conf) throws LensException { final String requestId = this.logSegregationContext.getLogSegragationId(); try { - final SavedQuery savedQuery = savedQueryService.get(id); + final SavedQuery savedQuery = savedQueryService.get(sessionid, id); final String query = ParameterResolver.resolve(savedQuery, info.getQueryParameters()); return LensAPIResult.composedOf( null, @@ -284,24 +278,4 @@ public class SavedQueryResource { e.buildLensErrorResponse(errorCollection, null, logSegregationContext.getLogSegragationId()); throw e; } - - /** - * Validates the saved query and throws LensException with. - * BAD_SYNTAX code if wrong - * - * @param savedQuery Saved query object - * @throws LensException if invalid - */ - private void validateSampleResolved(@NonNull SavedQuery savedQuery) throws LensException { - final String sampleResolved = SavedQueryHelper.getSampleResolvedQuery(savedQuery); - try { - HQLParser.parseHQL(sampleResolved, new HiveConf()); - } catch (Exception e) { - throw new LensException( - new LensErrorInfo(INVALID_XML_ERROR.getValue(), 0, INVALID_XML_ERROR.toString()) - , e - , "Encountered while resolving with sample values { " + sampleResolved + " }"); - } - } - } http://git-wip-us.apache.org/repos/asf/lens/blob/2da11440/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryServiceImpl.java ---------------------------------------------------------------------- diff --git a/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryServiceImpl.java b/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryServiceImpl.java index d9de897..e3ebca5 100644 --- a/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryServiceImpl.java +++ b/lens-server/src/main/java/org/apache/lens/server/query/save/SavedQueryServiceImpl.java @@ -18,14 +18,20 @@ */ package org.apache.lens.server.query.save; +import static org.apache.lens.api.error.LensCommonErrorCode.INVALID_XML_ERROR; + import javax.ws.rs.core.MultivaluedMap; +import org.apache.lens.api.LensSessionHandle; import org.apache.lens.api.query.save.ListResponse; import org.apache.lens.api.query.save.SavedQuery; +import org.apache.lens.cube.parse.HQLParser; import org.apache.lens.server.BaseLensService; import org.apache.lens.server.api.LensConfConstants; +import org.apache.lens.server.api.LensErrorInfo; import org.apache.lens.server.api.error.LensException; import org.apache.lens.server.api.health.HealthStatus; +import org.apache.lens.server.api.query.save.SavedQueryHelper; import org.apache.lens.server.api.query.save.SavedQueryService; import org.apache.lens.server.util.UtilityMethods; @@ -84,48 +90,75 @@ public class SavedQueryServiceImpl extends BaseLensService implements SavedQuery * {@inheritDoc} */ @Override - public long save(SavedQuery savedQuery) throws LensException { - return dao.saveQuery(savedQuery); + public long save(LensSessionHandle handle, SavedQuery savedQuery) throws LensException { + try { + acquire(handle); + validateSampleResolved(savedQuery); + return dao.saveQuery(savedQuery); + } finally { + release(handle); + } } /** * {@inheritDoc} */ @Override - public void update(long id, SavedQuery savedQuery) throws LensException { - dao.updateQuery(id, savedQuery); + public void update(LensSessionHandle handle, long id, SavedQuery savedQuery) throws LensException { + try { + acquire(handle); + validateSampleResolved(savedQuery); + dao.updateQuery(id, savedQuery); + } finally { + release(handle); + } } /** * {@inheritDoc} */ @Override - public void delete(long id) throws LensException { - dao.deleteSavedQueryByID(id); + public void delete(LensSessionHandle handle, long id) throws LensException { + try { + acquire(handle); + dao.deleteSavedQueryByID(id); + } finally { + release(handle); + } } /** * {@inheritDoc} */ @Override - public SavedQuery get(long id) throws LensException { - return dao.getSavedQueryByID(id); + public SavedQuery get(LensSessionHandle handle, long id) throws LensException { + try { + acquire(handle); + return dao.getSavedQueryByID(id); + } finally { + release(handle); + } } /** * {@inheritDoc} */ @Override - public ListResponse list( + public ListResponse list(LensSessionHandle handle, MultivaluedMap<String, String> criteria, long start, long count) throws LensException { - return dao.getList(criteria, start, count); + try { + acquire(handle); + return dao.getList(criteria, start, count); + } finally { + release(handle); + } } /** * {@inheritDoc} */ @Override - public void grant(long id, String sharingUser, String targetUserPath, String[] privileges) + public void grant(LensSessionHandle handle, long id, String sharingUser, String targetUserPath, String[] privileges) throws LensException { //NOOP } @@ -134,8 +167,27 @@ public class SavedQueryServiceImpl extends BaseLensService implements SavedQuery * {@inheritDoc} */ @Override - public void revoke(long id, String sharingUser, String targetUserPath, String[] privileges) + public void revoke(LensSessionHandle handle, long id, String sharingUser, String targetUserPath, String[] privileges) throws LensException { //NOOP } + /** + * Validates the saved query and throws LensException with. + * BAD_SYNTAX code if wrong + * + * @param savedQuery Saved query object + * @throws LensException if invalid + */ + private void validateSampleResolved(@NonNull SavedQuery savedQuery) throws LensException { + final String sampleResolved = SavedQueryHelper.getSampleResolvedQuery(savedQuery); + try { + HQLParser.parseHQL(sampleResolved, new HiveConf()); + } catch (Exception e) { + throw new LensException( + new LensErrorInfo(INVALID_XML_ERROR.getValue(), 0, INVALID_XML_ERROR.toString()) + , e + , "Encountered while resolving with sample values { " + sampleResolved + " }"); + } + } + } http://git-wip-us.apache.org/repos/asf/lens/blob/2da11440/lens-server/src/test/java/org/apache/lens/server/query/save/TestSavedQueryService.java ---------------------------------------------------------------------- diff --git a/lens-server/src/test/java/org/apache/lens/server/query/save/TestSavedQueryService.java b/lens-server/src/test/java/org/apache/lens/server/query/save/TestSavedQueryService.java index 70e2dbc..f9bacf1 100644 --- a/lens-server/src/test/java/org/apache/lens/server/query/save/TestSavedQueryService.java +++ b/lens-server/src/test/java/org/apache/lens/server/query/save/TestSavedQueryService.java @@ -42,6 +42,9 @@ import org.apache.lens.server.api.query.save.SavedQueryService; import org.apache.lens.server.error.LensExceptionMapper; import org.apache.lens.server.query.QueryExecutionServiceImpl; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.ql.session.SessionState; + import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; @@ -96,6 +99,7 @@ public class TestSavedQueryService extends LensJerseyTest { Map<String, String> sessionconf = Maps.newHashMap(); sessionconf.put("test.session.key", "svalue"); lensSessionId = queryService.openSession("foo", "bar", sessionconf); // @localhost should be removed + SessionState.start(new HiveConf()); } @AfterTest @@ -127,7 +131,7 @@ public class TestSavedQueryService extends LensJerseyTest { private ResourceModifiedResponse updateQuery(long id) { Response savedquery = savedQueriesRoot() - .path(String.valueOf(id)) + .path(String.valueOf(id)).queryParam("sessionid", lensSessionId) .request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .put(Entity.json(QUERY)); @@ -137,7 +141,7 @@ public class TestSavedQueryService extends LensJerseyTest { private ResourceModifiedResponse deleteQuery(long id) { Response savedquery = savedQueriesRoot() - .path(String.valueOf(id)) + .path(String.valueOf(id)).queryParam("sessionid", lensSessionId) .request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .delete(); @@ -147,7 +151,7 @@ public class TestSavedQueryService extends LensJerseyTest { private SavedQuery get(long id) { Response savedquery = savedQueriesRoot() - .path(String.valueOf(id)) + .path(String.valueOf(id)).queryParam("sessionid", lensSessionId) .request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .get(); @@ -157,7 +161,7 @@ public class TestSavedQueryService extends LensJerseyTest { private ParameterParserResponse extractParameters() { Response parameters = savedQueriesRoot() - .path("parameters") + .path("parameters").queryParam("sessionid", lensSessionId) .queryParam("query", QUERY_STRING) .request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) @@ -167,7 +171,7 @@ public class TestSavedQueryService extends LensJerseyTest { } private ResourceModifiedResponse saveQuery() { - Response savedquery = savedQueriesRoot() + Response savedquery = savedQueriesRoot().queryParam("sessionid", lensSessionId) .request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(QUERY)); @@ -178,7 +182,7 @@ public class TestSavedQueryService extends LensJerseyTest { private ListResponse list(long offset, long count) { Response savedquery = savedQueriesRoot() .queryParam("start", offset) - .queryParam("count", count) + .queryParam("count", count).queryParam("sessionid", lensSessionId) .request(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .get();