LCOV - code coverage report
Current view: top level - libs/utils/src - id.rs (source / functions) Coverage Total Hit
Test: 2aa98e37cd3250b9a68c97ef6050b16fe702ab33.info Lines: 85.4 % 268 229
Test Date: 2024-08-29 11:33:10 Functions: 54.0 % 163 88

            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        13654 :     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
      27        13654 :     where
      28        13654 :         S: serde::Serializer,
      29        13654 :     {
      30        13654 :         if serializer.is_human_readable() {
      31         3274 :             serializer.collect_str(self)
      32              :         } else {
      33        10380 :             self.0.serialize(serializer)
      34              :         }
      35        13654 :     }
      36              : }
      37              : 
      38              : impl<'de> Deserialize<'de> for Id {
      39       934308 :     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
      40       934308 :     where
      41       934308 :         D: serde::Deserializer<'de>,
      42       934308 :     {
      43       934308 :         struct IdVisitor {
      44       934308 :             is_human_readable_deserializer: bool,
      45       934308 :         }
      46       934308 : 
      47       934308 :         impl<'de> Visitor<'de> for IdVisitor {
      48       934308 :             type Value = Id;
      49       934308 : 
      50       934308 :             fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
      51            0 :                 if self.is_human_readable_deserializer {
      52       934308 :                     formatter.write_str("value in form of hex string")
      53       934308 :                 } else {
      54       934308 :                     formatter.write_str("value in form of integer array([u8; 16])")
      55       934308 :                 }
      56       934308 :             }
      57       934308 : 
      58       934308 :             fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
      59       933786 :             where
      60       933786 :                 A: serde::de::SeqAccess<'de>,
      61       933786 :             {
      62       933786 :                 let s = serde::de::value::SeqAccessDeserializer::new(seq);
      63       934308 :                 let id: [u8; 16] = Deserialize::deserialize(s)?;
      64       934308 :                 Ok(Id::from(id))
      65       934308 :             }
      66       934308 : 
      67       934308 :             fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
      68          522 :             where
      69          522 :                 E: serde::de::Error,
      70          522 :             {
      71          522 :                 Id::from_str(v).map_err(E::custom)
      72          522 :             }
      73       934308 :         }
      74       934308 : 
      75       934308 :         if deserializer.is_human_readable() {
      76          522 :             deserializer.deserialize_str(IdVisitor {
      77          522 :                 is_human_readable_deserializer: true,
      78          522 :             })
      79              :         } else {
      80       933786 :             deserializer.deserialize_tuple(
      81       933786 :                 16,
      82       933786 :                 IdVisitor {
      83       933786 :                     is_human_readable_deserializer: false,
      84       933786 :                 },
      85       933786 :             )
      86              :         }
      87       934308 :     }
      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           84 :     pub fn from_slice(src: &[u8]) -> Result<Id, IdError> {
      98           84 :         if src.len() != 16 {
      99            0 :             return Err(IdError::SliceParseError(src.len()));
     100           84 :         }
     101           84 :         let mut id_array = [0u8; 16];
     102           84 :         id_array.copy_from_slice(src);
     103           84 :         Ok(id_array.into())
     104           84 :     }
     105              : 
     106           66 :     pub fn as_arr(&self) -> [u8; 16] {
     107           66 :         self.0
     108           66 :     }
     109              : 
     110        25728 :     pub fn generate() -> Self {
     111        25728 :         let mut tli_buf = [0u8; 16];
     112        25728 :         rand::thread_rng().fill(&mut tli_buf);
     113        25728 :         Id::from(tli_buf)
     114        25728 :     }
     115              : 
     116       593900 :     fn hex_encode(&self) -> String {
     117       593900 :         static HEX: &[u8] = b"0123456789abcdef";
     118       593900 : 
     119       593900 :         let mut buf = vec![0u8; self.0.len() * 2];
     120      9502400 :         for (&b, chunk) in self.0.as_ref().iter().zip(buf.chunks_exact_mut(2)) {
     121      9502400 :             chunk[0] = HEX[((b >> 4) & 0xf) as usize];
     122      9502400 :             chunk[1] = HEX[(b & 0xf) as usize];
     123      9502400 :         }
     124              : 
     125              :         // SAFETY: vec constructed out of `HEX`, it can only be ascii
     126       593900 :         unsafe { String::from_utf8_unchecked(buf) }
     127       593900 :     }
     128              : }
     129              : 
     130              : impl FromStr for Id {
     131              :     type Err = hex::FromHexError;
     132              : 
     133        25549 :     fn from_str(s: &str) -> Result<Id, Self::Err> {
     134        25549 :         Self::from_hex(s)
     135        25549 :     }
     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        25717 :     fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
     143        25717 :         let mut buf: [u8; 16] = [0u8; 16];
     144        25717 :         hex::decode_to_slice(hex, &mut buf)?;
     145        25663 :         Ok(Id(buf))
     146        25717 :     }
     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      2560680 :     fn from(b: [u8; 16]) -> Self {
     157      2560680 :         Id(b)
     158      2560680 :     }
     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       580892 :     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     169       580892 :         f.write_str(&self.hex_encode())
     170       580892 :     }
     171              : }
     172              : 
     173              : impl fmt::Debug for Id {
     174        13008 :     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     175        13008 :         f.write_str(&self.hex_encode())
     176        13008 :     }
     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           84 :             pub fn from_slice(src: &[u8]) -> Result<$t, IdError> {
     187           84 :                 Ok($t(Id::from_slice(src)?))
     188           84 :             }
     189              : 
     190           66 :             pub fn as_arr(&self) -> [u8; 16] {
     191           66 :                 self.0.as_arr()
     192           66 :             }
     193              : 
     194        25728 :             pub fn generate() -> Self {
     195        25728 :                 $t(Id::generate())
     196        25728 :             }
     197              : 
     198           24 :             pub const fn from_array(b: [u8; 16]) -> Self {
     199           24 :                 $t(Id(b))
     200           24 :             }
     201              :         }
     202              : 
     203              :         impl FromStr for $t {
     204              :             type Err = hex::FromHexError;
     205              : 
     206        25027 :             fn from_str(s: &str) -> Result<$t, Self::Err> {
     207        25027 :                 let value = Id::from_str(s)?;
     208        24973 :                 Ok($t(value))
     209        25027 :             }
     210              :         }
     211              : 
     212              :         impl From<[u8; 16]> for $t {
     213      1601076 :             fn from(b: [u8; 16]) -> Self {
     214      1601076 :                 $t(Id::from(b))
     215      1601076 :             }
     216              :         }
     217              : 
     218              :         impl FromHex for $t {
     219              :             type Error = hex::FromHexError;
     220              : 
     221          168 :             fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
     222          168 :                 Ok($t(Id::from_hex(hex)?))
     223          168 :             }
     224              :         }
     225              : 
     226              :         impl AsRef<[u8]> for $t {
     227           96 :             fn as_ref(&self) -> &[u8] {
     228           96 :                 &self.0 .0
     229           96 :             }
     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       577618 :             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     240       577618 :                 self.0.fmt(f)
     241       577618 :             }
     242              :         }
     243              : 
     244              :         impl fmt::Debug for $t {
     245        13008 :             fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     246        13008 :                 self.0.fmt(f)
     247        13008 :             }
     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       467184 : #[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           24 :     fn try_from(value: Option<&str>) -> Result<Self, Self::Error> {
     287           24 :         value
     288           24 :             .unwrap_or_default()
     289           24 :             .parse::<TimelineId>()
     290           24 :             .with_context(|| format!("Could not parse timeline id from {:?}", value))
     291           24 :     }
     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       467106 : #[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      1074606 :     pub fn new(tenant_id: TenantId, timeline_id: TimelineId) -> Self {
     314      1074606 :         TenantTimelineId {
     315      1074606 :             tenant_id,
     316      1074606 :             timeline_id,
     317      1074606 :         }
     318      1074606 :     }
     319              : 
     320        12060 :     pub fn generate() -> Self {
     321        12060 :         Self::new(TenantId::generate(), TimelineId::generate())
     322        12060 :     }
     323              : 
     324       593248 :     pub fn empty() -> Self {
     325       593248 :         Self::new(TenantId::from([0u8; 16]), TimelineId::from([0u8; 16]))
     326       593248 :     }
     327              : }
     328              : 
     329              : impl fmt::Display for TenantTimelineId {
     330          306 :     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     331          306 :         write!(f, "{}/{}", self.tenant_id, self.timeline_id)
     332          306 :     }
     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           12 : #[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            6 :     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     363            6 :         write!(f, "{}", self.0)
     364            6 :     }
     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            6 :     fn test_id_serde_non_human_readable() {
     384            6 :         let original_id = Id([
     385            6 :             173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
     386            6 :         ]);
     387            6 :         let expected_tokens = Tokens(vec![
     388            6 :             Token::Tuple { len: 16 },
     389            6 :             Token::U8(173),
     390            6 :             Token::U8(80),
     391            6 :             Token::U8(132),
     392            6 :             Token::U8(115),
     393            6 :             Token::U8(129),
     394            6 :             Token::U8(226),
     395            6 :             Token::U8(72),
     396            6 :             Token::U8(254),
     397            6 :             Token::U8(170),
     398            6 :             Token::U8(201),
     399            6 :             Token::U8(135),
     400            6 :             Token::U8(108),
     401            6 :             Token::U8(199),
     402            6 :             Token::U8(26),
     403            6 :             Token::U8(228),
     404            6 :             Token::U8(24),
     405            6 :             Token::TupleEnd,
     406            6 :         ]);
     407            6 : 
     408            6 :         let serializer = Serializer::builder().is_human_readable(false).build();
     409            6 :         let serialized_tokens = original_id.serialize(&serializer).unwrap();
     410            6 :         assert_eq!(serialized_tokens, expected_tokens);
     411              : 
     412            6 :         let mut deserializer = Deserializer::builder()
     413            6 :             .is_human_readable(false)
     414            6 :             .tokens(serialized_tokens)
     415            6 :             .build();
     416            6 :         let deserialized_id = Id::deserialize(&mut deserializer).unwrap();
     417            6 :         assert_eq!(deserialized_id, original_id);
     418            6 :     }
     419              : 
     420              :     #[test]
     421            6 :     fn test_id_serde_human_readable() {
     422            6 :         let original_id = Id([
     423            6 :             173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
     424            6 :         ]);
     425            6 :         let expected_tokens = Tokens(vec![Token::Str(String::from(
     426            6 :             "ad50847381e248feaac9876cc71ae418",
     427            6 :         ))]);
     428            6 : 
     429            6 :         let serializer = Serializer::builder().is_human_readable(true).build();
     430            6 :         let serialized_tokens = original_id.serialize(&serializer).unwrap();
     431            6 :         assert_eq!(serialized_tokens, expected_tokens);
     432              : 
     433            6 :         let mut deserializer = Deserializer::builder()
     434            6 :             .is_human_readable(true)
     435            6 :             .tokens(Tokens(vec![Token::Str(String::from(
     436            6 :                 "ad50847381e248feaac9876cc71ae418",
     437            6 :             ))]))
     438            6 :             .build();
     439            6 :         assert_eq!(Id::deserialize(&mut deserializer).unwrap(), original_id);
     440            6 :     }
     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            6 :     fn test_id_bincode_serde() {
     457            6 :         let expected_bytes = [
     458            6 :             173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
     459            6 :         ];
     460            6 : 
     461            6 :         roundtrip_type!(Id, expected_bytes);
     462            6 :     }
     463              : 
     464              :     #[test]
     465            6 :     fn test_tenant_id_bincode_serde() {
     466            6 :         let expected_bytes = [
     467            6 :             173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
     468            6 :         ];
     469            6 : 
     470            6 :         roundtrip_type!(TenantId, expected_bytes);
     471            6 :     }
     472              : 
     473              :     #[test]
     474            6 :     fn test_timeline_id_bincode_serde() {
     475            6 :         let expected_bytes = [
     476            6 :             173, 80, 132, 115, 129, 226, 72, 254, 170, 201, 135, 108, 199, 26, 228, 24,
     477            6 :         ];
     478            6 : 
     479            6 :         roundtrip_type!(TimelineId, expected_bytes);
     480            6 :     }
     481              : }
        

Generated by: LCOV version 2.1-beta