Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nexus] Experiment with using closer-to-raw-SQL pagination #7717

Draft
wants to merge 209 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
209 commits
Select commit Hold shift + click to select a range
c9fb7a6
[nexus] Add Affinity/Anti-Affinity Groups to API (unimplemented)
smklein Jan 30, 2025
4020517
[nexus] Add Affinity/Anti-Affinity groups to database
smklein Jan 30, 2025
8f1d37c
[nexus] Add CRUD implementations for Affinity/Anti-Affinity Groups
smklein Jan 30, 2025
772e64f
[nexus] Consider Affinity/Anti-Affinity Groups during instance placement
smklein Jan 30, 2025
d8cff32
[nexus] Implement Affinity/Anti-Affinity Groups in external API
smklein Jan 30, 2025
161f9d6
fix policy tests
smklein Jan 30, 2025
df119b6
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Jan 30, 2025
83a26a4
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Jan 30, 2025
8dc0825
fmt
smklein Jan 30, 2025
e3113ff
Merge branch 'affinity-api' into affinity-db-model
smklein Jan 30, 2025
789bc97
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Jan 30, 2025
5e21f34
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Jan 30, 2025
fa9461b
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Jan 30, 2025
4e9cebc
tags
smklein Jan 30, 2025
6cfca2d
doc comments
smklein Jan 31, 2025
a1c97d4
Merge branch 'affinity-api' into affinity-db-model
smklein Jan 31, 2025
050b4c5
Merge branch 'main' into affinity-api
smklein Jan 31, 2025
4b08032
typed UUID
smklein Jan 31, 2025
8bc8f0c
Merge branch 'affinity-api' into affinity-db-model
smklein Jan 31, 2025
900f09c
Typed UUID
smklein Jan 31, 2025
195e167
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Jan 31, 2025
f2ebe31
Typed UUID
smklein Jan 31, 2025
62c38ec
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Jan 31, 2025
85985c1
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Jan 31, 2025
1326116
UUID typing
smklein Jan 31, 2025
aba9596
comments
smklein Jan 31, 2025
a271f1d
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Jan 31, 2025
1ad0101
review feedback
smklein Feb 1, 2025
4d26262
comment
smklein Feb 1, 2025
6ae1910
clippy
smklein Feb 1, 2025
58ebd65
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 1, 2025
ab817bc
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 1, 2025
9daf923
Converting sled_resource to sled_resource_vmm
smklein Feb 3, 2025
1b39c51
schema, more renaming
smklein Feb 3, 2025
e21125f
Merge branch 'main' into affinity-api
smklein Feb 4, 2025
bd95b03
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 4, 2025
76b0af9
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 4, 2025
1179919
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 4, 2025
a07c46e
review feedback
smklein Feb 4, 2025
fdccd6b
review feedback (grammatical)
smklein Feb 5, 2025
db49c67
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 5, 2025
6e44392
anti-affinity group description
smklein Feb 5, 2025
8e6fe1a
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 5, 2025
d5b951f
Update comment
smklein Feb 5, 2025
fdfb78d
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 5, 2025
f57a96a
Fix schema migration
smklein Feb 5, 2025
fb8976d
Redo regression test for OID poisoning, and move it
smklein Feb 5, 2025
17dc409
clippppyyyyyy
smklein Feb 6, 2025
97b9b2e
Eliza's feedback
smklein Feb 6, 2025
d949890
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 6, 2025
fa2550e
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 6, 2025
79b4252
[nexus-db-queries] Benchmark for VMM reservation
smklein Feb 6, 2025
04a4b98
Tweak usable hardware threads to make instance placement less flaky
smklein Feb 6, 2025
db40d05
Normalize reservation time, only benchmark creation pathway
smklein Feb 6, 2025
b68239b
Normalize
smklein Feb 7, 2025
ca8f890
cleanup
smklein Feb 7, 2025
5406dd4
Better contention info
smklein Feb 10, 2025
bb0f349
Better contention information
smklein Feb 11, 2025
6704be1
restructure benchmark
smklein Feb 11, 2025
127285c
more refactoring
smklein Feb 11, 2025
acf3a64
test naming
smklein Feb 12, 2025
2eaef4f
Start working towards a reduced-contention sled reservation
smklein Feb 12, 2025
c9bb4c7
passing tests
smklein Feb 12, 2025
8e5b1b0
cleanup
smklein Feb 12, 2025
d1356f8
Updated expectorate output
smklein Feb 12, 2025
7abc2b4
Testing contention more explicitly
smklein Feb 13, 2025
bd14e7b
fmt
smklein Feb 13, 2025
d55e540
Patch benchmark
smklein Feb 13, 2025
f710687
Partway through affinity group testing
smklein Feb 14, 2025
82b028c
cache instance/group records
smklein Feb 14, 2025
43f4437
benchmark updates
smklein Feb 14, 2025
7e80208
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 14, 2025
9a3ef20
fmt
smklein Feb 14, 2025
f028351
clippy
smklein Feb 14, 2025
df9a1f8
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 14, 2025
2ea4dea
Remove unused test output
smklein Feb 14, 2025
45b993a
Merge branch 'main' into affinity-api
smklein Feb 18, 2025
05c8b60
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 18, 2025
eee1d6f
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 18, 2025
10aaed9
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 18, 2025
f8b9b5d
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 18, 2025
1032128
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 18, 2025
859f099
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 18, 2025
7e5cd6a
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 18, 2025
3edc5ed
Fix mismerge
smklein Feb 18, 2025
8566395
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 18, 2025
5c55192
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 18, 2025
393a925
Merge branch 'main' into affinity-api
smklein Feb 19, 2025
7a99a96
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 19, 2025
ee657cc
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 19, 2025
931801d
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 19, 2025
5f75a2b
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 19, 2025
b1689c8
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 19, 2025
fd09de7
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 19, 2025
81d843e
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 19, 2025
c18449c
Merge branch 'main' into affinity-api
smklein Feb 19, 2025
4bf0e48
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 19, 2025
1a7e7e0
Merge branch 'main' into affinity-api
smklein Feb 19, 2025
01b8f03
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 19, 2025
a35a33c
Merge branch 'main' into affinity-api
smklein Feb 20, 2025
eb9682f
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 20, 2025
0e6eaf2
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 20, 2025
45a97a3
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 20, 2025
bd1f2cc
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 20, 2025
7ba08b0
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 20, 2025
1add3c1
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 20, 2025
18d3031
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 20, 2025
3bf35a2
Basic implementation of affinity group member within anti-affinity group
smklein Feb 21, 2025
4a00ddd
Merge branch 'main' into affinity-api
smklein Feb 21, 2025
7a8da16
Merge branch 'affinity-api' into affinity-db-model (schema)
smklein Feb 21, 2025
081e8b4
Merge branch 'main' into affinity-api
smklein Feb 21, 2025
7733054
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 21, 2025
e9cc19c
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 21, 2025
01914c3
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 21, 2025
e6634dc
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 21, 2025
565ffc8
Merge branch 'main' into affinity-api
smklein Feb 21, 2025
e15460c
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 21, 2025
9190d80
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 21, 2025
8d90161
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 21, 2025
9f6ad7b
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 21, 2025
49196d2
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 21, 2025
7193c88
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 21, 2025
c3f164f
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 21, 2025
1430c4a
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 21, 2025
daeb2e1
Update API
smklein Feb 21, 2025
2d65048
Schema migration for db updates
smklein Feb 21, 2025
4e6362d
boilerplate, gotta fix pagination on list next
smklein Feb 24, 2025
f50749d
Manual pagination of both tables at once
smklein Feb 24, 2025
16b3df3
Merge branch 'main' into affinity-api
smklein Feb 24, 2025
f5bbe28
Merge branch 'affinity-api' into affinity-db-model
smklein Feb 24, 2025
beaedfc
Merge branch 'affinity-db-model' into affinity-db-crud
smklein Feb 24, 2025
afbc4ad
Merge branch 'affinity-db-crud' into affinity-instance-integration
smklein Feb 24, 2025
25a6e5e
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 24, 2025
7c90072
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 24, 2025
701106d
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 24, 2025
40cef0b
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 24, 2025
75869c7
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 24, 2025
0419d37
review feedback
smklein Feb 24, 2025
2ad7bb7
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 24, 2025
da625b9
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 24, 2025
36ddafc
Relocate some functions into pub_test_utils
smklein Feb 25, 2025
b8c5024
Code review feedback
smklein Feb 25, 2025
bba32a6
Merge branch 'main' into affinity-instance-integration
smklein Feb 25, 2025
e893250
Merge branch 'affinity-instance-integration' into affinity-integration
smklein Feb 25, 2025
f5ec80d
Merge branch 'affinity-integration' into sled-resource-vmm
smklein Feb 25, 2025
fba0614
Merge branch 'sled-resource-vmm' into vmm-reserve-bench
smklein Feb 25, 2025
0e54843
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 25, 2025
804b2ee
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 25, 2025
e79fa5f
README
smklein Feb 25, 2025
faed1c4
Merge branch 'main' into vmm-reserve-bench
smklein Feb 25, 2025
01029f1
Add issue
smklein Feb 25, 2025
df01f7a
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 25, 2025
0e8e7fc
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 25, 2025
b36be57
new args to test helpers
smklein Feb 25, 2025
c96034c
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 25, 2025
167a2d8
Clippy
smklein Feb 25, 2025
3c0cc80
Merge branch 'main' into vmm-reserve-bench
smklein Feb 25, 2025
c2e9493
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 25, 2025
1882d74
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 25, 2025
17887d4
please no panic
smklein Feb 25, 2025
5865732
Safer group-to-group rule respecting
smklein Feb 25, 2025
6ab1c67
Add integration test for group of group
smklein Feb 25, 2025
0cb05b5
Expanding testing, improving deletion semantics
smklein Feb 26, 2025
9ad2313
More testing
smklein Feb 26, 2025
56f300a
Fix listing, add listing test
smklein Feb 26, 2025
05ebab7
clipperoni
smklein Feb 26, 2025
ec9a490
AffinityGroupMember exposes member name
smklein Feb 26, 2025
2f8a0e6
AntiAffinityGroupMember exposes member name
smklein Feb 27, 2025
388b6cd
Update API
smklein Feb 27, 2025
2fdc267
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Feb 27, 2025
462afc7
Merge branch 'main' into vmm-reserve-bench
smklein Feb 27, 2025
9d10bd8
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 27, 2025
9da8120
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 27, 2025
aa9de03
Unauthorized coverage test
smklein Feb 27, 2025
ec54f4e
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Feb 27, 2025
93d4ac3
Merge branch 'main' into vmm-reserve-bench
smklein Feb 28, 2025
77f2675
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Feb 28, 2025
90d41c8
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Feb 28, 2025
20496ce
i scream you scream we all scream for authcream
smklein Feb 28, 2025
af06ec7
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Feb 28, 2025
be6ac0f
Name-based pagination (needs tests)
smklein Feb 28, 2025
de905ff
Fix pagination, add tests
smklein Feb 28, 2025
20e6bbb
Clippy
smklein Feb 28, 2025
b5138eb
Plumb state out too
smklein Feb 28, 2025
75acd32
Merge branch 'main' into vmm-reserve-bench
smklein Mar 3, 2025
744d97f
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Mar 3, 2025
a0c4ce4
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Mar 3, 2025
fa358bf
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Mar 3, 2025
b15b883
[nexus] Experiment with using closer-to-raw-SQL pagination
smklein Mar 4, 2025
a3e885a
Merge branch 'main' into vmm-reserve-bench
smklein Mar 4, 2025
72ecbf2
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Mar 4, 2025
c39e421
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Mar 4, 2025
15b67bd
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Mar 4, 2025
cf6fad1
Merge branch 'affinity-expose-name' into paginated-manually
smklein Mar 4, 2025
0becc08
Fix query
smklein Mar 4, 2025
8d729c5
docs
smklein Mar 4, 2025
6ac39e5
Finish paginating instance.rs
smklein Mar 5, 2025
4ed71c3
Merge branch 'main' into vmm-reserve-bench
smklein Mar 7, 2025
d8f173d
Merge branch 'vmm-reserve-bench' into vmm-reduce-contention
smklein Mar 7, 2025
22c3eb0
Merge branch 'vmm-reduce-contention' into aa-group-of-group
smklein Mar 7, 2025
e765503
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Mar 7, 2025
797d340
Merge branch 'affinity-expose-name' into paginated-manually
smklein Mar 7, 2025
37fa3fb
Merge branch 'main' into vmm-reduce-contention
smklein Mar 10, 2025
dec670d
Merge branch 'main' into vmm-reduce-contention
smklein Mar 17, 2025
c9f38d4
Merge branch 'aa-group-of-group' into affinity-expose-name
smklein Mar 17, 2025
6e369d9
Undo group-of-groups
smklein Mar 18, 2025
af5c3fa
shrinking the diff
smklein Mar 18, 2025
08801b1
Update nexus.json
smklein Mar 18, 2025
5b4e822
Merge branch 'affinity-expose-name' into paginated-manually
smklein Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions common/src/api/external/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ pub struct IdentityMetadataUpdateParams {
Debug,
Deserialize,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
Expand Down Expand Up @@ -1320,14 +1321,20 @@ pub enum FailureDomain {
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum AffinityGroupMember {
/// An instance belonging to this group, identified by UUID.
Instance(InstanceUuid),
/// An instance belonging to this group
Instance { id: InstanceUuid, name: Name, run_state: InstanceState },
}

impl SimpleIdentity for AffinityGroupMember {
impl SimpleIdentityOrName for AffinityGroupMember {
fn id(&self) -> Uuid {
match self {
AffinityGroupMember::Instance(id) => *id.as_untyped_uuid(),
AffinityGroupMember::Instance { id, .. } => *id.as_untyped_uuid(),
}
}

fn name(&self) -> &Name {
match self {
AffinityGroupMember::Instance { name, .. } => name,
}
}
}
Expand All @@ -1339,14 +1346,22 @@ impl SimpleIdentity for AffinityGroupMember {
#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum AntiAffinityGroupMember {
/// An instance belonging to this group, identified by UUID.
Instance(InstanceUuid),
/// An instance belonging to this group
Instance { id: InstanceUuid, name: Name, run_state: InstanceState },
}

impl SimpleIdentity for AntiAffinityGroupMember {
impl SimpleIdentityOrName for AntiAffinityGroupMember {
fn id(&self) -> Uuid {
match self {
AntiAffinityGroupMember::Instance(id) => *id.as_untyped_uuid(),
AntiAffinityGroupMember::Instance { id, .. } => {
*id.as_untyped_uuid()
}
}
}

fn name(&self) -> &Name {
match self {
AntiAffinityGroupMember::Instance { name, .. } => name,
}
}
}
Expand Down
30 changes: 20 additions & 10 deletions nexus/db-model/src/affinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,17 @@ impl AffinityGroupInstanceMembership {
pub fn new(group_id: AffinityGroupUuid, instance_id: InstanceUuid) -> Self {
Self { group_id: group_id.into(), instance_id: instance_id.into() }
}
}

impl From<AffinityGroupInstanceMembership> for external::AffinityGroupMember {
fn from(member: AffinityGroupInstanceMembership) -> Self {
Self::Instance(member.instance_id.into())
pub fn to_external(
self,
member_name: external::Name,
run_state: external::InstanceState,
) -> external::AffinityGroupMember {
external::AffinityGroupMember::Instance {
id: self.instance_id.into(),
name: member_name,
run_state,
}
}
}

Expand All @@ -250,12 +256,16 @@ impl AntiAffinityGroupInstanceMembership {
) -> Self {
Self { group_id: group_id.into(), instance_id: instance_id.into() }
}
}

impl From<AntiAffinityGroupInstanceMembership>
for external::AntiAffinityGroupMember
{
fn from(member: AntiAffinityGroupInstanceMembership) -> Self {
Self::Instance(member.instance_id.into())
pub fn to_external(
self,
member_name: external::Name,
run_state: external::InstanceState,
) -> external::AntiAffinityGroupMember {
external::AntiAffinityGroupMember::Instance {
id: self.instance_id.into(),
name: member_name,
run_state,
}
}
}
63 changes: 0 additions & 63 deletions nexus/db-model/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use crate::collection::DatastoreAttachTargetConfig;
use crate::schema::{disk, external_ip, instance};
use chrono::{DateTime, TimeDelta, Utc};
use db_macros::Resource;
use diesel::expression::{ValidGrouping, is_aggregate};
use diesel::pg;
use diesel::prelude::*;
use diesel::sql_types::{Bool, Nullable};
use nexus_types::external_api::params;
use omicron_uuid_kinds::{GenericUuid, InstanceUuid};
use serde::Deserialize;
Expand Down Expand Up @@ -392,66 +389,6 @@ impl InstanceAutoRestart {

Reincarnatability::WillReincarnate
}

/// Filters a database query to include only instances whose auto-restart
/// configs permit them to reincarnate.
///
/// Yes, this should probably be in `nexus-db-queries`, but it seemed nice
/// for it to be defined on the same struct as the in-memory logic
/// (`can_reincarnate`).
pub fn filter_reincarnatable()
-> impl diesel::query_builder::QueryFragment<pg::Pg>
+ diesel::query_builder::QueryId
// All elements in this expression appear on the `instance` table, so
// it's a valid `filter` for that table, and the expression evaluates
// to a bool (or NULL), making it a valid WHERE clause.
+ AppearsOnTable<instance::table, SqlType = Nullable<Bool>>
// I think this trait tells diesel that the query fragment has no
// GROUP BY clause, so that it knows it can be used as a WHERE clause
+ ValidGrouping<(), IsAggregate = is_aggregate::No> {
use instance::dsl;

let now = diesel::dsl::now.into_sql::<pg::sql_types::Timestamptz>();

// The instance's auto-restart policy must allow the control plane
// to restart it automatically.
//
// N.B. that this may become more complex in the future if we grow
// additional auto-restart policies that require additional logic
// (such as restart limits...)
(dsl::auto_restart_policy
.eq(InstanceAutoRestartPolicy::BestEffort)
// If the auto-restart policy is null, then it should
// default to "best effort".
.or(dsl::auto_restart_policy.is_null()))
// An instance whose last reincarnation was within the cooldown
// interval from now must remain in _bardo_ --- the liminal
// state between death and rebirth --- before its next
// reincarnation.
.and(
// If the instance has never previously been reincarnated, then
// it's allowed to reincarnate.
dsl::time_last_auto_restarted
.is_null()
// Or, if it has an overridden cooldown period, has that elapsed?
.or(dsl::auto_restart_cooldown.is_not_null().and(
dsl::time_last_auto_restarted
.le(now.nullable() - dsl::auto_restart_cooldown),
))
// Or, finally, if it does not have an overridden cooldown
// period, has the default cooldown period elapsed?
.or(dsl::auto_restart_cooldown.is_null().and(
dsl::time_last_auto_restarted
.le((now - Self::DEFAULT_COOLDOWN).nullable()),
)),
)
// Deleted instances may not be reincarnated.
.and(dsl::time_deleted.is_null())
// If the instance is currently in the process of being updated,
// let's not mess with it for now and try to restart it on another
// pass.
.and(dsl::updater_id.is_null())
}
}

/// It's just a type with the same representation as a `TimeDelta` that
Expand Down
44 changes: 44 additions & 0 deletions nexus/db-queries/src/db/column_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl<T> ColumnWalker<T> {

macro_rules! impl_column_walker {
( $len:literal $($column:ident)+ ) => (
/// Returns all columns with the "(prefix)." string prepended.
///
/// For example, with the columns "id, name" and the prefix "foo:
/// The output string would be: "foo.id, foo.name"
#[allow(dead_code)]
impl<$($column: Column),+> ColumnWalker<($($column,)+)> {
pub fn with_prefix(prefix: &'static str) -> TrustedStr {
Expand All @@ -42,6 +46,46 @@ macro_rules! impl_column_walker {
}
}

/// Identical to [Self::with_prefix], but also aliases each column.
///
/// The aliased name is "(prefix)_(column name)".
///
/// For example, with the columns "id, name" and the prefix "foo:
/// The output string would be: "foo.id as foo_id, foo.name as foo_name"
#[allow(dead_code)]
impl<$($column: Column),+> ColumnWalker<($($column,)+)> {
pub fn with_prefix_and_alias(prefix: &'static str) -> TrustedStr {
// This string is derived from:
// - The "table" type, with associated columns, which
// are not controlled by an arbitrary user, and
// - The "prefix" type, which is a "&'static str" (AKA,
// hopefully known at compile-time, and not leaked).
TrustedStr::i_take_responsibility_for_validating_this_string(
[
$(
format!(
"{prefix}.{column} as {prefix}_{column}",
prefix = prefix, column = $column::NAME
),
)+
].join(", ")
)
}
}

/// Returns all columns without any suffix.
#[allow(dead_code)]
impl<$($column: Column),+> ColumnWalker<($($column,)+)> {
pub fn as_str() -> TrustedStr {
// This string is derived from:
// - The "table" type, with associated columns, which
// are not controlled by an arbitrary user, and
TrustedStr::i_take_responsibility_for_validating_this_string(
[$($column::NAME,)+].join(", ")
)
}
}

impl<$($column: Column),+> IntoIterator for ColumnWalker<($($column,)+)> {
type Item = &'static str;
type IntoIter = std::array::IntoIter<Self::Item, $len>;
Expand Down
Loading
Loading