Skip to content

Core: Report a rename that loses the race for its name as ENTITY_ALREADY_EXISTS - #5986

Merged
jbonofre merged 3 commits into
apache:mainfrom
ZephyrYWZhou:fix-rename-name-race
Nov 5, 2026
Merged

jbonofre merged 3 commits into
apache:mainfrom
ZephyrYWZhou:fix-rename-name-race

Conversation

@ZephyrYWZhou

Copy link
Copy Markdown
Contributor

Summary

AtomicOperationMetaStoreManager.renameEntity() checks that the destination name is free, then writes the renamed entity. If another writer claims that name in between, the write fails on the name constraint. renameEntity() only caught RetryOnConcurrencyException, so this race surfaced as an unhandled error instead of ENTITY_ALREADY_EXISTS. A create in the same situation already returns ENTITY_ALREADY_EXISTS.

Raised by @flyrain in #5892.

Changes

  • AtomicOperationMetaStoreManager.renameEntity(): map EntityAlreadyExistsException to ENTITY_ALREADY_EXISTS, as persistNewEntity does.
  • JdbcBasePersistenceImpl.writeEntity(): on the update path, translate a uniqueness violation into EntityAlreadyExistsException, matching the create path. Before this, it was wrapped into a generic RuntimeException. If the conflicting entity is not visible in the snapshot, it raises RetryOnConcurrencyException, same as the create path.

Testing

  • JdbcBasePersistenceImplTest.writeEntity_renameOntoTakenName_throwsEntityAlreadyExistsException (H2): renaming onto a taken name throws EntityAlreadyExistsException and leaves the entity unchanged. It fails without the JDBC change.
  • AtomicMetastoreManagerWithJdbcBasePersistenceImplTest.renameThatLosesTheRaceForItsNameReportsAlreadyExists: hides the taken name from the pre-check, as if another writer claimed it right after, and checks that renameEntity() returns ENTITY_ALREADY_EXISTS. Runs in the Postgres IT.
  • ./gradlew :polaris-core:test :polaris-relational-jdbc:test plus spotless and checkstyle pass locally. The Postgres IT needs Docker and is expected to run in CI.

Checklist

@ZephyrYWZhou
ZephyrYWZhou marked this pull request as ready for review November 5, 2026 04:10

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice small fix. The rename now reports ENTITY_ALREADY_EXISTS the same way create does, and the mapping lives at the right layers: JDBC turns the SQL error into a domain exception, and core turns that into a status. I checked that lookupEntityByName uses its own connection (executeSelect without the caller's connection), so it's safe after Postgres aborts the failed UPDATE, same as on the create path.

One test gap: nothing covers the new update-path RetryOnConcurrencyException branch (JdbcBasePersistenceImpl.java:277). Can we parameterize writeEntity_uniquenessViolationWithInvisibleConflict_throwsRetryOnConcurrencyException over insert vs. update so that branch gets hit too?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

originalEntity.getGrantRecordsVersion());
}
} catch (SQLException e) {
if (datasourceOperations.isUniquenessConstraintViolation(e)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This block is nearly the same as the insert-path handler a few lines up. The only difference is the extra getId() != entity.getId() check. The visibility handling already changed once (#5094), and next time it'd be easy to update only one copy. Can we pull it into a small private helper that both branches call?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call on that, let me create a shared helper method for it.

@flyrain flyrain 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.

+1 Thanks @ZephyrYWZhou !

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Nov 5, 2026

@jbonofre jbonofre left a comment

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.

Thanks for the fix! The change is well scoped and an improvement: a rename that loses the race for its destination name now return ENTITY_ALREADY_EXISTS, the same as the create path, instead of failing with a generic error.

@jbonofre
jbonofre merged commit 06a526f into apache:main Nov 5, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle a lost destination-name race in AtomicOperationMetaStoreManager.renameEntity()

4 participants