-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathidentification.rs
More file actions
225 lines (197 loc) · 6.64 KB
/
Copy pathidentification.rs
File metadata and controls
225 lines (197 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2023 - 2026 Restate Software, Inc., Restate GmbH.
// All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
// re-export the AddressKind enum from the protobuf
pub use crate::protobuf::node_ctl_svc::ident_response::AddressKind as NetAddressKind;
use std::time::Duration;
use enumset::EnumSet;
use serde_with::serde_as;
use restate_types::config::Configuration;
use restate_types::health::{
AdminStatus, LogServerStatus, MetadataServerStatus, NodeStatus, WorkerStatus,
};
use restate_types::net::address::{
AdminPort, AdvertisedAddress, ControlPort, FabricPort, HttpIngressPort, ListenerPort,
PeerNetAddress,
};
use restate_types::net::listener::{AddressBook, Addresses};
use restate_types::nodes_config::Role;
use restate_types::{NodeId, Version};
use restate_util_time::FriendlyDuration;
use crate::task_center::TaskCenterMonitoring;
use crate::{Metadata, TaskCenter};
#[serde_as]
#[derive(serde::Serialize, prost_dto::IntoProst)]
#[prost(target = "crate::protobuf::node_ctl_svc::IdentResponse")]
pub struct Identification {
pub status: NodeStatus,
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub node_id: Option<NodeId>,
pub cluster_name: String,
#[into_prost(map = "enum_set_to_vec", map_by_ref)]
pub roles: EnumSet<Role>,
#[prost(name=age_s)]
#[into_prost(map=Duration::as_secs, map_by_ref)]
pub age: FriendlyDuration,
pub admin_status: AdminStatus,
pub worker_status: WorkerStatus,
pub log_server_status: LogServerStatus,
pub metadata_server_status: MetadataServerStatus,
pub nodes_config_version: Version,
pub logs_version: Version,
pub schema_version: Version,
pub partition_table_version: Version,
pub bound_addresses: Vec<NetAddress>,
pub advertised_addresses: Vec<NetAddress>,
}
#[derive(serde::Serialize, prost_dto::IntoProst)]
#[prost(target = "crate::protobuf::node_ctl_svc::ident_response::NetAddress")]
pub struct NetAddress {
pub name: String,
pub address: String,
pub kind: NetAddressKind,
}
fn enum_set_to_vec(roles: &EnumSet<Role>) -> Vec<String> {
roles.iter().map(|role| role.to_string()).collect()
}
impl Identification {
/// Gets the identification information for this node. It needs to be called from within the
/// [`TaskCenter`].
pub fn get() -> Self {
let configuration = Configuration::pinned();
let (
node_status,
admin_status,
worker_status,
metadata_server_status,
log_server_status,
bound_addresses,
advertised_addresses,
) = TaskCenter::with_current(|tc| {
let health = tc.health();
let address_book = tc.address_book();
let bound_addresses = collect_bound_addresses(address_book);
let advertised_addresses = collect_advertised_addresses(&configuration, address_book);
(
health.current_node_status(),
health.current_admin_status(),
health.current_worker_status(),
health.current_metadata_store_status(),
health.current_log_server_status(),
bound_addresses,
advertised_addresses,
)
});
let age = TaskCenter::with_current(|tc| tc.age());
let metadata = Metadata::current();
Identification {
status: node_status,
node_id: metadata.my_node_id_opt().map(Into::into),
roles: *configuration.roles(),
cluster_name: configuration.common.cluster_name().to_owned(),
age: age.into(),
admin_status,
worker_status,
metadata_server_status,
log_server_status,
nodes_config_version: metadata.nodes_config_version(),
logs_version: metadata.logs_version(),
schema_version: metadata.schema_version(),
partition_table_version: metadata.partition_table_version(),
bound_addresses,
advertised_addresses,
}
}
}
fn collect_bound_addresses(address_book: &AddressBook) -> Vec<NetAddress> {
let mut addresses = Vec::with_capacity(4);
push_addresses(
address_book.get_bound_addresses::<HttpIngressPort>(),
&mut addresses,
);
push_addresses(
address_book.get_bound_addresses::<AdminPort>(),
&mut addresses,
);
push_addresses(
address_book.get_bound_addresses::<FabricPort>(),
&mut addresses,
);
push_addresses(
address_book.get_bound_addresses::<ControlPort>(),
&mut addresses,
);
addresses
}
fn collect_advertised_addresses(
config: &Configuration,
address_book: &AddressBook,
) -> Vec<NetAddress> {
let mut addresses = Vec::with_capacity(3);
if config.has_role(Role::HttpIngress) {
push_advertised(
config.ingress.advertised_address(address_book),
&mut addresses,
);
}
if config.has_role(Role::Admin) {
push_advertised(
config.admin.advertised_address(address_book),
&mut addresses,
);
}
// fabric
push_advertised(
config
.common
.advertised_address(address_book, config.networking.tls.is_some()),
&mut addresses,
);
addresses
}
#[inline(always)]
fn push_addresses<P: ListenerPort + 'static>(
address: Option<Addresses<P>>,
buf: &mut Vec<NetAddress>,
) {
let Some(addresses) = address else {
return;
};
if let Some(tcp_bind_address) = addresses.tcp_bind_address() {
buf.push(NetAddress {
name: P::NAME.to_owned(),
address: tcp_bind_address.to_string(),
kind: NetAddressKind::Tcp,
});
}
if let Some(uds_path) = addresses.uds_path() {
buf.push(NetAddress {
name: P::NAME.to_owned(),
address: uds_path.display().to_string(),
kind: NetAddressKind::Unix,
});
}
}
#[inline(always)]
fn push_advertised<P: ListenerPort + 'static>(
address: AdvertisedAddress<P>,
buf: &mut Vec<NetAddress>,
) {
let Ok(address) = address.into_address() else {
return;
};
buf.push(NetAddress {
kind: match &address {
PeerNetAddress::Uds(..) => NetAddressKind::Unix,
PeerNetAddress::Http(..) => NetAddressKind::Http,
},
name: P::NAME.to_owned(),
address: address.to_string(),
})
}