Line data Source code
1 : //! See `pageserver_api::shard` for description on sharding.
2 :
3 : use std::ops::RangeInclusive;
4 : use std::str::FromStr;
5 :
6 : use hex::FromHex;
7 : use serde::{Deserialize, Serialize};
8 :
9 : use crate::id::TenantId;
10 :
11 0 : #[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Copy, Serialize, Deserialize, Debug, Hash)]
12 : pub struct ShardNumber(pub u8);
13 :
14 0 : #[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Copy, Serialize, Deserialize, Debug, Hash)]
15 : pub struct ShardCount(pub u8);
16 :
17 : /// Combination of ShardNumber and ShardCount.
18 : ///
19 : /// For use within the context of a particular tenant, when we need to know which shard we're
20 : /// dealing with, but do not need to know the full ShardIdentity (because we won't be doing
21 : /// any page->shard mapping), and do not need to know the fully qualified TenantShardId.
22 : #[derive(Eq, PartialEq, PartialOrd, Ord, Clone, Copy, Hash)]
23 : pub struct ShardIndex {
24 : pub shard_number: ShardNumber,
25 : pub shard_count: ShardCount,
26 : }
27 :
28 : /// Formatting helper, for generating the `shard_id` label in traces.
29 : pub struct ShardSlug<'a>(&'a TenantShardId);
30 :
31 : /// TenantShardId globally identifies a particular shard in a particular tenant.
32 : ///
33 : /// These are written as `<TenantId>-<ShardSlug>`, for example:
34 : /// # The second shard in a two-shard tenant
35 : /// 072f1291a5310026820b2fe4b2968934-0102
36 : ///
37 : /// If the `ShardCount` is _unsharded_, the `TenantShardId` is written without
38 : /// a shard suffix and is equivalent to the encoding of a `TenantId`: this enables
39 : /// an unsharded [`TenantShardId`] to be used interchangably with a [`TenantId`].
40 : ///
41 : /// The human-readable encoding of an unsharded TenantShardId, such as used in API URLs,
42 : /// is both forward and backward compatible with TenantId: a legacy TenantId can be
43 : /// decoded as a TenantShardId, and when re-encoded it will be parseable
44 : /// as a TenantId.
45 : #[derive(Eq, PartialEq, PartialOrd, Ord, Clone, Copy, Hash)]
46 : pub struct TenantShardId {
47 : pub tenant_id: TenantId,
48 : pub shard_number: ShardNumber,
49 : pub shard_count: ShardCount,
50 : }
51 :
52 : impl ShardCount {
53 : pub const MAX: Self = Self(u8::MAX);
54 : pub const MIN: Self = Self(0);
55 :
56 : /// The internal value of a ShardCount may be zero, which means "1 shard, but use
57 : /// legacy format for TenantShardId that excludes the shard suffix", also known
58 : /// as [`TenantShardId::unsharded`].
59 : ///
60 : /// This method returns the actual number of shards, i.e. if our internal value is
61 : /// zero, we return 1 (unsharded tenants have 1 shard).
62 2407222 : pub fn count(&self) -> u8 {
63 2407222 : if self.0 > 0 { self.0 } else { 1 }
64 2407222 : }
65 :
66 : /// The literal internal value: this is **not** the number of shards in the
67 : /// tenant, as we have a special zero value for legacy unsharded tenants. Use
68 : /// [`Self::count`] if you want to know the cardinality of shards.
69 2 : pub fn literal(&self) -> u8 {
70 2 : self.0
71 2 : }
72 :
73 : /// Whether the `ShardCount` is for an unsharded tenant, so uses one shard but
74 : /// uses the legacy format for `TenantShardId`. See also the documentation for
75 : /// [`Self::count`].
76 0 : pub fn is_unsharded(&self) -> bool {
77 0 : self.0 == 0
78 0 : }
79 :
80 : /// `v` may be zero, or the number of shards in the tenant. `v` is what
81 : /// [`Self::literal`] would return.
82 10494 : pub const fn new(val: u8) -> Self {
83 10494 : Self(val)
84 10494 : }
85 : }
86 :
87 : impl ShardNumber {
88 : pub const MAX: Self = Self(u8::MAX);
89 : }
90 :
91 : impl TenantShardId {
92 34 : pub fn unsharded(tenant_id: TenantId) -> Self {
93 34 : Self {
94 34 : tenant_id,
95 34 : shard_number: ShardNumber(0),
96 34 : shard_count: ShardCount(0),
97 34 : }
98 34 : }
99 :
100 : /// The range of all TenantShardId that belong to a particular TenantId. This is useful when
101 : /// you have a BTreeMap of TenantShardId, and are querying by TenantId.
102 0 : pub fn tenant_range(tenant_id: TenantId) -> RangeInclusive<Self> {
103 0 : RangeInclusive::new(
104 0 : Self {
105 0 : tenant_id,
106 0 : shard_number: ShardNumber(0),
107 0 : shard_count: ShardCount(0),
108 0 : },
109 0 : Self {
110 0 : tenant_id,
111 0 : shard_number: ShardNumber::MAX,
112 0 : shard_count: ShardCount::MAX,
113 0 : },
114 : )
115 0 : }
116 :
117 0 : pub fn range(&self) -> RangeInclusive<Self> {
118 0 : RangeInclusive::new(*self, *self)
119 0 : }
120 :
121 18878 : pub fn shard_slug(&self) -> impl std::fmt::Display + '_ {
122 18878 : ShardSlug(self)
123 18878 : }
124 :
125 : /// Convenience for code that has special behavior on the 0th shard.
126 308 : pub fn is_shard_zero(&self) -> bool {
127 308 : self.shard_number == ShardNumber(0)
128 308 : }
129 :
130 : /// The "unsharded" value is distinct from simply having a single shard: it represents
131 : /// a tenant which is not shard-aware at all, and whose storage paths will not include
132 : /// a shard suffix.
133 0 : pub fn is_unsharded(&self) -> bool {
134 0 : self.shard_number == ShardNumber(0) && self.shard_count.is_unsharded()
135 0 : }
136 :
137 : /// Convenience for dropping the tenant_id and just getting the ShardIndex: this
138 : /// is useful when logging from code that is already in a span that includes tenant ID, to
139 : /// keep messages reasonably terse.
140 0 : pub fn to_index(&self) -> ShardIndex {
141 0 : ShardIndex {
142 0 : shard_number: self.shard_number,
143 0 : shard_count: self.shard_count,
144 0 : }
145 0 : }
146 :
147 : /// Calculate the children of this TenantShardId when splitting the overall tenant into
148 : /// the given number of shards.
149 5 : pub fn split(&self, new_shard_count: ShardCount) -> Vec<TenantShardId> {
150 5 : let effective_old_shard_count = std::cmp::max(self.shard_count.0, 1);
151 5 : let mut child_shards = Vec::new();
152 24 : for shard_number in 0..ShardNumber(new_shard_count.0).0 {
153 : // Key mapping is based on a round robin mapping of key hash modulo shard count,
154 : // so our child shards are the ones which the same keys would map to.
155 24 : if shard_number % effective_old_shard_count == self.shard_number.0 {
156 20 : child_shards.push(TenantShardId {
157 20 : tenant_id: self.tenant_id,
158 20 : shard_number: ShardNumber(shard_number),
159 20 : shard_count: new_shard_count,
160 20 : })
161 4 : }
162 : }
163 :
164 5 : child_shards
165 5 : }
166 : }
167 :
168 : impl std::fmt::Display for ShardNumber {
169 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170 0 : self.0.fmt(f)
171 0 : }
172 : }
173 :
174 : impl std::fmt::Display for ShardSlug<'_> {
175 5975 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
176 5975 : write!(
177 5975 : f,
178 5975 : "{:02x}{:02x}",
179 : self.0.shard_number.0, self.0.shard_count.0
180 : )
181 5975 : }
182 : }
183 :
184 : impl std::fmt::Display for TenantShardId {
185 11669 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186 11669 : if self.shard_count != ShardCount(0) {
187 118 : write!(f, "{}-{}", self.tenant_id, self.shard_slug())
188 : } else {
189 : // Legacy case (shard_count == 0) -- format as just the tenant id. Note that this
190 : // is distinct from the normal single shard case (shard count == 1).
191 11551 : self.tenant_id.fmt(f)
192 : }
193 11669 : }
194 : }
195 :
196 : impl std::fmt::Debug for TenantShardId {
197 5681 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
198 : // Debug is the same as Display: the compact hex representation
199 5681 : write!(f, "{self}")
200 5681 : }
201 : }
202 :
203 : impl std::str::FromStr for TenantShardId {
204 : type Err = hex::FromHexError;
205 :
206 16 : fn from_str(s: &str) -> Result<Self, Self::Err> {
207 : // Expect format: 16 byte TenantId, '-', 1 byte shard number, 1 byte shard count
208 16 : if s.len() == 32 {
209 : // Legacy case: no shard specified
210 : Ok(Self {
211 13 : tenant_id: TenantId::from_str(s)?,
212 13 : shard_number: ShardNumber(0),
213 13 : shard_count: ShardCount(0),
214 : })
215 3 : } else if s.len() == 37 {
216 3 : let bytes = s.as_bytes();
217 3 : let tenant_id = TenantId::from_hex(&bytes[0..32])?;
218 3 : let mut shard_parts: [u8; 2] = [0u8; 2];
219 3 : hex::decode_to_slice(&bytes[33..37], &mut shard_parts)?;
220 3 : Ok(Self {
221 3 : tenant_id,
222 3 : shard_number: ShardNumber(shard_parts[0]),
223 3 : shard_count: ShardCount(shard_parts[1]),
224 3 : })
225 : } else {
226 0 : Err(hex::FromHexError::InvalidStringLength)
227 : }
228 16 : }
229 : }
230 :
231 : impl From<[u8; 18]> for TenantShardId {
232 25 : fn from(b: [u8; 18]) -> Self {
233 25 : let tenant_id_bytes: [u8; 16] = b[0..16].try_into().unwrap();
234 :
235 25 : Self {
236 25 : tenant_id: TenantId::from(tenant_id_bytes),
237 25 : shard_number: ShardNumber(b[16]),
238 25 : shard_count: ShardCount(b[17]),
239 25 : }
240 25 : }
241 : }
242 :
243 : impl ShardIndex {
244 7 : pub fn new(number: ShardNumber, count: ShardCount) -> Self {
245 7 : Self {
246 7 : shard_number: number,
247 7 : shard_count: count,
248 7 : }
249 7 : }
250 83 : pub fn unsharded() -> Self {
251 83 : Self {
252 83 : shard_number: ShardNumber(0),
253 83 : shard_count: ShardCount(0),
254 83 : }
255 83 : }
256 :
257 : /// The "unsharded" value is distinct from simply having a single shard: it represents
258 : /// a tenant which is not shard-aware at all, and whose storage paths will not include
259 : /// a shard suffix.
260 37292 : pub fn is_unsharded(&self) -> bool {
261 37292 : self.shard_number == ShardNumber(0) && self.shard_count == ShardCount(0)
262 37292 : }
263 :
264 : /// For use in constructing remote storage paths: concatenate this with a TenantId
265 : /// to get a fully qualified TenantShardId.
266 : ///
267 : /// Backward compat: this function returns an empty string if Self::is_unsharded, such
268 : /// that the legacy pre-sharding remote key format is preserved.
269 1048 : pub fn get_suffix(&self) -> String {
270 1048 : if self.is_unsharded() {
271 1042 : "".to_string()
272 : } else {
273 6 : format!("-{:02x}{:02x}", self.shard_number.0, self.shard_count.0)
274 : }
275 1048 : }
276 : }
277 :
278 : impl std::fmt::Display for ShardIndex {
279 1170 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
280 1170 : write!(f, "{:02x}{:02x}", self.shard_number.0, self.shard_count.0)
281 1170 : }
282 : }
283 :
284 : impl std::fmt::Debug for ShardIndex {
285 894 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
286 : // Debug is the same as Display: the compact hex representation
287 894 : write!(f, "{self}")
288 894 : }
289 : }
290 :
291 : impl std::str::FromStr for ShardIndex {
292 : type Err = hex::FromHexError;
293 :
294 1565 : fn from_str(s: &str) -> Result<Self, Self::Err> {
295 : // Expect format: 1 byte shard number, 1 byte shard count
296 1565 : if s.len() == 4 {
297 1565 : let bytes = s.as_bytes();
298 1565 : let mut shard_parts: [u8; 2] = [0u8; 2];
299 1565 : hex::decode_to_slice(bytes, &mut shard_parts)?;
300 1565 : Ok(Self {
301 1565 : shard_number: ShardNumber(shard_parts[0]),
302 1565 : shard_count: ShardCount(shard_parts[1]),
303 1565 : })
304 : } else {
305 0 : Err(hex::FromHexError::InvalidStringLength)
306 : }
307 1565 : }
308 : }
309 :
310 : impl From<[u8; 2]> for ShardIndex {
311 1 : fn from(b: [u8; 2]) -> Self {
312 1 : Self {
313 1 : shard_number: ShardNumber(b[0]),
314 1 : shard_count: ShardCount(b[1]),
315 1 : }
316 1 : }
317 : }
318 :
319 : impl Serialize for TenantShardId {
320 35 : fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
321 35 : where
322 35 : S: serde::Serializer,
323 : {
324 35 : if serializer.is_human_readable() {
325 31 : serializer.collect_str(self)
326 : } else {
327 : // Note: while human encoding of [`TenantShardId`] is backward and forward
328 : // compatible, this binary encoding is not.
329 4 : let mut packed: [u8; 18] = [0; 18];
330 4 : packed[0..16].clone_from_slice(&self.tenant_id.as_arr());
331 4 : packed[16] = self.shard_number.0;
332 4 : packed[17] = self.shard_count.0;
333 :
334 4 : packed.serialize(serializer)
335 : }
336 0 : }
337 : }
338 :
339 : impl<'de> Deserialize<'de> for TenantShardId {
340 6 : fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
341 6 : where
342 6 : D: serde::Deserializer<'de>,
343 : {
344 : struct IdVisitor {
345 : is_human_readable_deserializer: bool,
346 : }
347 :
348 : impl<'de> serde::de::Visitor<'de> for IdVisitor {
349 : type Value = TenantShardId;
350 :
351 0 : fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
352 0 : if self.is_human_readable_deserializer {
353 0 : formatter.write_str("value in form of hex string")
354 : } else {
355 0 : formatter.write_str("value in form of integer array([u8; 18])")
356 : }
357 0 : }
358 :
359 2 : fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
360 2 : where
361 2 : A: serde::de::SeqAccess<'de>,
362 : {
363 2 : let s = serde::de::value::SeqAccessDeserializer::new(seq);
364 2 : let id: [u8; 18] = Deserialize::deserialize(s)?;
365 2 : Ok(TenantShardId::from(id))
366 0 : }
367 :
368 4 : fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
369 4 : where
370 4 : E: serde::de::Error,
371 : {
372 4 : TenantShardId::from_str(v).map_err(E::custom)
373 0 : }
374 : }
375 :
376 6 : if deserializer.is_human_readable() {
377 4 : deserializer.deserialize_str(IdVisitor {
378 4 : is_human_readable_deserializer: true,
379 4 : })
380 : } else {
381 2 : deserializer.deserialize_tuple(
382 : 18,
383 2 : IdVisitor {
384 2 : is_human_readable_deserializer: false,
385 2 : },
386 : )
387 : }
388 0 : }
389 : }
390 :
391 : impl Serialize for ShardIndex {
392 16 : fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
393 16 : where
394 16 : S: serde::Serializer,
395 : {
396 16 : if serializer.is_human_readable() {
397 14 : serializer.collect_str(self)
398 : } else {
399 : // Binary encoding is not used in index_part.json, but is included in anticipation of
400 : // switching various structures (e.g. inter-process communication, remote metadata) to more
401 : // compact binary encodings in future.
402 2 : let mut packed: [u8; 2] = [0; 2];
403 2 : packed[0] = self.shard_number.0;
404 2 : packed[1] = self.shard_count.0;
405 2 : packed.serialize(serializer)
406 : }
407 0 : }
408 : }
409 :
410 : impl<'de> Deserialize<'de> for ShardIndex {
411 1565 : fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
412 1565 : where
413 1565 : D: serde::Deserializer<'de>,
414 : {
415 : struct IdVisitor {
416 : is_human_readable_deserializer: bool,
417 : }
418 :
419 : impl<'de> serde::de::Visitor<'de> for IdVisitor {
420 : type Value = ShardIndex;
421 :
422 0 : fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
423 0 : if self.is_human_readable_deserializer {
424 0 : formatter.write_str("value in form of hex string")
425 : } else {
426 0 : formatter.write_str("value in form of integer array([u8; 2])")
427 : }
428 0 : }
429 :
430 1 : fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
431 1 : where
432 1 : A: serde::de::SeqAccess<'de>,
433 : {
434 1 : let s = serde::de::value::SeqAccessDeserializer::new(seq);
435 1 : let id: [u8; 2] = Deserialize::deserialize(s)?;
436 1 : Ok(ShardIndex::from(id))
437 0 : }
438 :
439 1564 : fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
440 1564 : where
441 1564 : E: serde::de::Error,
442 : {
443 1564 : ShardIndex::from_str(v).map_err(E::custom)
444 0 : }
445 : }
446 :
447 1565 : if deserializer.is_human_readable() {
448 1564 : deserializer.deserialize_str(IdVisitor {
449 1564 : is_human_readable_deserializer: true,
450 1564 : })
451 : } else {
452 1 : deserializer.deserialize_tuple(
453 : 2,
454 1 : IdVisitor {
455 1 : is_human_readable_deserializer: false,
456 1 : },
457 : )
458 : }
459 0 : }
460 : }
|