Skip to content

HIVE-30217: Batch partition fetches to reduce memory footprint - #6935

Merged
dengzhhu653 merged 6 commits into
apache:masterfrom
dengzhhu653:HIVE-30217
Dec 1, 2026
Merged

dengzhhu653 merged 6 commits into
apache:masterfrom
dengzhhu653:HIVE-30217

Conversation

@dengzhhu653

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

Why are the changes needed?

Does this PR introduce any user-facing change?

How was this patch tested?

@wecharyu wecharyu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title only cover the rename OOM, but this PR also switches other handler to PartitionsIterable. Could you update the title and description to cover the broader scope and list the affected paths?

@dengzhhu653 dengzhhu653 changed the title HIVE-30217: Renaming large partitioned table causes OOM HIVE-30217: Batch partition fetches to reduce memory footprint Nov 10, 2026
@dengzhhu653
dengzhhu653 requested a balanced review from Copilot November 10, 2026 02:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical issues can cause failure after a committed Iceberg delete, partial truncation before all listener checks complete, and nonterminating loops with a zero batch size.

3 open issues
What changed in this PR

This PR reduces Hive Metastore memory usage by fetching and processing partition objects in batches instead of loading all partitions at once.

Changes:

  • Adds a reusable PartitionsIterable with configurable batch sizes.
  • Applies batching to table alterations, truncation, transactional validation, statistics updates, and file-metadata caching.
  • Adds iterator tests and updates integration tests for batched processing and listener behavior.
File Description
standalone-metastore/​metastore-server/​src/​test/​java/​org/​apache/​hadoop/​hive/​metastore/​TestMetaStoreEventListener.java Updates expected pre-event ordering for table renames.
standalone-metastore/​metastore-server/​src/​test/​java/​org/​apache/​hadoop/​hive/​metastore/​TestHiveMetaStore.java Expects cascade alteration to succeed beyond partition-fetch limits.
standalone-metastore/​metastore-server/​src/​test/​java/​org/​apache/​hadoop/​hive/​metastore/​metastore/​TestPartitionsIterable.java Tests batching, missing partitions, and iterator behavior.
standalone-metastore/​metastore-server/​src/​test/​java/​org/​apache/​hadoop/​hive/​metastore/​client/​TestTablesCreateDropAlterTruncate.java Exercises multi-batch truncation and verifies statistics resets.
standalone-metastore/​metastore-server/​src/​main/​java/​org/​apache/​hadoop/​hive/​metastore/​TransactionalValidationListener.java Validates partition directories in batches.
standalone-metastore/​metastore-server/​src/​main/​java/​org/​apache/​hadoop/​hive/​metastore/​metastore/​PartitionsIterable.java Introduces lazy partition-object retrieval by name batches.
standalone-metastore/​metastore-server/​src/​main/​java/​org/​apache/​hadoop/​hive/​metastore/​metastore/​impl/​ColStatsStoreImpl.java Batches partition column-statistics invalidation.
standalone-metastore/​metastore-server/​src/​main/​java/​org/​apache/​hadoop/​hive/​metastore/​HiveAlterHandler.java Batches partition updates during renames and schema changes.
standalone-metastore/​metastore-server/​src/​main/​java/​org/​apache/​hadoop/​hive/​metastore/​handler/​TruncateTableHandler.java Processes truncation, statistics updates, and notifications per batch.
standalone-metastore/​metastore-server/​src/​main/​java/​org/​apache/​hadoop/​hive/​metastore/​handler/​DeprecatedHandler.java Uses the shared iterator for file-metadata caching.

Review effort: Balanced

continue;
}
EnvironmentContext environmentContext = new EnvironmentContext();
List<Partition> newPartitions = buildAndCheckPartitions(partitions, environmentContext);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's intentional. A validation pass beforehand would read every partition once to fire the pre-event, then read them again to delete and alter. That doubles the metastore reads on every partitioned truncate, which is the cost this change avoids. The truncate table is a best-effort operation, i.e, we cannot restore the deleted data if the metadata change fails.

Comment on lines +73 to +87
public List<Partition> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
List<Partition> result = new ArrayList<>(batchSize);
if (hasPendingPartitions()) {
appendPartitions(result, leftParts);
leftParts = null;
}
while (result.size() < batchSize && partitionNamesIter.hasNext()) {
List<String> nameBatch = nextNameBatch();
appendPartitions(result, fetchBatch(nameBatch));
}
return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do callers need every batch filled to exactly batchSize? If next() returned just one getPartitionsByNames result per call, leftParts, appendPartitions, hasPendingPartitions could all be removed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation is the Metastore might drop the partitions during iterating over the partition names, it might always give us a "batchSize" number of partitions, and we use the returned partitions to perform some batched operations, such as "alterPartitions". In worst case it might return an empty partition list in the middle of iteration, which might surprise the caller.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fill doesn't prevent empty batches: if the trailing names are all dropped, next() still returns [] (nullMetastoreResponseTreatedAsEmpty). If empties are the concern, skipping empty fetches in hasNext() would avoid them with less code than leftParts/appendPartitions. WDYT?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

skipping empty fetches in hasNext() would avoid them with less code than leftParts/appendPartition

IMO, still we need leftParts/appendPartition, if the size of partitions fetched during the iteration is less than the batchSize, the iterator will fetch next partitions until the batch is full or the names are exhausted, that's means partitions fetched might be split across batches.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Javadoc says each lookup requests only as many names as the batch has room for, but readBatch() still calls nextNames(batchSize). Sizing it to batchSize - result.size() would also let remainder go.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack, removed the doc. Using batchSize other than batchSize - result.size() is to reduce the roundtrip to database, and speed up the iteration.

@dengzhhu653
dengzhhu653 requested a review from wecharyu November 30, 2026 06:30
Comment on lines +73 to +87
public List<Partition> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
List<Partition> result = new ArrayList<>(batchSize);
if (hasPendingPartitions()) {
appendPartitions(result, leftParts);
leftParts = null;
}
while (result.size() < batchSize && partitionNamesIter.hasNext()) {
List<String> nameBatch = nextNameBatch();
appendPartitions(result, fetchBatch(nameBatch));
}
return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fill doesn't prevent empty batches: if the trailing names are all dropped, next() still returns [] (nullMetastoreResponseTreatedAsEmpty). If empties are the concern, skipping empty fetches in hasNext() would avoid them with less code than leftParts/appendPartitions. WDYT?

@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@wecharyu wecharyu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@dengzhhu653
dengzhhu653 merged commit e3ac753 into apache:master Dec 1, 2026
5 checks passed
@dengzhhu653
dengzhhu653 deleted the HIVE-30217 branch December 1, 2026 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants