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 : /// Status of the LFC prewarm process. The same state machine is reused for
50 : /// both autoprewarm (prewarm after compute/Postgres start using the previously
51 : /// stored LFC state) and explicit prewarming via API.
52 : #[derive(Serialize, Default, Debug, Clone, PartialEq)]
53 : #[serde(tag = "status", rename_all = "snake_case")]
54 : pub enum LfcPrewarmState {
55 : /// Default value when compute boots up.
56 : #[default]
57 : NotPrewarmed,
58 : /// Prewarming thread is active and loading pages into LFC.
59 : Prewarming,
60 : /// We found requested LFC state in the endpoint storage and
61 : /// completed prewarming successfully.
62 : Completed,
63 : /// Unexpected error happened during prewarming. Note, `Not Found 404`
64 : /// response from the endpoint storage is explicitly excluded here
65 : /// because it can normally happen on the first compute start,
66 : /// since LFC state is not available yet.
67 : Failed { error: String },
68 : /// We tried to fetch the corresponding LFC state from the endpoint storage,
69 : /// but received `Not Found 404`. This should normally happen only during the
70 : /// first endpoint start after creation with `autoprewarm: true`.
71 : ///
72 : /// During the orchestrated prewarm via API, when a caller explicitly
73 : /// provides the LFC state key to prewarm from, it's the caller responsibility
74 : /// to handle this status as an error state in this case.
75 : Skipped,
76 : }
77 :
78 : impl Display for LfcPrewarmState {
79 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 0 : match self {
81 0 : LfcPrewarmState::NotPrewarmed => f.write_str("NotPrewarmed"),
82 0 : LfcPrewarmState::Prewarming => f.write_str("Prewarming"),
83 0 : LfcPrewarmState::Completed => f.write_str("Completed"),
84 0 : LfcPrewarmState::Skipped => f.write_str("Skipped"),
85 0 : LfcPrewarmState::Failed { error } => write!(f, "Error({error})"),
86 : }
87 0 : }
88 : }
89 :
90 : #[derive(Serialize, Default, Debug, Clone, PartialEq)]
91 : #[serde(tag = "status", rename_all = "snake_case")]
92 : pub enum LfcOffloadState {
93 : #[default]
94 : NotOffloaded,
95 : Offloading,
96 : Completed,
97 : Failed {
98 : error: String,
99 : },
100 : }
101 :
102 : #[derive(Serialize, Debug, Clone, PartialEq)]
103 : #[serde(tag = "status", rename_all = "snake_case")]
104 : /// Response of /promote
105 : pub enum PromoteState {
106 : NotPromoted,
107 : Completed,
108 : Failed { error: String },
109 : }
110 :
111 0 : #[derive(Deserialize, Serialize, Default, Debug, Clone)]
112 : #[serde(rename_all = "snake_case")]
113 : /// Result of /safekeepers_lsn
114 : pub struct SafekeepersLsn {
115 : pub safekeepers: String,
116 : pub wal_flush_lsn: utils::lsn::Lsn,
117 : }
118 :
119 : /// Response of the /status API
120 0 : #[derive(Serialize, Debug, Deserialize)]
121 : #[serde(rename_all = "snake_case")]
122 : pub struct ComputeStatusResponse {
123 : pub start_time: DateTime<Utc>,
124 : pub tenant: Option<String>,
125 : pub timeline: Option<String>,
126 : pub status: ComputeStatus,
127 : #[serde(serialize_with = "rfc3339_serialize")]
128 : pub last_active: Option<DateTime<Utc>>,
129 : pub error: Option<String>,
130 : }
131 :
132 0 : #[derive(Serialize, Clone, Copy, Debug, Deserialize, PartialEq, Eq, Default)]
133 : #[serde(rename_all = "snake_case")]
134 : pub enum TerminateMode {
135 : #[default]
136 : /// wait 30s till returning from /terminate to allow control plane to get the error
137 : Fast,
138 : /// return from /terminate immediately as soon as all components are terminated
139 : Immediate,
140 : }
141 :
142 : impl From<TerminateMode> for ComputeStatus {
143 0 : fn from(mode: TerminateMode) -> Self {
144 0 : match mode {
145 0 : TerminateMode::Fast => ComputeStatus::TerminationPendingFast,
146 0 : TerminateMode::Immediate => ComputeStatus::TerminationPendingImmediate,
147 : }
148 0 : }
149 : }
150 :
151 0 : #[derive(Serialize, Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
152 : #[serde(rename_all = "snake_case")]
153 : pub enum ComputeStatus {
154 : // Spec wasn't provided at start, waiting for it to be
155 : // provided by control-plane.
156 : Empty,
157 : // Compute configuration was requested.
158 : ConfigurationPending,
159 : // Compute node has spec and initial startup and
160 : // configuration is in progress.
161 : Init,
162 : // Compute is configured and running.
163 : Running,
164 : // New spec is being applied.
165 : Configuration,
166 : // Either startup or configuration failed,
167 : // compute will exit soon or is waiting for
168 : // control-plane to terminate it.
169 : Failed,
170 : // Termination requested
171 : TerminationPendingFast,
172 : // Termination requested, without waiting 30s before returning from /terminate
173 : TerminationPendingImmediate,
174 : // Terminated Postgres
175 : Terminated,
176 : }
177 :
178 0 : #[derive(Deserialize, Serialize)]
179 : pub struct TerminateResponse {
180 : pub lsn: Option<utils::lsn::Lsn>,
181 : }
182 :
183 : impl Display for ComputeStatus {
184 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185 0 : match self {
186 0 : ComputeStatus::Empty => f.write_str("empty"),
187 0 : ComputeStatus::ConfigurationPending => f.write_str("configuration-pending"),
188 0 : ComputeStatus::Init => f.write_str("init"),
189 0 : ComputeStatus::Running => f.write_str("running"),
190 0 : ComputeStatus::Configuration => f.write_str("configuration"),
191 0 : ComputeStatus::Failed => f.write_str("failed"),
192 0 : ComputeStatus::TerminationPendingFast => f.write_str("termination-pending-fast"),
193 : ComputeStatus::TerminationPendingImmediate => {
194 0 : f.write_str("termination-pending-immediate")
195 : }
196 0 : ComputeStatus::Terminated => f.write_str("terminated"),
197 : }
198 0 : }
199 : }
200 :
201 0 : pub fn rfc3339_serialize<S>(x: &Option<DateTime<Utc>>, s: S) -> Result<S::Ok, S::Error>
202 0 : where
203 0 : S: Serializer,
204 : {
205 0 : if let Some(x) = x {
206 0 : x.to_rfc3339().serialize(s)
207 : } else {
208 0 : s.serialize_none()
209 : }
210 0 : }
211 :
212 : /// Response of the /metrics.json API
213 : #[derive(Clone, Debug, Default, Serialize)]
214 : pub struct ComputeMetrics {
215 : /// Time spent waiting in pool
216 : pub wait_for_spec_ms: u64,
217 :
218 : /// Time spent checking if safekeepers are synced
219 : pub sync_sk_check_ms: u64,
220 :
221 : /// Time spent syncing safekeepers (walproposer.c).
222 : /// In most cases this should be zero.
223 : pub sync_safekeepers_ms: u64,
224 :
225 : /// Time it took to establish a pg connection to the pageserver.
226 : /// This is two roundtrips, so it's a good proxy for compute-pageserver
227 : /// latency. The latency is usually 0.2ms, but it's not safe to assume
228 : /// that.
229 : pub pageserver_connect_micros: u64,
230 :
231 : /// Time to get basebackup from pageserver and write it to disk.
232 : pub basebackup_ms: u64,
233 :
234 : /// Compressed size of basebackup received.
235 : pub basebackup_bytes: u64,
236 :
237 : /// Time spent starting potgres. This includes initialization of shared
238 : /// buffers, preloading extensions, and other pg operations.
239 : pub start_postgres_ms: u64,
240 :
241 : /// Time spent applying pg catalog updates that were made in the console
242 : /// UI. This should be 0 when startup time matters, since cplane tries
243 : /// to do these updates eagerly, and passes the skip_pg_catalog_updates
244 : /// when it's safe to skip this step.
245 : pub config_ms: u64,
246 :
247 : /// Total time, from when we receive the spec to when we're ready to take
248 : /// pg connections.
249 : pub total_startup_ms: u64,
250 : pub load_ext_ms: u64,
251 : pub num_ext_downloaded: u64,
252 : pub largest_ext_size: u64, // these are measured in bytes
253 : pub total_ext_download_size: u64,
254 : }
255 :
256 : #[derive(Clone, Debug, Default, Serialize)]
257 : pub struct CatalogObjects {
258 : pub roles: Vec<Role>,
259 : pub databases: Vec<Database>,
260 : }
261 :
262 0 : #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
263 : pub struct ComputeCtlConfig {
264 : /// Set of JSON web keys that the compute can use to authenticate
265 : /// communication from the control plane.
266 : pub jwks: JwkSet,
267 : pub tls: Option<TlsConfig>,
268 : }
269 :
270 : impl Default for ComputeCtlConfig {
271 0 : fn default() -> Self {
272 0 : Self {
273 0 : jwks: JwkSet {
274 0 : keys: Vec::default(),
275 0 : },
276 0 : tls: None,
277 0 : }
278 0 : }
279 : }
280 :
281 0 : #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
282 : pub struct TlsConfig {
283 : pub key_path: String,
284 : pub cert_path: String,
285 : }
286 :
287 : /// Response of the `/computes/{compute_id}/spec` control-plane API.
288 0 : #[derive(Deserialize, Debug)]
289 : pub struct ControlPlaneConfigResponse {
290 : pub spec: Option<ComputeSpec>,
291 : pub status: ControlPlaneComputeStatus,
292 : pub compute_ctl_config: ComputeCtlConfig,
293 : }
294 :
295 0 : #[derive(Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
296 : #[serde(rename_all = "snake_case")]
297 : pub enum ControlPlaneComputeStatus {
298 : // Compute is known to control-plane, but it's not
299 : // yet attached to any timeline / endpoint.
300 : Empty,
301 : // Compute is attached to some timeline / endpoint and
302 : // should be able to start with provided spec.
303 : Attached,
304 : }
305 :
306 : #[derive(Clone, Debug, Default, Serialize)]
307 : pub struct InstalledExtension {
308 : pub extname: String,
309 : pub version: String,
310 : pub n_databases: u32, // Number of databases using this extension
311 : pub owned_by_superuser: String,
312 : }
313 :
314 : #[derive(Clone, Debug, Default, Serialize)]
315 : pub struct InstalledExtensions {
316 : pub extensions: Vec<InstalledExtension>,
317 : }
318 :
319 : #[derive(Clone, Debug, Default, Serialize)]
320 : pub struct ExtensionInstallResult {
321 : pub extension: PgIdent,
322 : pub version: ExtVersion,
323 : }
324 : #[derive(Clone, Debug, Default, Serialize)]
325 : pub struct SetRoleGrantsResponse {
326 : pub database: PgIdent,
327 : pub schema: PgIdent,
328 : pub privileges: Vec<Privilege>,
329 : pub role: PgIdent,
330 : }
|