integra8_scheduling 0.0.4-alpha

Component scheduling for integra8 test framework.
Documentation
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
use std::cmp;

pub trait TaskStream {
    type Payload;
    fn try_poll(&mut self) -> PollTaskResult<Self::Payload>;
    fn max_concurrency(&self) -> usize;
    fn max_concurrency_or_limit(&self, limit: usize) -> usize {
        if limit == 0 {
            // if max_concurrency == 0 then "auto" detect
            self.max_concurrency()
        } else {
            cmp::min(limit, self.max_concurrency())
        }
    }
    fn complete_task(&mut self, path: TaskNodePath) -> bool;
    fn len(&self) -> usize;
}

pub enum PollTaskResult<Payload> {
    Next(Payload, TaskNodePath),
    None,
    Busy,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TaskNodePath {
    elements: Vec<usize>,
}

impl TaskNodePath {
    pub fn new() -> Self {
        Self {
            elements: Vec::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.elements.is_empty()
    }

    pub fn pop(&mut self) -> Option<usize> {
        self.elements.pop()
    }

    pub fn append(mut self, idx: usize) -> Self {
        self.elements.push(idx);
        self
    }
}

#[derive(Debug)]
pub enum TaskStateMachineNode<Payload> {
    Single(TaskNode<Payload>),
    Serial(SerialTaskNode<Payload>),
    Parallel(ParallelTaskNode<Payload>),
}

impl<Payload> From<TaskNode<Payload>> for TaskStateMachineNode<Payload> {
    fn from(n: TaskNode<Payload>) -> TaskStateMachineNode<Payload> {
        TaskStateMachineNode::Single(n)
    }
}

impl<Payload> From<Payload> for TaskStateMachineNode<Payload> {
    fn from(p: Payload) -> TaskStateMachineNode<Payload> {
        TaskStateMachineNode::Single(TaskNode::new(p))
    }
}

impl<Payload> From<SerialTaskNode<Payload>> for TaskStateMachineNode<Payload> {
    fn from(n: SerialTaskNode<Payload>) -> TaskStateMachineNode<Payload> {
        TaskStateMachineNode::Serial(n)
    }
}

impl<Payload> From<ParallelTaskNode<Payload>> for TaskStateMachineNode<Payload> {
    fn from(n: ParallelTaskNode<Payload>) -> TaskStateMachineNode<Payload> {
        TaskStateMachineNode::Parallel(n)
    }
}

impl<Payload> TaskStream for TaskStateMachineNode<Payload> {
    type Payload = Payload;
    fn try_poll(&mut self) -> PollTaskResult<Self::Payload> {
        match self {
            Self::Single(node) => node.try_poll(),
            Self::Serial(node) => node.try_poll(),
            Self::Parallel(node) => node.try_poll(),
        }
    }

    fn max_concurrency(&self) -> usize {
        match self {
            Self::Single(node) => node.max_concurrency(),
            Self::Serial(node) => node.max_concurrency(),
            Self::Parallel(node) => node.max_concurrency(),
        }
    }

    fn complete_task(&mut self, path: TaskNodePath) -> bool {
        match self {
            Self::Single(node) => node.complete_task(path),
            Self::Serial(node) => node.complete_task(path),
            Self::Parallel(node) => node.complete_task(path),
        }
    }

    fn len(&self) -> usize {
        match self {
            Self::Single(_) => 1,
            Self::Serial(node) => node.len(),
            Self::Parallel(node) => node.len(),
        }
    }
}

// TaskNode

#[derive(Debug)]
pub enum TaskNode<Payload> {
    NotStarted(Option<Payload>),
    InProgress,
    IsDone,
}

impl<Payload> TaskNode<Payload> {
    pub fn new(payload: Payload) -> Self {
        Self::NotStarted(Some(payload))
    }

    pub fn start(&mut self) -> Payload {
        let payload = match self {
            Self::NotStarted(ref mut p) => std::mem::take(p),
            _ => None,
        };

        *self = Self::InProgress;
        payload.unwrap()
    }
}

impl<Payload> TaskStream for TaskNode<Payload> {
    type Payload = Payload;

    fn try_poll(&mut self) -> PollTaskResult<Self::Payload> {
        match self {
            Self::NotStarted(_) => {}
            Self::InProgress => {
                return PollTaskResult::Busy;
            }
            Self::IsDone => {
                return PollTaskResult::None;
            }
        };

        PollTaskResult::Next(self.start(), TaskNodePath::new())
    }

    fn complete_task(&mut self, path: TaskNodePath) -> bool {
        match self {
            Self::IsDone => {
                // Already done,
                false
            }
            _ => {
                *self = Self::IsDone;
                // The path should be empty at this point, otherwise
                // this path didn't resolve to a node correctly.
                // should probably raise some kind of error here
                path.is_empty()
            }
        }
    }

    fn max_concurrency(&self) -> usize {
        1
    }

    fn len(&self) -> usize {
        1
    }
}
// Parallel Task Node

#[derive(Debug)]
pub struct ParallelTaskNode<Payload> {
    nodes: Vec<TaskStateMachineNode<Payload>>,
    done: usize,
    total: usize,
}

impl<Payload> ParallelTaskNode<Payload> {
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            done: 0,
            total: 0,
        }
    }

    pub fn append(&mut self, node: impl Into<TaskStateMachineNode<Payload>>) {
        match node.into() {
            TaskStateMachineNode::Single(node) => self.append_task(node),
            TaskStateMachineNode::Serial(node) => self.append_serial(node),
            TaskStateMachineNode::Parallel(node) => self.append_parallel(node),
        }
    }

    pub fn append_all<I, IntoNode>(&mut self, iter: I)
    where
        I: IntoIterator<Item = IntoNode>,
        IntoNode: Into<TaskStateMachineNode<Payload>>,
    {
        for node in iter.into_iter() {
            self.append(node)
        }
    }

    pub fn append_task(&mut self, task: TaskNode<Payload>) {
        self.total = self.total.saturating_add(1);
        self.nodes.push(TaskStateMachineNode::Single(task));
    }

    pub fn append_parallel(&mut self, mut parallel: ParallelTaskNode<Payload>) {
        if !parallel.is_empty() {
            self.total = self.total.saturating_add(parallel.total);
            self.nodes.append(&mut parallel.nodes);
        }
    }

    pub fn append_serial(&mut self, serial: SerialTaskNode<Payload>) {
        if !serial.is_empty() {
            self.total = self.total.saturating_add(serial.total);
            self.nodes.push(TaskStateMachineNode::Serial(serial));
        }
    }

    pub fn is_empty(&self) -> bool {
        self.total == 0
    }
}

impl<Payload> TaskStream for ParallelTaskNode<Payload> {
    type Payload = Payload;

    fn try_poll(&mut self) -> PollTaskResult<Self::Payload> {
        if self.len() == 0 {
            return PollTaskResult::None;
        }

        let mut is_busy = false;

        self.nodes
            .iter_mut()
            .enumerate()
            .find_map(|(idx, node)| {
                match node.try_poll() {
                    PollTaskResult::None => None,
                    PollTaskResult::Busy => {
                        is_busy = true;
                        // keep looking
                        None
                    }
                    PollTaskResult::Next(payload, path) => {
                        // Break on first ready
                        Some(PollTaskResult::Next(payload, path.append(idx)))
                    }
                }
            })
            .unwrap_or_else(|| match is_busy {
                true => PollTaskResult::Busy,
                false => PollTaskResult::None,
            })
    }

    fn complete_task(&mut self, mut path: TaskNodePath) -> bool {
        match path.pop() {
            None => {
                // This should be some kind of error
                false
            }
            Some(idx) => {
                if let Some(n) = self.nodes.get_mut(idx) {
                    if n.complete_task(path) {
                        self.done += 1;
                        return true;
                    }
                }
                return false;
            }
        }
    }

    fn max_concurrency(&self) -> usize {
        self.nodes
            .iter()
            .fold(0, |total, x| x.max_concurrency() + total)
    }

    fn len(&self) -> usize {
        self.total.saturating_sub(self.done)
    }
}

// Serial Task Node

#[derive(Debug)]
pub struct SerialTaskNode<Payload> {
    nodes: Vec<TaskStateMachineNode<Payload>>,
    done: usize,
    total: usize,
    current_idx: usize,
}

impl<Payload> SerialTaskNode<Payload> {
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            done: 0,
            total: 0,
            current_idx: 0,
        }
    }

    pub fn enqueue(&mut self, node: impl Into<TaskStateMachineNode<Payload>>) {
        match node.into() {
            TaskStateMachineNode::Single(node) => self.enqueue_task(node),
            TaskStateMachineNode::Serial(node) => self.enqueue_serial(node),
            TaskStateMachineNode::Parallel(node) => self.enqueue_parallel(node),
        }
    }

    pub fn enqueue_all<I, IntoNode>(&mut self, iter: I)
    where
        I: IntoIterator<Item = IntoNode>,
        IntoNode: Into<TaskStateMachineNode<Payload>>,
    {
        for node in iter.into_iter() {
            self.enqueue(node)
        }
    }

    pub fn enqueue_task(&mut self, single: TaskNode<Payload>) {
        self.total = self.total.saturating_add(1);
        self.nodes.push(TaskStateMachineNode::Single(single));
    }

    pub fn enqueue_parallel(&mut self, parallel: ParallelTaskNode<Payload>) {
        if !parallel.is_empty() {
            self.total = self.total.saturating_add(parallel.total);
            self.nodes.push(TaskStateMachineNode::Parallel(parallel));
        }
    }

    pub fn enqueue_serial(&mut self, mut serial: SerialTaskNode<Payload>) {
        if !serial.is_empty() {
            self.total = self.total.saturating_add(serial.total);
            self.nodes.append(&mut serial.nodes);
        }
    }

    pub fn is_empty(&self) -> bool {
        self.total == 0
    }
}

impl<Payload> TaskStream for SerialTaskNode<Payload> {
    type Payload = Payload;

    fn try_poll(&mut self) -> PollTaskResult<Self::Payload> {
        loop {
            match self
                .nodes
                .get_mut(self.current_idx)
                .map(|node| node.try_poll())
            {
                Some(PollTaskResult::Next(payload, path)) => {
                    return PollTaskResult::Next(payload, path.append(self.current_idx))
                }
                Some(PollTaskResult::None) => {
                    // Move to the next node, and get the next task
                    self.current_idx += 1;
                }
                Some(PollTaskResult::Busy) => {
                    return PollTaskResult::Busy;
                }
                None => {
                    return PollTaskResult::None;
                }
            }
        }
    }

    fn complete_task(&mut self, mut path: TaskNodePath) -> bool {
        match path.pop() {
            None => {
                // This should be some kind of error
                false
            }
            Some(idx) => {
                if let Some(n) = self.nodes.get_mut(idx) {
                    if n.complete_task(path) {
                        self.done += 1;
                        return true;
                    }
                }
                return false;
            }
        }
    }

    fn max_concurrency(&self) -> usize {
        self.nodes.iter().fold(
            1, /*min value */
            |max, x| cmp::max(max, x.max_concurrency()),
        )
    }

    fn len(&self) -> usize {
        self.total.saturating_sub(self.done)
    }
}