You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lens.apache.org by pr...@apache.org on 2016/07/01 10:05:22 UTC

[1/4] lens git commit: LENS-1187: Refactor exception handling in MetastoreResource

Repository: lens
Updated Branches:
  refs/heads/master 9068e3c17 -> 4bcb7aa96


http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/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 57b1836..f684247 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
@@ -32,7 +32,6 @@ import org.apache.lens.api.DateTime;
 import org.apache.lens.api.LensSessionHandle;
 import org.apache.lens.api.StringList;
 import org.apache.lens.api.metastore.*;
-import org.apache.lens.api.result.LensErrorTO;
 import org.apache.lens.server.LensServices;
 import org.apache.lens.server.api.error.LensException;
 import org.apache.lens.server.api.metastore.CubeMetastoreService;
@@ -41,7 +40,6 @@ import org.apache.commons.lang.NotImplementedException;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 
-import com.google.common.collect.Lists;
 import lombok.extern.slf4j.Slf4j;
 
 /**
@@ -58,14 +56,8 @@ public class MetastoreResource {
   public static CubeMetastoreService getSvc() {
     return LensServices.get().getService(CubeMetastoreService.NAME);
   }
-  private static void checkSessionId(LensSessionHandle sessionHandle) {
-    try {
-      getSvc().validateSession(sessionHandle);
-    } catch (LensException e) {
-      LensErrorTO to = e.buildLensErrorTO(LensServices.get().getErrorCollection());
-      throw new ClientErrorException(to.getMessage(),
-        LensServices.get().getErrorCollection().getLensError(e.getErrorCode()).getHttpStatusCode().getStatusCode());
-    }
+  private static void checkSessionId(LensSessionHandle sessionHandle) throws LensException {
+    getSvc().validateSession(sessionHandle);
   }
 
   private void checkNonNullArgs(String message, Object... args) {
@@ -193,35 +185,23 @@ public class MetastoreResource {
 
     public abstract void doDelete(LensSessionHandle sessionid, String entityName, Boolean cascade) throws LensException;
 
-    public StringList getAll(LensSessionHandle sessionid) {
+    public StringList getAll(LensSessionHandle sessionid) throws LensException {
       checkSessionId(sessionid);
-      List<String> allNames;
-      try {
-        allNames = doGetAll(sessionid);
-      } catch (LensException e) {
-        throw new WebApplicationException(e);
-      }
-      return new StringList(allNames);
+      return new StringList(doGetAll(sessionid));
     }
 
-    public APIResult delete(LensSessionHandle sessionid, String entityName, Boolean cascade) {
+    public APIResult delete(LensSessionHandle sessionid, String entityName, Boolean cascade) throws LensException {
       log.info("Drop {} {} cascade: {}", name(), entityName, cascade);
       checkSessionId(sessionid);
-      try {
-        doDelete(sessionid, entityName, cascade);
-      } catch (LensException e) {
-        checkTableNotFound(e, entityName);
-        log.error("Error droping {} {}", name(), entityName, e);
-        return failure(processLensException(e));
-      }
+      doDelete(sessionid, entityName, cascade);
       return success();
     }
 
-    public APIResult delete(LensSessionHandle sessionid, String entityName) {
+    public APIResult delete(LensSessionHandle sessionid, String entityName) throws LensException {
       return delete(sessionid, entityName, null);
     }
 
-    public APIResult deleteAll(LensSessionHandle sessionid, Boolean cascade) {
+    public APIResult deleteAll(LensSessionHandle sessionid, Boolean cascade) throws LensException {
       checkSessionId(sessionid);
       List<String> entities;
       int numDeleted = 0;
@@ -262,7 +242,7 @@ public class MetastoreResource {
    */
   @GET
   @Path("databases")
-  public StringList getAllDatabases(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public StringList getAllDatabases(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     return Entity.DATABASE.getAll(sessionid);
   }
 
@@ -274,14 +254,10 @@ public class MetastoreResource {
    */
   @GET
   @Path("databases/current")
-  public String getDatabase(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public String getDatabase(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     checkSessionId(sessionid);
     log.info("Get database");
-    try {
-      return getSvc().getCurrentDatabase(sessionid);
-    } catch (LensException e) {
-      throw new WebApplicationException(e);
-    }
+    return getSvc().getCurrentDatabase(sessionid);
   }
 
   /**
@@ -295,16 +271,12 @@ public class MetastoreResource {
   @PUT
   @Path("databases/current")
   @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
-  public APIResult setDatabase(@QueryParam("sessionid") LensSessionHandle sessionid, String dbName) {
+  public APIResult setDatabase(@QueryParam("sessionid") LensSessionHandle sessionid, String dbName)
+    throws LensException {
     checkSessionId(sessionid);
     log.info("Set database:{}", dbName);
-    try {
-      getSvc().setCurrentDatabase(sessionid, dbName);
-      return success();
-    } catch (LensException e) {
-      log.error("Error changing current database", e);
-      return failure(processLensException(e));
-    }
+    getSvc().setCurrentDatabase(sessionid, dbName);
+    return success();
   }
 
   /**
@@ -320,7 +292,7 @@ public class MetastoreResource {
   @Path("databases/{dbName}")
   public APIResult dropDatabase(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dbName") String dbName,
-    @QueryParam("cascade") boolean cascade) {
+    @QueryParam("cascade") boolean cascade) throws LensException {
     return Entity.DATABASE.delete(sessionid, dbName, cascade);
   }
 
@@ -337,16 +309,11 @@ public class MetastoreResource {
   @Path("databases")
   public APIResult createDatabase(@QueryParam("sessionid") LensSessionHandle sessionid,
     @QueryParam("ignoreIfExisting") @DefaultValue("true") boolean ignoreIfExisting,
-    String dbName) {
+    String dbName) throws LensException {
     checkSessionId(sessionid);
     log.info("Create database {} Ignore Existing? {}", dbName, ignoreIfExisting);
-    try {
-      getSvc().createDatabase(sessionid, dbName, ignoreIfExisting);
-      return success();
-    } catch (LensException e) {
-      log.error("Error creating database {}", dbName, e);
-      return failure(processLensException(e));
-    }
+    getSvc().createDatabase(sessionid, dbName, ignoreIfExisting);
+    return success();
   }
 
   /**
@@ -364,21 +331,15 @@ public class MetastoreResource {
   @Path("nativetables")
   public StringList getAllNativeTables(@QueryParam("sessionid") LensSessionHandle sessionid,
     @QueryParam("dbOption") String dbOption,
-    @QueryParam("dbName") String dbName) {
+    @QueryParam("dbName") String dbName) throws LensException {
     checkSessionId(sessionid);
-    List<String> allNames;
-    try {
-      if (StringUtils.isBlank(dbName) && !StringUtils.isBlank(dbOption)) {
-        if (!dbOption.equalsIgnoreCase("current") && !dbOption.equalsIgnoreCase("all")) {
-          throw new BadRequestException("Invalid dbOption param:" + dbOption
-            + " Allowed values are 'current' and 'all'");
-        }
+    if (StringUtils.isBlank(dbName) && !StringUtils.isBlank(dbOption)) {
+      if (!dbOption.equalsIgnoreCase("current") && !dbOption.equalsIgnoreCase("all")) {
+        throw new BadRequestException("Invalid dbOption param:" + dbOption
+          + " Allowed values are 'current' and 'all'");
       }
-      allNames = getSvc().getAllNativeTableNames(sessionid, dbOption, dbName);
-    } catch (LensException e) {
-      throw new WebApplicationException(e);
     }
-    return new StringList(allNames);
+    return new StringList(getSvc().getAllNativeTableNames(sessionid, dbOption, dbName));
   }
 
   /**
@@ -392,15 +353,9 @@ public class MetastoreResource {
   @GET
   @Path("nativetables/{tableName}")
   public JAXBElement<XNativeTable> getNativeTable(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("tableName") String tableName) {
+    @PathParam("tableName") String tableName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXNativeTable(getSvc().getNativeTable(sessionid, tableName));
-    } catch (LensException e) {
-      checkTableNotFound(e, tableName);
-      log.error("Error getting native table", e);
-      throw new WebApplicationException(e);
-    }
+    return X_CUBE_OBJECT_FACTORY.createXNativeTable(getSvc().getNativeTable(sessionid, tableName));
   }
 
   /**
@@ -413,25 +368,20 @@ public class MetastoreResource {
   @GET
   @Path("cubes")
   public StringList getAllCubes(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @QueryParam("type") @DefaultValue("all") String cubeTypes) {
+    @QueryParam("type") @DefaultValue("all") String cubeTypes) throws LensException {
     checkSessionId(sessionid);
-    try {
-      switch (cubeTypes) {
-      case "all":
-        return new StringList(getSvc().getAllCubeNames(sessionid));
-      case "base":
-        return new StringList(getSvc().getAllBaseCubeNames(sessionid));
-      case "derived":
-        return new StringList(getSvc().getAllDerivedCubeNames(sessionid));
-      case "queryable":
-        return new StringList(getSvc().getAllQueryableCubeNames(sessionid));
-      default:
-        throw new BadRequestException("Invalid type " + cubeTypes + " Accepted"
-          + " values are 'all' or 'base' or 'derived' or 'queryable'");
-      }
-    } catch (LensException e) {
-      log.error("Error getting cube names", e);
-      throw new WebApplicationException(e);
+    switch (cubeTypes) {
+    case "all":
+      return new StringList(getSvc().getAllCubeNames(sessionid));
+    case "base":
+      return new StringList(getSvc().getAllBaseCubeNames(sessionid));
+    case "derived":
+      return new StringList(getSvc().getAllDerivedCubeNames(sessionid));
+    case "queryable":
+      return new StringList(getSvc().getAllQueryableCubeNames(sessionid));
+    default:
+      throw new BadRequestException("Invalid type " + cubeTypes + " Accepted"
+        + " values are 'all' or 'base' or 'derived' or 'queryable'");
     }
   }
 
@@ -444,7 +394,7 @@ public class MetastoreResource {
    */
   @DELETE
   @Path("cubes")
-  public APIResult deleteAllCubes(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public APIResult deleteAllCubes(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     checkSessionId(sessionid);
     return Entity.CUBE.deleteAll(sessionid, null);
   }
@@ -459,35 +409,13 @@ public class MetastoreResource {
    */
   @POST
   @Path("cubes")
-  public APIResult createNewCube(@QueryParam("sessionid") LensSessionHandle sessionid, XCube cube) {
+  public APIResult createNewCube(@QueryParam("sessionid") LensSessionHandle sessionid, XCube cube)
+    throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().createCube(sessionid, cube);
-    } catch (LensException e) {
-      if (cube instanceof XDerivedCube) {
-        // parent should exist
-        checkTableNotFound(e, ((XDerivedCube) cube).getParent());
-      }
-      log.error("Error creating cube {}", cube.getName(), e);
-      return failure(processLensException(e));
-    }
+    getSvc().createCube(sessionid, cube);
     return success();
   }
 
-
-  private static void checkTableNotFound(LensException e, String table) {
-    List<String> messages = Lists.newArrayList();
-    messages.add(e.getMessage());
-    if (e.getCause() instanceof HiveException) {
-      messages.add(e.getCause().getMessage());
-    }
-    for (String message : messages) {
-      if (message != null && message.startsWith("Could not get table")) {
-        throw new NotFoundException("Table not found " + table, e);
-      }
-    }
-  }
-
   /**
    * Update cube definition
    *
@@ -500,19 +428,9 @@ public class MetastoreResource {
   @PUT
   @Path("/cubes/{cubeName}")
   public APIResult updateCube(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("cubeName") String cubeName, XCube cube) {
+    @PathParam("cubeName") String cubeName, XCube cube) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().updateCube(sessionid, cube);
-    } catch (LensException e) {
-      if (cube instanceof XDerivedCube) {
-        // parent should exist
-        checkTableNotFound(e, ((XDerivedCube) cube).getParent());
-      }
-      checkTableNotFound(e, cube.getName());
-      log.error("Error updating cube {}", cube.getName(), e);
-      return failure(processLensException(e));
-    }
+    getSvc().updateCube(sessionid, cube);
     return success();
   }
 
@@ -526,15 +444,9 @@ public class MetastoreResource {
   @GET
   @Path("/cubes/{cubeName}")
   public JAXBElement<XCube> getCube(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("cubeName") String cubeName) {
+    @PathParam("cubeName") String cubeName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXCube(getSvc().getCube(sessionid, cubeName));
-    } catch (LensException e) {
-      checkTableNotFound(e, cubeName);
-      log.error("Error getting cube", e);
-      throw new WebApplicationException(e);
-    }
+    return X_CUBE_OBJECT_FACTORY.createXCube(getSvc().getCube(sessionid, cubeName));
   }
 
   /**
@@ -548,7 +460,7 @@ public class MetastoreResource {
   @DELETE
   @Path("/cubes/{cubeName}")
   public APIResult dropCube(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("cubeName") String cubeName) {
+    @PathParam("cubeName") String cubeName) throws LensException {
     return Entity.CUBE.delete(sessionid, cubeName, null);
   }
 
@@ -561,7 +473,7 @@ public class MetastoreResource {
    */
   @GET
   @Path("storages")
-  public StringList getAllStorages(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public StringList getAllStorages(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     checkSessionId(sessionid);
     return Entity.STORAGE.getAll(sessionid);
   }
@@ -576,14 +488,10 @@ public class MetastoreResource {
    */
   @POST
   @Path("storages")
-  public APIResult createNewStorage(@QueryParam("sessionid") LensSessionHandle sessionid, XStorage storage) {
+  public APIResult createNewStorage(@QueryParam("sessionid") LensSessionHandle sessionid, XStorage storage)
+    throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().createStorage(sessionid, storage);
-    } catch (LensException e) {
-      log.error("Error creating storage {}", storage.getName(), e);
-      return failure(processLensException(e));
-    }
+    getSvc().createStorage(sessionid, storage);
     return success();
   }
 
@@ -596,7 +504,7 @@ public class MetastoreResource {
    */
   @DELETE
   @Path("storages")
-  public APIResult deleteAllStorages(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public APIResult deleteAllStorages(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     return Entity.STORAGE.deleteAll(sessionid, null);
   }
 
@@ -612,15 +520,9 @@ public class MetastoreResource {
   @PUT
   @Path("/storages/{storageName}")
   public APIResult updateStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("storageName") String storageName, XStorage storage) {
+    @PathParam("storageName") String storageName, XStorage storage) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().alterStorage(sessionid, storageName, storage);
-    } catch (LensException e) {
-      checkTableNotFound(e, storageName);
-      log.error("Error updating storage {}", storageName, e);
-      return failure(processLensException(e));
-    }
+    getSvc().alterStorage(sessionid, storageName, storage);
     return success();
   }
 
@@ -634,14 +536,9 @@ public class MetastoreResource {
   @GET
   @Path("/storages/{storage}")
   public JAXBElement<XStorage> getStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("storage") String storageName) throws Exception {
+    @PathParam("storage") String storageName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXStorage(getSvc().getStorage(sessionid, storageName));
-    } catch (LensException e) {
-      checkTableNotFound(e, storageName);
-      throw e;
-    }
+    return X_CUBE_OBJECT_FACTORY.createXStorage(getSvc().getStorage(sessionid, storageName));
   }
 
   /**
@@ -655,7 +552,7 @@ public class MetastoreResource {
   @DELETE
   @Path("/storages/{storage}")
   public APIResult dropStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("storage") String storageName) {
+    @PathParam("storage") String storageName) throws LensException {
     return Entity.STORAGE.delete(sessionid, storageName, null);
   }
 
@@ -668,7 +565,7 @@ public class MetastoreResource {
    */
   @GET
   @Path("dimensions")
-  public StringList getAllDimensionNames(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public StringList getAllDimensionNames(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     return Entity.DIMENSION.getAll(sessionid);
   }
 
@@ -682,14 +579,10 @@ public class MetastoreResource {
    */
   @POST
   @Path("dimensions")
-  public APIResult createDimension(@QueryParam("sessionid") LensSessionHandle sessionid, XDimension dimension) {
+  public APIResult createDimension(@QueryParam("sessionid") LensSessionHandle sessionid, XDimension dimension)
+    throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().createDimension(sessionid, dimension);
-    } catch (LensException e) {
-      log.error("Error creating dimension {}", dimension.getName(), e);
-      return failure(processLensException(e));
-    }
+    getSvc().createDimension(sessionid, dimension);
     return success();
   }
 
@@ -702,7 +595,7 @@ public class MetastoreResource {
    */
   @DELETE
   @Path("dimensions")
-  public APIResult deleteAllDimensions(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public APIResult deleteAllDimensions(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     return Entity.DIMENSION.deleteAll(sessionid, null);
   }
 
@@ -718,15 +611,9 @@ public class MetastoreResource {
   @PUT
   @Path("/dimensions/{dimName}")
   public APIResult updateDimension(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("dimName") String dimName, XDimension dimension) {
+    @PathParam("dimName") String dimName, XDimension dimension) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().updateDimension(sessionid, dimName, dimension);
-    } catch (LensException e) {
-      checkTableNotFound(e, dimName);
-      log.error("Error updating dimension {}", dimName, e);
-      return failure(processLensException(e));
-    }
+    getSvc().updateDimension(sessionid, dimName, dimension);
     return success();
   }
 
@@ -740,14 +627,9 @@ public class MetastoreResource {
   @GET
   @Path("/dimensions/{dimName}")
   public JAXBElement<XDimension> getDimension(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("dimName") String dimName) throws Exception {
+    @PathParam("dimName") String dimName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXDimension(getSvc().getDimension(sessionid, dimName));
-    } catch (LensException e) {
-      checkTableNotFound(e, dimName);
-      throw e;
-    }
+    return X_CUBE_OBJECT_FACTORY.createXDimension(getSvc().getDimension(sessionid, dimName));
   }
 
   /**
@@ -761,7 +643,7 @@ public class MetastoreResource {
   @DELETE
   @Path("/dimensions/{dimName}")
   public APIResult dropDimension(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("dimName") String dimName) {
+    @PathParam("dimName") String dimName) throws LensException {
     return Entity.DIMENSION.delete(sessionid, dimName, null);
   }
 
@@ -778,12 +660,7 @@ public class MetastoreResource {
     @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("dimName") String dimensionName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return new StringList(getSvc().getAllDimTableNames(sessionid, dimensionName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, dimensionName);
-      throw exc;
-    }
+    return new StringList(getSvc().getAllDimTableNames(sessionid, dimensionName));
   }
 
   /**
@@ -799,12 +676,7 @@ public class MetastoreResource {
     @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("cubeName") String cubeName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return new StringList(getSvc().getAllFactNames(sessionid, cubeName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, cubeName);
-      throw exc;
-    }
+    return new StringList(getSvc().getAllFactNames(sessionid, cubeName));
   }
 
   /**
@@ -820,12 +692,7 @@ public class MetastoreResource {
     @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("cubeName") String cubeName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return new StringList(getSvc().getAllSegmentations(sessionid, cubeName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, cubeName);
-      throw exc;
-    }
+    return new StringList(getSvc().getAllSegmentations(sessionid, cubeName));
   }
 
 
@@ -870,7 +737,7 @@ public class MetastoreResource {
   @DELETE
   @Path("facts")
   public APIResult deleteAllFacts(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @DefaultValue("false") @QueryParam("cascade") boolean cascade) {
+    @DefaultValue("false") @QueryParam("cascade") boolean cascade) throws LensException {
     return Entity.FACT.deleteAll(sessionid, cascade);
   }
 
@@ -883,7 +750,7 @@ public class MetastoreResource {
    */
   @DELETE
   @Path("segmentations")
-  public APIResult deleteAllSegmentations(@QueryParam("sessionid") LensSessionHandle sessionid) {
+  public APIResult deleteAllSegmentations(@QueryParam("sessionid") LensSessionHandle sessionid) throws LensException {
     return Entity.SEGMENTATION.deleteAll(sessionid, null);
   }
 
@@ -900,12 +767,7 @@ public class MetastoreResource {
     @PathParam("factName") String factName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXFactTable(getSvc().getFactTable(sessionid, factName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      throw exc;
-    }
+    return X_CUBE_OBJECT_FACTORY.createXFactTable(getSvc().getFactTable(sessionid, factName));
   }
 
   /**
@@ -921,13 +783,7 @@ public class MetastoreResource {
     @PathParam("segmentationName") String segmentationName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXSegmentation(getSvc().
-              getSegmentation(sessionid, segmentationName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, segmentationName);
-      throw exc;
-    }
+    return X_CUBE_OBJECT_FACTORY.createXSegmentation(getSvc().getSegmentation(sessionid, segmentationName));
   }
 
   /**
@@ -943,13 +799,8 @@ public class MetastoreResource {
   public APIResult createFactTable(@QueryParam("sessionid") LensSessionHandle sessionid, XFactTable fact)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      log.info("Create fact table");
-      getSvc().createFactTable(sessionid, fact);
-    } catch (LensException exc) {
-      log.error("Exception creating fact:", exc);
-      return failure(processLensException(exc));
-    }
+    log.info("Create fact table");
+    getSvc().createFactTable(sessionid, fact);
     return success();
   }
 
@@ -966,13 +817,8 @@ public class MetastoreResource {
   public APIResult createSegmentation(@QueryParam("sessionid") LensSessionHandle sessionid, XSegmentation seg)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      log.info("Create segmentation");
-      getSvc().createSegmentation(sessionid, seg);
-    } catch (LensException exc) {
-      log.error("Exception creating segmentation:", exc);
-      return failure(processLensException(exc));
-    }
+    log.info("Create segmentation");
+    getSvc().createSegmentation(sessionid, seg);
     return success();
   }
 
@@ -992,13 +838,7 @@ public class MetastoreResource {
     @PathParam("factName") String factName, XFactTable fact)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().updateFactTable(sessionid, fact);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error updating fact {}", factName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().updateFactTable(sessionid, fact);
     return success();
   }
 
@@ -1017,13 +857,7 @@ public class MetastoreResource {
     @PathParam("segmentationName") String segmentationName, XSegmentation seg)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().updateSegmentation(sessionid, seg);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, segmentationName);
-      log.error("Error updating segmentation {}", segmentationName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().updateSegmentation(sessionid, seg);
     return success();
   }
 
@@ -1075,12 +909,7 @@ public class MetastoreResource {
   public StringList getStoragesOfFact(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      return new StringList(getSvc().getStoragesOfFact(sessionid, factName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      throw new WebApplicationException(exc);
-    }
+    return new StringList(getSvc().getStoragesOfFact(sessionid, factName));
   }
 
   /**
@@ -1094,15 +923,9 @@ public class MetastoreResource {
   @DELETE
   @Path("/facts/{factName}/storages")
   public APIResult dropAllStoragesOfFact(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("factName") String factName) {
+    @PathParam("factName") String factName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().dropAllStoragesOfFact(sessionid, factName);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error dropping storages of fact {}", factName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().dropAllStoragesOfFact(sessionid, factName);
     return success();
   }
 
@@ -1119,15 +942,9 @@ public class MetastoreResource {
   @Path("/facts/{factName}/storages")
   public APIResult addStorageToFact(
     @QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("factName") String factName, XStorageTableElement storageTable) {
+    @PathParam("factName") String factName, XStorageTableElement storageTable) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().addStorageToFact(sessionid, factName, storageTable);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error adding storage to fact {}", factName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().addStorageToFact(sessionid, factName, storageTable);
     return success();
   }
 
@@ -1145,15 +962,9 @@ public class MetastoreResource {
   public APIResult dropStorageFromFact(
     @QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
-    @PathParam("storage") String storage) {
+    @PathParam("storage") String storage) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().dropStorageOfFact(sessionid, factName, storage);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error dropping storage of fact {}", factName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().dropStorageOfFact(sessionid, factName, storage);
     return success();
   }
 
@@ -1190,13 +1001,8 @@ public class MetastoreResource {
     @PathParam("storage") String storage,
     @QueryParam("filter") String filter) throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY
-        .createXPartitionList(getSvc().getAllPartitionsOfFactStorage(sessionid, factName, storage, filter));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      throw exc;
-    }
+    return X_CUBE_OBJECT_FACTORY
+      .createXPartitionList(getSvc().getAllPartitionsOfFactStorage(sessionid, factName, storage, filter));
   }
 
   /**
@@ -1215,13 +1021,12 @@ public class MetastoreResource {
   public APIResult dropPartitionsOfFactStorageByFilter(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
     @PathParam("storage") String storage,
-    @QueryParam("filter") String filter) {
+    @QueryParam("filter") String filter) throws LensException {
     checkSessionId(sessionid);
     try {
       getSvc().dropPartitionFromStorageByFilter(sessionid, factName, storage, filter);
     } catch (LensException exc) {
       log.warn("Got exception while dropping partition.", exc);
-      checkTableNotFound(exc, factName);
       return partial(processLensException(exc));
     }
     return success();
@@ -1242,18 +1047,12 @@ public class MetastoreResource {
   public APIResult addPartitionToFactStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
     @PathParam("storage") String storage,
-    XPartition partition) {
+    XPartition partition) throws LensException {
     checkSessionId(sessionid);
     checkNonNullArgs("Partition is null", partition);
     checkNonNullArgs("Partition elements are null", partition.getFactOrDimensionTableName(),
       partition.getUpdatePeriod());
-    try {
-      return successOrPartialOrFailure(getSvc().addPartitionToFactStorage(sessionid, factName, storage, partition), 1);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error adding partition to storage of fact {}:{}", factName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    return successOrPartialOrFailure(getSvc().addPartitionToFactStorage(sessionid, factName, storage, partition), 1);
   }
 
   /**
@@ -1272,19 +1071,8 @@ public class MetastoreResource {
   public APIResult updatePartitionOfFactStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
     @PathParam("storage") String storage,
-    XPartition partition) {
-    checkSessionId(sessionid);
-    checkNonNullArgs("Partition is null", partition);
-    checkNonNullArgs("Partition elements are null", partition.getFactOrDimensionTableName(),
-      partition.getUpdatePeriod());
-    try {
-      getSvc().updatePartition(sessionid, factName, storage, partition);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error adding partition to storage of fact {}:{}", factName, storage, exc);
-      return failure(processLensException(exc));
-    }
-    return success();
+    XPartition partition) throws LensException {
+    return checkAndUpdatePartitions(sessionid, factName, storage, partition);
   }
 
   /**
@@ -1302,17 +1090,11 @@ public class MetastoreResource {
   public APIResult addPartitionsToFactStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
     @PathParam("storage") String storage,
-    XPartitionList partitions) {
+    XPartitionList partitions) throws LensException {
     checkSessionId(sessionid);
     checkNonNullPartitionList(partitions);
-    try {
-      return successOrPartialOrFailure(getSvc().addPartitionsToFactStorage(sessionid, factName, storage, partitions),
-        partitions.getPartition().size());
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error adding partition to storage of fact {}:{}", factName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    return successOrPartialOrFailure(getSvc().addPartitionsToFactStorage(sessionid, factName, storage, partitions),
+      partitions.getPartition().size());
   }
 
   /**
@@ -1331,16 +1113,10 @@ public class MetastoreResource {
   public APIResult updatePartitionsOfFactStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
     @PathParam("storage") String storage,
-    XPartitionList partitions) {
+    XPartitionList partitions) throws LensException {
     checkSessionId(sessionid);
     checkNonNullPartitionList(partitions);
-    try {
-      getSvc().updatePartitions(sessionid, factName, storage, partitions);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error adding partition to storage of fact {}:{}", factName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().updatePartitions(sessionid, factName, storage, partitions);
     return success();
   }
 
@@ -1359,17 +1135,9 @@ public class MetastoreResource {
   public APIResult dropPartitionOfFactStorageByValues(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("factName") String factName,
     @PathParam("storage") String storage,
-    @QueryParam("values") String values) {
+    @QueryParam("values") String values) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().dropPartitionFromStorageByValues(sessionid, factName, storage,
-        values);
-
-    } catch (LensException e) {
-      checkTableNotFound(e, factName);
-      log.error("Error dropping partition to storage of fact {}:{}", factName, storage, e);
-      return failure(processLensException(e));
-    }
+    getSvc().dropPartitionFromStorageByValues(sessionid, factName, storage, values);
     return success();
   }
 
@@ -1396,14 +1164,9 @@ public class MetastoreResource {
   @POST
   @Path("/dimtables")
   public APIResult createDimensionTable(@QueryParam("sessionid") LensSessionHandle sessionid,
-                                        XDimensionTable dimensionTable) {
+                                        XDimensionTable dimensionTable) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().createDimensionTable(sessionid, dimensionTable);
-    } catch (LensException exc) {
-      log.error("Error creating cube dimension table {}", dimensionTable.getTableName(), exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().createDimensionTable(sessionid, dimensionTable);
     return success();
   }
 
@@ -1419,15 +1182,9 @@ public class MetastoreResource {
   @Path("/dimtables/{dimTableName}")
   public APIResult updateCubeDimension(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
-    XDimensionTable dimensionTable) {
+    XDimensionTable dimensionTable) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().updateDimensionTable(sessionid, dimensionTable);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, dimTableName);
-      log.error("Error updating cube dimension table {}", dimTableName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().updateDimensionTable(sessionid, dimensionTable);
     return success();
   }
 
@@ -1444,7 +1201,7 @@ public class MetastoreResource {
   @Path("/dimtables/{dimTableName}")
   public APIResult dropDimensionTable(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimension,
-    @QueryParam("cascade") boolean cascade) {
+    @QueryParam("cascade") boolean cascade) throws LensException {
     return Entity.DIMTABLE.delete(sessionid, dimension, cascade);
   }
 
@@ -1461,12 +1218,7 @@ public class MetastoreResource {
     @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("dimTableName") String dimTableName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXDimensionTable(getSvc().getDimensionTable(sessionid, dimTableName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, dimTableName);
-      throw exc;
-    }
+    return X_CUBE_OBJECT_FACTORY.createXDimensionTable(getSvc().getDimensionTable(sessionid, dimTableName));
   }
 
   /**
@@ -1499,15 +1251,9 @@ public class MetastoreResource {
   @Path("/dimtables/{dimTableName}/storages")
   public APIResult createDimensionStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
-    XStorageTableElement storageTbl) {
+    XStorageTableElement storageTbl) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().addDimTableStorage(sessionid, dimTableName, storageTbl);
-    } catch (LensException e) {
-      checkTableNotFound(e, dimTableName);
-      log.error("Error creating dimension table storage {}:{}", dimTableName, storageTbl.getStorageName(), e);
-      return failure(processLensException(e));
-    }
+    getSvc().addDimTableStorage(sessionid, dimTableName, storageTbl);
     return success();
   }
 
@@ -1539,15 +1285,9 @@ public class MetastoreResource {
   @DELETE
   @Path("/dimtables/{dimTableName}/storages")
   public APIResult dropAllStoragesOfDim(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("dimTableName") String dimTableName) {
+    @PathParam("dimTableName") String dimTableName) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().dropAllStoragesOfDimTable(sessionid, dimTableName);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, dimTableName);
-      log.error("Error dropping storages of dimension table {}", dimTableName, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().dropAllStoragesOfDimTable(sessionid, dimTableName);
     return success();
   }
 
@@ -1564,15 +1304,9 @@ public class MetastoreResource {
   @Path("/dimtables/{dimTableName}/storages/{storage}")
   public APIResult dropStorageOfDim(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
-    @PathParam("storage") String storage) {
+    @PathParam("storage") String storage) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().dropStorageOfDimTable(sessionid, dimTableName, storage);
-    } catch (LensException exc) {
-      checkTableNotFound(exc, dimTableName);
-      log.error("Error dropping storage of dimension table {}:{}", dimTableName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().dropStorageOfDimTable(sessionid, dimTableName, storage);
     return success();
   }
 
@@ -1614,7 +1348,7 @@ public class MetastoreResource {
   public APIResult dropPartitionsOfDimStorageByFilter(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
     @PathParam("storage") String storage,
-    @QueryParam("filter") String filter) {
+    @QueryParam("filter") String filter) throws LensException {
     checkSessionId(sessionid);
     try {
       getSvc().dropPartitionFromStorageByFilter(sessionid, dimTableName, storage, filter);
@@ -1640,15 +1374,9 @@ public class MetastoreResource {
   public APIResult dropPartitionsOfDimStorageByValue(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
     @PathParam("storage") String storage,
-    @QueryParam("values") String values) {
+    @QueryParam("values") String values) throws LensException {
     checkSessionId(sessionid);
-    try {
-      getSvc().dropPartitionFromStorageByValues(sessionid, dimTableName, storage,
-        values);
-    } catch (LensException exc) {
-      log.error("Error dropping partitions on storage of dimension table {}:{}", dimTableName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().dropPartitionFromStorageByValues(sessionid, dimTableName, storage, values);
     return success();
   }
 
@@ -1667,18 +1395,12 @@ public class MetastoreResource {
   public APIResult addPartitionToDimStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
     @PathParam("storage") String storage,
-    XPartition partition) {
+    XPartition partition) throws LensException {
     checkSessionId(sessionid);
     checkNonNullArgs("Partition is null", partition);
     checkNonNullArgs("Partition elements are null", partition.getFactOrDimensionTableName(),
       partition.getUpdatePeriod());
-    try {
-      return successOrPartialOrFailure(getSvc().addPartitionToDimStorage(sessionid, dimTableName, storage, partition),
-        1);
-    } catch (LensException exc) {
-      log.error("Error adding partition to storage of dimension table {}:{}", dimTableName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    return successOrPartialOrFailure(getSvc().addPartitionToDimStorage(sessionid, dimTableName, storage, partition), 1);
   }
 
   /**
@@ -1695,20 +1417,9 @@ public class MetastoreResource {
   @PUT
   @Path("/dimtables/{dimTableName}/storages/{storage}/partition")
   public APIResult updatePartitionOfDimStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("dimTableName") String dimTableName,
-                                               @PathParam("storage") String storage,
-    XPartition partition) {
-    checkSessionId(sessionid);
-    checkNonNullArgs("Partition is null", partition);
-    checkNonNullArgs("Partition elements are null", partition.getFactOrDimensionTableName(),
-      partition.getUpdatePeriod());
-    try {
-      getSvc().updatePartition(sessionid, dimTableName, storage, partition);
-    } catch (LensException exc) {
-      log.error("Error adding partition to storage of dimension table {}:{}", dimTableName, storage, exc);
-      return failure(processLensException(exc));
-    }
-    return success();
+    @PathParam("dimTableName") String dimTableName, @PathParam("storage") String storage,
+    XPartition partition) throws LensException {
+    return checkAndUpdatePartitions(sessionid, dimTableName, storage, partition);
   }
 
   /**
@@ -1726,16 +1437,11 @@ public class MetastoreResource {
   public APIResult addPartitionsToDimStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
     @PathParam("storage") String storage,
-    XPartitionList partitions) {
+    XPartitionList partitions) throws LensException {
     checkSessionId(sessionid);
     checkNonNullPartitionList(partitions);
-    try {
-      return successOrPartialOrFailure(getSvc().addPartitionsToDimStorage(sessionid, dimTableName, storage, partitions),
-        partitions.getPartition().size());
-    } catch (LensException exc) {
-      log.error("Error adding partition to storage of dimension table {}:{}", dimTableName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    return successOrPartialOrFailure(getSvc().addPartitionsToDimStorage(sessionid, dimTableName, storage, partitions),
+      partitions.getPartition().size());
   }
 
   /**
@@ -1754,15 +1460,10 @@ public class MetastoreResource {
   public APIResult updatePartitionsOfDimStorage(@QueryParam("sessionid") LensSessionHandle sessionid,
     @PathParam("dimTableName") String dimTableName,
     @PathParam("storage") String storage,
-    XPartitionList partitions) {
+    XPartitionList partitions) throws LensException {
     checkSessionId(sessionid);
     checkNonNullPartitionList(partitions);
-    try {
-      getSvc().updatePartitions(sessionid, dimTableName, storage, partitions);
-    } catch (LensException exc) {
-      log.error("Error adding partition to storage of dimension table {}:{}", dimTableName, storage, exc);
-      return failure(processLensException(exc));
-    }
+    getSvc().updatePartitions(sessionid, dimTableName, storage, partitions);
     return success();
   }
 
@@ -1776,16 +1477,12 @@ public class MetastoreResource {
    */
   @GET
   @Path("flattened/{tableName}")
-  public JAXBElement<XFlattenedColumns> getFlattenedColumns(
-    @QueryParam("sessionid") LensSessionHandle sessionid,
-    @PathParam("tableName") String tableName, @QueryParam("add_chains") @DefaultValue("true") boolean addChains) {
+  public JAXBElement<XFlattenedColumns> getFlattenedColumns(@QueryParam("sessionid") LensSessionHandle sessionid,
+    @PathParam("tableName") String tableName, @QueryParam("add_chains") @DefaultValue("true") boolean addChains)
+    throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXFlattenedColumns(
-        getSvc().getFlattenedColumns(sessionid, tableName, addChains));
-    } catch (LensException exc) {
-      throw new WebApplicationException(exc);
-    }
+    return X_CUBE_OBJECT_FACTORY.createXFlattenedColumns(
+      getSvc().getFlattenedColumns(sessionid, tableName, addChains));
   }
 
   /**
@@ -1801,12 +1498,7 @@ public class MetastoreResource {
     @QueryParam("sessionid") LensSessionHandle sessionid, @PathParam("tableName") String tableName)
     throws LensException {
     checkSessionId(sessionid);
-    try {
-      return X_CUBE_OBJECT_FACTORY.createXJoinChains(getSvc().getAllJoinChains(sessionid, tableName));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, tableName);
-      throw exc;
-    }
+    return X_CUBE_OBJECT_FACTORY.createXJoinChains(getSvc().getAllJoinChains(sessionid, tableName));
   }
 
   /**
@@ -1823,11 +1515,7 @@ public class MetastoreResource {
     @PathParam("cubeName") String cubeName, @QueryParam("timeDimension") String timeDimension) throws LensException,
     HiveException {
     checkSessionId(sessionid);
-    try {
-      return new DateTime(getSvc().getLatestDateOfCube(sessionid, cubeName, timeDimension));
-    } catch (LensException exc) {
-      throw new WebApplicationException(exc);
-    }
+    return new DateTime(getSvc().getLatestDateOfCube(sessionid, cubeName, timeDimension));
   }
 
   /**
@@ -1847,13 +1535,17 @@ public class MetastoreResource {
     @QueryParam("updatePeriod") String updatePeriod, @QueryParam("timeDimension") String timeDimension)
     throws LensException, HiveException {
     checkSessionId(sessionid);
-    try {
-      return new StringList(getSvc().getPartitionTimelines(sessionid, factName, storage,
-        updatePeriod, timeDimension));
-    } catch (LensException exc) {
-      checkTableNotFound(exc, factName);
-      log.error("Error finding partition timelines for fact: {}", factName, exc);
-      throw exc;
-    }
+    return new StringList(getSvc().getPartitionTimelines(sessionid, factName, storage,
+      updatePeriod, timeDimension));
+  }
+
+  private APIResult checkAndUpdatePartitions(LensSessionHandle sessionid, String table, String storage,
+    XPartition partition) throws LensException {
+    checkSessionId(sessionid);
+    checkNonNullArgs("Partition is null", partition);
+    checkNonNullArgs("Partition elements are null", partition.getFactOrDimensionTableName(),
+      partition.getUpdatePeriod());
+    getSvc().updatePartition(sessionid, table, storage, partition);
+    return success();
   }
 }

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-server/src/test/java/org/apache/lens/server/TestServerRestart.java
----------------------------------------------------------------------
diff --git a/lens-server/src/test/java/org/apache/lens/server/TestServerRestart.java b/lens-server/src/test/java/org/apache/lens/server/TestServerRestart.java
index db46da8..bb57614 100644
--- a/lens-server/src/test/java/org/apache/lens/server/TestServerRestart.java
+++ b/lens-server/src/test/java/org/apache/lens/server/TestServerRestart.java
@@ -325,13 +325,17 @@ public class TestServerRestart extends LensAllApplicationJerseyTest {
 
     final String query = "select COUNT(ID) from test_hive_server_restart";
     Response response = null;
-    while (response == null || response.getStatus() == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
+    while (true) {
       response = execute(target(), Optional.of(lensSessionId), Optional.of(query), defaultMT);
+      if (response != null) {
+        LensAPIResult<QueryHandle> result = response.readEntity(new GenericType<LensAPIResult<QueryHandle>>() {});
+        handle = result.getData();
+        if (handle != null) {
+          break;
+        }
+      }
       Thread.sleep(1000);
     }
-
-    handle = response.readEntity(new GenericType<LensAPIResult<QueryHandle>>() {}).getData();
-
     // Poll for second query, this should finish successfully
     ctx = waitForQueryToFinish(target(), lensSessionId, handle, defaultMT);
     log.info("Final status for {}: {}", handle, ctx.getStatus().getStatus());

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-server/src/test/java/org/apache/lens/server/metastore/TestMetastoreService.java
----------------------------------------------------------------------
diff --git a/lens-server/src/test/java/org/apache/lens/server/metastore/TestMetastoreService.java b/lens-server/src/test/java/org/apache/lens/server/metastore/TestMetastoreService.java
index 1b76ea5..61bc133 100644
--- a/lens-server/src/test/java/org/apache/lens/server/metastore/TestMetastoreService.java
+++ b/lens-server/src/test/java/org/apache/lens/server/metastore/TestMetastoreService.java
@@ -158,9 +158,11 @@ public class TestMetastoreService extends LensJerseyTest {
     assertSuccess(result);
 
     // Create again
-    result = dbTarget.queryParam("sessionid", lensSessionId).queryParam("ignoreIfExisting", false)
-      .request(mediaType).post(getEntityForString(newDb, mediaType), APIResult.class);
-    assertEquals(result.getStatus(), APIResult.Status.FAILED);
+    Response response = dbTarget.queryParam("sessionid", lensSessionId).queryParam("ignoreIfExisting", false)
+      .request(mediaType).post(getEntityForString(newDb, mediaType));
+    assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
+    result = response.readEntity(APIResult.class);
+    assertEquals(result.getStatus(), Status.FAILED);
     log.info(">> Result message " + result.getMessage());
 
     // Drop
@@ -490,9 +492,11 @@ public class TestMetastoreService extends LensJerseyTest {
       assertCubesExistence(cubes.getElements(), LensUtil.<String, Boolean>getHashMap("testCube1", Boolean.TRUE));
       // create invalid derived cube
       XCube dcube = createDerivedCube("testderived", "testCube1", true);
-      result = target.queryParam("sessionid", lensSessionId).request(
+      Response resp = target.queryParam("sessionid", lensSessionId).request(
         mediaType).post(Entity.entity(new GenericEntity<JAXBElement<XCube>>(cubeObjectFactory.createXCube(dcube)){},
-        mediaType), APIResult.class);
+        mediaType));
+      assertEquals(resp.getStatus(), 400);
+      result = resp.readEntity(APIResult.class);
       assertEquals(result.getStatus(), Status.FAILED);
       assertEquals(result.getMessage(), "Problem in submitting entity: Derived cube invalid: Measures "
         + "[random_measure] and Dim Attributes [random_dim] were not present in parent cube testcube1");
@@ -815,9 +819,11 @@ public class TestMetastoreService extends LensJerseyTest {
 
       XDerivedCube dcube = createDerivedCube("test_update_derived", cubeName, true);
       element = cubeObjectFactory.createXCube(dcube);
-      result =
+      Response resp =
         target.queryParam("sessionid", lensSessionId).request(mediaType).post(Entity.entity(
-          new GenericEntity<JAXBElement<XCube>>(element){}, mediaType), APIResult.class);
+          new GenericEntity<JAXBElement<XCube>>(element){}, mediaType));
+      assertEquals(resp.getStatus(), 400);
+      result = resp.readEntity(APIResult.class);
       assertEquals(result.getStatus(), Status.FAILED);
       assertEquals(result.getMessage(), "Problem in submitting entity: Derived cube invalid: Measures "
         + "[random_measure] and Dim Attributes [random_dim] were not present in parent cube test_update");
@@ -2164,12 +2170,12 @@ public class TestMetastoreService extends LensJerseyTest {
       // Create Partition with prepared partition spec elements
       XPartition xp = createPartition(fact1TableName, Arrays.asList(timePartSpecElement1, timePartSpecElement2));
 
-      APIResult partAddResult = target().path("metastore/facts/").path(fact1TableName)
+      Response response = target().path("metastore/facts/").path(fact1TableName)
         .path("storages/" + storages[0] + "/partition")
         .queryParam("sessionid", lensSessionId).request(mediaType)
         .post(Entity.entity(new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp)){},
-            mediaType),
-          APIResult.class);
+            mediaType));
+      APIResult partAddResult = response.readEntity(APIResult.class);
       assertSuccess(partAddResult);
 
       // End: Setup
@@ -2227,11 +2233,12 @@ public class TestMetastoreService extends LensJerseyTest {
       XPartitionList partList = new XPartitionList();
       partList.getPartition().add(createPartition(table, DateUtils.addHours(partDate, 1)));
       partList.getPartition().add(createPartition(table, DateUtils.addHours(partDate, -300)));
-      partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
+      Response response = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
               .queryParam("sessionid", lensSessionId).request(mediaType)
               .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(
                       cubeObjectFactory.createXPartitionList(partList)) {
-              }, mediaType), APIResult.class);
+              }, mediaType));
+      partAddResult = response.readEntity(APIResult.class);
       assertEquals(partAddResult.getStatus(), Status.PARTIAL);
     } finally {
       setCurrentDatabase(prevDb, mediaType);
@@ -2284,10 +2291,12 @@ public class TestMetastoreService extends LensJerseyTest {
       XPartition xp2 = createPartition(table, partDate);
       xp2.getTimePartitionSpec().getPartSpecElement()
         .add(createTimePartSpecElement(partDate, "non_existant_time_part"));
-      partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partition")
+      Response response = target().path("metastore/facts/").path(table).path("storages/S2/partition")
         .queryParam("sessionid", lensSessionId).request(mediaType)
         .post(Entity.entity(new GenericEntity<JAXBElement<XPartition>>(cubeObjectFactory.createXPartition(xp2)){},
-          mediaType), APIResult.class);
+          mediaType));
+      assertEquals(response.getStatus(), 400);
+      partAddResult = response.readEntity(APIResult.class);
       assertEquals(partAddResult.getStatus(), Status.FAILED);
       assertEquals(partAddResult.getMessage(), "No timeline found for fact=testFactStoragePartitions, storage=S2, "
         + "update period=HOURLY, partition column=non_existant_time_part.");
@@ -2394,11 +2403,12 @@ public class TestMetastoreService extends LensJerseyTest {
       }
 
       // Try adding in batch with one partition being wrong wrt partition column.
-      partAddResult = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
+      response = target().path("metastore/facts/").path(table).path("storages/S2/partitions")
         .queryParam("sessionid", lensSessionId).request(mediaType)
         .post(Entity.entity(new GenericEntity<JAXBElement<XPartitionList>>(cubeObjectFactory
-          .createXPartitionList(toXPartitionList(xp2))){}, mediaType),
-          APIResult.class);
+          .createXPartitionList(toXPartitionList(xp2))){}, mediaType));
+      assertEquals(response.getStatus(), 400);
+      partAddResult = response.readEntity(APIResult.class);
       assertEquals(partAddResult.getStatus(), Status.FAILED);
       assertEquals(partAddResult.getMessage(), "No timeline found for fact=testFactStoragePartitions, storage=S2, "
         + "update period=HOURLY, partition column=non_existant_time_part.");
@@ -2757,7 +2767,7 @@ public class TestMetastoreService extends LensJerseyTest {
       // get a cube table
       Response response = target.path("testhiveCube").queryParam(
         "sessionid", lensSessionId).request(mediaType).get(Response.class);
-      assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
+      assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
 
       // get a non existing table
       response = target.path("nonexisting").queryParam(


[4/4] lens git commit: LENS-1187: Refactor exception handling in MetastoreResource

Posted by pr...@apache.org.
LENS-1187: Refactor exception handling in MetastoreResource


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

Branch: refs/heads/master
Commit: 4bcb7aa9615d6c83f84fef2e115ca51d66ef6b07
Parents: 9068e3c
Author: Rajat Khandelwal <pr...@apache.org>
Authored: Fri Jul 1 15:32:31 2016 +0530
Committer: Rajat Khandelwal <ra...@gmail.com>
Committed: Fri Jul 1 15:32:31 2016 +0530

----------------------------------------------------------------------
 checkstyle/src/main/resources/checkstyle.xml    |   4 +-
 lens-api/src/main/resources/lens-errors.conf    |   5 +
 .../lens/cli/TestLensDatabaseCommands.java      |  11 +-
 .../lens/cli/TestLensNativeTableCommands.java   |   5 +
 .../apache/lens/cli/TestLensQueryCommands.java  |  10 +-
 .../lens/cube/error/LensCubeErrorCode.java      |   3 +-
 .../lens/cube/metadata/CubeMetastoreClient.java | 758 +++++++++----------
 .../apache/lens/cube/metadata/JoinChain.java    |   7 +-
 .../org/apache/lens/cube/metadata/Storage.java  |   6 +-
 .../apache/lens/cube/parse/CandidateFact.java   |   7 +-
 .../lens/cube/parse/CandidateTableResolver.java |  67 +-
 .../lens/cube/parse/CubeQueryContext.java       |   3 +-
 .../apache/lens/cube/parse/FieldValidator.java  |   7 +-
 .../apache/lens/cube/parse/JoinResolver.java    |   2 +-
 .../lens/cube/parse/StorageTableResolver.java   |  56 +-
 .../apache/lens/driver/cube/RewriterPlan.java   |   4 +-
 .../cube/metadata/TestCubeMetastoreClient.java  |  66 +-
 .../apache/lens/cube/parse/CubeTestSetup.java   |  24 +-
 .../lens/driver/hive/TestRemoteHiveDriver.java  |   2 +
 .../lens/driver/jdbc/ColumnarSQLRewriter.java   |   6 +-
 .../java/org/apache/lens/server/BaseApp.java    |   2 +
 .../org/apache/lens/server/BaseLensService.java |  13 +
 .../org/apache/lens/server/LensApplication.java |   3 +-
 .../org/apache/lens/server/LensServices.java    |   2 +-
 .../server/error/GenericExceptionMapper.java    |   7 +-
 .../metastore/CubeMetastoreServiceImpl.java     | 533 +++----------
 .../server/metastore/MetastoreResource.java     | 602 ++++-----------
 .../apache/lens/server/TestServerRestart.java   |  12 +-
 .../server/metastore/TestMetastoreService.java  |  46 +-
 29 files changed, 845 insertions(+), 1428 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/checkstyle/src/main/resources/checkstyle.xml
----------------------------------------------------------------------
diff --git a/checkstyle/src/main/resources/checkstyle.xml b/checkstyle/src/main/resources/checkstyle.xml
index 0a37d01..17e55ea 100644
--- a/checkstyle/src/main/resources/checkstyle.xml
+++ b/checkstyle/src/main/resources/checkstyle.xml
@@ -198,7 +198,9 @@
             <property name="ignoreSetter" value="true"/>
         </module-->
         <module name="IllegalInstantiation"/>
-        <module name="InnerAssignment"/>
+        <module name="InnerAssignment">
+            <property name="tokens" value="BAND_ASSIGN,BOR_ASSIGN,BSR_ASSIGN,BXOR_ASSIGN,DIV_ASSIGN,MINUS_ASSIGN,MOD_ASSIGN,PLUS_ASSIGN,SL_ASSIGN,SR_ASSIGN,STAR_ASSIGN"/>
+        </module>
         <!--  module name="MissingSwitchDefault"/-->
         <!--  module name="RedundantThrows"/-->
         <module name="SimplifyBooleanExpression"/>

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-api/src/main/resources/lens-errors.conf
----------------------------------------------------------------------
diff --git a/lens-api/src/main/resources/lens-errors.conf b/lens-api/src/main/resources/lens-errors.conf
index 0d7b345..537b458 100644
--- a/lens-api/src/main/resources/lens-errors.conf
+++ b/lens-api/src/main/resources/lens-errors.conf
@@ -347,6 +347,11 @@ lensCubeErrorsForMetastore = [
     errorMsg = "The Expression %s is Not Parsable."
   }
 
+  {
+    errorCode = 3104
+    httpStatusCode = ${NOT_FOUND}
+    errorMsg = "%s %s Not found."
+  }
 ]
 
 lensDriverErrors = [

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java
index 705aace..dfa02ec 100644
--- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java
+++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java
@@ -23,6 +23,9 @@ import static org.testng.Assert.*;
 import java.io.File;
 import java.net.URISyntaxException;
 
+import javax.ws.rs.InternalServerErrorException;
+
+import org.apache.lens.api.APIResult;
 import org.apache.lens.cli.commands.LensCubeCommands;
 import org.apache.lens.cli.commands.LensDatabaseCommands;
 import org.apache.lens.client.LensClient;
@@ -79,7 +82,13 @@ public class TestLensDatabaseCommands extends LensCliApplicationTest {
     assertEquals(result, "Successfully switched to default");
     assertFalse(cubeCommand.showCubes().contains("sample_cube"));
     if (cascade) {
-      assertEquals(command.dropDatabase(myDatabase, false), "failed");
+      try {
+        command.dropDatabase(myDatabase, false);
+        fail("Should have failed");
+      } catch(InternalServerErrorException ignored) {
+        APIResult apiResult = ignored.getResponse().readEntity(APIResult.class);
+        assertTrue(apiResult.getMessage().contains("my_db is not empty"));
+      }
     }
     result = command.dropDatabase(myDatabase, cascade);
     assertEquals(result, "succeeded");

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java
index 80dd0d1..6ea6fe4 100644
--- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java
+++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java
@@ -24,6 +24,9 @@ import org.apache.lens.cli.commands.LensNativeTableCommands;
 import org.apache.lens.client.LensClient;
 import org.apache.lens.server.LensServerTestUtil;
 
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.session.SessionState;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
@@ -47,6 +50,7 @@ public class TestLensNativeTableCommands extends LensCliApplicationTest {
   public void testNativeTableCommands() throws Exception {
     LensClient client = new LensClient();
     try {
+      SessionState.start(new HiveConf());
       LensNativeTableCommands command = new LensNativeTableCommands();
       command.setClient(client);
       LOG.debug("Starting to test nativetable commands");
@@ -65,6 +69,7 @@ public class TestLensNativeTableCommands extends LensCliApplicationTest {
     } finally {
       LensServerTestUtil.dropHiveTable("test_native_table_command");
       client.closeConnection();
+      SessionState.detachSession();
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java
----------------------------------------------------------------------
diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java
index 4d8341a..9274bf1 100644
--- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java
+++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java
@@ -151,7 +151,15 @@ public class TestLensQueryCommands extends LensCliApplicationTest {
     }
     try {
       String result = qCom.executeQuery(sql, false, "testQuerySync");
-      assertTrue(result.contains("1\tfirst"), result);
+      if (shouldPass) {
+        assertTrue(result.contains("1\tfirst"), result);
+      } else {
+        assertTrue(result.contains("Error"));
+        if (closeConn) {
+          closeClientConnection(qCom);
+        }
+        return;
+      }
     } catch (Exception e) {
       if (closeConn) {
         closeClientConnection(qCom);

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/error/LensCubeErrorCode.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/error/LensCubeErrorCode.java b/lens-cube/src/main/java/org/apache/lens/cube/error/LensCubeErrorCode.java
index e49b58b..571b481 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/error/LensCubeErrorCode.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/error/LensCubeErrorCode.java
@@ -60,7 +60,8 @@ public enum LensCubeErrorCode {
   // Error codes greater than 3100 are errors while doing a metastore operation.
   ERROR_IN_ENTITY_DEFINITION(3101, 100),
   TIMELINE_ABSENT(3102, 100),
-  EXPRESSION_NOT_PARSABLE(3103, 1500);
+  EXPRESSION_NOT_PARSABLE(3103, 1500),
+  ENTITY_NOT_FOUND(3104, 1500);
 
   public LensErrorInfo getLensErrorInfo() {
     return this.errorInfo;

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeMetastoreClient.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeMetastoreClient.java b/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeMetastoreClient.java
index de8f23b..61f9f99 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeMetastoreClient.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeMetastoreClient.java
@@ -180,12 +180,12 @@ public class CubeMetastoreClient {
   }
 
   public void updatePartition(String fact, String storageName, Partition partition)
-    throws HiveException, InvalidOperationException {
+    throws HiveException, InvalidOperationException, LensException {
     updatePartitions(fact, storageName, Collections.singletonList(partition));
   }
 
   public void updatePartitions(String factOrDimtableName, String storageName, List<Partition> partitions)
-    throws HiveException, InvalidOperationException {
+    throws HiveException, InvalidOperationException, LensException {
     List<Partition> partitionsToAlter = Lists.newArrayList();
     partitionsToAlter.addAll(partitions);
     partitionsToAlter.addAll(getAllLatestPartsEquivalentTo(factOrDimtableName, storageName, partitions));
@@ -193,7 +193,7 @@ public class CubeMetastoreClient {
   }
 
   private List<Partition> getAllLatestPartsEquivalentTo(String factOrDimtableName, String storageName,
-    List<Partition> partitions) throws HiveException {
+    List<Partition> partitions) throws HiveException, LensException {
     if (isFactTable(factOrDimtableName)) {
       return Lists.newArrayList();
     }
@@ -245,11 +245,18 @@ public class CubeMetastoreClient {
       }
       CubeTableType type = CubeTableType.valueOf(typeProperty);
       return type == CubeTableType.CUBE || type == CubeTableType.DIMENSION;
-    } catch (HiveException e) {
+    } catch (LensException e) {
       return false;
     }
   }
 
+  public void verifyStorageExists(AbstractCubeTable cdt, String storage) throws LensException {
+    if (cdt.getStorages() == null || !cdt.getStorages().contains(storage)) {
+      throw new LensException(LensCubeErrorCode.ENTITY_NOT_FOUND.getLensErrorInfo(), "storage " + storage + " for",
+        cdt.getTableType().name().toLowerCase() + " " + cdt.getName());
+    }
+  }
+
 
   /**
    * In-memory storage of {@link PartitionTimeline} objects for each valid
@@ -537,7 +544,7 @@ public class CubeMetastoreClient {
   }
 
   private void createOrAlterStorageHiveTable(Table parent, String storage, StorageTableDesc crtTblDesc)
-    throws HiveException {
+    throws LensException {
     try {
       Table tbl = getStorage(storage).getStorageTable(getClient(), parent, crtTblDesc);
       if (tableExists(tbl.getTableName())) {
@@ -548,12 +555,12 @@ public class CubeMetastoreClient {
         // do get to update cache
         getTable(tbl.getTableName());
       }
-    } catch (Exception e) {
-      throw new HiveException("Exception creating table", e);
+    } catch (HiveException e) {
+      throw new LensException("Exception creating table", e);
     }
   }
 
-  private Table createCubeHiveTable(AbstractCubeTable table) throws HiveException {
+  private Table createCubeHiveTable(AbstractCubeTable table) throws LensException {
     try {
       Table tbl = getClient().newTable(table.getName().toLowerCase());
       tbl.setTableType(TableType.MANAGED_TABLE);
@@ -564,11 +571,11 @@ public class CubeMetastoreClient {
       getTable(tbl.getTableName());
       return tbl;
     } catch (Exception e) {
-      throw new HiveException("Exception creating table", e);
+      throw new LensException("Exception creating table", e);
     }
   }
 
-  public void createStorage(Storage storage) throws HiveException {
+  public void createStorage(Storage storage) throws LensException {
     createCubeHiveTable(storage);
     // do a get to update cache
     getStorage(storage.getName());
@@ -578,9 +585,9 @@ public class CubeMetastoreClient {
    * Create cube in metastore defined by {@link Cube} or {@link DerivedCube} object
    *
    * @param cube the {@link Cube} object.
-   * @throws HiveException
+   * @throws LensException
    */
-  public void createCube(CubeInterface cube) throws HiveException {
+  public void createCube(CubeInterface cube) throws LensException {
     createCubeHiveTable((AbstractCubeTable) cube);
     // do a get to update cache
     getCube(cube.getName());
@@ -592,10 +599,10 @@ public class CubeMetastoreClient {
    * @param name       Name of the cube
    * @param measures   Measures of the cube
    * @param dimensions Dimensions of the cube
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCube(String name, Set<CubeMeasure> measures, Set<CubeDimAttribute> dimensions)
-    throws HiveException {
+    throws LensException {
     Cube cube = new Cube(name, measures, dimensions);
     createCube(cube);
   }
@@ -607,10 +614,10 @@ public class CubeMetastoreClient {
    * @param measures   Measures of the cube
    * @param dimensions Dimensions of the cube
    * @param properties Properties of the cube
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCube(String name, Set<CubeMeasure> measures, Set<CubeDimAttribute> dimensions,
-    Map<String, String> properties) throws HiveException {
+    Map<String, String> properties) throws LensException {
     Cube cube = new Cube(name, measures, dimensions, properties);
     createCube(cube);
   }
@@ -622,10 +629,10 @@ public class CubeMetastoreClient {
    * @param measures   Measures of the cube
    * @param dimensions Dimensions of the cube
    * @param properties Properties of the cube
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCube(String name, Set<CubeMeasure> measures, Set<CubeDimAttribute> dimensions,
-    Set<ExprColumn> expressions, Map<String, String> properties) throws HiveException {
+    Set<ExprColumn> expressions, Map<String, String> properties) throws LensException {
     Cube cube = new Cube(name, measures, dimensions, expressions, null, properties, 0L);
     createCube(cube);
   }
@@ -639,11 +646,11 @@ public class CubeMetastoreClient {
    * @param expressions Expressions of the cube
    * @param chains      JoinChains of the cube
    * @param properties  Properties of the cube
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCube(String name, Set<CubeMeasure> measures, Set<CubeDimAttribute> dimensions,
     Set<ExprColumn> expressions, Set<JoinChain> chains, Map<String, String> properties)
-    throws HiveException {
+    throws LensException {
     Cube cube = new Cube(name, measures, dimensions, expressions, chains, properties, 0L);
     createCube(cube);
   }
@@ -655,10 +662,10 @@ public class CubeMetastoreClient {
    * @param attributes Attributes of the dimension
    * @param properties Properties of the dimension
    * @param weight     Weight of the dimension
-   * @throws HiveException
+   * @throws LensException
    */
   public void createDimension(String name, Set<CubeDimAttribute> attributes, Map<String, String> properties,
-    double weight) throws HiveException {
+    double weight) throws LensException {
     Dimension dim = new Dimension(name, attributes, properties, weight);
     createDimension(dim);
   }
@@ -667,9 +674,9 @@ public class CubeMetastoreClient {
    * Create dimension in metastore defined by {@link Dimension} object
    *
    * @param dim the {@link Dimension} object.
-   * @throws HiveException
+   * @throws LensException
    */
-  public void createDimension(Dimension dim) throws HiveException {
+  public void createDimension(Dimension dim) throws LensException {
     createCubeHiveTable(dim);
     // do a get to update cache
     getDimension(dim.getName());
@@ -684,10 +691,10 @@ public class CubeMetastoreClient {
    * @param dimensions Dimensions of the derived cube
    * @param properties Properties of the derived cube
    * @param weight     Weight of the derived cube
-   * @throws HiveException
+   * @throws LensException
    */
   public void createDerivedCube(String parent, String name, Set<String> measures, Set<String> dimensions,
-    Map<String, String> properties, double weight) throws HiveException, LensException {
+    Map<String, String> properties, double weight) throws LensException {
     DerivedCube cube = new DerivedCube(name, measures, dimensions, properties, weight, (Cube) getCube(parent));
     createCube(cube);
   }
@@ -702,11 +709,11 @@ public class CubeMetastoreClient {
    * @param weight                  Weight of the cube
    * @param properties              Properties of fact table
    * @param storageTableDescs       Map of storage to its storage table description
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCubeFactTable(String cubeName, String factName, List<FieldSchema> columns,
     Map<String, Set<UpdatePeriod>> storageAggregatePeriods, double weight, Map<String, String> properties,
-    Map<String, StorageTableDesc> storageTableDescs) throws HiveException {
+    Map<String, StorageTableDesc> storageTableDescs) throws LensException {
     CubeFactTable factTable =
       new CubeFactTable(cubeName, factName, columns, storageAggregatePeriods, weight, properties);
     createCubeTable(factTable, storageTableDescs);
@@ -721,10 +728,10 @@ public class CubeMetastoreClient {
    * @param segments             Participating cube segements
    * @param weight                   Weight of segmentation
    * @param properties               Properties of segmentation
-   * @throws HiveException
+   * @throws LensException
    */
   public void createSegmentation(String baseCubeName, String segmentationName, Set<Segment> segments,
-                                     double weight, Map<String, String> properties) throws HiveException {
+                                     double weight, Map<String, String> properties) throws LensException {
     Segmentation cubeSeg =
             new Segmentation(baseCubeName, segmentationName, segments, weight, properties);
     createSegmentation(cubeSeg);
@@ -742,11 +749,11 @@ public class CubeMetastoreClient {
    * @param storageNames      Storages on which dimension is available without any dumps
    * @param properties        Properties of dimension table
    * @param storageTableDescs Map of storage to its storage table description
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCubeDimensionTable(String dimName, String dimTblName, List<FieldSchema> columns, double weight,
     Set<String> storageNames, Map<String, String> properties, Map<String, StorageTableDesc> storageTableDescs)
-    throws HiveException {
+    throws LensException {
     CubeDimensionTable dimTable =
       new CubeDimensionTable(dimName, dimTblName, columns, weight, storageNames, properties);
     createCubeTable(dimTable, storageTableDescs);
@@ -764,11 +771,11 @@ public class CubeMetastoreClient {
    * @param dumpPeriods       Storage names and their dump periods on which dimension is available
    * @param properties        properties of dimension table
    * @param storageTableDescs Map of storage to its storage table description
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCubeDimensionTable(String dimName, String dimTblName, List<FieldSchema> columns, double weight,
     Map<String, UpdatePeriod> dumpPeriods, Map<String, String> properties,
-    Map<String, StorageTableDesc> storageTableDescs) throws HiveException {
+    Map<String, StorageTableDesc> storageTableDescs) throws LensException {
     CubeDimensionTable dimTable = new CubeDimensionTable(dimName, dimTblName, columns, weight, dumpPeriods, properties);
     createCubeTable(dimTable, storageTableDescs);
     // do a get to update cache
@@ -780,10 +787,10 @@ public class CubeMetastoreClient {
    *
    * @param cubeTable         Can be fact or dimension table
    * @param storageTableDescs Map of storage to its storage table description
-   * @throws HiveException
+   * @throws LensException
    */
   public void createCubeTable(AbstractCubeTable cubeTable, Map<String, StorageTableDesc> storageTableDescs)
-    throws HiveException {
+    throws LensException {
     // create virtual cube table in metastore
     Table cTable = createCubeHiveTable(cubeTable);
 
@@ -796,7 +803,7 @@ public class CubeMetastoreClient {
   }
 
   public void createSegmentation(Segmentation cubeSeg)
-    throws HiveException {
+    throws LensException {
     // create virtual cube table in metastore
     createCubeHiveTable(cubeSeg);
   }
@@ -808,13 +815,14 @@ public class CubeMetastoreClient {
    * @param storage          The storage
    * @param updatePeriods    Update periods of the fact on the storage
    * @param storageTableDesc The storage table description
-   * @throws HiveException
+   * @throws LensException
    */
   public void addStorage(CubeFactTable fact, String storage, Set<UpdatePeriod> updatePeriods,
-    StorageTableDesc storageTableDesc) throws HiveException {
+    StorageTableDesc storageTableDesc) throws LensException {
     fact.addStorage(storage, updatePeriods);
-    createOrAlterStorageHiveTable(getTable(fact.getName()), storage, storageTableDesc);
-    alterCubeTable(fact.getName(), getTable(fact.getName()), fact);
+    createOrAlterStorageHiveTable(getTableWithTypeFailFast(fact.getName(), CubeTableType.FACT),
+      storage, storageTableDesc);
+    alterCubeTable(fact.getName(), getTableWithTypeFailFast(fact.getName(), CubeTableType.FACT), fact);
     updateFactCache(fact.getName());
   }
 
@@ -825,13 +833,14 @@ public class CubeMetastoreClient {
    * @param storage          The storage
    * @param dumpPeriod       The dumpPeriod if any, null otherwise
    * @param storageTableDesc The storage table description
-   * @throws HiveException
+   * @throws LensException
    */
   public void addStorage(CubeDimensionTable dim, String storage, UpdatePeriod dumpPeriod,
-    StorageTableDesc storageTableDesc) throws HiveException {
+    StorageTableDesc storageTableDesc) throws LensException {
     dim.alterSnapshotDumpPeriod(storage, dumpPeriod);
-    createOrAlterStorageHiveTable(getTable(dim.getName()), storage, storageTableDesc);
-    alterCubeTable(dim.getName(), getTable(dim.getName()), dim);
+    createOrAlterStorageHiveTable(getTableWithTypeFailFast(dim.getName(), CubeTableType.DIM_TABLE),
+      storage, storageTableDesc);
+    alterCubeTable(dim.getName(), getTableWithTypeFailFast(dim.getName(), CubeTableType.DIM_TABLE), dim);
     updateDimCache(dim.getName());
   }
 
@@ -842,32 +851,34 @@ public class CubeMetastoreClient {
    *
    * @param partSpec    The storage partition description
    * @param storageName The storage object
+   * @param type
    * @throws HiveException
    */
-  public List<Partition> addPartition(StoragePartitionDesc partSpec, String storageName)
+  public List<Partition> addPartition(StoragePartitionDesc partSpec, String storageName, CubeTableType type)
     throws HiveException, LensException {
-    return addPartitions(Collections.singletonList(partSpec), storageName);
+    return addPartitions(Collections.singletonList(partSpec), storageName, type);
   }
 
   /** batch addition */
-  public List<Partition> addPartitions(List<StoragePartitionDesc> storagePartitionDescs, String storageName)
+  public List<Partition> addPartitions(List<StoragePartitionDesc> storagePartitionDescs, String storageName,
+    CubeTableType type)
     throws HiveException, LensException {
     List<Partition> partsAdded = Lists.newArrayList();
     for (Map.Entry<String, Map<UpdatePeriod, List<StoragePartitionDesc>>> group : groupPartitionDescs(
       storagePartitionDescs).entrySet()) {
       String factOrDimtable = group.getKey();
       for (Map.Entry<UpdatePeriod, List<StoragePartitionDesc>> entry : group.getValue().entrySet()) {
-        partsAdded.addAll(addPartitions(factOrDimtable, storageName, entry.getKey(), entry.getValue()));
+        partsAdded.addAll(addPartitions(factOrDimtable, storageName, entry.getKey(), entry.getValue(), type));
       }
     }
     return partsAdded;
   }
 
   private List<Partition> addPartitions(String factOrDimTable, String storageName, UpdatePeriod updatePeriod,
-    List<StoragePartitionDesc> storagePartitionDescs) throws HiveException, LensException {
+    List<StoragePartitionDesc> storagePartitionDescs, CubeTableType type) throws HiveException, LensException {
     String storageTableName = getStorageTableName(factOrDimTable.trim(),
       Storage.getPrefix(storageName.trim())).toLowerCase();
-    if (getDimensionTable(factOrDimTable) != null) {
+    if (type == CubeTableType.DIM_TABLE) {
       // Adding partition in dimension table.
       Map<Map<String, String>, LatestInfo> latestInfos = Maps.newHashMap();
       for (Map.Entry<Map<String, String>, List<StoragePartitionDesc>> entry : groupByNonTimePartitions(
@@ -886,7 +897,7 @@ public class CubeMetastoreClient {
       }
       latestLookupCache.add(storageTableName);
       return partsAdded;
-    } else {
+    } else if (type == CubeTableType.FACT) {
       List<Partition> partsAdded = new ArrayList<>();
       // first update in memory, then add to hive table's partitions. delete is reverse.
       partitionTimelineCache.updateForAddition(factOrDimTable, storageName, updatePeriod,
@@ -900,18 +911,20 @@ public class CubeMetastoreClient {
       // update hive table
       alterTablePartitionCache(getStorageTableName(factOrDimTable, Storage.getPrefix(storageName)));
       return partsAdded;
+    } else {
+      throw new LensException("Can't add partitions to anything other than fact or dimtable");
     }
   }
 
   private Date getStorageTableStartDate(String storageTable, String factTableName)
-    throws HiveException, LensException {
+    throws LensException {
     List<Date> startDates = getStorageTimes(storageTable, MetastoreUtil.getStoragetableStartTimesKey());
     startDates.add(getFactTable(factTableName).getStartTime());
     return Collections.max(startDates);
   }
 
   private Date getStorageTableEndDate(String storageTable, String factTableName)
-    throws HiveException, LensException {
+    throws LensException {
     List<Date> endDates = getStorageTimes(storageTable, MetastoreUtil.getStoragetableEndTimesKey());
     endDates.add(getFactTable(factTableName).getEndTime());
     return Collections.min(endDates);
@@ -950,7 +963,7 @@ public class CubeMetastoreClient {
           timeSpecs.get(entry.getKey()).add(entry.getValue());
         } else {
           if (!skippedParts.containsKey(entry.getKey())) {
-            skippedParts.put(entry.getKey(), Sets.<Date>newHashSet(entry.getValue()));
+            skippedParts.put(entry.getKey(), Sets.newHashSet(entry.getValue()));
           } else {
             skippedParts.get(entry.getKey()).add(entry.getValue());
           }
@@ -989,7 +1002,7 @@ public class CubeMetastoreClient {
    * @param storageTableName  storage table name
    * @throws HiveException
    */
-  private void alterTablePartitionCache(String storageTableName) throws HiveException {
+  private void alterTablePartitionCache(String storageTableName) throws HiveException, LensException {
     Table table = getTable(storageTableName);
     Map<String, String> params = table.getParameters();
     if (partitionTimelineCache.get(storageTableName) != null) {
@@ -1011,7 +1024,7 @@ public class CubeMetastoreClient {
 
   private LatestInfo getDimTableLatestInfo(String storageTableName, Map<String, String> nonTimeParts,
     Map<String, TreeSet<Date>> timePartSpecs,
-    UpdatePeriod updatePeriod) throws HiveException {
+    UpdatePeriod updatePeriod) throws HiveException, LensException {
     Table hiveTable = getHiveTable(storageTableName);
     String timePartColsStr = hiveTable.getTTable().getParameters().get(MetastoreConstants.TIME_PART_COLUMNS);
     if (timePartColsStr != null) {
@@ -1242,22 +1255,22 @@ public class CubeMetastoreClient {
 
   public boolean factPartitionExists(String factName, String storageName, UpdatePeriod updatePeriod,
                                      Map<String, Date> partitionTimestamp,
-                                     Map<String, String> partSpec) throws HiveException {
+                                     Map<String, String> partSpec) throws HiveException, LensException {
     String storageTableName = getFactOrDimtableStorageTableName(factName, storageName);
     return partitionExists(storageTableName, updatePeriod, partitionTimestamp, partSpec);
   }
 
   public boolean partitionExists(String storageTableName, UpdatePeriod updatePeriod,
-    Map<String, Date> partitionTimestamps) throws HiveException {
+    Map<String, Date> partitionTimestamps) throws HiveException, LensException {
     return partitionExists(storageTableName, getPartitionSpec(updatePeriod, partitionTimestamps));
   }
 
-  public boolean partitionExistsByFilter(String cubeTableName, String storageName, String filter) throws HiveException {
+  public boolean partitionExistsByFilter(String cubeTableName, String storageName, String filter) throws LensException {
     return partitionExistsByFilter(getStorageTableName(cubeTableName, Storage.getPrefix(storageName)),
       filter);
   }
 
-  public boolean partitionExistsByFilter(String storageTableName, String filter) throws HiveException {
+  public boolean partitionExistsByFilter(String storageTableName, String filter) throws LensException {
     int parts;
     Table tbl;
     try {
@@ -1268,12 +1281,12 @@ public class CubeMetastoreClient {
     try {
       parts = getClient().getNumPartitionsByFilter(tbl, filter);
     } catch (Exception e) {
-      throw new HiveException("Could not find partitions for given filter", e);
+      throw new LensException("Could not find partitions for given filter", e);
     }
     return parts > 0;
   }
 
-  public List<Partition> getAllParts(String storageTableName) throws HiveException {
+  public List<Partition> getAllParts(String storageTableName) throws HiveException, LensException {
     return getClient().getPartitions(getHiveTable(storageTableName));
   }
 
@@ -1295,24 +1308,24 @@ public class CubeMetastoreClient {
   }
 
   boolean partitionExists(String storageTableName, UpdatePeriod updatePeriod, Map<String, Date> partitionTimestamps,
-    Map<String, String> nonTimePartSpec) throws HiveException {
+    Map<String, String> nonTimePartSpec) throws HiveException, LensException {
     HashMap<String, String> partSpec = new HashMap<>(nonTimePartSpec);
     partSpec.putAll(getPartitionSpec(updatePeriod, partitionTimestamps));
     return partitionExists(storageTableName, partSpec);
   }
 
-  private boolean partitionExists(String storageTableName, Map<String, String> partSpec) throws HiveException {
+  private boolean partitionExists(String storageTableName, Map<String, String> partSpec) throws LensException {
     try {
       Table storageTbl = getTable(storageTableName);
       Partition p = getClient().getPartition(storageTbl, partSpec, false);
       return (p != null && p.getTPartition() != null);
     } catch (HiveException e) {
-      throw new HiveException("Could not check whether table exists", e);
+      throw new LensException("Could not check whether table exists", e);
     }
   }
 
   boolean dimPartitionExists(String dimTblName, String storageName, Map<String, Date> partitionTimestamps)
-    throws HiveException {
+    throws HiveException, LensException {
     String storageTableName = getFactOrDimtableStorageTableName(dimTblName, storageName);
     return partitionExists(storageTableName, getDimensionTable(dimTblName).getSnapshotDumpPeriods().get(storageName),
             partitionTimestamps);
@@ -1329,7 +1342,7 @@ public class CubeMetastoreClient {
   }
 
   private boolean dimTableLatestPartitionExistsInMetastore(String storageTableName, String latestPartCol)
-    throws HiveException {
+    throws LensException {
     return partitionExistsByFilter(storageTableName, StorageConstants.getLatestPartFilter(latestPartCol));
   }
 
@@ -1352,13 +1365,13 @@ public class CubeMetastoreClient {
    *
    * @param tableName table name
    * @return {@link Table} object corresponding to the name
-   * @throws HiveException
+   * @throws LensException
    */
-  public Table getHiveTable(String tableName) throws HiveException {
+  public Table getHiveTable(String tableName) throws LensException {
     return getTable(tableName);
   }
 
-  public List<String> getTimePartColNamesOfTable(String storageTableName) throws HiveException {
+  public List<String> getTimePartColNamesOfTable(String storageTableName) throws LensException {
     return getTimePartColNamesOfTable(getTable(storageTableName));
   }
 
@@ -1370,8 +1383,42 @@ public class CubeMetastoreClient {
     }
     return ret == null ? new ArrayList<String>() : ret;
   }
-
-  public Table getTable(String tableName) throws HiveException {
+  public Table getTableWithTypeFailFast(String tableName, CubeTableType type) throws LensException {
+    return getTableWithType(tableName, type, true);
+  }
+  public Table getTableWithType(String tableName, CubeTableType type, boolean throwException) throws LensException {
+    String typeName = type == null ? "nativetable" : type.name().toLowerCase();
+    Table table = getTable(tableName, false);
+    if (table == null) {
+      if (throwException) {
+        throw new LensException(LensCubeErrorCode.ENTITY_NOT_FOUND.getLensErrorInfo(), typeName, tableName);
+      } else {
+        return null;
+      }
+    }
+    if (type == null && table.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY) != null) {
+      if (throwException) {
+        throw new LensException(LensCubeErrorCode.ENTITY_NOT_FOUND.getLensErrorInfo(), typeName, tableName);
+      } else {
+        return null;
+      }
+    }
+    if (type != null) {
+      String typeStr = table.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY);
+      if (typeStr == null || CubeTableType.valueOf(typeStr.toUpperCase()) != type) {
+        if (throwException) {
+          throw new LensException(LensCubeErrorCode.ENTITY_NOT_FOUND.getLensErrorInfo(), typeName, tableName);
+        } else {
+          return null;
+        }
+      }
+    }
+    return table;
+  }
+  public Table getTable(String tableName) throws LensException {
+    return getTable(tableName, true);
+  }
+  public Table getTable(String tableName, boolean throwException) throws LensException {
     Table tbl;
     try {
       tableName = tableName.trim().toLowerCase();
@@ -1379,8 +1426,8 @@ public class CubeMetastoreClient {
       if (tbl == null) {
         synchronized (allHiveTables) {
           if (!allHiveTables.containsKey(tableName)) {
-            tbl = getClient().getTable(tableName);
-            if (enableCaching) {
+            tbl = getClient().getTable(tableName, throwException);
+            if (enableCaching && tbl != null) {
               allHiveTables.put(tableName, tbl);
             }
           } else {
@@ -1389,25 +1436,29 @@ public class CubeMetastoreClient {
         }
       }
     } catch (HiveException e) {
-      throw new HiveException("Could not get table: " + tableName, e);
+      throw new LensException("Could not get table: " + tableName, e);
     }
     return tbl;
   }
 
-  private Table refreshTable(String tableName) throws HiveException {
+  private Table refreshTable(String tableName) throws LensException {
     Table tbl;
     try {
       tableName = tableName.trim().toLowerCase();
       tbl = getClient().getTable(tableName);
       allHiveTables.put(tableName, tbl);
     } catch (HiveException e) {
-      throw new HiveException("Could not get table: " + tableName, e);
+      throw new LensException("Could not get table: " + tableName, e);
     }
     return tbl;
   }
 
-  public void dropHiveTable(String table) throws HiveException {
-    getClient().dropTable(table);
+  public void dropHiveTable(String table) throws LensException {
+    try {
+      getClient().dropTable(table);
+    } catch (HiveException e) {
+      throw new LensException("Couldn't drop hive table: " + table, e);
+    }
     allHiveTables.remove(table.trim().toLowerCase());
   }
 
@@ -1418,7 +1469,7 @@ public class CubeMetastoreClient {
    * @return true if it is cube fact, false otherwise
    * @throws HiveException
    */
-  public boolean isFactTable(String tableName) throws HiveException {
+  public boolean isFactTable(String tableName) throws LensException {
     Table tbl = getTable(tableName);
     return isFactTable(tbl);
   }
@@ -1428,16 +1479,6 @@ public class CubeMetastoreClient {
     return CubeTableType.FACT.name().equals(tableType);
   }
 
-  public boolean isSegmentation(String segName) throws HiveException {
-    Table tbl = getTable(segName);
-    return isSegmentation(tbl);
-  }
-
-  boolean isSegmentation(Table tbl) {
-    String tableType = tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY);
-    return CubeTableType.SEGMENTATION.name().equals(tableType);
-  }
-
 
   boolean isFactTableForCube(Table tbl, String cube) {
     return isFactTable(tbl) && CubeFactTable.getCubeName(tbl.getTableName(), tbl.getParameters())
@@ -1449,14 +1490,14 @@ public class CubeMetastoreClient {
    *
    * @param tableName table name
    * @return true if it is cube dimension, false otherwise
-   * @throws HiveException
+   * @throws LensException
    */
-  public boolean isDimensionTable(String tableName) throws HiveException {
+  public boolean isDimensionTable(String tableName) throws LensException {
     Table tbl = getTable(tableName);
     return isDimensionTable(tbl);
   }
 
-  boolean isDimensionTable(Table tbl) throws HiveException {
+  boolean isDimensionTable(Table tbl) {
     String tableType = tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY);
     return CubeTableType.DIM_TABLE.name().equals(tableType);
   }
@@ -1466,9 +1507,9 @@ public class CubeMetastoreClient {
    *
    * @param tableName table name
    * @return true if it is cube, false otherwise
-   * @throws HiveException
+   * @throws LensException
    */
-  public boolean isCube(String tableName) throws HiveException {
+  public boolean isCube(String tableName) throws LensException {
     if (allCubesPopulated) {
       if (allCubes.containsKey(tableName.trim().toLowerCase())) {
         return true;
@@ -1485,9 +1526,9 @@ public class CubeMetastoreClient {
    *
    * @param tableName table name
    * @return true if it is dimension, false otherwise
-   * @throws HiveException
+   * @throws LensException
    */
-  public boolean isDimension(String tableName) throws HiveException {
+  public boolean isDimension(String tableName) throws LensException {
     if (allDimensionsPopulated) {
       if (allDims.containsKey(tableName.trim().toLowerCase())) {
         return true;
@@ -1504,9 +1545,8 @@ public class CubeMetastoreClient {
    *
    * @param tbl table
    * @return    whether it's a cube table or not
-   * @throws HiveException
    */
-  boolean isCube(Table tbl) throws HiveException {
+  boolean isCube(Table tbl) {
     String tableType = tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY);
     return CubeTableType.CUBE.name().equals(tableType);
   }
@@ -1516,89 +1556,58 @@ public class CubeMetastoreClient {
    *
    * @param tbl  table
    * @return     whether the hive table is a dimension or not
-   * @throws HiveException
    */
-  boolean isDimension(Table tbl) throws HiveException {
+  boolean isDimension(Table tbl)  {
     String tableType = tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY);
     return CubeTableType.DIMENSION.name().equals(tableType);
   }
 
   /**
-   * Is the table name passed a storage?
-   *
-   * @param tableName table name
-   * @return true if it is storage, false otherwise
-   * @throws HiveException
-   */
-  public boolean isStorage(String tableName) throws HiveException {
-    Table tbl = getTable(tableName);
-    return isStorage(tbl);
-  }
-
-  /**
-   * Is the hive table a storage
-   *
-   * @param tbl table
-   * @return    whether the hive table is a storage
-   * @throws HiveException
-   */
-  boolean isStorage(Table tbl) throws HiveException {
-    String tableType = tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY);
-    return CubeTableType.STORAGE.name().equals(tableType);
-  }
-
-  /**
    * Get {@link CubeFactTable} object corresponding to the name
    *
    * @param tableName The cube fact name
    * @return Returns CubeFactTable if table name passed is a fact table, null otherwise
-   * @throws HiveException
+   * @throws LensException
    */
 
-  public CubeFactTable getFactTable(String tableName) throws HiveException {
-    return getFactTable(getTable(tableName));
+  public CubeFactTable getFactTable(String tableName) throws LensException {
+    return new CubeFactTable(getTableWithTypeFailFast(tableName, CubeTableType.FACT));
   }
 
-  private CubeFactTable getFactTable(Table tbl) throws HiveException {
-    return isFactTable(tbl) ? new CubeFactTable(tbl) : null;
-  }
-
-  public Segmentation getSegmentationTable(String tableName) throws HiveException {
-    return getSegmentationTable(getTable(tableName));
-  }
-
-  private Segmentation getSegmentationTable(Table tbl) throws HiveException {
-    return isSegmentation(tbl) ? new Segmentation(tbl) : null;
+  public Segmentation getSegmentationTable(String tableName) throws HiveException, LensException {
+    return new Segmentation(getTableWithTypeFailFast(tableName, CubeTableType.SEGMENTATION));
   }
 
   /**
    * Get {@link CubeDimensionTable} object corresponding to the name
    *
    * @param tableName The cube dimension name
-   * @return Returns CubeDimensionTable if table name passed is a dimension table, null otherwise
-   * @throws HiveException
+   * @return Returns CubeDimensionTable if table name passed is a dimension table
+   * @throws LensException if there is no dimension table with the name
    */
-  public CubeDimensionTable getDimensionTable(String tableName) throws HiveException {
+  public CubeDimensionTable getDimensionTable(String tableName) throws LensException {
+    return getDimensionTable(tableName, true);
+  }
+  private CubeDimensionTable getDimensionTable(String tableName, boolean throwException)
+    throws LensException {
     tableName = tableName.trim().toLowerCase();
     CubeDimensionTable dimTable = allDimTables.get(tableName);
     if (dimTable == null) {
       synchronized (allDimTables) {
         if (!allDimTables.containsKey(tableName)) {
-          Table tbl = getTable(tableName);
-          if (isDimensionTable(tbl)) {
-            dimTable = getDimensionTable(tbl);
-            if (enableCaching) {
-              allDimTables.put(tableName, dimTable);
-              // update latest partition cache for all storages
-              if (!dimTable.getStorages().isEmpty()) {
-                for (String storageName : dimTable.getStorages()) {
-                  if (dimTable.hasStorageSnapshots(storageName)) {
-                    String storageTableName = getFactOrDimtableStorageTableName(dimTable.getName(),
-                      storageName);
-                    if (dimTableLatestPartitionExistsInMetastore(storageTableName,
-                      getDimension(dimTable.getDimName()).getTimedDimension())) {
-                      latestLookupCache.add(storageTableName.trim().toLowerCase());
-                    }
+          Table tbl = getTableWithType(tableName, CubeTableType.DIM_TABLE, throwException);
+          dimTable = tbl == null ? null : getDimensionTable(tbl);
+          if (enableCaching && dimTable != null) {
+            allDimTables.put(tableName, dimTable);
+            // update latest partition cache for all storages
+            if (!dimTable.getStorages().isEmpty()) {
+              for (String storageName : dimTable.getStorages()) {
+                if (dimTable.hasStorageSnapshots(storageName)) {
+                  String storageTableName = getFactOrDimtableStorageTableName(dimTable.getName(),
+                    storageName);
+                  if (dimTableLatestPartitionExistsInMetastore(storageTableName,
+                    getDimension(dimTable.getDimName()).getTimedDimension())) {
+                    latestLookupCache.add(storageTableName.trim().toLowerCase());
                   }
                 }
               }
@@ -1612,7 +1621,7 @@ public class CubeMetastoreClient {
     return dimTable;
   }
 
-  private CubeDimensionTable getDimensionTable(Table tbl) throws HiveException {
+  private CubeDimensionTable getDimensionTable(Table tbl) {
     return new CubeDimensionTable(tbl);
   }
 
@@ -1620,19 +1629,22 @@ public class CubeMetastoreClient {
    * Get {@link Storage} object corresponding to the name
    *
    * @param storageName The storage name
-   * @return Returns storage if name passed is a storage, null otherwise
-   * @throws HiveException
+   * @return Returns storage if name passed is a storage
+   * @throws LensException if there is no storage by the name
    */
-  public Storage getStorage(String storageName) throws HiveException {
+  public Storage getStorage(String storageName) throws LensException {
+    return getStorage(storageName, true);
+  }
+  public Storage getStorage(String storageName, boolean throwException) throws LensException {
     storageName = storageName.trim().toLowerCase();
     Storage storage = allStorages.get(storageName);
     if (storage == null) {
       synchronized (allStorages) {
         if (!allStorages.containsKey(storageName)) {
-          Table tbl = getTable(storageName);
-          if (isStorage(tbl)) {
+          Table tbl = getTableWithType(storageName, CubeTableType.STORAGE, throwException);
+          if (tbl != null) {
             storage = getStorage(tbl);
-            if (enableCaching) {
+            if (enableCaching && storage != null) {
               allStorages.put(storageName, storage);
             }
           }
@@ -1644,7 +1656,7 @@ public class CubeMetastoreClient {
     return storage;
   }
 
-  private Storage getStorage(Table tbl) throws HiveException {
+  private Storage getStorage(Table tbl) throws LensException {
     return Storage.createInstance(tbl);
   }
 
@@ -1652,10 +1664,14 @@ public class CubeMetastoreClient {
    * Get {@link Cube} object corresponding to the name
    *
    * @param tableName The cube name
-   * @return Returns cube is table name passed is a cube, null otherwise
-   * @throws HiveException
+   * @return Returns cube is table name passed is a cube
+   * @throws LensException when the table name does not correspond to a cube
    */
-  public CubeInterface getCube(String tableName) throws HiveException {
+  public CubeInterface getCube(String tableName) throws LensException {
+    return getCube(tableName, true);
+  }
+
+  private CubeInterface getCube(String tableName, boolean throwException) throws LensException {
     if (tableName == null) {
       return null;
     }
@@ -1664,12 +1680,10 @@ public class CubeMetastoreClient {
     if (cube == null) {
       synchronized (allCubes) {
         if (!allCubes.containsKey(tableName)) {
-          Table tbl = getTable(tableName);
-          if (isCube(tbl)) {
-            cube = getCube(tbl);
-            if (enableCaching) {
-              allCubes.put(tableName, cube);
-            }
+          Table tbl = getTableWithType(tableName, CubeTableType.CUBE, throwException);
+          cube = tbl == null ? null : getCube(tbl);
+          if (enableCaching && cube != null) {
+            allCubes.put(tableName, cube);
           }
         } else {
           cube = allCubes.get(tableName);
@@ -1683,10 +1697,13 @@ public class CubeMetastoreClient {
    * Get {@link Cube} object corresponding to the name
    *
    * @param tableName The cube name
-   * @return Returns cube is table name passed is a cube, null otherwise
-   * @throws HiveException
+   * @return Returns Dimension if table name passed is a Dimension
+   * @throws LensException if the table name passed is not a Dimension
    */
-  public Dimension getDimension(String tableName) throws HiveException {
+  public Dimension getDimension(String tableName) throws LensException {
+    return getDimension(tableName, true);
+  }
+  private Dimension getDimension(String tableName, boolean throwException) throws LensException {
     if (tableName == null) {
       return null;
     }
@@ -1695,12 +1712,10 @@ public class CubeMetastoreClient {
     if (dim == null) {
       synchronized (allDims) {
         if (!allDims.containsKey(tableName)) {
-          Table tbl = getTable(tableName);
-          if (isDimension(tbl)) {
-            dim = getDimension(tbl);
-            if (enableCaching) {
-              allDims.put(tableName, dim);
-            }
+          Table tbl = getTableWithType(tableName, CubeTableType.DIMENSION, throwException);
+          dim = tbl == null ? null : getDimension(tbl);
+          if (enableCaching && dim != null) {
+            allDims.put(tableName, dim);
           }
         } else {
           dim = allDims.get(tableName);
@@ -1714,16 +1729,20 @@ public class CubeMetastoreClient {
    * Get {@link Cube} object corresponding to the name
    *
    * @param tableName The cube name
-   * @return Returns cube is table name passed is a cube, null otherwise
-   * @throws HiveException
+   * @return Returns cube is table name passed is a cube
+   * @throws LensException if there is no cube by the name
    */
-  public CubeFactTable getCubeFact(String tableName) throws HiveException {
+  public CubeFactTable getCubeFact(String tableName) throws LensException {
+    return getCubeFact(tableName, true);
+  }
+  private CubeFactTable getCubeFact(String tableName, boolean throwException) throws LensException {
     tableName = tableName.trim().toLowerCase();
     CubeFactTable fact = allFactTables.get(tableName);
     if (fact == null) {
       synchronized (allFactTables) {
         if (!allFactTables.containsKey(tableName)) {
-          fact = getFactTable(tableName);
+          Table tbl = getTableWithType(tableName, CubeTableType.FACT, throwException);
+          fact = tbl == null ? null : new CubeFactTable(tbl);
           if (enableCaching && fact != null) {
             allFactTables.put(tableName, fact);
           }
@@ -1735,13 +1754,17 @@ public class CubeMetastoreClient {
     return fact;
   }
 
-  public Segmentation getSegmentation(String segName) throws HiveException {
+  public Segmentation getSegmentation(String segName) throws LensException {
+    return getSegmentation(segName, true);
+  }
+  public Segmentation getSegmentation(String segName, boolean throwException) throws LensException {
     segName = segName.trim().toLowerCase();
     Segmentation seg = allSegmentations.get(segName);
     if (seg == null) {
       synchronized (allSegmentations) {
         if (!allSegmentations.containsKey(segName)) {
-          seg = getSegmentationTable(segName);
+          Table tbl = getTableWithType(segName, CubeTableType.SEGMENTATION, throwException);
+          seg = tbl == null ? null : new Segmentation(tbl);
           if (enableCaching && seg != null) {
             allSegmentations.put(segName, seg);
           }
@@ -1754,7 +1777,7 @@ public class CubeMetastoreClient {
   }
 
 
-  private CubeInterface getCube(Table tbl) throws HiveException {
+  private CubeInterface getCube(Table tbl) throws LensException {
     String parentCube = tbl.getParameters().get(getParentCubeNameKey(tbl.getTableName()));
     if (parentCube != null) {
       return new DerivedCube(tbl, (Cube) getCube(parentCube));
@@ -1771,20 +1794,20 @@ public class CubeMetastoreClient {
    * Get all dimension tables in metastore
    *
    * @return List of dimension tables
-   * @throws HiveException
+   * @throws LensException
    */
-  public Collection<CubeDimensionTable> getAllDimensionTables() throws HiveException {
+  public Collection<CubeDimensionTable> getAllDimensionTables() throws LensException {
     if (!allDimTablesPopulated) {
       List<CubeDimensionTable> dimTables = new ArrayList<>();
       try {
         for (String table : getAllHiveTableNames()) {
-          CubeDimensionTable dim = getDimensionTable(table);
+          CubeDimensionTable dim = getDimensionTable(table, false);
           if (dim != null) {
             dimTables.add(dim);
           }
         }
       } catch (HiveException e) {
-        throw new HiveException("Could not get all dimension tables", e);
+        throw new LensException("Could not get all dimension tables", e);
       }
       allDimTablesPopulated = enableCaching;
       return dimTables;
@@ -1797,20 +1820,20 @@ public class CubeMetastoreClient {
    * Get all storages in metastore
    *
    * @return List of Storage objects
-   * @throws HiveException
+   * @throws LensException
    */
-  public Collection<Storage> getAllStorages() throws HiveException {
+  public Collection<Storage> getAllStorages() throws LensException {
     if (!allStoragesPopulated) {
       List<Storage> storages = new ArrayList<>();
       try {
         for (String table : getAllHiveTableNames()) {
-          Storage storage = getStorage(table);
+          Storage storage = getStorage(table, false);
           if (storage != null) {
             storages.add(storage);
           }
         }
       } catch (HiveException e) {
-        throw new HiveException("Could not get all storages", e);
+        throw new LensException("Could not get all storages", e);
       }
       allStoragesPopulated = enableCaching;
       return storages;
@@ -1823,20 +1846,20 @@ public class CubeMetastoreClient {
    * Get all cubes in metastore
    *
    * @return List of Cube objects
-   * @throws HiveException
+   * @throws LensException
    */
-  public Collection<CubeInterface> getAllCubes() throws HiveException {
+  public Collection<CubeInterface> getAllCubes() throws LensException {
     if (!allCubesPopulated) {
       List<CubeInterface> cubes = new ArrayList<>();
       try {
         for (String table : getAllHiveTableNames()) {
-          CubeInterface cube = getCube(table);
+          CubeInterface cube = getCube(table, false);
           if (cube != null) {
             cubes.add(cube);
           }
         }
       } catch (HiveException e) {
-        throw new HiveException("Could not get all cubes", e);
+        throw new LensException("Could not get all cubes", e);
       }
       allCubesPopulated = enableCaching;
       return cubes;
@@ -1849,20 +1872,20 @@ public class CubeMetastoreClient {
    * Get all cubes in metastore
    *
    * @return List of Cube objects
-   * @throws HiveException
+   * @throws LensException
    */
-  public Collection<Dimension> getAllDimensions() throws HiveException {
+  public Collection<Dimension> getAllDimensions() throws LensException {
     if (!allDimensionsPopulated) {
       List<Dimension> dims = new ArrayList<>();
       try {
         for (String table : getAllHiveTableNames()) {
-          Dimension dim = getDimension(table);
+          Dimension dim =  getDimension(table, false);
           if (dim != null) {
             dims.add(dim);
           }
         }
       } catch (HiveException e) {
-        throw new HiveException("Could not get all dimensions", e);
+        throw new LensException("Could not get all dimensions", e);
       }
       allDimensionsPopulated = enableCaching;
       return dims;
@@ -1875,20 +1898,20 @@ public class CubeMetastoreClient {
    * Get all facts in metastore
    *
    * @return List of Cube Fact Table objects
-   * @throws HiveException
+   * @throws LensException
    */
-  public Collection<CubeFactTable> getAllFacts() throws HiveException {
+  public Collection<CubeFactTable> getAllFacts() throws LensException {
     if (!allFactTablesPopulated) {
       List<CubeFactTable> facts = new ArrayList<>();
       try {
         for (String table : getAllHiveTableNames()) {
-          CubeFactTable fact = getCubeFact(table);
+          CubeFactTable fact = getCubeFact(table, false);
           if (fact != null) {
             facts.add(fact);
           }
         }
       } catch (HiveException e) {
-        throw new HiveException("Could not get all fact tables", e);
+        throw new LensException("Could not get all fact tables", e);
       }
       allFactTablesPopulated = enableCaching;
       return facts;
@@ -1901,20 +1924,20 @@ public class CubeMetastoreClient {
    * Get all segmentations in metastore
    *
    * @return List of segmentation objects
-   * @throws HiveException
+   * @throws LensException
    */
-  public Collection<Segmentation> getAllSegmentations() throws HiveException {
+  public Collection<Segmentation> getAllSegmentations() throws LensException {
     if (!allSegmentationPopulated) {
       List<Segmentation> segs = new ArrayList<>();
       try {
         for (String table : getAllHiveTableNames()) {
-          Segmentation seg = getSegmentation(table);
+          Segmentation seg = getSegmentation(table, false);
           if (seg != null) {
             segs.add(seg);
           }
         }
       } catch (HiveException e) {
-        throw new HiveException("Could not get all fact tables", e);
+        throw new LensException("Could not get all fact tables", e);
       }
       allFactTablesPopulated = enableCaching;
       return segs;
@@ -1925,7 +1948,7 @@ public class CubeMetastoreClient {
 
 
 
-  private Collection<String> getAllHiveTableNames() throws HiveException {
+  private Collection<String> getAllHiveTableNames() throws HiveException, LensException {
     if (!allTablesPopulated) {
       List<String> allTables = getClient().getAllTables();
       for (String tblName : allTables) {
@@ -1944,9 +1967,9 @@ public class CubeMetastoreClient {
    *
    * @param cube Cube object
    * @return List of fact tables
-   * @throws HiveException
+   * @throws LensException
    */
-  public List<CubeFactTable> getAllFacts(CubeInterface cube) throws HiveException {
+  public List<CubeFactTable> getAllFacts(CubeInterface cube) throws LensException {
     String cubeName = null;
     if (cube != null) {
       if (cube instanceof DerivedCube) {
@@ -1955,19 +1978,15 @@ public class CubeMetastoreClient {
       cubeName = cube.getName();
     }
     List<CubeFactTable> cubeFacts = new ArrayList<>();
-    try {
-      for (CubeFactTable fact : getAllFacts()) {
-        if (cubeName == null || fact.getCubeName().equalsIgnoreCase(cubeName)) {
-          cubeFacts.add(fact);
-        }
+    for (CubeFactTable fact : getAllFacts()) {
+      if (cubeName == null || fact.getCubeName().equalsIgnoreCase(cubeName)) {
+        cubeFacts.add(fact);
       }
-    } catch (HiveException e) {
-      throw new HiveException("Could not get all fact tables of " + cube, e);
     }
     return cubeFacts;
   }
 
-  public List<Segmentation> getAllSegmentations(CubeInterface cube) throws HiveException {
+  public List<Segmentation> getAllSegmentations(CubeInterface cube) throws LensException {
     String cubeName = null;
     if (cube != null) {
       if (cube instanceof DerivedCube) {
@@ -1976,14 +1995,10 @@ public class CubeMetastoreClient {
       cubeName = cube.getName();
     }
     List<Segmentation> cubeSegs = new ArrayList<>();
-    try {
-      for (Segmentation seg : getAllSegmentations()) {
-        if (cubeName == null || seg.getBaseCube().equalsIgnoreCase(cubeName)) {
-          cubeSegs.add(seg);
-        }
+    for (Segmentation seg : getAllSegmentations()) {
+      if (cubeName == null || seg.getBaseCube().equalsIgnoreCase(cubeName)) {
+        cubeSegs.add(seg);
       }
-    } catch (HiveException e) {
-      throw new HiveException("Could not get all segmentations of " + cube, e);
     }
     return cubeSegs;
   }
@@ -1994,19 +2009,15 @@ public class CubeMetastoreClient {
    *
    * @param cube Cube object
    * @return List of DerivedCube objects
-   * @throws HiveException
+   * @throws LensException
    */
-  public List<DerivedCube> getAllDerivedQueryableCubes(CubeInterface cube) throws HiveException {
+  public List<DerivedCube> getAllDerivedQueryableCubes(CubeInterface cube) throws LensException {
     List<DerivedCube> dcubes = new ArrayList<>();
-    try {
-      for (CubeInterface cb : getAllCubes()) {
-        if (cb.isDerivedCube() && ((DerivedCube) cb).getParent().getName().equalsIgnoreCase(cube.getName())
-          && cb.allFieldsQueriable()) {
-          dcubes.add((DerivedCube) cb);
-        }
+    for (CubeInterface cb : getAllCubes()) {
+      if (cb.isDerivedCube() && ((DerivedCube) cb).getParent().getName().equalsIgnoreCase(cube.getName())
+        && cb.allFieldsQueriable()) {
+        dcubes.add((DerivedCube) cb);
       }
-    } catch (HiveException e) {
-      throw new HiveException("Could not get all derived queryable cubes of " + cube, e);
     }
     return dcubes;
   }
@@ -2016,23 +2027,19 @@ public class CubeMetastoreClient {
    *
    * @param dim Dimension object
    * @return List of fact tables
-   * @throws HiveException
+   * @throws LensException
    */
-  public List<CubeDimensionTable> getAllDimensionTables(Dimension dim) throws HiveException {
+  public List<CubeDimensionTable> getAllDimensionTables(Dimension dim) throws LensException {
     List<CubeDimensionTable> dimTables = new ArrayList<>();
-    try {
-      for (CubeDimensionTable dimTbl : getAllDimensionTables()) {
-        if (dim == null || dimTbl.getDimName().equalsIgnoreCase(dim.getName().toLowerCase())) {
-          dimTables.add(dimTbl);
-        }
+    for (CubeDimensionTable dimTbl : getAllDimensionTables()) {
+      if (dim == null || dimTbl.getDimName().equalsIgnoreCase(dim.getName().toLowerCase())) {
+        dimTables.add(dimTbl);
       }
-    } catch (HiveException e) {
-      throw new HiveException("Could not get all dimension tables of " + dim, e);
     }
     return dimTables;
   }
 
-  public boolean partColExists(String tableName, String partCol) throws HiveException {
+  public boolean partColExists(String tableName, String partCol) throws LensException {
     Table tbl = getTable(tableName);
     for (FieldSchema f : tbl.getPartCols()) {
       if (f.getName().equalsIgnoreCase(partCol)) {
@@ -2048,9 +2055,9 @@ public class CubeMetastoreClient {
    * @param hiveTable hive table
    * @param cubeTable lens cube table
    * @return true if columns changed in alter
-   * @throws HiveException
+   * @throws LensException
    */
-  private boolean alterCubeTable(String table, Table hiveTable, AbstractCubeTable cubeTable) throws HiveException {
+  private boolean alterCubeTable(String table, Table hiveTable, AbstractCubeTable cubeTable) throws LensException {
     hiveTable.getParameters().putAll(cubeTable.getProperties());
     boolean columnsChanged = !(hiveTable.getCols().equals(cubeTable.getColumns()));
     if (columnsChanged) {
@@ -2059,17 +2066,17 @@ public class CubeMetastoreClient {
     hiveTable.getTTable().getParameters().putAll(cubeTable.getProperties());
     try {
       getClient().alterTable(table, hiveTable, null);
-    } catch (InvalidOperationException e) {
-      throw new HiveException(e);
+    } catch (Exception e) {
+      throw new LensException(e);
     }
     return columnsChanged;
   }
 
-  public void pushHiveTable(Table hiveTable) throws HiveException {
+  public void pushHiveTable(Table hiveTable) throws HiveException, LensException {
     alterHiveTable(hiveTable.getTableName(), hiveTable);
   }
 
-  public void alterHiveTable(String table, Table hiveTable) throws HiveException {
+  public void alterHiveTable(String table, Table hiveTable) throws HiveException, LensException {
     try {
       getClient().alterTable(table, hiveTable, null);
     } catch (InvalidOperationException e) {
@@ -2088,15 +2095,11 @@ public class CubeMetastoreClient {
    * @param cube     The new cube definition {@link Cube} or {@link DerivedCube}
    * @throws HiveException
    */
-  public void alterCube(String cubeName, CubeInterface cube) throws HiveException {
-    Table cubeTbl = getTable(cubeName);
-    if (isCube(cubeTbl)) {
-      alterCubeTable(cubeName, cubeTbl, (AbstractCubeTable) cube);
-      if (enableCaching) {
-        allCubes.put(cubeName.trim().toLowerCase(), getCube(refreshTable(cubeName)));
-      }
-    } else {
-      throw new HiveException(cubeName + " is not a cube");
+  public void alterCube(String cubeName, CubeInterface cube) throws HiveException, LensException {
+    Table cubeTbl = getTableWithTypeFailFast(cubeName, CubeTableType.CUBE);
+    alterCubeTable(cubeName, cubeTbl, (AbstractCubeTable) cube);
+    if (enableCaching) {
+      allCubes.put(cubeName.trim().toLowerCase(), getCube(refreshTable(cubeName)));
     }
   }
 
@@ -2107,15 +2110,11 @@ public class CubeMetastoreClient {
    * @param newDim  The new dimension definition
    * @throws HiveException
    */
-  public void alterDimension(String dimName, Dimension newDim) throws HiveException {
-    Table tbl = getTable(dimName);
-    if (isDimension(tbl)) {
-      alterCubeTable(dimName, tbl, newDim);
-      if (enableCaching) {
-        allDims.put(dimName.trim().toLowerCase(), getDimension(refreshTable(dimName)));
-      }
-    } else {
-      throw new HiveException(dimName + " is not a dimension");
+  public void alterDimension(String dimName, Dimension newDim) throws HiveException, LensException {
+    Table tbl = getTableWithTypeFailFast(dimName, CubeTableType.DIMENSION);
+    alterCubeTable(dimName, tbl, newDim);
+    if (enableCaching) {
+      allDims.put(dimName.trim().toLowerCase(), getDimension(refreshTable(dimName)));
     }
   }
 
@@ -2124,17 +2123,13 @@ public class CubeMetastoreClient {
    *
    * @param storageName The storage name to be altered
    * @param storage     The new storage definition
-   * @throws HiveException
+   * @throws LensException
    */
-  public void alterStorage(String storageName, Storage storage) throws HiveException {
-    Table storageTbl = getTable(storageName);
-    if (isStorage(storageTbl)) {
-      alterCubeTable(storageName, storageTbl, storage);
-      if (enableCaching) {
-        allStorages.put(storageName.trim().toLowerCase(), getStorage(refreshTable(storageName)));
-      }
-    } else {
-      throw new HiveException(storageName + " is not a storage");
+  public void alterStorage(String storageName, Storage storage) throws LensException, HiveException {
+    Table storageTbl = getTableWithTypeFailFast(storageName, CubeTableType.STORAGE);
+    alterCubeTable(storageName, storageTbl, storage);
+    if (enableCaching) {
+      allStorages.put(storageName.trim().toLowerCase(), getStorage(refreshTable(storageName)));
     }
   }
 
@@ -2142,47 +2137,36 @@ public class CubeMetastoreClient {
    * Drop a storage
    *
    * @param storageName  storage name
-   * @throws HiveException
+   * @throws LensException
    */
-  public void dropStorage(String storageName) throws HiveException {
-    if (isStorage(storageName)) {
-      allStorages.remove(storageName.trim().toLowerCase());
-      dropHiveTable(storageName);
-    } else {
-      throw new HiveException(storageName + " is not a storage");
-    }
+  public void dropStorage(String storageName) throws LensException {
+    getTableWithTypeFailFast(storageName, CubeTableType.STORAGE);
+    allStorages.remove(storageName.trim().toLowerCase());
+    dropHiveTable(storageName);
   }
 
   /**
    * Drop a cube
    *
    * @param cubeName cube name
-   * @throws HiveException
+   * @throws LensException
    */
-  public void dropCube(String cubeName) throws HiveException {
-    Table tbl = getTable(cubeName);
-    if (isCube(tbl)) {
-      allCubes.remove(cubeName.trim().toLowerCase());
-      dropHiveTable(cubeName);
-    } else {
-      throw new HiveException(cubeName + " is not a cube");
-    }
+  public void dropCube(String cubeName) throws LensException {
+    getTableWithTypeFailFast(cubeName, CubeTableType.CUBE);
+    allCubes.remove(cubeName.trim().toLowerCase());
+    dropHiveTable(cubeName);
   }
 
   /**
    * Drop a dimension
    *
    * @param dimName dimension name to be dropped
-   * @throws HiveException
+   * @throws LensException
    */
-  public void dropDimension(String dimName) throws HiveException {
-    Table tbl = getTable(dimName);
-    if (isDimension(tbl)) {
-      allDims.remove(dimName.trim().toLowerCase());
-      dropHiveTable(dimName);
-    } else {
-      throw new HiveException(dimName + " is not a dimension");
-    }
+  public void dropDimension(String dimName) throws LensException {
+    getTableWithTypeFailFast(dimName, CubeTableType.DIMENSION);
+    allDims.remove(dimName.trim().toLowerCase());
+    dropHiveTable(dimName);
   }
 
   /**
@@ -2190,31 +2174,25 @@ public class CubeMetastoreClient {
    *
    * @param factName fact name
    * @param cascade  If true, will drop all the storages of the fact
-   * @throws HiveException
+   * @throws LensException
    */
-  public void dropFact(String factName, boolean cascade) throws HiveException {
-    if (isFactTable(factName)) {
-      CubeFactTable fact = getFactTable(factName);
-      if (cascade) {
-        for (String storage : fact.getStorages()) {
-          dropStorageFromFact(factName, storage, false);
-        }
+  public void dropFact(String factName, boolean cascade) throws LensException {
+    getTableWithTypeFailFast(factName, CubeTableType.FACT);
+    CubeFactTable fact = getFactTable(factName);
+    if (cascade) {
+      for (String storage : fact.getStorages()) {
+        dropStorageFromFact(factName, storage, false);
       }
-      dropHiveTable(factName);
-      allFactTables.remove(factName.trim().toLowerCase());
-    } else {
-      throw new HiveException(factName + " is not a CubeFactTable");
     }
+    dropHiveTable(factName);
+    allFactTables.remove(factName.trim().toLowerCase());
   }
 
 
-  public void dropSegmentation(String segName) throws HiveException {
-    if (isSegmentation(segName)) {
-      dropHiveTable(segName);
-      allSegmentations.remove(segName.trim().toLowerCase());
-    } else {
-      throw new HiveException(segName + " is not a Segmentation");
-    }
+  public void dropSegmentation(String segName) throws LensException {
+    getTableWithTypeFailFast(segName, CubeTableType.SEGMENTATION);
+    dropHiveTable(segName);
+    allSegmentations.remove(segName.trim().toLowerCase());
   }
 
   /**
@@ -2222,23 +2200,24 @@ public class CubeMetastoreClient {
    *
    * @param factName fact name
    * @param storage  storage name
-   * @throws HiveException
+   * @throws LensException
    */
-  public void dropStorageFromFact(String factName, String storage) throws HiveException {
+  public void dropStorageFromFact(String factName, String storage) throws LensException {
     CubeFactTable cft = getFactTable(factName);
     cft.dropStorage(storage);
     dropHiveTable(getFactOrDimtableStorageTableName(factName, storage));
-    alterCubeTable(factName, getTable(factName), cft);
+    alterCubeTable(factName, getTableWithTypeFailFast(factName, CubeTableType.FACT), cft);
     updateFactCache(factName);
   }
 
   // updateFact will be false when fact is fully dropped
-  private void dropStorageFromFact(String factName, String storage, boolean updateFact) throws HiveException {
+  private void dropStorageFromFact(String factName, String storage, boolean updateFact)
+    throws LensException {
     CubeFactTable cft = getFactTable(factName);
     dropHiveTable(getFactOrDimtableStorageTableName(factName, storage));
     if (updateFact) {
       cft.dropStorage(storage);
-      alterCubeTable(factName, getTable(factName), cft);
+      alterCubeTable(factName, getTableWithTypeFailFast(factName, CubeTableType.FACT), cft);
       updateFactCache(factName);
     }
   }
@@ -2250,19 +2229,20 @@ public class CubeMetastoreClient {
    * @param storage    storage
    * @throws HiveException
    */
-  public void dropStorageFromDim(String dimTblName, String storage) throws HiveException {
+  public void dropStorageFromDim(String dimTblName, String storage) throws HiveException, LensException {
     dropStorageFromDim(dimTblName, storage, true);
   }
 
   // updateDimTbl will be false when dropping dimTbl
-  private void dropStorageFromDim(String dimTblName, String storage, boolean updateDimTbl) throws HiveException {
+  private void dropStorageFromDim(String dimTblName, String storage, boolean updateDimTbl)
+    throws LensException {
     CubeDimensionTable cdt = getDimensionTable(dimTblName);
     String storageTableName = getFactOrDimtableStorageTableName(dimTblName, storage);
     dropHiveTable(storageTableName);
     latestLookupCache.remove(storageTableName.trim().toLowerCase());
     if (updateDimTbl) {
       cdt.dropStorage(storage);
-      alterCubeTable(dimTblName, getTable(dimTblName), cdt);
+      alterCubeTable(dimTblName, getTableWithTypeFailFast(dimTblName, CubeTableType.DIM_TABLE), cdt);
       updateDimCache(dimTblName);
     }
   }
@@ -2274,19 +2254,16 @@ public class CubeMetastoreClient {
    * @param cascade    If true, will drop all the dimension storages
    * @throws HiveException
    */
-  public void dropDimensionTable(String dimTblName, boolean cascade) throws HiveException {
-    if (isDimensionTable(dimTblName)) {
-      CubeDimensionTable dim = getDimensionTable(dimTblName);
-      if (cascade) {
-        for (String storage : dim.getStorages()) {
-          dropStorageFromDim(dimTblName, storage, false);
-        }
+  public void dropDimensionTable(String dimTblName, boolean cascade) throws LensException {
+    getTableWithTypeFailFast(dimTblName, CubeTableType.DIM_TABLE);
+    CubeDimensionTable dim = getDimensionTable(dimTblName);
+    if (cascade) {
+      for (String storage : dim.getStorages()) {
+        dropStorageFromDim(dimTblName, storage, false);
       }
-      dropHiveTable(dimTblName);
-      allDimTables.remove(dimTblName.trim().toLowerCase());
-    } else {
-      throw new HiveException(dimTblName + " is not a dimension table");
     }
+    dropHiveTable(dimTblName);
+    allDimTables.remove(dimTblName.trim().toLowerCase());
   }
 
   /**
@@ -2299,49 +2276,41 @@ public class CubeMetastoreClient {
    * @throws HiveException
    */
   public void alterCubeFactTable(String factTableName, CubeFactTable cubeFactTable,
-    Map<String, StorageTableDesc> storageTableDescs) throws HiveException {
-    Table factTbl = getTable(factTableName);
-    if (isFactTable(factTbl)) {
-      alterCubeTable(factTableName, factTbl, cubeFactTable);
-      if (storageTableDescs != null) {
-        // create/alter tables for each storage
-        for (Map.Entry<String, StorageTableDesc> entry : storageTableDescs.entrySet()) {
-          createOrAlterStorageHiveTable(getTable(factTableName), entry.getKey(), entry.getValue());
-        }
+    Map<String, StorageTableDesc> storageTableDescs) throws HiveException, LensException {
+    Table factTbl = getTableWithTypeFailFast(factTableName, CubeTableType.FACT);
+    alterCubeTable(factTableName, factTbl, cubeFactTable);
+    if (storageTableDescs != null) {
+      // create/alter tables for each storage
+      for (Map.Entry<String, StorageTableDesc> entry : storageTableDescs.entrySet()) {
+        createOrAlterStorageHiveTable(getTable(factTableName), entry.getKey(), entry.getValue());
       }
-      updateFactCache(factTableName);
-    } else {
-      throw new HiveException(factTableName + " is not a fact table");
     }
+    updateFactCache(factTableName);
   }
 
   public void alterSegmentation(String segName, Segmentation seg)
-    throws HiveException {
-    Table segTbl = getTable(segName);
-    if (isSegmentation(segTbl)) {
-      if (!(getSegmentation(segName) == seg)) {
-        dropSegmentation(segName);
-        createSegmentation(seg);
-        updateSegmentationCache(segName);
-      }
-    } else {
-      throw new HiveException(segName + " is not a segment");
+    throws HiveException, LensException {
+    getTableWithTypeFailFast(segName, CubeTableType.SEGMENTATION);
+    if (!(getSegmentation(segName) == seg)) {
+      dropSegmentation(segName);
+      createSegmentation(seg);
+      updateSegmentationCache(segName);
     }
   }
 
-  private void updateSegmentationCache(String segmentName) throws HiveException {
+  private void updateSegmentationCache(String segmentName) throws HiveException, LensException {
     if (enableCaching) {
-      allSegmentations.put(segmentName.trim().toLowerCase(), getSegmentationTable(refreshTable(segmentName)));
+      allSegmentations.put(segmentName.trim().toLowerCase(), new Segmentation(refreshTable(segmentName)));
     }
   }
 
-  private void updateFactCache(String factTableName) throws HiveException {
+  private void updateFactCache(String factTableName) throws LensException {
     if (enableCaching) {
-      allFactTables.put(factTableName.trim().toLowerCase(), getFactTable(refreshTable(factTableName)));
+      allFactTables.put(factTableName.trim().toLowerCase(), new CubeFactTable(refreshTable(factTableName)));
     }
   }
 
-  private void updateDimCache(String dimTblName) throws HiveException {
+  private void updateDimCache(String dimTblName) throws LensException {
     if (enableCaching) {
       allDimTables.put(dimTblName.trim().toLowerCase(), getDimensionTable(refreshTable(dimTblName)));
     }
@@ -2355,26 +2324,23 @@ public class CubeMetastoreClient {
    * @throws HiveException
    */
   public void alterCubeDimensionTable(String dimTableName, CubeDimensionTable cubeDimensionTable,
-    Map<String, StorageTableDesc> storageTableDescs) throws HiveException {
-    Table dimTbl = getTable(dimTableName);
-    if (isDimensionTable(dimTbl)) {
-      alterCubeTable(dimTableName, dimTbl, cubeDimensionTable);
-      if (storageTableDescs != null) {
-        // create/alter tables for each storage
-        for (Map.Entry<String, StorageTableDesc> entry : storageTableDescs.entrySet()) {
-          createOrAlterStorageHiveTable(getTable(dimTableName), entry.getKey(), entry.getValue());
-        }
+    Map<String, StorageTableDesc> storageTableDescs) throws HiveException, LensException {
+    Table dimTbl = getTableWithTypeFailFast(dimTableName, CubeTableType.DIM_TABLE);
+    alterCubeTable(dimTableName, dimTbl, cubeDimensionTable);
+    if (storageTableDescs != null) {
+      // create/alter tables for each storage
+      for (Map.Entry<String, StorageTableDesc> entry : storageTableDescs.entrySet()) {
+        createOrAlterStorageHiveTable(getTable(dimTableName), entry.getKey(), entry.getValue());
       }
-      updateDimCache(dimTableName);
-    } else {
-      throw new HiveException(dimTableName + " is not a dimension table");
     }
+    updateDimCache(dimTableName);
   }
 
-  private List<Date> getStorageTimes(String storageTableName, String timeKey) throws HiveException, LensException {
+  private List<Date> getStorageTimes(String storageTableName, String timeKey) throws LensException {
     Date now = new Date();
-    List<Date> storageTimes = new ArrayList<Date>();
-    String property =  getTable(storageTableName).getProperty(timeKey);
+    List<Date> storageTimes = new ArrayList<>();
+    String property;
+    property = getTable(storageTableName).getProperty(timeKey);
     if (StringUtils.isNotBlank(property)) {
       for (String timeStr : property.split("\\s*,\\s*")) {
         storageTimes.add(resolveDate(timeStr, now));
@@ -2387,8 +2353,8 @@ public class CubeMetastoreClient {
    * Storage table is not a candidate for range (t0, tn) :
    * if start_time is after tn; or end date is before t0.
    */
-  public boolean isStorageTableCandidateForRange(String storageTableName, Date fromDate, Date toDate) throws
-    HiveException, LensException {
+  public boolean isStorageTableCandidateForRange(String storageTableName, Date fromDate, Date toDate)
+    throws LensException {
     List<Date> storageEndDates = getStorageTimes(storageTableName, MetastoreUtil.getStoragetableEndTimesKey());
     for(Date endDate : storageEndDates) {
       // endDate is exclusive
@@ -2412,8 +2378,8 @@ public class CubeMetastoreClient {
   }
 
   // Check if partition is valid wrt start and end dates
-  public boolean isStorageTablePartitionACandidate(String storageTableName, Date partDate) throws
-    HiveException, LensException {
+  public boolean isStorageTablePartitionACandidate(String storageTableName, Date partDate)
+    throws LensException {
     List<Date> storageStartDates = getStorageTimes(storageTableName, MetastoreUtil.getStoragetableStartTimesKey());
     for(Date startDate : storageStartDates) {
       if (partDate.before(startDate)) {

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/metadata/JoinChain.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/metadata/JoinChain.java b/lens-cube/src/main/java/org/apache/lens/cube/metadata/JoinChain.java
index cc8929f..b9f9157 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/metadata/JoinChain.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/metadata/JoinChain.java
@@ -22,6 +22,7 @@ import java.util.*;
 
 import org.apache.lens.cube.metadata.join.JoinPath;
 import org.apache.lens.cube.metadata.join.TableRelationship;
+import org.apache.lens.server.api.error.LensException;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
@@ -173,7 +174,7 @@ public class JoinChain implements Named {
       this.to = to;
     }
 
-    TableRelationship toDimToDimRelationship(CubeMetastoreClient client) throws HiveException {
+    TableRelationship toDimToDimRelationship(CubeMetastoreClient client) throws HiveException, LensException {
       if (relationShip == null) {
         relationShip = new TableRelationship(from.getDestColumn(),
           client.getDimension(from.getDestTable()),
@@ -191,7 +192,7 @@ public class JoinChain implements Named {
      * @return
      * @throws HiveException
      */
-    TableRelationship toCubeOrDimRelationship(CubeMetastoreClient client) throws HiveException {
+    TableRelationship toCubeOrDimRelationship(CubeMetastoreClient client) throws HiveException, LensException {
       if (relationShip == null) {
         AbstractCubeTable fromTable = null;
         if (client.isCube(from.getDestTable())) {
@@ -319,7 +320,7 @@ public class JoinChain implements Named {
    * @return List&lt;JoinPath&gt;
    * @throws HiveException
    */
-  public List<JoinPath> getRelationEdges(CubeMetastoreClient client) throws HiveException {
+  public List<JoinPath> getRelationEdges(CubeMetastoreClient client) throws HiveException, LensException {
     List<JoinPath> joinPaths = new ArrayList<>();
     for (Path path : paths) {
       JoinPath jp = new JoinPath();

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/metadata/Storage.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/metadata/Storage.java b/lens-cube/src/main/java/org/apache/lens/cube/metadata/Storage.java
index 012081a..cd9f705 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/metadata/Storage.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/metadata/Storage.java
@@ -23,6 +23,8 @@ import java.lang.reflect.Constructor;
 import java.util.*;
 import java.util.Map.Entry;
 
+import org.apache.lens.server.api.error.LensException;
+
 import org.apache.commons.lang.NotImplementedException;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.hadoop.fs.Path;
@@ -462,7 +464,7 @@ public abstract class Storage extends AbstractCubeTable implements PartitionMeta
     }
   }
 
-  static Storage createInstance(Table tbl) throws HiveException {
+  static Storage createInstance(Table tbl) throws LensException {
     String storageName = tbl.getTableName();
     String storageClassName = tbl.getParameters().get(MetastoreUtil.getStorageClassKey(storageName));
     try {
@@ -470,7 +472,7 @@ public abstract class Storage extends AbstractCubeTable implements PartitionMeta
       Constructor<?> constructor = clazz.getConstructor(Table.class);
       return (Storage) constructor.newInstance(tbl);
     } catch (Exception e) {
-      throw new HiveException("Could not create storage class" + storageClassName, e);
+      throw new LensException("Could not create storage class" + storageClassName, e);
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateFact.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateFact.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateFact.java
index e19e03a..187d98d 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateFact.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateFact.java
@@ -28,7 +28,6 @@ import org.apache.lens.server.api.error.LensException;
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.hive.metastore.api.FieldSchema;
 import org.apache.hadoop.hive.ql.lib.Node;
-import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.parse.ASTNode;
 import org.apache.hadoop.hive.ql.parse.HiveParser;
 import org.apache.hadoop.hive.ql.session.SessionState;
@@ -331,11 +330,7 @@ public class CandidateFact implements CandidateTable, QueryAST {
     Set<String> timePartDimensions = new HashSet<String>();
     String singleStorageTable = storageTables.iterator().next();
     List<FieldSchema> partitionKeys = null;
-    try {
-      partitionKeys = query.getMetastoreClient().getTable(singleStorageTable).getPartitionKeys();
-    } catch (HiveException e) {
-      throw new LensException(e);
-    }
+    partitionKeys = query.getMetastoreClient().getTable(singleStorageTable).getPartitionKeys();
     for (FieldSchema fs : partitionKeys) {
       if (cubeTimeDimensions.contains(CubeQueryContext.getTimeDimOfPartitionColumn(baseTable, fs.getName()))) {
         timePartDimensions.add(fs.getName());


[3/4] lens git commit: LENS-1187: Refactor exception handling in MetastoreResource

Posted by pr...@apache.org.
http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateTableResolver.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateTableResolver.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateTableResolver.java
index 31d2d2c..12d6e3a 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateTableResolver.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/CandidateTableResolver.java
@@ -31,7 +31,6 @@ import org.apache.lens.server.api.error.LensException;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hive.ql.metadata.HiveException;
 
 import com.google.common.collect.Sets;
 
@@ -88,27 +87,23 @@ class CandidateTableResolver implements ContextRewriter {
   }
 
   private void populateCandidateTables(CubeQueryContext cubeql) throws LensException {
-    try {
-      if (cubeql.getCube() != null) {
-        List<CubeFactTable> factTables = cubeql.getMetastoreClient().getAllFacts(cubeql.getCube());
-        if (factTables.isEmpty()) {
-          throw new LensException(LensCubeErrorCode.NO_CANDIDATE_FACT_AVAILABLE.getLensErrorInfo(),
-              cubeql.getCube().getName() + " does not have any facts");
-        }
-        for (CubeFactTable fact : factTables) {
-          CandidateFact cfact = new CandidateFact(fact, cubeql.getCube());
-          cubeql.getCandidateFacts().add(cfact);
-        }
-        log.info("Populated candidate facts: {}", cubeql.getCandidateFacts());
+    if (cubeql.getCube() != null) {
+      List<CubeFactTable> factTables = cubeql.getMetastoreClient().getAllFacts(cubeql.getCube());
+      if (factTables.isEmpty()) {
+        throw new LensException(LensCubeErrorCode.NO_CANDIDATE_FACT_AVAILABLE.getLensErrorInfo(),
+            cubeql.getCube().getName() + " does not have any facts");
+      }
+      for (CubeFactTable fact : factTables) {
+        CandidateFact cfact = new CandidateFact(fact, cubeql.getCube());
+        cubeql.getCandidateFacts().add(cfact);
       }
+      log.info("Populated candidate facts: {}", cubeql.getCandidateFacts());
+    }
 
-      if (cubeql.getDimensions().size() != 0) {
-        for (Dimension dim : cubeql.getDimensions()) {
-          populateDimTables(dim, cubeql, false);
-        }
+    if (cubeql.getDimensions().size() != 0) {
+      for (Dimension dim : cubeql.getDimensions()) {
+        populateDimTables(dim, cubeql, false);
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
     }
   }
 
@@ -116,27 +111,23 @@ class CandidateTableResolver implements ContextRewriter {
     if (cubeql.getCandidateDimTables().get(dim) != null) {
       return;
     }
-    try {
-      Set<CandidateDim> candidates = new HashSet<>();
-      cubeql.getCandidateDimTables().put(dim, candidates);
-      List<CubeDimensionTable> dimtables = cubeql.getMetastoreClient().getAllDimensionTables(dim);
-      if (dimtables.isEmpty()) {
-        if (!optional) {
-          throw new LensException(LensCubeErrorCode.NO_CANDIDATE_DIM_AVAILABLE.getLensErrorInfo(),
-                  dim.getName().concat(" has no dimension tables"));
-        } else {
-          log.info("Not considering optional dimension {}  as, No dimension tables exist", dim);
-          removeOptionalDimWithoutAlias(cubeql, dim);
-        }
-      }
-      for (CubeDimensionTable dimtable : dimtables) {
-        CandidateDim cdim = new CandidateDim(dimtable, dim);
-        candidates.add(cdim);
+    Set<CandidateDim> candidates = new HashSet<>();
+    cubeql.getCandidateDimTables().put(dim, candidates);
+    List<CubeDimensionTable> dimtables = cubeql.getMetastoreClient().getAllDimensionTables(dim);
+    if (dimtables.isEmpty()) {
+      if (!optional) {
+        throw new LensException(LensCubeErrorCode.NO_CANDIDATE_DIM_AVAILABLE.getLensErrorInfo(),
+                dim.getName().concat(" has no dimension tables"));
+      } else {
+        log.info("Not considering optional dimension {}  as, No dimension tables exist", dim);
+        removeOptionalDimWithoutAlias(cubeql, dim);
       }
-      log.info("Populated candidate dims: {} for {}", cubeql.getCandidateDimTables().get(dim), dim);
-    } catch (HiveException e) {
-      throw new LensException(e);
     }
+    for (CubeDimensionTable dimtable : dimtables) {
+      CandidateDim cdim = new CandidateDim(dimtable, dim);
+      candidates.add(cdim);
+    }
+    log.info("Populated candidate dims: {} for {}", cubeql.getCandidateDimTables().get(dim), dim);
   }
 
   private void removeOptionalDimWithoutAlias(CubeQueryContext cubeql, Dimension dim) {

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/parse/CubeQueryContext.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/CubeQueryContext.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/CubeQueryContext.java
index cf32746..63ec8b2 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/CubeQueryContext.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/CubeQueryContext.java
@@ -334,7 +334,8 @@ public class CubeQueryContext implements TrackQueriedColumns, QueryAST {
       } else {
         return false;
       }
-    } catch (HiveException e) {
+    } catch (LensException e) {
+      //TODO: check if catch can be removed
       return false;
     }
     return true;

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/parse/FieldValidator.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/FieldValidator.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/FieldValidator.java
index 36ee9d4..7f4aa93 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/FieldValidator.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/FieldValidator.java
@@ -29,7 +29,6 @@ import org.apache.lens.cube.metadata.ReferencedDimAttribute.ChainRefCol;
 import org.apache.lens.cube.parse.ExpressionResolver.ExprSpecContext;
 import org.apache.lens.server.api.error.LensException;
 
-import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.parse.ASTNode;
 import org.apache.hadoop.hive.ql.parse.HiveParser;
 
@@ -54,11 +53,7 @@ public class FieldValidator implements ContextRewriter {
     if (!cube.allFieldsQueriable()) {
       // do queried field validation
       List<DerivedCube> dcubes;
-      try {
-        dcubes = cubeql.getMetastoreClient().getAllDerivedQueryableCubes(cube);
-      } catch (HiveException e) {
-        throw new LensException(e);
-      }
+      dcubes = cubeql.getMetastoreClient().getAllDerivedQueryableCubes(cube);
 
       ImmutableSet<String> queriedTimeDimCols = cubeql.getQueriedTimeDimCols();
 

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/parse/JoinResolver.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/JoinResolver.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/JoinResolver.java
index 7dceee1..7b865bf 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/JoinResolver.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/JoinResolver.java
@@ -88,7 +88,7 @@ class JoinResolver implements ContextRewriter {
     }
   }
 
-  private void processJoinChains(CubeQueryContext cubeql) throws HiveException {
+  private void processJoinChains(CubeQueryContext cubeql) throws HiveException, LensException {
     for (JoinChain chain : cubeql.getJoinchains().values()) {
       Set<String> dims = chain.getIntermediateDimensions();
 

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/cube/parse/StorageTableResolver.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/cube/parse/StorageTableResolver.java b/lens-cube/src/main/java/org/apache/lens/cube/parse/StorageTableResolver.java
index f685500..db26718 100644
--- a/lens-cube/src/main/java/org/apache/lens/cube/parse/StorageTableResolver.java
+++ b/lens-cube/src/main/java/org/apache/lens/cube/parse/StorageTableResolver.java
@@ -318,38 +318,34 @@ class StorageTableResolver implements ContextRewriter {
   private TimeRange getFallbackRange(TimeRange range, CandidateFact cfact, CubeQueryContext cubeql)
     throws LensException {
     Cube baseCube = cubeql.getBaseCube();
-    try {
-      ArrayList<String> tableNames = Lists.newArrayList(cfact.fact.getName(), cubeql.getCube().getName());
-      if (!cubeql.getCube().getName().equals(baseCube.getName())) {
-        tableNames.add(baseCube.getName());
-      }
-      String fallBackString = null;
-      String timedim = baseCube.getTimeDimOfPartitionColumn(range.getPartitionColumn());
-      for (String tableName : tableNames) {
-        fallBackString = cubeql.getMetastoreClient().getTable(tableName).getParameters()
-          .get(MetastoreConstants.TIMEDIM_RELATION + timedim);
-        if (StringUtils.isNotBlank(fallBackString)) {
-          break;
-        }
-      }
-      if (StringUtils.isBlank(fallBackString)) {
-        return null;
-      }
-      Matcher matcher = Pattern.compile("(.*?)\\+\\[(.*?),(.*?)\\]").matcher(fallBackString.replaceAll(WSPACE, ""));
-      if (!matcher.matches()) {
-        return null;
+    ArrayList<String> tableNames = Lists.newArrayList(cfact.fact.getName(), cubeql.getCube().getName());
+    if (!cubeql.getCube().getName().equals(baseCube.getName())) {
+      tableNames.add(baseCube.getName());
+    }
+    String fallBackString = null;
+    String timedim = baseCube.getTimeDimOfPartitionColumn(range.getPartitionColumn());
+    for (String tableName : tableNames) {
+      fallBackString = cubeql.getMetastoreClient().getTable(tableName).getParameters()
+        .get(MetastoreConstants.TIMEDIM_RELATION + timedim);
+      if (StringUtils.isNotBlank(fallBackString)) {
+        break;
       }
-      DateUtil.TimeDiff diff1 = DateUtil.TimeDiff.parseFrom(matcher.group(2).trim());
-      DateUtil.TimeDiff diff2 = DateUtil.TimeDiff.parseFrom(matcher.group(3).trim());
-      String relatedTimeDim = matcher.group(1).trim();
-      String fallbackPartCol = baseCube.getPartitionColumnOfTimeDim(relatedTimeDim);
-      return TimeRange.getBuilder()
-        .fromDate(diff2.negativeOffsetFrom(range.getFromDate()))
-        .toDate(diff1.negativeOffsetFrom(range.getToDate()))
-        .partitionColumn(fallbackPartCol).build();
-    } catch (HiveException e) {
-      throw new LensException(e);
     }
+    if (StringUtils.isBlank(fallBackString)) {
+      return null;
+    }
+    Matcher matcher = Pattern.compile("(.*?)\\+\\[(.*?),(.*?)\\]").matcher(fallBackString.replaceAll(WSPACE, ""));
+    if (!matcher.matches()) {
+      return null;
+    }
+    DateUtil.TimeDiff diff1 = DateUtil.TimeDiff.parseFrom(matcher.group(2).trim());
+    DateUtil.TimeDiff diff2 = DateUtil.TimeDiff.parseFrom(matcher.group(3).trim());
+    String relatedTimeDim = matcher.group(1).trim();
+    String fallbackPartCol = baseCube.getPartitionColumnOfTimeDim(relatedTimeDim);
+    return TimeRange.getBuilder()
+      .fromDate(diff2.negativeOffsetFrom(range.getFromDate()))
+      .toDate(diff1.negativeOffsetFrom(range.getToDate()))
+      .partitionColumn(fallbackPartCol).build();
   }
 
   private void resolveFactStoragePartitions(CubeQueryContext cubeql) throws LensException {

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/main/java/org/apache/lens/driver/cube/RewriterPlan.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/main/java/org/apache/lens/driver/cube/RewriterPlan.java b/lens-cube/src/main/java/org/apache/lens/driver/cube/RewriterPlan.java
index 7725043..fd6c30d 100644
--- a/lens-cube/src/main/java/org/apache/lens/driver/cube/RewriterPlan.java
+++ b/lens-cube/src/main/java/org/apache/lens/driver/cube/RewriterPlan.java
@@ -27,9 +27,9 @@ import org.apache.lens.cube.parse.CandidateTable;
 import org.apache.lens.cube.parse.CubeQueryContext;
 import org.apache.lens.server.api.LensConfConstants;
 import org.apache.lens.server.api.driver.DriverQueryPlan;
+import org.apache.lens.server.api.error.LensException;
 import org.apache.lens.server.api.query.cost.QueryCost;
 
-import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.metadata.Table;
 
 import lombok.extern.slf4j.Slf4j;
@@ -73,7 +73,7 @@ public final class RewriterPlan extends DriverQueryPlan {
           Table tbl;
           try {
             tbl = ctx.getMetastoreClient().getTable(table);
-          } catch (HiveException e) {
+          } catch (LensException e) {
             log.error("Error while getting table:" + table, e);
             continue;
           }

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java b/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
index f84f2e0..62c7ec9 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/metadata/TestCubeMetastoreClient.java
@@ -639,7 +639,7 @@ public class TestCubeMetastoreClient {
   }
 
   private void validateDim(Dimension udim, Set<CubeDimAttribute> attrs, String basedim, String referdim)
-    throws HiveException {
+    throws HiveException, LensException {
     assertTrue(client.tableExists(udim.getName()));
     Table dimTbl = client.getHiveTable(udim.getName());
     assertTrue(client.isDimension(dimTbl));
@@ -1048,14 +1048,14 @@ public class TestCubeMetastoreClient {
     // test partition
     StoragePartitionDesc partSpec = new StoragePartitionDesc(cubeFact.getName(), timeParts, null, HOURLY);
     try {
-      client.addPartition(partSpec, c1);
+      client.addPartition(partSpec, c1, CubeTableType.FACT);
       fail("Add should fail since non_existing_part_col is non-existing");
     } catch (LensException e) {
       assertEquals(e.getErrorCode(), LensCubeErrorCode.TIMELINE_ABSENT.getLensErrorInfo().getErrorCode());
     }
     timeParts.remove("non_existing_part_col");
     partSpec = new StoragePartitionDesc(cubeFact.getName(), timeParts, null, HOURLY);
-    client.addPartition(partSpec, c1);
+    client.addPartition(partSpec, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFact.getName(), c1, HOURLY, timeParts, emptyHashMap));
     assertTrue(client.latestPartitionExists(cubeFact.getName(), c1, getDatePartitionKey()));
 
@@ -1072,7 +1072,7 @@ public class TestCubeMetastoreClient {
     StoragePartitionDesc partSpec2 = new StoragePartitionDesc(cubeFact.getName(), timeParts2, null, HOURLY);
     partSpec2.setInputFormat(SequenceFileInputFormat.class.getCanonicalName());
     partSpec2.setOutputFormat(HiveIgnoreKeyTextOutputFormat.class.getCanonicalName());
-    client.addPartition(partSpec2, c1);
+    client.addPartition(partSpec2, c1, CubeTableType.FACT);
     assertEquals(client.getAllParts(storageTableName).size(), 1);
     assertTrue(client.factPartitionExists(cubeFact.getName(), c1, HOURLY, timeParts, emptyHashMap));
     assertFalse(client.factPartitionExists(cubeFact.getName(), c1, HOURLY, timeParts2, emptyHashMap));
@@ -1334,7 +1334,7 @@ public class TestCubeMetastoreClient {
     //test partition
     Map<String, Date> timeParts = getTimePartitionByOffsets(getDatePartitionKey(), 0, testDtPart.getName(), -1);
     StoragePartitionDesc partSpec = new StoragePartitionDesc(cubeFact.getName(), timeParts, null, HOURLY);
-    client.addPartition(partSpec, c1);
+    client.addPartition(partSpec, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFact.getName(), c1, HOURLY, timeParts, emptyHashMap));
     assertTrue(client.latestPartitionExists(cubeFact.getName(), c1, testDtPart.getName()));
     assertTrue(client.latestPartitionExists(cubeFact.getName(), c1, getDatePartitionKey()));
@@ -1434,8 +1434,8 @@ public class TestCubeMetastoreClient {
     final StoragePartitionDesc partSpec3 = new StoragePartitionDesc(cubeFact.getName(), timeParts3, nonTimeSpec,
       HOURLY);
 
-    client.addPartitions(Arrays.asList(partSpec1, partSpec2, partSpec3), c1);
-    client.addPartitions(Arrays.asList(partSpec1, partSpec2, partSpec3), c2);
+    client.addPartitions(Arrays.asList(partSpec1, partSpec2, partSpec3), c1, CubeTableType.FACT);
+    client.addPartitions(Arrays.asList(partSpec1, partSpec2, partSpec3), c2, CubeTableType.FACT);
     PartitionTimeline timeline1Temp = client.partitionTimelineCache.get(factName, c1, HOURLY, getDatePartitionKey());
     PartitionTimeline timeline2Temp = client.partitionTimelineCache.get(factName, c2, HOURLY, getDatePartitionKey());
 
@@ -1467,8 +1467,8 @@ public class TestCubeMetastoreClient {
       etPart.getName(), -2);
     final StoragePartitionDesc partSpec5 = new StoragePartitionDesc(cubeFact.getName(), timeParts5, null, HOURLY);
 
-    client.addPartitions(Arrays.asList(partSpec4, partSpec5), c1);
-    client.addPartitions(Arrays.asList(partSpec4, partSpec5), c2);
+    client.addPartitions(Arrays.asList(partSpec4, partSpec5), c1, CubeTableType.FACT);
+    client.addPartitions(Arrays.asList(partSpec4, partSpec5), c2, CubeTableType.FACT);
 
     assertEquals(client.getAllParts(c1TableName).size(), 5);
     assertEquals(client.getAllParts(c2TableName).size(), 5);
@@ -1487,8 +1487,8 @@ public class TestCubeMetastoreClient {
       etPart.getName(), -2);
     final StoragePartitionDesc partSpec6 = new StoragePartitionDesc(cubeFact.getName(), timeParts6, null, HOURLY);
 
-    client.addPartition(partSpec6, c1);
-    client.addPartition(partSpec6, c2);
+    client.addPartition(partSpec6, c1, CubeTableType.FACT);
+    client.addPartition(partSpec6, c2, CubeTableType.FACT);
 
     assertEquals(client.getAllParts(c1TableName).size(), 6);
     assertEquals(client.getAllParts(c2TableName).size(), 6);
@@ -1503,8 +1503,8 @@ public class TestCubeMetastoreClient {
       etPart.getName(), -5);
     final StoragePartitionDesc partSpec7 = new StoragePartitionDesc(cubeFact.getName(), timeParts7, null, HOURLY);
 
-    client.addPartition(partSpec7, c1);
-    client.addPartition(partSpec7, c2);
+    client.addPartition(partSpec7, c1, CubeTableType.FACT);
+    client.addPartition(partSpec7, c2, CubeTableType.FACT);
 
     List<Partition> c1Parts = client.getAllParts(c1TableName);
     List<Partition> c2Parts = client.getAllParts(c2TableName);
@@ -1763,7 +1763,7 @@ public class TestCubeMetastoreClient {
     // test partition
     Map<String, Date> timeParts = getTimePartitionByOffsets(getDatePartitionKey(), 0);
     StoragePartitionDesc partSpec = new StoragePartitionDesc(cubeFact.getName(), timeParts, null, HOURLY);
-    client.addPartition(partSpec, c1);
+    client.addPartition(partSpec, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFact.getName(), c1, HOURLY, timeParts, emptyHashMap));
     assertTrue(client.latestPartitionExists(cubeFact.getName(), c1, getDatePartitionKey()));
     String storageTableName = getFactOrDimtableStorageTableName(cubeFact.getName(), c1);
@@ -1822,7 +1822,7 @@ public class TestCubeMetastoreClient {
     // test partition
     StoragePartitionDesc sPartSpec =
       new StoragePartitionDesc(cubeFactWithParts.getName(), timeParts, partSpec, HOURLY);
-    client.addPartition(sPartSpec, c1);
+    client.addPartition(sPartSpec, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFactWithParts.getName(), c1, HOURLY, timeParts, partSpec));
     assertTrue(client.latestPartitionExists(cubeFactWithParts.getName(), c1, getDatePartitionKey()));
     assertFalse(client.latestPartitionExists(cubeFactWithParts.getName(), c1, factPartColumns.get(0).getName()));
@@ -1898,7 +1898,7 @@ public class TestCubeMetastoreClient {
     storageDescs.add(sPartSpecNow);
     storageDescs.add(sPartSpecTwoMonthsBack);
 
-    client.addPartitions(storageDescs, c1);
+    client.addPartitions(storageDescs, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFactWithParts.getName(), c1, HOURLY, timePartsNow, partSpec));
     assertFalse(client.factPartitionExists(cubeFactWithParts.getName(), c1, HOURLY,
             timePartsBeforeTwoMonths, partSpec));
@@ -1968,7 +1968,7 @@ public class TestCubeMetastoreClient {
     storageDescs.add(sPartSpecTwoMonthsBack);
 
     client.getTimelines(factNameSkipPart, null, null, null);
-    client.addPartitions(storageDescs, c1);
+    client.addPartitions(storageDescs, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFactWithParts.getName(), c1, HOURLY, timePartsNow, partSpec));
     assertFalse(client.factPartitionExists(cubeFactWithParts.getName(), c1, HOURLY,
             timePartsBeforeTwoMonths, partSpec));
@@ -2022,7 +2022,7 @@ public class TestCubeMetastoreClient {
     // test partition
     StoragePartitionDesc sPartSpec =
       new StoragePartitionDesc(cubeFactWithParts.getName(), timeParts, partSpec, HOURLY);
-    client.addPartition(sPartSpec, c1);
+    client.addPartition(sPartSpec, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFactWithParts.getName(), c1, HOURLY, timeParts, partSpec));
     assertTrue(client.latestPartitionExists(cubeFactWithParts.getName(), c1, getDatePartitionKey()));
     assertTrue(client.latestPartitionExists(cubeFactWithParts.getName(), c1, testDtPart.getName()));
@@ -2086,7 +2086,7 @@ public class TestCubeMetastoreClient {
     // test partition
     StoragePartitionDesc sPartSpec =
       new StoragePartitionDesc(cubeFactWithTwoStorages.getName(), timeParts, partSpec, HOURLY);
-    client.addPartition(sPartSpec, c1);
+    client.addPartition(sPartSpec, c1, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFactWithTwoStorages.getName(), c1, HOURLY, timeParts, partSpec));
     assertTrue(client.latestPartitionExists(cubeFactWithTwoStorages.getName(), c1, getDatePartitionKey()));
     String storageTableName = getFactOrDimtableStorageTableName(cubeFactWithTwoStorages.getName(), c1);
@@ -2096,7 +2096,7 @@ public class TestCubeMetastoreClient {
 
     StoragePartitionDesc sPartSpec2 =
       new StoragePartitionDesc(cubeFactWithTwoStorages.getName(), timeParts, null, HOURLY);
-    client.addPartition(sPartSpec2, c2);
+    client.addPartition(sPartSpec2, c2, CubeTableType.FACT);
     assertTrue(client.factPartitionExists(cubeFactWithTwoStorages.getName(), c2, HOURLY, timeParts, emptyHashMap));
     assertTrue(client.latestPartitionExists(cubeFactWithTwoStorages.getName(), c2, getDatePartitionKey()));
     String storageTableName2 = getFactOrDimtableStorageTableName(cubeFactWithTwoStorages.getName(), c2);
@@ -2159,7 +2159,7 @@ public class TestCubeMetastoreClient {
     // test partition
     Map<String, Date> timeParts = getTimePartitionByOffsets(getDatePartitionKey(), 0);
     StoragePartitionDesc sPartSpec = new StoragePartitionDesc(cubeDim.getName(), timeParts, null, HOURLY);
-    client.addPartition(sPartSpec, c1);
+    client.addPartition(sPartSpec, c1, CubeTableType.DIM_TABLE);
     assertTrue(client.dimPartitionExists(cubeDim.getName(), c1, timeParts));
     assertTrue(client.latestPartitionExists(cubeDim.getName(), c1, getDatePartitionKey()));
     String storageTableName = getFactOrDimtableStorageTableName(dimName, c1);
@@ -2229,7 +2229,7 @@ public class TestCubeMetastoreClient {
     Map<String, Date> timePartsNow = getHashMap(getDatePartitionKey(), NOW);
     StoragePartitionDesc sPartSpec0 = new StoragePartitionDesc(cubeDim.getName(), timePartsNow, null, HOURLY);
 
-    client.addPartition(sPartSpec0, c1);
+    client.addPartition(sPartSpec0, c1, CubeTableType.DIM_TABLE);
     assertTrue(client.dimPartitionExists(cubeDim.getName(), c1, timePartsNow));
     assertTrue(client.latestPartitionExists(cubeDim.getName(), c1, getDatePartitionKey()));
     assertEquals(client.getAllParts(storageTableName).size(), 2);
@@ -2249,7 +2249,7 @@ public class TestCubeMetastoreClient {
     StoragePartitionDesc sPartSpec1 = new StoragePartitionDesc(cubeDim.getName(), timeParts1, null, HOURLY);
     sPartSpec1.setInputFormat(SequenceFileInputFormat.class.getCanonicalName());
     sPartSpec1.setOutputFormat(HiveIgnoreKeyTextOutputFormat.class.getCanonicalName());
-    client.addPartition(sPartSpec1, c1);
+    client.addPartition(sPartSpec1, c1, CubeTableType.DIM_TABLE);
     // assert on all partitions
     assertEquals(client.getAllParts(storageTableName).size(), 3);
     // non-latest partitions
@@ -2267,7 +2267,7 @@ public class TestCubeMetastoreClient {
     // add one more partition
     Map<String, Date> timeParts2 = getTimePartitionByOffsets(getDatePartitionKey(), 2);
     StoragePartitionDesc sPartSpec2 = new StoragePartitionDesc(cubeDim.getName(), timeParts2, null, HOURLY);
-    client.addPartition(sPartSpec2, c1);
+    client.addPartition(sPartSpec2, c1, CubeTableType.DIM_TABLE);
     // assert on all partitions
     assertEquals(client.getAllParts(storageTableName).size(), 4);
     // non-latest partitions
@@ -2314,7 +2314,7 @@ public class TestCubeMetastoreClient {
     assertEquals(client.getAllParts(storageTableName).size(), 0);
     assertFalse(client.dimTableLatestPartitionExists(storageTableName));
 
-    client.addPartition(sPartSpec1, c1);
+    client.addPartition(sPartSpec1, c1, CubeTableType.DIM_TABLE);
     assertTrue(client.dimTableLatestPartitionExists(storageTableName));
     client.dropStorageFromDim(cubeDim.getName(), c1);
     assertFalse(client.dimTableLatestPartitionExists(storageTableName));
@@ -2350,14 +2350,14 @@ public class TestCubeMetastoreClient {
     timeParts.put(getDatePartitionKey(), NOW);
     nonTimeParts.put("region", "asia");
     StoragePartitionDesc sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, HOURLY);
-    client.addPartition(sPartSpec, c3);
+    client.addPartition(sPartSpec, c3, CubeTableType.DIM_TABLE);
     expectedLatestValues.put("asia", NOW);
     assertLatestForRegions(storageTableName, expectedLatestValues);
 
     timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, -1));
     nonTimeParts.put("region", "africa");
     sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, HOURLY);
-    client.addPartition(sPartSpec, c3);
+    client.addPartition(sPartSpec, c3, CubeTableType.DIM_TABLE);
     expectedLatestValues.put("asia", NOW);
     expectedLatestValues.put("africa", getDateWithOffset(HOURLY, -1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
@@ -2365,7 +2365,7 @@ public class TestCubeMetastoreClient {
     timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, 1));
     nonTimeParts.put("region", "africa");
     sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, HOURLY);
-    client.addPartition(sPartSpec, c3);
+    client.addPartition(sPartSpec, c3, CubeTableType.DIM_TABLE);
     expectedLatestValues.put("asia", NOW);
     expectedLatestValues.put("africa", getDateWithOffset(HOURLY, 1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
@@ -2373,7 +2373,7 @@ public class TestCubeMetastoreClient {
     timeParts.put(getDatePartitionKey(), getDateWithOffset(HOURLY, 3));
     nonTimeParts.put("region", "asia");
     sPartSpec = new StoragePartitionDesc(dimName, timeParts, nonTimeParts, HOURLY);
-    client.addPartition(sPartSpec, c3);
+    client.addPartition(sPartSpec, c3, CubeTableType.DIM_TABLE);
     expectedLatestValues.put("asia", getDateWithOffset(HOURLY, 3));
     expectedLatestValues.put("africa", getDateWithOffset(HOURLY, 1));
     assertLatestForRegions(storageTableName, expectedLatestValues);
@@ -2467,7 +2467,7 @@ public class TestCubeMetastoreClient {
     Map<String, Date> timeParts3 = getTimePartitionByOffsets(getDatePartitionKey(), 0, etPart.getName(), 0);
     final StoragePartitionDesc partSpec3 = new StoragePartitionDesc(cubeDim.getName(), timeParts3, nonTimeSpec, HOURLY);
 
-    client.addPartitions(Arrays.asList(partSpec1, partSpec2, partSpec3), c1);
+    client.addPartitions(Arrays.asList(partSpec1, partSpec2, partSpec3), c1, CubeTableType.DIM_TABLE);
     String c1TableName = getFactOrDimtableStorageTableName(cubeDim.getName(), c1);
     assertEquals(client.getAllParts(c1TableName).size(), 8);
 
@@ -2481,7 +2481,7 @@ public class TestCubeMetastoreClient {
       etPart.getName(), -2);
     final StoragePartitionDesc partSpec5 = new StoragePartitionDesc(cubeDim.getName(), timeParts5, null, HOURLY);
 
-    client.addPartitions(Arrays.asList(partSpec4, partSpec5), c1);
+    client.addPartitions(Arrays.asList(partSpec4, partSpec5), c1, CubeTableType.DIM_TABLE);
 
     assertEquals(client.getAllParts(c1TableName).size(), 10);
     assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), toPartitionArray(HOURLY, 1, 1, 1));
@@ -2489,7 +2489,7 @@ public class TestCubeMetastoreClient {
       etPart.getName(), -2);
     final StoragePartitionDesc partSpec6 = new StoragePartitionDesc(cubeDim.getName(), timeParts6, null, HOURLY);
 
-    client.addPartition(partSpec6, c1);
+    client.addPartition(partSpec6, c1, CubeTableType.DIM_TABLE);
 
     assertEquals(client.getAllParts(c1TableName).size(), 11);
 
@@ -2497,7 +2497,7 @@ public class TestCubeMetastoreClient {
       etPart.getName(), -5);
     final StoragePartitionDesc partSpec7 = new StoragePartitionDesc(cubeDim.getName(), timeParts7, null, HOURLY);
 
-    client.addPartition(partSpec7, c1);
+    client.addPartition(partSpec7, c1, CubeTableType.DIM_TABLE);
     assertEquals(client.getAllParts(c1TableName).size(), 12);
     assertEquals(getLatestValues(c1TableName, HOURLY, partColNames, null), toPartitionArray(HOURLY, 1, 1, 1));
 

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
----------------------------------------------------------------------
diff --git a/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java b/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
index 86db011..ea6b4a1 100644
--- a/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
+++ b/lens-cube/src/test/java/org/apache/lens/cube/parse/CubeTestSetup.java
@@ -1637,7 +1637,7 @@ public class CubeTestSetup {
       timeParts.put("ttd", temp);
       timeParts.put("ttd2", temp);
       StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
-      client.addPartition(sPartSpec, c99);
+      client.addPartition(sPartSpec, c99, CubeTableType.FACT);
       cal.add(HOUR_OF_DAY, 1);
       temp = cal.getTime();
     }
@@ -1650,7 +1650,7 @@ public class CubeTestSetup {
       timeParts.put("ttd", temp);
       timeParts.put("ttd2", temp);
       StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
-      client.addPartition(sPartSpec, c99);
+      client.addPartition(sPartSpec, c99, CubeTableType.FACT);
       cal.add(HOUR_OF_DAY, 1);
       temp = cal.getTime();
     }
@@ -1748,7 +1748,7 @@ public class CubeTestSetup {
       timeParts.put(TestCubeMetastoreClient.getDatePartitionKey(), temp);
       StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
       try {
-        client.addPartition(sPartSpec, c1);
+        client.addPartition(sPartSpec, c1, CubeTableType.FACT);
       } catch (HiveException e) {
         log.error("Encountered Hive exception.", e);
       } catch (LensException e) {
@@ -1765,7 +1765,7 @@ public class CubeTestSetup {
       Map<String, Date> timeParts = new HashMap<String, Date>();
       timeParts.put(TestCubeMetastoreClient.getDatePartitionKey(), temp);
       StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
-      client.addPartition(sPartSpec, c1);
+      client.addPartition(sPartSpec, c1, CubeTableType.FACT);
       cal.add(HOUR_OF_DAY, 1);
       temp = cal.getTime();
     }
@@ -1803,7 +1803,7 @@ public class CubeTestSetup {
       cal.add(HOUR_OF_DAY, 1);
       temp = cal.getTime();
     }
-    client.addPartitions(storagePartitionDescs, c4);
+    client.addPartitions(storagePartitionDescs, c4, CubeTableType.FACT);
     client.clearHiveTableCache();
     table = client.getTable(MetastoreUtil.getStorageTableName(fact.getName(), Storage.getPrefix(c4)));
     assertEquals(table.getParameters().get(MetastoreUtil.getPartitionTimelineCachePresenceKey()), "true");
@@ -1818,7 +1818,7 @@ public class CubeTestSetup {
       timeParts.put("ttd", temp);
       timeParts.put("ttd2", temp);
       StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
-      client.addPartition(sPartSpec, c4);
+      client.addPartition(sPartSpec, c4, CubeTableType.FACT);
       cal.add(HOUR_OF_DAY, 1);
       temp = cal.getTime();
     }
@@ -1879,7 +1879,7 @@ public class CubeTestSetup {
       Map<String, Date> timeParts = new HashMap<String, Date>();
       timeParts.put(TestCubeMetastoreClient.getDatePartitionKey(), temp);
       StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact2.getName(), timeParts, null, HOURLY);
-      client.addPartition(sPartSpec, c3);
+      client.addPartition(sPartSpec, c3, CubeTableType.FACT);
       cal.add(HOUR_OF_DAY, 1);
       temp = cal.getTime();
     }
@@ -2983,7 +2983,7 @@ public class CubeTestSetup {
         StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, DAILY);
         pTimes.get(DAILY).add(ptime);
         iTimes.get(DAILY).add(itime);
-        client.addPartition(sPartSpec, storageName);
+        client.addPartition(sPartSpec, storageName, CubeTableType.FACT);
         pcal.add(DAY_OF_MONTH, 1);
         ical.add(HOUR_OF_DAY, 20);
       } else if (p == 2) { // day2
@@ -3000,7 +3000,7 @@ public class CubeTestSetup {
         StoragePartitionDesc sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, DAILY);
         pTimes.get(DAILY).add(ptime);
         iTimes.get(DAILY).add(itime);
-        client.addPartition(sPartSpec, storageName);
+        client.addPartition(sPartSpec, storageName, CubeTableType.FACT);
         // pt=day2-hour[0-3] it = day1-hour[20-23]
         // pt=day2-hour[4-23] it = day2-hour[0-19]
         for (int i = 0; i < 24; i++) {
@@ -3012,7 +3012,7 @@ public class CubeTestSetup {
           sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
           pTimes.get(HOURLY).add(ptime);
           iTimes.get(HOURLY).add(itime);
-          client.addPartition(sPartSpec, storageName);
+          client.addPartition(sPartSpec, storageName, CubeTableType.FACT);
           pcal.add(HOUR_OF_DAY, 1);
           ical.add(HOUR_OF_DAY, 1);
         }
@@ -3020,7 +3020,7 @@ public class CubeTestSetup {
         sPartSpec = new StoragePartitionDesc(fact.getName(), timeParts, null, DAILY);
         pTimes.get(DAILY).add(ptime);
         iTimes.get(DAILY).add(itime);
-        client.addPartition(sPartSpec, storageName);
+        client.addPartition(sPartSpec, storageName, CubeTableType.FACT);
       } else if (p == 3) { // day3
         // pt=day3-hour[0-3] it = day2-hour[20-23]
         // pt=day3-hour[4-23] it = day3-hour[0-19]
@@ -3034,7 +3034,7 @@ public class CubeTestSetup {
             new StoragePartitionDesc(fact.getName(), timeParts, null, HOURLY);
           pTimes.get(HOURLY).add(ptime);
           iTimes.get(HOURLY).add(itime);
-          client.addPartition(sPartSpec, storageName);
+          client.addPartition(sPartSpec, storageName, CubeTableType.FACT);
           pcal.add(HOUR_OF_DAY, 1);
           ical.add(HOUR_OF_DAY, 1);
         }

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-driver-hive/src/test/java/org/apache/lens/driver/hive/TestRemoteHiveDriver.java
----------------------------------------------------------------------
diff --git a/lens-driver-hive/src/test/java/org/apache/lens/driver/hive/TestRemoteHiveDriver.java b/lens-driver-hive/src/test/java/org/apache/lens/driver/hive/TestRemoteHiveDriver.java
index 00d2a1c..6dff173 100644
--- a/lens-driver-hive/src/test/java/org/apache/lens/driver/hive/TestRemoteHiveDriver.java
+++ b/lens-driver-hive/src/test/java/org/apache/lens/driver/hive/TestRemoteHiveDriver.java
@@ -62,6 +62,7 @@ public class TestRemoteHiveDriver extends TestHiveDriver {
 
   /** The Constant HS2_PORT. */
   static final int HS2_PORT = 12345;
+  static final int HS2_UI_PORT = 12346;
 
   /** The server. */
   private static HiveServer2 server;
@@ -91,6 +92,7 @@ public class TestRemoteHiveDriver extends TestHiveDriver {
     remoteConf.set("hive.lock.manager", "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager");
     HiveConf.setVar(remoteConf, HiveConf.ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, HS2_HOST);
     HiveConf.setIntVar(remoteConf, HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT, HS2_PORT);
+    HiveConf.setIntVar(remoteConf, HiveConf.ConfVars.HIVE_SERVER2_WEBUI_PORT, HS2_UI_PORT);
     HiveConf.setIntVar(remoteConf, HiveConf.ConfVars.HIVE_SERVER2_THRIFT_CLIENT_CONNECTION_RETRY_LIMIT, 3);
     HiveConf.setIntVar(remoteConf, HiveConf.ConfVars.HIVE_SERVER2_THRIFT_CLIENT_RETRY_LIMIT, 3);
     HiveConf.setVar(remoteConf, HiveConf.ConfVars.HIVE_SERVER2_THRIFT_CLIENT_RETRY_DELAY_SECONDS, "10s");

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-driver-jdbc/src/main/java/org/apache/lens/driver/jdbc/ColumnarSQLRewriter.java
----------------------------------------------------------------------
diff --git a/lens-driver-jdbc/src/main/java/org/apache/lens/driver/jdbc/ColumnarSQLRewriter.java b/lens-driver-jdbc/src/main/java/org/apache/lens/driver/jdbc/ColumnarSQLRewriter.java
index 34312c0..b1fd459 100644
--- a/lens-driver-jdbc/src/main/java/org/apache/lens/driver/jdbc/ColumnarSQLRewriter.java
+++ b/lens-driver-jdbc/src/main/java/org/apache/lens/driver/jdbc/ColumnarSQLRewriter.java
@@ -1328,7 +1328,7 @@ public class ColumnarSQLRewriter implements QueryRewriter {
               tabNameChild.insertChild(0, dbIdentifier);
             }
           }
-        } catch (HiveException e) {
+        } catch (LensException | HiveException e) {
           log.warn("No corresponding table in metastore:", e);
         }
       }
@@ -1377,7 +1377,7 @@ public class ColumnarSQLRewriter implements QueryRewriter {
   /**
    * Gets the underlying db name.
    *
-   * @param table the table
+   * @param tbl the table
    * @return the underlying db name
    * @throws HiveException the hive exception
    */
@@ -1388,7 +1388,7 @@ public class ColumnarSQLRewriter implements QueryRewriter {
   /**
    * Gets the underlying table name.
    *
-   * @param table the table
+   * @param tbl the table
    * @return the underlying table name
    * @throws HiveException the hive exception
    */

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-server/src/main/java/org/apache/lens/server/BaseApp.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/BaseApp.java b/lens-server/src/main/java/org/apache/lens/server/BaseApp.java
index 07f6837..19de31c 100644
--- a/lens-server/src/main/java/org/apache/lens/server/BaseApp.java
+++ b/lens-server/src/main/java/org/apache/lens/server/BaseApp.java
@@ -25,6 +25,7 @@ import javax.ws.rs.core.Application;
 
 import org.apache.lens.api.jaxb.LensJAXBContextResolver;
 import org.apache.lens.api.util.MoxyJsonConfigurationContextResolver;
+import org.apache.lens.server.error.GenericExceptionMapper;
 import org.apache.lens.server.error.LensJAXBValidationExceptionMapper;
 
 import org.glassfish.jersey.filter.LoggingFilter;
@@ -46,6 +47,7 @@ public abstract class BaseApp extends Application {
     classes.add(LensApplicationListener.class);
     classes.add(MoxyJsonConfigurationContextResolver.class);
     classes.add(MoxyJsonFeature.class);
+    classes.add(GenericExceptionMapper.class);
     return classes;
   }
 

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/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 0b4720e..74bc0be 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
@@ -537,5 +537,18 @@ public abstract class BaseLensService extends CompositeService implements Extern
       throw new LensException(SESSION_CLOSED.getLensErrorInfo(), handle);
     }
   }
+
+  public class SessionContext implements AutoCloseable {
+    private LensSessionHandle sessionHandle;
+
+    public SessionContext(LensSessionHandle sessionHandle) {
+      this.sessionHandle = sessionHandle;
+      acquire(sessionHandle);
+    }
+    @Override
+    public void close() {
+      release(sessionHandle);
+    }
+  }
 }
 

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-server/src/main/java/org/apache/lens/server/LensApplication.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/LensApplication.java b/lens-server/src/main/java/org/apache/lens/server/LensApplication.java
index 71ff9ea..bace84d 100644
--- a/lens-server/src/main/java/org/apache/lens/server/LensApplication.java
+++ b/lens-server/src/main/java/org/apache/lens/server/LensApplication.java
@@ -25,6 +25,7 @@ import javax.ws.rs.ApplicationPath;
 import javax.ws.rs.core.Application;
 
 import org.apache.lens.server.api.LensConfConstants;
+import org.apache.lens.server.error.GenericExceptionMapper;
 
 import org.apache.hadoop.conf.Configuration;
 
@@ -74,7 +75,7 @@ public class LensApplication extends Application {
 
     log.debug("LensRequestContextInitFilter added...");
     classes.add(LensRequestContextInitFilter.class);
-
+    classes.add(GenericExceptionMapper.class);
     return classes;
   }
 

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/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 f6f2f36..7618669 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
@@ -227,7 +227,7 @@ public class LensServices extends CompositeService implements ServiceProvider {
             Class<? extends BaseLensService> serviceClass = (Class<? extends BaseLensService>) cls;
             log.info("Adding {}  service with {}", sName, serviceClass);
             Constructor<?> constructor = serviceClass.getConstructor(CLIService.class);
-            BaseLensService service = (BaseLensService) constructor.newInstance(new Object[]{cliService});
+            BaseLensService service = (BaseLensService) constructor.newInstance(cliService);
             addService(service);
             lensServices.add(service);
           } else if (Service.class.isAssignableFrom(cls)) {

http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-server/src/main/java/org/apache/lens/server/error/GenericExceptionMapper.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/error/GenericExceptionMapper.java b/lens-server/src/main/java/org/apache/lens/server/error/GenericExceptionMapper.java
index 658fcb2..9aa74ed 100644
--- a/lens-server/src/main/java/org/apache/lens/server/error/GenericExceptionMapper.java
+++ b/lens-server/src/main/java/org/apache/lens/server/error/GenericExceptionMapper.java
@@ -24,6 +24,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.Provider;
 
+import org.apache.lens.api.APIResult;
 import org.apache.lens.api.error.ErrorCollection;
 import org.apache.lens.api.result.LensAPIResult;
 import org.apache.lens.api.result.LensErrorTO;
@@ -64,7 +65,9 @@ public class GenericExceptionMapper implements ExceptionMapper<Exception> {
     } else {
       status = Response.Status.INTERNAL_SERVER_ERROR;
     }
-
+    if (extendedUriInfo.getMatchedResourceMethod() == null) {
+      return Response.status(Response.Status.METHOD_NOT_ALLOWED).build();
+    }
     if (extendedUriInfo.getMatchedResourceMethod().getInvocable().getRawResponseType() == LensAPIResult.class) {
       if (le != null) {
         return Response.status(status).entity(le.getLensAPIResult()).build();
@@ -72,6 +75,8 @@ public class GenericExceptionMapper implements ExceptionMapper<Exception> {
       // if no LensException construct LensAPIResult
       LensAPIResult lensAPIResult = constructLensAPIResult(exception, status);
       return Response.status(lensAPIResult.getHttpStatusCode()).entity(lensAPIResult).build();
+    } else if (extendedUriInfo.getMatchedResourceMethod().getInvocable().getRawResponseType() == APIResult.class) {
+      return Response.status(status).entity(APIResult.failure(exception)).build();
     } else {
       return Response.status(status).entity(exception.getMessage()).build();
     }


[2/4] lens git commit: LENS-1187: Refactor exception handling in MetastoreResource

Posted by pr...@apache.org.
http://git-wip-us.apache.org/repos/asf/lens/blob/4bcb7aa9/lens-server/src/main/java/org/apache/lens/server/metastore/CubeMetastoreServiceImpl.java
----------------------------------------------------------------------
diff --git a/lens-server/src/main/java/org/apache/lens/server/metastore/CubeMetastoreServiceImpl.java b/lens-server/src/main/java/org/apache/lens/server/metastore/CubeMetastoreServiceImpl.java
index 595520e..c1f9a02 100644
--- a/lens-server/src/main/java/org/apache/lens/server/metastore/CubeMetastoreServiceImpl.java
+++ b/lens-server/src/main/java/org/apache/lens/server/metastore/CubeMetastoreServiceImpl.java
@@ -36,7 +36,6 @@ import org.apache.lens.server.api.LensConfConstants;
 import org.apache.lens.server.api.error.LensException;
 import org.apache.lens.server.api.health.HealthStatus;
 import org.apache.lens.server.api.metastore.CubeMetastoreService;
-import org.apache.lens.server.session.LensSessionImpl;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.hadoop.conf.Configuration;
@@ -45,7 +44,6 @@ import org.apache.hadoop.hive.metastore.api.*;
 import org.apache.hadoop.hive.ql.metadata.Hive;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.metadata.Partition;
-import org.apache.hadoop.hive.ql.metadata.Table;
 import org.apache.hive.service.cli.CLIService;
 
 import com.google.common.collect.Lists;
@@ -59,7 +57,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   }
 
   synchronized CubeMetastoreClient getClient(LensSessionHandle sessionid) throws LensException {
-    return ((LensSessionImpl) getSession(sessionid)).getCubeMetastoreClient();
+    return getSession(sessionid).getCubeMetastoreClient();
   }
 
 
@@ -70,23 +68,19 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
    */
   @Override
   public String getCurrentDatabase(LensSessionHandle sessionid) throws LensException {
-    try {
-      acquire(sessionid);
+    try(SessionContext ignored = new SessionContext(sessionid)) {
       return getSession(sessionid).getCurrentDatabase();
-    } finally {
-      release(sessionid);
     }
   }
 
   /**
    * Change the current database used by the CubeMetastoreClient
    *
-   * @param database
+   * @param database current database to set
    */
   @Override
   public void setCurrentDatabase(LensSessionHandle sessionid, String database) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)) {
       if (!Hive.get(getSession(sessionid).getHiveConf()).databaseExists(database)) {
         throw new NotFoundException("Database " + database + " does not exist");
       }
@@ -94,8 +88,6 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       getSession(sessionid).setCurrentDatabase(database);
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
@@ -107,16 +99,11 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
    */
   @Override
   public void dropDatabase(LensSessionHandle sessionid, String database, boolean cascade) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Hive.get(getSession(sessionid).getHiveConf()).dropDatabase(database, false, true, cascade);
       log.info("Database dropped " + database + " cascade? " + true);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } catch (NoSuchObjectException e) {
+    } catch (HiveException | NoSuchObjectException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
@@ -129,17 +116,12 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
    */
   @Override
   public void createDatabase(LensSessionHandle sessionid, String database, boolean ignore) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Database db = new Database();
       db.setName(database);
       Hive.get(getSession(sessionid).getHiveConf()).createDatabase(db, ignore);
-    } catch (AlreadyExistsException e) {
-      throw new LensException(e);
-    } catch (HiveException e) {
+    } catch (AlreadyExistsException | HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     log.info("Database created " + database);
   }
@@ -149,13 +131,10 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
    */
   @Override
   public List<String> getAllDatabases(LensSessionHandle sessionid) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       return Hive.get(getSession(sessionid).getHiveConf()).getAllDatabases();
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
@@ -167,20 +146,15 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
    */
   @Override
   public List<String> getAllCubeNames(LensSessionHandle sessionid) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Collection<CubeInterface> cubes = getClient(sessionid).getAllCubes();
       if (cubes != null && !cubes.isEmpty()) {
-        List<String> names = new ArrayList<String>(cubes.size());
+        List<String> names = new ArrayList<>(cubes.size());
         for (CubeInterface cube : cubes) {
           names.add(cube.getName());
         }
         return names;
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -188,44 +162,34 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   /**
    * Create cube based on the JAXB cube object
    *
-   * @param cube
+   * @param cube cube spec
    * @throws LensException
    */
   @Override
   public void createCube(LensSessionHandle sessionid, XCube cube) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       Cube parent = cube instanceof XDerivedCube ? (Cube) msClient.getCube(
         ((XDerivedCube) cube).getParent()) : null;
       msClient.createCube(JAXBUtils.hiveCubeFromXCube(cube, parent));
       log.info("Created cube " + cube.getName());
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   /**
    * Get a cube from the metastore
    *
-   * @param cubeName
+   * @param cubeName cube name
    * @return The cube object as {@link XCube}
    * @throws LensException
    */
   @Override
   public XCube getCube(LensSessionHandle sessionid, String cubeName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeInterface c = getClient(sessionid).getCube(cubeName);
       if (c != null) {
         return JAXBUtils.xCubeFromHiveCube(c);
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -233,17 +197,11 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   /**
    * Drop a cube from the metastore in the currently deleted database
    *
-   * @param cubeName
+   * @param cubeName cube name
    */
   public void dropCube(LensSessionHandle sessionid, String cubeName) throws LensException {
-    try {
-      acquire(sessionid);
+    try(SessionContext ignored = new SessionContext(sessionid)) {
       getClient(sessionid).dropCube(cubeName);
-      log.info("Dropped cube " + cubeName);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
@@ -255,8 +213,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
    */
   @Override
   public void updateCube(LensSessionHandle sessionid, XCube cube) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       Cube parent = cube instanceof XDerivedCube ? (Cube) msClient.getCube(
         ((XDerivedCube) cube).getParent()) : null;
@@ -264,15 +221,13 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       log.info("Cube updated " + cube.getName());
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   /**
    * Create a cube dimension table based on JAXB object
    *
-   * @param xDimTable
+   * @param xDimTable dim table spec
    * @throws LensException
    */
   @Override
@@ -286,8 +241,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
     Map<String, StorageTableDesc> storageDesc = JAXBUtils.storageTableMapFromXStorageTables(
       xDimTable.getStorageTables());
 
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       log.info("# Columns: " + columns);
       getClient(sessionid).createCubeDimensionTable(xDimTable.getDimensionName(),
         dimTblName,
@@ -297,31 +251,20 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         properties,
         storageDesc);
       log.info("Dimension Table created " + xDimTable.getTableName());
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropDimensionTable(LensSessionHandle sessionid, String dimTblName, boolean cascade) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).dropDimensionTable(dimTblName, cascade);
       log.info("Dropped dimension table " + dimTblName + " cascade? " + cascade);
-    } catch (HiveException e) {
-      log.error("@@@@ Got HiveException: >>>>>>>" + e.getMessage() + "<<<<<<<<<", e);
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public XDimensionTable getDimensionTable(LensSessionHandle sessionid, String dimTblName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeDimensionTable dimTable = msClient.getDimensionTable(dimTblName);
       XDimensionTable dt = JAXBUtils.dimTableFromCubeDimTable(dimTable);
@@ -338,38 +281,26 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         }
       }
       return dt;
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void updateDimensionTable(LensSessionHandle sessionid, XDimensionTable dimensionTable) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).alterCubeDimensionTable(dimensionTable.getTableName(),
         JAXBUtils.cubeDimTableFromDimTable(dimensionTable),
         JAXBUtils.storageTableMapFromXStorageTables(dimensionTable.getStorageTables()));
       log.info("Updated dimension table " + dimensionTable.getTableName());
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public List<String> getDimTableStorages(LensSessionHandle sessionid, String dimension) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeDimensionTable dimTable = getClient(sessionid).getDimensionTable(dimension);
-      return new ArrayList<String>(dimTable.getStorages());
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
+      return new ArrayList<>(dimTable.getStorages());
     }
   }
 
@@ -377,8 +308,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public void addDimTableStorage(LensSessionHandle sessionid,
     String dimTblName, XStorageTableElement storageTable) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeDimensionTable dimTable = msClient.getDimensionTable(dimTblName);
       UpdatePeriod period = null;
@@ -389,22 +319,17 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         JAXBUtils.storageTableDescFromXStorageTableDesc(storageTable.getTableDesc()));
       log.info("Added storage " + storageTable.getStorageName() + " for dimension table " + dimTblName
         + " with update period " + period);
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropAllStoragesOfDimTable(LensSessionHandle sessionid, String dimTblName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeDimensionTable tab = msClient.getDimensionTable(dimTblName);
       int total = tab.getStorages().size();
       int i = 0;
-      List<String> storageNames = new ArrayList<String>(tab.getStorages());
+      List<String> storageNames = new ArrayList<>(tab.getStorages());
       for (String s : storageNames) {
         msClient.dropStorageFromDim(dimTblName, s);
         log.info("Dropped storage " + s + " from dimension table " + dimTblName
@@ -413,60 +338,47 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       log.info("Dropped " + total + " storages from dimension table " + dimTblName);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public List<String> getAllDimTableNames(LensSessionHandle sessionid, String dimensionName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient client = getClient(sessionid);
       Dimension dimension = client.getDimension(dimensionName);
       if (dimensionName != null && dimension == null) {
         throw new LensException("Could not get table: " + dimensionName + " as a dimension");
       }
       Collection<CubeDimensionTable> dims = client.getAllDimensionTables(dimension);
-      List<String> dimNames = new ArrayList<String>(dims.size());
+      List<String> dimNames = new ArrayList<>(dims.size());
       for (CubeDimensionTable cdt : dims) {
         dimNames.add(cdt.getName());
       }
       return dimNames;
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropAllStoragesOfFact(LensSessionHandle sessionid, String factName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeFactTable tab = msClient.getFactTable(factName);
       int total = tab.getStorages().size();
       int i = 0;
-      List<String> storageNames = new ArrayList<String>(tab.getStorages());
+      List<String> storageNames = new ArrayList<>(tab.getStorages());
       for (String s : storageNames) {
         msClient.dropStorageFromFact(factName, s);
         log.info("Dropped storage " + s + " from fact table " + factName
           + " [" + ++i + "/" + total + "]");
       }
       log.info("Dropped " + total + " storages from fact table " + factName);
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropStorageOfDimTable(LensSessionHandle sessionid, String dimTblName, String storage)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeDimensionTable tab = msClient.getDimensionTable(dimTblName);
       if (!tab.getStorages().contains(storage)) {
@@ -477,15 +389,12 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       log.info("Dropped storage " + storage + " from dimension table " + dimTblName);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public XFactTable getFactTable(LensSessionHandle sessionid, String fact) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeFactTable cft = msClient.getFactTable(fact);
       XFactTable factTable = JAXBUtils.factTableFromCubeFactTable(cft);
@@ -500,32 +409,22 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         factTable.getStorageTables().getStorageTable().add(tblElement);
       }
       return factTable;
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public XSegmentation getSegmentation(LensSessionHandle sessionid, String cubeSegName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       Segmentation cubeSeg = msClient.getSegmentation(cubeSegName);
       return JAXBUtils.xsegmentationFromSegmentation(cubeSeg);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
 
   @Override
   public void createFactTable(LensSessionHandle sessionid, XFactTable fact) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).createCubeFactTable(fact.getCubeName(),
         fact.getName(),
         JAXBUtils.fieldSchemaListFromColumns(fact.getColumns()),
@@ -534,17 +433,12 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         JAXBUtils.mapFromXProperties(fact.getProperties()),
         JAXBUtils.storageTableMapFromXStorageTables(fact.getStorageTables()));
       log.info("Created fact table " + fact.getName());
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void createSegmentation(LensSessionHandle sessionid, XSegmentation cubeSeg) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).createSegmentation(
               cubeSeg.getCubeName(),
               cubeSeg.getName(),
@@ -552,109 +446,79 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
               cubeSeg.getWeight(),
               JAXBUtils.mapFromXProperties(cubeSeg.getProperties()));
       log.info("Created segmentation " + cubeSeg.getName());
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
 
   @Override
   public void updateFactTable(LensSessionHandle sessionid, XFactTable fact) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).alterCubeFactTable(fact.getName(), JAXBUtils.cubeFactFromFactTable(fact),
         JAXBUtils.storageTableMapFromXStorageTables(fact.getStorageTables()));
       log.info("Updated fact table " + fact.getName());
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void updateSegmentation(LensSessionHandle sessionid, XSegmentation cubeSeg) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).alterSegmentation(cubeSeg.getName(), segmentationFromXSegmentation(cubeSeg));
       log.info("Updated segmentation " + cubeSeg.getName());
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropFactTable(LensSessionHandle sessionid, String fact, boolean cascade) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).dropFact(fact, cascade);
       log.info("Dropped fact table " + fact + " cascade? " + cascade);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropSegmentation(LensSessionHandle sessionid, String cubeSegName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).dropSegmentation(cubeSegName);
       log.info("Dropped segemntation " + cubeSegName);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
 
   }
 
   @Override
   public List<String> getAllFactNames(LensSessionHandle sessionid, String cubeName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient client = getClient(sessionid);
       CubeInterface fact = client.getCube(cubeName);
       if (cubeName != null && fact == null) {
         throw new LensException("Could not get table: " + cubeName + " as a cube");
       }
       Collection<CubeFactTable> facts = client.getAllFacts(fact);
-      List<String> factNames = new ArrayList<String>(facts.size());
+      List<String> factNames = new ArrayList<>(facts.size());
       for (CubeFactTable cft : facts) {
         factNames.add(cft.getName());
       }
       return factNames;
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public List<String> getAllSegmentations(LensSessionHandle sessionid, String cubeName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient client = getClient(sessionid);
       CubeInterface seg = client.getCube(cubeName);
       if (cubeName != null && seg == null) {
         throw new LensException("Could not get table: " + cubeName + " as a cube");
       }
       Collection<Segmentation> segs = client.getAllSegmentations(seg);
-      List<String> segNames = new ArrayList<String>(segs.size());
+      List<String> segNames = new ArrayList<>(segs.size());
       for (Segmentation cs : segs) {
         segNames.add(cs.getName());
       }
       return segNames;
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
 
   }
@@ -662,8 +526,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
 
   @Override
   public List<String> getStoragesOfFact(LensSessionHandle sessionid, String fact) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       if (!msClient.isFactTable(fact)) {
         throw new NotFoundException("Not a fact table " + fact);
@@ -671,21 +534,16 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
 
       CubeFactTable cft = msClient.getFactTable(fact);
       if (cft != null) {
-        return new ArrayList<String>(cft.getStorages());
+        return new ArrayList<>(cft.getStorages());
       } else {
         throw new NotFoundException("Could not get fact table " + fact);
       }
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   public XStorageTableElement getStorageOfFact(LensSessionHandle sessionid, String fact, String storageName)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)) {
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeFactTable factTable = msClient.getFactTable(fact);
       Set<UpdatePeriod> updatePeriods = factTable.getUpdatePeriods().get(storageName);
@@ -696,17 +554,12 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         tblElement.getUpdatePeriods().getUpdatePeriod().add(XUpdatePeriod.valueOf(p.name()));
       }
       return tblElement;
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   public XStorageTableElement getStorageOfDim(LensSessionHandle sessionid, String dimTblName, String storageName)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       CubeDimensionTable dimTable = msClient.getDimensionTable(dimTblName);
       XStorageTableElement tblElement = JAXBUtils.getXStorageTableFromHiveTable(
@@ -717,60 +570,41 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         tblElement.getUpdatePeriods().getUpdatePeriod().add(XUpdatePeriod.valueOf(p.name()));
       }
       return tblElement;
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void addStorageToFact(LensSessionHandle sessionid, String fact, XStorageTableElement storageTable)
     throws LensException {
-    Set<UpdatePeriod> updatePeriods = new TreeSet<UpdatePeriod>();
+    Set<UpdatePeriod> updatePeriods = new TreeSet<>();
     for (XUpdatePeriod sup : storageTable.getUpdatePeriods().getUpdatePeriod()) {
       updatePeriods.add(UpdatePeriod.valueOf(sup.name()));
     }
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient msClient = getClient(sessionid);
       msClient.addStorage(msClient.getFactTable(fact),
         storageTable.getStorageName(), updatePeriods,
         JAXBUtils.storageTableDescFromXStorageTableElement(storageTable));
       log.info("Added storage " + storageTable.getStorageName() + ":" + updatePeriods + " for fact " + fact);
-    } catch (HiveException exc) {
-      throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropStorageOfFact(LensSessionHandle sessionid, String fact, String storage) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkFactStorage(sessionid, fact, storage);
       getClient(sessionid).dropStorageFromFact(fact, storage);
       log.info("Dropped storage " + storage + " from fact " + fact);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   private CubeFactTable checkFactStorage(LensSessionHandle sessionid, String fact, String storage)
     throws HiveException, LensException {
     CubeMetastoreClient client = getClient(sessionid);
-    if (!client.isFactTable(fact)) {
-      throw new NotFoundException("Fact table not found: " + fact);
-    }
-
-    CubeFactTable factTable = client.getFactTable(fact);
-
-    if (!factTable.getStorages().contains(storage)) {
-      throw new NotFoundException("Storage " + storage + " not found for fact table " + fact);
-    }
+    CubeFactTable factTable = client.getCubeFact(fact);
+    client.verifyStorageExists(factTable, storage);
     return factTable;
   }
 
@@ -778,8 +612,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   public XPartitionList getAllPartitionsOfFactStorage(
     LensSessionHandle sessionid, String fact, String storageName,
     String filter) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkFactStorage(sessionid, fact, storageName);
       CubeMetastoreClient client = getClient(sessionid);
       String storageTableName = MetastoreUtil.getFactOrDimtableStorageTableName(fact,
@@ -789,49 +622,38 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       return xpartitionListFromPartitionList(fact, parts, timePartCols);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public int addPartitionToFactStorage(LensSessionHandle sessionid, String fact, String storageName,
     XPartition partition) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkFactStorage(sessionid, fact, storageName);
-      return getClient(sessionid).addPartition(storagePartSpecFromXPartition(partition), storageName).size();
+      return getClient(sessionid).addPartition(storagePartSpecFromXPartition(partition), storageName,
+        CubeTableType.FACT).size();
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public int addPartitionsToFactStorage(LensSessionHandle sessionid, String fact, String storageName,
     XPartitionList partitions) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkFactStorage(sessionid, fact, storageName);
-      return getClient(sessionid).addPartitions(storagePartSpecListFromXPartitionList(partitions), storageName).size();
+      return getClient(sessionid).addPartitions(storagePartSpecListFromXPartitionList(partitions), storageName,
+        CubeTableType.FACT).size();
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   private CubeDimensionTable checkDimTableStorage(LensSessionHandle sessionid, String dimTable, String storage)
     throws HiveException, LensException {
     CubeMetastoreClient client = getClient(sessionid);
-    if (!client.isDimensionTable(dimTable)) {
-      throw new NotFoundException("Dimension table not found: " + dimTable);
-    }
     CubeDimensionTable cdt = client.getDimensionTable(dimTable);
-    if (!cdt.getStorages().contains(storage)) {
-      throw new NotFoundException("Storage " + storage + " not found for dimension table " + dimTable);
-    }
+    client.verifyStorageExists(cdt, storage);
     return cdt;
   }
 
@@ -839,8 +661,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   public XPartitionList getAllPartitionsOfDimTableStorage(
     LensSessionHandle sessionid, String dimTable, String storageName, String filter)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkDimTableStorage(sessionid, dimTable, storageName);
       CubeMetastoreClient client = getClient(sessionid);
       String storageTableName = MetastoreUtil.getFactOrDimtableStorageTableName(dimTable, storageName);
@@ -849,30 +670,25 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       return xpartitionListFromPartitionList(dimTable, partitions, timePartCols);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public int addPartitionToDimStorage(LensSessionHandle sessionid,
     String dimTblName, String storageName, XPartition partition) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkDimTableStorage(sessionid, dimTblName, storageName);
-      return getClient(sessionid).addPartition(storagePartSpecFromXPartition(partition), storageName).size();
+      return getClient(sessionid).addPartition(storagePartSpecFromXPartition(partition), storageName,
+        CubeTableType.DIM_TABLE).size();
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void updatePartition(LensSessionHandle sessionid, String tblName, String storageName,
     XPartition xPartition) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient client = getClient(sessionid);
       String storageTableName = MetastoreUtil.getFactOrDimtableStorageTableName(tblName, storageName);
       Partition existingPartition = client.getPartitionByFilter(storageTableName,
@@ -881,16 +697,13 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       client.updatePartition(tblName, storageName, existingPartition);
     } catch (HiveException | ClassNotFoundException | InvalidOperationException | UnsupportedOperationException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void updatePartitions(LensSessionHandle sessionid, String tblName, String storageName,
     XPartitionList xPartitions) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       CubeMetastoreClient client = getClient(sessionid);
       String storageTableName = MetastoreUtil.getFactOrDimtableStorageTableName(tblName, storageName);
       List<Partition> partitionsToUpdate = new ArrayList<>(xPartitions.getPartition().size());
@@ -903,31 +716,27 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       client.updatePartitions(tblName, storageName, partitionsToUpdate);
     } catch (HiveException | ClassNotFoundException | InvalidOperationException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public int addPartitionsToDimStorage(LensSessionHandle sessionid,
     String dimTblName, String storageName, XPartitionList partitions) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       checkDimTableStorage(sessionid, dimTblName, storageName);
-      return getClient(sessionid).addPartitions(storagePartSpecListFromXPartitionList(partitions), storageName).size();
+      return getClient(sessionid).addPartitions(storagePartSpecListFromXPartitionList(partitions), storageName,
+        CubeTableType.DIM_TABLE).size();
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   private String getFilter(CubeMetastoreClient client, String tableName,
-    String values) throws HiveException {
+    String values) throws LensException {
     List<FieldSchema> cols = client.getHiveTable(tableName).getPartCols();
     String[] vals = StringUtils.split(values, ",");
     if (vals.length != cols.size()) {
-      log.error("Values for all the part columns not specified, cols:" + cols + " vals:" + vals);
+      log.error("Values for all the part columns not specified, cols:" + cols + " vals:" + Arrays.toString(vals));
       throw new BadRequestException("Values for all the part columns not specified");
     }
     StringBuilder filter = new StringBuilder();
@@ -949,13 +758,13 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
     String timePartColsStr = p.getTable().getTTable().getParameters().get(MetastoreConstants.TIME_PART_COLUMNS);
     String upParam = p.getParameters().get(MetastoreConstants.PARTITION_UPDATE_PERIOD);
     UpdatePeriod period = UpdatePeriod.valueOf(upParam);
-    Map<String, String> partSpec = new HashMap<String, String>();
+    Map<String, String> partSpec = new HashMap<>();
     partSpec.putAll(p.getSpec());
     if (timePartColsStr != null) {
       String[] timePartCols = StringUtils.split(timePartColsStr, ',');
       for (String partCol : timePartCols) {
         String dateStr = partSpec.get(partCol);
-        Date date = null;
+        Date date;
         try {
           date = period.parse(dateStr);
         } catch (Exception e) {
@@ -973,8 +782,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
 
   public void dropPartitionFromStorageByValues(LensSessionHandle sessionid,
     String cubeTableName, String storageName, String values) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       String tableName = MetastoreUtil.getStorageTableName(cubeTableName,
         Storage.getPrefix(storageName));
       CubeMetastoreClient msClient = getClient(sessionid);
@@ -988,8 +796,8 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         log.error("No partition exists with specified values, correspoding filter:" + filter);
         throw new NotFoundException("No partition exists with specified values");
       }
-      Map<String, Date> timeSpec = new HashMap<String, Date>();
-      Map<String, String> nonTimeSpec = new HashMap<String, String>();
+      Map<String, Date> timeSpec = new HashMap<>();
+      Map<String, String> nonTimeSpec = new HashMap<>();
       UpdatePeriod updatePeriod = populatePartSpec(partitions.get(0), timeSpec, nonTimeSpec);
       msClient.dropPartition(cubeTableName,
         storageName, timeSpec, nonTimeSpec, updatePeriod);
@@ -997,28 +805,23 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         + " storage: " + storageName + " values:" + values);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   public void dropPartitionFromStorageByFilter(LensSessionHandle sessionid, String cubeTableName,
     String storageName, String filter) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       String tableName = MetastoreUtil.getStorageTableName(cubeTableName, Storage.getPrefix(storageName));
       CubeMetastoreClient msClient = getClient(sessionid);
       List<Partition> partitions = msClient.getPartitionsByFilter(tableName, filter);
       for (Partition part : partitions) {
         try {
-          Map<String, Date> timeSpec = new HashMap<String, Date>();
-          Map<String, String> nonTimeSpec = new HashMap<String, String>();
+          Map<String, Date> timeSpec = new HashMap<>();
+          Map<String, String> nonTimeSpec = new HashMap<>();
           UpdatePeriod updatePeriod = populatePartSpec(part, timeSpec, nonTimeSpec);
           msClient.dropPartition(cubeTableName, storageName, timeSpec, nonTimeSpec, updatePeriod);
         } catch (HiveException e) {
-          if (e.getCause() instanceof NoSuchObjectException) {
-            continue;
-          } else {
+          if (!(e.getCause() instanceof NoSuchObjectException)) {
             throw new LensException(e);
           }
         }
@@ -1027,22 +830,15 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         + " storage: " + storageName + " by filter:" + filter);
     } catch (HiveException exc) {
       throw new LensException(exc);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void createStorage(LensSessionHandle sessionid, XStorage storage)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).createStorage(JAXBUtils.storageFromXStorage(storage));
       log.info("Created storage " + storage.getName());
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
 
   }
@@ -1050,62 +846,44 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public void dropStorage(LensSessionHandle sessionid, String storageName)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).dropStorage(storageName);
       log.info("Dropped storage " + storageName);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void alterStorage(LensSessionHandle sessionid, String storageName,
     XStorage storage) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).alterStorage(storageName,
         JAXBUtils.storageFromXStorage(storage));
       log.info("Altered storage " + storageName);
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public XStorage getStorage(LensSessionHandle sessionid, String storageName)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       return JAXBUtils.xstorageFromStorage(getClient(sessionid).getStorage(storageName));
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public List<String> getAllStorageNames(LensSessionHandle sessionid)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Collection<Storage> storages = getClient(sessionid).getAllStorages();
       if (storages != null && !storages.isEmpty()) {
-        List<String> names = new ArrayList<String>(storages.size());
+        List<String> names = new ArrayList<>(storages.size());
         for (Storage storage : storages) {
           names.add(storage.getName());
         }
         return names;
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -1113,11 +891,10 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public List<String> getAllBaseCubeNames(LensSessionHandle sessionid)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Collection<CubeInterface> cubes = getClient(sessionid).getAllCubes();
       if (cubes != null && !cubes.isEmpty()) {
-        List<String> names = new ArrayList<String>(cubes.size());
+        List<String> names = new ArrayList<>(cubes.size());
         for (CubeInterface cube : cubes) {
           if (!cube.isDerivedCube()) {
             names.add(cube.getName());
@@ -1125,10 +902,6 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         }
         return names;
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -1136,11 +909,10 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public List<String> getAllDerivedCubeNames(LensSessionHandle sessionid)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Collection<CubeInterface> cubes = getClient(sessionid).getAllCubes();
       if (cubes != null && !cubes.isEmpty()) {
-        List<String> names = new ArrayList<String>(cubes.size());
+        List<String> names = new ArrayList<>(cubes.size());
         for (CubeInterface cube : cubes) {
           if (cube.isDerivedCube()) {
             names.add(cube.getName());
@@ -1148,10 +920,6 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         }
         return names;
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -1159,11 +927,10 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public List<String> getAllQueryableCubeNames(LensSessionHandle sessionid)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Collection<CubeInterface> cubes = getClient(sessionid).getAllCubes();
       if (cubes != null && !cubes.isEmpty()) {
-        List<String> names = new ArrayList<String>(cubes.size());
+        List<String> names = new ArrayList<>(cubes.size());
         for (CubeInterface cube : cubes) {
           if (cube.allFieldsQueriable()) {
             names.add(cube.getName());
@@ -1171,10 +938,6 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         }
         return names;
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -1182,76 +945,53 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public void createDimension(LensSessionHandle sessionid, XDimension dimension)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).createDimension(JAXBUtils.dimensionFromXDimension(dimension));
       log.info("Created dimension " + dimension.getName());
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public XDimension getDimension(LensSessionHandle sessionid, String dimName)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       return JAXBUtils.xdimensionFromDimension(getClient(sessionid).getDimension(dimName));
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void dropDimension(LensSessionHandle sessionid, String dimName)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).dropDimension(dimName);
       log.info("Dropped dimension " + dimName);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public void updateDimension(LensSessionHandle sessionid, String dimName, XDimension dimension)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       getClient(sessionid).alterDimension(dimName,
         JAXBUtils.dimensionFromXDimension(dimension));
       log.info("Altered dimension " + dimName);
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public List<String> getAllDimensionNames(LensSessionHandle sessionid)
     throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       Collection<Dimension> dimensions = getClient(sessionid).getAllDimensions();
       if (dimensions != null && !dimensions.isEmpty()) {
-        List<String> names = new ArrayList<String>(dimensions.size());
+        List<String> names = new ArrayList<>(dimensions.size());
         for (Dimension dim : dimensions) {
           names.add(dim.getName());
         }
         return names;
       }
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
     return null;
   }
@@ -1259,23 +999,14 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public XNativeTable getNativeTable(LensSessionHandle sessionid, String name)
     throws LensException {
-    try {
-      acquire(sessionid);
-      Table tbl = getClient(sessionid).getHiveTable(name);
-      if (tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY) != null) {
-        throw new BadRequestException(name + " is not a native table");
-      }
-      return JAXBUtils.nativeTableFromMetaTable(tbl);
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
+      return JAXBUtils.nativeTableFromMetaTable(getClient(sessionid).getTableWithTypeFailFast(name, null));
     }
   }
 
   private List<String> getNativeTablesFromDB(LensSessionHandle sessionid, String dbName, boolean prependDbName)
     throws LensException {
-    IMetaStoreClient msc = null;
+    IMetaStoreClient msc;
     try {
       msc = getSession(sessionid).getMetaStoreClient();
       List<String> tables = msc.getAllTables(
@@ -1284,13 +1015,11 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       if (!conf.getBoolean(LensConfConstants.EXCLUDE_CUBE_TABLES, LensConfConstants.DEFAULT_EXCLUDE_CUBE_TABLES)) {
         return tables;
       }
-      List<String> result = new ArrayList<String>();
+      List<String> result = new ArrayList<>();
       if (tables != null && !tables.isEmpty()) {
         List<org.apache.hadoop.hive.metastore.api.Table> tblObjects =
           msc.getTableObjectsByName(dbName, tables);
-        Iterator<org.apache.hadoop.hive.metastore.api.Table> it = tblObjects.iterator();
-        while (it.hasNext()) {
-          org.apache.hadoop.hive.metastore.api.Table tbl = it.next();
+        for (Table tbl : tblObjects) {
           if (tbl.getParameters().get(MetastoreConstants.TABLE_TYPE_KEY) == null) {
             if (prependDbName) {
               result.add(dbName + "." + tbl.getTableName());
@@ -1309,8 +1038,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public List<String> getAllNativeTableNames(LensSessionHandle sessionid,
     String dbOption, String dbName) throws LensException {
-    try {
-      acquire(sessionid);
+    try (SessionContext ignored = new SessionContext(sessionid)){
       if (!StringUtils.isBlank(dbName)) {
         if (!Hive.get(getSession(sessionid).getHiveConf()).databaseExists(dbName)) {
           throw new NotFoundException("Database " + dbName + " does not exist");
@@ -1335,8 +1063,6 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
       return tables;
     } catch (HiveException e) {
       throw new LensException(e);
-    } finally {
-      release(sessionid);
     }
   }
 
@@ -1406,7 +1132,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   }
 
   private void addAllChainedColsToFlattenedListFromCube(CubeMetastoreClient client, ObjectFactory objectFactory,
-    CubeInterface cube, List<XFlattenedColumn> columnList) throws HiveException {
+    CubeInterface cube, List<XFlattenedColumn> columnList) throws HiveException, LensException {
     if (cube instanceof DerivedCube) {
       return;
     }
@@ -1414,7 +1140,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   }
 
   private void addAllChainedColsToFlattenedList(CubeMetastoreClient client, ObjectFactory objectFactory,
-    AbstractBaseTable baseTbl, List<XFlattenedColumn> columnList) throws HiveException {
+    AbstractBaseTable baseTbl, List<XFlattenedColumn> columnList) throws HiveException, LensException {
     for (JoinChain chain : baseTbl.getJoinChains()) {
       Dimension dim = client.getDimension(chain.getDestTable());
       addAllDirectAttributesToFlattenedListFromDimension(objectFactory, dim, columnList, chain.getName());
@@ -1425,8 +1151,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
   @Override
   public XFlattenedColumns getFlattenedColumns(LensSessionHandle sessionHandle, String tableName, boolean addChains)
     throws LensException {
-    try {
-      acquire(sessionHandle);
+    try (SessionContext ignored = new SessionContext(sessionHandle)){
       CubeMetastoreClient client = getClient(sessionHandle);
 
       ObjectFactory objectFactory = new ObjectFactory();
@@ -1453,50 +1178,41 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
           + tableName + "' is neither a cube nor a dimension");
       }
       return flattenedColumns;
-    } catch (LensException exc) {
-      throw exc;
     } catch (HiveException e) {
       throw new LensException("Error getting flattened view for " + tableName, e);
-    } finally {
-      release(sessionHandle);
     }
   }
 
   @Override
   public Date getLatestDateOfCube(LensSessionHandle sessionid, String cubeName, String timeDimension)
     throws LensException, HiveException {
-    acquire(sessionid);
-    // get the partitionColumn corresponding to timeDimension passed
-    CubeMetastoreClient msClient = getClient(sessionid);
-    CubeInterface ci = msClient.getCube(cubeName);
-    if (!(ci instanceof Cube)) {
-      throw new BadRequestException("cubeName : " + cubeName + " is not a base cube.");
+    try(SessionContext ignored = new SessionContext(sessionid)) {
+      // get the partitionColumn corresponding to timeDimension passed
+      CubeMetastoreClient msClient = getClient(sessionid);
+      CubeInterface ci = msClient.getCube(cubeName);
+      if (!(ci instanceof Cube)) {
+        throw new BadRequestException("cubeName : " + cubeName + " is not a base cube.");
+      }
+      Cube c = (Cube) ci;
+      return msClient.getLatestDateOfCube(c, timeDimension);
     }
-    Cube c = (Cube) ci;
-    Date latest = msClient.getLatestDateOfCube(c, timeDimension);
-    release(sessionid);
-    return latest;
   }
 
   public List<String> getPartitionTimelines(LensSessionHandle sessionid, String factName, String storage,
     String updatePeriod, String timeDimension) throws LensException, HiveException {
-    acquire(sessionid);
-    try {
+    try (SessionContext ignored = new SessionContext(sessionid)) {
       CubeMetastoreClient client = getClient(sessionid);
       List<String> ret = Lists.newArrayList();
       for (PartitionTimeline timeline : client.getTimelines(factName, storage, updatePeriod, timeDimension)) {
         ret.add(timeline.toString());
       }
       return ret;
-    } finally {
-      release(sessionid);
     }
   }
 
   @Override
   public XJoinChains getAllJoinChains(LensSessionHandle sessionHandle, String tableName) throws LensException {
-    try {
-      acquire(sessionHandle);
+    try (SessionContext ignored = new SessionContext(sessionHandle)){
       CubeMetastoreClient client = getClient(sessionHandle);
       Set<JoinChain> chains;
       if (client.isCube(tableName)) {
@@ -1515,10 +1231,6 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
         }
       }
       return xJoinChains;
-    } catch (HiveException e) {
-      throw new LensException(e);
-    } finally {
-      release(sessionHandle);
     }
   }
 
@@ -1547,8 +1259,7 @@ public class CubeMetastoreServiceImpl extends BaseLensService implements CubeMet
     }
 
     return isHealthy
-      ? new HealthStatus(isHealthy, "Cube metastore service is healthy.")
-      : new HealthStatus(isHealthy, details.toString());
+      ? new HealthStatus(true, "Cube metastore service is healthy.")
+      : new HealthStatus(false, details.toString());
   }
-
 }