-
Notifications
You must be signed in to change notification settings - Fork 160
/
JbatchDriver.java
364 lines (312 loc) · 17.5 KB
/
JbatchDriver.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
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
/*******************************************************************************
* Copyright (c) 2018, 2019 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.job.engine.jbatch.driver;
import com.google.common.collect.Lists;
import com.ibm.jbatch.container.jsl.ExecutionElement;
import com.ibm.jbatch.container.jsl.ModelSerializerFactory;
import com.ibm.jbatch.container.servicesmanager.ServicesManagerImpl;
import com.ibm.jbatch.jsl.model.JSLJob;
import com.ibm.jbatch.jsl.model.Step;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.job.engine.JobStartOptions;
import org.eclipse.kapua.job.engine.commons.wrappers.JobContextPropertyNames;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.CannotBuildJobDefDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.CannotCleanJobDefFileDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.CannotCreateTmpDirDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.CannotWriteJobDefFileDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.CleanJobDataDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.ExecutionNotFoundDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.ExecutionNotRunningDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.JbatchDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.JobExecutionIsRunningDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.exception.JobStartingDriverException;
import org.eclipse.kapua.job.engine.jbatch.driver.utils.JobDefinitionBuildUtils;
import org.eclipse.kapua.job.engine.jbatch.persistence.KapuaJDBCPersistenceManagerImpl;
import org.eclipse.kapua.job.engine.jbatch.setting.JobEngineSettingKeys;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.job.Job;
import org.eclipse.kapua.service.job.execution.JobExecutionService;
import org.eclipse.kapua.service.job.step.JobStep;
import org.eclipse.kapua.service.job.step.JobStepAttributes;
import org.eclipse.kapua.service.job.step.JobStepFactory;
import org.eclipse.kapua.service.job.step.JobStepListResult;
import org.eclipse.kapua.service.job.step.JobStepQuery;
import org.eclipse.kapua.service.job.step.JobStepService;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinition;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.batch.operations.JobExecutionNotRunningException;
import javax.batch.operations.JobOperator;
import javax.batch.operations.JobSecurityException;
import javax.batch.operations.NoSuchJobException;
import javax.batch.operations.NoSuchJobExecutionException;
import javax.batch.operations.NoSuchJobInstanceException;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.JobExecution;
import javax.batch.runtime.JobInstance;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* Driver class for Java Batch API
*
* @since 1.0.0
*/
public class JbatchDriver {
private static final Logger LOG = LoggerFactory.getLogger(JbatchDriver.class);
private static final String JBATCH_EXECUTION_ID = "JBATCH_EXECUTION_ID";
private static final JobOperator JOB_OPERATOR = BatchRuntime.getJobOperator();
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final JobExecutionService JOB_EXECUTION_SERVICE = LOCATOR.getService(JobExecutionService.class);
private static final JobStepService JOB_STEP_SERVICE = LOCATOR.getService(JobStepService.class);
private static final JobStepFactory JOB_STEP_FACTORY = LOCATOR.getFactory(JobStepFactory.class);
private static final JobStepDefinitionService STEP_DEFINITION_SERVICE = LOCATOR.getService(JobStepDefinitionService.class);
private JbatchDriver() {
}
/**
* Builds the jBatch job name from the {@link Job#getScopeId()} and the {@link Job#getId()}.
* <p>
* Format is: job-{scopeIdShort}-{jobIdShort}
*
* @param scopeId The scopeId of the {@link Job}
* @param jobId The id of the {@link Job}
* @return The jBatch {@link Job} name
* @since 1.0.0
*/
public static String getJbatchJobName(@NotNull KapuaId scopeId, @NotNull KapuaId jobId) {
return String.format("job-%s-%s", scopeId.toCompactId(), jobId.toCompactId());
}
/**
* Starts a jBatch job with data sourced from the Kapua {@link Job} definition.
* <p>
* It builds the XML jBatch job definition using the {@link JSLJob} model definition.
* The generated XML is store in the {@link SystemUtils#getJavaIoTmpDir()} since the default configuration of jBatch requires a path name to start the jBatch job
*
* @param scopeId The scopeId of the {@link Job}
* @param jobId The id of the {@link Job}
* @param jobStartOptions The {@link JobStartOptions} for this start {@link org.eclipse.kapua.service.job.Job} request.
* @throws CannotBuildJobDefDriverException if the creation of the {@link JSLJob} fails
* @throws CannotCreateTmpDirDriverException if the temp directory for storing the XML job definition file cannot be created
* @throws CannotCleanJobDefFileDriverException if the XML job definition file cannot be deleted, when existing
* @throws CannotWriteJobDefFileDriverException if the XML job definition file cannot be created and written in the tmp directory
* @throws JobExecutionIsRunningDriverException if the jBatch job has another {@link JobExecution} running
* @throws JobStartingDriverException if invoking {@link JobOperator#start(String, Properties)} throws an {@link Exception}
* @since 1.0.0
*/
public static void startJob(@NotNull KapuaId scopeId, @NotNull KapuaId jobId, @NotNull JobStartOptions jobStartOptions)
throws JbatchDriverException {
String jobXmlDefinition;
String jobName = JbatchDriver.getJbatchJobName(scopeId, jobId);
try {
JobStepQuery query = JOB_STEP_FACTORY.newQuery(scopeId);
query.setPredicate(query.attributePredicate(JobStepAttributes.JOB_ID, jobId));
JobStepListResult jobSteps = JOB_STEP_SERVICE.query(query);
jobSteps.sort(Comparator.comparing(JobStep::getStepIndex));
List<ExecutionElement> jslExecutionElements = new ArrayList<>();
Iterator<JobStep> jobStepIterator = jobSteps.getItems().iterator();
while (jobStepIterator.hasNext()) {
JobStep jobStep = jobStepIterator.next();
Step jslStep = new Step();
JobStepDefinition jobStepDefinition = STEP_DEFINITION_SERVICE.find(jobStep.getScopeId(), jobStep.getJobStepDefinitionId());
switch (jobStepDefinition.getStepType()) {
case GENERIC:
jslStep.setBatchlet(JobDefinitionBuildUtils.buildGenericStep(jobStepDefinition));
break;
case TARGET:
jslStep.setChunk(JobDefinitionBuildUtils.buildChunkStep(jobStepDefinition));
break;
default:
throw new KapuaIllegalArgumentException(jobStepDefinition.getStepType().name(), "jobStepDefinition.stepType");
}
jslStep.setId("step-" + jobStep.getStepIndex());
if (jobStepIterator.hasNext()) {
jslStep.setNextFromAttribute("step-" + (jobStep.getStepIndex() + 1));
}
jslStep.setProperties(JobDefinitionBuildUtils.buildStepProperties(jobStepDefinition, jobStep, jobStepIterator.hasNext()));
jslExecutionElements.add(jslStep);
}
JSLJob jslJob = new JSLJob();
jslJob.setRestartable("true");
jslJob.setId(jobName);
jslJob.setVersion("1.0");
jslJob.setProperties(JobDefinitionBuildUtils.buildJobProperties(scopeId, jobId, jobStartOptions));
jslJob.setListeners(JobDefinitionBuildUtils.buildListener());
jslJob.getExecutionElements().addAll(jslExecutionElements);
jobXmlDefinition = ModelSerializerFactory.createJobModelSerializer().serializeModel(jslJob);
} catch (Exception e) {
throw new CannotBuildJobDefDriverException(e, jobName);
}
//
// Retrieve temporary directory for job XML definition
String tmpDirectory = SystemUtils.getJavaIoTmpDir().getAbsolutePath();
File jobTempDirectory = new File(tmpDirectory, "kapua-job/" + scopeId.toCompactId());
if (!jobTempDirectory.exists() && !jobTempDirectory.mkdirs()) {
throw new CannotCreateTmpDirDriverException(jobName, jobTempDirectory.getAbsolutePath());
}
//
// Retrieve job XML definition file. Delete it if exist
File jobXmlDefinitionFile = new File(jobTempDirectory, jobId.toCompactId().concat("-").concat(String.valueOf(System.nanoTime())).concat(".xml"));
if (jobXmlDefinitionFile.exists() && !jobXmlDefinitionFile.delete()) {
throw new CannotCleanJobDefFileDriverException(jobName, jobXmlDefinitionFile.getAbsolutePath());
}
try (FileOutputStream tmpStream = new FileOutputStream(jobXmlDefinitionFile)) {
IOUtils.write(jobXmlDefinition, tmpStream);
} catch (IOException e) {
throw new CannotWriteJobDefFileDriverException(e, jobName, jobXmlDefinitionFile.getAbsolutePath());
}
//
// Start job
try {
JOB_OPERATOR.start(jobXmlDefinitionFile.getAbsolutePath().replaceAll("\\.xml$", ""), new Properties());
} catch (NoSuchJobExecutionException | NoSuchJobException | JobSecurityException e) {
throw new JobStartingDriverException(e, jobName);
}
}
/**
* Stops completely the jBatch job.
* <p>
* First invokes the {@link JobOperator#stop(long)} on the running execution, which stop the running execution.
* Secondly, according to the {@link JobEngineSettingKeys#JOB_ENGINE_STOP_WAIT_CHECK} value, it waits asynchronously the complete stop of the job
* to be able to invoke {@link JobOperator#abandon(long)} which terminate the jBatch Job.
* <p>
* A jBatch job cannot be resumed after this method is invoked on it.
*
* @param scopeId The scopeId of the {@link Job}
* @param jobId The id of the {@link Job}
* @param toStopJobExecutionId The optional {@link org.eclipse.kapua.service.job.execution.JobExecution#getId()} to stop.
* @throws ExecutionNotFoundDriverException when there isn't a corresponding job execution in jBatch tables
* @throws ExecutionNotRunningDriverException when the corresponding job execution is not running.
* @since 1.0.0
*/
public static void stopJob(@NotNull KapuaId scopeId, @NotNull KapuaId jobId, KapuaId toStopJobExecutionId) throws JbatchDriverException, KapuaException {
String jobName = getJbatchJobName(scopeId, jobId);
//
// Check running
List<JobExecution> runningExecutions = getRunningJobExecutions(scopeId, jobId);
if (runningExecutions.isEmpty()) {
throw new ExecutionNotRunningDriverException(jobName);
}
//
// Filter execution to stop
if (toStopJobExecutionId != null) {
org.eclipse.kapua.service.job.execution.JobExecution je = JOB_EXECUTION_SERVICE.find(scopeId, toStopJobExecutionId);
long toStopJbatchExecutionId = Long.parseLong((String) je.getEntityAttributes().get(JBATCH_EXECUTION_ID));
runningExecutions = runningExecutions.stream().filter(re -> re.getExecutionId() == toStopJbatchExecutionId).collect(Collectors.toList());
}
//
// Do stop
try {
runningExecutions.forEach((runningExecution -> {
JOB_OPERATOR.stop(runningExecution.getExecutionId());
}));
} catch (NoSuchJobExecutionException e) {
throw new ExecutionNotFoundDriverException(e, jobName);
} catch (JobExecutionNotRunningException e) {
throw new ExecutionNotRunningDriverException(e, jobName);
}
}
public static void resumeJob(@NotNull KapuaId scopeId, @NotNull KapuaId jobId, @NotNull KapuaId toResumeJobExecutionId) throws JbatchDriverException, KapuaException {
String jobName = getJbatchJobName(scopeId, jobId);
//
// Get list
List<JobExecution> stoppedJobExecutions = getJobExecutions(scopeId, jobId);
if (stoppedJobExecutions.isEmpty()) {
throw new ExecutionNotFoundDriverException(jobName);
}
//
// Filter execution to resume
org.eclipse.kapua.service.job.execution.JobExecution je = JOB_EXECUTION_SERVICE.find(scopeId, toResumeJobExecutionId);
long toResumeJbatchExecutionId = Long.parseLong((String) je.getEntityAttributes().get(JBATCH_EXECUTION_ID));
stoppedJobExecutions = stoppedJobExecutions.stream().filter(re -> re.getExecutionId() == toResumeJbatchExecutionId).collect(Collectors.toList());
//
// Do stop
try {
stoppedJobExecutions.forEach((stoppedExecution -> {
Properties properties = new Properties();
properties.setProperty(JobContextPropertyNames.RESUMED_KAPUA_EXECUTION_ID, toResumeJobExecutionId.toCompactId());
JOB_OPERATOR.restart(stoppedExecution.getExecutionId(), properties);
}));
} catch (NoSuchJobExecutionException e) {
throw new ExecutionNotFoundDriverException(e, jobName);
} catch (JobExecutionNotRunningException e) {
throw new ExecutionNotRunningDriverException(e, jobName);
}
}
/**
* Checks whether or not the {@link Job} identified by the parametersis in a running status.
* <p>
* jBatch {@link Job} running statuses are listed in {@link JbatchJobRunningStatuses}
*
* @param scopeId The scopeId of the {@link Job}
* @param jobId The id of the {@link Job}
* @return {@code true} if the jBatch {@link Job} is running, {@code false} otherwise.
* @since 1.0.0
*/
public static boolean isRunningJob(@NotNull KapuaId scopeId, @NotNull KapuaId jobId) {
return !getRunningJobExecutions(scopeId, jobId).isEmpty();
}
public static void cleanJobData(@NotNull KapuaId scopeId, @NotNull KapuaId jobId) throws CleanJobDataDriverException {
String jobName = getJbatchJobName(scopeId, jobId);
try {
((KapuaJDBCPersistenceManagerImpl) ServicesManagerImpl.getInstance().getPersistenceManagerService()).purgeByName(jobName);
} catch (Exception ex) {
throw new CleanJobDataDriverException(ex, jobName);
}
}
//
// Private methods
//
private static List<JobExecution> getRunningJobExecutions(@NotNull KapuaId scopeId, @NotNull KapuaId jobId) {
return getJobExecutions(scopeId, jobId).stream().filter(je -> JbatchJobRunningStatuses.getStatuses().contains(je.getBatchStatus())).collect(Collectors.toList());
}
private static List<JobExecution> getJobExecutions(@NotNull KapuaId scopeId, @NotNull KapuaId jobId) {
List<JobExecution> jobExecutions = Lists.newArrayList();
String jobName = getJbatchJobName(scopeId, jobId);
try {
int jobInstanceCount = JOB_OPERATOR.getJobInstanceCount(jobName);
List<JobInstance> jobInstances = JOB_OPERATOR.getJobInstances(jobName, 0, jobInstanceCount);
jobInstances.forEach(ji -> jobExecutions.addAll(getJbatchJobExecutions(ji)));
} catch (NoSuchJobException e) {
LOG.debug("Error while getting Job: " + jobName, e);
// This exception is thrown when there is no job, this means that the job never run before
// So we can ignore it and return `null`
} catch (NoSuchJobExecutionException e) {
LOG.debug("Error while getting execution status for Job: " + jobName, e);
// This exception is thrown when there is no execution is running.
// So we can ignore it and return `null`
}
return jobExecutions;
}
private static List<JobExecution> getJbatchJobExecutions(@NotNull JobInstance jobInstance) {
try {
return JOB_OPERATOR.getJobExecutions(jobInstance);
} catch (NoSuchJobInstanceException e) {
LOG.debug("Error while getting Job Instance: " + jobInstance.getInstanceId(), e);
// This exception is thrown when there is no job instance, this means that the job never run before
// So we can ignore it and return `null`
}
return Collections.emptyList();
}
}