shubhluck opened a new pull request, #6991:
URL: https://github.com/apache/hive/pull/6991

   ### What changes were proposed in this pull request?
   
   Fix a silent managed-directory leak on `DROP DATABASE … CASCADE` that 
triggers whenever a database holds more tables than 
`hive.metastore.batch.retrieve.max` (default 300). After the leak the managed 
warehouse path survives the drop, and a subsequent `CREATE DATABASE` at the 
same name fails with `MetaException: Unable to create database managed 
directory`.
   
   Two small guards:
   
   1. 
`standalone-metastore/metastore-client/.../HookEnabledMetaStoreClient.java` — 
in `dropDatabaseCascadePerTable`, gate the `req.setDeleteManagedDir(false)` 
mutation on `req.isSoftDelete()` instead of `req.isSetTxnId()`. The 
`deleteManagedDir=false` semantic was added for the async ACID cleaner under 
soft-delete / lockless reads; it should not apply to plain cascade drops.
   2. `ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java` — only call 
`req.setTxnId(txnId)` when `txnId > 0`. Belt-and-suspenders: prevents thrift's 
isset bit from being flipped for a zero txnId and keeps the batch path safe for 
any future caller.
   
   ### Why are the changes needed?
   
   #### Reproduction
   
   1. Create a database and 1000 managed tables in it.
   2. `DROP DATABASE test_db CASCADE;` — completes without error.
   3. `hadoop fs -ls -d /warehouse/tablespace/managed/hive/test_db.db` — 
directory still exists, empty (table subdirs are gone).
   4. `CREATE DATABASE test_db;` — fails:
      ```
      FAILED: Execution Error, return code 40000 from 
org.apache.hadoop.hive.ql.ddl.DDLTask.
      MetaException(message:Unable to create database managed directory
        hdfs://.../warehouse/tablespace/managed/hive/test_db.db,
        failed to create database test_db)
      ```
   
   The bug reproduces with `hive.acid.lockless.reads.enabled=false` and no open 
ACID transaction.
   
   #### Root cause
   
   Two independent bugs interact.
   
   (a) `Hive.dropDatabase(DropDatabaseDesc)` unconditionally calls 
`req.setTxnId(txnId)` with `txnId==0` when there is no open transaction. 
Thrift's generated setter flips the isset bit regardless of value, so 
`req.isSetTxnId()` returns `true` on every drop.
   
   ```java
   long txnId = Optional.ofNullable(SessionState.get())
     .map(SessionState::getTxnMgr)
     .map(HiveTxnManager::getCurrentTxnId).orElse(0L);
   ...
   req.setSoftDelete(isSoftDelete);
   req.setTxnId(txnId);            // txnId==0 still flips the isset bit
   ```
   
   (b) `HookEnabledMetaStoreClient.dropDatabaseCascadePerTable` — taken when 
`tableCount > hive.metastore.batch.retrieve.max` — mutates the shared 
`DropDatabaseRequest` on `req.isSetTxnId()`:
   
   ```java
   if (req.isSetTxnId()) {
     table.setTxnId(req.getTxnId());
     req.setDeleteManagedDir(false);     // clobbers shared req
   }
   ```
   
   The final `delegate.dropDatabase(req)` then reaches 
`HMSHandler.drop_database_core` with `deleteManagedDir=false`, which skips the 
managed-dir cleanup:
   
   ```java
   if (req.isDeleteManagedDir()) {
     ... wh.deleteDir(path, true, dbFinal) ...
   }
   ```
   
   Databases at or below the batch threshold go through 
`dropDatabaseCascadePerDb`, which does not mutate the request, so small 
databases are unaffected.
   
   #### Scope and relation to other tickets
   
   - 4.0.0-alpha1 and earlier: NOT affected (no `deleteManagedDir` field on 
`DropDatabaseRequest`).
   - 4.0.1 and newer, including current master: affected.
   - HIVE-29528 and HIVE-29587 are related but do NOT fix this. Both are gated 
on soft-delete / lockless-reads semantics and do not touch the per-table 
cascade path in the client. The code path fixed here still carries the mutation 
on `master` after the HIVE-27473 client refactor.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No behavior change for soft-delete / lockless-reads cascade drops — those 
still reach `HMSHandler` with `deleteManagedDir=false` because 
`req.isSoftDelete()` is `true` on that path, preserving the existing 
async-cleaner contract.
   
   For plain cascade drops (the regressed path) the user-visible change is 
restorative: `DROP DATABASE … CASCADE` once again removes the managed warehouse 
directory, and a follow-up `CREATE DATABASE` at the same name no longer fails 
with `Unable to create database managed directory`.
   
   No thrift / API change.
   
   ### How was this patch tested?
   
   Added `testDropDatabaseCascadeDeletesManagedDirForLargeDb` in 
`TestHiveMetaStore` (standalone-metastore/metastore-server). It runs under both 
concrete subclasses:
   
   - `TestEmbeddedHiveMetaStore` — direct in-process `IMetaStoreClient`.
   - `TestRemoteHiveMetaStore` — over the HMS thrift server.
   
   The test uses the existing `BATCH_RETRIEVE_MAX=2` already set in the base 
class `setUp()`, creates 3 managed tables, builds a `DropDatabaseRequest` 
mirroring what `Hive.dropDatabase` sends (`txnId=0`, `softDelete=false`, 
`cascade=true`), and asserts:
   
   - The external and managed DB directories exist before the drop.
   - The DB metadata is gone after the drop.
   - Both directories (external **and** managed) are removed after the drop.
   - A follow-up `CREATE DATABASE` at the same locations succeeds.
   
   Without the patch the test fails on `"Managed db dir not deleted after 
CASCADE drop"` (and would also fail on the re-create assertion). With the patch 
it passes under both direct and thrift clients.
   
   Run locally with:
   
   ```
   mvn -pl standalone-metastore/metastore-server -am test \
     
-Dtest=TestEmbeddedHiveMetaStore#testDropDatabaseCascadeDeletesManagedDirForLargeDb
   mvn -pl standalone-metastore/metastore-server -am test \
     
-Dtest=TestRemoteHiveMetaStore#testDropDatabaseCascadeDeletesManagedDirForLargeDb
   ```
   
   JIRA: https://issues.apache.org/jira/browse/HIVE-30284


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to