forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExportChecker.java
150 lines (129 loc) · 6.1 KB
/
ExportChecker.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
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
// This file is made available under Elastic License 2.0.
// This file is based on code available under the Apache license here:
// https://github.com/apache/incubator-doris/blob/master/fe/fe-core/src/main/java/org/apache/doris/load/ExportChecker.java
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
package com.starrocks.load;
import com.google.common.collect.Maps;
import com.starrocks.common.Config;
import com.starrocks.common.util.LeaderDaemon;
import com.starrocks.load.ExportJob.JobState;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.task.ExportExportingTask;
import com.starrocks.task.ExportPendingTask;
import com.starrocks.task.LeaderTask;
import com.starrocks.task.LeaderTaskExecutor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.Map;
public final class ExportChecker extends LeaderDaemon {
private static final Logger LOG = LogManager.getLogger(ExportChecker.class);
// checkers for running job state
private static Map<JobState, ExportChecker> checkers = Maps.newHashMap();
// executors for pending tasks
private static Map<JobState, LeaderTaskExecutor> executors = Maps.newHashMap();
private JobState jobState;
private static LeaderTaskExecutor exportingSubTaskExecutor;
private ExportChecker(JobState jobState, long intervalMs) {
super("export checker " + jobState.name().toLowerCase(), intervalMs);
this.jobState = jobState;
}
public static void init(long intervalMs) {
checkers.put(JobState.PENDING, new ExportChecker(JobState.PENDING, intervalMs));
checkers.put(JobState.EXPORTING, new ExportChecker(JobState.EXPORTING, intervalMs));
int poolSize = Config.export_running_job_num_limit == 0 ? 5 : Config.export_running_job_num_limit;
LeaderTaskExecutor pendingTaskExecutor = new LeaderTaskExecutor("export_pending_job", poolSize, true);
executors.put(JobState.PENDING, pendingTaskExecutor);
LeaderTaskExecutor exportingTaskExecutor = new LeaderTaskExecutor("export_exporting_job", poolSize, true);
executors.put(JobState.EXPORTING, exportingTaskExecutor);
// One export job will be split into multiple exporting sub tasks, the queue size is not determined, so set Integer.MAX_VALUE.
exportingSubTaskExecutor = new LeaderTaskExecutor("export_exporting_sub_task", Config.export_task_pool_size,
Integer.MAX_VALUE, true);
}
public static void startAll() {
for (ExportChecker exportChecker : checkers.values()) {
exportChecker.start();
}
for (LeaderTaskExecutor leaderTaskExecutor : executors.values()) {
leaderTaskExecutor.start();
}
exportingSubTaskExecutor.start();
}
public static LeaderTaskExecutor getExportingSubTaskExecutor() {
return exportingSubTaskExecutor;
}
@Override
protected void runAfterCatalogReady() {
LOG.debug("start check export jobs. job state: {}", jobState.name());
switch (jobState) {
case PENDING:
runPendingJobs();
break;
case EXPORTING:
runExportingJobs();
break;
default:
LOG.warn("wrong export job state: {}", jobState.name());
break;
}
}
private void runPendingJobs() {
ExportMgr exportMgr = GlobalStateMgr.getCurrentState().getExportMgr();
List<ExportJob> pendingJobs = exportMgr.getExportJobs(JobState.PENDING);
// check to limit running etl job num
int runningJobNumLimit = Config.export_running_job_num_limit;
if (runningJobNumLimit > 0 && !pendingJobs.isEmpty()) {
// pending executor running + exporting state
int runningJobNum = executors.get(JobState.PENDING).getTaskNum()
+ executors.get(JobState.EXPORTING).getTaskNum();
if (runningJobNum >= runningJobNumLimit) {
LOG.info("running export job num {} exceeds system limit {}", runningJobNum, runningJobNumLimit);
return;
}
int remain = runningJobNumLimit - runningJobNum;
if (pendingJobs.size() > remain) {
pendingJobs = pendingJobs.subList(0, remain);
}
}
LOG.debug("pending export job num: {}", pendingJobs.size());
for (ExportJob job : pendingJobs) {
try {
LeaderTask task = new ExportPendingTask(job);
if (executors.get(JobState.PENDING).submit(task)) {
LOG.info("run pending export job. job: {}", job);
}
} catch (Exception e) {
LOG.warn("run pending export job error", e);
}
}
}
private void runExportingJobs() {
List<ExportJob> jobs = GlobalStateMgr.getCurrentState().getExportMgr().getExportJobs(JobState.EXPORTING);
LOG.debug("exporting export job num: {}", jobs.size());
for (ExportJob job : jobs) {
try {
LeaderTask task = new ExportExportingTask(job);
if (executors.get(JobState.EXPORTING).submit(task)) {
LOG.info("run exporting export job. job: {}", job);
}
} catch (Exception e) {
LOG.warn("run export exporing job error", e);
}
}
}
}