Skip to content

Commit 238ebe8

Browse files
committed
[nexus] Add DataStore::instance_fetch_all
In order to implement the `instance-update` saga ongoing in #5749, it's necessary to have a way to query the database for the state of an instance, its active VMM, its target VMM, and active migration in a single, atomic query. This will be used as a snapshot of the instance state that the update saga will operate on. This commit adds an implementation of such a query in the form of a `DataStore::instance_fetch_all` method, returning an `InstanceSnapshot` struct that bundles together all these records. We do this by `LEFT JOIN`ing the `instance` table with the `vmm` and `migration` table on the `instance`'s various ID columns, with a query that should look something like: ```sql SELECT * FROM instance LEFT JOIN vmm ON ((instance.active_propolis_id = vmm.id) OR (instance.target_propolis_id = vmm.id)) LEFT JOIN migration ON (instance.migration_id = migration.id) WHERE instance.id = instance_id ``` Ideally, we would instead do two subqueries that join `instance` with `vmm` on the active and destination IDs separately and return a single row which contains both in a way that can be distinguished in the result set, but I wasn't able to figure out how to do that in Diesel. Instead, the query may return two rows, if the instance has both an active and target VMM, and iterate over the result set to find both VMMs in Rust code. This is a bit uglier, but it works fine.
1 parent 0a0db97 commit 238ebe8

File tree

4 files changed

+325
-7
lines changed

4 files changed

+325
-7
lines changed

nexus/db-model/src/migration.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ use uuid::Uuid;
1414

1515
/// The state of a migration as understood by Nexus.
1616
#[derive(
17-
Clone, Debug, Queryable, Insertable, Selectable, Serialize, Deserialize,
17+
Clone,
18+
Debug,
19+
Queryable,
20+
Insertable,
21+
Selectable,
22+
Serialize,
23+
Deserialize,
24+
Eq,
25+
PartialEq,
1826
)]
1927
#[diesel(table_name = migration)]
2028
pub struct Migration {

nexus/db-model/src/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ table! {
17761776
}
17771777

17781778
allow_tables_to_appear_in_same_query!(instance, migration);
1779+
allow_tables_to_appear_in_same_query!(migration, vmm);
17791780
joinable!(instance -> migration (migration_id));
17801781

17811782
allow_tables_to_appear_in_same_query!(

nexus/db-model/src/vmm.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ use uuid::Uuid;
2121

2222
/// An individual VMM process that incarnates a specific instance.
2323
#[derive(
24-
Clone, Queryable, Debug, Selectable, Serialize, Deserialize, Insertable,
24+
Clone,
25+
Queryable,
26+
Debug,
27+
Selectable,
28+
Serialize,
29+
Deserialize,
30+
Insertable,
31+
PartialEq,
2532
)]
2633
#[diesel(table_name = vmm)]
2734
pub struct Vmm {
@@ -101,6 +108,7 @@ impl Vmm {
101108
Queryable,
102109
Serialize,
103110
Deserialize,
111+
PartialEq,
104112
)]
105113
#[diesel(table_name = vmm)]
106114
pub struct VmmRuntimeState {

0 commit comments

Comments
 (0)