| 1 | //! The bucket format a node can read, and the fleet-wide gate that keeps a |
| 2 | //! node from writing what a live peer cannot read. |
| 3 | //! |
| 4 | //! A rolling update runs two releases against one bucket for the length of |
| 5 | //! the rollout. Anything the newer release writes that the older one cannot |
| 6 | //! restore is a cell that goes dark when it fails over to an old node, and a |
| 7 | //! cell that cannot come back on a downgrade. The rule that prevents both is |
| 8 | //! structural: each node publishes the newest format it reads, and a node |
| 9 | //! writes a format only while every live lease reads it. |
| 10 | |
| 11 | use crate::CapacityPeer; |
| 12 | |
| 13 | /// The newest bucket format this release reads. |
| 14 | /// |
| 15 | /// - `1`: every epoch opens with a whole-database snapshot, so a restore |
| 16 | /// reads one epoch. Every release before paged restore reads this. |
| 17 | /// - `2`: an epoch can page in and continue its predecessor's chain from a |
| 18 | /// marker at the cut. A format-1 reader finds no snapshot in that epoch and |
| 19 | /// cannot restore the cell. |
| 20 | pub const BUCKET_FORMAT: u16 = 2; |
| 21 | |
| 22 | /// The format a lease without the field reads: the releases before the |
| 23 | /// field existed all read format 1. |
| 24 | const FIELDLESS_FORMAT: u16 = 1; |
| 25 | |
| 26 | /// Whether every live lease reads `format`, so a node can write it. |
| 27 | /// |
| 28 | /// A lease counts while it has not expired, so a crashed old node holds the |
| 29 | /// gate closed for one lease lifetime, which is the same bound its cells |
| 30 | /// wait before a peer takes them over. No live lease at all is `false`: a |
| 31 | /// node that has not seen the fleet does not know who is in it, and the |
| 32 | /// older format is always safe to write. |
| 33 | pub fn fleet_reads(peers: &[CapacityPeer], now_ms: u64, format: u16) -> bool { |
| 34 | let mut live = peers |
| 35 | .iter() |
| 36 | .filter(|peer| peer.expires_ms > now_ms) |
| 37 | .peekable(); |
| 38 | live.peek().is_some() |
| 39 | && live.all(|peer| peer.bucket_format.unwrap_or(FIELDLESS_FORMAT) >= format) |
| 40 | } |