|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use anyhow::anyhow; |
| 4 | +use graph::components::store::DeploymentLocator; |
| 5 | +use graph::components::store::StoreEvent; |
| 6 | +use graph::prelude::EntityChange; |
| 7 | +use graph::prelude::NodeId; |
| 8 | +use graph_store_postgres::command_support::catalog; |
| 9 | +use graph_store_postgres::command_support::catalog::Site; |
| 10 | +use graph_store_postgres::connection_pool::ConnectionPool; |
| 11 | +use graph_store_postgres::NotificationSender; |
| 12 | +use thiserror::Error; |
| 13 | + |
| 14 | +use crate::deployment::DeploymentSelector; |
| 15 | +use crate::deployment::DeploymentVersionSelector; |
| 16 | +use crate::GraphmanError; |
| 17 | + |
| 18 | +pub struct Deployment { |
| 19 | + locator: DeploymentLocator, |
| 20 | + site: Site, |
| 21 | +} |
| 22 | + |
| 23 | +impl Deployment { |
| 24 | + pub fn locator(&self) -> &DeploymentLocator { |
| 25 | + &self.locator |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Error)] |
| 30 | +pub enum ReassignDeploymentError { |
| 31 | + #[error("deployment '{0}' is already assigned to '{1}'")] |
| 32 | + AlreadyAssigned(String, String), |
| 33 | + |
| 34 | + #[error(transparent)] |
| 35 | + Common(#[from] GraphmanError), |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Clone, Debug)] |
| 39 | +pub enum ReassignResult { |
| 40 | + EmptyResponse, |
| 41 | + CompletedWithWarnings(Vec<String>), |
| 42 | +} |
| 43 | + |
| 44 | +pub fn load_deployment( |
| 45 | + primary_pool: ConnectionPool, |
| 46 | + deployment: &DeploymentSelector, |
| 47 | +) -> Result<Deployment, ReassignDeploymentError> { |
| 48 | + let mut primary_conn = primary_pool.get().map_err(GraphmanError::from)?; |
| 49 | + |
| 50 | + let locator = crate::deployment::load_deployment_locator( |
| 51 | + &mut primary_conn, |
| 52 | + deployment, |
| 53 | + &DeploymentVersionSelector::All, |
| 54 | + )?; |
| 55 | + |
| 56 | + let mut catalog_conn = catalog::Connection::new(primary_conn); |
| 57 | + |
| 58 | + let site = catalog_conn |
| 59 | + .locate_site(locator.clone()) |
| 60 | + .map_err(GraphmanError::from)? |
| 61 | + .ok_or_else(|| { |
| 62 | + GraphmanError::Store(anyhow!("deployment site not found for '{locator}'")) |
| 63 | + })?; |
| 64 | + |
| 65 | + Ok(Deployment { locator, site }) |
| 66 | +} |
| 67 | + |
| 68 | +pub fn reassign_deployment( |
| 69 | + primary_pool: ConnectionPool, |
| 70 | + notification_sender: Arc<NotificationSender>, |
| 71 | + deployment: &Deployment, |
| 72 | + node: &NodeId, |
| 73 | +) -> Result<ReassignResult, ReassignDeploymentError> { |
| 74 | + let primary_conn = primary_pool.get().map_err(GraphmanError::from)?; |
| 75 | + let mut catalog_conn = catalog::Connection::new(primary_conn); |
| 76 | + |
| 77 | + let changes: Vec<EntityChange> = match catalog_conn |
| 78 | + .assigned_node(&deployment.site) |
| 79 | + .map_err(GraphmanError::from)? |
| 80 | + { |
| 81 | + Some(curr) => { |
| 82 | + if &curr == node { |
| 83 | + vec![] |
| 84 | + } else { |
| 85 | + catalog_conn |
| 86 | + .reassign_subgraph(&deployment.site, &node) |
| 87 | + .map_err(GraphmanError::from)? |
| 88 | + } |
| 89 | + } |
| 90 | + None => catalog_conn |
| 91 | + .assign_subgraph(&deployment.site, &node) |
| 92 | + .map_err(GraphmanError::from)?, |
| 93 | + }; |
| 94 | + |
| 95 | + if changes.is_empty() { |
| 96 | + return Err(ReassignDeploymentError::AlreadyAssigned( |
| 97 | + deployment.locator.to_string(), |
| 98 | + node.to_string(), |
| 99 | + )); |
| 100 | + } |
| 101 | + |
| 102 | + catalog_conn |
| 103 | + .send_store_event(¬ification_sender, &StoreEvent::new(changes)) |
| 104 | + .map_err(GraphmanError::from)?; |
| 105 | + |
| 106 | + let mirror = catalog::Mirror::primary_only(primary_pool); |
| 107 | + let count = mirror |
| 108 | + .assignments(&node) |
| 109 | + .map_err(GraphmanError::from)? |
| 110 | + .len(); |
| 111 | + if count == 1 { |
| 112 | + let warning_msg = format!("This is the only deployment assigned to '{}'. Please make sure that the node ID is spelled correctly.",node.as_str()); |
| 113 | + Ok(ReassignResult::CompletedWithWarnings(vec![warning_msg])) |
| 114 | + } else { |
| 115 | + Ok(ReassignResult::EmptyResponse) |
| 116 | + } |
| 117 | +} |
0 commit comments