Line data Source code
1 : use std::num::ParseIntError;
2 : use std::{fmt, str::FromStr};
3 :
4 : use anyhow::Context;
5 : use hex::FromHex;
6 : use rand::Rng;
7 : use serde::de::Visitor;
8 : use serde::{Deserialize, Serialize};
9 : use thiserror::Error;
10 :
11 0 : #[derive(Error, Debug)]
12 : pub enum IdError {
13 : #[error("invalid id length {0}")]
14 : SliceParseError(usize),
15 : }
16 :
17 : /// Neon ID is a 128-bit random ID.
18 : /// Used to represent various identifiers. Provides handy utility methods and impls.
19 : ///
20 : /// NOTE: It (de)serializes as an array of hex bytes, so the string representation would look
21 : /// like `[173,80,132,115,129,226,72,254,170,201,135,108,199,26,228,24]`.
22 : #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
23 : struct Id([u8; 16]);
24 :
25 : impl Serialize for Id {
26 4423 : fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
27 4423 : where
28 4423 : S: serde::Serializer,
29 4423 : {
30 4423 : if serializer.is_human_readable() {
31 1091 : serializer.collect_str(self)
32 : } else {
33 3332 : self.0.serialize(serializer)
34 : }
35 4423 : }
36 : }
37 :
38 : impl<'de> Deserialize<'de> for Id {
39 313688 : fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
40 313688 : where
41 313688 : D: serde::Deserializer<'de>,
42 313688 : {
43 313688 : struct IdVisitor {
44 313688 : is_human_readable_deserializer: bool,
45 313688 : }
46 313688 :
47 313688 : impl<'de> Visitor<'de> for IdVisitor {
48 313688 : type Value = Id;
49 313688 :
50 313688 : fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
51 0 : if self.is_human_readable_deserializer {
52 313688 : formatter.write_str("value in form of hex string")
53 313688 : } else {
54 313688 : formatter.write_str("value in form of integer array([u8; 16])")
55 313688 : }
56 313688 : }
57 313688 :
58 313688 : fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
59 313516 : where
60 313516 : A: serde::de::SeqAccess<'de>,
61 313516 : {
62 313516 : let s = serde::de::value::SeqAccessDeserializer::new(seq);
63 313688 : let id: [u8; 16] = Deserialize::deserialize(s)?;
64 313688 : Ok(Id::from(id))
65 313688 : }
66 313688 :
67 313688 : fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
68 172 : where
69 172 : E: serde::de::Error,
70 172 : {
71 172 : Id::from_str(v).map_err(E::custom)
72 172 : }
73 313688 : }
74 313688 :
75 313688 : if deserializer.is_human_readable() {
76 172 : deserializer.deserialize_str(IdVisitor {
77 172 : is_human_readable_deserializer: true,
78 172 : })
79 : } else {
80 313516 : deserializer.deserialize_tuple(
81 313516 : 16,
82 313516 : IdVisitor {
83 313516 : is_human_readable_deserializer: false,
84 313516 : },
85 313516 : )
86 : }
87 313688 : }
88 : }
89 :
90 : impl Id {
91 0 : pub fn get_from_buf(buf: &mut impl bytes::Buf) -> Id {
92 0 : let mut arr = [0u8; 16];
93 0 : buf.copy_to_slice(&mut arr);
94 0 : Id::from(arr)
95 0 : }
96 :
97 28 : pub fn from_slice(src: &[u8]) -> Result<Id, IdError> {
98 28 : if src.len() != 16 {
99 0 : return Err(IdError::SliceParseError(src.len()));
100 28 : }
101 28 : let mut id_array = [0u8; 16];
102 28 : id_array.copy_from_slice(src);
103 28 : Ok(id_array.into())
104 28 : }
105 :
106 18 : pub fn as_arr(&self) -> [u8; 16] {
107 18 : self.0
108 18 : }
109 :
110 8558 : pub fn generate() -> Self {
111 8558 : let mut tli_buf = [0u8; 16];
112 8558 : rand::thread_rng().fill(&mut tli_buf);
113 8558 : Id::from(tli_buf)
114 8558 : }
115 :
116 200925 : fn hex_encode(&self) -> String {
117 200925 : static HEX: &[u8] = b"0123456789abcdef";
118 200925 :
119 200925 : let mut buf = vec![0u8; self.0.len() * 2];
120 3214800 : for (&b, chunk) in self.0.as_ref().iter().zip(buf.chunks_exact_mut(2)) {
121 3214800 : chunk[0] = HEX[((b >> 4) & 0xf) as usize];
122 3214800 : chunk[1] = HEX[(b & 0xf) as usize];
123 3214800 : }
124 :
125 : // SAFETY: vec constructed out of `HEX`, it can only be ascii
126 200925 : unsafe { String::from_utf8_unchecked(buf) }
127 200925 : }
128 : }
129 :
130 : impl FromStr for Id {
131 : type Err = hex::FromHexError;
132 :
133 8455 : fn from_str(s: &str) -> Result<Id, Self::Err> {
134 8455 : Self::from_hex(s)
135 8455 : }
136 : }
137 :
138 : // this is needed for pretty serialization and deserialization of Id's using serde integration with hex crate
139 : impl FromHex for Id {
140 : type Error = hex::FromHexError;
141 :
142 8503 : fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
143 8503 : let mut buf: [u8; 16] = [0u8; 16];
144 8503 : hex::decode_to_slice(hex, &mut buf)?;
145 8485 : Ok(Id(buf))
146 8503 : }
147 : }
148 :
149 : impl AsRef<[u8]> for Id {
150 0 : fn as_ref(&self) -> &[u8] {
151 0 : &self.0
152 0 : }
153 : }
154 :
155 : impl From<[u8; 16]> for Id {
156 862344 : fn from(b: [u8; 16]) -> Self {
157 862344 : Id(b)
158 862344 : }
159 : }
160 :
161 : impl From<Id> for u128 {
162 0 : fn from(id: Id) -> Self {
163 0 : u128::from_le_bytes(id.0)
164 0 : }
165 : }
166 :
167 : impl fmt::Display for Id {
168 196589 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 196589 : f.write_str(&self.hex_encode())
170 196589 : }
171 : }
172 :
173 : impl fmt::Debug for Id {
174 4336 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 4336 : f.write_str(&self.hex_encode())
176 4336 : }
177 : }
178 :
179 : macro_rules! id_newtype {
180 : ($t:ident) => {
181 : impl $t {
182 0 : pub fn get_from_buf(buf: &mut impl bytes::Buf) -> $t {
183 0 : $t(Id::get_from_buf(buf))
184 0 : }
185 :
186 28 : pub fn from_slice(src: &[u8]) -> Result<$t, IdError> {
187 28 : Ok($t(Id::from_slice(src)?))
188 28 : }
189 :
190 18 : pub fn as_arr(&self) -> [u8; 16] {
191 18 : self.0.as_arr()
192 18 : }
193 :
194 8558 : pub fn generate() -> Self {
195 8558 : $t(Id::generate())
196 8558 : }
197 :
198 8 : pub const fn from_array(b: [u8; 16]) -> Self {
199 8 : $t(Id(b))
200 8 : }
201 : }
202 :
203 : impl FromStr for $t {
204 : type Err = hex::FromHexError;
205 :
206 8283 : fn from_str(s: &str) -> Result<$t, Self::Err> {
207 8283 : let value = Id::from_str(s)?;
208 8265 : Ok($t(value))
209 8283 : }
210 : }
211 :
212 : impl From<[u8; 16]> for $t {
213 540240 : fn from(b: [u8; 16]) -> Self {
214 540240 : $t(Id::from(b))
215 540240 : }
216 : }
217 :
218 : impl FromHex for $t {
219 : type Error = hex::FromHexError;
220 :
221 48 : fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
222 48 : Ok($t(Id::from_hex(hex)?))
223 48 : }
224 : }
225 :
226 : impl AsRef<[u8]> for $t {
227 32 : fn as_ref(&self) -> &[u8] {
228 32 : &self.0 .0
229 32 : }
230 : }
231 :
232 : impl From<$t> for u128 {
233 0 : fn from(id: $t) -> Self {
234 0 : u128::from(id.0)
235 0 : }
236 : }
237 :
238 : impl fmt::Display for $t {
239 195498 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240 195498 : self.0.fmt(f)
241 195498 : }
242 : }
243 :
244 : impl fmt::Debug for $t {
245 4336 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246 4336 : self.0.fmt(f)
247 4336 : }
248 : }
249 : };
250 : }
251 :
252 : /// Neon timeline IDs are different from PostgreSQL timeline
253 : /// IDs. They serve a similar purpose though: they differentiate
254 : /// between different "histories" of the same cluster. However,
255 : /// PostgreSQL timeline IDs are a bit cumbersome, because they are only
256 : /// 32-bits wide, and they must be in ascending order in any given
257 : /// timeline history. Those limitations mean that we cannot generate a
258 : /// new PostgreSQL timeline ID by just generating a random number. And
259 : /// that in turn is problematic for the "pull/push" workflow, where you
260 : /// have a local copy of a Neon repository, and you periodically sync
261 : /// the local changes with a remote server. When you work "detached"
262 : /// from the remote server, you cannot create a PostgreSQL timeline ID
263 : /// that's guaranteed to be different from all existing timelines in
264 : /// the remote server. For example, if two people are having a clone of
265 : /// the repository on their laptops, and they both create a new branch
266 : /// with different name. What timeline ID would they assign to their
267 : /// branches? If they pick the same one, and later try to push the
268 : /// branches to the same remote server, they will get mixed up.
269 : ///
270 : /// To avoid those issues, Neon has its own concept of timelines that
271 : /// is separate from PostgreSQL timelines, and doesn't have those
272 : /// limitations. A Neon timeline is identified by a 128-bit ID, which
273 : /// is usually printed out as a hex string.
274 : ///
275 : /// NOTE: It (de)serializes as an array of hex bytes, so the string representation would look
276 : /// like `[173,80,132,115,129,226,72,254,170,201,135,108,199,26,228,24]`.
277 : /// See [`Id`] for alternative ways to serialize it.
278 156853 : #[derive(Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
279 : pub struct TimelineId(Id);
280 :
281 : id_newtype!(TimelineId);
282 :
283 : impl TryFrom<Option<&str>> for TimelineId {
284 : type Error = anyhow::Error;
285 :
286 8 : fn try_from(value: Option<&str>) -> Result<Self, Self::Error> {
287 8 : value
288 8 : .unwrap_or_default()
289 8 : .parse::<TimelineId>()
290 8 : .with_context(|| format!("Could not parse timeline id from {:?}", value))
291 8 : }
292 : }
293 :
294 : /// Neon Tenant Id represents identifiar of a particular tenant.
295 : /// Is used for distinguishing requests and data belonging to different users.
296 : ///
297 : /// NOTE: It (de)serializes as an array of hex bytes, so the string representation would look
298 : /// like `[173,80,132,115,129,226,72,254,170,201,135,108,199,26,228,24]`.
299 : /// See [`Id`] for alternative ways to serialize it.
300 156829 : #[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
301 : pub struct TenantId(Id);
302 :
303 : id_newtype!(TenantId);
304 :
305 : // A pair uniquely identifying Neon instance.
306 0 : #[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
307 : pub struct TenantTimelineId {
308 : pub tenant_id: TenantId,
309 : pub timeline_id: TimelineId,
310 : }
311 :
312 : impl TenantTimelineId {
313 361605 : pub fn new(tenant_id: TenantId, timeline_id: TimelineId) -> Self {
314 361605 : TenantTimelineId {
315 361605 : tenant_id,
316 361605 : timeline_id,
317 361605 : }
318 361605 : }
319 :
320 4020 : pub fn generate() -> Self {
321 4020 : Self::new(TenantId::generate(), TimelineId::generate())
322 4020 : }
323 :
324 199920 : pub fn empty() -> Self {
325 199920 : Self::new(TenantId::from([0u8; 16]), TimelineId::from([0u8; 16]))
326 199920 : }
327 : }
328 :
329 : impl fmt::Display for TenantTimelineId {
330 102 : fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
331 102 : write!(f, "{}/{}", self.tenant_id, self.timeline_id)
332 102 : }
333 : }
334 :
335 : impl FromStr for TenantTimelineId {
336 : type Err = anyhow::Error;
337 :
338 0 : fn from_str(s: &str) -> Result<Self, Self::Err> {
339 0 : let mut parts = s.split('/');
340 0 : let tenant_id = parts
341 0 : .next()
342 0 : .ok_or_else(|| anyhow::anyhow!("TenantTimelineId must contain tenant_id"))?
343 0 : .parse()?;
344 0 : let timeline_id = parts
345 0 : .next()
346 0 : .ok_or_else(|| anyhow::anyhow!("TenantTimelineId must contain timeline_id"))?
347 0 : .parse()?;
348 0 : if parts.next().is_some() {
349 0 : anyhow::bail!("TenantTimelineId must contain only tenant_id and timeline_id");
350 0 : }
351 0 : Ok(TenantTimelineId::new(tenant_id, timeline_id))
352 0 : }
353 : }
354 :
355 : // Unique ID of a storage node (safekeeper or pageserver). Supposed to be issued
356 : // by the console.
357 4 : #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash, Debug, Serialize, Deserialize)]
358 : #[serde(transparent)]
359 : pub struct NodeId(pub u64);
360 :
361 : impl fmt::Display for NodeId {
362 2 : fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363 2 : write!(f, "{}", self.0)
364 2 : }
365 : }
366 :
367 : impl FromStr for NodeId {
368 : type Err = ParseIntError;
369 0 : fn from_str(s: &str) -> Result<Self, Self::Err> {
370 0 : Ok(NodeId(u64::from_str(s)?))
371 0 : }
372 : }
373 :
374 : #[cfg(test)]
375 : mod tests {
376 : use serde_assert::{Deserializer, Serializer, Token, Tokens};
377 :
378 : use crate::bin_ser::BeSer;
379 :
380 : use super::*;
381 :
382 : #[test]
383 2 : fn test_id_serde_non_human_readable() {
384 2 : let original_id = Id([
385 2 : 173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
386 2 : ]);
387 2 : let expected_tokens = Tokens(vec![
388 2 : Token::Tuple { len: 16 },
389 2 : Token::U8(173),
390 2 : Token::U8(80),
391 2 : Token::U8(132),
392 2 : Token::U8(115),
393 2 : Token::U8(129),
394 2 : Token::U8(226),
395 2 : Token::U8(72),
396 2 : Token::U8(254),
397 2 : Token::U8(170),
398 2 : Token::U8(201),
399 2 : Token::U8(135),
400 2 : Token::U8(108),
401 2 : Token::U8(199),
402 2 : Token::U8(26),
403 2 : Token::U8(228),
404 2 : Token::U8(24),
405 2 : Token::TupleEnd,
406 2 : ]);
407 2 :
408 2 : let serializer = Serializer::builder().is_human_readable(false).build();
409 2 : let serialized_tokens = original_id.serialize(&serializer).unwrap();
410 2 : assert_eq!(serialized_tokens, expected_tokens);
411 :
412 2 : let mut deserializer = Deserializer::builder()
413 2 : .is_human_readable(false)
414 2 : .tokens(serialized_tokens)
415 2 : .build();
416 2 : let deserialized_id = Id::deserialize(&mut deserializer).unwrap();
417 2 : assert_eq!(deserialized_id, original_id);
418 2 : }
419 :
420 : #[test]
421 2 : fn test_id_serde_human_readable() {
422 2 : let original_id = Id([
423 2 : 173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
424 2 : ]);
425 2 : let expected_tokens = Tokens(vec![Token::Str(String::from(
426 2 : "ad50847381e248feaac9876cc71ae418",
427 2 : ))]);
428 2 :
429 2 : let serializer = Serializer::builder().is_human_readable(true).build();
430 2 : let serialized_tokens = original_id.serialize(&serializer).unwrap();
431 2 : assert_eq!(serialized_tokens, expected_tokens);
432 :
433 2 : let mut deserializer = Deserializer::builder()
434 2 : .is_human_readable(true)
435 2 : .tokens(Tokens(vec![Token::Str(String::from(
436 2 : "ad50847381e248feaac9876cc71ae418",
437 2 : ))]))
438 2 : .build();
439 2 : assert_eq!(Id::deserialize(&mut deserializer).unwrap(), original_id);
440 2 : }
441 :
442 : macro_rules! roundtrip_type {
443 : ($type:ty, $expected_bytes:expr) => {{
444 : let expected_bytes: [u8; 16] = $expected_bytes;
445 : let original_id = <$type>::from(expected_bytes);
446 :
447 : let ser_bytes = original_id.ser().unwrap();
448 : assert_eq!(ser_bytes, expected_bytes);
449 :
450 : let des_id = <$type>::des(&ser_bytes).unwrap();
451 : assert_eq!(des_id, original_id);
452 : }};
453 : }
454 :
455 : #[test]
456 2 : fn test_id_bincode_serde() {
457 2 : let expected_bytes = [
458 2 : 173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
459 2 : ];
460 2 :
461 2 : roundtrip_type!(Id, expected_bytes);
462 2 : }
463 :
464 : #[test]
465 2 : fn test_tenant_id_bincode_serde() {
466 2 : let expected_bytes = [
467 2 : 173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
468 2 : ];
469 2 :
470 2 : roundtrip_type!(TenantId, expected_bytes);
471 2 : }
472 :
473 : #[test]
474 2 : fn test_timeline_id_bincode_serde() {
475 2 : let expected_bytes = [
476 2 : 173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
477 2 : ];
478 2 :
479 2 : roundtrip_type!(TimelineId, expected_bytes);
480 2 : }
481 : }
|