Skip to content

HIVE-30234: Iceberg: HiveCatalog rename can revert a concurrent commit's metadata_location - #6977

Open
hemanthumashankar0511 wants to merge 2 commits into
apache:masterfrom
hemanthumashankar0511:iceberg_concurrency
Open

hemanthumashankar0511 wants to merge 2 commits into
apache:masterfrom
hemanthumashankar0511:iceberg_concurrency

Conversation

@hemanthumashankar0511

@hemanthumashankar0511 hemanthumashankar0511 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Send metadata_location CAS on HiveCatalog rename (same as no-lock commits) and fail with CommitFailedException on concurrent modify.

Why are the changes needed?

Rename could overwrite HMS params and silently drop a concurrent Iceberg snapshot.

Does this PR introduce any user-facing change?

Yes, concurrent rename now fails loudly instead of losing data

How was this patch tested?

Ran new unit test mvn test -pl iceberg/iceberg-catalog -Dtest=TestHiveCatalogRenameConcurrency

Tez on Yarn Integration test

Started the cluster in debug mode:
mvn test -Pitests,tez-yarn -pl itests/tez-yarn-it -Dtest=StartTezYarnCluster -Dmaven.surefire.debug
Attached the IDE debugger to port 5005 and set a thread breakpoint in HiveCatalog.renameTableOrView after getTable (before alterTable).

Opened two Beeline sessions to HiveServer2 (session 1 and 2):
beeline -u 'jdbc:hive2://localhost:10000/default;auth=noSasl' -n hive

In session 2, created the table and inserted the first row (no-lock mode for embedded Derby):

CREATE TABLE ice_t (id INT) STORED BY ICEBERG TBLPROPERTIES ('engine.hive.lock-enabled'='false');
INSERT INTO ice_t VALUES (1);

In session 1, started a rename and paused on the breakpoint:
ALTER TABLE ice_t RENAME TO ice_t2;

In session 2, while 1 was paused, committed a concurrent write:
INSERT INTO ice_t VALUES (2);

Resumed the debugger. With the fix, session 1’s rename failed (concurrent modification), SELECT * FROM ice_t still returned both rows, and a second ALTER TABLE ice_t RENAME TO ice_t2 completed successfully with both rows visible on ice_t2.


clients.run(client -> {
MetastoreUtil.alterTable(client, fromDatabase, fromName, table);
MetastoreUtil.alterTable(client, fromDatabase, fromName, table,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This follows #6930's rule that the lock and the CAS are alternatives. A rename holds no HMS lock when it alters: the SQL path's hook unlocks in commitAlterTable before Catalogs.renameTable, and the catalog API never locks. So the CAS is its only guard, whatever the lock mode.

Could you say so in a comment, so nobody later "aligns" it with the hook's lock-only branch? For example:

// The alter sends the whole table back. A rename holds no HMS lock, so the metadata_location CAS keeps it                                                                                                   
// from reverting a commit that landed since the read.


LOG.info("Renamed {} from {}, to {}", contentType.value(), from, to);

} catch (MetaException e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the 4th copy of the CAS message match (HiveTableOperations, HiveViewOperations and HiveTxnCoordinator.isCasFailure have the others), and the only one that uses startsWith.

Could you move HiveTxnCoordinator.isCasFailure into HiveOperationsBase as a static method and check it in the existing catch (TException e) arm?

 } catch (TException e) {                                                                                                                                                                                     
   if (HiveOperationsBase.isCasFailure(e)) {                                                                                                                                                                  
     throw new CommitFailedException(e, "Cannot rename %s to %s. It has been modified concurrently", from, to);                                                                                               
   }                                                                                                                                                                                                          
   throw new RuntimeException("Failed to rename " + from + " to " + to, e);                                                                                                                                   

That drops this arm and the MetaException import (line 36). It should also bring the method back within Sonar's S3776 limit (it reports 17 > 15 on this PR).

* Returns the environment context that makes HMS apply an alter only if the table's current
* metadata location is still the given one.
*/
static Map<String, String> expectedMetadataLocationContext(String metadataLocation) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of a 2nd name for the same map: hmsEnvContext (line 96) has no override, and its only caller is persistTable in this interface.

Could you make it static in place and call HiveOperationsBase.hmsEnvContext(metadataLocation) from the rename?
That keeps the upstream name, and the diff against upstream shrinks to one keyword.

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestHiveCatalogRenameConcurrency {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could this go into TestHiveCatalog instead of a new class?
Rename tests already live there, and its extension (line 110) can take the PRE_EVENT_LISTENERS config. The listener is a no-op until a test arms it, so nothing else changes, and we save one HMS startup.

@deniskuzZ

Copy link
Copy Markdown
Member

Upstream Iceberg's HiveCatalog.renameTableOrView has the same race in both lock modes: it takes no lock and sends no env context, so a commit made under the HMS lock can still be reverted. Spark and Impala use that class, not Hive's copy.

Could you file it against apache/iceberg and link it from the JIRA?

if (e.getMessage() != null && e.getMessage().startsWith(
"The table has been modified. The parameter value for key '" +
BaseMetastoreTableOperations.METADATA_LOCATION_PROP + "' is")) {
throw new CommitFailedException(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Through the HMS REST catalog this surfaces as the wrong error. HMSCatalogAdapter maps CommitFailedException to 409, and Iceberg's REST client turns a 409 on rename into AlreadyExistsException, so engines report "table already exists" and don't retry. A rename doesn't depend on the table contents, and the failed alter wrote nothing.

Could renameTableOrView re-read and retry a few times on a CAS failure, and throw only when the attempts run out?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you add the retry, the test changes too. With the listener committing once, the rename should now succeed, and the renamed table should keep the concurrent snapshot. To cover the give-up path, let the listener commit on every attempt and assert CommitFailedException after the last one.

@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@hemanthumashankar0511

hemanthumashankar0511 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Upstream Iceberg's HiveCatalog.renameTableOrView has the same race in both lock modes: it takes no lock and sends no env context, so a commit made under the HMS lock can still be reverted. Spark and Impala use that class, not Hive's copy.

Could you file it against apache/iceberg and link it from the JIRA?

Opened a new github issue at apache iceberg: apache/iceberg#19211 and linked it to the asf jira

@hemanthumashankar0511

Copy link
Copy Markdown
Contributor Author

Thanks @deniskuzZ for the review. I've tried to address the comments in the latest commit. Could you please take another look when you have the bandwidth to do so. Thanks!!

This branch has not been deployed

No deployments
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.

3 participants