|
| 1 | +use std::{collections::BTreeMap, sync::Arc}; |
| 2 | + |
| 3 | +use axum::{ |
| 4 | + Json, |
| 5 | + extract::{Path, State}, |
| 6 | + response::{IntoResponse, Response}, |
| 7 | +}; |
| 8 | +use axum_extra::{ |
| 9 | + TypedHeader, |
| 10 | + headers::{Authorization, authorization::Basic}, |
| 11 | +}; |
| 12 | +use http::StatusCode; |
| 13 | +use opentelemetry::KeyValue; |
| 14 | +use serde::{Deserialize, Serialize}; |
| 15 | +use snafu::{ResultExt, Snafu, ensure}; |
| 16 | +use tracing::{info, instrument, warn}; |
| 17 | +use trino_lb_core::{ |
| 18 | + TrinoClusterName, config::TrinoLbAdminAuthenticationConfig, trino_cluster::ClusterState, |
| 19 | +}; |
| 20 | +use trino_lb_persistence::Persistence; |
| 21 | + |
| 22 | +use crate::{ |
| 23 | + cluster_group_manager::{self, ClusterStats}, |
| 24 | + http_server::AppState, |
| 25 | +}; |
| 26 | + |
| 27 | +#[derive(Snafu, Debug)] |
| 28 | +pub enum Error { |
| 29 | + #[snafu(display("No admin authentication method defined"))] |
| 30 | + NoAdminAuthenticationMethodDefined, |
| 31 | + |
| 32 | + #[snafu(display("Invalid admin credentials"))] |
| 33 | + InvalidAdminCredentials, |
| 34 | + |
| 35 | + #[snafu(display("Unknown Trino cluster {cluster:?}"))] |
| 36 | + UnknownCluster { cluster: TrinoClusterName }, |
| 37 | + |
| 38 | + #[snafu(display("Failed to set cluster state for cluster {cluster:?} in persistence"))] |
| 39 | + SetClusterStateInPersistence { |
| 40 | + source: trino_lb_persistence::Error, |
| 41 | + cluster: TrinoClusterName, |
| 42 | + }, |
| 43 | + |
| 44 | + #[snafu(display("Failed to get all cluster states"))] |
| 45 | + GetAllClusterStates { |
| 46 | + source: cluster_group_manager::Error, |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +impl IntoResponse for Error { |
| 51 | + fn into_response(self) -> Response { |
| 52 | + warn!(error = ?self, "Error while processing admin request"); |
| 53 | + let status_code = match self { |
| 54 | + Error::NoAdminAuthenticationMethodDefined => StatusCode::UNAUTHORIZED, |
| 55 | + Error::InvalidAdminCredentials => StatusCode::UNAUTHORIZED, |
| 56 | + Error::UnknownCluster { .. } => StatusCode::NOT_FOUND, |
| 57 | + Error::SetClusterStateInPersistence { .. } => StatusCode::INTERNAL_SERVER_ERROR, |
| 58 | + Error::GetAllClusterStates { .. } => StatusCode::INTERNAL_SERVER_ERROR, |
| 59 | + }; |
| 60 | + (status_code, format!("{self}")).into_response() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// (Re)-Activates a Trino Cluster, so that it receives new queries. |
| 65 | +/// |
| 66 | +/// This is useful for maintenance actions (in combination with deactivation). |
| 67 | +#[instrument(name = "POST /admin/activate-cluster/{cluster_name}", skip(state))] |
| 68 | +pub async fn post_activate_cluster( |
| 69 | + TypedHeader(Authorization(basic_auth)): TypedHeader<Authorization<Basic>>, |
| 70 | + State(state): State<Arc<AppState>>, |
| 71 | + Path(cluster_name): Path<TrinoClusterName>, |
| 72 | +) -> Result<Json<ClusterActivationResponse>, Error> { |
| 73 | + state |
| 74 | + .metrics |
| 75 | + .http_counter |
| 76 | + .add(1, &[KeyValue::new("resource", "post_activate_cluster")]); |
| 77 | + |
| 78 | + set_cluster_activation(state, basic_auth, &cluster_name, true).await |
| 79 | +} |
| 80 | + |
| 81 | +/// Deactivate a Trino Cluster, so that it doesn't receive any new queries. |
| 82 | +/// |
| 83 | +/// This is useful for maintenance actions (in combination with activation). |
| 84 | +#[instrument(name = "POST /admin/deactivate-cluster/{cluster_name}", skip(state))] |
| 85 | +pub async fn post_deactivate_cluster( |
| 86 | + TypedHeader(Authorization(basic_auth)): TypedHeader<Authorization<Basic>>, |
| 87 | + State(state): State<Arc<AppState>>, |
| 88 | + Path(cluster_name): Path<TrinoClusterName>, |
| 89 | +) -> Result<Json<ClusterActivationResponse>, Error> { |
| 90 | + state |
| 91 | + .metrics |
| 92 | + .http_counter |
| 93 | + .add(1, &[KeyValue::new("resource", "post_deactivate_cluster")]); |
| 94 | + |
| 95 | + set_cluster_activation(state, basic_auth, &cluster_name, false).await |
| 96 | +} |
| 97 | + |
| 98 | +/// Get the status of the Trino clusters |
| 99 | +#[instrument(name = "GET /admin/cluster-status", skip(state))] |
| 100 | +pub async fn get_cluster_status( |
| 101 | + State(state): State<Arc<AppState>>, |
| 102 | +) -> Result<Json<BTreeMap<TrinoClusterName, ClusterStats>>, Error> { |
| 103 | + state |
| 104 | + .metrics |
| 105 | + .http_counter |
| 106 | + .add(1, &[KeyValue::new("resource", "get_cluster_status")]); |
| 107 | + |
| 108 | + let cluster_stats = state |
| 109 | + .cluster_group_manager |
| 110 | + .get_all_cluster_stats() |
| 111 | + .await |
| 112 | + .context(GetAllClusterStatesSnafu)?; |
| 113 | + |
| 114 | + Ok(Json( |
| 115 | + cluster_stats |
| 116 | + .into_iter() |
| 117 | + .map(|(cluster, stats)| (cluster.name.clone(), stats)) |
| 118 | + .collect(), |
| 119 | + )) |
| 120 | +} |
| 121 | + |
| 122 | +#[derive(Debug, Deserialize, Serialize)] |
| 123 | +pub struct ClusterActivationResponse { |
| 124 | + state: ClusterState, |
| 125 | +} |
| 126 | + |
| 127 | +#[instrument(skip(state))] |
| 128 | +async fn set_cluster_activation( |
| 129 | + state: Arc<AppState>, |
| 130 | + basic_auth: Basic, |
| 131 | + cluster_name: &TrinoClusterName, |
| 132 | + activation: bool, |
| 133 | +) -> Result<Json<ClusterActivationResponse>, Error> { |
| 134 | + match &state.config.trino_lb.admin_authentication { |
| 135 | + Some(TrinoLbAdminAuthenticationConfig::BasicAuth { username, password }) => { |
| 136 | + ensure!( |
| 137 | + basic_auth.username() == username && basic_auth.password() == password, |
| 138 | + InvalidAdminCredentialsSnafu {} |
| 139 | + ); |
| 140 | + } |
| 141 | + None => return Err(Error::NoAdminAuthenticationMethodDefined), |
| 142 | + } |
| 143 | + |
| 144 | + ensure!( |
| 145 | + state.config.cluster_in_config(cluster_name), |
| 146 | + UnknownClusterSnafu { |
| 147 | + cluster: cluster_name |
| 148 | + } |
| 149 | + ); |
| 150 | + |
| 151 | + let desired_state = if activation { |
| 152 | + info!(cluster = cluster_name, "Re-activating Trino cluster"); |
| 153 | + ClusterState::Unknown |
| 154 | + } else { |
| 155 | + info!(cluster = cluster_name, "Deactivating Trino cluster"); |
| 156 | + ClusterState::Deactivated |
| 157 | + }; |
| 158 | + |
| 159 | + state |
| 160 | + .persistence |
| 161 | + .set_cluster_state(cluster_name, desired_state.clone()) |
| 162 | + .await |
| 163 | + .context(SetClusterStateInPersistenceSnafu { |
| 164 | + cluster: cluster_name, |
| 165 | + })?; |
| 166 | + |
| 167 | + Ok(Json(ClusterActivationResponse { |
| 168 | + state: desired_state, |
| 169 | + })) |
| 170 | +} |
0 commit comments