-
Notifications
You must be signed in to change notification settings - Fork 52
/
Scheduler.java
96 lines (80 loc) · 2.51 KB
/
Scheduler.java
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
package cn.hashdata.bireme;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Future;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import cn.hashdata.bireme.pipeline.PipeLine;
import cn.hashdata.bireme.pipeline.PipeLine.PipeLineState;
/**
* {@code Scheduler} collects all {@code PipeLine}. Successively and constantly drive the
* {@code PipeLine}s to work.
*
* @author yuze
*
*/
public class Scheduler implements Callable<Long> {
public Logger logger = LogManager.getLogger("Bireme.Scheduler");
public Context cxt;
public CompletionService<PipeLine> cs;
public LinkedList<PipeLine> pipeLineQueue;
public int workingPipeLine;
public Scheduler(Context cxt) {
this.cxt = cxt;
this.cs = new ExecutorCompletionService<PipeLine>(cxt.pipeLinePool);
this.pipeLineQueue = new LinkedList<PipeLine>();
Iterator<PipeLine> iter = cxt.pipeLines.iterator();
while (iter.hasNext()) {
pipeLineQueue.add(iter.next());
}
workingPipeLine = 0;
}
@Override
public Long call() throws BiremeException, InterruptedException {
logger.info("Scheduler start working.");
PipeLine pipeLine = null;
while (!cxt.stop) {
// start up all normal pipeline
while (!pipeLineQueue.isEmpty() && !cxt.stop) {
pipeLine = pipeLineQueue.removeFirst();
switch (pipeLine.state) {
case NORMAL:
cs.submit(pipeLine);
workingPipeLine++;
break;
case ERROR:
pipeLine.state = PipeLineState.STOP;
break;
case STOP:
default:
}
}
// get result of all completed pipeline
if (workingPipeLine != 0) {
while (!cxt.stop) {
Future<PipeLine> result = cs.poll();
PipeLine complete = null;
if (result == null) {
break;
}
try {
complete = result.get();
} catch (ExecutionException e) {
logger.warn("Pipeline throw out exception. Message {}", e.getMessage());
throw new BiremeException("Schedule Exception", e.getCause());
}
pipeLineQueue.add(complete);
workingPipeLine--;
}
} else {
logger.info("All pipeline stop.");
break;
}
}
return 0L;
}
}