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
use crate::{Controller, Id};
use std::collections::{HashMap, VecDeque};
#[derive(Debug, PartialEq, Eq)]
pub enum TerminationProgress {
ShouldWaitMore,
SafeToStop,
}
pub struct Stage {
stage_id: String,
terminating: bool,
supervised: HashMap<Id, Controller>,
}
impl Stage {
fn new(stage_id: String) -> Self {
Self {
stage_id,
terminating: false,
supervised: HashMap::new(),
}
}
pub fn is_drained(&self) -> bool {
self.supervised.is_empty()
}
pub fn is_done(&self) -> bool {
self.is_terminating() && self.is_drained()
}
pub fn is_terminating(&self) -> bool {
self.terminating
}
pub fn insert(&mut self, mut controller: Controller) {
if self.terminating {
controller.shutdown();
} else {
self.supervised.insert(controller.id(), controller);
}
}
fn start_termination(&mut self) {
log::debug!("Terminating stage: {}", self.stage_id);
self.terminating = true;
for controller in self.supervised.values_mut() {
controller.shutdown();
}
}
fn absorb(&mut self, id: &Id) -> bool {
self.supervised.remove(id).is_some()
}
}
pub struct Terminator {
related_id: Id,
default_stage: Option<usize>,
stages: VecDeque<Stage>,
stop_signal_received: bool,
need_stop_signal: bool,
}
impl Terminator {
pub fn new(related_id: Id) -> Self {
Self {
related_id,
default_stage: None,
stages: VecDeque::new(),
stop_signal_received: false,
need_stop_signal: true,
}
}
pub fn stop_not_required(&mut self) {
self.need_stop_signal = false;
}
pub fn stage(&mut self) -> &mut Stage {
let idx = self.default_stage.expect("default stage not set");
self.stages
.get_mut(idx)
.expect("invalid index of the default stage")
}
pub fn new_stage(&mut self, stage_id: &str, default: bool) -> &mut Stage {
if default {
self.default_stage = Some(0);
} else {
if let Some(default_stage) = self.default_stage.as_mut() {
*default_stage += 1;
}
}
let full_id = format!("{}.{}", self.related_id, stage_id);
let term = Stage::new(full_id);
self.stages.push_front(term);
self.stages.get_mut(0).expect("stages list broken")
}
fn try_terminate_next(&mut self) {
for stage in self.stages.iter_mut() {
if stage.is_terminating() {
if stage.is_drained() {
} else {
break;
}
} else {
stage.start_termination();
}
}
}
pub fn track_child(&mut self, child: Id) {
let mut consumed = false;
for term in self.stages.iter_mut() {
if term.absorb(&child) {
consumed = true;
break;
}
}
if !consumed {
log::error!("Unknown child for stages chain: {:?}", child);
}
}
pub fn track_child_or_stop_signal(&mut self, child: Option<Id>) -> TerminationProgress {
match child {
None => {
self.stop_signal_received = true;
self.try_terminate_next();
}
Some(id) => {
self.track_child(id);
if self.stop_signal_received {
self.try_terminate_next();
}
}
}
let all_stages_drained = self.stages.iter().all(Stage::is_drained);
if all_stages_drained {
if self.need_stop_signal {
if self.stop_signal_received {
TerminationProgress::SafeToStop
} else {
TerminationProgress::ShouldWaitMore
}
} else {
TerminationProgress::SafeToStop
}
} else {
TerminationProgress::ShouldWaitMore
}
}
}