Line data Source code
1 : //! This module implements pulling WAL from peer safekeepers if compute can't
2 : //! provide it, i.e. safekeeper lags too much.
3 :
4 : use std::fmt;
5 : use std::pin::pin;
6 : use std::time::SystemTime;
7 :
8 : use anyhow::{Context, bail};
9 : use futures::StreamExt;
10 : use postgres_protocol::message::backend::ReplicationMessage;
11 : use safekeeper_api::Term;
12 : use safekeeper_api::membership::INVALID_GENERATION;
13 : use safekeeper_api::models::{PeerInfo, TimelineStatus};
14 : use tokio::select;
15 : use tokio::sync::mpsc::{Receiver, Sender, channel};
16 : use tokio::time::{self, Duration, sleep, timeout};
17 : use tokio_postgres::replication::ReplicationStream;
18 : use tokio_postgres::types::PgLsn;
19 : use tracing::*;
20 : use utils::id::NodeId;
21 : use utils::lsn::Lsn;
22 : use utils::postgres_client::{
23 : ConnectionConfigArgs, PostgresClientProtocol, wal_stream_connection_config,
24 : };
25 :
26 : use crate::SafeKeeperConf;
27 : use crate::receive_wal::{MSG_QUEUE_SIZE, REPLY_QUEUE_SIZE, WalAcceptor};
28 : use crate::safekeeper::{
29 : AcceptorProposerMessage, AppendRequest, AppendRequestHeader, ProposerAcceptorMessage,
30 : ProposerElected, TermHistory, TermLsn, VoteRequest,
31 : };
32 : use crate::timeline::WalResidentTimeline;
33 :
34 : /// Entrypoint for per timeline task which always runs, checking whether
35 : /// recovery for this safekeeper is needed and starting it if so.
36 : #[instrument(name = "recovery", skip_all, fields(ttid = %tli.ttid))]
37 : pub async fn recovery_main(tli: WalResidentTimeline, conf: SafeKeeperConf) {
38 : info!("started");
39 :
40 : let cancel = tli.cancel.clone();
41 : select! {
42 : _ = recovery_main_loop(tli, conf) => { unreachable!() }
43 : _ = cancel.cancelled() => {
44 : info!("stopped");
45 : }
46 : }
47 : }
48 :
49 : /// Should we start fetching WAL from a peer safekeeper, and if yes, from
50 : /// which? Answer is yes, i.e. .donors is not empty if 1) there is something
51 : /// to fetch, and we can do that without running elections; 2) there is no
52 : /// actively streaming compute, as we don't want to compete with it.
53 : ///
54 : /// If donor(s) are choosen, theirs last_log_term is guaranteed to be equal
55 : /// to its last_log_term so we are sure such a leader ever had been elected.
56 : ///
57 : /// All possible donors are returned so that we could keep connection to the
58 : /// current one if it is good even if it slightly lags behind.
59 : ///
60 : /// Note that term conditions above might be not met, but safekeepers are
61 : /// still not aligned on last flush_lsn. Generally in this case until
62 : /// elections are run it is not possible to say which safekeeper should
63 : /// recover from which one -- history which would be committed is different
64 : /// depending on assembled quorum (e.g. classic picture 8 from Raft paper).
65 : /// Thus we don't try to predict it here.
66 15 : async fn recovery_needed(
67 15 : tli: &WalResidentTimeline,
68 15 : heartbeat_timeout: Duration,
69 15 : ) -> RecoveryNeededInfo {
70 15 : let ss = tli.read_shared_state().await;
71 15 : let term = ss.sk.state().acceptor_state.term;
72 15 : let last_log_term = ss.sk.last_log_term();
73 15 : let flush_lsn = ss.sk.flush_lsn();
74 15 : // note that peers contain myself, but that's ok -- we are interested only in peers which are strictly ahead of us.
75 15 : let mut peers = ss.get_peers(heartbeat_timeout);
76 15 : // Sort by <last log term, lsn> pairs.
77 15 : peers.sort_by(|p1, p2| {
78 0 : let tl1 = TermLsn {
79 0 : term: p1.last_log_term,
80 0 : lsn: p1.flush_lsn,
81 0 : };
82 0 : let tl2 = TermLsn {
83 0 : term: p2.last_log_term,
84 0 : lsn: p2.flush_lsn,
85 0 : };
86 0 : tl2.cmp(&tl1) // desc
87 15 : });
88 15 : let num_streaming_computes = tli.get_walreceivers().get_num_streaming();
89 15 : let donors = if num_streaming_computes > 0 {
90 0 : vec![] // If there is a streaming compute, don't try to recover to not intervene.
91 : } else {
92 15 : peers
93 15 : .iter()
94 15 : .filter_map(|candidate| {
95 0 : // Are we interested in this candidate?
96 0 : let candidate_tl = TermLsn {
97 0 : term: candidate.last_log_term,
98 0 : lsn: candidate.flush_lsn,
99 0 : };
100 0 : let my_tl = TermLsn {
101 0 : term: last_log_term,
102 0 : lsn: flush_lsn,
103 0 : };
104 0 : if my_tl < candidate_tl {
105 : // Yes, we are interested. Can we pull from it without
106 : // (re)running elections? It is possible if 1) his term
107 : // is equal to his last_log_term so we could act on
108 : // behalf of leader of this term (we must be sure he was
109 : // ever elected) and 2) our term is not higher, or we'll refuse data.
110 0 : if candidate.term == candidate.last_log_term && candidate.term >= term {
111 0 : Some(Donor::from(candidate))
112 : } else {
113 0 : None
114 : }
115 : } else {
116 0 : None
117 : }
118 15 : })
119 15 : .collect()
120 : };
121 15 : RecoveryNeededInfo {
122 15 : term,
123 15 : last_log_term,
124 15 : flush_lsn,
125 15 : peers,
126 15 : num_streaming_computes,
127 15 : donors,
128 15 : }
129 15 : }
130 : /// Result of Timeline::recovery_needed, contains donor(s) if recovery needed and
131 : /// fields to explain the choice.
132 : #[derive(Debug)]
133 : pub struct RecoveryNeededInfo {
134 : /// my term
135 : pub term: Term,
136 : /// my last_log_term
137 : pub last_log_term: Term,
138 : /// my flush_lsn
139 : pub flush_lsn: Lsn,
140 : /// peers from which we can fetch WAL, for observability.
141 : pub peers: Vec<PeerInfo>,
142 : /// for observability
143 : pub num_streaming_computes: usize,
144 : pub donors: Vec<Donor>,
145 : }
146 :
147 : // Custom to omit not important fields from PeerInfo.
148 : impl fmt::Display for RecoveryNeededInfo {
149 0 : fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150 0 : write!(f, "{{")?;
151 0 : write!(
152 0 : f,
153 0 : "term: {}, last_log_term: {}, flush_lsn: {}, peers: {{",
154 0 : self.term, self.last_log_term, self.flush_lsn
155 0 : )?;
156 0 : for p in self.peers.iter() {
157 0 : write!(
158 0 : f,
159 0 : "PeerInfo {{ sk_id: {}, term: {}, last_log_term: {}, flush_lsn: {} }}, ",
160 0 : p.sk_id, p.term, p.last_log_term, p.flush_lsn
161 0 : )?;
162 : }
163 0 : write!(
164 0 : f,
165 0 : "}} num_streaming_computes: {}, donors: {:?}",
166 0 : self.num_streaming_computes, self.donors
167 0 : )
168 0 : }
169 : }
170 :
171 : #[derive(Clone, Debug)]
172 : pub struct Donor {
173 : pub sk_id: NodeId,
174 : /// equals to last_log_term
175 : pub term: Term,
176 : pub flush_lsn: Lsn,
177 : pub pg_connstr: String,
178 : pub http_connstr: String,
179 : pub https_connstr: Option<String>,
180 : }
181 :
182 : impl From<&PeerInfo> for Donor {
183 0 : fn from(p: &PeerInfo) -> Self {
184 0 : Donor {
185 0 : sk_id: p.sk_id,
186 0 : term: p.term,
187 0 : flush_lsn: p.flush_lsn,
188 0 : pg_connstr: p.pg_connstr.clone(),
189 0 : http_connstr: p.http_connstr.clone(),
190 0 : https_connstr: p.https_connstr.clone(),
191 0 : }
192 0 : }
193 : }
194 :
195 : const CHECK_INTERVAL_MS: u64 = 2000;
196 :
197 : /// Check regularly whether we need to start recovery.
198 5 : async fn recovery_main_loop(tli: WalResidentTimeline, conf: SafeKeeperConf) {
199 5 : let check_duration = Duration::from_millis(CHECK_INTERVAL_MS);
200 : loop {
201 15 : let recovery_needed_info = recovery_needed(&tli, conf.heartbeat_timeout).await;
202 15 : match recovery_needed_info.donors.first() {
203 0 : Some(donor) => {
204 0 : info!(
205 0 : "starting recovery from donor {}: {}",
206 : donor.sk_id, recovery_needed_info
207 : );
208 0 : let res = tli.wal_residence_guard().await;
209 0 : if let Err(e) = res {
210 0 : warn!("failed to obtain guard: {}", e);
211 0 : continue;
212 0 : }
213 0 : match recover(res.unwrap(), donor, &conf).await {
214 : // Note: 'write_wal rewrites WAL written before' error is
215 : // expected here and might happen if compute and recovery
216 : // concurrently write the same data. Eventually compute
217 : // should win.
218 0 : Err(e) => warn!("recovery failed: {:#}", e),
219 0 : Ok(msg) => info!("recovery finished: {}", msg),
220 : }
221 : }
222 : None => {
223 15 : trace!(
224 0 : "recovery not needed or not possible: {}",
225 : recovery_needed_info
226 : );
227 : }
228 : }
229 15 : sleep(check_duration).await;
230 : }
231 : }
232 :
233 : /// Recover from the specified donor. Returns message explaining normal finish
234 : /// reason or error.
235 0 : async fn recover(
236 0 : tli: WalResidentTimeline,
237 0 : donor: &Donor,
238 0 : conf: &SafeKeeperConf,
239 0 : ) -> anyhow::Result<String> {
240 0 : // Learn donor term switch history to figure out starting point.
241 0 :
242 0 : let mut client = reqwest::Client::builder();
243 0 : for cert in &conf.ssl_ca_certs {
244 0 : client = client.add_root_certificate(cert.clone());
245 0 : }
246 0 : let client = client
247 0 : .build()
248 0 : .context("Failed to build http client for recover")?;
249 :
250 0 : let url = if conf.use_https_safekeeper_api {
251 0 : if let Some(https_connstr) = donor.https_connstr.as_ref() {
252 0 : format!("https://{https_connstr}")
253 : } else {
254 0 : anyhow::bail!(
255 0 : "cannot recover from donor {}: \
256 0 : https is enabled, but https_connstr is not specified",
257 0 : donor.sk_id
258 0 : );
259 : }
260 : } else {
261 0 : format!("http://{}", donor.http_connstr)
262 : };
263 :
264 0 : let timeline_info: TimelineStatus = client
265 0 : .get(format!(
266 0 : "{}/v1/tenant/{}/timeline/{}",
267 0 : url, tli.ttid.tenant_id, tli.ttid.timeline_id
268 0 : ))
269 0 : .send()
270 0 : .await?
271 0 : .json()
272 0 : .await?;
273 0 : if timeline_info.acceptor_state.term != donor.term {
274 0 : bail!(
275 0 : "donor term changed from {} to {}",
276 0 : donor.term,
277 0 : timeline_info.acceptor_state.term
278 0 : );
279 0 : }
280 0 : // convert from API TermSwitchApiEntry into TermLsn.
281 0 : let donor_th = TermHistory(
282 0 : timeline_info
283 0 : .acceptor_state
284 0 : .term_history
285 0 : .iter()
286 0 : .map(|tl| Into::<TermLsn>::into(*tl))
287 0 : .collect(),
288 0 : );
289 0 :
290 0 : // Now understand our term history.
291 0 : let vote_request = ProposerAcceptorMessage::VoteRequest(VoteRequest {
292 0 : generation: INVALID_GENERATION,
293 0 : term: donor.term,
294 0 : });
295 0 : let vote_response = match tli
296 0 : .process_msg(&vote_request)
297 0 : .await
298 0 : .context("VoteRequest handling")?
299 : {
300 0 : Some(AcceptorProposerMessage::VoteResponse(vr)) => vr,
301 : _ => {
302 0 : bail!("unexpected VoteRequest response"); // unreachable
303 : }
304 : };
305 0 : if vote_response.term != donor.term {
306 0 : bail!(
307 0 : "our term changed from {} to {}",
308 0 : donor.term,
309 0 : vote_response.term
310 0 : );
311 0 : }
312 :
313 0 : let last_common_point = match TermHistory::find_highest_common_point(
314 0 : &donor_th,
315 0 : &vote_response.term_history,
316 0 : vote_response.flush_lsn,
317 0 : ) {
318 0 : None => bail!(
319 0 : "couldn't find common point in histories, donor {:?}, sk {:?}",
320 0 : donor_th,
321 0 : vote_response.term_history,
322 0 : ),
323 0 : Some(lcp) => lcp,
324 0 : };
325 0 : info!("found last common point at {:?}", last_common_point);
326 :
327 : // truncate WAL locally
328 0 : let pe = ProposerAcceptorMessage::Elected(ProposerElected {
329 0 : generation: INVALID_GENERATION,
330 0 : term: donor.term,
331 0 : start_streaming_at: last_common_point.lsn,
332 0 : term_history: donor_th,
333 0 : });
334 0 : // Successful ProposerElected handling always returns None. If term changed,
335 0 : // we'll find out that during the streaming. Note: it is expected to get
336 0 : // 'refusing to overwrite correct WAL' here if walproposer reconnected
337 0 : // concurrently, restart helps here.
338 0 : tli.process_msg(&pe)
339 0 : .await
340 0 : .context("ProposerElected handling")?;
341 :
342 0 : recovery_stream(tli, donor, last_common_point.lsn, conf).await
343 0 : }
344 :
345 : // Pull WAL from donor, assuming handshake is already done.
346 0 : async fn recovery_stream(
347 0 : tli: WalResidentTimeline,
348 0 : donor: &Donor,
349 0 : start_streaming_at: Lsn,
350 0 : conf: &SafeKeeperConf,
351 0 : ) -> anyhow::Result<String> {
352 0 : // TODO: pass auth token
353 0 : let connection_conf_args = ConnectionConfigArgs {
354 0 : protocol: PostgresClientProtocol::Vanilla,
355 0 : ttid: tli.ttid,
356 0 : shard_number: None,
357 0 : shard_count: None,
358 0 : shard_stripe_size: None,
359 0 : listen_pg_addr_str: &donor.pg_connstr,
360 0 : auth_token: None,
361 0 : availability_zone: None,
362 0 : };
363 0 : let cfg = wal_stream_connection_config(connection_conf_args)?;
364 0 : let mut cfg = cfg.to_tokio_postgres_config();
365 0 : // It will make safekeeper give out not committed WAL (up to flush_lsn).
366 0 : cfg.application_name(&format!("safekeeper_{}", conf.my_id));
367 0 : cfg.replication_mode(tokio_postgres::config::ReplicationMode::Physical);
368 0 :
369 0 : let connect_timeout = Duration::from_millis(10000);
370 0 : let (client, connection) = match time::timeout(
371 0 : connect_timeout,
372 0 : cfg.connect(tokio_postgres::NoTls),
373 0 : )
374 0 : .await
375 : {
376 0 : Ok(client_and_conn) => client_and_conn?,
377 0 : Err(_elapsed) => {
378 0 : bail!(
379 0 : "timed out while waiting {connect_timeout:?} for connection to peer safekeeper to open"
380 0 : );
381 : }
382 : };
383 0 : trace!("connected to {:?}", donor);
384 :
385 : // The connection object performs the actual communication with the
386 : // server, spawn it off to run on its own.
387 0 : let ttid = tli.ttid;
388 0 : tokio::spawn(async move {
389 0 : if let Err(e) = connection
390 0 : .instrument(info_span!("recovery task connection poll", ttid = %ttid))
391 0 : .await
392 : {
393 : // This logging isn't very useful as error is anyway forwarded to client.
394 0 : trace!(
395 0 : "tokio_postgres connection object finished with error: {}",
396 : e
397 : );
398 0 : }
399 0 : });
400 0 :
401 0 : let query = format!(
402 0 : "START_REPLICATION PHYSICAL {} (term='{}')",
403 0 : start_streaming_at, donor.term
404 0 : );
405 :
406 0 : let copy_stream = client.copy_both_simple(&query).await?;
407 0 : let physical_stream = ReplicationStream::new(copy_stream);
408 0 :
409 0 : // As in normal walreceiver, do networking and writing to disk in parallel.
410 0 : let (msg_tx, msg_rx) = channel(MSG_QUEUE_SIZE);
411 0 : let (reply_tx, reply_rx) = channel(REPLY_QUEUE_SIZE);
412 0 : let wa = WalAcceptor::spawn(tli.wal_residence_guard().await?, msg_rx, reply_tx, None);
413 :
414 0 : let res = tokio::select! {
415 0 : r = network_io(physical_stream, msg_tx, donor.clone(), tli, conf.clone()) => r,
416 0 : r = read_replies(reply_rx, donor.term) => r.map(|()| None),
417 : };
418 :
419 : // Join the spawned WalAcceptor. At this point chans to/from it passed to
420 : // network routines are dropped, so it will exit as soon as it touches them.
421 0 : match wa.await {
422 : Ok(Ok(())) => {
423 : // WalAcceptor finished normally, termination reason is different
424 0 : match res {
425 0 : Ok(Some(success_desc)) => Ok(success_desc),
426 0 : Ok(None) => bail!("unexpected recovery end without error/success"), // can't happen
427 0 : Err(e) => Err(e), // network error or term change
428 : }
429 : }
430 0 : Ok(Err(e)) => Err(e), // error while processing message
431 0 : Err(e) => bail!("WalAcceptor panicked: {}", e),
432 : }
433 0 : }
434 :
435 : // Perform network part of streaming: read data and push it to msg_tx, send KA
436 : // to make sender hear from us. If there is nothing coming for a while, check
437 : // for termination.
438 : // Returns
439 : // - Ok(None) if channel to WalAcceptor closed -- its task should return error.
440 : // - Ok(Some(String)) if recovery successfully completed.
441 : // - Err if error happened while reading/writing to socket.
442 0 : async fn network_io(
443 0 : physical_stream: ReplicationStream,
444 0 : msg_tx: Sender<ProposerAcceptorMessage>,
445 0 : donor: Donor,
446 0 : tli: WalResidentTimeline,
447 0 : conf: SafeKeeperConf,
448 0 : ) -> anyhow::Result<Option<String>> {
449 0 : let mut physical_stream = pin!(physical_stream);
450 0 : let mut last_received_lsn = Lsn::INVALID;
451 0 : // tear down connection if no data arrives withing this period
452 0 : let no_data_timeout = Duration::from_millis(30000);
453 :
454 : loop {
455 0 : let msg = match timeout(no_data_timeout, physical_stream.next()).await {
456 0 : Ok(next) => match next {
457 0 : None => bail!("unexpected end of replication stream"),
458 0 : Some(msg) => msg.context("get replication message")?,
459 : },
460 0 : Err(_) => bail!("no message received within {:?}", no_data_timeout),
461 : };
462 :
463 0 : match msg {
464 0 : ReplicationMessage::XLogData(xlog_data) => {
465 0 : let ar_hdr = AppendRequestHeader {
466 0 : generation: INVALID_GENERATION,
467 0 : term: donor.term,
468 0 : begin_lsn: Lsn(xlog_data.wal_start()),
469 0 : end_lsn: Lsn(xlog_data.wal_start()) + xlog_data.data().len() as u64,
470 0 : commit_lsn: Lsn::INVALID, // do not attempt to advance, peer communication anyway does it
471 0 : truncate_lsn: Lsn::INVALID, // do not attempt to advance
472 0 : };
473 0 : let ar = AppendRequest {
474 0 : h: ar_hdr,
475 0 : wal_data: xlog_data.into_data(),
476 0 : };
477 0 : trace!(
478 0 : "processing AppendRequest {}-{}, len {}",
479 0 : ar.h.begin_lsn,
480 0 : ar.h.end_lsn,
481 0 : ar.wal_data.len()
482 : );
483 0 : last_received_lsn = ar.h.end_lsn;
484 0 : if msg_tx
485 0 : .send(ProposerAcceptorMessage::AppendRequest(ar))
486 0 : .await
487 0 : .is_err()
488 : {
489 0 : return Ok(None); // chan closed, WalAcceptor terminated
490 0 : }
491 : }
492 : ReplicationMessage::PrimaryKeepAlive(_) => {
493 : // keepalive means nothing is being streamed for a while. Check whether we need to stop.
494 0 : let recovery_needed_info = recovery_needed(&tli, conf.heartbeat_timeout).await;
495 : // do current donors still contain one we currently connected to?
496 0 : if !recovery_needed_info
497 0 : .donors
498 0 : .iter()
499 0 : .any(|d| d.sk_id == donor.sk_id)
500 : {
501 : // Most likely it means we are caughtup.
502 : // note: just exiting makes tokio_postgres send CopyFail to the far end.
503 0 : return Ok(Some(format!(
504 0 : "terminating at {} as connected safekeeper {} with term {} is not a donor anymore: {}",
505 0 : last_received_lsn, donor.sk_id, donor.term, recovery_needed_info
506 0 : )));
507 0 : }
508 : }
509 0 : _ => {}
510 : }
511 : // Send reply to each message to keep connection alive. Ideally we
512 : // should do that once in a while instead, but this again requires
513 : // stream split or similar workaround, and recovery is anyway not that
514 : // performance critical.
515 : //
516 : // We do not know here real write/flush LSNs (need to take mutex again
517 : // or check replies which are read in different future), but neither
518 : // sender much cares about them, so just send last received.
519 0 : physical_stream
520 0 : .as_mut()
521 0 : .standby_status_update(
522 0 : PgLsn::from(last_received_lsn.0),
523 0 : PgLsn::from(last_received_lsn.0),
524 0 : PgLsn::from(last_received_lsn.0),
525 0 : SystemTime::now(),
526 0 : 0,
527 0 : )
528 0 : .await?;
529 : }
530 0 : }
531 :
532 : // Read replies from WalAcceptor. We are not interested much in sending them to
533 : // donor safekeeper, so don't route them anywhere. However, we should check if
534 : // term changes and exit if it does.
535 : // Returns Ok(()) if channel closed, Err in case of term change.
536 0 : async fn read_replies(
537 0 : mut reply_rx: Receiver<AcceptorProposerMessage>,
538 0 : donor_term: Term,
539 0 : ) -> anyhow::Result<()> {
540 : loop {
541 0 : match reply_rx.recv().await {
542 0 : Some(msg) => {
543 0 : if let AcceptorProposerMessage::AppendResponse(ar) = msg {
544 0 : if ar.term != donor_term {
545 0 : bail!("donor term changed from {} to {}", donor_term, ar.term);
546 0 : }
547 0 : }
548 : }
549 0 : None => return Ok(()), // chan closed, WalAcceptor terminated
550 : }
551 : }
552 0 : }
|