mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-13 08:22:55 +00:00
+ neondatabase/cloud#1103 This adds a couple of control endpoints to simplify compute state discovery for control-plane. For example, now we may figure out that Postgres wasn't able to start or basebackup failed within seconds instead of just blindly polling the compute readiness for a minute or two. Also we now expose startup metrics (time of the each step: basebackup, sync safekeepers, config, total). Console grabs them after each successful start and report as histogram to prometheus and grafana. OpenAPI spec is added and up-tp date, but is not currently used in the console yet.
47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use std::sync::Arc;
|
|
|
|
use anyhow::{anyhow, Result};
|
|
use log::error;
|
|
use postgres::Client;
|
|
use tokio_postgres::NoTls;
|
|
|
|
use crate::compute::ComputeNode;
|
|
|
|
pub fn create_writablity_check_data(client: &mut Client) -> Result<()> {
|
|
let query = "
|
|
CREATE TABLE IF NOT EXISTS health_check (
|
|
id serial primary key,
|
|
updated_at timestamptz default now()
|
|
);
|
|
INSERT INTO health_check VALUES (1, now())
|
|
ON CONFLICT (id) DO UPDATE
|
|
SET updated_at = now();";
|
|
let result = client.simple_query(query)?;
|
|
if result.len() < 2 {
|
|
return Err(anyhow::format_err!("executed {} queries", result.len()));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn check_writability(compute: &Arc<ComputeNode>) -> Result<()> {
|
|
let connstr = &compute.connstr;
|
|
let (client, connection) = tokio_postgres::connect(connstr, NoTls).await?;
|
|
if client.is_closed() {
|
|
return Err(anyhow!("connection to postgres closed"));
|
|
}
|
|
tokio::spawn(async move {
|
|
if let Err(e) = connection.await {
|
|
error!("connection error: {}", e);
|
|
}
|
|
});
|
|
|
|
let result = client
|
|
.simple_query("UPDATE health_check SET updated_at = now() WHERE id = 1;")
|
|
.await?;
|
|
|
|
if result.len() != 1 {
|
|
return Err(anyhow!("statement can't be executed"));
|
|
}
|
|
Ok(())
|
|
}
|