Skip to content

[IcebergIO] Improve TableCache#38882

Open
ahmedabu98 wants to merge 1 commit into
apache:masterfrom
ahmedabu98:iceberg-table-cache
Open

[IcebergIO] Improve TableCache#38882
ahmedabu98 wants to merge 1 commit into
apache:masterfrom
ahmedabu98:iceberg-table-cache

Conversation

@ahmedabu98

Copy link
Copy Markdown
Contributor

Makes TableCache more robust so it can support both source and sink use cases.

Previously, source and sink paths used separate table caches, which led to unnecessary loadTable and refresh calls. This changes Iceberg IO to use one process-wide table cache keyed by IcebergCatalogConfig and table identifier. Callers no longer need to load/create the underlying Catalog just to check for a cached table. The catalog is only loaded on cache miss.

Supports normal cache lookup, force refresh, and refresh if stale. Also allows sinks to pass loaders to create the table if needed.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@ahmedabu98

Copy link
Copy Markdown
Contributor Author

@dejii any chance you can take a look?

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request centralizes and optimizes the TableCache mechanism within the Iceberg IO connector. By transitioning to a process-wide cache keyed by catalog configuration and table identifiers, the changes significantly reduce overhead associated with redundant table loading and metadata refreshing. These improvements enhance robustness for both source and sink operations and simplify the codebase by offloading cache management from individual IO components.

Highlights

  • Unified TableCache: Refactored TableCache to a process-wide implementation keyed by IcebergCatalogConfig and TableIdentifier, enabling shared metadata across source and sink operations.
  • Optimized Loading: Improved performance by deferring catalog resolution until a cache miss occurs, eliminating unnecessary catalog calls.
  • Refresh Mechanisms: Added robust support for force-refresh and stale-based refresh operations to ensure metadata consistency.
  • Code Simplification: Simplified RecordWriterManager and various IO classes by removing manual cache management and setup logic.
  • Sink Support: Enabled sinks to provide custom loaders for table creation, improving flexibility in dynamic destination scenarios.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the Iceberg table caching mechanism by introducing a process-wide, thread-safe TableCache keyed by both IcebergCatalogConfig and TableIdentifier. This replaces various local caches and simplifies several DoFns across the SDK. The review feedback correctly identifies a critical bug in WritePartitionedRowsToFiles.java where storing specId and partitionFieldMap as single instance variables in WriteDoFn can cause state collisions when processing multiple tables in the same bundle; these should be keyed by TableIdentifier instead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +89 to +90
private int specId = Integer.MIN_VALUE;
private Map<String, PartitionField> partitionFieldMap = Maps.newHashMap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Storing specId and partitionFieldMap as single instance variables in WriteDoFn is incorrect because a single WriteDoFn instance can process elements for multiple different tables (destinations) within the same bundle. Since different tables can have the same specId (e.g., 0), this can lead to using the partition field map of one table for another table, causing incorrect partition paths or failures. We should key these by TableIdentifier instead.

Suggested change
private int specId = Integer.MIN_VALUE;
private Map<String, PartitionField> partitionFieldMap = Maps.newHashMap();
private final Map<TableIdentifier, Integer> specIds = Maps.newHashMap();
private final Map<TableIdentifier, Map<String, PartitionField>> partitionFieldMaps = Maps.newHashMap();

Table table = lastRefreshedTable.table;
partitionPath = getPartitionDataPath(partitionPath, lastRefreshedTable.partitionFieldMap);
Table table = getOrCreateTable(destination, dataSchema);
partitionPath = getPartitionDataPath(partitionPath, getPartitionFieldMap(table));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Update the call to getPartitionFieldMap to pass the table identifier so that the correct partition field map is retrieved for the specific table.

Suggested change
partitionPath = getPartitionDataPath(partitionPath, getPartitionFieldMap(table));
partitionPath = getPartitionDataPath(partitionPath, getPartitionFieldMap(destination.getTableIdentifier(), table));

Comment on lines +143 to 154
private Map<String, PartitionField> getPartitionFieldMap(Table table) {
if (table.spec().specId() == this.specId) {
return partitionFieldMap;
}

/**
* Refreshes the table metadata if it is considered stale (older than 2 minutes).
*
* <p>This method first performs a non-synchronized check on the table's freshness. This
* provides a lock-free fast path that avoids synchronization overhead in the common case
* where the table does not need to be refreshed. If the table might be stale, it then enters
* a synchronized block to ensure that only one thread performs the refresh operation.
*/
void refreshIfStale() {
// Fast path: Avoid entering the synchronized block if the table is not stale.
if (lastRefreshTime.isAfter(Instant.now().minus(STALENESS_THRESHOLD))) {
return;
}
synchronized (this) {
if (lastRefreshTime.isBefore(Instant.now().minus(STALENESS_THRESHOLD))) {
table.refresh();
lastRefreshTime = Instant.now();
if (table.spec().specId() != this.specId) {
partitionFieldMap = Maps.newHashMap();
for (PartitionField partitionField : table.spec().fields()) {
partitionFieldMap.put(partitionField.name(), partitionField);
}
this.specId = table.spec().specId();
}
}
}
Map<String, PartitionField> partitionFieldMap = Maps.newHashMap();
for (PartitionField partitionField : table.spec().fields()) {
partitionFieldMap.put(partitionField.name(), partitionField);
}
this.specId = table.spec().specId();
this.partitionFieldMap = partitionFieldMap;
return partitionFieldMap;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Update getPartitionFieldMap to accept the TableIdentifier and look up/store the partition field map and spec ID per table identifier.

Suggested change
private Map<String, PartitionField> getPartitionFieldMap(Table table) {
if (table.spec().specId() == this.specId) {
return partitionFieldMap;
}
/**
* Refreshes the table metadata if it is considered stale (older than 2 minutes).
*
* <p>This method first performs a non-synchronized check on the table's freshness. This
* provides a lock-free fast path that avoids synchronization overhead in the common case
* where the table does not need to be refreshed. If the table might be stale, it then enters
* a synchronized block to ensure that only one thread performs the refresh operation.
*/
void refreshIfStale() {
// Fast path: Avoid entering the synchronized block if the table is not stale.
if (lastRefreshTime.isAfter(Instant.now().minus(STALENESS_THRESHOLD))) {
return;
}
synchronized (this) {
if (lastRefreshTime.isBefore(Instant.now().minus(STALENESS_THRESHOLD))) {
table.refresh();
lastRefreshTime = Instant.now();
if (table.spec().specId() != this.specId) {
partitionFieldMap = Maps.newHashMap();
for (PartitionField partitionField : table.spec().fields()) {
partitionFieldMap.put(partitionField.name(), partitionField);
}
this.specId = table.spec().specId();
}
}
}
Map<String, PartitionField> partitionFieldMap = Maps.newHashMap();
for (PartitionField partitionField : table.spec().fields()) {
partitionFieldMap.put(partitionField.name(), partitionField);
}
this.specId = table.spec().specId();
this.partitionFieldMap = partitionFieldMap;
return partitionFieldMap;
}
private Map<String, PartitionField> getPartitionFieldMap(TableIdentifier tableId, Table table) {
int currentSpecId = table.spec().specId();
if (currentSpecId == specIds.getOrDefault(tableId, Integer.MIN_VALUE)) {
return partitionFieldMaps.get(tableId);
}
Map<String, PartitionField> fieldMap = Maps.newHashMap();
for (PartitionField partitionField : table.spec().fields()) {
fieldMap.put(partitionField.name(), partitionField);
}
specIds.put(tableId, currentSpecId);
partitionFieldMaps.put(tableId, fieldMap);
return fieldMap;
}

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant