sfo_pool/
classified_worker_pool.rs

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
use std::collections::{HashMap};
use std::hash::Hash;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex};
use notify_future::NotifyFuture;
use crate::PoolResult;

pub trait WorkerClassification: Send + Sync + 'static + Clone + Hash + Eq + PartialEq {

}

impl<T: Send + Sync + 'static + Clone + Hash + Eq + PartialEq> WorkerClassification for T {

}

#[async_trait::async_trait]
pub trait ClassifiedWorker<C: WorkerClassification>: Send + Sync + 'static {
    fn is_work(&self) -> bool;
    fn is_valid(&self, c: C) -> bool;
    fn classification(&self) -> C;
}

pub struct ClassifiedWorkerGuard<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> {
    pool_ref: ClassifiedWorkerPoolRef<C, W, F>,
    worker: Option<W>
}

impl<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> ClassifiedWorkerGuard<C, W, F> {
    fn new(worker: W, pool_ref: ClassifiedWorkerPoolRef<C, W, F>) -> Self {
        ClassifiedWorkerGuard {
            pool_ref,
            worker: Some(worker)
        }
    }
}

impl<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> Deref for ClassifiedWorkerGuard<C, W, F> {
    type Target = W;

    fn deref(&self) -> &Self::Target {
        self.worker.as_ref().unwrap()
    }
}

impl<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> DerefMut for ClassifiedWorkerGuard<C, W, F> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.worker.as_mut().unwrap()
    }
}

impl<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> Drop for ClassifiedWorkerGuard<C, W, F> {
    fn drop(&mut self) {
        if let Some(worker) = self.worker.take() {
            self.pool_ref.release(worker);
        }
    }
}

#[async_trait::async_trait]
pub trait ClassifiedWorkerFactory<C: WorkerClassification, W: ClassifiedWorker<C>>: Send + Sync + 'static {
    async fn create(&self, c: Option<C>) -> PoolResult<W>;
}

struct WaitingItem<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> {
    future: NotifyFuture<PoolResult<ClassifiedWorkerGuard<C, W, F>>>,
    condition: Option<C>,
}
struct WorkerPoolState<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> {
    current_count: u16,
    classified_count_map: HashMap<C, u16>,
    worker_list: Vec<W>,
    waiting_list: Vec<WaitingItem<C, W, F>>,
}

impl<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> WorkerPoolState<C, W, F> {
    fn inc_classified_count(&mut self, c: C) {
        let count = self.classified_count_map.entry(c).or_insert(0);
        *count += 1;
    }

    fn dec_classified_count(&mut self, c: C) {
        let count = self.classified_count_map.entry(c).or_insert(0);
        *count -= 1;
    }

    fn get_classified_count(&self, c: C) -> u16 {
        *self.classified_count_map.get(&c).unwrap_or(&0)
    }
}

pub struct ClassifiedWorkerPool<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> {
    factory: Arc<F>,
    max_count: u16,
    state: Mutex<WorkerPoolState<C, W, F>>,
}
pub type ClassifiedWorkerPoolRef<C, W, F> = Arc<ClassifiedWorkerPool<C, W, F>>;

impl<C: WorkerClassification, W: ClassifiedWorker<C>, F: ClassifiedWorkerFactory<C, W>> ClassifiedWorkerPool<C, W, F> {
    pub fn new(max_count: u16, factory: F) -> ClassifiedWorkerPoolRef<C, W, F> {
        Arc::new(ClassifiedWorkerPool {
            factory: Arc::new(factory),
            max_count,
            state: Mutex::new(WorkerPoolState {
                current_count: 0,
                classified_count_map: HashMap::new(),
                worker_list: Vec::with_capacity(max_count as usize),
                waiting_list: Vec::new(),
            }),
        })
    }

    pub async fn get_worker(self: &ClassifiedWorkerPoolRef<C, W, F>) -> PoolResult<ClassifiedWorkerGuard<C, W, F>> {
        let wait = {
            let mut state = self.state.lock().unwrap();

            while state.worker_list.len() > 0 {
                let worker = state.worker_list.pop().unwrap();
                if !worker.is_work() {
                    state.current_count -= 1;
                    state.dec_classified_count(worker.classification());
                    continue;
                }
                return Ok(ClassifiedWorkerGuard::new(worker, self.clone()));
            }

            if state.current_count < self.max_count {
                state.current_count += 1;
                None
            } else {
                let future = NotifyFuture::new();
                state.waiting_list.push(WaitingItem {
                    future: future.clone(),
                    condition: None,
                });
                Some(future)
            }
        };

        if let Some(wait) = wait {
            wait.await
        } else {
            let worker = match self.factory.create(None).await {
                Ok(worker) => worker,
                Err(err) => {
                    let mut state = self.state.lock().unwrap();
                    state.current_count -= 1;
                    return Err(err)
                },
            };
            let mut state = self.state.lock().unwrap();
            state.inc_classified_count(worker.classification());
            Ok(ClassifiedWorkerGuard::new(worker, self.clone()))
        }
    }

    pub async fn get_classified_worker(self: &ClassifiedWorkerPoolRef<C, W, F>, classification: C) -> PoolResult<ClassifiedWorkerGuard<C, W, F>> {
        let wait = {
            let mut state = self.state.lock().unwrap();

            let old_count = state.worker_list.len() as u16;
            let unwork_classification = state.worker_list.iter().filter(|worker| !worker.is_work()).map(|worker| worker.classification()).collect::<Vec<C>>();
            for classification in unwork_classification.iter() {
                state.dec_classified_count(classification.clone());
            }
            state.worker_list.retain(|worker| worker.is_work());
            state.current_count -= old_count - state.worker_list.len() as u16;
            for (index, worker) in state.worker_list.iter().enumerate() {
                if worker.is_valid(classification.clone()) {
                    let worker = state.worker_list.remove(index);
                    return Ok(ClassifiedWorkerGuard::new(worker, self.clone()));
                }
            }

            if state.current_count < self.max_count || state.get_classified_count(classification.clone()) == 0 {
                state.current_count += 1;
                None
            } else {
                let future = NotifyFuture::new();
                state.waiting_list.push(WaitingItem {
                    future: future.clone(),
                    condition: Some(classification.clone()),
                });
                Some(future)
            }
        };

        if let Some(wait) = wait {
            wait.await
        } else {
            let worker = match self.factory.create(Some(classification)).await {
                Ok(worker) => worker,
                Err(err) => {
                    let mut state = self.state.lock().unwrap();
                    state.current_count -= 1;
                    return Err(err)
                },
            };
            let mut state = self.state.lock().unwrap();
            state.inc_classified_count(worker.classification());
            Ok(ClassifiedWorkerGuard::new(worker, self.clone()))
        }
    }

    fn release(self: &ClassifiedWorkerPoolRef<C, W, F>, work: W) {
        if work.is_work() {
            let mut state = self.state.lock().unwrap();
            for (index, waiting) in state.waiting_list.iter().enumerate() {
                if waiting.condition.is_none() {
                    let waiting_item = state.waiting_list.remove(index);
                    waiting_item.future.set_complete(Ok(ClassifiedWorkerGuard::new(work, self.clone())));
                    return;
                } else {
                    if work.is_valid(waiting.condition.as_ref().unwrap().clone()) {
                        let waiting_item = state.waiting_list.remove(index);
                        waiting_item.future.set_complete(Ok(ClassifiedWorkerGuard::new(work, self.clone())));
                        return;
                    }
                }
            }
            state.worker_list.push(work);
        } else {
            let mut state = self.state.lock().unwrap();
            let classification = work.classification();
            for (index, waiting) in state.waiting_list.iter().enumerate() {
                if waiting.condition.is_none() {
                    let waiting_item = state.waiting_list.remove(index);
                    let factory = self.factory.clone();
                    let this = self.clone();
                    let classification = classification.clone();
                    tokio::spawn(async move {
                        match factory.create(Some(classification.clone())).await {
                            Ok(worker) => {
                                waiting_item.future.set_complete(Ok(ClassifiedWorkerGuard::new(worker, this)));
                            }
                            Err(err) => {
                                let mut state = this.state.lock().unwrap();
                                state.current_count -= 1;
                                state.dec_classified_count(classification);
                                waiting_item.future.set_complete(Err(err));
                            }
                        }
                    });
                    return;
                } else {
                    if classification == waiting.condition.as_ref().unwrap().clone() {
                        let waiting_item = state.waiting_list.remove(index);
                        let factory = self.factory.clone();
                        let this = self.clone();
                        let classification = classification.clone();
                        tokio::spawn(async move {
                            match factory.create(Some(classification.clone())).await {
                                Ok(worker) => {
                                    waiting_item.future.set_complete(Ok(ClassifiedWorkerGuard::new(worker, this)));
                                }
                                Err(err) => {
                                    let mut state = this.state.lock().unwrap();
                                    state.current_count -= 1;
                                    state.dec_classified_count(classification);
                                    waiting_item.future.set_complete(Err(err));
                                }
                            }
                        });
                        return;
                    }
                }
            }
            state.current_count -= 1;
            state.dec_classified_count(classification);
        }
    }
}

#[tokio::test]
async fn test_pool() {
    struct TestWorker {
        work: bool,
        classification: TestWorkerClassification,
    }

    #[derive(Clone, Debug, Eq, PartialEq, Hash)]
    enum TestWorkerClassification {
        A,
        B,
    }
    #[async_trait::async_trait]
    impl ClassifiedWorker<TestWorkerClassification> for TestWorker {
        fn is_work(&self) -> bool {
            self.work
        }

        fn is_valid(&self, c: TestWorkerClassification) -> bool {
            self.classification == c
        }

        fn classification(&self) -> TestWorkerClassification {
            self.classification.clone()
        }
    }

    struct TestWorkerFactory;

    #[async_trait::async_trait]
    impl ClassifiedWorkerFactory<TestWorkerClassification, TestWorker> for TestWorkerFactory {
        async fn create(&self, classification: Option<TestWorkerClassification>) -> PoolResult<TestWorker> {
            if let Some(classification) = classification {
                Ok(TestWorker { work: true, classification })
            } else {
                Ok(TestWorker { work: true, classification: TestWorkerClassification::A })
            }
        }
    }

    let pool = ClassifiedWorkerPool::new(2, TestWorkerFactory);
    let pool_ref = pool.clone();
    tokio::spawn(async move {
        let _worker = pool_ref.get_worker().await.unwrap();
        tokio::time::sleep(std::time::Duration::from_secs(5)).await;
    });
    let pool_ref = pool.clone();
    tokio::spawn(async move {
        let _worker = pool_ref.get_worker().await.unwrap();
        tokio::time::sleep(std::time::Duration::from_secs(10)).await;
    });

    let pool_ref = pool.clone();
    tokio::spawn(async move {
        let _worker = pool_ref.get_classified_worker(TestWorkerClassification::B).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_secs(6)).await;
    });

    let pool_ref = pool.clone();
    tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_secs(2)).await;

        let start = std::time::Instant::now();
        let _worker3 = pool_ref.get_classified_worker(TestWorkerClassification::B).await.unwrap();
        let end = std::time::Instant::now();
        let duration = end.duration_since(start);
        println!("classified duration {}", duration.as_millis());
        assert!(duration.as_millis() > 2000);
    });

    let pool_ref = pool.clone();
    tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_secs(2)).await;

        let start = std::time::Instant::now();
        let _worker3 = pool_ref.get_worker().await.unwrap();
        let end = std::time::Instant::now();
        let duration = end.duration_since(start);
        println!("classified duration2 {}", duration.as_millis());
        assert!(duration.as_millis() > 2000);
    });

    tokio::time::sleep(std::time::Duration::from_secs(15)).await;
}