refactor(plugin-iceberg): Fix thread safety in Iceberg procedures#27341
Merged
agrawalreetika merged 1 commit intoprestodb:masterfrom Mar 16, 2026
Merged
refactor(plugin-iceberg): Fix thread safety in Iceberg procedures#27341agrawalreetika merged 1 commit intoprestodb:masterfrom
agrawalreetika merged 1 commit intoprestodb:masterfrom
Conversation
Contributor
Reviewer's GuideWraps Iceberg snapshot/branch management procedures in a ThreadContextClassLoader to fix thread-safety issues related to the context class loader during metadata and snapshot operations. Sequence diagram for Iceberg procedure execution with ThreadContextClassLoadersequenceDiagram
participant PrestoEngine
participant SetCurrentSnapshotProcedure
participant ThreadContextClassLoader
participant IcebergTransactionMetadata
participant IcebergTable
PrestoEngine->>SetCurrentSnapshotProcedure: setCurrentSnapshot(session, schema, table, snapshotId, reference)
activate SetCurrentSnapshotProcedure
SetCurrentSnapshotProcedure->>ThreadContextClassLoader: new ThreadContextClassLoader(procedureClassLoader)
activate ThreadContextClassLoader
SetCurrentSnapshotProcedure->>ThreadContextClassLoader: enter try_with_resources
SetCurrentSnapshotProcedure->>SetCurrentSnapshotProcedure: checkState(snapshotId xor reference)
SetCurrentSnapshotProcedure->>IcebergTransactionMetadata: metadataFactory.create()
activate IcebergTransactionMetadata
IcebergTransactionMetadata-->>SetCurrentSnapshotProcedure: metadata
SetCurrentSnapshotProcedure->>IcebergTransactionMetadata: getIcebergTable(metadata, session, schemaTableName)
IcebergTransactionMetadata-->>SetCurrentSnapshotProcedure: IcebergTable
activate IcebergTable
SetCurrentSnapshotProcedure->>SetCurrentSnapshotProcedure: resolve targetSnapshotId
SetCurrentSnapshotProcedure->>IcebergTable: manageSnapshots().setCurrentSnapshot(targetSnapshotId).commit()
SetCurrentSnapshotProcedure->>IcebergTransactionMetadata: commit()
deactivate IcebergTransactionMetadata
deactivate IcebergTable
SetCurrentSnapshotProcedure->>ThreadContextClassLoader: close() (restore previous context loader)
deactivate ThreadContextClassLoader
SetCurrentSnapshotProcedure-->>PrestoEngine: return
deactivate SetCurrentSnapshotProcedure
Class diagram for Iceberg procedures using ThreadContextClassLoaderclassDiagram
class SetCurrentSnapshotProcedure {
- IcebergTransactionMetadataFactory metadataFactory
+ Procedure get()
+ void setCurrentSnapshot(ConnectorSession clientSession, String schema, String table, Long snapshotId, String reference)
}
class FastForwardBranchProcedure {
- IcebergTransactionMetadataFactory metadataFactory
+ Procedure get()
+ void fastForwardToBranch(ConnectorSession clientSession, String schemaName, String tableName, String fromBranch, String targetBranch)
}
class RollbackToSnapshotProcedure {
- IcebergTransactionMetadataFactory metadataFactory
+ Procedure get()
+ void rollbackToSnapshot(ConnectorSession clientSession, String schema, String table, Long snapshotId)
}
class ThreadContextClassLoader {
- ClassLoader newContextClassLoader
- ClassLoader previousContextClassLoader
+ ThreadContextClassLoader(ClassLoader newContextClassLoader)
+ void close()
}
class IcebergTransactionMetadataFactory {
+ IcebergTransactionMetadata create()
}
class IcebergTransactionMetadata {
+ void commit()
}
class Table {
+ SnapshotManager manageSnapshots()
}
class SnapshotManager {
+ SnapshotManager setCurrentSnapshot(long snapshotId)
+ SnapshotManager rollbackTo(long snapshotId)
+ SnapshotManager fastForwardBranch(String fromBranch, String targetBranch)
+ void commit()
}
SetCurrentSnapshotProcedure --> ThreadContextClassLoader : uses
FastForwardBranchProcedure --> ThreadContextClassLoader : uses
RollbackToSnapshotProcedure --> ThreadContextClassLoader : uses
SetCurrentSnapshotProcedure --> IcebergTransactionMetadataFactory : uses
FastForwardBranchProcedure --> IcebergTransactionMetadataFactory : uses
RollbackToSnapshotProcedure --> IcebergTransactionMetadataFactory : uses
IcebergTransactionMetadataFactory --> IcebergTransactionMetadata : creates
IcebergTransactionMetadata --> Table : provides
Table --> SnapshotManager : provides
SetCurrentSnapshotProcedure --> Table : obtains via metadata
FastForwardBranchProcedure --> Table : obtains via metadata
RollbackToSnapshotProcedure --> Table : obtains via metadata
SetCurrentSnapshotProcedure --> SnapshotManager : manageSnapshots()
FastForwardBranchProcedure --> SnapshotManager : manageSnapshots()
RollbackToSnapshotProcedure --> SnapshotManager : manageSnapshots()
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
ThreadContextClassLoaderwrapping pattern is duplicated across three procedures; consider extracting a small helper (e.g.,withIcebergClassLoader(Runnable)or similar) so future changes to the classloader handling are centralized. - If other Iceberg procedures invoke
manageSnapshots()or similar Iceberg APIs, it may be worth standardizing on the sameThreadContextClassLoaderusage there as well to avoid subtle inconsistencies in thread-safety behavior across procedures. - Consider whether the
ThreadContextClassLoaderscope can be narrowed to just the IcebergmanageSnapshots()calls rather than the whole method body (includingmetadataFactory.create()), to minimize the surface area where the thread context classloader is altered.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `ThreadContextClassLoader` wrapping pattern is duplicated across three procedures; consider extracting a small helper (e.g., `withIcebergClassLoader(Runnable)` or similar) so future changes to the classloader handling are centralized.
- If other Iceberg procedures invoke `manageSnapshots()` or similar Iceberg APIs, it may be worth standardizing on the same `ThreadContextClassLoader` usage there as well to avoid subtle inconsistencies in thread-safety behavior across procedures.
- Consider whether the `ThreadContextClassLoader` scope can be narrowed to just the Iceberg `manageSnapshots()` calls rather than the whole method body (including `metadataFactory.create()`), to minimize the surface area where the thread context classloader is altered.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This was referenced Mar 31, 2026
15 tasks
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.
Description
Fix thread safety in Iceberg procedures
Motivation and Context
Fix thread safety in Iceberg procedures
Impact
Fix thread safety in Iceberg procedures
Test Plan
Contributor checklist
Release Notes
Please follow release notes guidelines and fill in the release notes below.
Summary by Sourcery
Bug Fixes: