Skip to content

[#12576] improvement(core): optimize schema write fencing - #12577

Open
yuqi1129 wants to merge 10 commits into
apache:mainfrom
yuqi1129:improve/12576-schema-write-fencing
Open

[#12576] improvement(core): optimize schema write fencing#12577
yuqi1129 wants to merge 10 commits into
apache:mainfrom
yuqi1129:improve/12576-schema-write-fencing

Conversation

@yuqi1129

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

  • Add one schema-scoped transaction entry point that acquires the parent schema lock and runs child writes in the same transaction.
  • Migrate table, view, fileset, function, model, model-version, and topic insert/update paths to that entry point.
  • Replace six complete child metadata loads in the non-cascade schema emptiness check with one lightweight existence query that stops after the first active child.
  • Preserve missing-schema errors when the fence rejects view or fileset updates.
  • Cover all six direct child types through real service create/update paths and the existence query.

This is a follow-up stacked on #12456. Until #12456 merges, GitHub also shows its commits in this draft; the #12576 follow-up itself is commit 10a17fe and changes 871 lines.

Why are the changes needed?

The previous implementation held the schema delete fence correctly, but every schema-scoped service had to assemble the lock and transaction manually. That made future omissions easy. Non-cascade deletion also materialized complete child objects and version information when it only needed a yes/no answer, extending the time spent under the schema delete lock.

Fix: #12576

Does this PR introduce any user-facing change?

No.

How was this patch tested?

  • Full TestSchemaMetaService on H2.
  • Full table, view, fileset, function, model, model-version, and topic service suites on H2.
  • Schema child create/update fencing, all six existence-query branches, and table/function/model-version race tests on H2, MySQL, and PostgreSQL.
  • ./gradlew :core:spotlessApply and git diff --check.

Advance the schema OCC version on every alter and guard alter and drop
with a compare-and-set on the observed version, classifying a failed
CAS as either a stale conflict or a missing entity.

Make managed schema creation insert-only so a concurrent same-name
create returns SchemaAlreadyExistsException instead of overwriting the
winner, and take a shared lock on the parent catalog row so a schema
cannot be created below a catalog that is being dropped. Serialize
hierarchical ancestor materialization and schema drops through the
catalog row so overlapping cascades share one lock order.

Lock the parent schema row before writing a table, view, fileset,
function, model, or topic, and check views and functions before a
non-cascade schema drop.

Accepted tradeoff: a hierarchical schema create that materializes
implicit ancestors takes an exclusive lock on the catalog row, because
two concurrent creates can both find the same ancestor missing and both
insert it, and a shared lock does not prevent that under MySQL
REPEATABLE READ.
… backends

H2 is also the default embedded backend, not only a test backend. Spell out
that falling back to an exclusive lock serializes schema creations under one
catalog there and can surface as an H2 lock timeout.
…n code

Review feedback: the concurrency-critical parts need comments so a reader can
follow why the statements are ordered the way they are.

- Say what the catalog row lock buys on a schema create, and why a nested name
  has to take it exclusively while a plain name does not.
- Say why both drop paths delete the schema row before looking at its children,
  and why every drop takes catalog before schema.
- Say what the shared schema lock in front of a table, view, fileset, function,
  model, or topic write is for, and that only a cross-schema rename needs it.
- Say why the alter UPDATE compares only the version, what zero affected rows
  can mean, and why a partial cascade must roll back.
- Say why managed schema creation is insert-only now.
- Correct the schemaWriteFailure comment: sessions run at READ_COMMITTED, so
  the locking read is there to wait out an in-flight writer.
…ic on overwrite

Carry the fix that apache#12455 already made for catalogs over to schemas, so both
sides of the hierarchy follow the same rule.

- Advance current_version on all four schema upsert paths (single and batch, on
  MySQL/H2 and PostgreSQL) instead of writing the initial version back, which
  would let a writer holding an older version still pass its own version check.
- Name the table on the PostgreSQL assignments: a bare column on that side of
  ON CONFLICT is ambiguous there, which is how the previous CI run broke.
- Add TestSchemaMetaPostgreSQLProvider to pin both rules without a database, so
  they are checked on every run and not only in the Docker-backed CI job.
- Cover the race this PR is meant to close: a catalog cascade that holds the
  catalog row makes a concurrent schema create wait and then report the catalog
  as missing, leaving no orphan behind. Reading the cascade snapshot moved into
  a package-private method so the test can pause exactly at that point, the
  same seam MetalakeMetaService already offers.
- Say that the H2 shared-lock fallback affects H2 backends, not just tests.
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

Code Coverage Report

Overall Project 68.48% +0.22% 🟢
Files changed 90.1% 🟢

Module Coverage
aliyun 19.74% 🔴
api 52.6% 🟢
authorization-common 85.96% 🟢
authorization-ranger 4.38% 🔴
aws 53.54% 🟢
azure 32.1% 🔴
catalog-common 9.9% 🔴
catalog-fileset 80.17% 🟢
catalog-glue 69.24% 🟢
catalog-hive 82.96% 🟢
catalog-jdbc-common 45.69% 🟢
catalog-jdbc-doris 82.44% 🟢
catalog-jdbc-mysql 79.33% 🟢
catalog-jdbc-postgresql 83.39% 🟢
catalog-jdbc-starrocks 79.16% 🟢
catalog-kafka 76.99% 🟢
catalog-lakehouse-generic 60.55% 🟢
catalog-lakehouse-hudi 79.1% 🟢
catalog-lakehouse-iceberg 85.86% 🟢
catalog-lakehouse-paimon 84.26% 🟢
catalog-model 77.99% 🟢
cli 44.48% 🟢
client-java 77.92% 🟢
common 55.74% 🟢
core 83.65% +0.23% 🟢
filesystem-hadoop3 76.45% 🟢
flink 0.0% 🔴
flink-common 50.29% 🟢
flink-runtime 0.0% 🔴
gcp 32.2% 🔴
hadoop-auth 68.0% 🟢
hadoop-common 17.84% 🔴
hive-metastore-common 53.4% 🟢
iceberg-aliyun-bundle 0.0% 🔴
iceberg-common 64.75% 🟢
iceberg-rest-server 75.68% 🟢
idp-basic 85.98% 🟢
integration-test-common 0.0% 🔴
jobs 62.92% 🟢
lance-common 32.72% 🔴
lance-rest-server 64.78% 🟢
lineage 53.02% 🟢
optimizer 83.24% 🟢
optimizer-api 21.95% 🔴
server 88.04% 🟢
server-common 80.35% 🟢
spark 28.57% 🔴
spark-common 48.92% 🟢
tencent 69.84% 🟢
trino-connector 47.44% 🟢
Files
Module File Coverage
core FunctionMetaService.java 100.0% 🟢
TableMetaService.java 100.0% 🟢
ViewMetaService.java 100.0% 🟢
SchemaMetaService.java 97.44% 🟢
ModelVersionMetaService.java 93.52% 🟢
FilesetMetaService.java 91.15% 🟢
SchemaMetaSQLProviderFactory.java 75.68% 🟢
ModelMetaService.java 75.45% 🟢
TopicMetaService.java 70.87% 🟢
SchemaMetaBaseSQLProvider.java 69.23% 🟢
SchemaMetaMapper.java 0.0% 🔴

@yuqi1129 yuqi1129 self-assigned this Aug 24, 2026
@yuqi1129
yuqi1129 marked this pull request as ready for review August 24, 2026 12:32
Copilot AI lite review requested due to automatic review settings August 24, 2026 12:32

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

+ TopicMetaMapper.TABLE_NAME
+ " WHERE schema_id = #{schemaId} AND deleted_at = 0",
"LIMIT 1"
})

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.

Shall we use our convention to define the SQL string in the provider, rather than add a notation here directly?

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.

Updated.

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.

[Improvement] Optimize schema emptiness checks and schema-scoped write fencing

3 participants