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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use crate::broadcast::Sender;
use crate::context::BastionId;
use fxhash::FxHashMap;
use lever::table::lotable::LOTable;
use lightproc::recoverable_handle::RecoverableHandle;
use std::cmp::min;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
#[cfg(feature = "scaling")]
#[derive(Debug)]
pub struct OptimalSizeExploringResizer {
stats: Arc<AtomicU64>,
actor_stats: Arc<LOTable<BastionId, u32>>,
lower_bound: u64,
upper_bound: UpperBound,
upscale_strategy: UpscaleStrategy,
upscale_rate: f64,
downscale_threshold: f64,
downscale_rate: f64,
}
#[cfg(feature = "scaling")]
#[derive(Debug)]
pub(crate) struct ActorGroupStats {
actors_count: u32,
average_mailbox_size: u32,
}
#[cfg(feature = "scaling")]
#[derive(Debug, Clone)]
pub enum UpperBound {
Limit(u64),
Unlimited,
}
#[cfg(feature = "scaling")]
#[derive(Debug, Clone)]
pub enum UpscaleStrategy {
MailboxSizeThreshold(u32),
}
#[cfg(feature = "scaling")]
#[derive(Debug)]
pub(crate) enum ScalingRule {
Upscale(u64),
Downscale(Vec<BastionId>),
DoNothing,
}
#[cfg(feature = "scaling")]
impl OptimalSizeExploringResizer {
pub(crate) fn stats(&self) -> Arc<AtomicU64> {
self.stats.clone()
}
pub(crate) fn actor_stats(&self) -> Arc<LOTable<BastionId, u32>> {
self.actor_stats.clone()
}
pub fn with_lower_bound(mut self, lower_bound: u64) -> Self {
self.lower_bound = lower_bound;
self
}
pub fn with_upper_bound(mut self, upper_bound: UpperBound) -> Self {
self.upper_bound = upper_bound;
self
}
pub fn with_upscale_strategy(mut self, upscale_strategy: UpscaleStrategy) -> Self {
self.upscale_strategy = upscale_strategy;
self
}
pub fn with_upscale_rate(mut self, upscale_rate: f64) -> Self {
self.upscale_rate = upscale_rate;
self
}
pub fn with_downscale_threshold(mut self, downscale_threshold: f64) -> Self {
self.downscale_threshold = downscale_threshold;
self
}
pub fn with_downscale_rate(mut self, downscale_rate: f64) -> Self {
self.downscale_rate = downscale_rate;
self
}
pub(crate) async fn scale(
&self,
actors: &FxHashMap<BastionId, (Sender, RecoverableHandle<()>)>,
) -> ScalingRule {
let active_actors_count = actors.len() as u64;
if active_actors_count < self.lower_bound {
let additional_actors_count = self.lower_bound - active_actors_count;
return ScalingRule::Upscale(additional_actors_count);
}
let mut stats = ActorGroupStats::load(self.stats.clone());
if let Some(scaling_rule) = self.do_upscaling(&mut stats, actors) {
return scaling_rule;
}
if let Some(scaling_rule) = self.do_downscaling(&mut stats, actors) {
return scaling_rule;
}
ScalingRule::DoNothing
}
fn do_upscaling(
&self,
stats: &mut ActorGroupStats,
actors: &FxHashMap<BastionId, (Sender, RecoverableHandle<()>)>,
) -> Option<ScalingRule> {
match self.upscale_strategy {
UpscaleStrategy::MailboxSizeThreshold(threshold) => {
if stats.average_mailbox_size > threshold {
let count = (stats.actors_count as f64 * self.upscale_rate).ceil() as u64;
stats.average_mailbox_size = 0;
stats.store(self.stats.clone());
return Some(self.adjustment_upscaling(actors, count));
}
}
}
None
}
fn do_downscaling(
&self,
_stats: &mut ActorGroupStats,
actors: &FxHashMap<BastionId, (Sender, RecoverableHandle<()>)>,
) -> Option<ScalingRule> {
let mut actors_to_stop = Vec::new();
for (actor_id, (_, handle)) in actors {
let state = handle.state();
if state.is_closed() || state.is_completed() {
actors_to_stop.push(actor_id.clone())
}
}
if !actors_to_stop.is_empty() {
let active_actors_count = actors.len();
let freed_actors_limit =
(self.downscale_rate * active_actors_count as f64).ceil() as usize;
let mut freed_actors_desired = min(actors_to_stop.len(), freed_actors_limit);
let left_active_actors = active_actors_count - freed_actors_desired;
freed_actors_desired = match left_active_actors >= self.lower_bound as usize {
true => freed_actors_desired,
false => {
let excessive_actors_to_stop = self.lower_bound as usize - left_active_actors;
freed_actors_desired - excessive_actors_to_stop
}
};
return match freed_actors_desired {
0 => Some(ScalingRule::DoNothing),
_ => {
let freed_actors = actors_to_stop.drain(0..freed_actors_desired).collect();
Some(ScalingRule::Downscale(freed_actors))
}
};
}
None
}
fn adjustment_upscaling(
&self,
actors: &FxHashMap<BastionId, (Sender, RecoverableHandle<()>)>,
desired_upscale: u64,
) -> ScalingRule {
match self.upper_bound {
UpperBound::Limit(actors_limit) => {
let active_actors = actors.len() as u64;
let can_be_added_actors_max = actors_limit - active_actors;
let added_actors_limit = min(desired_upscale, can_be_added_actors_max);
match added_actors_limit {
0 => ScalingRule::DoNothing,
_ => ScalingRule::Upscale(added_actors_limit),
}
}
UpperBound::Unlimited => ScalingRule::Upscale(desired_upscale),
}
}
}
#[cfg(feature = "scaling")]
impl Default for OptimalSizeExploringResizer {
fn default() -> Self {
OptimalSizeExploringResizer {
stats: Arc::new(AtomicU64::new(0)),
actor_stats: Arc::new(LOTable::new()),
lower_bound: 1,
upper_bound: UpperBound::Limit(10),
upscale_strategy: UpscaleStrategy::MailboxSizeThreshold(3),
upscale_rate: 0.1,
downscale_threshold: 0.3,
downscale_rate: 0.1,
}
}
}
#[cfg(feature = "scaling")]
impl ActorGroupStats {
fn actors_count_mask() -> u64 {
0xFFFFFFFF_00000000
}
fn average_mailbox_size_mask() -> u64 {
0x00000000_FFFFFFFF
}
pub(crate) fn load(storage: Arc<AtomicU64>) -> Self {
let actors_count_mask = ActorGroupStats::actors_count_mask();
let average_mailbox_size_mask = ActorGroupStats::average_mailbox_size_mask();
let value = storage.load(Ordering::SeqCst);
ActorGroupStats {
actors_count: ((value & actors_count_mask) >> 32) as u32,
average_mailbox_size: (value & average_mailbox_size_mask) as u32,
}
}
pub(crate) fn store(&self, storage: Arc<AtomicU64>) {
let actors_count_mask = ActorGroupStats::actors_count_mask();
let average_mailbox_size_mask = ActorGroupStats::average_mailbox_size_mask();
let mut value: u64 = 0;
value |= (self.average_mailbox_size() as u64) & average_mailbox_size_mask;
value |= ((self.actors_count() as u64) << 32) & actors_count_mask;
storage.store(value, Ordering::SeqCst);
}
pub(crate) fn actors_count(&self) -> u32 {
self.actors_count
}
pub(crate) fn update_actors_count(&mut self, value: u32) {
self.actors_count = value
}
pub(crate) fn average_mailbox_size(&self) -> u32 {
self.average_mailbox_size
}
pub(crate) fn update_average_mailbox_size(&mut self, value: u32) {
let avg = (self.average_mailbox_size + value) as f32 / 2.0;
self.average_mailbox_size = avg.floor() as u32;
}
}
#[cfg(test)]
mod tests {
use crate::resizer::{ActorGroupStats, OptimalSizeExploringResizer};
#[test]
fn test_resizer_stores_empty_stats_by_default() {
let resizer = OptimalSizeExploringResizer::default();
let stats = ActorGroupStats::load(resizer.stats);
assert_eq!(stats.actors_count, 0);
assert_eq!(stats.average_mailbox_size, 0);
}
#[test]
fn test_resizer_returns_refreshed_stats_after_actors_count_update() {
let resizer = OptimalSizeExploringResizer::default();
let atomic_stats = resizer.stats();
let mut stats = ActorGroupStats::load(atomic_stats.clone());
stats.update_actors_count(10);
stats.store(atomic_stats);
let updated_stats = ActorGroupStats::load(resizer.stats);
assert_eq!(updated_stats.actors_count, 10);
assert_eq!(updated_stats.average_mailbox_size, 0);
}
#[test]
fn test_resizer_returns_refreshed_stats_after_avg_mailbox_size_update() {
let resizer = OptimalSizeExploringResizer::default();
let atomic_stats = resizer.stats();
let mut stats = ActorGroupStats::load(atomic_stats.clone());
stats.update_average_mailbox_size(100);
stats.store(atomic_stats);
let updated_stats = ActorGroupStats::load(resizer.stats);
assert_eq!(updated_stats.actors_count, 0);
assert_eq!(updated_stats.average_mailbox_size, 50);
}
#[test]
fn test_resizer_returns_refreshed_stats_after_actor_count_and_avg_mailbox_size_update() {
let resizer = OptimalSizeExploringResizer::default();
let atomic_stats = resizer.stats();
let mut stats = ActorGroupStats::load(atomic_stats.clone());
stats.update_actors_count(10);
stats.update_average_mailbox_size(100);
stats.store(atomic_stats);
let updated_stats = ActorGroupStats::load(resizer.stats);
assert_eq!(updated_stats.actors_count, 10);
assert_eq!(updated_stats.average_mailbox_size, 50);
}
}