BatchSQLJob.java
1.38 KB
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
package io.mycat.sqlengine;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import io.mycat.MycatServer;
public class BatchSQLJob {
private ConcurrentHashMap<Integer, SQLJob> runningJobs = new ConcurrentHashMap<Integer, SQLJob>();
private ConcurrentLinkedQueue<SQLJob> waitingJobs = new ConcurrentLinkedQueue<SQLJob>();
private volatile boolean noMoreJobInput = false;
public void addJob(SQLJob newJob, boolean parallExecute) {
if (parallExecute) {
runJob(newJob);
} else {
waitingJobs.offer(newJob);
if (runningJobs.isEmpty()) {
SQLJob job = waitingJobs.poll();
if (job != null) {
runJob(job);
}
}
}
}
public void setNoMoreJobInput(boolean noMoreJobInput) {
this.noMoreJobInput = noMoreJobInput;
}
private void runJob(SQLJob newJob) {
// EngineCtx.LOGGER.info("run job " + newJob);
runningJobs.put(newJob.getId(), newJob);
MycatServer.getInstance().getBusinessExecutor().execute(newJob);
}
public boolean jobFinished(SQLJob sqlJob) {
if (EngineCtx.LOGGER.isDebugEnabled()) {
EngineCtx.LOGGER.info("job finished " + sqlJob);
}
runningJobs.remove(sqlJob.getId());
SQLJob job = waitingJobs.poll();
if (job != null) {
runJob(job);
return false;
} else {
if (noMoreJobInput) {
return runningJobs.isEmpty() && waitingJobs.isEmpty();
} else {
return false;
}
}
}
}