meta_srv/key/
flownode.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 lazy_static::lazy_static;
16use regex::Regex;
17use serde::{Deserialize, Serialize};
18
19pub(crate) const FLOWNODE_LEASE_PREFIX: &str = "__meta_flownode_lease";
20
21lazy_static! {
22    pub(crate) static ref FLOWNODE_LEASE_KEY_PATTERN: Regex =
23        Regex::new(&format!("^{FLOWNODE_LEASE_PREFIX}-([0-9]+)-([0-9]+)$")).unwrap();
24}
25
26#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
27pub struct FlownodeLeaseKey {
28    pub node_id: u64,
29}
30
31impl FlownodeLeaseKey {
32    pub fn prefix_key_by_cluster() -> Vec<u8> {
33        format!("{FLOWNODE_LEASE_PREFIX}-0-").into_bytes()
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_lease_key_round_trip() {
43        let key = FlownodeLeaseKey { node_id: 1 };
44
45        let key_bytes: Vec<u8> = key.clone().try_into().unwrap();
46        let new_key: FlownodeLeaseKey = key_bytes.try_into().unwrap();
47
48        assert_eq!(new_key, key);
49    }
50}