Line data Source code
1 : //! Structs representing the JSON formats used in the compute_ctl's HTTP API.
2 :
3 : use std::fmt::Display;
4 :
5 : use chrono::{DateTime, Utc};
6 : use jsonwebtoken::jwk::JwkSet;
7 : use serde::{Deserialize, Serialize, Serializer};
8 :
9 : use crate::privilege::Privilege;
10 : use crate::spec::{ComputeSpec, Database, ExtVersion, PgIdent, Role};
11 :
12 0 : #[derive(Serialize, Debug, Deserialize)]
13 : pub struct GenericAPIError {
14 : pub error: String,
15 : }
16 :
17 : /// All configuration parameters necessary for a compute. When
18 : /// [`ComputeConfig::spec`] is provided, it means that the compute is attached
19 : /// to a tenant. [`ComputeConfig::compute_ctl_config`] will always be provided
20 : /// and contains parameters necessary for operating `compute_ctl` independently
21 : /// of whether a tenant is attached to the compute or not.
22 : ///
23 : /// This also happens to be the body of `compute_ctl`'s /configure request.
24 0 : #[derive(Debug, Deserialize, Serialize)]
25 : pub struct ComputeConfig {
26 : /// The compute spec
27 : pub spec: Option<ComputeSpec>,
28 :
29 : /// The compute_ctl configuration
30 : #[allow(dead_code)]
31 : pub compute_ctl_config: ComputeCtlConfig,
32 : }
33 :
34 : impl From<ControlPlaneConfigResponse> for ComputeConfig {
35 0 : fn from(value: ControlPlaneConfigResponse) -> Self {
36 0 : Self {
37 0 : spec: value.spec,
38 0 : compute_ctl_config: value.compute_ctl_config,
39 0 : }
40 0 : }
41 : }
42 :
43 : #[derive(Debug, Clone, Serialize)]
44 : pub struct ExtensionInstallResponse {
45 : pub extension: PgIdent,
46 : pub version: ExtVersion,
47 : }
48 :
49 : #[derive(Serialize, Default, Debug, Clone)]
50 : #[serde(tag = "status", rename_all = "snake_case")]
51 : pub enum LfcPrewarmState {
52 : #[default]
53 : NotPrewarmed,
54 : Prewarming,
55 : Completed,
56 : Failed {
57 : error: String,
58 : },
59 : }
60 :
61 : #[derive(Serialize, Default, Debug, Clone)]
62 : #[serde(tag = "status", rename_all = "snake_case")]
63 : pub enum LfcOffloadState {
64 : #[default]
65 : NotOffloaded,
66 : Offloading,
67 : Completed,
68 : Failed {
69 : error: String,
70 : },
71 : }
72 :
73 : /// Response of the /status API
74 0 : #[derive(Serialize, Debug, Deserialize)]
75 : #[serde(rename_all = "snake_case")]
76 : pub struct ComputeStatusResponse {
77 : pub start_time: DateTime<Utc>,
78 : pub tenant: Option<String>,
79 : pub timeline: Option<String>,
80 : pub status: ComputeStatus,
81 : #[serde(serialize_with = "rfc3339_serialize")]
82 : pub last_active: Option<DateTime<Utc>>,
83 : pub error: Option<String>,
84 : }
85 :
86 0 : #[derive(Serialize, Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
87 : #[serde(rename_all = "snake_case")]
88 : pub enum ComputeStatus {
89 : // Spec wasn't provided at start, waiting for it to be
90 : // provided by control-plane.
91 : Empty,
92 : // Compute configuration was requested.
93 : ConfigurationPending,
94 : // Compute node has spec and initial startup and
95 : // configuration is in progress.
96 : Init,
97 : // Compute is configured and running.
98 : Running,
99 : // New spec is being applied.
100 : Configuration,
101 : // Either startup or configuration failed,
102 : // compute will exit soon or is waiting for
103 : // control-plane to terminate it.
104 : Failed,
105 : // Termination requested
106 : TerminationPending,
107 : // Terminated Postgres
108 : Terminated,
109 : }
110 :
111 : impl Display for ComputeStatus {
112 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 0 : match self {
114 0 : ComputeStatus::Empty => f.write_str("empty"),
115 0 : ComputeStatus::ConfigurationPending => f.write_str("configuration-pending"),
116 0 : ComputeStatus::Init => f.write_str("init"),
117 0 : ComputeStatus::Running => f.write_str("running"),
118 0 : ComputeStatus::Configuration => f.write_str("configuration"),
119 0 : ComputeStatus::Failed => f.write_str("failed"),
120 0 : ComputeStatus::TerminationPending => f.write_str("termination-pending"),
121 0 : ComputeStatus::Terminated => f.write_str("terminated"),
122 : }
123 0 : }
124 : }
125 :
126 0 : pub fn rfc3339_serialize<S>(x: &Option<DateTime<Utc>>, s: S) -> Result<S::Ok, S::Error>
127 0 : where
128 0 : S: Serializer,
129 0 : {
130 0 : if let Some(x) = x {
131 0 : x.to_rfc3339().serialize(s)
132 : } else {
133 0 : s.serialize_none()
134 : }
135 0 : }
136 :
137 : /// Response of the /metrics.json API
138 : #[derive(Clone, Debug, Default, Serialize)]
139 : pub struct ComputeMetrics {
140 : /// Time spent waiting in pool
141 : pub wait_for_spec_ms: u64,
142 :
143 : /// Time spent checking if safekeepers are synced
144 : pub sync_sk_check_ms: u64,
145 :
146 : /// Time spent syncing safekeepers (walproposer.c).
147 : /// In most cases this should be zero.
148 : pub sync_safekeepers_ms: u64,
149 :
150 : /// Time it took to establish a pg connection to the pageserver.
151 : /// This is two roundtrips, so it's a good proxy for compute-pageserver
152 : /// latency. The latency is usually 0.2ms, but it's not safe to assume
153 : /// that.
154 : pub pageserver_connect_micros: u64,
155 :
156 : /// Time to get basebackup from pageserver and write it to disk.
157 : pub basebackup_ms: u64,
158 :
159 : /// Compressed size of basebackup received.
160 : pub basebackup_bytes: u64,
161 :
162 : /// Time spent starting potgres. This includes initialization of shared
163 : /// buffers, preloading extensions, and other pg operations.
164 : pub start_postgres_ms: u64,
165 :
166 : /// Time spent applying pg catalog updates that were made in the console
167 : /// UI. This should be 0 when startup time matters, since cplane tries
168 : /// to do these updates eagerly, and passes the skip_pg_catalog_updates
169 : /// when it's safe to skip this step.
170 : pub config_ms: u64,
171 :
172 : /// Total time, from when we receive the spec to when we're ready to take
173 : /// pg connections.
174 : pub total_startup_ms: u64,
175 : pub load_ext_ms: u64,
176 : pub num_ext_downloaded: u64,
177 : pub largest_ext_size: u64, // these are measured in bytes
178 : pub total_ext_download_size: u64,
179 : }
180 :
181 : #[derive(Clone, Debug, Default, Serialize)]
182 : pub struct CatalogObjects {
183 : pub roles: Vec<Role>,
184 : pub databases: Vec<Database>,
185 : }
186 :
187 0 : #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
188 : pub struct ComputeCtlConfig {
189 : /// Set of JSON web keys that the compute can use to authenticate
190 : /// communication from the control plane.
191 : pub jwks: JwkSet,
192 : pub tls: Option<TlsConfig>,
193 : }
194 :
195 : impl Default for ComputeCtlConfig {
196 0 : fn default() -> Self {
197 0 : Self {
198 0 : jwks: JwkSet {
199 0 : keys: Vec::default(),
200 0 : },
201 0 : tls: None,
202 0 : }
203 0 : }
204 : }
205 :
206 0 : #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
207 : pub struct TlsConfig {
208 : pub key_path: String,
209 : pub cert_path: String,
210 : }
211 :
212 : /// Response of the `/computes/{compute_id}/spec` control-plane API.
213 0 : #[derive(Deserialize, Debug)]
214 : pub struct ControlPlaneConfigResponse {
215 : pub spec: Option<ComputeSpec>,
216 : pub status: ControlPlaneComputeStatus,
217 : pub compute_ctl_config: ComputeCtlConfig,
218 : }
219 :
220 0 : #[derive(Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
221 : #[serde(rename_all = "snake_case")]
222 : pub enum ControlPlaneComputeStatus {
223 : // Compute is known to control-plane, but it's not
224 : // yet attached to any timeline / endpoint.
225 : Empty,
226 : // Compute is attached to some timeline / endpoint and
227 : // should be able to start with provided spec.
228 : Attached,
229 : }
230 :
231 : #[derive(Clone, Debug, Default, Serialize)]
232 : pub struct InstalledExtension {
233 : pub extname: String,
234 : pub version: String,
235 : pub n_databases: u32, // Number of databases using this extension
236 : pub owned_by_superuser: String,
237 : }
238 :
239 : #[derive(Clone, Debug, Default, Serialize)]
240 : pub struct InstalledExtensions {
241 : pub extensions: Vec<InstalledExtension>,
242 : }
243 :
244 : #[derive(Clone, Debug, Default, Serialize)]
245 : pub struct ExtensionInstallResult {
246 : pub extension: PgIdent,
247 : pub version: ExtVersion,
248 : }
249 : #[derive(Clone, Debug, Default, Serialize)]
250 : pub struct SetRoleGrantsResponse {
251 : pub database: PgIdent,
252 : pub schema: PgIdent,
253 : pub privileges: Vec<Privilege>,
254 : pub role: PgIdent,
255 : }
|