Skip to content

fix(sql): fix view metadata race conditions on replica#6627

Merged
bluestreak01 merged 5 commits intomasterfrom
vi_fix_view
Jan 12, 2026
Merged

fix(sql): fix view metadata race conditions on replica#6627
bluestreak01 merged 5 commits intomasterfrom
vi_fix_view

Conversation

@bluestreak01
Copy link
Copy Markdown
Member

@bluestreak01 bluestreak01 commented Jan 12, 2026

Summary

Fix two race conditions in view state management that could cause incorrect view metadata on replicas:

  1. Stale update race: A task with older timestamp could overwrite newer, correct view state
  2. Empty metadata race: View metadata was hydrated with empty column info, then updated asynchronously by ViewCompilerJob

Problem 1: Stale Updates

When multiple ViewCompilerJob tasks run concurrently (e.g., during replica sync with random server restarts), a task that started earlier but completed later could overwrite a newer, correct view state.

Example from failing test:

17:21:10.197 - ViewCompilerJob sets invalid=true, updateTimestamp=196120 (correct)
17:21:10.199 - ViewCompilerJob sets invalid=false, updateTimestamp=195321 (stale, overwrites correct state)

Solution: Add timestamp check in updateViewState() - skip updates if updateTimestamp < viewState.getUpdateTimestamp().

Problem 2: Empty Metadata on Creation

When a view is created:

  1. createViewState() creates ViewState with empty ViewMetadata (no columns, no timestamp)
  2. Empty metadata is immediately hydrated into MetadataCache
  3. ViewCompilerJob asynchronously compiles view and updates metadata

If queries run between steps 2 and 3, they see empty designatedTimestamp in tables().

Solution: Pass the compiled RecordMetadata from view creation directly to createViewState(), so proper column info is hydrated immediately.

Changes

  • ViewCompilerJob.java: Add stale update timestamp check
  • ViewStateStore.java: Add createViewState(ViewDefinition, RecordMetadata) overload
  • ViewStateStoreImpl.java: Use passed metadata to create proper ViewMetadata
  • ViewState.java: Add constructor accepting ViewMetadata
  • CairoEngine.java: Pass metadata to createView()
  • SqlCompilerImpl.java: Pass factory metadata during view creation

Test plan

  • ViewInvalidationTest - all 12 tests pass
  • ViewsFunctionTest.testShowCreateView - passes
  • ViewReplicationTest.testReplicateTableDropInvalidateViewState - originally failing test
  • Full view test suite

🤖 Generated with Claude Code

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 12, 2026

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@bluestreak01 bluestreak01 changed the title fix(cairo): fix race condition in ViewCompilerJob causing incorrect view state on replica fix(sql): fix race condition in ViewCompilerJob causing incorrect view state on replica Jan 12, 2026
@bluestreak01 bluestreak01 changed the title fix(sql): fix race condition in ViewCompilerJob causing incorrect view state on replica fix(view): fix view metadata race conditions on replica Jan 12, 2026
@bluestreak01 bluestreak01 changed the title fix(view): fix view metadata race conditions on replica fix(sql): fix view metadata race conditions on replica Jan 12, 2026
@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 21 / 23 (91.30%)

file detail

path covered line new line coverage
🔵 io/questdb/cairo/view/ViewState.java 2 4 50.00%
🔵 io/questdb/cairo/view/ViewStateStoreImpl.java 4 4 100.00%
🔵 io/questdb/cairo/view/ViewStateStore.java 2 2 100.00%
🔵 io/questdb/cairo/view/ViewCompilerJob.java 12 12 100.00%
🔵 io/questdb/cairo/CairoEngine.java 1 1 100.00%

@bluestreak01 bluestreak01 merged commit 3a5f01d into master Jan 12, 2026
40 of 43 checks passed
@bluestreak01 bluestreak01 deleted the vi_fix_view branch January 12, 2026 13:12
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.

2 participants