From 79f9f7c5f8c902f7bfd825f0bc1295cc6a41417d Mon Sep 17 00:00:00 2001 From: John Spray Date: Tue, 29 Aug 2023 14:36:43 +0100 Subject: [PATCH] fixup control_api types --- libs/pageserver_api/Cargo.toml | 1 + libs/pageserver_api/src/control_api.rs | 67 ++++++++++++++++++++------ 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/libs/pageserver_api/Cargo.toml b/libs/pageserver_api/Cargo.toml index f97ec54e91..7fdd46eede 100644 --- a/libs/pageserver_api/Cargo.toml +++ b/libs/pageserver_api/Cargo.toml @@ -12,6 +12,7 @@ const_format.workspace = true anyhow.workspace = true bytes.workspace = true byteorder.workspace = true +hex.workspace = true utils.workspace = true postgres_ffi.workspace = true enum-map.workspace = true diff --git a/libs/pageserver_api/src/control_api.rs b/libs/pageserver_api/src/control_api.rs index 3ed7b03be5..7657b747dc 100644 --- a/libs/pageserver_api/src/control_api.rs +++ b/libs/pageserver_api/src/control_api.rs @@ -1,7 +1,55 @@ /// Types in this file are for pageserver's upward-facing API calls to the control plane +use hex::FromHex; use serde::{Deserialize, Serialize}; use utils::id::{NodeId, TenantId}; +/// TenantId's serialization is an array of u8, which is rather unfriendly +/// for outside callers who aren't working with the native Rust TenantId. +/// This class wraps it in serialization that is just the hex strict +/// representation. +#[derive(Eq, PartialEq, Clone, Hash)] +pub struct HexTenantId(TenantId); + +impl HexTenantId { + pub fn new(t: TenantId) -> Self { + Self(t) + } + + pub fn take(self) -> TenantId { + self.0 + } +} + +impl AsRef for HexTenantId { + fn as_ref(&self) -> &TenantId { + &self.0 + } +} + +impl Serialize for HexTenantId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let hex = self.0.hex_encode(); + serializer.collect_str(&hex) + } +} + +impl<'de> Deserialize<'de> for HexTenantId { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let string = String::deserialize(deserializer)?; + TenantId::from_hex(string) + .map(|t| HexTenantId::new(t)) + .map_err(|e| serde::de::Error::custom(format!("{e}"))) + } +} + +// Top level s + #[derive(Serialize, Deserialize)] pub struct ReAttachRequest { pub node_id: NodeId, @@ -9,7 +57,7 @@ pub struct ReAttachRequest { #[derive(Serialize, Deserialize)] pub struct ReAttachResponseTenant { - pub id: TenantId, + pub id: HexTenantId, pub generation: u32, } @@ -20,7 +68,7 @@ pub struct ReAttachResponse { #[derive(Serialize, Deserialize)] pub struct ValidateRequestTenant { - pub id: TenantId, + pub id: HexTenantId, pub gen: u32, } @@ -36,19 +84,6 @@ pub struct ValidateResponse { #[derive(Serialize, Deserialize)] pub struct ValidateResponseTenant { - pub id: TenantId, + pub id: HexTenantId, pub valid: bool, } - -#[cfg(testing)] -#[derive(Serialize, Deserialize)] -pub struct AttachHookRequest { - tenant_id: TenantId, - pageserver_id: Option, -} - -#[cfg(testing)] -#[derive(Serialize, Deserialize)] -pub struct AttachHookResponse { - gen: Option, -}