|
| 1 | +// Copyright (c) 2024 - Restate Software, Inc., Restate GmbH. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Use of this software is governed by the Business Source License |
| 5 | +// included in the LICENSE file. |
| 6 | +// |
| 7 | +// As of the Change Date specified in that file, in accordance with |
| 8 | +// the Business Source License, use of this software will be governed |
| 9 | +// by the Apache License, Version 2.0. |
| 10 | + |
| 11 | +use tokio::sync::mpsc; |
| 12 | +use tracing::trace; |
| 13 | + |
| 14 | +use restate_core::network::rpc_router::{RpcError, RpcRouter}; |
| 15 | +use restate_core::network::{Incoming, Networking, TransportConnect}; |
| 16 | +use restate_core::{TaskCenter, TaskKind}; |
| 17 | +use restate_types::config::Configuration; |
| 18 | +use restate_types::logs::{LogletOffset, SequenceNumber}; |
| 19 | +use restate_types::net::log_server::{Seal, Sealed, Status}; |
| 20 | +use restate_types::replicated_loglet::{ |
| 21 | + EffectiveNodeSet, ReplicatedLogletId, ReplicatedLogletParams, |
| 22 | +}; |
| 23 | +use restate_types::retries::RetryPolicy; |
| 24 | +use restate_types::{GenerationalNodeId, PlainNodeId}; |
| 25 | + |
| 26 | +use crate::loglet::util::TailOffsetWatch; |
| 27 | +use crate::providers::replicated_loglet::error::ReplicatedLogletError; |
| 28 | +use crate::providers::replicated_loglet::replication::NodeSetChecker; |
| 29 | + |
| 30 | +/// Sends a seal request to as many log-servers in the nodeset |
| 31 | +/// |
| 32 | +/// We broadcast the seal to all nodes that we can, but only wait for f-majority |
| 33 | +/// responses before acknowleding the seal. |
| 34 | +/// |
| 35 | +/// The seal operation is idempotent. It's safe to seal a loglet if it's already partially or fully |
| 36 | +/// sealed. Note that the seal task ignores the "seal" state in the input known_global_tail watch, |
| 37 | +/// but it will set it to `true` after the seal. |
| 38 | +pub struct SealTask { |
| 39 | + task_center: TaskCenter, |
| 40 | + my_params: ReplicatedLogletParams, |
| 41 | + seal_router: RpcRouter<Seal>, |
| 42 | + known_global_tail: TailOffsetWatch, |
| 43 | +} |
| 44 | + |
| 45 | +impl SealTask { |
| 46 | + pub fn new( |
| 47 | + task_center: TaskCenter, |
| 48 | + my_params: ReplicatedLogletParams, |
| 49 | + seal_router: RpcRouter<Seal>, |
| 50 | + known_global_tail: TailOffsetWatch, |
| 51 | + ) -> Self { |
| 52 | + Self { |
| 53 | + task_center, |
| 54 | + my_params, |
| 55 | + seal_router, |
| 56 | + known_global_tail, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + pub async fn run<T: TransportConnect>( |
| 61 | + self, |
| 62 | + networking: Networking<T>, |
| 63 | + ) -> Result<LogletOffset, ReplicatedLogletError> { |
| 64 | + // Use the entire nodeset except for StorageState::Disabled. |
| 65 | + let effective_nodeset = EffectiveNodeSet::new( |
| 66 | + &self.my_params.nodeset, |
| 67 | + &networking.metadata().nodes_config_ref(), |
| 68 | + ); |
| 69 | + |
| 70 | + let (tx, mut rx) = mpsc::unbounded_channel(); |
| 71 | + |
| 72 | + let mut nodeset_checker = NodeSetChecker::<'_, bool>::new( |
| 73 | + &effective_nodeset, |
| 74 | + &networking.metadata().nodes_config_ref(), |
| 75 | + &self.my_params.replication, |
| 76 | + ); |
| 77 | + |
| 78 | + let retry_policy = Configuration::pinned() |
| 79 | + .bifrost |
| 80 | + .replicated_loglet |
| 81 | + .log_server_retry_policy |
| 82 | + .clone(); |
| 83 | + |
| 84 | + for node in effective_nodeset.iter() { |
| 85 | + let task = SealSingleNode { |
| 86 | + node_id: *node, |
| 87 | + loglet_id: self.my_params.loglet_id, |
| 88 | + sequencer: self.my_params.sequencer, |
| 89 | + seal_router: self.seal_router.clone(), |
| 90 | + networking: networking.clone(), |
| 91 | + known_global_tail: self.known_global_tail.clone(), |
| 92 | + }; |
| 93 | + self.task_center.spawn_child( |
| 94 | + TaskKind::Disposable, |
| 95 | + "send-seal-request", |
| 96 | + None, |
| 97 | + task.run(tx.clone(), retry_policy.clone()), |
| 98 | + )?; |
| 99 | + } |
| 100 | + drop(tx); |
| 101 | + |
| 102 | + // Max observed local-tail from sealed nodes |
| 103 | + let mut max_tail = LogletOffset::INVALID; |
| 104 | + while let Some((node_id, local_tail)) = rx.recv().await { |
| 105 | + max_tail = std::cmp::max(max_tail, local_tail); |
| 106 | + nodeset_checker.set_attribute(node_id, true); |
| 107 | + |
| 108 | + // Do we have f-majority responses? |
| 109 | + if nodeset_checker.check_fmajority(|sealed| *sealed).passed() { |
| 110 | + self.known_global_tail.notify_seal(); |
| 111 | + // note that the rest of seal requests will continue in the background |
| 112 | + return Ok(max_tail); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // no more tasks left. We this means that we failed to seal |
| 117 | + Err(ReplicatedLogletError::SealFailed(self.my_params.loglet_id)) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +struct SealSingleNode<T> { |
| 122 | + node_id: PlainNodeId, |
| 123 | + loglet_id: ReplicatedLogletId, |
| 124 | + sequencer: GenerationalNodeId, |
| 125 | + seal_router: RpcRouter<Seal>, |
| 126 | + networking: Networking<T>, |
| 127 | + known_global_tail: TailOffsetWatch, |
| 128 | +} |
| 129 | + |
| 130 | +impl<T: TransportConnect> SealSingleNode<T> { |
| 131 | + /// Returns local-tail. Note that this will _only_ return if seal was successful, otherwise, |
| 132 | + /// it'll continue to retry. |
| 133 | + pub async fn run( |
| 134 | + self, |
| 135 | + tx: mpsc::UnboundedSender<(PlainNodeId, LogletOffset)>, |
| 136 | + retry_policy: RetryPolicy, |
| 137 | + ) -> anyhow::Result<()> { |
| 138 | + let mut retry_iter = retry_policy.into_iter(); |
| 139 | + loop { |
| 140 | + match self.do_seal().await { |
| 141 | + Ok(res) if res.body().sealed || res.body().status == Status::Ok => { |
| 142 | + let _ = tx.send((self.node_id, res.body().local_tail)); |
| 143 | + return Ok(()); |
| 144 | + } |
| 145 | + // not sealed, or seal has failed |
| 146 | + Ok(res) => { |
| 147 | + // Sent, but sealing not successful |
| 148 | + trace!(loglet_id = %self.loglet_id, "Seal failed on node {} with status {:?}", self.node_id, res.body().status); |
| 149 | + } |
| 150 | + Err(_) => { |
| 151 | + trace!(loglet_id = %self.loglet_id, "Failed to send seal message to node {}", self.node_id); |
| 152 | + } |
| 153 | + } |
| 154 | + if let Some(pause) = retry_iter.next() { |
| 155 | + tokio::time::sleep(pause).await; |
| 156 | + } else { |
| 157 | + return Err(anyhow::anyhow!(format!( |
| 158 | + "Exhausted retries while attempting to seal the loglet {} on node {}", |
| 159 | + self.loglet_id, self.node_id |
| 160 | + ))); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + async fn do_seal(&self) -> Result<Incoming<Sealed>, RpcError<Seal>> { |
| 166 | + let request = Seal { |
| 167 | + loglet_id: self.loglet_id, |
| 168 | + sequencer: self.sequencer.clone(), |
| 169 | + known_global_tail: self.known_global_tail.latest_offset(), |
| 170 | + }; |
| 171 | + trace!(loglet_id = %self.loglet_id, "Sending seal message to node {}", self.node_id); |
| 172 | + self.seal_router |
| 173 | + .call(&self.networking, self.node_id, request) |
| 174 | + .await |
| 175 | + } |
| 176 | +} |
0 commit comments