Skip to content

Commit 6f634f6

Browse files
authored
[sled-agent] Add Sleds to DB (#237)
- "Adding sleds once" supported; no liveness checking or re-insertion. - APIs to fetch/list sleds added. - Removed the 'collection' APIs in omicron-common; after using paginated DB support for sleds, these appear unused.
1 parent 0a62717 commit 6f634f6

File tree

10 files changed

+183
-185
lines changed

10 files changed

+183
-185
lines changed

omicron-common/src/collection.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.

omicron-common/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
pub mod api;
2424
pub mod backoff;
2525
pub mod cmd;
26-
pub mod collection;
2726
pub mod config;
2827
pub mod dev;
2928
pub mod http_client;

omicron-common/src/sql/dbinit.sql

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ CREATE DATABASE omicron;
3939
CREATE USER omicron;
4040
GRANT INSERT, SELECT, UPDATE, DELETE ON DATABASE omicron to omicron;
4141

42+
/*
43+
* Sleds
44+
*/
45+
46+
CREATE TABLE omicron.public.Sled (
47+
/* Identity metadata */
48+
id UUID PRIMARY KEY,
49+
time_created TIMESTAMPTZ NOT NULL,
50+
time_modified TIMESTAMPTZ NOT NULL,
51+
/* Indicates that the object has been deleted */
52+
time_deleted TIMESTAMPTZ,
53+
54+
ip INET NOT NULL,
55+
port INT4 NOT NULL
56+
);
57+
4258
/*
4359
* Projects
4460
*/
@@ -197,19 +213,6 @@ CREATE INDEX ON omicron.public.Disk (
197213
time_deleted IS NULL AND attach_instance_id IS NOT NULL;
198214

199215

200-
/*
201-
* Sleds
202-
*/
203-
204-
CREATE TABLE omicron.public.Sled (
205-
/* Identity metadata -- abbreviated for sleds */
206-
id UUID PRIMARY KEY,
207-
time_created TIMESTAMPTZ NOT NULL,
208-
time_modified TIMESTAMPTZ NOT NULL,
209-
210-
sled_agent_ip INET
211-
);
212-
213216
/*
214217
* Oximeter collector servers.
215218
*/

omicron-nexus/src/db/datastore.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,66 @@ impl DataStore {
6060
self.pool.pool()
6161
}
6262

63+
/// Stores a new sled in the database.
64+
pub async fn sled_upsert(
65+
&self,
66+
sled: db::model::Sled,
67+
) -> CreateResult<db::model::Sled> {
68+
use db::schema::sled::dsl;
69+
diesel::insert_into(dsl::sled)
70+
.values(sled.clone())
71+
.on_conflict(dsl::id)
72+
.do_update()
73+
.set((
74+
dsl::time_modified.eq(Utc::now()),
75+
dsl::ip.eq(sled.ip),
76+
dsl::port.eq(sled.port),
77+
))
78+
.get_result_async(self.pool())
79+
.await
80+
.map_err(|e| {
81+
public_error_from_diesel_pool_create(
82+
e,
83+
ResourceType::Sled,
84+
&sled.id.to_string(),
85+
)
86+
})
87+
}
88+
89+
pub async fn sled_list(
90+
&self,
91+
pagparams: &DataPageParams<'_, Uuid>,
92+
) -> ListResultVec<db::model::Sled> {
93+
use db::schema::sled::dsl;
94+
paginated(dsl::sled, dsl::id, pagparams)
95+
.filter(dsl::time_deleted.is_null())
96+
.load_async::<db::model::Sled>(self.pool())
97+
.await
98+
.map_err(|e| {
99+
public_error_from_diesel_pool(
100+
e,
101+
ResourceType::Sled,
102+
LookupType::Other("Listing All".to_string()),
103+
)
104+
})
105+
}
106+
107+
pub async fn sled_fetch(&self, id: Uuid) -> LookupResult<db::model::Sled> {
108+
use db::schema::sled::dsl;
109+
dsl::sled
110+
.filter(dsl::time_deleted.is_null())
111+
.filter(dsl::id.eq(id))
112+
.first_async::<db::model::Sled>(self.pool())
113+
.await
114+
.map_err(|e| {
115+
public_error_from_diesel_pool(
116+
e,
117+
ResourceType::Sled,
118+
LookupType::ById(id),
119+
)
120+
})
121+
}
122+
63123
/// Create a project
64124
pub async fn project_create(
65125
&self,

omicron-nexus/src/db/model.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Structures stored to the database.
22
33
use super::schema::{
4-
disk, instance, metricproducer, networkinterface, oximeter, project, vpc,
5-
vpcsubnet,
4+
disk, instance, metricproducer, networkinterface, oximeter, project, sled,
5+
vpc, vpcsubnet,
66
};
77
use chrono::{DateTime, Utc};
88
use diesel::backend::{Backend, RawValue};
@@ -33,20 +33,60 @@ impl Into<external::Rack> for Rack {
3333
}
3434
}
3535

36-
// NOTE: This object is not currently stored in the database.
37-
//
38-
// However, it likely will be in the future. At the moment,
39-
// Nexus simply reports all the live connections it knows about.
36+
/// Database representation of a Sled.
37+
#[derive(Queryable, Identifiable, Insertable, Debug, Clone)]
38+
#[table_name = "sled"]
4039
pub struct Sled {
41-
pub identity: IdentityMetadata,
42-
pub service_address: SocketAddr,
40+
// IdentityMetadata
41+
pub id: Uuid,
42+
pub time_created: DateTime<Utc>,
43+
pub time_modified: DateTime<Utc>,
44+
pub time_deleted: Option<DateTime<Utc>>,
45+
46+
// ServiceAddress (Sled Agent).
47+
pub ip: ipnetwork::IpNetwork,
48+
pub port: i32,
49+
}
50+
51+
impl Sled {
52+
pub fn new(
53+
id: Uuid,
54+
addr: SocketAddr,
55+
params: external::IdentityMetadataCreateParams,
56+
) -> Self {
57+
let identity = IdentityMetadata::new(id, params);
58+
Self {
59+
id,
60+
time_created: identity.time_created,
61+
time_modified: identity.time_modified,
62+
time_deleted: identity.time_deleted,
63+
ip: addr.ip().into(),
64+
port: addr.port().into(),
65+
}
66+
}
67+
68+
pub fn id(&self) -> &Uuid {
69+
&self.id
70+
}
71+
72+
pub fn address(&self) -> SocketAddr {
73+
// TODO: avoid this unwrap
74+
SocketAddr::new(self.ip.ip(), u16::try_from(self.port).unwrap())
75+
}
4376
}
4477

4578
impl Into<external::Sled> for Sled {
4679
fn into(self) -> external::Sled {
80+
let service_address = self.address();
4781
external::Sled {
48-
identity: self.identity.into(),
49-
service_address: self.service_address,
82+
identity: external::IdentityMetadata {
83+
id: self.id,
84+
name: external::Name::try_from("sled").unwrap(),
85+
description: "sled description".to_string(),
86+
time_created: self.time_created,
87+
time_modified: self.time_modified,
88+
},
89+
service_address,
5090
}
5191
}
5292
}

omicron-nexus/src/db/schema.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ table! {
118118
id -> Uuid,
119119
time_created -> Timestamptz,
120120
time_modified -> Timestamptz,
121-
sled_agent_ip -> Nullable<Inet>,
121+
time_deleted -> Nullable<Timestamptz>,
122+
123+
ip -> Inet,
124+
port -> Int4,
122125
}
123126
}
124127

omicron-nexus/src/http_entrypoints_external.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,13 @@ async fn hardware_sleds_get(
998998
let apictx = rqctx.context();
999999
let nexus = &apictx.nexus;
10001000
let query = query_params.into_inner();
1001-
let sled_stream =
1002-
nexus.sleds_list(&data_page_params_for(&rqctx, &query)?).await?;
1003-
let view_list = to_list::<db::model::Sled, Sled>(sled_stream).await;
1004-
Ok(HttpResponseOk(ScanById::results_page(&query, view_list)?))
1001+
let sleds = nexus
1002+
.sleds_list(&data_page_params_for(&rqctx, &query)?)
1003+
.await?
1004+
.into_iter()
1005+
.map(|s| s.into())
1006+
.collect();
1007+
Ok(HttpResponseOk(ScanById::results_page(&query, sleds)?))
10051008
}
10061009

10071010
/**

omicron-nexus/src/http_entrypoints_internal.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use omicron_common::api::internal::nexus::InstanceRuntimeState;
1515
use omicron_common::api::internal::nexus::OximeterInfo;
1616
use omicron_common::api::internal::nexus::ProducerEndpoint;
1717
use omicron_common::api::internal::nexus::SledAgentStartupInfo;
18-
use omicron_common::SledAgentClient;
1918
use schemars::JsonSchema;
2019
use serde::Deserialize;
2120
use std::sync::Arc;
@@ -68,11 +67,7 @@ async fn cpapi_sled_agents_post(
6867
let path = path_params.into_inner();
6968
let si = sled_info.into_inner();
7069
let sled_id = &path.sled_id;
71-
let client_log =
72-
apictx.log.new(o!("SledAgent" => sled_id.clone().to_string()));
73-
let client =
74-
Arc::new(SledAgentClient::new(&sled_id, si.sa_address, client_log));
75-
nexus.upsert_sled_agent(client).await;
70+
nexus.upsert_sled(*sled_id, si.sa_address).await?;
7671
Ok(HttpResponseUpdatedNoContent())
7772
}
7873

0 commit comments

Comments
 (0)