Line data Source code
1 : use crate::walrecord::NeonWalRecord;
2 : use anyhow::Result;
3 : use bytes::Bytes;
4 : use serde::{Deserialize, Serialize};
5 : use std::ops::AddAssign;
6 : use std::time::Duration;
7 :
8 : pub use pageserver_api::key::{Key, KEY_SIZE};
9 :
10 : /// A 'value' stored for a one Key.
11 5496066 : #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
12 : pub enum Value {
13 : /// An Image value contains a full copy of the value
14 : Image(Bytes),
15 : /// A WalRecord value contains a WAL record that needs to be
16 : /// replayed get the full value. Replaying the WAL record
17 : /// might need a previous version of the value (if will_init()
18 : /// returns false), or it may be replayed stand-alone (true).
19 : WalRecord(NeonWalRecord),
20 : }
21 :
22 : impl Value {
23 604 : pub fn is_image(&self) -> bool {
24 604 : matches!(self, Value::Image(_))
25 604 : }
26 :
27 7178192 : pub fn will_init(&self) -> bool {
28 7178192 : match self {
29 7031978 : Value::Image(_) => true,
30 146214 : Value::WalRecord(rec) => rec.will_init(),
31 : }
32 7178192 : }
33 : }
34 :
35 : #[derive(Debug, PartialEq)]
36 : pub(crate) enum InvalidInput {
37 : TooShortValue,
38 : TooShortPostgresRecord,
39 : }
40 :
41 : /// We could have a ValueRef where everything is `serde(borrow)`. Before implementing that, lets
42 : /// use this type for querying if a slice looks some particular way.
43 : pub(crate) struct ValueBytes;
44 :
45 : impl ValueBytes {
46 100 : pub(crate) fn will_init(raw: &[u8]) -> Result<bool, InvalidInput> {
47 100 : if raw.len() < 12 {
48 48 : return Err(InvalidInput::TooShortValue);
49 52 : }
50 52 :
51 52 : let value_discriminator = &raw[0..4];
52 52 :
53 52 : if value_discriminator == [0, 0, 0, 0] {
54 : // Value::Image always initializes
55 16 : return Ok(true);
56 36 : }
57 36 :
58 36 : if value_discriminator != [0, 0, 0, 1] {
59 : // not a Value::WalRecord(..)
60 0 : return Ok(false);
61 36 : }
62 36 :
63 36 : let walrecord_discriminator = &raw[4..8];
64 36 :
65 36 : if walrecord_discriminator != [0, 0, 0, 0] {
66 : // only NeonWalRecord::Postgres can have will_init
67 2 : return Ok(false);
68 34 : }
69 34 :
70 34 : if raw.len() < 17 {
71 10 : return Err(InvalidInput::TooShortPostgresRecord);
72 24 : }
73 24 :
74 24 : Ok(raw[8] == 1)
75 100 : }
76 : }
77 :
78 : #[cfg(test)]
79 : mod test {
80 : use super::*;
81 :
82 : use utils::bin_ser::BeSer;
83 :
84 : macro_rules! roundtrip {
85 : ($orig:expr, $expected:expr) => {{
86 : let orig: Value = $orig;
87 :
88 : let actual = Value::ser(&orig).unwrap();
89 : let expected: &[u8] = &$expected;
90 :
91 : assert_eq!(utils::Hex(&actual), utils::Hex(expected));
92 :
93 : let deser = Value::des(&actual).unwrap();
94 :
95 : assert_eq!(orig, deser);
96 : }};
97 : }
98 :
99 : #[test]
100 2 : fn image_roundtrip() {
101 2 : let image = Bytes::from_static(b"foobar");
102 2 : let image = Value::Image(image);
103 2 :
104 2 : #[rustfmt::skip]
105 2 : let expected = [
106 2 : // top level discriminator of 4 bytes
107 2 : 0x00, 0x00, 0x00, 0x00,
108 2 : // 8 byte length
109 2 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
110 2 : // foobar
111 2 : 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72
112 2 : ];
113 2 :
114 2 : roundtrip!(image, expected);
115 :
116 2 : assert!(ValueBytes::will_init(&expected).unwrap());
117 2 : }
118 :
119 : #[test]
120 2 : fn walrecord_postgres_roundtrip() {
121 2 : let rec = NeonWalRecord::Postgres {
122 2 : will_init: true,
123 2 : rec: Bytes::from_static(b"foobar"),
124 2 : };
125 2 : let rec = Value::WalRecord(rec);
126 2 :
127 2 : #[rustfmt::skip]
128 2 : let expected = [
129 2 : // flattened discriminator of total 8 bytes
130 2 : 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
131 2 : // will_init
132 2 : 0x01,
133 2 : // 8 byte length
134 2 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
135 2 : // foobar
136 2 : 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72
137 2 : ];
138 2 :
139 2 : roundtrip!(rec, expected);
140 :
141 2 : assert!(ValueBytes::will_init(&expected).unwrap());
142 2 : }
143 :
144 : #[test]
145 2 : fn bytes_inspection_too_short_image() {
146 2 : let rec = Value::Image(Bytes::from_static(b""));
147 2 :
148 2 : #[rustfmt::skip]
149 2 : let expected = [
150 2 : // top level discriminator of 4 bytes
151 2 : 0x00, 0x00, 0x00, 0x00,
152 2 : // 8 byte length
153 2 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 2 : ];
155 2 :
156 2 : roundtrip!(rec, expected);
157 :
158 2 : assert!(ValueBytes::will_init(&expected).unwrap());
159 2 : assert_eq!(expected.len(), 12);
160 26 : for len in 0..12 {
161 24 : assert_eq!(
162 24 : ValueBytes::will_init(&expected[..len]).unwrap_err(),
163 24 : InvalidInput::TooShortValue
164 24 : );
165 : }
166 2 : }
167 :
168 : #[test]
169 2 : fn bytes_inspection_too_short_postgres_record() {
170 2 : let rec = NeonWalRecord::Postgres {
171 2 : will_init: false,
172 2 : rec: Bytes::from_static(b""),
173 2 : };
174 2 : let rec = Value::WalRecord(rec);
175 2 :
176 2 : #[rustfmt::skip]
177 2 : let expected = [
178 2 : // flattened discriminator of total 8 bytes
179 2 : 0x00, 0x00, 0x00, 0x01,
180 2 : 0x00, 0x00, 0x00, 0x00,
181 2 : // will_init
182 2 : 0x00,
183 2 : // 8 byte length
184 2 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
185 2 : ];
186 2 :
187 2 : roundtrip!(rec, expected);
188 :
189 2 : assert!(!ValueBytes::will_init(&expected).unwrap());
190 2 : assert_eq!(expected.len(), 17);
191 12 : for len in 12..17 {
192 10 : assert_eq!(
193 10 : ValueBytes::will_init(&expected[..len]).unwrap_err(),
194 10 : InvalidInput::TooShortPostgresRecord
195 10 : )
196 : }
197 26 : for len in 0..12 {
198 24 : assert_eq!(
199 24 : ValueBytes::will_init(&expected[..len]).unwrap_err(),
200 24 : InvalidInput::TooShortValue
201 24 : )
202 : }
203 2 : }
204 :
205 : #[test]
206 2 : fn clear_visibility_map_flags_example() {
207 2 : let rec = NeonWalRecord::ClearVisibilityMapFlags {
208 2 : new_heap_blkno: Some(0x11),
209 2 : old_heap_blkno: None,
210 2 : flags: 0x03,
211 2 : };
212 2 : let rec = Value::WalRecord(rec);
213 2 :
214 2 : #[rustfmt::skip]
215 2 : let expected = [
216 2 : // discriminators
217 2 : 0x00, 0x00, 0x00, 0x01,
218 2 : 0x00, 0x00, 0x00, 0x01,
219 2 : // Some == 1 followed by 4 bytes
220 2 : 0x01, 0x00, 0x00, 0x00, 0x11,
221 2 : // None == 0
222 2 : 0x00,
223 2 : // flags
224 2 : 0x03
225 2 : ];
226 2 :
227 2 : roundtrip!(rec, expected);
228 :
229 2 : assert!(!ValueBytes::will_init(&expected).unwrap());
230 2 : }
231 : }
232 :
233 : ///
234 : /// Result of performing GC
235 : ///
236 0 : #[derive(Default, Serialize, Debug)]
237 : pub struct GcResult {
238 : pub layers_total: u64,
239 : pub layers_needed_by_cutoff: u64,
240 : pub layers_needed_by_pitr: u64,
241 : pub layers_needed_by_branches: u64,
242 : pub layers_needed_by_leases: u64,
243 : pub layers_not_updated: u64,
244 : pub layers_removed: u64, // # of layer files removed because they have been made obsolete by newer ondisk files.
245 :
246 : #[serde(serialize_with = "serialize_duration_as_millis")]
247 : pub elapsed: Duration,
248 :
249 : /// The layers which were garbage collected.
250 : ///
251 : /// Used in `/v1/tenant/:tenant_id/timeline/:timeline_id/do_gc` to wait for the layers to be
252 : /// dropped in tests.
253 : #[cfg(feature = "testing")]
254 : #[serde(skip)]
255 : pub(crate) doomed_layers: Vec<crate::tenant::storage_layer::Layer>,
256 : }
257 :
258 : // helper function for `GcResult`, serializing a `Duration` as an integer number of milliseconds
259 0 : fn serialize_duration_as_millis<S>(d: &Duration, serializer: S) -> Result<S::Ok, S::Error>
260 0 : where
261 0 : S: serde::Serializer,
262 0 : {
263 0 : d.as_millis().serialize(serializer)
264 0 : }
265 :
266 : impl AddAssign for GcResult {
267 4 : fn add_assign(&mut self, other: Self) {
268 4 : self.layers_total += other.layers_total;
269 4 : self.layers_needed_by_pitr += other.layers_needed_by_pitr;
270 4 : self.layers_needed_by_cutoff += other.layers_needed_by_cutoff;
271 4 : self.layers_needed_by_branches += other.layers_needed_by_branches;
272 4 : self.layers_needed_by_leases += other.layers_needed_by_leases;
273 4 : self.layers_not_updated += other.layers_not_updated;
274 4 : self.layers_removed += other.layers_removed;
275 4 :
276 4 : self.elapsed += other.elapsed;
277 4 :
278 4 : #[cfg(feature = "testing")]
279 4 : {
280 4 : let mut other = other;
281 4 : self.doomed_layers.append(&mut other.doomed_layers);
282 4 : }
283 4 : }
284 : }
|