1use crate::BucketId;
19use crate::metadata::{PhysicalTablePath, TableBucket};
20use std::fmt;
21use std::sync::Arc;
22
23#[allow(clippy::module_inception)]
24mod cluster;
25
26pub use cluster::Cluster;
27
28#[derive(Debug, Clone, PartialEq, Eq, Hash)]
29pub struct ServerNode {
30 id: i32,
31 uid: String,
32 host: String,
33 port: u32,
34 server_type: ServerType,
35}
36
37impl ServerNode {
38 pub fn new(id: i32, host: String, port: u32, server_type: ServerType) -> ServerNode {
39 ServerNode {
40 id,
41 uid: match server_type {
42 ServerType::CoordinatorServer => format!("cs-{id}"),
43 ServerType::TabletServer => format!("ts-{id}"),
44 },
45 host,
46 port,
47 server_type,
48 }
49 }
50
51 pub fn uid(&self) -> &str {
52 &self.uid
53 }
54
55 pub fn url(&self) -> String {
56 format!("{}:{}", self.host, self.port)
57 }
58
59 pub fn id(&self) -> i32 {
60 self.id
61 }
62
63 pub fn host(&self) -> &str {
64 &self.host
65 }
66
67 pub fn port(&self) -> u32 {
68 self.port
69 }
70
71 pub fn server_type(&self) -> &ServerType {
72 &self.server_type
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Hash)]
77pub enum ServerType {
78 TabletServer,
79 CoordinatorServer,
80}
81
82impl fmt::Display for ServerType {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 match self {
85 ServerType::TabletServer => write!(f, "TabletServer"),
86 ServerType::CoordinatorServer => write!(f, "CoordinatorServer"),
87 }
88 }
89}
90
91#[derive(Debug, Clone)]
92pub struct BucketLocation {
93 pub table_bucket: TableBucket,
94 leader: Option<ServerNode>,
95 physical_table_path: Arc<PhysicalTablePath>,
96}
97
98impl BucketLocation {
99 pub fn new(
100 table_bucket: TableBucket,
101 leader: Option<ServerNode>,
102 physical_table_path: Arc<PhysicalTablePath>,
103 ) -> BucketLocation {
104 BucketLocation {
105 table_bucket,
106 leader,
107 physical_table_path,
108 }
109 }
110
111 pub fn leader(&self) -> &Option<ServerNode> {
112 &self.leader
113 }
114
115 pub fn table_bucket(&self) -> &TableBucket {
116 &self.table_bucket
117 }
118
119 pub fn bucket_id(&self) -> BucketId {
120 self.table_bucket.bucket_id()
121 }
122
123 pub fn physical_table_path(&self) -> &Arc<PhysicalTablePath> {
124 &self.physical_table_path
125 }
126}