Skip to content

Commit

Permalink
deployer: changed service_id from uuid to ulid
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianbarbu committed Jul 18, 2023
1 parent 41263a6 commit 7e82443
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 65 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tracing-opentelemetry = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, optional = true }
ttl_cache = { workspace = true, optional = true }
utoipa = { workspace = true, optional = true }
ulid = { workspace = true, features = ["serde"] }
uuid = { workspace = true, features = ["v4", "serde"], optional = true }

[features]
Expand Down
21 changes: 18 additions & 3 deletions common/src/models/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@ use comfy_table::{
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};
use ulid::Ulid;
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use utoipa::{
openapi::{Object, ObjectBuilder},
ToSchema,
};
use uuid::Uuid;

#[cfg(feature = "openapi")]
fn ulid_type() -> Object {
ObjectBuilder::new()
.schema_type(utoipa::openapi::SchemaType::String)
.format(Some(utoipa::openapi::SchemaFormat::Custom(
"ulid".to_string(),
)))
.description(Some("String represention of an Ulid according to the spec found here: https://github.com/ulid/spec."))
.build()
}

#[derive(Deserialize, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(as = shuttle_common::models::deployment::Response))]
pub struct Response {
#[cfg_attr(feature = "openapi", schema(value_type = KnownFormat::Uuid))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(value_type = KnownFormat::Uuid))]
pub service_id: Uuid,
#[cfg_attr(feature = "openapi", schema(schema_with = ulid_type))]
pub service_id: Ulid,
#[cfg_attr(feature = "openapi", schema(value_type = shuttle_common::deployment::State))]
pub state: State,
#[cfg_attr(feature = "openapi", schema(value_type = KnownFormat::DateTime))]
Expand Down
4 changes: 2 additions & 2 deletions common/src/models/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crossterm::style::{Color, Stylize};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;
use ulid::Ulid;
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use uuid::Uuid;

use crate::models::deployment;

Expand All @@ -13,7 +13,7 @@ use crate::models::deployment;
#[cfg_attr(feature = "openapi", schema(as = shuttle_common::models::service::Response))]
pub struct Response {
#[cfg_attr(feature = "openapi", schema(value_type = KnownFormat::Uuid))]
pub id: Uuid,
pub id: Ulid,
pub name: String,
}

Expand Down
1 change: 1 addition & 0 deletions deployer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ tracing-subscriber = { workspace = true, features = [
] }
utoipa = { workspace = true }
utoipa-swagger-ui = { workspace = true }
ulid = { workspace = true }
uuid = { workspace = true, features = ["v4"] }

[dependencies.shuttle-common]
Expand Down
107 changes: 107 additions & 0 deletions deployer/migrations/0004_change_service_id_to_ulid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
-- Copy current service table. Keep the old table around because
-- the rest of the tables are still having an FK on service_id.
CREATE TABLE IF NOT EXISTS services_copy (
id TEXT PRIMARY KEY, -- Identifier of the service.
name TEXT UNIQUE -- Name of the service.
);

INSERT INTO services_copy (id, name)
SELECT
uuid_to_ulid(services.id),
services.name
FROM services;

-- Copy current deployments table without the FK service_id constraint.
CREATE TABLE IF NOT EXISTS deployments_copy (
id TEXT PRIMARY KEY, -- Identifier of the deployment.
service_id TEXT, -- Identifier of the service this deployment belongs to.
state TEXT, -- Enum indicating the current state of the deployment.
last_update INTEGER, -- Unix epoch of the last status update
address TEXT, -- Address a running deployment is active on
is_next BOOLEAN, -- Whether the deployment is for a shuttle-next runtime
git_commit_id TEXT, -- Deployment git commit id
git_commit_msg TEXT, -- Deployment last git commit msg
git_branch TEXT, -- Deployment git branch
git_dirty BOOLEAN -- Deployment git state is dirty
);

INSERT INTO deployments_copy (id, service_id, state, last_update, address, is_next)
SELECT
deployments.id,
uuid_to_ulid(deployments.service_id),
deployments.state,
deployments.last_update,
deployments.address,
deployments.is_next,
deployments.git_commit_id,
deployments.git_commit_msg,
deployments.git_branch,
deployments.git_dirty
FROM deployments;

-- Copy current resource table without the FK service_id constraint.
CREATE TABLE IF NOT EXISTS resources_copy (
service_id TEXT, -- Identifier of the service this resource belongs to.
type TEXT, -- Type of resource this is.
data TEXT, -- Data about this resource.
config TEXT, -- Resource configuration.
PRIMARY KEY (service_id, type),
);
INSERT INTO resources_copy (service_id, type, data, config)
SELECT
uuid_to_ulid(resources.service_id),
resources.type,
resources.data,
resources.config,
FROM resources;

-- Copy current secrets table without the FK service_id constraint.
CREATE TABLE IF NOT EXISTS secrets_copy (
service_id TEXT, -- Identifier of the service this secret belongs to.
key TEXT, -- Key / name of this secret.
value TEXT, -- The actual secret.
last_update INTEGER, -- Unix epoch of the last secret update
PRIMARY KEY (service_id, key),
);
INSERT INTO secrets_copy (service_id, key, value, last_update)
SELECT
uuid_to_ulid(secrets.service_id),
secrets.key,
secrets.value,
secrets.last_update
FROM secrets;

-- Recreate the deployments table with an FK constraint on the service_id.
DROP TABLE deployments;
CREATE TABLE IF NOT EXISTS deployments (
id TEXT PRIMARY KEY, -- Identifier of the deployment.
service_id TEXT, -- Identifier of the service this deployment belongs to.
state TEXT, -- Enum indicating the current state of the deployment.
last_update INTEGER, -- Unix epoch of the last status update
address TEXT -- Address a running deployment is active on
is_next BOOLEAN, -- Whether the deployment is for a shuttle-next runtime
git_commit_id TEXT, -- Deployment git commit id
git_commit_msg TEXT, -- Deployment last git commit msg
git_branch TEXT, -- Deployment git branch
git_dirty BOOLEAN -- Deployment git state is dirty
FOREIGN KEY(service_id) REFERENCES services(id)
);
INSERT INTO deployments SELECT * FROM deployments_copy;
DROP TABLE deployments_copy;

-- Recreate the resources table with an FK constraint on the service_id.
DROP TABLE resources;
CREATE TABLE IF NOT EXISTS resources (
service_id TEXT, -- Identifier of the service this resource belongs to.
type TEXT, -- Type of resource this is.
data TEXT, -- Data about this resource.
config TEXT, -- Resource configuration.
PRIMARY KEY (service_id, type),
FOREIGN KEY(service_id) REFERENCES services(id)
);
INSERT INTO resources SELECT * FROM resources_copy;
DROP TABLE resources_copy;

-- Replace the old services table with the updated one.
DROP TABLE services;
ALTER TABLE services_copy RENAME TO services;
15 changes: 8 additions & 7 deletions deployer/src/deployment/deploy_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ mod tests {
use tokio::{select, time::sleep};
use tonic::transport::Server;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use ulid::Ulid;
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -486,7 +487,7 @@ mod tests {

async fn insert_secret(
&self,
_service_id: &Uuid,
_service_id: &Ulid,
_key: &str,
_value: &str,
) -> Result<(), Self::Err> {
Expand Down Expand Up @@ -525,7 +526,7 @@ mod tests {

async fn get_active_deployments(
&self,
_service_id: &Uuid,
_service_id: &Ulid,
) -> std::result::Result<Vec<Uuid>, Self::Err> {
Ok(vec![])
}
Expand Down Expand Up @@ -558,7 +559,7 @@ mod tests {
impl SecretGetter for StubSecretGetter {
type Err = std::io::Error;

async fn get_secrets(&self, _service_id: &Uuid) -> Result<Vec<Secret>, Self::Err> {
async fn get_secrets(&self, _service_id: &Ulid) -> Result<Vec<Secret>, Self::Err> {
Ok(Default::default())
}
}
Expand All @@ -573,7 +574,7 @@ mod tests {
async fn insert_resource(&self, _resource: &Resource) -> Result<(), Self::Err> {
Ok(())
}
async fn get_resources(&self, _service_id: &Uuid) -> Result<Vec<Resource>, Self::Err> {
async fn get_resources(&self, _service_id: &Ulid) -> Result<Vec<Resource>, Self::Err> {
Ok(Vec::new())
}
}
Expand Down Expand Up @@ -825,7 +826,7 @@ mod tests {
.run_push(Built {
id,
service_name: "run-test".to_string(),
service_id: Uuid::new_v4(),
service_id: Ulid::new(),
tracing_context: Default::default(),
is_next: false,
claim: None,
Expand Down Expand Up @@ -868,7 +869,7 @@ mod tests {
.queue_push(Queued {
id,
service_name: "nil_id".to_string(),
service_id: Uuid::new_v4(),
service_id: Ulid::new(),
data: Bytes::from("violets are red").to_vec(),
will_run_tests: false,
tracing_context: Default::default(),
Expand Down Expand Up @@ -927,7 +928,7 @@ mod tests {
Queued {
id: Uuid::new_v4(),
service_name: format!("deploy-layer-{name}"),
service_id: Uuid::new_v4(),
service_id: Ulid::new(),
data: bytes,
will_run_tests: false,
tracing_context: Default::default(),
Expand Down
5 changes: 3 additions & 2 deletions deployer/src/deployment/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tokio::task::JoinSet;
use tokio::time::{sleep, timeout};
use tracing::{debug, debug_span, error, info, instrument, trace, warn, Instrument, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use ulid::Ulid;
use uuid::Uuid;

use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -161,7 +162,7 @@ async fn promote_to_run(mut built: Built, run_send: RunSender) {
pub struct Queued {
pub id: Uuid,
pub service_name: String,
pub service_id: Uuid,
pub service_id: Ulid,
pub data: Vec<u8>,
pub will_run_tests: bool,
pub tracing_context: HashMap<String, String>,
Expand Down Expand Up @@ -302,7 +303,7 @@ async fn get_secrets(project_path: &Path) -> Result<BTreeMap<String, String>> {
#[instrument(skip(secrets, service_id, secret_recorder))]
async fn set_secrets(
secrets: BTreeMap<String, String>,
service_id: &Uuid,
service_id: &Ulid,
secret_recorder: impl SecretRecorder,
) -> Result<()> {
for (key, value) in secrets.into_iter() {
Expand Down
16 changes: 9 additions & 7 deletions deployer/src/deployment/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tokio::{
use tonic::{transport::Channel, Code};
use tracing::{debug, debug_span, error, info, instrument, trace, warn, Instrument};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use ulid::Ulid;
use uuid::Uuid;

use super::{RunReceiver, State};
Expand Down Expand Up @@ -138,7 +139,7 @@ pub async fn task(

#[instrument(skip(active_deployment_getter, runtime_manager))]
async fn kill_old_deployments(
service_id: Uuid,
service_id: Ulid,
deployment_id: Uuid,
active_deployment_getter: impl ActiveDeploymentsGetter,
runtime_manager: Arc<Mutex<RuntimeManager>>,
Expand Down Expand Up @@ -195,15 +196,15 @@ pub trait ActiveDeploymentsGetter: Clone + Send + Sync + 'static {

async fn get_active_deployments(
&self,
service_id: &Uuid,
service_id: &Ulid,
) -> std::result::Result<Vec<Uuid>, Self::Err>;
}

#[derive(Clone, Debug)]
pub struct Built {
pub id: Uuid,
pub service_name: String,
pub service_id: Uuid,
pub service_id: Ulid,
pub tracing_context: HashMap<String, String>,
pub is_next: bool,
pub claim: Option<Claim>,
Expand Down Expand Up @@ -281,7 +282,7 @@ impl Built {

async fn load(
service_name: String,
service_id: Uuid,
service_id: Ulid,
executable_path: PathBuf,
secret_getter: impl SecretGetter,
resource_manager: impl ResourceManager,
Expand Down Expand Up @@ -453,6 +454,7 @@ mod tests {
time::sleep,
};
use tonic::transport::Server;
use ulid::Ulid;
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -534,7 +536,7 @@ mod tests {
impl SecretGetter for StubSecretGetter {
type Err = std::io::Error;

async fn get_secrets(&self, _service_id: &Uuid) -> Result<Vec<Secret>, Self::Err> {
async fn get_secrets(&self, _service_id: &Ulid) -> Result<Vec<Secret>, Self::Err> {
Ok(Default::default())
}
}
Expand All @@ -549,7 +551,7 @@ mod tests {
async fn insert_resource(&self, _resource: &Resource) -> Result<(), Self::Err> {
Ok(())
}
async fn get_resources(&self, _service_id: &Uuid) -> Result<Vec<Resource>, Self::Err> {
async fn get_resources(&self, _service_id: &Ulid) -> Result<Vec<Resource>, Self::Err> {
Ok(Vec::new())
}
}
Expand Down Expand Up @@ -746,7 +748,7 @@ mod tests {
Built {
id,
service_name: crate_name.to_string(),
service_id: Uuid::new_v4(),
service_id: Ulid::new(),
tracing_context: Default::default(),
is_next: false,
claim: None,
Expand Down

0 comments on commit 7e82443

Please sign in to comment.