From 52984a114b26159934de9e5c022b720270c47525 Mon Sep 17 00:00:00 2001 From: Alexander Kolbasov Date: Wed, 25 Oct 2017 17:12:42 -0700 Subject: [PATCH 1/1] HIVE-17730 Queries can be closed automatically --- .../apache/hadoop/hive/metastore/ObjectStore.java | 623 ++++++++++++--------- 1 file changed, 350 insertions(+), 273 deletions(-) diff --git a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java index 3deef0ee96..b694ff6648 100644 --- a/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java +++ b/standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java @@ -192,14 +192,8 @@ import org.slf4j.LoggerFactory; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; -import com.google.common.collect.SortedSetMultimap; -import com.google.common.collect.TreeMultimap; /** @@ -270,15 +264,103 @@ private Counter directSqlErrors; /** - * A Autocloseable wrapper around Query class to pass the Query object to the caller and let the caller release - * the resources when the QueryWrapper goes out of scope + * AutoCloseable query wrapper which doesn't throw exceptions on close. + * In current JDO implementation Query itself is AutoCloseable, + * but its close() method throws exceptions. To avoid propafating these up the call stack, + * this wrapper provides close() method that doesn't throw exceptions.

+ * + * QueryWrapper simulates regular query operations. New ones may be added as needed. + * The typical access pattern is

+ * + *

+   *   try (QueryWrapper query = new QueryWrapper(pm.newQuery(Foo.class))) {
+   *     return query.execute();
+   *   }
+   * 
+ *

+ * Note that calling explicit close() is never useful because it can be called + * on the query itself instead. */ public static class QueryWrapper implements AutoCloseable { - public Query query; + private Query query; + + public QueryWrapper() { + } /** - * Explicitly closes the query object to release the resources + * Initialize from existing query. + * @param query open query. */ + QueryWrapper(Query query) { + this.query = query; + } + + // Wrappers around regular Query operations + + long deletePersistentAll(Object... parameters) { + return query.deletePersistentAll(parameters); + } + + void declareParameters(String parameters) { + query.declareParameters(parameters); + } + + void declareImports(String imports) { + query.declareImports(imports); + } + + Object execute() { + return query.execute(); + } + + Object execute(Object p1) { + return query.execute(p1); + } + + Object execute(Object p1, Object p2) { + return query.execute(p1, p2); + } + + Object execute(Object p1, Object p2, Object p3) { + return query.execute(p1, p2, p3); + } + + Object executeWithMap (Map parameters) { + return query.executeWithMap(parameters); + } + + Object executeWithArray (Object... parameters) { + return query.executeWithArray(parameters); + } + + void setFilter(String filter) { + query.setFilter(filter); + } + + void setOrdering(String ordering) { + query.setOrdering(ordering); + } + + void setRange(long fromIncl, long toExcl) { + query.setRange(fromIncl, toExcl); + } + + void setResult(String data) { + query.setResult(data); + } + + void setUnique(boolean unique) { + query.setUnique(unique); + } + + void setResultClass(Class cls) { + query.setResultClass(cls); + } + + void setClass(Class cls) { + query.setClass(cls); + } + @Override public void close() { if (query != null) { @@ -819,6 +901,28 @@ private MDatabase getMDatabase(String name) throws NoSuchObjectException { return mdb; } + /** + * Get MDatabase object for the given database name. + * Should be executed inside a transaction. + * + * @param name normalized Database name. + * @return MDatabase object. + * @throws NoSuchObjectException if there is no database with the given name + */ + private MDatabase getMDatabaseTx(String name) throws NoSuchObjectException { + name = normalizeIdentifier(name); + try (QueryWrapper query = new QueryWrapper(pm.newQuery(MDatabase.class, "name == dbname"))) { + query.declareParameters("java.lang.String dbname"); + query.setUnique(true); + MDatabase mdb = (MDatabase) query.execute(name); + if (mdb == null) { + throw new NoSuchObjectException("There is no database named " + name); + } + pm.retrieve(mdb); + return mdb; + } + } + @Override public Database getDatabase(String name) throws NoSuchObjectException { MetaException ex = null; @@ -920,23 +1024,17 @@ public boolean dropDatabase(String dbname) throws NoSuchObjectException, MetaExc boolean success = false; LOG.info("Dropping database " + dbname + " along with all tables"); dbname = normalizeIdentifier(dbname); - QueryWrapper queryWrapper = new QueryWrapper(); try { openTransaction(); - - // then drop the database - MDatabase db = getMDatabase(dbname); - pm.retrieve(db); - if (db != null) { - List dbGrants = this.listDatabaseGrants(dbname, queryWrapper); - if (dbGrants != null && dbGrants.size() > 0) { - pm.deletePersistentAll(dbGrants); - } - pm.deletePersistent(db); - } + // Check that database exist. Will throw exception if it doesn't. + MDatabase db = getMDatabaseTx(dbname); + dropDatabaseGrantsTx(dbname); + pm.deletePersistent(db); success = commitTransaction(); } finally { - rollbackAndCleanup(success, queryWrapper); + if (!success) { + rollbackTransaction(); + } } return success; } @@ -2242,11 +2340,19 @@ private boolean dropPartitionCommon(MPartition part) throws NoSuchObjectExceptio @Override protected List getJdoResult( GetHelper> ctx) throws MetaException { - QueryWrapper queryWrapper = new QueryWrapper(); - try { - return convertToParts(listMPartitions(dbName, tblName, maxParts, queryWrapper)); + boolean success = false; + openTransaction(); + try (QueryWrapper query = listMPartitionsQueryTx(maxParts)) { + List mParts = (List) query.execute(normalizeIdentifier(tblName), + normalizeIdentifier(dbName)); + pm.retrieveAll(mParts); + List result = convertToParts(mParts); + success = commitTransaction(); + return result; } finally { - queryWrapper.close(); + if (!success) { + rollbackTransaction(); + } } } }.run(false); @@ -2257,31 +2363,34 @@ private boolean dropPartitionCommon(MPartition part) throws NoSuchObjectExceptio short max, String userName, List groupNames) throws MetaException, InvalidObjectException { boolean success = false; - QueryWrapper queryWrapper = new QueryWrapper(); + dbName = normalizeIdentifier(dbName); + tblName = normalizeIdentifier(tblName); - try { - openTransaction(); - List mparts = listMPartitions(dbName, tblName, max, queryWrapper); + openTransaction(); + try (QueryWrapper query = listMPartitionsQueryTx(max)) { + List mparts = (List) query.execute(normalizeIdentifier(tblName), + normalizeIdentifier(dbName)); + pm.retrieveAll(mparts); List parts = new ArrayList<>(mparts.size()); - if (mparts != null && mparts.size()>0) { - for (MPartition mpart : mparts) { - MTable mtbl = mpart.getTable(); - Partition part = convertToPart(mpart); - parts.add(part); - - if ("TRUE".equalsIgnoreCase(mtbl.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) { - String partName = Warehouse.makePartName(this.convertToFieldSchemas(mtbl - .getPartitionKeys()), part.getValues()); - PrincipalPrivilegeSet partAuth = this.getPartitionPrivilegeSet(dbName, - tblName, partName, userName, groupNames); - part.setPrivileges(partAuth); - } + for (MPartition mpart : mparts) { + MTable mtbl = mpart.getTable(); + Partition part = convertToPart(mpart); + parts.add(part); + + if ("TRUE".equalsIgnoreCase(mtbl.getParameters().get("PARTITION_LEVEL_PRIVILEGE"))) { + String partName = Warehouse.makePartName(this.convertToFieldSchemas(mtbl + .getPartitionKeys()), part.getValues()); + PrincipalPrivilegeSet partAuth = this.getPartitionPrivilegeSet(dbName, + tblName, partName, userName, groupNames); + part.setPrivileges(partAuth); } } success = commitTransaction(); return parts; } finally { - rollbackAndCleanup(success, queryWrapper); + if (!success) { + rollbackTransaction(); + } } } @@ -2749,32 +2858,20 @@ private Collection getPartitionPsQueryResults(String dbName, String tableName, return partitionNames; } - // TODO:pc implement max - private List listMPartitions(String dbName, String tableName, int max, QueryWrapper queryWrapper) { - boolean success = false; - List mparts = null; - try { - openTransaction(); - LOG.debug("Executing listMPartitions"); - dbName = normalizeIdentifier(dbName); - tableName = normalizeIdentifier(tableName); - Query query = queryWrapper.query = pm.newQuery(MPartition.class, "table.tableName == t1 && table.database.name == t2"); - query.declareParameters("java.lang.String t1, java.lang.String t2"); - query.setOrdering("partitionName ascending"); - if (max > 0) { - query.setRange(0, max); - } - mparts = (List) query.execute(tableName, dbName); - LOG.debug("Done executing query for listMPartitions"); - pm.retrieveAll(mparts); - success = commitTransaction(); - LOG.debug("Done retrieving all objects for listMPartitions " + mparts); - } finally { - if (!success) { - rollbackTransaction(); - } + /** + * Get a query for finding partitions matching given table and database. + * @param max maxinum number of results (if positive) + * @return Querry wrapper suitable for running a query on MPartitions + */ + private QueryWrapper listMPartitionsQueryTx(int max) { + QueryWrapper query = new QueryWrapper(pm.newQuery( + MPartition.class, "table.tableName == t1 && table.database.name == t2")); + query.declareParameters("java.lang.String t1, java.lang.String t2"); + query.setOrdering("partitionName ascending"); + if (max > 0) { + query.setRange(0, max); } - return mparts; + return query; } @Override @@ -4654,67 +4751,34 @@ private MRoleMap getMSecurityUserRoleMap(String userName, PrincipalType principa public boolean removeRole(String roleName) throws MetaException, NoSuchObjectException { boolean success = false; - QueryWrapper queryWrapper = new QueryWrapper(); try { openTransaction(); - MRole mRol = getMRole(roleName); - pm.retrieve(mRol); - if (mRol != null) { - // first remove all the membership, the membership that this role has - // been granted - List roleMap = listMRoleMembers(mRol.getRoleName()); - if (roleMap.size() > 0) { - pm.deletePersistentAll(roleMap); - } - List roleMember = listMSecurityPrincipalMembershipRole(mRol - .getRoleName(), PrincipalType.ROLE, queryWrapper); - if (roleMember.size() > 0) { - pm.deletePersistentAll(roleMember); - } - queryWrapper.close(); - // then remove all the grants - List userGrants = listPrincipalMGlobalGrants( - mRol.getRoleName(), PrincipalType.ROLE); - if (userGrants.size() > 0) { - pm.deletePersistentAll(userGrants); - } - List dbGrants = listPrincipalAllDBGrant(mRol - .getRoleName(), PrincipalType.ROLE, queryWrapper); - if (dbGrants.size() > 0) { - pm.deletePersistentAll(dbGrants); - } - queryWrapper.close(); - List tabPartGrants = listPrincipalAllTableGrants( - mRol.getRoleName(), PrincipalType.ROLE, queryWrapper); - if (tabPartGrants.size() > 0) { - pm.deletePersistentAll(tabPartGrants); - } - queryWrapper.close(); - List partGrants = listPrincipalAllPartitionGrants( - mRol.getRoleName(), PrincipalType.ROLE, queryWrapper); - if (partGrants.size() > 0) { - pm.deletePersistentAll(partGrants); - } - queryWrapper.close(); - List tblColumnGrants = listPrincipalAllTableColumnGrants( - mRol.getRoleName(), PrincipalType.ROLE, queryWrapper); - if (tblColumnGrants.size() > 0) { - pm.deletePersistentAll(tblColumnGrants); - } - queryWrapper.close(); - List partColumnGrants = listPrincipalAllPartitionColumnGrants( - mRol.getRoleName(), PrincipalType.ROLE, queryWrapper); - if (partColumnGrants.size() > 0) { - pm.deletePersistentAll(partColumnGrants); - } - queryWrapper.close(); - - // finally remove the role - pm.deletePersistent(mRol); + MRole mRol = getMRoleTx(roleName); + if (mRol == null) { + LOG.debug("Can not delete role {}: role not found", roleName); + success = commitTransaction(); + return success; } + // first remove all the membership, the membership that this role has + // been granted + deleteMRoleMembersTx(roleName); + deleteMSecurityPrincipalMembershipRoleAllTx(roleName, PrincipalType.ROLE); + + // then remove all the grants + deletePrincipalMGlobalGrantsTx(roleName, PrincipalType.ROLE); + deletePrincipalAllDBGrantTx(roleName, PrincipalType.ROLE); + deletePrincipalAllTableGrantsTx(roleName, PrincipalType.ROLE); + deletePrincipalAllPartitionGrantsTx(roleName, PrincipalType.ROLE); + deletePrincipalAllTableColumnGrantsTx(roleName, PrincipalType.ROLE); + deletePrincipalAllPartitionColumnGrantsTx(roleName, PrincipalType.ROLE); + + // finally remove the role + pm.deletePersistent(mRol); success = commitTransaction(); } finally { - rollbackAndCleanup(success, queryWrapper); + if (!success) { + rollbackTransaction(); + } } return success; } @@ -4839,27 +4903,19 @@ private void getAllRoleAncestors(Set processedRoleNames, List } @SuppressWarnings("unchecked") - private List listMSecurityPrincipalMembershipRole(final String roleName, - final PrincipalType principalType, - QueryWrapper queryWrapper) { - boolean success = false; - List mRoleMemebership = null; - try { - LOG.debug("Executing listMSecurityPrincipalMembershipRole"); - - openTransaction(); - Query query = queryWrapper.query = pm.newQuery(MRoleMap.class, "principalName == t1 && principalType == t2"); + private void deleteMSecurityPrincipalMembershipRoleAllTx(final String roleName, + final PrincipalType principalType) { + LOG.debug("Executing deleteMSecurityPrincipalMembershipRoleAllTx({}, {})", roleName, principalType); + try(QueryWrapper query = new QueryWrapper(pm.newQuery(MRoleMap.class, + "principalName == t1 && principalType == t2"))) { query.declareParameters("java.lang.String t1, java.lang.String t2"); - mRoleMemebership = (List) query.execute(roleName, principalType.toString()); - pm.retrieveAll(mRoleMemebership); - success = commitTransaction(); - LOG.debug("Done retrieving all objects for listMSecurityPrincipalMembershipRole"); - } finally { - if (!success) { - rollbackTransaction(); + List result = (List)query.execute(roleName, principalType.toString()); + if (!result.isEmpty()) { + pm.retrieveAll(result); + pm.deletePersistentAll(result); } + LOG.debug("Done deleting privileges"); } - return mRoleMemebership; } @Override @@ -4891,6 +4947,16 @@ private MRole getMRole(String roleName) { return mrole; } + private MRole getMRoleTx(String roleName) { + try (QueryWrapper query = new QueryWrapper(pm.newQuery(MRole.class, "roleName == t1"))) { + query.declareParameters("java.lang.String t1"); + query.setUnique(true); + MRole mrole = (MRole) query.execute(roleName); + pm.retrieve(mrole); + return mrole; + } + } + @Override public List listRoleNames() { boolean success = false; @@ -5739,6 +5805,28 @@ public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) return mRoleMemeberList; } + /** + * Delete all roles matching specified role name. + * Should be executed inside a transaction + * + * @param roleName Name of role to delete + */ + @SuppressWarnings("unchecked") + private void deleteMRoleMembersTx(String roleName) { + try (QueryWrapper query = + new QueryWrapper(pm.newQuery(MRoleMap.class, "role.roleName == t1"))) { + LOG.debug("Executing deleteRoleMembers({})", roleName); + query.declareParameters("java.lang.String t1"); + query.setUnique(false); + List mRoles = (List) query.execute(roleName); + if (!mRoles.isEmpty()) { + pm.retrieveAll(mRoles); + pm.deletePersistentAll(mRoles); + } + } + } + + @Override public List listRoleMembers(String roleName) { List roleMaps = listMRoleMembers(roleName); @@ -5790,6 +5878,22 @@ public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) return userNameDbPriv; } + @SuppressWarnings("unchecked") + public void deletePrincipalMGlobalGrantsTx(String principalName, + PrincipalType principalType) { + LOG.debug("Executing deletePrincipalMGlobalGrantsTx({}, {})", principalName, principalType); + try (QueryWrapper query = new QueryWrapper( + pm.newQuery(MGlobalPrivilege.class, "principalName == t1 && principalType == t2"))) { + query.declareParameters("java.lang.String t1, java.lang.String t2"); + List mPrivs = + (List) query.executeWithArray(principalName, principalType.toString()); + if (!mPrivs.isEmpty()) { + pm.retrieveAll(mPrivs); + pm.deletePersistentAll(mPrivs); + } + } + } + @Override public List listPrincipalGlobalGrants(String principalName, PrincipalType principalType) { @@ -5881,9 +5985,8 @@ public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) if (mDbs.isEmpty()) { return Collections.emptyList(); } - List result = new ArrayList<>(); - for (int i = 0; i < mDbs.size(); i++) { - MDBPrivilege sDB = mDbs.get(i); + List result = new ArrayList<>(mDbs.size()); + for (MDBPrivilege sDB : mDbs) { HiveObjectRef objectRef = new HiveObjectRef( HiveObjectType.DATABASE, dbName, null, null, null); HiveObjectPrivilege secObj = new HiveObjectPrivilege(objectRef, @@ -5899,26 +6002,51 @@ public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) @Override public List listPrincipalDBGrantsAll( String principalName, PrincipalType principalType) { - QueryWrapper queryWrapper = new QueryWrapper(); - try { - return convertDB(listPrincipalAllDBGrant(principalName, principalType, queryWrapper)); + LOG.debug("Executing listPrincipalAllDBGrant({}, {})", principalName, principalType); + boolean success = false; + List mSecurityDBList; + openTransaction(); + try (QueryWrapper query = new QueryWrapper(pm.newQuery(MDBPrivilege.class))) { + if (principalName != null && principalType != null) { + query.setFilter("principalName == t1 && principalType == t2"); + query.declareParameters("java.lang.String t1, java.lang.String t2"); + mSecurityDBList = + (List) query.execute(principalName, principalType.toString()); + } else { + mSecurityDBList = (List) query.execute(); + } + pm.retrieveAll(mSecurityDBList); + success = commitTransaction(); + return( convertDB(mSecurityDBList)); } finally { - queryWrapper.close(); + if (!success) { + rollbackTransaction(); + } } } @Override public List listDBGrantsAll(String dbName) { - QueryWrapper queryWrapper = new QueryWrapper(); - try { - return convertDB(listDatabaseGrants(dbName, queryWrapper)); - } finally { - queryWrapper.close(); + dbName = normalizeIdentifier(dbName); + LOG.debug("Executing listDBGrantsAll({}", dbName); + boolean success = false; + openTransaction(); + try (QueryWrapper query = new QueryWrapper(pm.newQuery( + MDBPrivilege.class, "database.name == t1"))){ + query.declareParameters("java.lang.String t1"); + List mSecurityDBList = (List) query.executeWithArray(dbName); + pm.retrieveAll(mSecurityDBList); + success = commitTransaction(); + return convertDB(mSecurityDBList); + } finally { + if (!success) { + rollbackTransaction(); } + } } private List convertDB(List privs) { - List result = new ArrayList<>(); + List result = new ArrayList<>(privs.size()); for (MDBPrivilege priv : privs) { String pname = priv.getPrincipalName(); PrincipalType ptype = PrincipalType.valueOf(priv.getPrincipalType()); @@ -5935,35 +6063,19 @@ public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) } @SuppressWarnings("unchecked") - private List listPrincipalAllDBGrant(String principalName, - PrincipalType principalType, - QueryWrapper queryWrapper) { - boolean success = false; - Query query = null; - List mSecurityDBList = null; - try { - LOG.debug("Executing listPrincipalAllDBGrant"); - - openTransaction(); - if (principalName != null && principalType != null) { - query = queryWrapper.query = pm.newQuery(MDBPrivilege.class, "principalName == t1 && principalType == t2"); - query.declareParameters("java.lang.String t1, java.lang.String t2"); - mSecurityDBList = - (List) query.execute(principalName, principalType.toString()); - } else { - query = queryWrapper.query = pm.newQuery(MDBPrivilege.class); - mSecurityDBList = (List) query.execute(); - } - pm.retrieveAll(mSecurityDBList); - success = commitTransaction(); - - LOG.debug("Done retrieving all objects for listPrincipalAllDBGrant"); - } finally { - if (!success) { - rollbackTransaction(); + private void deletePrincipalAllDBGrantTx(String principalName, + PrincipalType principalType) { + try (QueryWrapper query = new QueryWrapper(pm.newQuery(MDBPrivilege.class))){ + LOG.debug("Executing deletePrincipalAllDBGrantTx({}, {})", principalName, principalType); + query.setFilter("principalName == t1 && principalType == t2"); + query.declareParameters("java.lang.String t1, java.lang.String t2"); + List mSecurityDBList = + (List) query.execute(principalName, principalType.toString()); + if (!mSecurityDBList.isEmpty()) { + pm.retrieveAll(mSecurityDBList); + pm.deletePersistentAll(mSecurityDBList); } } - return mSecurityDBList; } @SuppressWarnings("unchecked") @@ -6114,24 +6226,21 @@ public void dropPartitionAllColumnGrantsNoTxn( queryWithParams.getFirst().deletePersistentAll(queryWithParams.getSecond()); } + /** + * Drop all database grants. + * Should be always called inside a transaction. + * @param dbName normalized database name. + */ @SuppressWarnings("unchecked") - private List listDatabaseGrants(String dbName, QueryWrapper queryWrapper) { - dbName = normalizeIdentifier(dbName); - boolean success = false; - try { - LOG.debug("Executing listDatabaseGrants"); - - openTransaction(); - Query query = queryWrapper.query = pm.newQuery(MDBPrivilege.class, "database.name == t1"); + private void dropDatabaseGrantsTx(String dbName) { + LOG.debug("Executing listDatabaseGrants({})", dbName); + try (QueryWrapper query = new QueryWrapper(pm.newQuery( + MDBPrivilege.class, "database.name == t1"))) { query.declareParameters("java.lang.String t1"); List mSecurityDBList = (List) query.executeWithArray(dbName); - pm.retrieveAll(mSecurityDBList); - success = commitTransaction(); - LOG.debug("Done retrieving all objects for listDatabaseGrants"); - return mSecurityDBList; - } finally { - if (!success) { - rollbackTransaction(); + if (!mSecurityDBList.isEmpty()) { + pm.retrieveAll(mSecurityDBList); + pm.deletePersistentAll(mSecurityDBList); } } } @@ -6526,29 +6635,22 @@ private void dropPartitionGrantsNoTxn(String dbName, String tableName, List listPrincipalAllTableGrants( - String principalName, PrincipalType principalType, QueryWrapper queryWrapper) { - boolean success = false; - List mSecurityTabPartList = null; - try { - LOG.debug("Executing listPrincipalAllTableGrants"); + private void deletePrincipalAllTableGrantsTx( + String principalName, PrincipalType principalType) { + LOG.debug("Executing deletePrincipalAllTableGrantsTx({}, {})", principalName, principalType); - openTransaction(); - Query query = queryWrapper.query = pm.newQuery(MTablePrivilege.class, - "principalName == t1 && principalType == t2"); + try (QueryWrapper query = new QueryWrapper(pm.newQuery(MTablePrivilege.class, + "principalName == t1 && principalType == t2"))) { query.declareParameters("java.lang.String t1, java.lang.String t2"); - mSecurityTabPartList = (List) query.execute( - principalName, principalType.toString()); - pm.retrieveAll(mSecurityTabPartList); - success = commitTransaction(); - - LOG.debug("Done retrieving all objects for listPrincipalAllTableGrants"); - } finally { - if (!success) { - rollbackTransaction(); + List result = + (List) query.execute(principalName, principalType.toString()); + if (!result.isEmpty()) { + pm.retrieveAll(result); + pm.deletePersistentAll(result); } + + LOG.debug("Done deleting all objects for deletePrincipalAllTableGrantsTx"); } - return mSecurityTabPartList; } @Override @@ -6625,26 +6727,20 @@ private void dropPartitionGrantsNoTxn(String dbName, String tableName, List listPrincipalAllPartitionGrants(String principalName, - PrincipalType principalType, QueryWrapper queryWrapper) { - boolean success = false; - List mSecurityTabPartList = null; - try { - openTransaction(); - LOG.debug("Executing listPrincipalAllPartitionGrants"); - Query query = queryWrapper.query = pm.newQuery(MPartitionPrivilege.class, "principalName == t1 && principalType == t2"); + private void deletePrincipalAllPartitionGrantsTx(String principalName, + PrincipalType principalType) { + try (QueryWrapper query = new QueryWrapper( + pm.newQuery(MPartitionPrivilege.class, "principalName == t1 && principalType == t2"))) { + LOG.debug("Executing deletePrincipalAllPartitionGrantsTx({}, {})", + principalName, principalType); query.declareParameters("java.lang.String t1, java.lang.String t2"); - mSecurityTabPartList = + List mSecurityTabPartList = (List) query.execute(principalName, principalType.toString()); - pm.retrieveAll(mSecurityTabPartList); - success = commitTransaction(); - LOG.debug("Done retrieving all objects for listPrincipalAllPartitionGrants"); - } finally { - if (!success) { - rollbackTransaction(); + if (!mSecurityTabPartList.isEmpty()) { + pm.retrieveAll(mSecurityTabPartList); + pm.deletePersistentAll(mSecurityTabPartList); } } - return mSecurityTabPartList; } @Override @@ -6724,30 +6820,20 @@ private void dropPartitionGrantsNoTxn(String dbName, String tableName, List listPrincipalAllTableColumnGrants(String principalName, - PrincipalType principalType, QueryWrapper queryWrapper) { - boolean success = false; - - List mSecurityColumnList = null; - try { - LOG.debug("Executing listPrincipalAllTableColumnGrants"); - - openTransaction(); - Query query = queryWrapper.query = - pm.newQuery(MTableColumnPrivilege.class, "principalName == t1 && principalType == t2"); + private void deletePrincipalAllTableColumnGrantsTx(String principalName, + PrincipalType principalType) { + try (QueryWrapper query = new QueryWrapper( + pm.newQuery(MTableColumnPrivilege.class, "principalName == t1 && principalType == t2"))) { + LOG.debug("Executing deletePrincipalAllTableColumnGrantsTx({}, {})", + principalName, principalType); query.declareParameters("java.lang.String t1, java.lang.String t2"); - mSecurityColumnList = + List mSecurityColumnList = (List) query.execute(principalName, principalType.toString()); - pm.retrieveAll(mSecurityColumnList); - success = commitTransaction(); - - LOG.debug("Done retrieving all objects for listPrincipalAllTableColumnGrants"); - } finally { - if (!success) { - rollbackTransaction(); + if (!mSecurityColumnList.isEmpty()) { + pm.retrieveAll(mSecurityColumnList); + pm.deletePersistentAll(mSecurityColumnList); } } - return mSecurityColumnList; } @Override @@ -6828,29 +6914,20 @@ private void dropPartitionGrantsNoTxn(String dbName, String tableName, List listPrincipalAllPartitionColumnGrants( - String principalName, PrincipalType principalType, QueryWrapper queryWrapper) { - boolean success = false; - List mSecurityColumnList = null; - try { - LOG.debug("Executing listPrincipalAllTableColumnGrants"); - - openTransaction(); - Query query = queryWrapper.query = - pm.newQuery(MPartitionColumnPrivilege.class, "principalName == t1 && principalType == t2"); + private void deletePrincipalAllPartitionColumnGrantsTx(String principalName, + PrincipalType principalType) { + try (QueryWrapper query = new QueryWrapper(pm.newQuery( + MPartitionColumnPrivilege.class, "principalName == t1 && principalType == t2"))) { + LOG.debug("Executing deletePrincipalAllTableColumnGrantsTx({}, {})", + principalName, principalType); query.declareParameters("java.lang.String t1, java.lang.String t2"); - mSecurityColumnList = + List mSecurityColumnList = (List) query.execute(principalName, principalType.toString()); - pm.retrieveAll(mSecurityColumnList); - success = commitTransaction(); - - LOG.debug("Done retrieving all objects for listPrincipalAllTableColumnGrants"); - } finally { - if (!success) { - rollbackTransaction(); + if (!mSecurityColumnList.isEmpty()) { + pm.retrieveAll(mSecurityColumnList); + pm.deletePersistentAll(mSecurityColumnList); } } - return mSecurityColumnList; } @Override -- 2.14.3