LCOV - code coverage report
Current view: top level - libs/utils/src - id.rs (source / functions) Coverage Total Hit
Test: f5f94ec0366b63fd2cbbe02edc2087dbd893d04d.info Lines: 85.7 % 245 210
Test Date: 2024-11-20 05:34:23 Functions: 56.6 % 159 90

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

Generated by: LCOV version 2.1-beta