Skip to content

Commit 9e03c71

Browse files
eventdlidongsjtu
authored andcommitted
Minor, use util class to hold static job function
1 parent b1cc0dd commit 9e03c71

File tree

3 files changed

+136
-101
lines changed

3 files changed

+136
-101
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ release.properties
1414

1515
#IDEA
1616
*.iml
17-
#.settings
17+
.settings
1818

1919
# External tool builders
2020
.externalToolBuilders/
@@ -88,4 +88,4 @@ build/commit_SHA1
8888
dist/
8989
tomcat/
9090
webapp/app/kylin-servlet.xml
91-
webapp/app/web.xml
91+
webapp/app/web.xml

server-base/src/main/java/org/apache/kylin/rest/service/JobService.java

+4-99
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@
3737
import org.apache.kylin.cube.model.CubeBuildTypeEnum;
3838
import org.apache.kylin.engine.EngineFactory;
3939
import org.apache.kylin.engine.mr.CubingJob;
40-
import org.apache.kylin.engine.mr.common.HadoopShellExecutable;
41-
import org.apache.kylin.engine.mr.common.MapReduceExecutable;
4240
import org.apache.kylin.engine.mr.steps.CubingExecutableUtil;
4341
import org.apache.kylin.job.JobInstance;
4442
import org.apache.kylin.job.Scheduler;
4543
import org.apache.kylin.job.SchedulerFactory;
46-
import org.apache.kylin.job.common.ShellExecutable;
4744
import org.apache.kylin.job.constant.JobStatusEnum;
48-
import org.apache.kylin.job.constant.JobStepStatusEnum;
4945
import org.apache.kylin.job.constant.JobTimeFilterEnum;
5046
import org.apache.kylin.job.engine.JobEngineConfig;
5147
import org.apache.kylin.job.exception.JobException;
@@ -59,6 +55,7 @@
5955
import org.apache.kylin.metadata.realization.RealizationStatusEnum;
6056
import org.apache.kylin.rest.constant.Constant;
6157
import org.apache.kylin.rest.exception.BadRequestException;
58+
import org.apache.kylin.rest.util.JobInfoConverter;
6259
import org.apache.kylin.source.ISource;
6360
import org.apache.kylin.source.SourceFactory;
6461
import org.apache.kylin.source.SourcePartition;
@@ -275,108 +272,16 @@ private JobInstance getSingleJobInstance(AbstractExecutable job) {
275272
result.setSubmitter(cubeJob.getSubmitter());
276273
result.setUuid(cubeJob.getId());
277274
result.setType(CubeBuildTypeEnum.BUILD);
278-
result.setStatus(parseToJobStatus(job.getStatus()));
275+
result.setStatus(JobInfoConverter.parseToJobStatus(job.getStatus()));
279276
result.setMrWaiting(cubeJob.getMapReduceWaitTime() / 1000);
280277
result.setDuration(cubeJob.getDuration() / 1000);
281278
for (int i = 0; i < cubeJob.getTasks().size(); ++i) {
282279
AbstractExecutable task = cubeJob.getTasks().get(i);
283-
result.addStep(parseToJobStep(task, i, getExecutableManager().getOutput(task.getId())));
280+
result.addStep(JobInfoConverter.parseToJobStep(task, i, getExecutableManager().getOutput(task.getId())));
284281
}
285282
return result;
286283
}
287284

288-
private JobInstance parseToJobInstance(AbstractExecutable job, Map<String, Output> outputs) {
289-
if (job == null) {
290-
return null;
291-
}
292-
Preconditions.checkState(job instanceof CubingJob, "illegal job type, id:" + job.getId());
293-
CubingJob cubeJob = (CubingJob) job;
294-
Output output = outputs.get(job.getId());
295-
final JobInstance result = new JobInstance();
296-
result.setName(job.getName());
297-
result.setRelatedCube(CubingExecutableUtil.getCubeName(cubeJob.getParams()));
298-
result.setRelatedSegment(CubingExecutableUtil.getSegmentId(cubeJob.getParams()));
299-
result.setLastModified(output.getLastModified());
300-
result.setSubmitter(cubeJob.getSubmitter());
301-
result.setUuid(cubeJob.getId());
302-
result.setType(CubeBuildTypeEnum.BUILD);
303-
result.setStatus(parseToJobStatus(output.getState()));
304-
result.setMrWaiting(AbstractExecutable.getExtraInfoAsLong(output, CubingJob.MAP_REDUCE_WAIT_TIME, 0L) / 1000);
305-
result.setExecStartTime(AbstractExecutable.getStartTime(output));
306-
result.setExecEndTime(AbstractExecutable.getEndTime(output));
307-
result.setDuration(AbstractExecutable.getDuration(result.getExecStartTime(), result.getExecEndTime()) / 1000);
308-
for (int i = 0; i < cubeJob.getTasks().size(); ++i) {
309-
AbstractExecutable task = cubeJob.getTasks().get(i);
310-
result.addStep(parseToJobStep(task, i, outputs.get(task.getId())));
311-
}
312-
return result;
313-
}
314-
315-
private JobInstance.JobStep parseToJobStep(AbstractExecutable task, int i, Output stepOutput) {
316-
Preconditions.checkNotNull(stepOutput);
317-
JobInstance.JobStep result = new JobInstance.JobStep();
318-
result.setId(task.getId());
319-
result.setName(task.getName());
320-
result.setSequenceID(i);
321-
result.setStatus(parseToJobStepStatus(stepOutput.getState()));
322-
for (Map.Entry<String, String> entry : stepOutput.getExtra().entrySet()) {
323-
if (entry.getKey() != null && entry.getValue() != null) {
324-
result.putInfo(entry.getKey(), entry.getValue());
325-
}
326-
}
327-
result.setExecStartTime(AbstractExecutable.getStartTime(stepOutput));
328-
result.setExecEndTime(AbstractExecutable.getEndTime(stepOutput));
329-
if (task instanceof ShellExecutable) {
330-
result.setExecCmd(((ShellExecutable) task).getCmd());
331-
}
332-
if (task instanceof MapReduceExecutable) {
333-
result.setExecCmd(((MapReduceExecutable) task).getMapReduceParams());
334-
result.setExecWaitTime(AbstractExecutable.getExtraInfoAsLong(stepOutput, MapReduceExecutable.MAP_REDUCE_WAIT_TIME, 0L) / 1000);
335-
}
336-
if (task instanceof HadoopShellExecutable) {
337-
result.setExecCmd(((HadoopShellExecutable) task).getJobParams());
338-
}
339-
return result;
340-
}
341-
342-
private JobStatusEnum parseToJobStatus(ExecutableState state) {
343-
switch (state) {
344-
case READY:
345-
return JobStatusEnum.PENDING;
346-
case RUNNING:
347-
return JobStatusEnum.RUNNING;
348-
case ERROR:
349-
return JobStatusEnum.ERROR;
350-
case DISCARDED:
351-
return JobStatusEnum.DISCARDED;
352-
case SUCCEED:
353-
return JobStatusEnum.FINISHED;
354-
case STOPPED:
355-
return JobStatusEnum.STOPPED;
356-
default:
357-
throw new RuntimeException("invalid state:" + state);
358-
}
359-
}
360-
361-
private JobStepStatusEnum parseToJobStepStatus(ExecutableState state) {
362-
switch (state) {
363-
case READY:
364-
return JobStepStatusEnum.PENDING;
365-
case RUNNING:
366-
return JobStepStatusEnum.RUNNING;
367-
case ERROR:
368-
return JobStepStatusEnum.ERROR;
369-
case DISCARDED:
370-
return JobStepStatusEnum.DISCARDED;
371-
case SUCCEED:
372-
return JobStepStatusEnum.FINISHED;
373-
case STOPPED:
374-
return JobStepStatusEnum.STOPPED;
375-
default:
376-
throw new RuntimeException("invalid state:" + state);
377-
}
378-
}
379-
380285
@PreAuthorize(Constant.ACCESS_HAS_ROLE_ADMIN + " or hasPermission(#job, 'ADMINISTRATION') or hasPermission(#job, 'OPERATION') or hasPermission(#job, 'MANAGEMENT')")
381286
public void resumeJob(JobInstance job) throws IOException, JobException {
382287
getExecutableManager().resumeJob(job.getId());
@@ -448,7 +353,7 @@ private List<JobInstance> searchJobs(final String cubeNameSubstring, final Strin
448353
return Lists.newArrayList(FluentIterable.from(searchCubingJobs(cubeNameSubstring, projectName, states, timeStartInMillis, timeEndInMillis, allOutputs, false)).transform(new Function<CubingJob, JobInstance>() {
449354
@Override
450355
public JobInstance apply(CubingJob cubingJob) {
451-
return parseToJobInstance(cubingJob, allOutputs);
356+
return JobInfoConverter.parseToJobInstance(cubingJob, allOutputs);
452357
}
453358
}));
454359
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.kylin.rest.util;
20+
21+
import java.util.Map;
22+
23+
import org.apache.kylin.cube.model.CubeBuildTypeEnum;
24+
import org.apache.kylin.engine.mr.CubingJob;
25+
import org.apache.kylin.engine.mr.common.HadoopShellExecutable;
26+
import org.apache.kylin.engine.mr.common.MapReduceExecutable;
27+
import org.apache.kylin.engine.mr.steps.CubingExecutableUtil;
28+
import org.apache.kylin.job.JobInstance;
29+
import org.apache.kylin.job.common.ShellExecutable;
30+
import org.apache.kylin.job.constant.JobStatusEnum;
31+
import org.apache.kylin.job.constant.JobStepStatusEnum;
32+
import org.apache.kylin.job.execution.AbstractExecutable;
33+
import org.apache.kylin.job.execution.ExecutableState;
34+
import org.apache.kylin.job.execution.Output;
35+
36+
import com.google.common.base.Preconditions;
37+
38+
public class JobInfoConverter {
39+
public static JobInstance parseToJobInstance(AbstractExecutable job, Map<String, Output> outputs) {
40+
if (job == null) {
41+
return null;
42+
}
43+
Preconditions.checkState(job instanceof CubingJob, "illegal job type, id:" + job.getId());
44+
CubingJob cubeJob = (CubingJob) job;
45+
Output output = outputs.get(job.getId());
46+
final JobInstance result = new JobInstance();
47+
result.setName(job.getName());
48+
result.setRelatedCube(CubingExecutableUtil.getCubeName(cubeJob.getParams()));
49+
result.setRelatedSegment(CubingExecutableUtil.getSegmentId(cubeJob.getParams()));
50+
result.setLastModified(output.getLastModified());
51+
result.setSubmitter(cubeJob.getSubmitter());
52+
result.setUuid(cubeJob.getId());
53+
result.setType(CubeBuildTypeEnum.BUILD);
54+
result.setStatus(parseToJobStatus(output.getState()));
55+
result.setMrWaiting(AbstractExecutable.getExtraInfoAsLong(output, CubingJob.MAP_REDUCE_WAIT_TIME, 0L) / 1000);
56+
result.setExecStartTime(AbstractExecutable.getStartTime(output));
57+
result.setExecEndTime(AbstractExecutable.getEndTime(output));
58+
result.setDuration(AbstractExecutable.getDuration(result.getExecStartTime(), result.getExecEndTime()) / 1000);
59+
for (int i = 0; i < cubeJob.getTasks().size(); ++i) {
60+
AbstractExecutable task = cubeJob.getTasks().get(i);
61+
result.addStep(parseToJobStep(task, i, outputs.get(task.getId())));
62+
}
63+
return result;
64+
}
65+
66+
public static JobInstance.JobStep parseToJobStep(AbstractExecutable task, int i, Output stepOutput) {
67+
Preconditions.checkNotNull(stepOutput);
68+
JobInstance.JobStep result = new JobInstance.JobStep();
69+
result.setId(task.getId());
70+
result.setName(task.getName());
71+
result.setSequenceID(i);
72+
result.setStatus(parseToJobStepStatus(stepOutput.getState()));
73+
for (Map.Entry<String, String> entry : stepOutput.getExtra().entrySet()) {
74+
if (entry.getKey() != null && entry.getValue() != null) {
75+
result.putInfo(entry.getKey(), entry.getValue());
76+
}
77+
}
78+
result.setExecStartTime(AbstractExecutable.getStartTime(stepOutput));
79+
result.setExecEndTime(AbstractExecutable.getEndTime(stepOutput));
80+
if (task instanceof ShellExecutable) {
81+
result.setExecCmd(((ShellExecutable) task).getCmd());
82+
}
83+
if (task instanceof MapReduceExecutable) {
84+
result.setExecCmd(((MapReduceExecutable) task).getMapReduceParams());
85+
result.setExecWaitTime(AbstractExecutable.getExtraInfoAsLong(stepOutput, MapReduceExecutable.MAP_REDUCE_WAIT_TIME, 0L) / 1000);
86+
}
87+
if (task instanceof HadoopShellExecutable) {
88+
result.setExecCmd(((HadoopShellExecutable) task).getJobParams());
89+
}
90+
return result;
91+
}
92+
93+
public static JobStatusEnum parseToJobStatus(ExecutableState state) {
94+
switch (state) {
95+
case READY:
96+
return JobStatusEnum.PENDING;
97+
case RUNNING:
98+
return JobStatusEnum.RUNNING;
99+
case ERROR:
100+
return JobStatusEnum.ERROR;
101+
case DISCARDED:
102+
return JobStatusEnum.DISCARDED;
103+
case SUCCEED:
104+
return JobStatusEnum.FINISHED;
105+
case STOPPED:
106+
return JobStatusEnum.STOPPED;
107+
default:
108+
throw new RuntimeException("invalid state:" + state);
109+
}
110+
}
111+
112+
public static JobStepStatusEnum parseToJobStepStatus(ExecutableState state) {
113+
switch (state) {
114+
case READY:
115+
return JobStepStatusEnum.PENDING;
116+
case RUNNING:
117+
return JobStepStatusEnum.RUNNING;
118+
case ERROR:
119+
return JobStepStatusEnum.ERROR;
120+
case DISCARDED:
121+
return JobStepStatusEnum.DISCARDED;
122+
case SUCCEED:
123+
return JobStepStatusEnum.FINISHED;
124+
case STOPPED:
125+
return JobStepStatusEnum.STOPPED;
126+
default:
127+
throw new RuntimeException("invalid state:" + state);
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)