Skip to content

Commit cc364a8

Browse files
committed
feedback
1 parent 1a1c2dd commit cc364a8

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

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

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ fn skippable_version(
105105
/// Reasons why [DataStore::ensure_schema] might fail
106106
#[derive(thiserror::Error, Debug)]
107107
pub enum EnsureSchemaError {
108-
/// This Nexus is too old to understand the schema
108+
/// This Nexus is too old to use the schema currently in the database,
109+
/// or quiescing has started (meaning that the schema will be updating
110+
/// beyond this Nexus' compatibility very soon).
109111
///
110-
/// This may happen when an old Nexus is booting / rebooting during
111-
/// a handoff to a newer Nexus.
112+
/// Typically, this happens during an upgrade when the older version of
113+
/// Nexus is starting up during the handoff to the newer Nexus.
112114
#[error("Nexus is too old to use this schema; handoff: {handoff}")]
113115
NexusTooOld {
114116
/// Should this Nexus still try to participate in handoff to a newer
@@ -141,8 +143,9 @@ pub enum EnsureSchemaError {
141143
Other(#[from] anyhow::Error),
142144
}
143145

144-
/// Describes whether or not [DataStore::ensure_schema] will proceed
145-
/// with an update.
146+
/// Describes whether or not [DataStore::ensure_schema] should proceed
147+
/// with a schema update if it finds the currently-deployed schema is
148+
/// out-of-date.
146149
pub enum UpdateConfiguration<'a> {
147150
/// The update will not be triggered
148151
Disabled,
@@ -152,12 +155,12 @@ pub enum UpdateConfiguration<'a> {
152155
/// Known schema versions, and ways to upgrade between them
153156
all_versions: &'a AllSchemaVersions,
154157

155-
/// If "true", ignore the quiesce status in the database
158+
/// If "true", proceed with schema updates (if necessary) even when the
159+
/// database state suggests older Nexus instances may have not quiesced.
156160
///
157-
/// This can be used to ignore the work of ongoing Nexuses which may be
158-
/// shutting down, and should only be set to "true" cautiously (e.g.,
159-
/// this is used by the schema-updater binary, which can be used by
160-
/// operators).
161+
/// This should only be set to "true" cautiously (e.g., this is used by
162+
/// the schema-updater binary, which can be used by operators when they
163+
/// know old Nexus instances are not running).
161164
///
162165
/// For most cases, this should be set to "false".
163166
ignore_quiesce: bool,
@@ -483,6 +486,9 @@ impl DataStore {
483486
.await
484487
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?;
485488
if base.version().0 < nexus_db_model::QUIESCE_VERSION {
489+
// TODO(https://github.com/oxidecomputer/omicron/issues/8751):
490+
// Remove this backwards compatibility layer.
491+
//
486492
// Why are we treating this case as "quiesced = true"?
487493
//
488494
// - If Nexus wants to be running on a schema older than
@@ -797,7 +803,7 @@ mod test {
797803
let quiesce_started = true;
798804
let quiesce_completed = true;
799805
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
800-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
806+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
801807

802808
let _ = futures::future::join_all((0..10).map(|_| {
803809
let all_versions = all_versions.clone();
@@ -936,7 +942,7 @@ mod test {
936942
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
937943
let quiesce_started = true;
938944
let quiesce_completed = true;
939-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
945+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
940946
while let Err(e) = datastore
941947
.ensure_schema(
942948
&log,
@@ -973,24 +979,29 @@ mod test {
973979
logctx.cleanup_successful();
974980
}
975981

976-
async fn set_quisece(
982+
async fn set_quiesce(
977983
datastore: &DataStore,
978984
started: bool,
979985
completed: bool,
980986
) {
981987
let conn = datastore.pool_connection_for_tests().await.unwrap();
982-
diesel::dsl::sql::<diesel::sql_types::Integer>(&format!(
983-
"UPDATE omicron.public.db_metadata \
984-
SET \
985-
quiesce_started = {started}, \
986-
quiesce_completed = {completed} \
987-
WHERE singleton = TRUE"
988-
))
989-
.execute_async(&*conn)
990-
.await
991-
.expect("Failed to update metadata");
988+
989+
use nexus_db_schema::schema::db_metadata::dsl;
990+
991+
diesel::update(dsl::db_metadata)
992+
.filter(dsl::singleton.eq(true))
993+
.set((
994+
dsl::quiesce_started.eq(started),
995+
dsl::quiesce_completed.eq(completed),
996+
))
997+
.execute_async(&*conn)
998+
.await
999+
.expect("Failed to update metadata");
9921000
}
9931001

1002+
// This test validates what happens when an old Nexus interacts with a
1003+
// database that has already started (or completed) going through a
1004+
// schema migration.
9941005
#[tokio::test]
9951006
async fn ensure_schema_with_old_nexus() {
9961007
let logctx = dev::test_setup_log("ensure_schema_with_old_nexus");
@@ -999,16 +1010,16 @@ mod test {
9991010
let pool = db.pool();
10001011

10011012
// The contents don't really matter here - just make the schema appear
1002-
// "old".
1013+
// older than the current Nexus expects.
10031014
let mut old_schema = SCHEMA_VERSION;
10041015
old_schema.major -= 1;
10051016

1006-
// Case: Version matches the DB, and quiesce has started.
1017+
// Case: Nexus version matches the DB, and quiesce has started.
10071018
{
10081019
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
10091020
let quiesce_started = true;
10101021
let quiesce_completed = false;
1011-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
1022+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
10121023

10131024
let Err(err) = datastore
10141025
.ensure_schema(
@@ -1026,12 +1037,12 @@ mod test {
10261037
);
10271038
}
10281039

1029-
// Case: Version matches the DB, and quiesce has completed.
1040+
// Case: Nexus version matches the DB, and quiesce has completed.
10301041
{
10311042
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
10321043
let quiesce_started = true;
10331044
let quiesce_completed = true;
1034-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
1045+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
10351046

10361047
let Err(err) = datastore
10371048
.ensure_schema(
@@ -1060,7 +1071,7 @@ mod test {
10601071
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
10611072
let quiesce_started = false;
10621073
let quiesce_completed = false;
1063-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
1074+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
10641075

10651076
let Err(err) = datastore
10661077
.ensure_schema(&log, old_schema, UpdateConfiguration::Disabled)
@@ -1110,7 +1121,7 @@ mod test {
11101121
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
11111122
let quiesce_started = false;
11121123
let quiesce_completed = false;
1113-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
1124+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
11141125

11151126
let Err(err) = datastore
11161127
.ensure_schema(
@@ -1134,7 +1145,7 @@ mod test {
11341145
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
11351146
let quiesce_started = true;
11361147
let quiesce_completed = false;
1137-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
1148+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
11381149

11391150
let Err(err) = datastore
11401151
.ensure_schema(
@@ -1159,7 +1170,7 @@ mod test {
11591170
// This should let us proceed with the update.
11601171
let quiesce_started = true;
11611172
let quiesce_completed = true;
1162-
set_quisece(&datastore, quiesce_started, quiesce_completed).await;
1173+
set_quiesce(&datastore, quiesce_started, quiesce_completed).await;
11631174
datastore
11641175
.ensure_schema(
11651176
&log,
@@ -1203,7 +1214,7 @@ mod test {
12031214
let mut new_schema = SCHEMA_VERSION;
12041215
new_schema.major += 1;
12051216

1206-
// Load the new version, but don't enabled update.
1217+
// Load the new version, but don't enable update.
12071218
let datastore = DataStore::new_unchecked(log.clone(), pool.clone());
12081219
let Err(err) = datastore
12091220
.ensure_schema(&log, new_schema, UpdateConfiguration::Disabled)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl DataStore {
298298
}
299299
Err(e) => {
300300
let err = slog_error_chain::InlineErrorChain::new(&e);
301-
warn!(log, "Failed to ensure schema version"; "error" => &err);
301+
warn!(log, "Failed to ensure schema version"; &err);
302302
return Err(BackoffError::transient(err.to_string()));
303303
}
304304
};

0 commit comments

Comments
 (0)