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
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use crate::sync::{Condvar, Mutex};
struct Inner {
flag: AtomicBool,
mutex: Mutex<bool>,
condvar: Condvar,
#[cfg(feature = "rpc")]
broadcast_txs: std::sync::Mutex<Vec<async_broadcast::Sender<()>>>,
}
/// Shared shutdown signal for coordinating graceful termination of background
/// workers (compactor, replication, RPC).
///
/// Cloning is cheap (`Arc` inside). Pass the same signal to every worker that
/// should stop together.
///
/// ```ignore
/// let signal = ShutdownSignal::new();
/// let compactor = Compactor::start_with_signal(compact_fn, interval, signal.clone());
///
/// // Later — trigger shutdown:
/// signal.shutdown(); // wakes all waiters immediately
/// ```
pub struct ShutdownSignal {
inner: Arc<Inner>,
}
impl Clone for ShutdownSignal {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl ShutdownSignal {
/// Create a new signal (not yet triggered).
pub fn new() -> Self {
Self {
inner: Arc::new(Inner {
flag: AtomicBool::new(false),
mutex: Mutex::new(false),
condvar: Condvar::new(),
#[cfg(feature = "rpc")]
broadcast_txs: std::sync::Mutex::new(Vec::new()),
}),
}
}
/// Trigger shutdown: sets the flag, wakes all threads blocked in
/// [`wait_timeout`](Self::wait_timeout), and broadcasts to async receivers.
pub fn shutdown(&self) {
self.inner.flag.store(true, Ordering::Release);
// Wake sync waiters.
#[cfg(feature = "parking_lot")]
{
let mut guard = self.inner.mutex.lock();
*guard = true;
self.inner.condvar.notify_all();
drop(guard);
}
#[cfg(not(feature = "parking_lot"))]
{
let mut guard = self.inner.mutex.lock().expect("shutdown mutex poisoned");
*guard = true;
self.inner.condvar.notify_all();
drop(guard);
}
// Wake async waiters (RPC accept loops / connections).
#[cfg(feature = "rpc")]
{
if let Ok(txs) = self.inner.broadcast_txs.lock() {
for tx in txs.iter() {
let _ = tx.try_broadcast(());
}
}
}
}
/// Non-blocking check.
#[inline]
pub fn is_shutdown(&self) -> bool {
self.inner.flag.load(Ordering::Acquire)
}
/// Sleep for at most `timeout`. Returns `true` if woken by shutdown
/// (i.e. shutdown was triggered), `false` on normal timeout.
pub fn wait_timeout(&self, timeout: Duration) -> bool {
if self.is_shutdown() {
return true;
}
#[cfg(feature = "parking_lot")]
{
let mut guard = self.inner.mutex.lock();
if *guard {
return true;
}
let result = self.inner.condvar.wait_for(&mut guard, timeout);
// parking_lot: wait_for returns WaitTimeoutResult
// If timed_out() is false, the condvar was notified.
*guard || !result.timed_out()
}
#[cfg(not(feature = "parking_lot"))]
{
let guard = self.inner.mutex.lock().expect("shutdown mutex poisoned");
if *guard {
return true;
}
let (guard, result) = self
.inner
.condvar
.wait_timeout(guard, timeout)
.expect("shutdown condvar poisoned");
*guard || !result.timed_out()
}
}
/// Raw flag reference — for code paths that poll `AtomicBool` directly
/// (e.g. inner replication loops).
#[inline]
pub fn as_flag(&self) -> &AtomicBool {
&self.inner.flag
}
/// Register an `async_broadcast` sender that will be triggered on
/// [`shutdown()`](Self::shutdown). Returns the corresponding receiver.
///
/// Used by the RPC accept loops so they can `select!` between accepting
/// connections and the shutdown signal.
#[cfg(feature = "rpc")]
pub fn subscribe_broadcast(&self) -> async_broadcast::Receiver<()> {
let (tx, rx) = async_broadcast::broadcast::<()>(1);
// If already shut down, fire immediately.
if self.is_shutdown() {
let _ = tx.try_broadcast(());
}
if let Ok(mut txs) = self.inner.broadcast_txs.lock() {
txs.push(tx);
}
rx
}
/// Install a Ctrl-C (SIGINT) handler that triggers this signal.
///
/// Safe to call multiple times — only the first call installs the handler.
#[cfg(feature = "rpc")]
pub fn install_ctrlc(&self) {
let signal = self.clone();
// ctrlc::set_handler returns Err if a handler is already installed.
let _ = ctrlc::set_handler(move || {
signal.shutdown();
});
}
}
impl Default for ShutdownSignal {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn not_shutdown_by_default() {
let s = ShutdownSignal::new();
assert!(!s.is_shutdown());
}
#[test]
fn shutdown_sets_flag() {
let s = ShutdownSignal::new();
s.shutdown();
assert!(s.is_shutdown());
}
#[test]
fn wait_timeout_returns_immediately_after_shutdown() {
let s = ShutdownSignal::new();
s.shutdown();
let start = std::time::Instant::now();
let woken = s.wait_timeout(Duration::from_secs(60));
assert!(woken);
assert!(start.elapsed() < Duration::from_millis(100));
}
#[test]
fn wait_timeout_wakes_on_shutdown() {
let s = ShutdownSignal::new();
let s2 = s.clone();
let handle = std::thread::spawn(move || {
let start = std::time::Instant::now();
let woken = s2.wait_timeout(Duration::from_secs(60));
(woken, start.elapsed())
});
std::thread::sleep(Duration::from_millis(50));
s.shutdown();
let (woken, elapsed) = handle.join().expect("thread panicked");
assert!(woken);
assert!(elapsed < Duration::from_secs(1));
}
#[test]
fn wait_timeout_times_out_normally() {
let s = ShutdownSignal::new();
let start = std::time::Instant::now();
let woken = s.wait_timeout(Duration::from_millis(50));
assert!(!woken);
assert!(start.elapsed() >= Duration::from_millis(40));
}
#[test]
fn clone_shares_state() {
let s1 = ShutdownSignal::new();
let s2 = s1.clone();
s1.shutdown();
assert!(s2.is_shutdown());
}
}