Line data Source code
1 : // Utils for running sync_safekeepers
2 : use anyhow::Result;
3 : use tracing::info;
4 : use utils::lsn::Lsn;
5 :
6 0 : #[derive(Copy, Clone, Debug)]
7 : pub enum TimelineStatusResponse {
8 : NotFound,
9 : Ok(TimelineStatusOkResponse),
10 : }
11 :
12 200 : #[derive(Copy, Clone, Debug)]
13 : pub struct TimelineStatusOkResponse {
14 : flush_lsn: Lsn,
15 : commit_lsn: Lsn,
16 : }
17 :
18 : /// Get a safekeeper's metadata for our timeline. The id is only used for logging
19 777 : pub async fn ping_safekeeper(
20 777 : id: String,
21 777 : config: tokio_postgres::Config,
22 777 : ) -> Result<TimelineStatusResponse> {
23 : // TODO add retries
24 :
25 : // Connect
26 777 : info!("connecting to {}", id);
27 1573 : let (client, conn) = config.connect(tokio_postgres::NoTls).await?;
28 770 : tokio::spawn(async move {
29 1456 : if let Err(e) = conn.await {
30 0 : eprintln!("connection error: {}", e);
31 728 : }
32 770 : });
33 :
34 : // Query
35 770 : info!("querying {}", id);
36 770 : let result = client.simple_query("TIMELINE_STATUS").await?;
37 :
38 : // Parse result
39 728 : info!("done with {}", id);
40 728 : if let postgres::SimpleQueryMessage::Row(row) = &result[0] {
41 : use std::str::FromStr;
42 236 : let response = TimelineStatusResponse::Ok(TimelineStatusOkResponse {
43 236 : flush_lsn: Lsn::from_str(row.get("flush_lsn").unwrap())?,
44 236 : commit_lsn: Lsn::from_str(row.get("commit_lsn").unwrap())?,
45 : });
46 236 : Ok(response)
47 : } else {
48 : // Timeline doesn't exist
49 492 : Ok(TimelineStatusResponse::NotFound)
50 : }
51 732 : }
52 :
53 : /// Given a quorum of responses, check if safekeepers are synced at some Lsn
54 574 : pub fn check_if_synced(responses: Vec<TimelineStatusResponse>) -> Option<Lsn> {
55 574 : // Check if all responses are ok
56 574 : let ok_responses: Vec<TimelineStatusOkResponse> = responses
57 574 : .iter()
58 677 : .filter_map(|r| match r {
59 200 : TimelineStatusResponse::Ok(ok_response) => Some(ok_response),
60 477 : _ => None,
61 677 : })
62 574 : .cloned()
63 574 : .collect();
64 574 : if ok_responses.len() < responses.len() {
65 441 : info!(
66 441 : "not synced. Only {} out of {} know about this timeline",
67 441 : ok_responses.len(),
68 441 : responses.len()
69 441 : );
70 441 : return None;
71 133 : }
72 133 :
73 133 : // Get the min and the max of everything
74 200 : let commit: Vec<Lsn> = ok_responses.iter().map(|r| r.commit_lsn).collect();
75 200 : let flush: Vec<Lsn> = ok_responses.iter().map(|r| r.flush_lsn).collect();
76 133 : let commit_max = commit.iter().max().unwrap();
77 133 : let commit_min = commit.iter().min().unwrap();
78 133 : let flush_max = flush.iter().max().unwrap();
79 133 : let flush_min = flush.iter().min().unwrap();
80 133 :
81 133 : // Check that all values are equal
82 133 : if commit_min != commit_max {
83 1 : info!("not synced. {:?} {:?}", commit_min, commit_max);
84 1 : return None;
85 132 : }
86 132 : if flush_min != flush_max {
87 0 : info!("not synced. {:?} {:?}", flush_min, flush_max);
88 0 : return None;
89 132 : }
90 132 :
91 132 : // Check that commit == flush
92 132 : if commit_max != flush_max {
93 1 : info!("not synced. {:?} {:?}", commit_max, flush_max);
94 1 : return None;
95 131 : }
96 131 :
97 131 : Some(*commit_max)
98 574 : }
|