[FLINK-36298][table-planner] Re-establish Calcite metadata provider on foreign threads#28738
Open
MartijnVisser wants to merge 1 commit into
Open
[FLINK-36298][table-planner] Re-establish Calcite metadata provider on foreign threads#28738MartijnVisser wants to merge 1 commit into
MartijnVisser wants to merge 1 commit into
Conversation
…n foreign threads RelMetadataQueryBase.THREAD_PROVIDERS is seeded only on the thread that builds the cached, shared RelOptCluster. When optimization runs on a different thread (for example a PyFlink py4j gateway thread) the thread-local is unset and metadata handlers throw NullPointerException. Guard FlinkRelMetadataQuery.instance() to re-seed the thread-local with the same provider FlinkRelOptClusterFactory configures. Generated-by: Claude Opus 4.8 (1M context)
Collaborator
Contributor
Author
|
@flinkbot run azure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the purpose of the change
Fixes the intermittent
NullPointerException: metadataHandlerProvider(CalciteRelMetadataQueryBase) that has recurred for years, only in flink-python (e.g.StreamPandasConversionTests).Root cause: Calcite keeps the metadata handler provider in a thread-local (
RelMetadataQueryBase.THREAD_PROVIDERS), seeded only on the thread that builds the cached, sharedRelOptCluster(FlinkRelOptClusterFactory). PyFlink runs optimization (to_pandas()->ArrowUtils.collectAsPandasDataFrame->table.execute().collect()) on a py4j gateway thread that can differ from the cluster-building thread, where the thread-local is unset, so the first metadata-handler lookup NPEs. The nondeterministic py4j thread assignment is why it is intermittent and PyFlink-only.The fix re-seeds the thread-local in
FlinkRelMetadataQuery.instance(), the single funnel through which all metadata queries are obtained (directly, viareuseOrCreate, and via the cluster's metadata-query supplier), using the same providerFlinkRelOptClusterFactoryinstalls.Notes for reviewers:
RelMetadataQueryis a separate, pre-existing concern that this change neither introduces nor fixes.RelOptCluster.setMetadataProviderseeding; the value is the statelessFlinkDefaultRelMetadataProvidersingleton chain.instance()funnel is provider-agnostic and robust to any foreign-thread caller, versus seeding at the PyFlink entry point, which is more invasive and PyFlink-specific.Likely duplicates of the same NPE: FLINK-33531, FLINK-38392.
Brief change log
FlinkRelMetadataQuery.instance()to re-seedRelMetadataQueryBase.THREAD_PROVIDERSwhen unset on the current threadFlinkRelOptClusterFactorydocumenting the single provider both sites rely onFlinkRelMetadataQueryThreadLocalTest, a deterministic red/green reproduction of the cross-thread NPEVerifying this change
This change added tests and can be verified as follows:
FlinkRelMetadataQueryThreadLocalTestbuilds the cluster on the main thread and issues a metadata query on a fresh worker thread; it fails withNullPointerException: metadataHandlerProviderwithout the fix and passes with itmvn test -Dtest=FlinkRelMd*inflink-table-plannerpasses (315 tests)Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Opus 4.8 (1M context)