Line data Source code
1 : //! WAL receiver logic that ensures the pageserver gets connectected to safekeeper,
2 : //! that contains the latest WAL to stream and this connection does not go stale.
3 : //!
4 : //! To achieve that, a storage broker is used: safekepers propagate their timelines' state in it,
5 : //! the manager subscribes for changes and accumulates those to query the one with the biggest Lsn for connection.
6 : //! Current connection state is tracked too, to ensure it's not getting stale.
7 : //!
8 : //! After every connection or storage broker update fetched, the state gets updated correspondingly and rechecked for the new conneciton leader,
9 : //! then a (re)connection happens, if necessary.
10 : //! Only WAL streaming task expects to be finished, other loops (storage broker, connection management) never exit unless cancelled explicitly via the dedicated channel.
11 :
12 : use std::{collections::HashMap, num::NonZeroU64, ops::ControlFlow, sync::Arc, time::Duration};
13 :
14 : use super::{TaskStateUpdate, WalReceiverConf};
15 : use crate::context::{DownloadBehavior, RequestContext};
16 : use crate::metrics::{
17 : WALRECEIVER_ACTIVE_MANAGERS, WALRECEIVER_BROKER_UPDATES, WALRECEIVER_CANDIDATES_ADDED,
18 : WALRECEIVER_CANDIDATES_REMOVED, WALRECEIVER_SWITCHES,
19 : };
20 : use crate::task_mgr::TaskKind;
21 : use crate::tenant::{debug_assert_current_span_has_tenant_and_timeline_id, Timeline};
22 : use anyhow::Context;
23 : use chrono::{NaiveDateTime, Utc};
24 : use pageserver_api::models::TimelineState;
25 :
26 : use storage_broker::proto::TenantTimelineId as ProtoTenantTimelineId;
27 : use storage_broker::proto::{
28 : FilterTenantTimelineId, MessageType, SafekeeperDiscoveryRequest, SafekeeperDiscoveryResponse,
29 : SubscribeByFilterRequest, TypeSubscription, TypedMessage,
30 : };
31 : use storage_broker::{BrokerClientChannel, Code, Streaming};
32 : use tokio_util::sync::CancellationToken;
33 : use tracing::*;
34 :
35 : use postgres_connection::PgConnectionConfig;
36 : use utils::backoff::{
37 : exponential_backoff, DEFAULT_BASE_BACKOFF_SECONDS, DEFAULT_MAX_BACKOFF_SECONDS,
38 : };
39 : use utils::postgres_client::{
40 : wal_stream_connection_config, ConnectionConfigArgs, PostgresClientProtocol,
41 : };
42 : use utils::{
43 : id::{NodeId, TenantTimelineId},
44 : lsn::Lsn,
45 : };
46 :
47 : use super::{
48 : walreceiver_connection::WalConnectionStatus, walreceiver_connection::WalReceiverError,
49 : TaskEvent, TaskHandle,
50 : };
51 :
52 : pub(crate) struct Cancelled;
53 :
54 : /// Attempts to subscribe for timeline updates, pushed by safekeepers into the broker.
55 : /// Based on the updates, desides whether to start, keep or stop a WAL receiver task.
56 : /// If storage broker subscription is cancelled, exits.
57 : ///
58 : /// # Cancel-Safety
59 : ///
60 : /// Not cancellation-safe. Use `cancel` token to request cancellation.
61 0 : pub(super) async fn connection_manager_loop_step(
62 0 : broker_client: &mut BrokerClientChannel,
63 0 : connection_manager_state: &mut ConnectionManagerState,
64 0 : ctx: &RequestContext,
65 0 : cancel: &CancellationToken,
66 0 : manager_status: &std::sync::RwLock<Option<ConnectionManagerStatus>>,
67 0 : ) -> Result<(), Cancelled> {
68 0 : match tokio::select! {
69 0 : _ = cancel.cancelled() => { return Err(Cancelled); },
70 0 : st = connection_manager_state.timeline.wait_to_become_active(ctx) => { st }
71 : } {
72 0 : Ok(()) => {}
73 0 : Err(new_state) => {
74 0 : debug!(
75 : ?new_state,
76 0 : "state changed, stopping wal connection manager loop"
77 : );
78 0 : return Err(Cancelled);
79 : }
80 : }
81 :
82 0 : WALRECEIVER_ACTIVE_MANAGERS.inc();
83 0 : scopeguard::defer! {
84 0 : WALRECEIVER_ACTIVE_MANAGERS.dec();
85 0 : }
86 0 :
87 0 : let id = TenantTimelineId {
88 0 : tenant_id: connection_manager_state.timeline.tenant_shard_id.tenant_id,
89 0 : timeline_id: connection_manager_state.timeline.timeline_id,
90 0 : };
91 0 :
92 0 : let mut timeline_state_updates = connection_manager_state
93 0 : .timeline
94 0 : .subscribe_for_state_updates();
95 0 :
96 0 : let mut wait_lsn_status = connection_manager_state
97 0 : .timeline
98 0 : .subscribe_for_wait_lsn_updates();
99 0 :
100 0 : // TODO: create a separate config option for discovery request interval
101 0 : let discovery_request_interval = connection_manager_state.conf.lagging_wal_timeout;
102 0 : let mut last_discovery_ts: Option<std::time::Instant> = None;
103 :
104 : // Subscribe to the broker updates. Stream shares underlying TCP connection
105 : // with other streams on this client (other connection managers). When
106 : // object goes out of scope, stream finishes in drop() automatically.
107 0 : let mut broker_subscription = subscribe_for_timeline_updates(broker_client, id, cancel).await?;
108 0 : debug!("Subscribed for broker timeline updates");
109 :
110 : loop {
111 0 : let time_until_next_retry = connection_manager_state.time_until_next_retry();
112 0 : let any_activity = connection_manager_state.wal_connection.is_some()
113 0 : || !connection_manager_state.wal_stream_candidates.is_empty();
114 :
115 : // These things are happening concurrently:
116 : //
117 : // - cancellation request
118 : // - keep receiving WAL on the current connection
119 : // - if the shared state says we need to change connection, disconnect and return
120 : // - this runs in a separate task and we receive updates via a watch channel
121 : // - change connection if the rules decide so, or if the current connection dies
122 : // - receive updates from broker
123 : // - this might change the current desired connection
124 : // - timeline state changes to something that does not allow walreceiver to run concurrently
125 : // - if there's no connection and no candidates, try to send a discovery request
126 :
127 : // NB: make sure each of the select expressions are cancellation-safe
128 : // (no need for arms to be cancellation-safe).
129 0 : tokio::select! {
130 0 : _ = cancel.cancelled() => { return Err(Cancelled); }
131 0 : Some(wal_connection_update) = async {
132 0 : match connection_manager_state.wal_connection.as_mut() {
133 0 : Some(wal_connection) => Some(wal_connection.connection_task.next_task_event().await),
134 0 : None => None,
135 : }
136 0 : } => {
137 0 : let wal_connection = connection_manager_state.wal_connection.as_mut()
138 0 : .expect("Should have a connection, as checked by the corresponding select! guard");
139 0 : match wal_connection_update {
140 0 : TaskEvent::Update(TaskStateUpdate::Started) => {},
141 0 : TaskEvent::Update(TaskStateUpdate::Progress(new_status)) => {
142 0 : if new_status.has_processed_wal {
143 0 : // We have advanced last_record_lsn by processing the WAL received
144 0 : // from this safekeeper. This is good enough to clean unsuccessful
145 0 : // retries history and allow reconnecting to this safekeeper without
146 0 : // sleeping for a long time.
147 0 : connection_manager_state.wal_connection_retries.remove(&wal_connection.sk_id);
148 0 : }
149 0 : wal_connection.status = new_status;
150 : }
151 0 : TaskEvent::End(walreceiver_task_result) => {
152 0 : match walreceiver_task_result {
153 0 : Ok(()) => debug!("WAL receiving task finished"),
154 0 : Err(e) => error!("wal receiver task finished with an error: {e:?}"),
155 : }
156 0 : connection_manager_state.drop_old_connection(false).await;
157 : },
158 : }
159 : },
160 :
161 : // Got a new update from the broker
162 0 : broker_update = broker_subscription.message() /* TODO: review cancellation-safety */ => {
163 0 : match broker_update {
164 0 : Ok(Some(broker_update)) => connection_manager_state.register_timeline_update(broker_update),
165 0 : Err(status) => {
166 0 : match status.code() {
167 0 : Code::Unknown if status.message().contains("stream closed because of a broken pipe") || status.message().contains("connection reset") => {
168 0 : // tonic's error handling doesn't provide a clear code for disconnections: we get
169 0 : // "h2 protocol error: error reading a body from connection: stream closed because of a broken pipe"
170 0 : info!("broker disconnected: {status}");
171 : },
172 : _ => {
173 0 : warn!("broker subscription failed: {status}");
174 : }
175 : }
176 0 : return Ok(());
177 : }
178 : Ok(None) => {
179 0 : error!("broker subscription stream ended"); // can't happen
180 0 : return Ok(());
181 : }
182 : }
183 : },
184 :
185 0 : new_event = async {
186 : // Reminder: this match arm needs to be cancellation-safe.
187 : loop {
188 0 : if connection_manager_state.timeline.current_state() == TimelineState::Loading {
189 0 : warn!("wal connection manager should only be launched after timeline has become active");
190 0 : }
191 0 : match timeline_state_updates.changed().await {
192 : Ok(()) => {
193 0 : let new_state = connection_manager_state.timeline.current_state();
194 0 : match new_state {
195 : // we're already active as walreceiver, no need to reactivate
196 0 : TimelineState::Active => continue,
197 : TimelineState::Broken { .. } | TimelineState::Stopping => {
198 0 : debug!("timeline entered terminal state {new_state:?}, stopping wal connection manager loop");
199 0 : return ControlFlow::Break(());
200 : }
201 : TimelineState::Loading => {
202 0 : warn!("timeline transitioned back to Loading state, that should not happen");
203 0 : return ControlFlow::Continue(());
204 : }
205 : }
206 : }
207 0 : Err(_sender_dropped_error) => return ControlFlow::Break(()),
208 : }
209 : }
210 0 : } => match new_event {
211 : ControlFlow::Continue(()) => {
212 0 : return Ok(());
213 : }
214 : ControlFlow::Break(()) => {
215 0 : debug!("Timeline is no longer active, stopping wal connection manager loop");
216 0 : return Err(Cancelled);
217 : }
218 : },
219 :
220 0 : Some(()) = async {
221 0 : match time_until_next_retry {
222 0 : Some(sleep_time) => {
223 0 : tokio::time::sleep(sleep_time).await;
224 0 : Some(())
225 : },
226 : None => {
227 0 : debug!("No candidates to retry, waiting indefinitely for the broker events");
228 0 : None
229 : }
230 : }
231 0 : } => debug!("Waking up for the next retry after waiting for {time_until_next_retry:?}"),
232 :
233 0 : Some(()) = async {
234 0 : // Reminder: this match arm needs to be cancellation-safe.
235 0 : // Calculating time needed to wait until sending the next discovery request.
236 0 : // Current implementation is conservative and sends discovery requests only when there are no candidates.
237 0 :
238 0 : if any_activity {
239 : // No need to send discovery requests if there is an active connection or candidates.
240 0 : return None;
241 0 : }
242 :
243 : // Waiting for an active wait_lsn request.
244 0 : while wait_lsn_status.borrow().is_none() {
245 0 : if wait_lsn_status.changed().await.is_err() {
246 : // wait_lsn_status channel was closed, exiting
247 0 : warn!("wait_lsn_status channel was closed in connection_manager_loop_step");
248 0 : return None;
249 0 : }
250 : }
251 :
252 : // All preconditions met, preparing to send a discovery request.
253 0 : let now = std::time::Instant::now();
254 0 : let next_discovery_ts = last_discovery_ts
255 0 : .map(|ts| ts + discovery_request_interval)
256 0 : .unwrap_or_else(|| now);
257 0 :
258 0 : if next_discovery_ts > now {
259 : // Prevent sending discovery requests too frequently.
260 0 : tokio::time::sleep(next_discovery_ts - now).await;
261 0 : }
262 :
263 0 : let tenant_timeline_id = Some(ProtoTenantTimelineId {
264 0 : tenant_id: id.tenant_id.as_ref().to_owned(),
265 0 : timeline_id: id.timeline_id.as_ref().to_owned(),
266 0 : });
267 0 : let request = SafekeeperDiscoveryRequest { tenant_timeline_id };
268 0 : let msg = TypedMessage {
269 0 : r#type: MessageType::SafekeeperDiscoveryRequest as i32,
270 0 : safekeeper_timeline_info: None,
271 0 : safekeeper_discovery_request: Some(request),
272 0 : safekeeper_discovery_response: None,
273 0 : };
274 0 :
275 0 : last_discovery_ts = Some(std::time::Instant::now());
276 0 : debug!("No active connection and no candidates, sending discovery request to the broker");
277 :
278 : // Cancellation safety: we want to send a message to the broker, but publish_one()
279 : // function can get cancelled by the other select! arm. This is absolutely fine, because
280 : // we just want to receive broker updates and discovery is not important if we already
281 : // receive updates.
282 : //
283 : // It is possible that `last_discovery_ts` will be updated, but the message will not be sent.
284 : // This is totally fine because of the reason above.
285 :
286 : // This is a fire-and-forget request, we don't care about the response
287 0 : let _ = broker_client.publish_one(msg).await;
288 0 : debug!("Discovery request sent to the broker");
289 0 : None
290 0 : } => {}
291 : }
292 :
293 0 : if let Some(new_candidate) = connection_manager_state.next_connection_candidate() {
294 0 : info!("Switching to new connection candidate: {new_candidate:?}");
295 0 : connection_manager_state
296 0 : .change_connection(new_candidate, ctx)
297 0 : .await
298 0 : }
299 0 : *manager_status.write().unwrap() = Some(connection_manager_state.manager_status());
300 : }
301 0 : }
302 :
303 : /// Endlessly try to subscribe for broker updates for a given timeline.
304 0 : async fn subscribe_for_timeline_updates(
305 0 : broker_client: &mut BrokerClientChannel,
306 0 : id: TenantTimelineId,
307 0 : cancel: &CancellationToken,
308 0 : ) -> Result<Streaming<TypedMessage>, Cancelled> {
309 0 : let mut attempt = 0;
310 : loop {
311 0 : exponential_backoff(
312 0 : attempt,
313 0 : DEFAULT_BASE_BACKOFF_SECONDS,
314 0 : DEFAULT_MAX_BACKOFF_SECONDS,
315 0 : cancel,
316 0 : )
317 0 : .await;
318 0 : attempt += 1;
319 0 :
320 0 : // subscribe to the specific timeline
321 0 : let request = SubscribeByFilterRequest {
322 0 : types: vec![
323 0 : TypeSubscription {
324 0 : r#type: MessageType::SafekeeperTimelineInfo as i32,
325 0 : },
326 0 : TypeSubscription {
327 0 : r#type: MessageType::SafekeeperDiscoveryResponse as i32,
328 0 : },
329 0 : ],
330 0 : tenant_timeline_id: Some(FilterTenantTimelineId {
331 0 : enabled: true,
332 0 : tenant_timeline_id: Some(ProtoTenantTimelineId {
333 0 : tenant_id: id.tenant_id.as_ref().to_owned(),
334 0 : timeline_id: id.timeline_id.as_ref().to_owned(),
335 0 : }),
336 0 : }),
337 0 : };
338 0 :
339 0 : match {
340 0 : tokio::select! {
341 0 : r = broker_client.subscribe_by_filter(request) => { r }
342 0 : _ = cancel.cancelled() => { return Err(Cancelled); }
343 : }
344 : } {
345 0 : Ok(resp) => {
346 0 : return Ok(resp.into_inner());
347 : }
348 0 : Err(e) => {
349 0 : // Safekeeper nodes can stop pushing timeline updates to the broker, when no new writes happen and
350 0 : // entire WAL is streamed. Keep this noticeable with logging, but do not warn/error.
351 0 : info!("Attempt #{attempt}, failed to subscribe for timeline {id} updates in broker: {e:#}");
352 0 : continue;
353 : }
354 : }
355 : }
356 0 : }
357 :
358 : const WALCONNECTION_RETRY_MIN_BACKOFF_SECONDS: f64 = 0.1;
359 : const WALCONNECTION_RETRY_MAX_BACKOFF_SECONDS: f64 = 15.0;
360 : const WALCONNECTION_RETRY_BACKOFF_MULTIPLIER: f64 = 1.5;
361 :
362 : /// All data that's needed to run endless broker loop and keep the WAL streaming connection alive, if possible.
363 : pub(super) struct ConnectionManagerState {
364 : id: TenantTimelineId,
365 : /// Use pageserver data about the timeline to filter out some of the safekeepers.
366 : timeline: Arc<Timeline>,
367 : /// Child token of [`super::WalReceiver::cancel`], inherited to all tasks we spawn.
368 : cancel: CancellationToken,
369 : conf: WalReceiverConf,
370 : /// Current connection to safekeeper for WAL streaming.
371 : wal_connection: Option<WalConnection>,
372 : /// Info about retries and unsuccessful attempts to connect to safekeepers.
373 : wal_connection_retries: HashMap<NodeId, RetryInfo>,
374 : /// Data about all timelines, available for connection, fetched from storage broker, grouped by their corresponding safekeeper node id.
375 : wal_stream_candidates: HashMap<NodeId, BrokerSkTimeline>,
376 : }
377 :
378 : /// An information about connection manager's current connection and connection candidates.
379 : #[derive(Debug, Clone)]
380 : pub struct ConnectionManagerStatus {
381 : existing_connection: Option<WalConnectionStatus>,
382 : wal_stream_candidates: HashMap<NodeId, BrokerSkTimeline>,
383 : }
384 :
385 : impl ConnectionManagerStatus {
386 : /// Generates a string, describing current connection status in a form, suitable for logging.
387 0 : pub fn to_human_readable_string(&self) -> String {
388 0 : let mut resulting_string = String::new();
389 0 : match &self.existing_connection {
390 0 : Some(connection) => {
391 0 : if connection.has_processed_wal {
392 0 : resulting_string.push_str(&format!(
393 0 : " (update {}): streaming WAL from node {}, ",
394 0 : connection.latest_wal_update.format("%Y-%m-%d %H:%M:%S"),
395 0 : connection.node,
396 0 : ));
397 0 :
398 0 : match (connection.streaming_lsn, connection.commit_lsn) {
399 0 : (None, None) => resulting_string.push_str("no streaming data"),
400 0 : (None, Some(commit_lsn)) => {
401 0 : resulting_string.push_str(&format!("commit Lsn: {commit_lsn}"))
402 : }
403 0 : (Some(streaming_lsn), None) => {
404 0 : resulting_string.push_str(&format!("streaming Lsn: {streaming_lsn}"))
405 : }
406 0 : (Some(streaming_lsn), Some(commit_lsn)) => resulting_string.push_str(
407 0 : &format!("commit|streaming Lsn: {commit_lsn}|{streaming_lsn}"),
408 0 : ),
409 : }
410 0 : } else if connection.is_connected {
411 0 : resulting_string.push_str(&format!(
412 0 : " (update {}): connecting to node {}",
413 0 : connection
414 0 : .latest_connection_update
415 0 : .format("%Y-%m-%d %H:%M:%S"),
416 0 : connection.node,
417 0 : ));
418 0 : } else {
419 0 : resulting_string.push_str(&format!(
420 0 : " (update {}): initializing node {} connection",
421 0 : connection
422 0 : .latest_connection_update
423 0 : .format("%Y-%m-%d %H:%M:%S"),
424 0 : connection.node,
425 0 : ));
426 0 : }
427 : }
428 0 : None => resulting_string.push_str(": disconnected"),
429 : }
430 :
431 0 : resulting_string.push_str(", safekeeper candidates (id|update_time|commit_lsn): [");
432 0 : let mut candidates = self.wal_stream_candidates.iter().peekable();
433 0 : while let Some((node_id, candidate_info)) = candidates.next() {
434 0 : resulting_string.push_str(&format!(
435 0 : "({}|{}|{})",
436 0 : node_id,
437 0 : candidate_info.latest_update.format("%H:%M:%S"),
438 0 : Lsn(candidate_info.timeline.commit_lsn)
439 0 : ));
440 0 : if candidates.peek().is_some() {
441 0 : resulting_string.push_str(", ");
442 0 : }
443 : }
444 0 : resulting_string.push(']');
445 0 :
446 0 : resulting_string
447 0 : }
448 : }
449 :
450 : /// Current connection data.
451 : #[derive(Debug)]
452 : struct WalConnection {
453 : /// Time when the connection was initiated.
454 : started_at: NaiveDateTime,
455 : /// Current safekeeper pageserver is connected to for WAL streaming.
456 : sk_id: NodeId,
457 : /// Availability zone of the safekeeper.
458 : availability_zone: Option<String>,
459 : /// Status of the connection.
460 : status: WalConnectionStatus,
461 : /// WAL streaming task handle.
462 : connection_task: TaskHandle<WalConnectionStatus>,
463 : /// Have we discovered that other safekeeper has more recent WAL than we do?
464 : discovered_new_wal: Option<NewCommittedWAL>,
465 : }
466 :
467 : /// Notion of a new committed WAL, which exists on other safekeeper.
468 : #[derive(Debug, Clone, Copy)]
469 : struct NewCommittedWAL {
470 : /// LSN of the new committed WAL.
471 : lsn: Lsn,
472 : /// When we discovered that the new committed WAL exists on other safekeeper.
473 : discovered_at: NaiveDateTime,
474 : }
475 :
476 : #[derive(Debug, Clone, Copy)]
477 : struct RetryInfo {
478 : next_retry_at: Option<NaiveDateTime>,
479 : retry_duration_seconds: f64,
480 : }
481 :
482 : /// Data about the timeline to connect to, received from the broker.
483 : #[derive(Debug, Clone)]
484 : struct BrokerSkTimeline {
485 : timeline: SafekeeperDiscoveryResponse,
486 : /// Time at which the data was fetched from the broker last time, to track the stale data.
487 : latest_update: NaiveDateTime,
488 : }
489 :
490 : impl ConnectionManagerState {
491 0 : pub(super) fn new(
492 0 : timeline: Arc<Timeline>,
493 0 : conf: WalReceiverConf,
494 0 : cancel: CancellationToken,
495 0 : ) -> Self {
496 0 : let id = TenantTimelineId {
497 0 : tenant_id: timeline.tenant_shard_id.tenant_id,
498 0 : timeline_id: timeline.timeline_id,
499 0 : };
500 0 : Self {
501 0 : id,
502 0 : timeline,
503 0 : cancel,
504 0 : conf,
505 0 : wal_connection: None,
506 0 : wal_stream_candidates: HashMap::new(),
507 0 : wal_connection_retries: HashMap::new(),
508 0 : }
509 0 : }
510 :
511 10 : fn spawn<Fut>(
512 10 : &self,
513 10 : task: impl FnOnce(
514 10 : tokio::sync::watch::Sender<TaskStateUpdate<WalConnectionStatus>>,
515 10 : CancellationToken,
516 10 : ) -> Fut
517 10 : + Send
518 10 : + 'static,
519 10 : ) -> TaskHandle<WalConnectionStatus>
520 10 : where
521 10 : Fut: std::future::Future<Output = anyhow::Result<()>> + Send,
522 10 : {
523 10 : // TODO: get rid of TaskHandle
524 10 : super::TaskHandle::spawn(&self.cancel, task)
525 10 : }
526 :
527 : /// Shuts down the current connection (if any) and immediately starts another one with the given connection string.
528 0 : async fn change_connection(&mut self, new_sk: NewWalConnectionCandidate, ctx: &RequestContext) {
529 0 : WALRECEIVER_SWITCHES
530 0 : .with_label_values(&[new_sk.reason.name()])
531 0 : .inc();
532 0 :
533 0 : self.drop_old_connection(true).await;
534 :
535 0 : let node_id = new_sk.safekeeper_id;
536 0 : let connect_timeout = self.conf.wal_connect_timeout;
537 0 : let ingest_batch_size = self.conf.ingest_batch_size;
538 0 : let protocol = self.conf.protocol;
539 0 : let timeline = Arc::clone(&self.timeline);
540 0 : let ctx = ctx.detached_child(
541 0 : TaskKind::WalReceiverConnectionHandler,
542 0 : DownloadBehavior::Download,
543 0 : );
544 :
545 0 : let span = info_span!("connection", %node_id);
546 0 : let connection_handle = self.spawn(move |events_sender, cancellation| {
547 0 : async move {
548 0 : debug_assert_current_span_has_tenant_and_timeline_id();
549 :
550 0 : let res = super::walreceiver_connection::handle_walreceiver_connection(
551 0 : timeline,
552 0 : protocol,
553 0 : new_sk.wal_source_connconf,
554 0 : events_sender,
555 0 : cancellation.clone(),
556 0 : connect_timeout,
557 0 : ctx,
558 0 : node_id,
559 0 : ingest_batch_size,
560 0 : )
561 0 : .await;
562 :
563 0 : match res {
564 0 : Ok(()) => Ok(()),
565 0 : Err(e) => {
566 0 : match e {
567 0 : WalReceiverError::SuccessfulCompletion(msg) => {
568 0 : info!("walreceiver connection handling ended with success: {msg}");
569 0 : Ok(())
570 : }
571 0 : WalReceiverError::ExpectedSafekeeperError(e) => {
572 0 : info!("walreceiver connection handling ended: {e}");
573 0 : Ok(())
574 : }
575 : WalReceiverError::ClosedGate => {
576 0 : info!(
577 0 : "walreceiver connection handling ended because of closed gate"
578 : );
579 0 : Ok(())
580 : }
581 0 : WalReceiverError::Other(e) => {
582 0 : // give out an error to have task_mgr give it a really verbose logging
583 0 : if cancellation.is_cancelled() {
584 : // Ideally we would learn about this via some path other than Other, but
585 : // that requires refactoring all the intermediate layers of ingest code
586 : // that only emit anyhow::Error
587 0 : Ok(())
588 : } else {
589 0 : Err(e).context("walreceiver connection handling failure")
590 : }
591 : }
592 : }
593 : }
594 : }
595 0 : }
596 0 : .instrument(span)
597 0 : });
598 0 :
599 0 : let now = Utc::now().naive_utc();
600 0 : self.wal_connection = Some(WalConnection {
601 0 : started_at: now,
602 0 : sk_id: new_sk.safekeeper_id,
603 0 : availability_zone: new_sk.availability_zone,
604 0 : status: WalConnectionStatus {
605 0 : is_connected: false,
606 0 : has_processed_wal: false,
607 0 : latest_connection_update: now,
608 0 : latest_wal_update: now,
609 0 : streaming_lsn: None,
610 0 : commit_lsn: None,
611 0 : node: node_id,
612 0 : },
613 0 : connection_task: connection_handle,
614 0 : discovered_new_wal: None,
615 0 : });
616 0 : }
617 :
618 : /// Drops the current connection (if any) and updates retry timeout for the next
619 : /// connection attempt to the same safekeeper.
620 : ///
621 : /// # Cancel-Safety
622 : ///
623 : /// Not cancellation-safe.
624 0 : async fn drop_old_connection(&mut self, needs_shutdown: bool) {
625 0 : let wal_connection = match self.wal_connection.take() {
626 0 : Some(wal_connection) => wal_connection,
627 0 : None => return,
628 : };
629 :
630 0 : if needs_shutdown {
631 0 : wal_connection
632 0 : .connection_task
633 0 : .shutdown()
634 : // This here is why this function isn't cancellation-safe.
635 : // If we got cancelled here, then self.wal_connection is already None and we lose track of the task.
636 : // Even if our caller diligently calls Self::shutdown(), it will find a self.wal_connection=None
637 : // and thus be ineffective.
638 0 : .await;
639 0 : }
640 :
641 0 : let retry = self
642 0 : .wal_connection_retries
643 0 : .entry(wal_connection.sk_id)
644 0 : .or_insert(RetryInfo {
645 0 : next_retry_at: None,
646 0 : retry_duration_seconds: WALCONNECTION_RETRY_MIN_BACKOFF_SECONDS,
647 0 : });
648 0 :
649 0 : let now = Utc::now().naive_utc();
650 0 :
651 0 : // Schedule the next retry attempt. We want to have exponential backoff for connection attempts,
652 0 : // and we add backoff to the time when we started the connection attempt. If the connection
653 0 : // was active for a long time, then next_retry_at will be in the past.
654 0 : retry.next_retry_at =
655 0 : wal_connection
656 0 : .started_at
657 0 : .checked_add_signed(chrono::Duration::milliseconds(
658 0 : (retry.retry_duration_seconds * 1000.0) as i64,
659 0 : ));
660 :
661 0 : if let Some(next) = &retry.next_retry_at {
662 0 : if next > &now {
663 0 : info!(
664 0 : "Next connection retry to {:?} is at {}",
665 : wal_connection.sk_id, next
666 : );
667 0 : }
668 0 : }
669 :
670 0 : let next_retry_duration =
671 0 : retry.retry_duration_seconds * WALCONNECTION_RETRY_BACKOFF_MULTIPLIER;
672 0 : // Clamp the next retry duration to the maximum allowed.
673 0 : let next_retry_duration = next_retry_duration.min(WALCONNECTION_RETRY_MAX_BACKOFF_SECONDS);
674 0 : // Clamp the next retry duration to the minimum allowed.
675 0 : let next_retry_duration = next_retry_duration.max(WALCONNECTION_RETRY_MIN_BACKOFF_SECONDS);
676 0 :
677 0 : retry.retry_duration_seconds = next_retry_duration;
678 0 : }
679 :
680 : /// Returns time needed to wait to have a new candidate for WAL streaming.
681 0 : fn time_until_next_retry(&self) -> Option<Duration> {
682 0 : let now = Utc::now().naive_utc();
683 :
684 0 : let next_retry_at = self
685 0 : .wal_connection_retries
686 0 : .values()
687 0 : .filter_map(|retry| retry.next_retry_at)
688 0 : .filter(|next_retry_at| next_retry_at > &now)
689 0 : .min()?;
690 :
691 0 : (next_retry_at - now).to_std().ok()
692 0 : }
693 :
694 : /// Adds another broker timeline into the state, if its more recent than the one already added there for the same key.
695 0 : fn register_timeline_update(&mut self, typed_msg: TypedMessage) {
696 0 : let mut is_discovery = false;
697 0 : let timeline_update = match typed_msg.r#type() {
698 : MessageType::SafekeeperTimelineInfo => {
699 0 : let info = match typed_msg.safekeeper_timeline_info {
700 0 : Some(info) => info,
701 : None => {
702 0 : warn!("bad proto message from broker: no safekeeper_timeline_info");
703 0 : return;
704 : }
705 : };
706 0 : SafekeeperDiscoveryResponse {
707 0 : safekeeper_id: info.safekeeper_id,
708 0 : tenant_timeline_id: info.tenant_timeline_id,
709 0 : commit_lsn: info.commit_lsn,
710 0 : safekeeper_connstr: info.safekeeper_connstr,
711 0 : availability_zone: info.availability_zone,
712 0 : standby_horizon: info.standby_horizon,
713 0 : }
714 : }
715 : MessageType::SafekeeperDiscoveryResponse => {
716 0 : is_discovery = true;
717 0 : match typed_msg.safekeeper_discovery_response {
718 0 : Some(response) => response,
719 : None => {
720 0 : warn!("bad proto message from broker: no safekeeper_discovery_response");
721 0 : return;
722 : }
723 : }
724 : }
725 : _ => {
726 : // unexpected message
727 0 : return;
728 : }
729 : };
730 :
731 0 : WALRECEIVER_BROKER_UPDATES.inc();
732 0 :
733 0 : trace!(
734 0 : "safekeeper info update: standby_horizon(cutoff)={}",
735 : timeline_update.standby_horizon
736 : );
737 0 : if timeline_update.standby_horizon != 0 {
738 0 : // ignore reports from safekeepers not connected to replicas
739 0 : self.timeline
740 0 : .standby_horizon
741 0 : .store(Lsn(timeline_update.standby_horizon));
742 0 : self.timeline
743 0 : .metrics
744 0 : .standby_horizon_gauge
745 0 : .set(timeline_update.standby_horizon as i64);
746 0 : }
747 :
748 0 : let new_safekeeper_id = NodeId(timeline_update.safekeeper_id);
749 0 : let old_entry = self.wal_stream_candidates.insert(
750 0 : new_safekeeper_id,
751 0 : BrokerSkTimeline {
752 0 : timeline: timeline_update,
753 0 : latest_update: Utc::now().naive_utc(),
754 0 : },
755 0 : );
756 0 :
757 0 : if old_entry.is_none() {
758 0 : info!(
759 : ?is_discovery,
760 : %new_safekeeper_id,
761 0 : "New SK node was added",
762 : );
763 0 : WALRECEIVER_CANDIDATES_ADDED.inc();
764 0 : }
765 0 : }
766 :
767 : /// Cleans up stale broker records and checks the rest for the new connection candidate.
768 : /// Returns a new candidate, if the current state is absent or somewhat lagging, `None` otherwise.
769 : /// The current rules for approving new candidates:
770 : /// * pick a candidate different from the connected safekeeper with biggest `commit_lsn` and lowest failed connection attemps
771 : /// * if there's no such entry, no new candidate found, abort
772 : /// * otherwise check if the candidate is much better than the current one
773 : ///
774 : /// To understand exact rules for determining if the candidate is better than the current one, refer to this function's implementation.
775 : /// General rules are following:
776 : /// * if connected safekeeper is not present, pick the candidate
777 : /// * if we haven't received any updates for some time, pick the candidate
778 : /// * if the candidate commit_lsn is much higher than the current one, pick the candidate
779 : /// * if the candidate commit_lsn is same, but candidate is located in the same AZ as the pageserver, pick the candidate
780 : /// * if connected safekeeper stopped sending us new WAL which is available on other safekeeper, pick the candidate
781 : ///
782 : /// This way we ensure to keep up with the most up-to-date safekeeper and don't try to jump from one safekeeper to another too frequently.
783 : /// Both thresholds are configured per tenant.
784 18 : fn next_connection_candidate(&mut self) -> Option<NewWalConnectionCandidate> {
785 18 : self.cleanup_old_candidates();
786 18 :
787 18 : match &self.wal_connection {
788 10 : Some(existing_wal_connection) => {
789 10 : let connected_sk_node = existing_wal_connection.sk_id;
790 :
791 10 : let (new_sk_id, new_safekeeper_broker_data, new_wal_source_connconf) =
792 10 : self.select_connection_candidate(Some(connected_sk_node))?;
793 10 : let new_availability_zone = new_safekeeper_broker_data.availability_zone.clone();
794 10 :
795 10 : let now = Utc::now().naive_utc();
796 10 : if let Ok(latest_interaciton) =
797 10 : (now - existing_wal_connection.status.latest_connection_update).to_std()
798 : {
799 : // Drop connection if we haven't received keepalive message for a while.
800 10 : if latest_interaciton > self.conf.wal_connect_timeout {
801 2 : return Some(NewWalConnectionCandidate {
802 2 : safekeeper_id: new_sk_id,
803 2 : wal_source_connconf: new_wal_source_connconf,
804 2 : availability_zone: new_availability_zone,
805 2 : reason: ReconnectReason::NoKeepAlives {
806 2 : last_keep_alive: Some(
807 2 : existing_wal_connection.status.latest_connection_update,
808 2 : ),
809 2 : check_time: now,
810 2 : threshold: self.conf.wal_connect_timeout,
811 2 : },
812 2 : });
813 8 : }
814 0 : }
815 :
816 8 : if !existing_wal_connection.status.is_connected {
817 : // We haven't connected yet and we shouldn't switch until connection timeout (condition above).
818 0 : return None;
819 8 : }
820 :
821 8 : if let Some(current_commit_lsn) = existing_wal_connection.status.commit_lsn {
822 8 : let new_commit_lsn = Lsn(new_safekeeper_broker_data.commit_lsn);
823 8 : // Check if the new candidate has much more WAL than the current one.
824 8 : match new_commit_lsn.0.checked_sub(current_commit_lsn.0) {
825 8 : Some(new_sk_lsn_advantage) => {
826 8 : if new_sk_lsn_advantage >= self.conf.max_lsn_wal_lag.get() {
827 2 : return Some(NewWalConnectionCandidate {
828 2 : safekeeper_id: new_sk_id,
829 2 : wal_source_connconf: new_wal_source_connconf,
830 2 : availability_zone: new_availability_zone,
831 2 : reason: ReconnectReason::LaggingWal {
832 2 : current_commit_lsn,
833 2 : new_commit_lsn,
834 2 : threshold: self.conf.max_lsn_wal_lag,
835 2 : },
836 2 : });
837 6 : }
838 6 : // If we have a candidate with the same commit_lsn as the current one, which is in the same AZ as pageserver,
839 6 : // and the current one is not, switch to the new one.
840 6 : if self.conf.availability_zone.is_some()
841 2 : && existing_wal_connection.availability_zone
842 2 : != self.conf.availability_zone
843 2 : && self.conf.availability_zone == new_availability_zone
844 : {
845 2 : return Some(NewWalConnectionCandidate {
846 2 : safekeeper_id: new_sk_id,
847 2 : availability_zone: new_availability_zone,
848 2 : wal_source_connconf: new_wal_source_connconf,
849 2 : reason: ReconnectReason::SwitchAvailabilityZone,
850 2 : });
851 4 : }
852 : }
853 0 : None => debug!(
854 0 : "Best SK candidate has its commit_lsn behind connected SK's commit_lsn"
855 : ),
856 : }
857 0 : }
858 :
859 4 : let current_lsn = match existing_wal_connection.status.streaming_lsn {
860 4 : Some(lsn) => lsn,
861 0 : None => self.timeline.get_last_record_lsn(),
862 : };
863 4 : let current_commit_lsn = existing_wal_connection
864 4 : .status
865 4 : .commit_lsn
866 4 : .unwrap_or(current_lsn);
867 4 : let candidate_commit_lsn = Lsn(new_safekeeper_broker_data.commit_lsn);
868 4 :
869 4 : // Keep discovered_new_wal only if connected safekeeper has not caught up yet.
870 4 : let mut discovered_new_wal = existing_wal_connection
871 4 : .discovered_new_wal
872 4 : .filter(|new_wal| new_wal.lsn > current_commit_lsn);
873 4 :
874 4 : if discovered_new_wal.is_none() {
875 : // Check if the new candidate has more WAL than the current one.
876 : // If the new candidate has more WAL than the current one, we consider switching to the new candidate.
877 2 : discovered_new_wal = if candidate_commit_lsn > current_commit_lsn {
878 2 : trace!(
879 0 : "New candidate has commit_lsn {}, higher than current_commit_lsn {}",
880 : candidate_commit_lsn,
881 : current_commit_lsn
882 : );
883 2 : Some(NewCommittedWAL {
884 2 : lsn: candidate_commit_lsn,
885 2 : discovered_at: Utc::now().naive_utc(),
886 2 : })
887 : } else {
888 0 : None
889 : };
890 2 : }
891 :
892 4 : let waiting_for_new_lsn_since = if current_lsn < current_commit_lsn {
893 : // Connected safekeeper has more WAL, but we haven't received updates for some time.
894 0 : trace!(
895 0 : "Connected safekeeper has more WAL, but we haven't received updates for {:?}. current_lsn: {}, current_commit_lsn: {}",
896 0 : (now - existing_wal_connection.status.latest_wal_update).to_std(),
897 : current_lsn,
898 : current_commit_lsn
899 : );
900 0 : Some(existing_wal_connection.status.latest_wal_update)
901 : } else {
902 4 : discovered_new_wal.as_ref().map(|new_wal| {
903 4 : // We know that new WAL is available on other safekeeper, but connected safekeeper don't have it.
904 4 : new_wal
905 4 : .discovered_at
906 4 : .max(existing_wal_connection.status.latest_wal_update)
907 4 : })
908 : };
909 :
910 : // If we haven't received any WAL updates for a while and candidate has more WAL, switch to it.
911 4 : if let Some(waiting_for_new_lsn_since) = waiting_for_new_lsn_since {
912 4 : if let Ok(waiting_for_new_wal) = (now - waiting_for_new_lsn_since).to_std() {
913 2 : if candidate_commit_lsn > current_commit_lsn
914 2 : && waiting_for_new_wal > self.conf.lagging_wal_timeout
915 : {
916 2 : return Some(NewWalConnectionCandidate {
917 2 : safekeeper_id: new_sk_id,
918 2 : wal_source_connconf: new_wal_source_connconf,
919 2 : availability_zone: new_availability_zone,
920 2 : reason: ReconnectReason::NoWalTimeout {
921 2 : current_lsn,
922 2 : current_commit_lsn,
923 2 : candidate_commit_lsn,
924 2 : last_wal_interaction: Some(
925 2 : existing_wal_connection.status.latest_wal_update,
926 2 : ),
927 2 : check_time: now,
928 2 : threshold: self.conf.lagging_wal_timeout,
929 2 : },
930 2 : });
931 0 : }
932 2 : }
933 0 : }
934 :
935 2 : self.wal_connection.as_mut().unwrap().discovered_new_wal = discovered_new_wal;
936 : }
937 : None => {
938 6 : let (new_sk_id, new_safekeeper_broker_data, new_wal_source_connconf) =
939 8 : self.select_connection_candidate(None)?;
940 6 : return Some(NewWalConnectionCandidate {
941 6 : safekeeper_id: new_sk_id,
942 6 : availability_zone: new_safekeeper_broker_data.availability_zone.clone(),
943 6 : wal_source_connconf: new_wal_source_connconf,
944 6 : reason: ReconnectReason::NoExistingConnection,
945 6 : });
946 : }
947 : }
948 :
949 2 : None
950 18 : }
951 :
952 : /// Selects the best possible candidate, based on the data collected from the broker updates about the safekeepers.
953 : /// Optionally, omits the given node, to support gracefully switching from a healthy safekeeper to another.
954 : ///
955 : /// The candidate that is chosen:
956 : /// * has no pending retry cooldown
957 : /// * has greatest commit_lsn among the ones that are left
958 18 : fn select_connection_candidate(
959 18 : &self,
960 18 : node_to_omit: Option<NodeId>,
961 18 : ) -> Option<(NodeId, &SafekeeperDiscoveryResponse, PgConnectionConfig)> {
962 18 : self.applicable_connection_candidates()
963 26 : .filter(|&(sk_id, _, _)| Some(sk_id) != node_to_omit)
964 20 : .max_by_key(|(_, info, _)| info.commit_lsn)
965 18 : }
966 :
967 : /// Returns a list of safekeepers that have valid info and ready for connection.
968 : /// Some safekeepers are filtered by the retry cooldown.
969 18 : fn applicable_connection_candidates(
970 18 : &self,
971 18 : ) -> impl Iterator<Item = (NodeId, &SafekeeperDiscoveryResponse, PgConnectionConfig)> {
972 18 : let now = Utc::now().naive_utc();
973 18 :
974 18 : self.wal_stream_candidates
975 18 : .iter()
976 36 : .filter(|(_, info)| Lsn(info.timeline.commit_lsn) != Lsn::INVALID)
977 32 : .filter(move |(sk_id, _)| {
978 32 : let next_retry_at = self
979 32 : .wal_connection_retries
980 32 : .get(sk_id)
981 32 : .and_then(|retry_info| {
982 2 : retry_info.next_retry_at
983 32 : });
984 32 :
985 32 : next_retry_at.is_none() || next_retry_at.unwrap() <= now
986 32 : }).filter_map(|(sk_id, broker_info)| {
987 30 : let info = &broker_info.timeline;
988 30 : if info.safekeeper_connstr.is_empty() {
989 4 : return None; // no connection string, ignore sk
990 26 : }
991 :
992 26 : let (shard_number, shard_count, shard_stripe_size) = match self.conf.protocol {
993 : PostgresClientProtocol::Vanilla => {
994 0 : (None, None, None)
995 : },
996 : PostgresClientProtocol::Interpreted { .. } => {
997 26 : let shard_identity = self.timeline.get_shard_identity();
998 26 : (
999 26 : Some(shard_identity.number.0),
1000 26 : Some(shard_identity.count.0),
1001 26 : Some(shard_identity.stripe_size.0),
1002 26 : )
1003 : }
1004 : };
1005 :
1006 26 : let connection_conf_args = ConnectionConfigArgs {
1007 26 : protocol: self.conf.protocol,
1008 26 : ttid: self.id,
1009 26 : shard_number,
1010 26 : shard_count,
1011 26 : shard_stripe_size,
1012 26 : listen_pg_addr_str: info.safekeeper_connstr.as_ref(),
1013 26 : auth_token: self.conf.auth_token.as_ref().map(|t| t.as_str()),
1014 26 : availability_zone: self.conf.availability_zone.as_deref()
1015 26 : };
1016 26 :
1017 26 : match wal_stream_connection_config(connection_conf_args) {
1018 26 : Ok(connstr) => Some((*sk_id, info, connstr)),
1019 0 : Err(e) => {
1020 0 : error!("Failed to create wal receiver connection string from broker data of safekeeper node {}: {e:#}", sk_id);
1021 0 : None
1022 : }
1023 : }
1024 30 : })
1025 18 : }
1026 :
1027 : /// Remove candidates which haven't sent broker updates for a while.
1028 18 : fn cleanup_old_candidates(&mut self) {
1029 18 : let mut node_ids_to_remove = Vec::with_capacity(self.wal_stream_candidates.len());
1030 18 : let lagging_wal_timeout = self.conf.lagging_wal_timeout;
1031 18 :
1032 38 : self.wal_stream_candidates.retain(|node_id, broker_info| {
1033 38 : if let Ok(time_since_latest_broker_update) =
1034 38 : (Utc::now().naive_utc() - broker_info.latest_update).to_std()
1035 : {
1036 38 : let should_retain = time_since_latest_broker_update < lagging_wal_timeout;
1037 38 : if !should_retain {
1038 2 : node_ids_to_remove.push(*node_id);
1039 36 : }
1040 38 : should_retain
1041 : } else {
1042 0 : true
1043 : }
1044 38 : });
1045 18 :
1046 18 : if !node_ids_to_remove.is_empty() {
1047 4 : for node_id in node_ids_to_remove {
1048 2 : info!("Safekeeper node {node_id} did not send events for over {lagging_wal_timeout:?}, not retrying the connections");
1049 2 : self.wal_connection_retries.remove(&node_id);
1050 2 : WALRECEIVER_CANDIDATES_REMOVED.inc();
1051 : }
1052 16 : }
1053 18 : }
1054 :
1055 : /// # Cancel-Safety
1056 : ///
1057 : /// Not cancellation-safe.
1058 0 : pub(super) async fn shutdown(mut self) {
1059 0 : if let Some(wal_connection) = self.wal_connection.take() {
1060 0 : wal_connection.connection_task.shutdown().await;
1061 0 : }
1062 0 : }
1063 :
1064 0 : fn manager_status(&self) -> ConnectionManagerStatus {
1065 0 : ConnectionManagerStatus {
1066 0 : existing_connection: self.wal_connection.as_ref().map(|conn| conn.status),
1067 0 : wal_stream_candidates: self.wal_stream_candidates.clone(),
1068 0 : }
1069 0 : }
1070 : }
1071 :
1072 : #[derive(Debug)]
1073 : struct NewWalConnectionCandidate {
1074 : safekeeper_id: NodeId,
1075 : wal_source_connconf: PgConnectionConfig,
1076 : availability_zone: Option<String>,
1077 : reason: ReconnectReason,
1078 : }
1079 :
1080 : /// Stores the reason why WAL connection was switched, for furter debugging purposes.
1081 : #[derive(Debug, PartialEq, Eq)]
1082 : enum ReconnectReason {
1083 : NoExistingConnection,
1084 : LaggingWal {
1085 : current_commit_lsn: Lsn,
1086 : new_commit_lsn: Lsn,
1087 : threshold: NonZeroU64,
1088 : },
1089 : SwitchAvailabilityZone,
1090 : NoWalTimeout {
1091 : current_lsn: Lsn,
1092 : current_commit_lsn: Lsn,
1093 : candidate_commit_lsn: Lsn,
1094 : last_wal_interaction: Option<NaiveDateTime>,
1095 : check_time: NaiveDateTime,
1096 : threshold: Duration,
1097 : },
1098 : NoKeepAlives {
1099 : last_keep_alive: Option<NaiveDateTime>,
1100 : check_time: NaiveDateTime,
1101 : threshold: Duration,
1102 : },
1103 : }
1104 :
1105 : impl ReconnectReason {
1106 0 : fn name(&self) -> &str {
1107 0 : match self {
1108 0 : ReconnectReason::NoExistingConnection => "NoExistingConnection",
1109 0 : ReconnectReason::LaggingWal { .. } => "LaggingWal",
1110 0 : ReconnectReason::SwitchAvailabilityZone => "SwitchAvailabilityZone",
1111 0 : ReconnectReason::NoWalTimeout { .. } => "NoWalTimeout",
1112 0 : ReconnectReason::NoKeepAlives { .. } => "NoKeepAlives",
1113 : }
1114 0 : }
1115 : }
1116 :
1117 : #[cfg(test)]
1118 : mod tests {
1119 : use super::*;
1120 : use crate::tenant::harness::{TenantHarness, TIMELINE_ID};
1121 : use pageserver_api::config::defaults::DEFAULT_WAL_RECEIVER_PROTOCOL;
1122 : use url::Host;
1123 :
1124 38 : fn dummy_broker_sk_timeline(
1125 38 : commit_lsn: u64,
1126 38 : safekeeper_connstr: &str,
1127 38 : latest_update: NaiveDateTime,
1128 38 : ) -> BrokerSkTimeline {
1129 38 : BrokerSkTimeline {
1130 38 : timeline: SafekeeperDiscoveryResponse {
1131 38 : safekeeper_id: 0,
1132 38 : tenant_timeline_id: None,
1133 38 : commit_lsn,
1134 38 : safekeeper_connstr: safekeeper_connstr.to_owned(),
1135 38 : availability_zone: None,
1136 38 : standby_horizon: 0,
1137 38 : },
1138 38 : latest_update,
1139 38 : }
1140 38 : }
1141 :
1142 : #[tokio::test]
1143 2 : async fn no_connection_no_candidate() -> anyhow::Result<()> {
1144 2 : let harness = TenantHarness::create("no_connection_no_candidate").await?;
1145 26 : let mut state = dummy_state(&harness).await;
1146 2 : let now = Utc::now().naive_utc();
1147 2 :
1148 2 : let lagging_wal_timeout = chrono::Duration::from_std(state.conf.lagging_wal_timeout)?;
1149 2 : let delay_over_threshold = now - lagging_wal_timeout - lagging_wal_timeout;
1150 2 :
1151 2 : state.wal_connection = None;
1152 2 : state.wal_stream_candidates = HashMap::from([
1153 2 : (NodeId(0), dummy_broker_sk_timeline(1, "", now)),
1154 2 : (NodeId(1), dummy_broker_sk_timeline(0, "no_commit_lsn", now)),
1155 2 : (NodeId(2), dummy_broker_sk_timeline(0, "no_commit_lsn", now)),
1156 2 : (
1157 2 : NodeId(3),
1158 2 : dummy_broker_sk_timeline(
1159 2 : 1 + state.conf.max_lsn_wal_lag.get(),
1160 2 : "delay_over_threshold",
1161 2 : delay_over_threshold,
1162 2 : ),
1163 2 : ),
1164 2 : ]);
1165 2 :
1166 2 : let no_candidate = state.next_connection_candidate();
1167 2 : assert!(
1168 2 : no_candidate.is_none(),
1169 2 : "Expected no candidate selected out of non full data options, but got {no_candidate:?}"
1170 2 : );
1171 2 :
1172 2 : Ok(())
1173 2 : }
1174 :
1175 : #[tokio::test]
1176 2 : async fn connection_no_candidate() -> anyhow::Result<()> {
1177 2 : let harness = TenantHarness::create("connection_no_candidate").await?;
1178 26 : let mut state = dummy_state(&harness).await;
1179 2 : let now = Utc::now().naive_utc();
1180 2 :
1181 2 : let connected_sk_id = NodeId(0);
1182 2 : let current_lsn = 100_000;
1183 2 :
1184 2 : let connection_status = WalConnectionStatus {
1185 2 : is_connected: true,
1186 2 : has_processed_wal: true,
1187 2 : latest_connection_update: now,
1188 2 : latest_wal_update: now,
1189 2 : commit_lsn: Some(Lsn(current_lsn)),
1190 2 : streaming_lsn: Some(Lsn(current_lsn)),
1191 2 : node: NodeId(1),
1192 2 : };
1193 2 :
1194 2 : state.conf.max_lsn_wal_lag = NonZeroU64::new(100).unwrap();
1195 2 : state.wal_connection = Some(WalConnection {
1196 2 : started_at: now,
1197 2 : sk_id: connected_sk_id,
1198 2 : availability_zone: None,
1199 2 : status: connection_status,
1200 2 : connection_task: state.spawn(move |sender, _| async move {
1201 2 : sender
1202 2 : .send(TaskStateUpdate::Progress(connection_status))
1203 2 : .ok();
1204 2 : Ok(())
1205 2 : }),
1206 2 : discovered_new_wal: None,
1207 2 : });
1208 2 : state.wal_stream_candidates = HashMap::from([
1209 2 : (
1210 2 : connected_sk_id,
1211 2 : dummy_broker_sk_timeline(
1212 2 : current_lsn + state.conf.max_lsn_wal_lag.get() * 2,
1213 2 : DUMMY_SAFEKEEPER_HOST,
1214 2 : now,
1215 2 : ),
1216 2 : ),
1217 2 : (
1218 2 : NodeId(1),
1219 2 : dummy_broker_sk_timeline(current_lsn, "not_advanced_lsn", now),
1220 2 : ),
1221 2 : (
1222 2 : NodeId(2),
1223 2 : dummy_broker_sk_timeline(
1224 2 : current_lsn + state.conf.max_lsn_wal_lag.get() / 2,
1225 2 : "not_enough_advanced_lsn",
1226 2 : now,
1227 2 : ),
1228 2 : ),
1229 2 : ]);
1230 2 :
1231 2 : let no_candidate = state.next_connection_candidate();
1232 2 : assert!(
1233 2 : no_candidate.is_none(),
1234 2 : "Expected no candidate selected out of valid options since candidate Lsn data is ignored and others' was not advanced enough, but got {no_candidate:?}"
1235 2 : );
1236 2 :
1237 2 : Ok(())
1238 2 : }
1239 :
1240 : #[tokio::test]
1241 2 : async fn no_connection_candidate() -> anyhow::Result<()> {
1242 2 : let harness = TenantHarness::create("no_connection_candidate").await?;
1243 26 : let mut state = dummy_state(&harness).await;
1244 2 : let now = Utc::now().naive_utc();
1245 2 :
1246 2 : state.wal_connection = None;
1247 2 : state.wal_stream_candidates = HashMap::from([(
1248 2 : NodeId(0),
1249 2 : dummy_broker_sk_timeline(
1250 2 : 1 + state.conf.max_lsn_wal_lag.get(),
1251 2 : DUMMY_SAFEKEEPER_HOST,
1252 2 : now,
1253 2 : ),
1254 2 : )]);
1255 2 :
1256 2 : let only_candidate = state
1257 2 : .next_connection_candidate()
1258 2 : .expect("Expected one candidate selected out of the only data option, but got none");
1259 2 : assert_eq!(only_candidate.safekeeper_id, NodeId(0));
1260 2 : assert_eq!(
1261 2 : only_candidate.reason,
1262 2 : ReconnectReason::NoExistingConnection,
1263 2 : "Should select new safekeeper due to missing connection, even if there's also a lag in the wal over the threshold"
1264 2 : );
1265 2 : assert_eq!(
1266 2 : only_candidate.wal_source_connconf.host(),
1267 2 : &Host::Domain(DUMMY_SAFEKEEPER_HOST.to_owned())
1268 2 : );
1269 2 :
1270 2 : let selected_lsn = 100_000;
1271 2 : state.wal_stream_candidates = HashMap::from([
1272 2 : (
1273 2 : NodeId(0),
1274 2 : dummy_broker_sk_timeline(selected_lsn - 100, "smaller_commit_lsn", now),
1275 2 : ),
1276 2 : (
1277 2 : NodeId(1),
1278 2 : dummy_broker_sk_timeline(selected_lsn, DUMMY_SAFEKEEPER_HOST, now),
1279 2 : ),
1280 2 : (
1281 2 : NodeId(2),
1282 2 : dummy_broker_sk_timeline(selected_lsn + 100, "", now),
1283 2 : ),
1284 2 : ]);
1285 2 : let biggest_wal_candidate = state.next_connection_candidate().expect(
1286 2 : "Expected one candidate selected out of multiple valid data options, but got none",
1287 2 : );
1288 2 :
1289 2 : assert_eq!(biggest_wal_candidate.safekeeper_id, NodeId(1));
1290 2 : assert_eq!(
1291 2 : biggest_wal_candidate.reason,
1292 2 : ReconnectReason::NoExistingConnection,
1293 2 : "Should select new safekeeper due to missing connection, even if there's also a lag in the wal over the threshold"
1294 2 : );
1295 2 : assert_eq!(
1296 2 : biggest_wal_candidate.wal_source_connconf.host(),
1297 2 : &Host::Domain(DUMMY_SAFEKEEPER_HOST.to_owned())
1298 2 : );
1299 2 :
1300 2 : Ok(())
1301 2 : }
1302 :
1303 : #[tokio::test]
1304 2 : async fn candidate_with_many_connection_failures() -> anyhow::Result<()> {
1305 2 : let harness = TenantHarness::create("candidate_with_many_connection_failures").await?;
1306 26 : let mut state = dummy_state(&harness).await;
1307 2 : let now = Utc::now().naive_utc();
1308 2 :
1309 2 : let current_lsn = Lsn(100_000).align();
1310 2 : let bigger_lsn = Lsn(current_lsn.0 + 100).align();
1311 2 :
1312 2 : state.wal_connection = None;
1313 2 : state.wal_stream_candidates = HashMap::from([
1314 2 : (
1315 2 : NodeId(0),
1316 2 : dummy_broker_sk_timeline(bigger_lsn.0, DUMMY_SAFEKEEPER_HOST, now),
1317 2 : ),
1318 2 : (
1319 2 : NodeId(1),
1320 2 : dummy_broker_sk_timeline(current_lsn.0, DUMMY_SAFEKEEPER_HOST, now),
1321 2 : ),
1322 2 : ]);
1323 2 : state.wal_connection_retries = HashMap::from([(
1324 2 : NodeId(0),
1325 2 : RetryInfo {
1326 2 : next_retry_at: now.checked_add_signed(chrono::Duration::hours(1)),
1327 2 : retry_duration_seconds: WALCONNECTION_RETRY_MAX_BACKOFF_SECONDS,
1328 2 : },
1329 2 : )]);
1330 2 :
1331 2 : let candidate_with_less_errors = state
1332 2 : .next_connection_candidate()
1333 2 : .expect("Expected one candidate selected, but got none");
1334 2 : assert_eq!(
1335 2 : candidate_with_less_errors.safekeeper_id,
1336 2 : NodeId(1),
1337 2 : "Should select the node with no pending retry cooldown"
1338 2 : );
1339 2 :
1340 2 : Ok(())
1341 2 : }
1342 :
1343 : #[tokio::test]
1344 2 : async fn lsn_wal_over_threshold_current_candidate() -> anyhow::Result<()> {
1345 2 : let harness = TenantHarness::create("lsn_wal_over_threshcurrent_candidate").await?;
1346 26 : let mut state = dummy_state(&harness).await;
1347 2 : let current_lsn = Lsn(100_000).align();
1348 2 : let now = Utc::now().naive_utc();
1349 2 :
1350 2 : let connected_sk_id = NodeId(0);
1351 2 : let new_lsn = Lsn(current_lsn.0 + state.conf.max_lsn_wal_lag.get() + 1);
1352 2 :
1353 2 : let connection_status = WalConnectionStatus {
1354 2 : is_connected: true,
1355 2 : has_processed_wal: true,
1356 2 : latest_connection_update: now,
1357 2 : latest_wal_update: now,
1358 2 : commit_lsn: Some(current_lsn),
1359 2 : streaming_lsn: Some(current_lsn),
1360 2 : node: connected_sk_id,
1361 2 : };
1362 2 :
1363 2 : state.wal_connection = Some(WalConnection {
1364 2 : started_at: now,
1365 2 : sk_id: connected_sk_id,
1366 2 : availability_zone: None,
1367 2 : status: connection_status,
1368 2 : connection_task: state.spawn(move |sender, _| async move {
1369 2 : sender
1370 2 : .send(TaskStateUpdate::Progress(connection_status))
1371 2 : .ok();
1372 2 : Ok(())
1373 2 : }),
1374 2 : discovered_new_wal: None,
1375 2 : });
1376 2 : state.wal_stream_candidates = HashMap::from([
1377 2 : (
1378 2 : connected_sk_id,
1379 2 : dummy_broker_sk_timeline(current_lsn.0, DUMMY_SAFEKEEPER_HOST, now),
1380 2 : ),
1381 2 : (
1382 2 : NodeId(1),
1383 2 : dummy_broker_sk_timeline(new_lsn.0, "advanced_by_lsn_safekeeper", now),
1384 2 : ),
1385 2 : ]);
1386 2 :
1387 2 : let over_threshcurrent_candidate = state.next_connection_candidate().expect(
1388 2 : "Expected one candidate selected out of multiple valid data options, but got none",
1389 2 : );
1390 2 :
1391 2 : assert_eq!(over_threshcurrent_candidate.safekeeper_id, NodeId(1));
1392 2 : assert_eq!(
1393 2 : over_threshcurrent_candidate.reason,
1394 2 : ReconnectReason::LaggingWal {
1395 2 : current_commit_lsn: current_lsn,
1396 2 : new_commit_lsn: new_lsn,
1397 2 : threshold: state.conf.max_lsn_wal_lag
1398 2 : },
1399 2 : "Should select bigger WAL safekeeper if it starts to lag enough"
1400 2 : );
1401 2 : assert_eq!(
1402 2 : over_threshcurrent_candidate.wal_source_connconf.host(),
1403 2 : &Host::Domain("advanced_by_lsn_safekeeper".to_owned())
1404 2 : );
1405 2 :
1406 2 : Ok(())
1407 2 : }
1408 :
1409 : #[tokio::test]
1410 2 : async fn timeout_connection_threshold_current_candidate() -> anyhow::Result<()> {
1411 2 : let harness =
1412 2 : TenantHarness::create("timeout_connection_threshold_current_candidate").await?;
1413 26 : let mut state = dummy_state(&harness).await;
1414 2 : let current_lsn = Lsn(100_000).align();
1415 2 : let now = Utc::now().naive_utc();
1416 2 :
1417 2 : let wal_connect_timeout = chrono::Duration::from_std(state.conf.wal_connect_timeout)?;
1418 2 : let time_over_threshold =
1419 2 : Utc::now().naive_utc() - wal_connect_timeout - wal_connect_timeout;
1420 2 :
1421 2 : let connection_status = WalConnectionStatus {
1422 2 : is_connected: true,
1423 2 : has_processed_wal: true,
1424 2 : latest_connection_update: time_over_threshold,
1425 2 : latest_wal_update: time_over_threshold,
1426 2 : commit_lsn: Some(current_lsn),
1427 2 : streaming_lsn: Some(current_lsn),
1428 2 : node: NodeId(1),
1429 2 : };
1430 2 :
1431 2 : state.wal_connection = Some(WalConnection {
1432 2 : started_at: now,
1433 2 : sk_id: NodeId(1),
1434 2 : availability_zone: None,
1435 2 : status: connection_status,
1436 2 : connection_task: state.spawn(move |sender, _| async move {
1437 2 : sender
1438 2 : .send(TaskStateUpdate::Progress(connection_status))
1439 2 : .ok();
1440 2 : Ok(())
1441 2 : }),
1442 2 : discovered_new_wal: None,
1443 2 : });
1444 2 : state.wal_stream_candidates = HashMap::from([(
1445 2 : NodeId(0),
1446 2 : dummy_broker_sk_timeline(current_lsn.0, DUMMY_SAFEKEEPER_HOST, now),
1447 2 : )]);
1448 2 :
1449 2 : let over_threshcurrent_candidate = state.next_connection_candidate().expect(
1450 2 : "Expected one candidate selected out of multiple valid data options, but got none",
1451 2 : );
1452 2 :
1453 2 : assert_eq!(over_threshcurrent_candidate.safekeeper_id, NodeId(0));
1454 2 : match over_threshcurrent_candidate.reason {
1455 2 : ReconnectReason::NoKeepAlives {
1456 2 : last_keep_alive,
1457 2 : threshold,
1458 2 : ..
1459 2 : } => {
1460 2 : assert_eq!(last_keep_alive, Some(time_over_threshold));
1461 2 : assert_eq!(threshold, state.conf.lagging_wal_timeout);
1462 2 : }
1463 2 : unexpected => panic!("Unexpected reason: {unexpected:?}"),
1464 2 : }
1465 2 : assert_eq!(
1466 2 : over_threshcurrent_candidate.wal_source_connconf.host(),
1467 2 : &Host::Domain(DUMMY_SAFEKEEPER_HOST.to_owned())
1468 2 : );
1469 2 :
1470 2 : Ok(())
1471 2 : }
1472 :
1473 : #[tokio::test]
1474 2 : async fn timeout_wal_over_threshold_current_candidate() -> anyhow::Result<()> {
1475 2 : let harness = TenantHarness::create("timeout_wal_over_threshold_current_candidate").await?;
1476 26 : let mut state = dummy_state(&harness).await;
1477 2 : let current_lsn = Lsn(100_000).align();
1478 2 : let new_lsn = Lsn(100_100).align();
1479 2 : let now = Utc::now().naive_utc();
1480 2 :
1481 2 : let lagging_wal_timeout = chrono::Duration::from_std(state.conf.lagging_wal_timeout)?;
1482 2 : let time_over_threshold =
1483 2 : Utc::now().naive_utc() - lagging_wal_timeout - lagging_wal_timeout;
1484 2 :
1485 2 : let connection_status = WalConnectionStatus {
1486 2 : is_connected: true,
1487 2 : has_processed_wal: true,
1488 2 : latest_connection_update: now,
1489 2 : latest_wal_update: time_over_threshold,
1490 2 : commit_lsn: Some(current_lsn),
1491 2 : streaming_lsn: Some(current_lsn),
1492 2 : node: NodeId(1),
1493 2 : };
1494 2 :
1495 2 : state.wal_connection = Some(WalConnection {
1496 2 : started_at: now,
1497 2 : sk_id: NodeId(1),
1498 2 : availability_zone: None,
1499 2 : status: connection_status,
1500 2 : connection_task: state.spawn(move |_, _| async move { Ok(()) }),
1501 2 : discovered_new_wal: Some(NewCommittedWAL {
1502 2 : discovered_at: time_over_threshold,
1503 2 : lsn: new_lsn,
1504 2 : }),
1505 2 : });
1506 2 : state.wal_stream_candidates = HashMap::from([(
1507 2 : NodeId(0),
1508 2 : dummy_broker_sk_timeline(new_lsn.0, DUMMY_SAFEKEEPER_HOST, now),
1509 2 : )]);
1510 2 :
1511 2 : let over_threshcurrent_candidate = state.next_connection_candidate().expect(
1512 2 : "Expected one candidate selected out of multiple valid data options, but got none",
1513 2 : );
1514 2 :
1515 2 : assert_eq!(over_threshcurrent_candidate.safekeeper_id, NodeId(0));
1516 2 : match over_threshcurrent_candidate.reason {
1517 2 : ReconnectReason::NoWalTimeout {
1518 2 : current_lsn,
1519 2 : current_commit_lsn,
1520 2 : candidate_commit_lsn,
1521 2 : last_wal_interaction,
1522 2 : threshold,
1523 2 : ..
1524 2 : } => {
1525 2 : assert_eq!(current_lsn, current_lsn);
1526 2 : assert_eq!(current_commit_lsn, current_lsn);
1527 2 : assert_eq!(candidate_commit_lsn, new_lsn);
1528 2 : assert_eq!(last_wal_interaction, Some(time_over_threshold));
1529 2 : assert_eq!(threshold, state.conf.lagging_wal_timeout);
1530 2 : }
1531 2 : unexpected => panic!("Unexpected reason: {unexpected:?}"),
1532 2 : }
1533 2 : assert_eq!(
1534 2 : over_threshcurrent_candidate.wal_source_connconf.host(),
1535 2 : &Host::Domain(DUMMY_SAFEKEEPER_HOST.to_owned())
1536 2 : );
1537 2 :
1538 2 : Ok(())
1539 2 : }
1540 :
1541 : const DUMMY_SAFEKEEPER_HOST: &str = "safekeeper_connstr";
1542 :
1543 16 : async fn dummy_state(harness: &TenantHarness) -> ConnectionManagerState {
1544 160 : let (tenant, ctx) = harness.load().await;
1545 16 : let timeline = tenant
1546 16 : .create_test_timeline(TIMELINE_ID, Lsn(0x8), crate::DEFAULT_PG_VERSION, &ctx)
1547 48 : .await
1548 16 : .expect("Failed to create an empty timeline for dummy wal connection manager");
1549 16 :
1550 16 : ConnectionManagerState {
1551 16 : id: TenantTimelineId {
1552 16 : tenant_id: harness.tenant_shard_id.tenant_id,
1553 16 : timeline_id: TIMELINE_ID,
1554 16 : },
1555 16 : timeline,
1556 16 : cancel: CancellationToken::new(),
1557 16 : conf: WalReceiverConf {
1558 16 : protocol: DEFAULT_WAL_RECEIVER_PROTOCOL,
1559 16 : wal_connect_timeout: Duration::from_secs(1),
1560 16 : lagging_wal_timeout: Duration::from_secs(1),
1561 16 : max_lsn_wal_lag: NonZeroU64::new(1024 * 1024).unwrap(),
1562 16 : auth_token: None,
1563 16 : availability_zone: None,
1564 16 : ingest_batch_size: 1,
1565 16 : },
1566 16 : wal_connection: None,
1567 16 : wal_stream_candidates: HashMap::new(),
1568 16 : wal_connection_retries: HashMap::new(),
1569 16 : }
1570 16 : }
1571 :
1572 : #[tokio::test]
1573 2 : async fn switch_to_same_availability_zone() -> anyhow::Result<()> {
1574 2 : // Pageserver and one of safekeepers will be in the same availability zone
1575 2 : // and pageserver should prefer to connect to it.
1576 2 : let test_az = Some("test_az".to_owned());
1577 2 :
1578 2 : let harness = TenantHarness::create("switch_to_same_availability_zone").await?;
1579 26 : let mut state = dummy_state(&harness).await;
1580 2 : state.conf.availability_zone.clone_from(&test_az);
1581 2 : let current_lsn = Lsn(100_000).align();
1582 2 : let now = Utc::now().naive_utc();
1583 2 :
1584 2 : let connected_sk_id = NodeId(0);
1585 2 :
1586 2 : let connection_status = WalConnectionStatus {
1587 2 : is_connected: true,
1588 2 : has_processed_wal: true,
1589 2 : latest_connection_update: now,
1590 2 : latest_wal_update: now,
1591 2 : commit_lsn: Some(current_lsn),
1592 2 : streaming_lsn: Some(current_lsn),
1593 2 : node: connected_sk_id,
1594 2 : };
1595 2 :
1596 2 : state.wal_connection = Some(WalConnection {
1597 2 : started_at: now,
1598 2 : sk_id: connected_sk_id,
1599 2 : availability_zone: None,
1600 2 : status: connection_status,
1601 2 : connection_task: state.spawn(move |sender, _| async move {
1602 2 : sender
1603 2 : .send(TaskStateUpdate::Progress(connection_status))
1604 2 : .ok();
1605 2 : Ok(())
1606 2 : }),
1607 2 : discovered_new_wal: None,
1608 2 : });
1609 2 :
1610 2 : // We have another safekeeper with the same commit_lsn, and it have the same availability zone as
1611 2 : // the current pageserver.
1612 2 : let mut same_az_sk = dummy_broker_sk_timeline(current_lsn.0, "same_az", now);
1613 2 : same_az_sk.timeline.availability_zone.clone_from(&test_az);
1614 2 :
1615 2 : state.wal_stream_candidates = HashMap::from([
1616 2 : (
1617 2 : connected_sk_id,
1618 2 : dummy_broker_sk_timeline(current_lsn.0, DUMMY_SAFEKEEPER_HOST, now),
1619 2 : ),
1620 2 : (NodeId(1), same_az_sk),
1621 2 : ]);
1622 2 :
1623 2 : // We expect that pageserver will switch to the safekeeper in the same availability zone,
1624 2 : // even if it has the same commit_lsn.
1625 2 : let next_candidate = state.next_connection_candidate().expect(
1626 2 : "Expected one candidate selected out of multiple valid data options, but got none",
1627 2 : );
1628 2 :
1629 2 : assert_eq!(next_candidate.safekeeper_id, NodeId(1));
1630 2 : assert_eq!(
1631 2 : next_candidate.reason,
1632 2 : ReconnectReason::SwitchAvailabilityZone,
1633 2 : "Should switch to the safekeeper in the same availability zone, if it has the same commit_lsn"
1634 2 : );
1635 2 : assert_eq!(
1636 2 : next_candidate.wal_source_connconf.host(),
1637 2 : &Host::Domain("same_az".to_owned())
1638 2 : );
1639 2 :
1640 2 : Ok(())
1641 2 : }
1642 : }
|