anshuksi282-ksolves opened a new pull request, #6650:
URL: https://github.com/apache/hive/pull/6650
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://cwiki.apache.org/confluence/display/Hive/HowToContribute
2. Ensure that you have created an issue on the Hive project JIRA:
https://issues.apache.org/jira/projects/HIVE/summary
3. Ensure you have added or run the appropriate tests for your PR:
4. If the PR is unfinished, add '[WIP]' in your PR title, e.g.,
'[WIP]HIVE-XXXXX: Your PR title ...'.
5. Be sure to keep the PR description updated to reflect all changes.
6. Please write your PR title to summarize what this PR proposes.
7. If possible, provide a concise example to reproduce the issue for a
faster review.
-->
### What changes were proposed in this pull request?
`HiveMaterializedViewsRegistry.createMaterialization()` creates a `Context`
object (used for query parsing/semantic analysis) before attempting to parse
the materialized view's query. If parsing or semantic analysis fails, the
method logs a warning and returns `null`, but never called `context.clear()` to
clean up any scratch/staging directories the `Context` may have created during
that attempt.
This PR adds a call to `context.clear()` inside the `catch` block, so that
any such directories are removed eagerly, at the point the exception is caught,
rather than being left behind.
### Why are the changes needed?
Directories created by `Context` (e.g. for CTE materialization, or
dummy-table handling for FROM-less queries) are only registered via
`fs.deleteOnExit()`. This means they are only cleaned up when the JVM exits.
For a long-running process like HiveServer2, if `createMaterialization()`
repeatedly hits parse/semantic exceptions (e.g. due to a materialized view
definition referencing a table that no longer exists), these directories
accumulate over time and are never removed until the server restarts. This can
eventually cause HDFS directory-item-limit errors, as reported in the JIRA:
Cannot create staging directory '...': The directory item limit of ... is
exceeded: limit=1048576 items=1048576
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
- Added
`TestHiveMaterializedViewsRegistry#testStagingDirIsCleanedUpOnParseException`.
It uses a materialized view query with a CTE referenced enough times to trigger
CTE materialization (which creates a real directory tracked by `Context`),
followed by an invalid column reference that fails during semantic analysis.
The test asserts that no per-query scratch subdirectory remains once
`createMaterialization()` returns.
- Verified this test fails, with a leftover directory reported, when the
fix is reverted, and passes with the fix applied.
- Added
`TestContext#testClearRemovesScratchDirEagerlyWhenHDFSCleanupEnabled`, a more
targeted test confirming that `Context.clear()` removes a scratch directory
immediately (rather than relying on `deleteOnExit`) when `isHDFSCleanup` is
enabled.
- `mvn -pl ql test -Dtest=TestContext,TestHiveMaterializedViewsRegistry`
passes.
Contains content generated by Claude (Anthropic), used to help investigate
the root cause, draft the fix, and design/verify the accompanying tests.
--
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]