Skip to main content

common_meta/key/
datanode_epoch.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Display;
16
17use serde::{Deserialize, Serialize};
18
19use crate::DatanodeId;
20use crate::key::DATANODE_EPOCH_KEY_PREFIX;
21
22/// The key of the highest `node_epoch` the metasrv leader has accepted from a datanode.
23///
24/// The layout: `__datanode_epoch/{node_id}`
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct DatanodeEpochKey {
27    pub node_id: DatanodeId,
28}
29
30impl DatanodeEpochKey {
31    pub fn new(node_id: DatanodeId) -> Self {
32        Self { node_id }
33    }
34
35    pub fn to_bytes(&self) -> Vec<u8> {
36        self.to_string().into_bytes()
37    }
38}
39
40impl Display for DatanodeEpochKey {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{}/{}", DATANODE_EPOCH_KEY_PREFIX, self.node_id)
43    }
44}
45
46/// The value of [DatanodeEpochKey]. It only grows.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
48pub struct DatanodeEpochValue {
49    pub node_epoch: u64,
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::key::MetadataValue;
56
57    #[test]
58    fn test_frozen_datanode_epoch_record() {
59        let value =
60            include_bytes!("../../../../../tests/compatibility/meta/datanode_epoch/value.json");
61        assert_eq!(DatanodeEpochKey::new(7).to_bytes(), b"__datanode_epoch/7");
62        let decoded = DatanodeEpochValue::try_from_raw_value(value).unwrap();
63        assert_eq!(decoded.node_epoch, 1761091200000);
64        assert_eq!(decoded.try_as_raw_value().unwrap(), value);
65    }
66}