You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by am...@apache.org on 2015/02/17 06:58:17 UTC

incubator-lens git commit: LENS-297 : Accept db to be set as param of open session call (Jaideep Dhok via amareshwari)

Repository: incubator-lens
Updated Branches:
  refs/heads/master 971daa468 -> ea9c83c92


LENS-297 : Accept db to be set as param of open session call (Jaideep Dhok via amareshwari)


Project: http://git-wip-us.apache.org/repos/asf/incubator-lens/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-lens/commit/ea9c83c9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-lens/tree/ea9c83c9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-lens/diff/ea9c83c9

Branch: refs/heads/master
Commit: ea9c83c92820c5fb37845b580bf1e55b9c7a3de1
Parents: 971daa4
Author: Amareshwari Sriramadasu <am...@apache.org>
Authored: Tue Feb 17 11:28:06 2015 +0530
Committer: Amareshwari Sriramadasu <am...@apache.org>
Committed: Tue Feb 17 11:28:06 2015 +0530

----------------------------------------------------------------------
 .../org/apache/lens/client/LensConnection.java  |  9 ++--
 .../org/apache/lens/client/TestLensClient.java  | 24 +++++++--
 .../lens/server/api/session/SessionService.java |  6 ++-
 .../lens/server/session/HiveSessionService.java | 21 +++++++-
 .../lens/server/session/SessionResource.java    |  7 ++-
 .../lens/server/ui/SessionUIResource.java       |  7 ++-
 .../server/session/TestSessionResource.java     | 51 ++++++++++++++++++++
 7 files changed, 109 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/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 2458620..61e16f7 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
@@ -134,6 +134,10 @@ public class LensConnection {
     FormDataMultiPart mp = new FormDataMultiPart();
     mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("username").build(), params.getUser()));
     mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("password").build(), password));
+
+    String database = params.getDbName();
+    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("database").build(), database));
+
     mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionconf").fileName("sessionconf").build(),
       params.getSessionConf(), MediaType.APPLICATION_XML_TYPE));
     try {
@@ -154,12 +158,7 @@ public class LensConnection {
       }
     }
 
-    APIResult result = attachDatabaseToSession();
     LOG.debug("Successfully switched to database " + params.getDbName());
-    if (result.getStatus() != APIResult.Status.SUCCEEDED) {
-      throw new IllegalStateException("Unable to connect to lens database " + params.getDbName());
-    }
-
     open.set(true);
 
     return sessionHandle;

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/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 1563104..a6f25cb 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
@@ -26,6 +26,10 @@ import javax.ws.rs.core.UriBuilder;
 import org.apache.lens.server.LensAllApplicationJerseyTest;
 import org.apache.lens.server.api.LensConfConstants;
 
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.ql.metadata.Hive;
+
 import org.testng.Assert;
 import org.testng.annotations.AfterTest;
 import org.testng.annotations.BeforeTest;
@@ -33,6 +37,7 @@ import org.testng.annotations.Test;
 
 @Test(groups = "unit-test")
 public class TestLensClient extends LensAllApplicationJerseyTest {
+  private static final String TEST_DB = TestLensClient.class.getSimpleName();
 
   @Override
   protected int getTestPort() {
@@ -47,25 +52,36 @@ public class TestLensClient extends LensAllApplicationJerseyTest {
   @BeforeTest
   public void setUp() throws Exception {
     super.setUp();
+
+    Hive hive = Hive.get(new HiveConf());
+    Database db = new Database();
+    db.setName(TEST_DB);
+    hive.createDatabase(db, true);
   }
 
   @AfterTest
   public void tearDown() throws Exception {
     super.tearDown();
+
+    Hive hive = Hive.get(new HiveConf());
+    hive.dropDatabase(TEST_DB);
   }
 
   @Test
   public void testClient() throws Exception {
     LensClientConfig lensClientConfig = new LensClientConfig();
+    lensClientConfig.setLensDatabase(TEST_DB);
+    Assert.assertEquals(lensClientConfig.getLensDatabase(), TEST_DB);
+
     lensClientConfig.set(LensConfConstants.SERVER_BASE_URL, "http://localhost:" + getTestPort() + "/lensapi");
     LensClient client = new LensClient(lensClientConfig);
-    Assert.assertEquals(client.getCurrentDatabae(), "default",
+    Assert.assertEquals(client.getCurrentDatabae(), TEST_DB,
       "current database");
     List<String> dbs = client.getAllDatabases();
-    Assert.assertEquals(dbs.size(), 1, "no of databases");
+    Assert.assertEquals(dbs.size(), 2, "no of databases");
     client.createDatabase("testclientdb", true);
-    Assert.assertEquals(client.getAllDatabases().size(), 2, " no of databases");
+    Assert.assertEquals(client.getAllDatabases().size(), 3, " no of databases");
     client.dropDatabase("testclientdb");
-    Assert.assertEquals(client.getAllDatabases().size(), 1, " no of databases");
+    Assert.assertEquals(client.getAllDatabases().size(), 2, " no of databases");
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/lens-server-api/src/main/java/org/apache/lens/server/api/session/SessionService.java
----------------------------------------------------------------------
diff --git a/lens-server-api/src/main/java/org/apache/lens/server/api/session/SessionService.java b/lens-server-api/src/main/java/org/apache/lens/server/api/session/SessionService.java
index 88d4869..74c8b0a 100644
--- a/lens-server-api/src/main/java/org/apache/lens/server/api/session/SessionService.java
+++ b/lens-server-api/src/main/java/org/apache/lens/server/api/session/SessionService.java
@@ -27,7 +27,7 @@ import org.apache.lens.api.LensSessionHandle;
 
 public interface SessionService {
 
-  /** The Constant NAME. */
+  /** Name of session service */
   String NAME = "session";
 
   /**
@@ -35,12 +35,14 @@ public interface SessionService {
    *
    * @param username      the username
    * @param password      the password
+   * @param database      Set current database to the supplied value
    * @param configuration the configuration
    * @return the lens session handle
    * @throws LensException the lens exception
    */
 
-  LensSessionHandle openSession(String username, String password, Map<String, String> configuration)
+  LensSessionHandle openSession(String username, String password, String database,
+                                Map<String, String> configuration)
     throws LensException;
 
   /**

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/lens-server/src/main/java/org/apache/lens/server/session/HiveSessionService.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/session/HiveSessionService.java b/lens-server/src/main/java/org/apache/lens/server/session/HiveSessionService.java
index 61e2d22..7b365db 100644
--- a/lens-server/src/main/java/org/apache/lens/server/session/HiveSessionService.java
+++ b/lens-server/src/main/java/org/apache/lens/server/session/HiveSessionService.java
@@ -44,6 +44,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.processors.SetProcessor;
 import org.apache.hadoop.hive.ql.session.SessionState;
 import org.apache.hive.service.cli.CLIService;
@@ -185,12 +187,29 @@ public class HiveSessionService extends LensService implements SessionService {
    * @inheritDoc
    */
   @Override
-  public LensSessionHandle openSession(String username, String password, Map<String, String> configuration)
+  public LensSessionHandle openSession(String username, String password, String database,
+                                       Map<String, String> configuration)
     throws LensException {
     LensSessionHandle sessionid = super.openSession(username, password, configuration);
     LOG.info("Opened session " + sessionid + " for user " + username);
     // add auxuiliary jars
     String[] auxJars = getSession(sessionid).getSessionConf().getStrings(LensConfConstants.AUX_JARS);
+
+    // Set current database
+    if (StringUtils.isNotBlank(database)) {
+      try {
+        if (!Hive.get(getSession(sessionid).getHiveConf()).databaseExists(database)) {
+          throw new NotFoundException("Database " + database + " does not exist");
+        }
+      } catch (HiveException e) {
+        LOG.error("Error in checking if database exists " + database, e);
+        throw new LensException(e);
+      }
+
+      getSession(sessionid).setCurrentDatabase(database);
+      LOG.info("Set database to " + database + " for session " + sessionid.getPublicId());
+    }
+
     if (auxJars != null) {
       LOG.info("Adding aux jars:" + auxJars);
       for (String jar : auxJars) {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java b/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
index 22a9440..ed906fe 100644
--- a/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
+++ b/lens-server/src/main/java/org/apache/lens/server/session/SessionResource.java
@@ -75,6 +75,7 @@ public class SessionResource {
    *
    * @param username    User name of the Lens server user
    * @param password    Password of the Lens server user
+   * @param database    Set current database to the supplied value, if provided
    * @param sessionconf Key-value properties which will be used to configure this session
    * @return A Session handle unique to this session
    */
@@ -82,7 +83,9 @@ public class SessionResource {
   @Consumes({MediaType.MULTIPART_FORM_DATA})
   @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
   public LensSessionHandle openSession(@FormDataParam("username") String username,
-    @FormDataParam("password") String password, @FormDataParam("sessionconf") LensConf sessionconf) {
+    @FormDataParam("password") String password,
+    @FormDataParam("database")  @DefaultValue("") String database,
+    @FormDataParam("sessionconf") LensConf sessionconf) {
     try {
       Map<String, String> conf;
       if (sessionconf != null) {
@@ -90,7 +93,7 @@ public class SessionResource {
       } else {
         conf = new HashMap<String, String>();
       }
-      return sessionService.openSession(username, password, conf);
+      return sessionService.openSession(username, password, database,   conf);
     } catch (LensException e) {
       throw new WebApplicationException(e);
     }

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/lens-server/src/main/java/org/apache/lens/server/ui/SessionUIResource.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/ui/SessionUIResource.java b/lens-server/src/main/java/org/apache/lens/server/ui/SessionUIResource.java
index c32edb4..e235d1d 100644
--- a/lens-server/src/main/java/org/apache/lens/server/ui/SessionUIResource.java
+++ b/lens-server/src/main/java/org/apache/lens/server/ui/SessionUIResource.java
@@ -101,6 +101,7 @@ public class SessionUIResource {
    *
    * @param username    User name of the Lens server user
    * @param password    Password of the Lens server user
+   * @param database    (Optional) Set current database to supplied value
    * @param sessionconf Key-value properties which will be used to configure this session
    * @return A Session handle unique to this session
    */
@@ -108,7 +109,9 @@ public class SessionUIResource {
   @Consumes({MediaType.MULTIPART_FORM_DATA})
   @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
   public LensSessionHandle openSession(@FormDataParam("username") String username,
-    @FormDataParam("password") String password, @FormDataParam("sessionconf") LensConf sessionconf) {
+    @FormDataParam("password") String password,
+    @FormDataParam("database") @DefaultValue("") String database,
+    @FormDataParam("sessionconf") LensConf sessionconf) {
     try {
       Map<String, String> conf;
       if (sessionconf != null) {
@@ -116,7 +119,7 @@ public class SessionUIResource {
       } else {
         conf = new HashMap<String, String>();
       }
-      LensSessionHandle handle = sessionService.openSession(username, password, conf);
+      LensSessionHandle handle = sessionService.openSession(username, password, database, conf);
       openSessions.put(handle.getPublicId(), handle);
       return handle;
     } catch (LensException e) {

http://git-wip-us.apache.org/repos/asf/incubator-lens/blob/ea9c83c9/lens-server/src/test/java/org/apache/lens/server/session/TestSessionResource.java
----------------------------------------------------------------------
diff --git a/lens-server/src/test/java/org/apache/lens/server/session/TestSessionResource.java b/lens-server/src/test/java/org/apache/lens/server/session/TestSessionResource.java
index 78ffc87..7cff12a 100644
--- a/lens-server/src/test/java/org/apache/lens/server/session/TestSessionResource.java
+++ b/lens-server/src/test/java/org/apache/lens/server/session/TestSessionResource.java
@@ -42,6 +42,9 @@ import org.apache.lens.server.common.LensServerTestFileUtils;
 import org.apache.lens.server.common.TestResourceFile;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.ql.metadata.Hive;
 
 import org.glassfish.jersey.client.ClientConfig;
 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
@@ -381,4 +384,52 @@ public class TestSessionResource extends LensJerseyTest {
     }
   }
 
+  @Test
+  public void testOpenSessionWithDatabase() throws Exception {
+    // TEST1 - Check if call with database parameter sets current database
+    // Create the test DB
+    Hive hive = Hive.get(new HiveConf());
+    final String testDbName = TestSessionResource.class.getSimpleName();
+    Database testOpenDb = new Database();
+    testOpenDb.setName(testDbName);
+    hive.createDatabase(testOpenDb, true);
+
+    final WebTarget target = target().path("session");
+    final FormDataMultiPart mp = new FormDataMultiPart();
+
+    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("username").build(), "foo"));
+    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("password").build(), "bar"));
+    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("database").build(), testDbName));
+    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionconf").fileName("sessionconf").build(),
+      new LensConf(), MediaType.APPLICATION_XML_TYPE));
+
+    final LensSessionHandle handle = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE),
+      LensSessionHandle.class);
+    Assert.assertNotNull(handle);
+
+    // Check if DB set in session service.
+    HiveSessionService service = LensServices.get().getService(SessionService.NAME);
+    LensSessionImpl session = service.getSession(handle);
+    Assert.assertEquals(session.getCurrentDatabase(), testDbName, "Expected current DB to be set to " + testDbName);
+
+
+    // TEST 2 - Try set database with invalid db name
+    final String invalidDB = testDbName + "_invalid_db";
+    final FormDataMultiPart form2 = new FormDataMultiPart();
+
+    form2.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("username").build(), "foo"));
+    form2.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("password").build(), "bar"));
+    form2.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("database").build(), invalidDB));
+    form2.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("sessionconf").fileName("sessionconf").build(),
+      new LensConf(), MediaType.APPLICATION_XML_TYPE));
+
+    try {
+      final LensSessionHandle handle2 = target.request().post(Entity.entity(form2, MediaType.MULTIPART_FORM_DATA_TYPE),
+        LensSessionHandle.class);
+      Assert.fail("Expected above call to fail with not found exception");
+    } catch (NotFoundException nfe) {
+      // PASS
+    }
+  }
+
 }