Skip to content

refactor(plugin-iceberg): Fix thread safety in Iceberg procedures#27341

Merged
agrawalreetika merged 1 commit intoprestodb:masterfrom
agrawalreetika:iceberg-refactor
Mar 16, 2026
Merged

refactor(plugin-iceberg): Fix thread safety in Iceberg procedures#27341
agrawalreetika merged 1 commit intoprestodb:masterfrom
agrawalreetika:iceberg-refactor

Conversation

@agrawalreetika
Copy link
Copy Markdown
Member

@agrawalreetika agrawalreetika commented Mar 16, 2026

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

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.
  • If adding new dependencies, verified they have an OpenSSF Scorecard score of 5.0 or higher (or obtained explicit TSC approval for lower scores).

Release Notes

Please follow release notes guidelines and fill in the release notes below.

== NO RELEASE NOTE ==

Summary by Sourcery

Bug Fixes:

  • Wrap Iceberg snapshot management operations in a thread context class loader to avoid thread-safety issues when invoking these procedures.

@agrawalreetika agrawalreetika self-assigned this Mar 16, 2026
@prestodb-ci prestodb-ci added the from:IBM PR from IBM label Mar 16, 2026
@prestodb-ci prestodb-ci requested review from a team, anandamideShakyan and sh-shamsan and removed request for a team March 16, 2026 07:16
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 16, 2026

Reviewer's Guide

Wraps 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 ThreadContextClassLoader

sequenceDiagram
    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
Loading

Class diagram for Iceberg procedures using ThreadContextClassLoader

classDiagram
    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()
Loading

File-Level Changes

Change Details Files
Ensure Iceberg snapshot-setting procedure executes with a controlled thread context class loader.
  • Wrap the body of setCurrentSnapshot in a try-with-resources using ThreadContextClassLoader(getClass().getClassLoader())
  • Keep existing validation that exactly one of snapshotId or reference is provided
  • Perform Iceberg table lookup, snapshot ID resolution, snapshot switch, and transaction commit within the protected context
presto-iceberg/src/main/java/com/facebook/presto/iceberg/procedure/SetCurrentSnapshotProcedure.java
Ensure Iceberg fast-forward branch procedure runs under a deterministic thread context class loader.
  • Wrap the body of fastForwardToBranch in a try-with-resources using ThreadContextClassLoader(getClass().getClassLoader())
  • Perform table lookup, fast-forward branch operation, and metadata commit inside this context
presto-iceberg/src/main/java/com/facebook/presto/iceberg/procedure/FastForwardBranchProcedure.java
Ensure Iceberg rollback-to-snapshot procedure runs under a deterministic thread context class loader.
  • Wrap the body of rollbackToSnapshot in a try-with-resources using ThreadContextClassLoader(getClass().getClassLoader())
  • Perform table lookup, snapshot rollback, and metadata commit inside this context
presto-iceberg/src/main/java/com/facebook/presto/iceberg/procedure/RollbackToSnapshotProcedure.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Member

@hantangwangd hantangwangd left a comment

Choose a reason for hiding this comment

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

@agrawalreetika agrawalreetika merged commit 0eab0ac into prestodb:master Mar 16, 2026
120 of 125 checks passed
@agrawalreetika agrawalreetika deleted the iceberg-refactor branch March 16, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

from:IBM PR from IBM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants