Skip to content

Commit 6dac46e

Browse files
nipunn1313Convex, Inc.
authored andcommitted
Toggle the staged state if the dev config switches staged on the fly (#39997)
Example scenario 1. dev pushes with {staged:true} 2. Before backfilling completes, dev pushes without staged Here we want to toggle the backfilling state without restarting the backfill, thus ensuring that the CLI blocks until its completed. GitOrigin-RevId: 78a421b63cbf82e4ac9bba78b5dd602ec71a0255
1 parent 8c5640e commit 6dac46e

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

crates/common/src/bootstrap_model/index/database_index/index_state.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ impl DatabaseIndexState {
3535
Self::Enabled => false,
3636
}
3737
}
38+
39+
pub fn set_staged(&mut self, staged_new: bool) {
40+
match self {
41+
Self::Backfilling(index_state) => {
42+
index_state.staged = staged_new;
43+
},
44+
Self::Backfilled { staged } => {
45+
*staged = staged_new;
46+
},
47+
Self::Enabled => {},
48+
}
49+
}
3850
}
3951

4052
#[derive(Serialize, Deserialize)]

crates/common/src/bootstrap_model/index/index_config.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ pub enum IndexConfig {
5757
impl IndexConfig {
5858
pub fn is_staged(&self) -> bool {
5959
match self {
60-
Self::Database {
61-
developer_config: _,
62-
on_disk_state,
63-
} => on_disk_state.is_staged(),
64-
Self::Text {
65-
developer_config: _,
66-
on_disk_state,
67-
} => on_disk_state.is_staged(),
68-
Self::Vector {
69-
developer_config: _,
70-
on_disk_state,
71-
} => on_disk_state.is_staged(),
60+
Self::Database { on_disk_state, .. } => on_disk_state.is_staged(),
61+
Self::Text { on_disk_state, .. } => on_disk_state.is_staged(),
62+
Self::Vector { on_disk_state, .. } => on_disk_state.is_staged(),
63+
}
64+
}
65+
66+
pub fn set_staged(&mut self, staged: bool) {
67+
match self {
68+
Self::Database { on_disk_state, .. } => on_disk_state.set_staged(staged),
69+
Self::Text { on_disk_state, .. } => on_disk_state.set_staged(staged),
70+
Self::Vector { on_disk_state, .. } => on_disk_state.set_staged(staged),
7271
}
7372
}
7473

crates/common/src/bootstrap_model/index/text_index/index_state.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ impl TextIndexState {
3838
Self::SnapshottedAt(_) => false,
3939
}
4040
}
41+
42+
pub fn set_staged(&mut self, staged_new: bool) {
43+
match self {
44+
Self::Backfilling(index_state) => {
45+
index_state.staged = staged_new;
46+
},
47+
Self::Backfilled { staged, .. } => {
48+
*staged = staged_new;
49+
},
50+
Self::SnapshottedAt(_) => {},
51+
}
52+
}
4153
}
4254

4355
#[derive(Serialize, Deserialize)]

crates/common/src/bootstrap_model/index/vector_index/index_state.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ impl VectorIndexState {
3737
}
3838
}
3939

40+
pub fn set_staged(&mut self, staged_new: bool) {
41+
match self {
42+
VectorIndexState::Backfilling(backfill_state) => {
43+
backfill_state.staged = staged_new;
44+
},
45+
VectorIndexState::Backfilled { staged, .. } => {
46+
*staged = staged_new;
47+
},
48+
VectorIndexState::SnapshottedAt(_) => {},
49+
}
50+
}
51+
4052
pub fn segments(&self) -> anyhow::Result<&Vec<FragmentedVectorSegment>> {
4153
match self {
4254
VectorIndexState::Backfilling(backfill_state) => Ok(&backfill_state.segments),

crates/database/src/bootstrap_model/index.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
602602

603603
fn identical_or_replaced(
604604
&mut self,
605-
existing_index: ParsedDocument<DeveloperIndexMetadata>,
605+
mut existing_index: ParsedDocument<DeveloperIndexMetadata>,
606606
new_index: DeveloperIndexMetadata,
607607
) -> IndexComparison {
608608
let existing_fields = DeveloperIndexConfig::from(existing_index.config.clone());
@@ -612,12 +612,13 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
612612
IndexComparison::Identical(existing_index)
613613
} else {
614614
// Staged status changed - use the previous on-disk state
615-
// staged -> enabled
615+
// Toggle the staged status and return enabled/disabled to
616+
// inform callsite.
616617
if existing_index.config.is_staged() {
617-
// Should we modify the index's staged flag here or when we actually go to
618-
// change its state?
618+
existing_index.config.set_staged(false);
619619
IndexComparison::Enabled(existing_index)
620620
} else {
621+
existing_index.config.set_staged(true);
621622
IndexComparison::Disabled(existing_index)
622623
}
623624
}
@@ -708,6 +709,18 @@ impl<'a, RT: Runtime> IndexModel<'a, RT> {
708709
for index in &diff.added {
709710
self.add_application_index(namespace, index.clone()).await?;
710711
}
712+
713+
// Replace all the enabled/disabled ones in-place to update their `staged`
714+
// status. The actual enabling/disabling will be done at finish_push
715+
// (apply_config).
716+
for index in diff.enabled.iter().chain(diff.disabled.iter()) {
717+
let (id, value) = index.clone().into_id_and_value();
718+
let new_config = value.config.try_into()?;
719+
SystemMetadataModel::new_global(self.tx)
720+
.patch(id, patch_value!("config" => Some(new_config))?)
721+
.await?;
722+
}
723+
711724
Ok(diff)
712725
}
713726

0 commit comments

Comments
 (0)