Repository navigation
fix(txn,search): route predicate writes outside a block, refuse two writes of one sum balance, refuse the doc_id score alias #426
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
Draft
EnRaiha
wants to merge
8
commits into
NodeDB-Lab:main
Choose a base branch
from
EnRaiha:fix/fusion-and-txn-bulk-on-d1e957217
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ee1613
fix(txn): refuse several writes of one sum balance before a route is …
EnRaiha 40124aa
fix(txn): run a request of several predicate writes as one implicit t…
EnRaiha eb0c9b2
fix(search): refuse a hybrid score alias named doc_id
EnRaiha 55fb680
fix(txn): carry settlement reads into the recon transaction
EnRaiha 29047a3
fix(txn): carry settlement reads into the pgwire implicit edge recon
EnRaiha 468ac53
test(wire): TRUNCATE of a cross-shard sum source keeps its balance
EnRaiha a33b6df
fix(txn): TRUNCATE carries settled balance tasks and reads
EnRaiha cfe5152
test(txn): cover native settlement reads in recon
EnRaiha 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
Large diffs are not rendered by default.
Oops, something went wrong.
175 changes: 175 additions & 0 deletions
175
nodedb/src/control/planner/calvin/dependent_recon_bulks.rs
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,175 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //! The predicate writes of one dependent submission. | ||
| //! | ||
| //! A submission can hold several `BulkUpdate`/`BulkDelete` tasks, for example | ||
| //! the statements of one request. Each task has its own collection, filters | ||
| //! and edge lifecycle, so each gets its own reconnaissance scan and carries | ||
| //! only the rows and edges that scan found. | ||
|
|
||
| use std::collections::BTreeMap; | ||
|
|
||
| use nodedb_physical::physical_plan::OllpPredictedEdge; | ||
| use nodedb_physical::physical_task::PhysicalTask; | ||
|
|
||
| use crate::control::planner::calvin::is_dependent_predicate; | ||
| use crate::control::planner::calvin::preexec::PreexecScan; | ||
| use crate::control::state::SharedState; | ||
| use crate::types::{DatabaseId, TenantId}; | ||
|
|
||
| use super::dependent_recon_node_edges::reconnoitre; | ||
| use super::dependent_recon_plan::{inject_ollp_predicted_edges, inject_ollp_surrogates}; | ||
| use super::dependent_recon_predicate::{ | ||
| EdgeLifecycle, classify_edge_lifecycle, extract_bulk_predicate_info, | ||
| }; | ||
|
|
||
| /// What one predicate write carries into the submission: the rows it matched | ||
| /// and the implicit edges of those rows. | ||
| pub(super) type BulkPrediction = (Vec<u32>, Vec<OllpPredictedEdge>); | ||
|
|
||
| /// One predicate write of a submission and the inputs of its reconnaissance. | ||
| /// They are constant across retries. | ||
| pub(super) struct BulkRecon { | ||
| /// Position of the task in the submission's task list. | ||
| pub(super) index: usize, | ||
| pub(super) collection: String, | ||
| pub(super) filters: Vec<u8>, | ||
| pub(super) edge_mode: EdgeLifecycle, | ||
| } | ||
|
|
||
| impl BulkRecon { | ||
| /// Every predicate write of `tasks`, in task order. | ||
| pub(super) fn of(tasks: &[PhysicalTask]) -> crate::Result<Vec<Self>> { | ||
| tasks | ||
| .iter() | ||
| .enumerate() | ||
| .filter(|(_, task)| is_dependent_predicate(&task.plan)) | ||
| .map(|(index, task)| { | ||
| let (collection, filters) = extract_bulk_predicate_info(&task.plan); | ||
| Ok(Self { | ||
| index, | ||
| collection, | ||
| filters, | ||
| edge_mode: classify_edge_lifecycle(&task.plan)?, | ||
| }) | ||
| }) | ||
| .collect() | ||
| } | ||
| } | ||
|
|
||
| /// The reconnaissance scan of every predicate write, in the order of `bulks`. | ||
| pub(super) async fn reconnoitre_all( | ||
| state: &SharedState, | ||
| tenant_id: TenantId, | ||
| database_id: DatabaseId, | ||
| bulks: &[BulkRecon], | ||
| ) -> crate::Result<Vec<PreexecScan>> { | ||
| let mut scans = Vec::with_capacity(bulks.len()); | ||
| for bulk in bulks { | ||
| scans.push( | ||
| reconnoitre( | ||
| state, | ||
| tenant_id, | ||
| database_id, | ||
| &bulk.collection, | ||
| bulk.filters.clone(), | ||
| &bulk.edge_mode, | ||
| ) | ||
| .await?, | ||
| ); | ||
| } | ||
| Ok(scans) | ||
| } | ||
|
|
||
| /// A copy of `tasks` in which the predicate write at each index of | ||
| /// `predictions` carries its own rows and edges. | ||
| pub(super) fn with_bulk_predictions( | ||
| tasks: &[PhysicalTask], | ||
| predictions: &BTreeMap<usize, BulkPrediction>, | ||
| ) -> Vec<PhysicalTask> { | ||
| let mut out = tasks.to_vec(); | ||
| for (index, (surrogates, edges)) in predictions { | ||
| if let Some(task) = out.get_mut(*index) { | ||
| inject_ollp_surrogates(&mut task.plan, surrogates.clone()); | ||
| inject_ollp_predicted_edges(&mut task.plan, edges.clone()); | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use nodedb_physical::physical_plan::{DocumentOp, PhysicalPlan}; | ||
|
|
||
| fn bulk_delete(collection: &str) -> PhysicalTask { | ||
| PhysicalTask { | ||
| tenant_id: TenantId::new(1), | ||
| vshard_id: crate::types::VShardId::new(0), | ||
| database_id: DatabaseId::DEFAULT, | ||
| plan: PhysicalPlan::Document(DocumentOp::BulkDelete { | ||
| collection: nodedb_types::QualifiedCollection::new(DatabaseId::DEFAULT, collection), | ||
| filters: Vec::new(), | ||
| returning: None, | ||
| ollp_predicted_surrogates: None, | ||
| ollp_predicted_edges: None, | ||
| rls_filters: Vec::new(), | ||
| rls_write_check: nodedb_types::RlsWriteCheck::pending_injection(), | ||
| resolved_sum_targets: Vec::new(), | ||
| declared_primary_key: None, | ||
| }), | ||
| post_set_op: nodedb_physical::physical_task::PostSetOp::None, | ||
| txn_id: None, | ||
| } | ||
| } | ||
|
|
||
| fn carried(task: &PhysicalTask) -> (Option<Vec<u32>>, Option<Vec<OllpPredictedEdge>>) { | ||
| match &task.plan { | ||
| PhysicalPlan::Document(DocumentOp::BulkDelete { | ||
| ollp_predicted_surrogates, | ||
| ollp_predicted_edges, | ||
| .. | ||
| }) => ( | ||
| ollp_predicted_surrogates.clone(), | ||
| ollp_predicted_edges.clone(), | ||
| ), | ||
| _ => (None, None), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn each_predicate_write_receives_its_own_prediction() { | ||
| let edge = OllpPredictedEdge { | ||
| surrogate: 3, | ||
| from: "a".into(), | ||
| to: "z".into(), | ||
| label: None, | ||
| }; | ||
| let tasks = vec![bulk_delete("orders"), bulk_delete("users")]; | ||
| let predictions = BTreeMap::from([ | ||
| (0, (vec![3, 5], vec![edge.clone()])), | ||
| (1, (vec![9], Vec::new())), | ||
| ]); | ||
|
|
||
| let out = with_bulk_predictions(&tasks, &predictions); | ||
|
|
||
| assert_eq!(carried(&out[0]), (Some(vec![3, 5]), Some(vec![edge]))); | ||
| assert_eq!(carried(&out[1]), (Some(vec![9]), Some(Vec::new()))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_predicate_write_is_found_at_its_task_position() { | ||
| let tasks = vec![bulk_delete("orders"), bulk_delete("users")]; | ||
|
|
||
| let bulks = BulkRecon::of(&tasks).expect("classify the predicate writes"); | ||
|
|
||
| let found: Vec<_> = bulks | ||
| .iter() | ||
| .map(|bulk| (bulk.index, bulk.collection.as_str())) | ||
| .collect(); | ||
| assert_eq!(found.len(), 2); | ||
| assert_eq!(found[0].0, 0); | ||
| assert_eq!(found[1].0, 1); | ||
| assert!(found[0].1.ends_with("orders") && found[1].1.ends_with("users")); | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| //! The request guard for writes that move one materialized-sum balance. | ||
|
|
||
| use std::collections::BTreeSet; | ||
|
|
||
| use nodedb_physical::physical_task::PhysicalTask; | ||
|
|
||
| use super::is_dependent_predicate; | ||
| use crate::Error; | ||
|
|
||
| /// Refuse a request in which a predicate write and another write move the | ||
| /// same materialized-sum balance row. Each write settles its delta from the | ||
| /// committed rows, so a later write cannot see the rows an earlier one | ||
| /// changes and the balance would drift from the sum of the source. | ||
| pub fn refuse_several_settling_writes(tasks: &[PhysicalTask]) -> crate::Result<()> { | ||
| use nodedb_physical::physical_plan::{DocumentOp, PhysicalPlan}; | ||
| if !tasks.iter().any(|task| is_dependent_predicate(&task.plan)) { | ||
| return Ok(()); | ||
| } | ||
| let mut targets = BTreeSet::new(); | ||
| for task in tasks { | ||
| if let PhysicalPlan::Document(DocumentOp::ApplyBalanceDelta { | ||
| collection, | ||
| document_id, | ||
| .. | ||
| }) = &task.plan | ||
| && !targets.insert((collection.to_string(), document_id.clone())) | ||
| { | ||
| return Err(Error::BadRequest { | ||
| detail: "several writes of one request move the same materialized-sum balance \ | ||
| and one is a predicate write; run them as separate statements or in a \ | ||
| transaction block" | ||
| .to_owned(), | ||
| }); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use nodedb_physical::physical_plan::{DocumentOp, PhysicalPlan}; | ||
|
|
||
| use crate::types::{DatabaseId, TenantId, VShardId}; | ||
|
|
||
| fn task(plan: PhysicalPlan) -> PhysicalTask { | ||
| PhysicalTask { | ||
| tenant_id: TenantId::new(1), | ||
| vshard_id: VShardId::new(0), | ||
| database_id: DatabaseId::DEFAULT, | ||
| plan, | ||
| post_set_op: nodedb_physical::physical_task::PostSetOp::None, | ||
| txn_id: None, | ||
| } | ||
| } | ||
|
|
||
| fn bulk_delete(collection: &str) -> PhysicalTask { | ||
| task(PhysicalPlan::Document(DocumentOp::BulkDelete { | ||
| collection: nodedb_types::QualifiedCollection::new(DatabaseId::DEFAULT, collection), | ||
| filters: Vec::new(), | ||
| returning: None, | ||
| ollp_predicted_surrogates: None, | ||
| ollp_predicted_edges: None, | ||
| rls_filters: Vec::new(), | ||
| rls_write_check: nodedb_types::RlsWriteCheck::pending_injection(), | ||
| resolved_sum_targets: Vec::new(), | ||
| declared_primary_key: None, | ||
| })) | ||
| } | ||
|
|
||
| #[test] | ||
| fn plain_predicate_writes_are_not_refused() { | ||
| let tasks = vec![bulk_delete("orders"), bulk_delete("users")]; | ||
| assert!(refuse_several_settling_writes(&tasks).is_ok()); | ||
| } | ||
| } |
Oops, something went wrong.
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.
[P1] Predict later statements against preceding writes
Every recon scan reads committed state with
txn_id: None, before any batch statement applies. Sequentially awaiting these scans does not expose preceding writes.For an edge-bearing collection containing
n=1, submit this HTTP SQL batch without joined triggers:The implicit-transaction gate does not fire for this pair of ordinary bulk updates. Recon predicts
[row]for the first statement and[]for the second. Calvin applies those fixed sets and reports success withn=2. The second update silently disappears.Compute each prediction against the preceding statements' staged writes. Cover dependent predicates in one collection, alongside the existing independent two-collection case.