Repository navigation
HIVE-30217: Batch partition fetches to reduce memory footprint - #6935
Conversation
c6d7129 to
67331a5
Compare
wecharyu
left a comment
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
🟡 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
PartitionsIterablewith 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); |
There was a problem hiding this comment.
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.
dfa9d18 to
ef2513b
Compare
| 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; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
ack, removed the doc. Using batchSize other than batchSize - result.size() is to reduce the roundtrip to database, and speed up the iteration.
| 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; | ||
| } |
There was a problem hiding this comment.
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?
|




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?