Skip to content

Commit 7d8cb15

Browse files
authored
Remove updater lock from instance runtime state (#5890)
1 parent a8b3ce2 commit 7d8cb15

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

nexus/db-model/src/instance.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ pub struct Instance {
5858

5959
#[diesel(embed)]
6060
pub runtime_state: InstanceRuntimeState,
61+
62+
/// A UUID identifying the saga currently holding the update lock on this
63+
/// instance. If this is [`None`] the instance is not locked. Otherwise, if
64+
/// this is [`Some`], the instance is locked by the saga owning this UUID.
65+
/// Note that this is not (presently) the UUID *of* the locking saga, but
66+
/// rather, a UUID *generated by* that saga. Therefore, it may not be
67+
/// useable to look up which saga holds the lock.
68+
///
69+
/// This field is guarded by the instance's `updater_gen`
70+
#[diesel(column_name = updater_id)]
71+
pub updater_id: Option<Uuid>,
72+
73+
/// The generation number for the updater lock. This is updated whenever the
74+
/// lock is acquired or released, and is used in attempts to set the
75+
/// `updater_id` field to ensure that the snapshot which indicated that the
76+
/// lock was not held is still valid when setting the lock ID.
77+
#[diesel(column_name = updater_gen)]
78+
pub updater_gen: Generation,
6179
}
6280

6381
impl Instance {
@@ -85,6 +103,9 @@ impl Instance {
85103
hostname: params.hostname.to_string(),
86104
boot_on_fault: false,
87105
runtime_state,
106+
107+
updater_gen: Generation::new(),
108+
updater_id: None,
88109
}
89110
}
90111

@@ -171,24 +192,6 @@ pub struct InstanceRuntimeState {
171192
#[diesel(column_name = migration_id)]
172193
pub migration_id: Option<Uuid>,
173194

174-
/// A UUID identifying the saga currently holding the update lock on this
175-
/// instance. If this is [`None`] the instance is not locked. Otherwise, if
176-
/// this is [`Some`], the instance is locked by the saga owning this UUID.
177-
/// Note that this is not (presently) the UUID *of* the locking saga, but
178-
/// rather, a UUID *generated by* that saga. Therefore, it may not be
179-
/// useable to look up which saga holds the lock.
180-
///
181-
/// This field is guarded by the instance's `updater_gen`
182-
#[diesel(column_name = updater_id)]
183-
pub updater_id: Option<Uuid>,
184-
185-
/// The generation number for the updater lock. This is updated whenever the
186-
/// lock is acquired or released, and is used in attempts to set the
187-
/// `updater_id` field to ensure that the snapshot which indicated that the
188-
/// lock was not held is still valid when setting the lock ID.
189-
#[diesel(column_name = updater_gen)]
190-
pub updater_gen: Generation,
191-
192195
/// The "internal" state of this instance. The instance's externally-visible
193196
/// state may be delegated to the instance's active VMM, if it has one.
194197
///
@@ -206,8 +209,6 @@ impl InstanceRuntimeState {
206209
dst_propolis_id: None,
207210
migration_id: None,
208211
gen: Generation::new(),
209-
updater_gen: Generation::new(),
210-
updater_id: None,
211212
}
212213
}
213214
}
@@ -231,8 +232,6 @@ impl From<omicron_common::api::internal::nexus::InstanceRuntimeState>
231232
propolis_id: state.propolis_id,
232233
dst_propolis_id: state.dst_propolis_id,
233234
migration_id: state.migration_id,
234-
updater_gen: Generation::new(),
235-
updater_id: None,
236235
}
237236
}
238237
}

nexus/db-model/src/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ table! {
430430
active_propolis_id -> Nullable<Uuid>,
431431
target_propolis_id -> Nullable<Uuid>,
432432
migration_id -> Nullable<Uuid>,
433+
state -> crate::InstanceStateEnum,
433434
updater_id -> Nullable<Uuid>,
434435
updater_gen-> Int8,
435-
state -> crate::InstanceStateEnum,
436436
}
437437
}
438438

nexus/db-queries/src/db/datastore/instance.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ impl DataStore {
753753
// important than handling that extremely unlikely edge case.
754754
let mut did_lock = false;
755755
loop {
756-
match instance.runtime_state.updater_id {
756+
match instance.updater_id {
757757
// If the `updater_id` field is not null and the ID equals this
758758
// saga's ID, we already have the lock. We're done here!
759759
Some(lock_id) if lock_id == saga_lock_id => {
@@ -766,7 +766,7 @@ impl DataStore {
766766
);
767767
return Ok(UpdaterLock {
768768
saga_lock_id,
769-
locked_gen: instance.runtime_state.updater_gen,
769+
locked_gen: instance.updater_gen,
770770
});
771771
}
772772
// The `updater_id` field is set, but it's not our ID. The instance
@@ -787,7 +787,7 @@ impl DataStore {
787787
}
788788

789789
// Okay, now attempt to acquire the lock
790-
let current_gen = instance.runtime_state.updater_gen;
790+
let current_gen = instance.updater_gen;
791791
slog::debug!(
792792
&opctx.log,
793793
"attempting to acquire instance updater lock";
@@ -902,12 +902,12 @@ impl DataStore {
902902
UpdateAndQueryResult {
903903
status: UpdateStatus::NotUpdatedButExists,
904904
ref found,
905-
} if found.runtime_state.updater_gen > locked_gen => Ok(false),
905+
} if found.updater_gen > locked_gen => Ok(false),
906906
// The instance exists, but the lock ID doesn't match our lock ID.
907907
// This means we were trying to release a lock we never held, whcih
908908
// is almost certainly a programmer error.
909909
UpdateAndQueryResult { ref found, .. } => {
910-
match found.runtime_state.updater_id {
910+
match found.updater_id {
911911
Some(lock_holder) => {
912912
debug_assert_ne!(lock_holder, saga_lock_id);
913913
Err(Error::internal_error(

0 commit comments

Comments
 (0)