[ 
https://issues.apache.org/jira/browse/HIVE-30284?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Shubham Sharma updated HIVE-30284:
----------------------------------
    Description: 
h2. Problem

On any Hive release that carries the soft-delete / deleteManagedDir thrift 
fields on DropDatabaseRequest (4.0.1 onwards, including current master), "DROP 
DATABASE <db> CASCADE" leaks the managed warehouse directory whenever the 
database has more tables than hive.metastore.batch.retrieve.max (default 300). 
A subsequent CREATE DATABASE of the same name then fails with:
{noformat}
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/<db>.db,
failed to create database <db>)
{noformat}
The bug reproduces with hive.acid.lockless.reads.enabled=false and with no open 
ACID transaction.
h2. Reproduce
 # Create a database and 1000 managed tables in it.
 # Run {{DROP DATABASE test_db CASCADE;}} – completes without error.
 # {{hadoop fs -ls -d /warehouse/tablespace/managed/hive/test_db.db}} –
  directory still exists, empty (table subdirectories were removed).
 # Run {{CREATE DATABASE test_db;}} – fails as above.

h2. Root cause

Two independent bugs interact:

(1) Hive.dropDatabase(DropDatabaseDesc) unconditionally calls 
{{req.setTxnId(txnId)}} with {{txnId==0}} when there is no open ACID 
transaction. Thrift's generated setter flips the isset bit regardless of value, 
so {{req.isSetTxnId()}} returns true for every drop.
{code:java}
// ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.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 isset bit
{code}
(2) HookEnabledMetaStoreClient.dropDatabaseCascadePerTable (taken when 
{{{}tableCount > hive.metastore.batch.retrieve.max{}}}) mutates the shared 
request to set {{deleteManagedDir=false}} purely on {{{}req.isSetTxnId(){}}}. 
The final {{delegate.dropDatabase(req)}} call then tells 
HMSHandler.drop_database_core to skip the managed-warehouse cleanup:
{code:java}
// standalone-metastore/metastore-client/.../HookEnabledMetaStoreClient.java
if (req.isSetTxnId()) {
  table.setTxnId(req.getTxnId());
  req.setDeleteManagedDir(false);     // <-- clobbers shared req
}
{code}
HMSHandler.drop_database_core honours the flag and skips the managed dir:
{code:java}
if (req.isDeleteManagedDir()) {
  ... wh.deleteDir(path, true, dbFinal) ...
}
{code}
The {{deleteManagedDir=false}} semantic was introduced for the lockless-reads / 
soft-delete path so the async ACID cleaner can remove table data later. 
Applying it on every drop with txnId set – including the degenerate 
{{txnId==0}} case – turns it into a leak. Reads at or below the batch threshold 
go through {{dropDatabaseCascadePerDb}} which does not mutate the request, 
which is why small databases are unaffected.
h2. Scope
 * 4.0.0-alpha1 and earlier: NOT affected (no deleteManagedDir field).
 * 4.0.1, 4.1.0, 4.2.0, 4.3.0: affected.
 * origin/master: still affected (verified – the hunk above is unchanged
  in HookEnabledMetaStoreClient after the HIVE-27473 client refactor;
  Hive.java still carries the unconditional setTxnId(0)).
 * HIVE-29528 and HIVE-29587 are related but do NOT fix this: both are
  gated on lockless-reads / soft-delete semantics and do not touch the
  per-table cascade path in the client.

  was:
h2. Problem

On any Hive release that carries the soft-delete / deleteManagedDir thrift
fields on DropDatabaseRequest (4.0.1 onwards, including current master),
"DROP DATABASE <db> CASCADE" leaks the managed warehouse directory
whenever the database has more tables than
hive.metastore.batch.retrieve.max (default 300). A subsequent CREATE
DATABASE of the same name then fails with:

{noformat}
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/<db>.db,
failed to create database <db>)
{noformat}

The bug reproduces with hive.acid.lockless.reads.enabled=false and with
no open ACID transaction.

h2. Reproduce

# Create a database and 1000 managed tables in it.
# Run \{{DROP DATABASE test_db CASCADE;}} -- completes without error.
# \{{hadoop fs -ls -d /warehouse/tablespace/managed/hive/test_db.db}} --
  directory still exists, empty (table subdirectories were removed).
# Run \{{CREATE DATABASE test_db;}} -- fails as above.

h2. Root cause

Two independent bugs interact:

(1) Hive.dropDatabase(DropDatabaseDesc) unconditionally calls
{\{req.setTxnId(txnId)}} with \{{txnId==0}} when there is no open ACID
transaction. Thrift's generated setter flips the isset bit regardless of
value, so \{{req.isSetTxnId()}} returns true for every drop.

{code:java}
// ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.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 isset bit
{code}

(2) HookEnabledMetaStoreClient.dropDatabaseCascadePerTable (taken when
{\{tableCount > hive.metastore.batch.retrieve.max}}) mutates the shared
request to set \{{deleteManagedDir=false}} purely on
{\{req.isSetTxnId()}}. The final \{{delegate.dropDatabase(req)}} call then
tells HMSHandler.drop_database_core to skip the managed-warehouse
cleanup:

{code:java}
// standalone-metastore/metastore-client/.../HookEnabledMetaStoreClient.java
if (req.isSetTxnId()) {
  table.setTxnId(req.getTxnId());
  req.setDeleteManagedDir(false);     // <-- clobbers shared req
}
{code}

HMSHandler.drop_database_core honours the flag and skips the managed dir:

{code:java}
if (req.isDeleteManagedDir()) {
  ... wh.deleteDir(path, true, dbFinal) ...
}
{code}

The \{{deleteManagedDir=false}} semantic was introduced for the
lockless-reads / soft-delete path so the async ACID cleaner can remove
table data later. Applying it on every drop with txnId set -- including
the degenerate \{{txnId==0}} case -- turns it into a leak.

Reads at or below the batch threshold go through
{\{dropDatabaseCascadePerDb}} which does not mutate the request, which is
why small databases are unaffected.

h2. Scope

* 4.0.0-alpha1 and earlier: NOT affected (no deleteManagedDir field).
* 4.0.1, 4.1.0, 4.2.0, 4.3.0: affected.
* origin/master: still affected (verified -- the hunk above is unchanged
  in HookEnabledMetaStoreClient after the HIVE-27473 client refactor;
  Hive.java still carries the unconditional setTxnId(0)).
* HIVE-29528 and HIVE-29587 are related but do NOT fix this: both are
  gated on lockless-reads / soft-delete semantics and do not touch the
  per-table cascade path in the client.

h2. Proposed fix

Minimal, two-line guard. Preserves the soft-delete semantics introduced
by HIVE-26882 and friends.

(1) standalone-metastore/metastore-client/.../HookEnabledMetaStoreClient.java
Gate the managed-dir mutation on \{{req.isSoftDelete()}} instead of
{\{req.isSetTxnId()}}:

{code:java}
if (req.isSetTxnId()) {
  table.setTxnId(req.getTxnId());
  if (req.isSoftDelete()) {
    req.setDeleteManagedDir(false);
  }
}
{code}

(2) ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java
Belt-and-suspenders -- do not mark txnId as set when there is no real
transaction:

{code:java}
if (txnId > 0) {
  req.setTxnId(txnId);
}
{code}

h2. Test

Added \{{testDropDatabaseCascadeDeletesManagedDirForLargeDb}} to
{\{TestHiveMetaStore}} (metastore-server/src/test). It runs under both
{\{TestEmbeddedHiveMetaStore}} and \{{TestRemoteHiveMetaStore}} (direct
client + over-thrift). Uses the existing \{{BATCH_RETRIEVE_MAX=2}} set in
the base class setUp(), creates 3 tables, and asserts that:

* the managed warehouse directory is removed after
  \{{DROP DATABASE CASCADE}},
* a subsequent CREATE DATABASE at the same location succeeds.

Without the patch the test fails on the "managed dir not deleted" or
"re-CREATE must succeed" assertion. With the patch it passes under both
direct and thrift clients.


> DROP DATABASE CASCADE leaks managed warehouse directory when table count 
> exceeds hive.metastore.batch.retrieve.max, blocking subsequent CREATE DATABASE
> -------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: HIVE-30284
>                 URL: https://issues.apache.org/jira/browse/HIVE-30284
>             Project: Hive
>          Issue Type: Bug
>          Components: Metastore, Standalone Metastore
>    Affects Versions: 4.1.0, 4.0.1, 4.2.0, 4.3.0
>         Environment: {{Apache Hive 4.0.1 and newer (including current master 
> at the time of
> filing). Reproduces on Hadoop 3.x HDFS warehouse layouts
> (/warehouse/tablespace/managed/hive). No dependence on
> hive.acid.lockless.reads.enabled, no open ACID transaction required.
> Trigger is purely table count > hive.metastore.batch.retrieve.max
> (default 300).}}
>            Reporter: Shubham Sharma
>            Assignee: Shubham Sharma
>            Priority: Major
>




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to