-
Notifications
You must be signed in to change notification settings - Fork 850
fix: r_cte wrong/flaky results under concurrency #19439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KKould
wants to merge
7
commits into
databendlabs:main
Choose a base branch
from
KKould:fix/r_cte_random_result
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+502
−18
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1676798
fix: r_cte wrong/flaky results under concurrency
KKould 2d01ad3
chore: codefmt
KKould f6e0c6c
Merge branch 'main' into fix/r_cte_random_result
SkyFan2002 3cf409a
chore: codex review comment
KKould 406b4f9
fix: cte repeating prefix
KKould a75dbe6
fix: scope recursive CTE rewrite to local scan names
KKould 24cf459
fix: typo?
KKould File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,4 @@ pub use config::config_with_spill; | |
| pub use context::*; | ||
| pub use fixture::*; | ||
| pub use fuse::*; | ||
| pub mod rcte_hooks; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| // Copyright 2021 Datafuse Labs | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Test-only hooks for recursive CTE execution. | ||
| //! | ||
| //! This module is intended to make race conditions reproducible by providing | ||
| //! deterministic pause/resume points in the recursive CTE executor. | ||
| //! | ||
| //! By default no hooks are installed and the hook checks are no-ops. | ||
|
|
||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use std::sync::Mutex; | ||
| use std::sync::OnceLock; | ||
| use std::sync::atomic::AtomicUsize; | ||
| use std::sync::atomic::Ordering; | ||
|
|
||
| use tokio::sync::Notify; | ||
|
|
||
| static HOOKS: OnceLock<Arc<RcteHookRegistry>> = OnceLock::new(); | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
| struct GateKey { | ||
| query_id: String, | ||
| step: usize, | ||
| } | ||
|
|
||
| impl GateKey { | ||
| fn new(query_id: &str, step: usize) -> Self { | ||
| Self { | ||
| query_id: query_id.to_string(), | ||
| step, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub struct RcteHookRegistry { | ||
| gates: Mutex<HashMap<GateKey, Arc<PauseGate>>>, | ||
| } | ||
|
|
||
| impl RcteHookRegistry { | ||
| pub fn global() -> Arc<RcteHookRegistry> { | ||
| HOOKS | ||
| .get_or_init(|| Arc::new(RcteHookRegistry::default())) | ||
| .clone() | ||
| } | ||
|
|
||
| pub fn install_pause_before_step(&self, query_id: &str, step: usize) -> Arc<PauseGate> { | ||
| let mut gates = self.gates.lock().unwrap(); | ||
| let key = GateKey::new(query_id, step); | ||
| gates | ||
| .entry(key) | ||
| .or_insert_with(|| Arc::new(PauseGate::new(step))) | ||
| .clone() | ||
| } | ||
|
|
||
| fn get_gate(&self, query_id: &str, step: usize) -> Option<Arc<PauseGate>> { | ||
| let key = GateKey::new(query_id, step); | ||
| self.gates.lock().unwrap().get(&key).cloned() | ||
| } | ||
| } | ||
|
|
||
| /// A reusable pause gate for a single step number. | ||
| /// | ||
| /// When the code hits the hook point, it increments `arrived` and blocks until | ||
| /// the test releases the same hit index via `release(hit_no)`. | ||
| pub struct PauseGate { | ||
| step: usize, | ||
| arrived: AtomicUsize, | ||
| released: AtomicUsize, | ||
| arrived_notify: Notify, | ||
| released_notify: Notify, | ||
| } | ||
|
|
||
| impl PauseGate { | ||
| fn new(step: usize) -> Self { | ||
| Self { | ||
| step, | ||
| arrived: AtomicUsize::new(0), | ||
| released: AtomicUsize::new(0), | ||
| arrived_notify: Notify::new(), | ||
| released_notify: Notify::new(), | ||
| } | ||
| } | ||
|
|
||
| pub fn step(&self) -> usize { | ||
| self.step | ||
| } | ||
|
|
||
| pub fn arrived(&self) -> usize { | ||
| self.arrived.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| pub async fn wait_arrived_at_least(&self, n: usize) { | ||
| loop { | ||
| let notified = self.arrived_notify.notified(); | ||
| tokio::pin!(notified); | ||
| notified.as_mut().enable(); | ||
|
|
||
| if self.arrived() >= n { | ||
| return; | ||
| } | ||
|
|
||
| // Re-check after registration to avoid missing a notify between | ||
| // condition check and awaiting. | ||
| if self.arrived() >= n { | ||
| return; | ||
| } | ||
|
|
||
| notified.await; | ||
| } | ||
| } | ||
|
|
||
| /// Release the `hit_no`-th arrival (1-based). | ||
| pub fn release(&self, hit_no: usize) { | ||
| // Monotonic release. | ||
| let mut cur = self.released.load(Ordering::Acquire); | ||
| while cur < hit_no { | ||
| match self | ||
| .released | ||
| .compare_exchange(cur, hit_no, Ordering::AcqRel, Ordering::Acquire) | ||
| { | ||
| Ok(_) => break, | ||
| Err(v) => cur = v, | ||
| } | ||
| } | ||
| self.released_notify.notify_waiters(); | ||
| } | ||
|
|
||
| async fn hit(&self) { | ||
| let hit_no = self.arrived.fetch_add(1, Ordering::AcqRel) + 1; | ||
| self.arrived_notify.notify_waiters(); | ||
|
|
||
| loop { | ||
| let notified = self.released_notify.notified(); | ||
| tokio::pin!(notified); | ||
| notified.as_mut().enable(); | ||
|
|
||
| let released = self.released.load(Ordering::Acquire); | ||
| if released >= hit_no { | ||
| return; | ||
| } | ||
|
|
||
| // Re-check after registration to avoid missing a notify between | ||
| // condition check and awaiting. | ||
| let released = self.released.load(Ordering::Acquire); | ||
| if released >= hit_no { | ||
| return; | ||
| } | ||
|
|
||
| notified.await; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Called from the recursive CTE executor. | ||
| /// | ||
| /// If a pause gate is installed for `step`, this call will block until released. | ||
| #[async_backtrace::framed] | ||
| pub async fn maybe_pause_before_step(query_id: &str, step: usize) { | ||
| let Some(registry) = HOOKS.get() else { | ||
| return; | ||
| }; | ||
| let Some(gate) = registry.get_gate(query_id, step) else { | ||
| return; | ||
| }; | ||
| gate.hit().await; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ | |
| mod exec; | ||
| mod expr; | ||
| mod planner; | ||
| mod recursive_cte; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make_rcte_prefixstops after collecting 32 alphanumeric characters, so two concurrent queries whose IDs share the same first 32 alnum chars will still map to the same internal__rcte_*table names. That reintroduces the same cross-query interference this patch is trying to eliminate (wrong/flaky recursive CTE results) for clients that provide custom/long query IDs. Generate the prefix from the full query ID (e.g., full sanitized ID or a hash of it) instead of truncating here.Useful? React with 👍 / 👎.