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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use super::cluster::Cluster;
use crate::app::cluster::ClusterEvent;
pub(crate) use crate::cql::PasswordAuth;
use async_trait::async_trait;
use maplit::hashmap;
use overclock::core::{
Actor,
ActorResult,
Rt,
ScopeId,
Service,
ServiceEvent,
ServiceStatus,
ShutdownEvent,
StreamExt,
SupHandle,
UnboundedChannel,
UnboundedHandle,
};
use serde::{
Deserialize,
Serialize,
};
use std::{
collections::{
HashMap,
HashSet,
},
net::SocketAddr,
};
pub type ScyllaHandle = UnboundedHandle<ScyllaEvent>;
pub type DatacenterName = String;
pub type KeyspaceName = String;
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct DatacenterConfig {
pub replication_factor: u8,
}
impl Default for KeyspaceConfig {
fn default() -> Self {
Self {
name: "permanode".to_string(),
data_centers: hashmap! {
"datacenter1".to_string() => DatacenterConfig {
replication_factor: 2,
},
},
}
}
}
impl std::hash::Hash for KeyspaceConfig {
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
self.name.hash(hasher)
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct KeyspaceConfig {
pub name: KeyspaceName,
pub data_centers: HashMap<DatacenterName, DatacenterConfig>,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Scylla {
pub local_dc: String,
pub nodes: HashSet<SocketAddr>,
pub keyspaces: HashSet<KeyspaceConfig>,
pub reporter_count: u8,
pub buffer_size: Option<usize>,
pub recv_buffer_size: Option<u32>,
pub send_buffer_size: Option<u32>,
pub authenticator: PasswordAuth,
}
impl Default for Scylla {
fn default() -> Self {
Self {
local_dc: "datacenter1".to_string(),
nodes: HashSet::new(),
keyspaces: HashSet::new(),
reporter_count: 2,
buffer_size: None,
recv_buffer_size: None,
send_buffer_size: None,
authenticator: PasswordAuth::default(),
}
}
}
impl Scylla {
pub fn new<T: Into<String>>(local_datacenter: T, reporter_count: u8, password_auth: PasswordAuth) -> Self {
Self {
local_dc: local_datacenter.into(),
nodes: HashSet::new(),
keyspaces: HashSet::new(),
reporter_count,
buffer_size: None,
recv_buffer_size: None,
send_buffer_size: None,
authenticator: password_auth,
}
}
pub fn insert_node(&mut self, node: SocketAddr) -> &mut Self {
self.nodes.insert(node);
self
}
pub fn remove_node(&mut self, node: &SocketAddr) -> &mut Self {
self.nodes.remove(&node);
self
}
pub fn insert_keyspace(&mut self, keyspace: KeyspaceConfig) -> &mut Self {
self.keyspaces.insert(keyspace);
self
}
pub fn remove_keyspace(&mut self, keyspace: &str) -> &mut Self {
self.keyspaces.retain(|k| k.name != keyspace);
self
}
}
pub enum ScyllaEvent {
GetClusterHandle(tokio::sync::oneshot::Sender<UnboundedHandle<ClusterEvent>>),
Microservice(ScopeId, Service),
UpdateState(Scylla),
Shutdown,
}
impl ShutdownEvent for ScyllaEvent {
fn shutdown_event() -> Self {
Self::Shutdown
}
}
impl<T: Actor<ScyllaHandle>> ServiceEvent<T> for ScyllaEvent {
fn eol_event(
scope: overclock::core::ScopeId,
service: Service,
_actor: T,
_r: overclock::core::ActorResult<()>,
) -> Self {
Self::Microservice(scope, service)
}
fn report_event(scope: overclock::core::ScopeId, service: Service) -> Self {
Self::Microservice(scope, service)
}
}
#[async_trait]
impl<S> Actor<S> for Scylla
where
S: SupHandle<Self>,
{
type Data = UnboundedHandle<ClusterEvent>;
type Channel = UnboundedChannel<ScyllaEvent>;
async fn init(&mut self, rt: &mut Rt<Self, S>) -> ActorResult<Self::Data> {
log::info!("Scylla is {}", rt.service().status());
rt.add_resource(self.clone()).await;
let cluster = Cluster::new();
let cluster_handle = rt.start("cluster".to_string(), cluster).await?;
if rt.microservices_all(|ms| ms.is_idle()) {
rt.update_status(ServiceStatus::Idle).await;
}
Ok(cluster_handle)
}
async fn run(&mut self, rt: &mut Rt<Self, S>, cluster_handle: Self::Data) -> ActorResult<()> {
log::info!("Scylla is {}", rt.service().status());
while let Some(event) = rt.inbox_mut().next().await {
match event {
ScyllaEvent::UpdateState(new_state) => {
*self = new_state;
rt.publish(self.clone()).await;
}
ScyllaEvent::GetClusterHandle(oneshot) => {
oneshot.send(cluster_handle.clone()).ok();
}
ScyllaEvent::Microservice(scope_id, service) => {
if service.is_stopped() {
rt.remove_microservice(scope_id);
} else {
rt.upsert_microservice(scope_id, service);
}
if !rt.service().is_stopping() {
if rt.microservices_all(|cluster| cluster.is_running()) {
if !rt.service().is_running() {
log::info!("Scylla is Running");
}
rt.update_status(ServiceStatus::Running).await;
} else if rt.microservices_all(|cluster| cluster.is_maintenance()) {
if !rt.service().is_maintenance() {
log::info!("Scylla is Maintenance");
}
rt.update_status(ServiceStatus::Maintenance).await;
} else if rt.microservices_all(|cluster| cluster.is_idle()) {
if !rt.service().is_idle() {
log::info!("Scylla is Idle");
}
rt.update_status(ServiceStatus::Idle).await;
} else if rt.microservices_all(|cluster| cluster.is_outage()) {
if !rt.service().is_outage() {
log::info!("Scylla is experiencing an Outage");
}
rt.update_status(ServiceStatus::Outage).await;
} else {
if !rt.service().is_degraded() {
log::info!("Scylla is Degraded");
}
rt.update_status(ServiceStatus::Degraded).await;
}
} else {
if !rt.service().is_stopping() {
log::info!("Scylla is Stopping");
}
rt.update_status(ServiceStatus::Stopping).await;
if rt.microservices_stopped() {
rt.inbox_mut().close();
}
}
}
ScyllaEvent::Shutdown => {
log::warn!("Scylla is Stopping");
rt.stop().await;
if rt.microservices_stopped() {
rt.inbox_mut().close();
}
}
}
}
log::info!("Scylla gracefully shutdown");
Ok(())
}
}
#[async_trait]
pub trait ScyllaHandleExt {
async fn cluster_handle(&self) -> Option<UnboundedHandle<ClusterEvent>>;
}
#[async_trait]
impl ScyllaHandleExt for UnboundedHandle<ScyllaEvent> {
async fn cluster_handle(&self) -> Option<UnboundedHandle<ClusterEvent>> {
let (tx, rx) = tokio::sync::oneshot::channel();
if let Ok(_) = self.send(ScyllaEvent::GetClusterHandle(tx)) {
if let Ok(handle) = rx.await {
Some(handle)
} else {
None
}
} else {
None
}
}
}
#[async_trait]
impl ScyllaHandleExt for overclock::core::Runtime<UnboundedHandle<ScyllaEvent>> {
async fn cluster_handle(&self) -> Option<UnboundedHandle<ClusterEvent>> {
self.handle().cluster_handle().await
}
}