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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright 2018-2022 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::thread;

trait FnBox {
    fn call_box(self: Box<Self>);
}

impl<F: FnOnce()> FnBox for F {
    fn call_box(self: Box<F>) {
        (*self)()
    }
}

type Job = Box<dyn FnBox + Send + 'static>;

fn new_job<F>(f: F) -> Job
where
    F: FnBox + Send + 'static,
{
    Box::new(f)
}

enum Message {
    NewJob(Job),
    Terminate,
}

#[derive(Debug)]
pub struct ThreadPoolBuildError(pub String);

impl std::error::Error for ThreadPoolBuildError {}

impl std::fmt::Display for ThreadPoolBuildError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

#[derive(Default)]
pub struct ThreadPoolBuilder {
    size: Option<usize>,
    prefix: Option<String>,
}

impl ThreadPoolBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_size(mut self, size: usize) -> Self {
        self.size = Some(size);
        self
    }

    pub fn with_prefix(mut self, prefix: String) -> Self {
        self.prefix = Some(prefix);
        self
    }

    pub fn build(self) -> Result<ThreadPool, ThreadPoolBuildError> {
        let size = self
            .size
            .ok_or_else(|| ThreadPoolBuildError("Must configure thread pool size".into()))
            .and_then(|size| {
                if size == 0 {
                    Err(ThreadPoolBuildError(
                        "Must configure more than 0 threads".into(),
                    ))
                } else {
                    Ok(size)
                }
            })?;

        let prefix = self.prefix.unwrap_or_else(|| "ThreadPool".into());

        let (sender, receiver) = mpsc::channel();
        let (supervisor_tx, supervisor_rx) = mpsc::channel();

        let receiver = Arc::new(Mutex::new(receiver));

        let mut workers = Vec::with_capacity(size);
        for id in 0..size {
            workers.push(Worker::new(
                &prefix,
                id,
                receiver.clone(),
                supervisor_tx.clone(),
            )?);
        }
        let workers = Arc::new(Mutex::new(workers));

        let supervisor = Supervisor::new(
            prefix.clone(),
            workers.clone(),
            supervisor_rx,
            supervisor_tx.clone(),
            receiver,
        );
        let supervisor_thread = thread::Builder::new()
            .name(format!("{}-Supervisor", prefix))
            .spawn(move || supervisor.run())
            .map_err(|err| {
                ThreadPoolBuildError(format!("Unable to spawn supervisor thread: {}", err))
            })?;

        Ok(ThreadPool {
            workers,
            sender,
            supervisor_thread,
            supervisor_tx,
        })
    }
}

pub struct ThreadPool {
    workers: Arc<Mutex<Vec<Worker>>>,
    sender: mpsc::Sender<Message>,
    supervisor_thread: thread::JoinHandle<()>,
    supervisor_tx: mpsc::Sender<SupervisorSignal>,
}

impl ThreadPool {
    pub fn executor(&self) -> JobExecutor {
        JobExecutor {
            sender: self.sender.clone(),
        }
    }

    pub fn shutdown_signaler(&self) -> ShutdownSignaler {
        let worker_count = match self.workers.lock() {
            Ok(workers) => workers.len(),
            Err(err) => {
                warn!("Attempting to recover from a poisoned lock while joining",);
                err.into_inner().len()
            }
        };
        ShutdownSignaler {
            worker_count,
            sender: self.sender.clone(),
            supervisor_tx: self.supervisor_tx.clone(),
        }
    }

    pub fn join_all(self) {
        if let Err(_err) = self.supervisor_thread.join() {
            warn!("failed to cleanly join supervisor thread");
        }
        let mut workers = match self.workers.lock() {
            Ok(workers) => workers,
            Err(err) => {
                warn!("Attempting to recover from a poisoned lock while joining",);
                err.into_inner()
            }
        };
        for worker in workers.drain(..) {
            debug!("Shutting down worker {}", worker.id);
            if let Err(_err) = worker.thread.join() {
                warn!("Failed to cleanly join worker thread {}", worker.id);
            }
        }
    }
}

pub struct ShutdownSignaler {
    worker_count: usize,
    sender: mpsc::Sender<Message>,
    supervisor_tx: mpsc::Sender<SupervisorSignal>,
}

impl ShutdownSignaler {
    pub fn shutdown(&self) {
        if let Err(_err) = self.supervisor_tx.send(SupervisorSignal::Shutdown) {
            // ignore a dropped receiver
        }
        // Terminate all
        for _ in 0..self.worker_count {
            if let Err(_err) = self.sender.send(Message::Terminate) {
                // ignore a dropped receiver
            }
        }
    }
}

#[derive(Clone)]
pub struct JobExecutor {
    sender: mpsc::Sender<Message>,
}

impl JobExecutor {
    pub fn execute<F>(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let job = new_job(f);
        if self.sender.send(Message::NewJob(job)).is_err() {
            // ignore the dropped receiver
        }
    }
}

struct Worker {
    id: usize,
    thread: thread::JoinHandle<()>,
}

impl Worker {
    fn new(
        prefix: &str,
        id: usize,
        receiver: Arc<Mutex<mpsc::Receiver<Message>>>,
        supervisor_tx: mpsc::Sender<SupervisorSignal>,
    ) -> Result<Worker, ThreadPoolBuildError> {
        let thread = thread::Builder::new()
            .name(format!("{}-{}", prefix, id))
            .spawn(move || {
                // we just have to hold on to this until it is dropped
                let _supervisor = PanicMonitor { id, supervisor_tx };
                loop {
                    let msg = {
                        let receiver = match receiver.lock() {
                            Ok(recv) => recv,
                            Err(err) => {
                                warn!(
                                    "Attempting to recover from a poisoned lock in worker {}",
                                    id
                                );
                                err.into_inner()
                            }
                        };

                        match receiver.recv() {
                            Ok(msg) => msg,
                            Err(_) => break,
                        }
                    };
                    match msg {
                        Message::NewJob(job) => {
                            trace!("Worker {} received job; executing.", id);
                            job.call_box();
                        }
                        Message::Terminate => {
                            debug!("Worker {} received terminate cmd.", id);
                            break;
                        }
                    }
                }
            })
            .map_err(|err| {
                ThreadPoolBuildError(format!("Unable to spawn worker thread: {}", err))
            })?;

        Ok(Worker { id, thread })
    }
}

enum SupervisorSignal {
    Restart(usize),
    Shutdown,
}

struct Supervisor {
    prefix: String,
    workers: Arc<Mutex<Vec<Worker>>>,
    supervisor_rx: mpsc::Receiver<SupervisorSignal>,
    supervisor_tx: mpsc::Sender<SupervisorSignal>,
    job_receiver: Arc<Mutex<mpsc::Receiver<Message>>>,
}

impl Supervisor {
    fn new(
        prefix: String,
        workers: Arc<Mutex<Vec<Worker>>>,
        supervisor_rx: mpsc::Receiver<SupervisorSignal>,
        supervisor_tx: mpsc::Sender<SupervisorSignal>,
        job_receiver: Arc<Mutex<mpsc::Receiver<Message>>>,
    ) -> Self {
        Self {
            prefix,
            workers,
            supervisor_rx,
            supervisor_tx,
            job_receiver,
        }
    }

    fn run(&self) {
        while let Ok(report) = self.supervisor_rx.recv() {
            match report {
                SupervisorSignal::Restart(id) => {
                    debug!("Replacing {}-{}", &self.prefix, id);

                    let mut workers = match self.workers.lock() {
                        Ok(workers) => workers,
                        Err(err) => {
                            debug!("Recovering from a poisoned lock in supervisor thread");
                            err.into_inner()
                        }
                    };

                    let old_worker = match workers.get_mut(id) {
                        Some(worker) => worker,
                        // if this is None, the workers have been drained during the shutdown join,
                        // and there's no entry for it in the list. The supervisor probably hasn't
                        // received its shutdown message yet.
                        None => continue,
                    };

                    match Worker::new(
                        &self.prefix,
                        id,
                        self.job_receiver.clone(),
                        self.supervisor_tx.clone(),
                    ) {
                        Ok(mut worker) => {
                            std::mem::swap(old_worker, &mut worker);
                            // join out the old worker:
                            if let Err(_err) = worker.thread.join() {
                                // as we know the thread panicked, we can ignore this error, which
                                // we can't log as debug anyway, due to its type.
                            } else {
                                // as we know the thread panicked, a Result::Ok variant should not
                                // be possible
                                unreachable!()
                            }
                        }
                        Err(err) => error!("Unable to restart {}-{}: {}", &self.prefix, id, err),
                    }
                }
                SupervisorSignal::Shutdown => break,
            }
        }
    }
}

struct PanicMonitor {
    id: usize,
    supervisor_tx: mpsc::Sender<SupervisorSignal>,
}

impl Drop for PanicMonitor {
    fn drop(&mut self) {
        if thread::panicking()
            && self
                .supervisor_tx
                .send(SupervisorSignal::Restart(self.id))
                .is_err()
        {
            error!(
                "Unable to notify supervisor thread of Worker {} termination due to a panic",
                self.id
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::sync::{mpsc::channel, Arc, Barrier};
    use std::thread;

    /// Test that all threads in the pool are used by creating MAX_THREADS jobs, each that sleeps a
    /// descending number of milliseconds such that the pool will be saturated.
    #[test]
    fn test_job() -> Result<(), Box<dyn std::error::Error>> {
        let max_threads = 4;
        let thread_pool = ThreadPoolBuilder::new().with_size(max_threads).build()?;
        let (tx, rx) = channel();

        let barrier = Arc::new(Barrier::new(max_threads));
        for _ in 0..max_threads {
            let job_tx = tx.clone();
            let barrier = Arc::clone(&barrier);
            thread_pool.executor().execute(move || {
                job_tx
                    .send(
                        thread::current()
                            .name()
                            .expect("worker thread was not named")
                            .to_string(),
                    )
                    .expect("Unable to send result");

                // ensures that every worker is in use by the time the last job is run
                barrier.wait();
            });
        }

        // Drop the sender so that the receiver will close when the last job is done.
        drop(tx);

        let mut results: Vec<_> = rx.iter().collect();

        results.sort();

        assert_eq!(
            vec![
                "ThreadPool-0".to_string(),
                "ThreadPool-1".to_string(),
                "ThreadPool-2".to_string(),
                "ThreadPool-3".to_string()
            ],
            results
        );

        thread_pool.shutdown_signaler().shutdown();

        thread_pool.join_all();

        Ok(())
    }

    /// Test that the pool can recover workers when their jobs panic.  Submit two jobs that panic,
    /// and then submit MAX_THREADS jobs, each that sleep a descending number of milliseconds such
    /// that the pool will be saturated. Verify that there are still 4 threads in play.
    #[test]
    fn test_panic_recovery() -> Result<(), Box<dyn std::error::Error>> {
        let max_threads = 4;
        let thread_pool = ThreadPoolBuilder::new().with_size(max_threads).build()?;

        let executor = thread_pool.executor();

        executor.execute(|| panic!("first panicking!"));
        executor.execute(|| panic!("second panicking!"));

        // verify that we still have `max_threads`
        let (tx, rx) = channel();
        let barrier = Arc::new(Barrier::new(max_threads));
        for _ in 0..max_threads {
            let job_tx = tx.clone();
            let barrier = Arc::clone(&barrier);
            executor.execute(move || {
                job_tx
                    .send(
                        thread::current()
                            .name()
                            .expect("worker thread was not named")
                            .to_string(),
                    )
                    .expect("Unable to send result");

                // ensures that every worker is in use by the time the last job is run
                barrier.wait();
            });
        }
        // Drop the sender so that the receiver will close when the last job is done.
        drop(tx);

        let mut results: Vec<_> = rx.iter().collect();

        results.sort();

        assert_eq!(
            vec![
                "ThreadPool-0".to_string(),
                "ThreadPool-1".to_string(),
                "ThreadPool-2".to_string(),
                "ThreadPool-3".to_string()
            ],
            results
        );

        thread_pool.shutdown_signaler().shutdown();

        thread_pool.join_all();

        Ok(())
    }
}