Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit 109cf9f

Browse files
author
Zachary Hamm
committed
feat(*): Apply to head happens in rebaser
Currently we handle applies to head by calculating the set of updates in the current change set with respect to HEAD on the client side (e.g., in the rebaser or luminork) and then write a change set batch of those updates and send it to the rebaser. This is the wrong way to do it: with concurrent users, the set of updates calculated could easily be out of date by the time they reach the rebaser. Both HEAD and the change set we are updating could have moved forward, making our update set incomplete or incorrect. Further, we move the change set's status to applied on the client side as well, opening up further possibilities for races in a multiplayer setting. To fix this, we have moved the apply to head into the rebaser in a two phase commit approach: 1. First, on the request stream for the change set being applied, we send a request to prepare the change set for apply. This first locks all schemas and funcs that may have been in an editing state, writes the updated workspace snapshot, and then marks the change set with the "ApplyStarted" status. This prevents further updates from happening on the the change set since it is no longer "open". 2. Next, on the request stream for head, we request an apply to head. This calculates the update batch for an apply and performs the rebase, then moves the change set status to "Applied" if successful. If unsuccessful, the change set status will be reset to what it was when the prepare request was issued above, so that change sets are not left in "ApplyStarted" after an apply has failed. This ensures that updates to the change set, or to head, cannot race an "apply", and that updates that land to a change set *after* an apply has started will be rejected (since they are no longer up to date). Also added here are request handlers for change set status changes and change set creation. The requests for these have not yet been integrated into the rebaser client, but they will similarly ensure that change set creation and status updates are handled in the correct order in multiplayer scenarios.
1 parent d4c1bbf commit 109cf9f

28 files changed

Lines changed: 1684 additions & 412 deletions

File tree

‎lib/acceptable-macros/src/versioned.rs‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use proc_macro2::TokenStream as TokenStream2;
88
use quote::quote;
99
use syn::{
1010
Data,
11+
DataEnum,
1112
DataStruct,
1213
DeriveInput,
14+
Fields,
1315
Ident,
1416
};
1517

@@ -28,8 +30,9 @@ pub(crate) fn expand(input: TokenStream, errors: &mut manyhow::Emitter) -> Resul
2830

2931
match data {
3032
Data::Struct(data_struct) => derive_from_struct(ident, data_struct, config, errors),
31-
Data::Enum(_) | Data::Union(_) => {
32-
bail!("current only struct types are supported for `#[derive(Versioned)]`")
33+
Data::Enum(data_enum) => derive_from_enum(ident, data_enum, config, errors),
34+
Data::Union(_) => {
35+
bail!("union types are not supported for `#[derive(Versioned)]`")
3336
}
3437
}
3538
}
@@ -64,3 +67,56 @@ fn derive_from_struct(
6467
}
6568
})
6669
}
70+
71+
fn derive_from_enum(
72+
ident: Ident,
73+
data: DataEnum,
74+
config: Config,
75+
_errors: &mut manyhow::Emitter,
76+
) -> Result<TokenStream2> {
77+
let version = config.version;
78+
79+
// Verify that every variant has an `id` field
80+
let mut match_arms = Vec::new();
81+
for variant in &data.variants {
82+
let variant_ident = &variant.ident;
83+
84+
let has_id_field = match &variant.fields {
85+
Fields::Named(fields) => fields
86+
.named
87+
.iter()
88+
.filter_map(|field| field.ident.as_ref())
89+
.any(|ident| ident == "id"),
90+
Fields::Unnamed(_) => false,
91+
Fields::Unit => false,
92+
};
93+
94+
if !has_id_field {
95+
bail!(
96+
variant_ident,
97+
"enum variant `{}` must have an `id` field for versioning",
98+
variant_ident
99+
);
100+
}
101+
102+
match_arms.push(quote! {
103+
Self::#variant_ident { id, .. } => (*id).into()
104+
});
105+
}
106+
107+
Ok(quote! {
108+
impl acceptable::Versioned for #ident {
109+
#[inline]
110+
fn id(&self) -> acceptable::RequestId {
111+
match self {
112+
#(#match_arms),*
113+
}
114+
}
115+
116+
#[inline]
117+
fn message_version() -> u64 {
118+
#version
119+
}
120+
}
121+
})
122+
}

‎lib/dal-test/src/helpers/change_set.rs‎

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ use dal::{
1111
ChangeSetId,
1212
ComponentId,
1313
DalContext,
14-
Func,
15-
Schema,
16-
SchemaVariant,
1714
Ulid,
1815
action::{
1916
Action,
2017
ActionState,
2118
dependency_graph::ActionDependencyGraph,
2219
},
2320
diagram::view::View,
24-
workspace_snapshot::selector::WorkspaceSnapshotSelectorDiscriminants,
21+
workspace_snapshot::{
22+
DependentValueRoot,
23+
selector::WorkspaceSnapshotSelectorDiscriminants,
24+
},
2525
};
2626
use si_db::{
2727
ManagementFuncJobState,
@@ -73,19 +73,17 @@ impl ChangeSetTestHelpers {
7373
pub async fn commit_and_update_snapshot_to_visibility(ctx: &mut DalContext) -> Result<()> {
7474
// The rebaser has responsibility for executing dvu jobs, so we should
7575
// prevent them from running in tests
76-
let has_roots = ctx
77-
.txns()
76+
ctx.txns()
7877
.await?
7978
.job_queue()
8079
.clear_dependent_values_jobs()
8180
.await;
8281
ctx.blocking_commit().await?;
82+
ctx.update_snapshot_to_visibility().await?;
8383

8484
// But we have to wait until the dvu jobs complete
85-
if has_roots {
85+
if DependentValueRoot::roots_exist(ctx).await? {
8686
ChangeSet::wait_for_dvu(ctx, false).await?;
87-
} else {
88-
ctx.update_snapshot_to_visibility().await?;
8987
}
9088

9189
Ok(())
@@ -122,13 +120,6 @@ impl ChangeSetTestHelpers {
122120
))
123121
}
124122

125-
/// Apply Changeset To base Approvals
126-
pub async fn apply_change_set_to_base_approvals(ctx: &mut DalContext) -> Result<()> {
127-
ChangeSet::prepare_for_apply(ctx).await?;
128-
Self::apply_change_set_to_base_approvals_without_prepare_step(ctx).await?;
129-
Ok(())
130-
}
131-
132123
/// Switch to the [`ChangeSet`] corresponding to the provided ID.
133124
pub async fn switch_to_change_set(
134125
ctx: &mut DalContext,
@@ -196,23 +187,17 @@ impl ChangeSetTestHelpers {
196187

197188
/// Force Apply Changeset To base Approvals
198189
pub async fn force_apply_change_set_to_base_approvals(ctx: &mut DalContext) -> Result<()> {
199-
ChangeSet::prepare_for_force_apply(ctx).await?;
200-
Self::apply_change_set_to_base_approvals_without_prepare_step(ctx).await?;
201-
Ok(())
190+
ChangeSet::force_change_set_approval(ctx).await?;
191+
Self::commit_and_update_snapshot_to_visibility(ctx).await?;
192+
Self::apply_change_set_to_base_approvals_with_status_check(ctx).await?;
193+
Ok(()) //
202194
}
203195

204-
/// Apply the change set to base for the approvals flow, but without performing the "prepare"
205-
/// step.
206-
///
207-
/// _Warning:_ if you do not know what to choose, use [Self::force_apply_change_set_to_base_approvals]
208-
/// or [Self::apply_change_set_to_base_approvals] instead of this function. This function
209-
/// should only be used if you have an alternative "prepare" workflow (e.g. testing fine grained
210-
/// access control in sdf tests).
211-
pub async fn apply_change_set_to_base_approvals_without_prepare_step(
196+
/// Apply changeset to head, but only if Approved
197+
pub async fn apply_change_set_to_base_approvals_with_status_check(
212198
ctx: &mut DalContext,
213199
) -> Result<()> {
214-
Self::commit_and_update_snapshot_to_visibility(ctx).await?;
215-
Self::apply_change_set_to_base_inner(ctx).await?;
200+
Self::apply_change_set_to_base_inner(ctx, false, true).await?;
216201
Ok(())
217202
}
218203

@@ -233,16 +218,44 @@ impl ChangeSetTestHelpers {
233218
)
234219
}
235220

236-
/// Applies the current change set to the base change set, waiting for replays to land on any open change sets.
237-
pub async fn apply_change_set_to_base_inner(ctx: &mut DalContext) -> Result<bool> {
221+
/// Applies the current change set to the base change set, waiting for
222+
/// replays to land on any open change sets.
223+
pub async fn apply_change_set_to_base_inner(
224+
ctx: &mut DalContext,
225+
skip_status_check: bool,
226+
lock_schemas_and_funcs: bool,
227+
) -> Result<bool> {
238228
let mut open_change_sets = ChangeSet::list_active(ctx)
239229
.await?
240230
.iter()
241231
.map(|change_set| (change_set.id, change_set.updated_at))
242232
.collect::<Vec<(_, _)>>();
243233

244234
let had_updates = Self::has_updates(ctx).await?;
245-
let applied_change_set = ChangeSet::apply_to_base_change_set(ctx).await?;
235+
let applied_change_set = ChangeSet::get_by_id(ctx, ctx.change_set_id()).await?;
236+
237+
let previous_change_set_status = if lock_schemas_and_funcs {
238+
if skip_status_check {
239+
ChangeSet::prepare_for_apply_without_status_check(ctx).await?
240+
} else {
241+
ChangeSet::prepare_for_apply(ctx).await?
242+
}
243+
} else {
244+
// We only skip locking schemas/funcs in tests
245+
if skip_status_check {
246+
ChangeSet::prepare_for_apply_no_lock_no_status_check(ctx).await?
247+
} else {
248+
ChangeSet::prepare_for_apply_no_lock(ctx).await?
249+
}
250+
};
251+
252+
// Now that applies are rejected if DVU is running on HEAD, we must wait
253+
// for DVU on head to finish here
254+
let mut head_ctx = ctx.clone_with_head().await?;
255+
ChangeSet::wait_for_dvu(&mut head_ctx, false).await?;
256+
257+
ChangeSet::apply_to_base_change_set(ctx, previous_change_set_status).await?;
258+
246259
ctx.update_visibility_and_snapshot_to_visibility(
247260
applied_change_set.base_change_set_id.ok_or(eyre!(
248261
"base change set not found for change set: {}",
@@ -282,35 +295,9 @@ impl ChangeSetTestHelpers {
282295
}
283296

284297
/// Applies the current [`ChangeSet`] to its base [`ChangeSet`], leaving ctx pointed at HEAD.
285-
/// 1. Prepares the apply by locking unlocked variants.
286-
/// 2. Prepares the apply by Then, it updates the snapshot
287-
/// to the visibility without using an editing [`ChangeSet`]. In other words, the resulting,
288-
/// snapshot is "HEAD" without an editing [`ChangeSet`].
289-
///
290-
/// Also locks existing editing funcs and schema variants to mimic SDF
291298
pub async fn apply_change_set_to_base(ctx: &mut DalContext) -> Result<bool> {
292-
// Lock all unlocked variants
293-
for schema_id in Schema::list_ids(ctx).await? {
294-
let schema = Schema::get_by_id(ctx, schema_id).await?;
295-
let Some(variant) = SchemaVariant::get_unlocked_for_schema(ctx, schema_id).await?
296-
else {
297-
continue;
298-
};
299-
300-
let variant_id = variant.id();
301-
302-
variant.lock(ctx).await?;
303-
schema.set_default_variant_id(ctx, variant_id).await?;
304-
}
305-
// Lock all unlocked functions too
306-
for func in Func::list_for_default_and_editing(ctx).await? {
307-
if !func.is_locked {
308-
func.lock(ctx).await?;
309-
}
310-
}
311-
312-
ctx.commit().await?;
313-
Self::apply_change_set_to_base_inner(ctx).await
299+
ChangeSetTestHelpers::commit_and_update_snapshot_to_visibility(ctx).await?;
300+
Self::apply_change_set_to_base_inner(ctx, true, true).await
314301
}
315302

316303
/// Abandons the current [`ChangeSet`].

0 commit comments

Comments
 (0)