-
Notifications
You must be signed in to change notification settings - Fork 138
/
JobManagerService.java
555 lines (475 loc) · 19 KB
/
JobManagerService.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.v3.admin;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.AdminCommandState;
import org.glassfish.api.admin.AdminCommandState.State;
import org.glassfish.api.admin.Job;
import org.glassfish.api.admin.JobManager;
import org.glassfish.api.admin.Payload;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.admin.progress.JobInfo;
import org.glassfish.api.admin.progress.JobInfos;
import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.EventTypes;
import org.glassfish.api.event.Events;
import org.glassfish.api.event.RestrictTo;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.kernel.KernelLoggerInfo;
import org.jvnet.hk2.annotations.Service;
import com.sun.enterprise.admin.event.AdminCommandEventBrokerImpl;
import com.sun.enterprise.admin.remote.RestPayloadImpl;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.ManagedJobConfig;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.util.StringUtils;
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.v3.admin.CheckpointHelper.CheckpointFilename;
import com.sun.enterprise.v3.server.ExecutorServiceFactory;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
/**
* This is the implementation for the JobManagerService The JobManager is responsible for
* <ol>
* <li>generating unique ids for jobs
* <li>serving as a registry for jobs
* <li>creating threadpools for jobs
* <li>removing expired jobs
* </ol>
*
* @author Martin Mares
* @author Bhakti Mehta
*/
@Service(name = "job-manager")
@Singleton
public class JobManagerService implements JobManager, PostConstruct, EventListener {
private static final String CHECKPOINT_MAINDATA = "MAINCMD";
@Inject
private Domain domain;
private ManagedJobConfig managedJobConfig;
private static final int MAX_SIZE = 65535;
private final ConcurrentHashMap<String, Job> jobRegistry = new ConcurrentHashMap<>();
private final AtomicInteger lastId = new AtomicInteger(0);
protected static final LocalStringManagerImpl adminStrings = new LocalStringManagerImpl(JobManagerService.class);
private final static Logger logger = KernelLoggerInfo.getLogger();
private ExecutorService pool;
@Inject
private ExecutorServiceFactory executorFactory;
@Inject
private ServerEnvironment serverEnvironment;
private final String JOBS_FILE = "jobs.xml";
protected JAXBContext jaxbContext;
protected File jobsFile;
@Inject
private ServiceLocator serviceLocator;
@Inject
private JobFileScanner jobFileScanner;
@Inject
Events events;
@Inject
CheckpointHelper checkpointHelper;
@Inject
CommandRunnerImpl commandRunner;
// This will store the data related to completed jobs so that unique ids
// can be generated for new jobs. This is populated lazily the first
// time the JobManagerService is created, it will scan the
// jobs.xml and load the information in memory
private final ConcurrentHashMap<String, CompletedJob> completedJobsInfo = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, CheckpointFilename> retryableJobsInfo = new ConcurrentHashMap<>();
/**
* This will return a new id which is unused
*
* @return
*/
@Override
public synchronized String getNewId() {
int nextId = lastId.incrementAndGet();
if (nextId > MAX_SIZE) {
reset();
}
String nextIdToUse = String.valueOf(nextId);
return !idInUse(nextIdToUse) ? String.valueOf(nextId) : getNewId();
}
public JobInfo getCompletedJobForId(String id, File file) {
for (JobInfo jobInfo : getCompletedJobs(file).getJobInfoList()) {
if (jobInfo.jobId.equals(id)) {
return jobInfo;
}
}
return null;
}
@Override
public JobInfo getCompletedJobForId(String id) {
return getCompletedJobForId(id, getJobsFile());
}
/**
* This resets the id to 0
*/
private void reset() {
lastId.set(0);
}
/**
* This method will return if the id is in use
*
* @param id
* @return true if id is in use
*/
private boolean idInUse(String id) {
return jobRegistry.containsKey(id) || completedJobsInfo.containsKey(id) || retryableJobsInfo.containsKey(id);
}
/**
* This adds the jobs
*
* @param job
* @throws IllegalArgumentException
*/
@Override
public synchronized void registerJob(Job job) throws IllegalArgumentException {
if (job == null) {
throw new IllegalArgumentException(adminStrings.getLocalString("job.cannot.be.null", "Job cannot be null"));
}
if (jobRegistry.containsKey(job.getId())) {
throw new IllegalArgumentException(adminStrings.getLocalString("job.id.in.use", "Job id is already in use."));
}
retryableJobsInfo.remove(job.getId());
jobRegistry.put(job.getId(), job);
if (job.getState() == State.PREPARED && (job instanceof AdminCommandInstanceImpl)) {
((AdminCommandInstanceImpl) job).setState(AdminCommandState.State.RUNNING);
}
}
/**
* This returns all the jobs in the registry
*
* @return The iterator of jobs
*/
@Override
public Iterator<Job> getJobs() {
return jobRegistry.values().iterator();
}
/**
* This will return a job associated with the id
*
* @param id The job whose id matches
* @return
*/
@Override
public Job get(String id) {
return jobRegistry.get(id);
}
/**
* This will return a list of jobs which have crossed the JOBS_RETENTION_PERIOD and need to be purged
*
* @return list of jobs to be purged
*/
public ArrayList<JobInfo> getExpiredJobs(File file) {
ArrayList<JobInfo> expiredJobs = new ArrayList<>();
synchronized (file) {
JobInfos jobInfos = getCompletedJobs(file);
for (JobInfo job : jobInfos.getJobInfoList()) {
long executedTime = job.commandExecutionDate;
long currentTime = System.currentTimeMillis();
long jobsRetentionPeriod = 86400000;
managedJobConfig = domain.getExtensionByType(ManagedJobConfig.class);
jobsRetentionPeriod = convert(managedJobConfig.getJobRetentionPeriod());
if (currentTime - executedTime > jobsRetentionPeriod && (job.state.equals(AdminCommandState.State.COMPLETED.name())
|| job.state.equals(AdminCommandState.State.REVERTED.name()))) {
expiredJobs.add(job);
}
}
}
return expiredJobs;
}
public long convert(String input) {
String period = input.substring(0, input.length() - 1);
long timeInterval = Long.parseLong(period);
String s = input.toLowerCase(Locale.US);
long milliseconds = 86400000;
if (s.indexOf("s") > 0) {
milliseconds = timeInterval * 1000;
} else if (s.indexOf("h") > 0) {
milliseconds = timeInterval * 3600 * 1000;
} else if (s.indexOf("m") > 0) {
milliseconds = timeInterval * 60 * 1000;
}
return milliseconds;
}
/**
* This will remove the job from the registry
*
* @param id The job id of the job to be removed
*/
@Override
public synchronized void purgeJob(final String id) {
Job job = jobRegistry.remove(id);
logger.fine(adminStrings.getLocalString("removed.expired.job", "Removed expired job ", job));
}
public void deleteCheckpoint(final File parentDir, final String jobId) {
// list all related files
File[] toDelete = parentDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(jobId + ".") || name.startsWith(jobId + "-");
}
});
for (File td : toDelete) {
td.delete();
}
}
public ExecutorService getThreadPool() {
return pool;
}
/**
* This will load the jobs which have already completed and persisted in the jobs.xml
*
* @return JobsInfos which contains information about completed jobs
*/
@Override
public JobInfos getCompletedJobs(File jobsFile) {
synchronized (jobsFile) {
try {
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance(JobInfos.class);
}
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if (jobsFile.exists()) {
JobInfos jobInfos = (JobInfos) unmarshaller.unmarshal(jobsFile);
return jobInfos;
}
} catch (JAXBException e) {
throw new RuntimeException(adminStrings.getLocalString("error.reading.completed.jobs", "Error reading completed jobs ",
e.getLocalizedMessage()), e);
}
return null;
}
}
/**
* This method looks for the completed jobs and purges a job which is marked with the jobId
*
* @param jobId the job to purge
* @return the new list of completed jobs
*/
public JobInfos purgeCompletedJobForId(String jobId, File file) {
JobInfos completedJobInfos = getCompletedJobs(file);
synchronized (file) {
CopyOnWriteArrayList<JobInfo> jobList = new CopyOnWriteArrayList<>();
if (completedJobInfos != null) {
jobList.addAll(completedJobInfos.getJobInfoList());
for (JobInfo jobInfo : jobList) {
if (jobInfo.jobId.equals(jobId)) {
jobList.remove(jobInfo);
}
}
}
JobInfos jobInfos = new JobInfos();
// if (jobList.size() > 0) {
try {
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance(JobInfos.class);
}
jobInfos.setJobInfoList(jobList);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(jobInfos, file);
} catch (JAXBException e) {
throw new RuntimeException(adminStrings.getLocalString("error.purging.completed.job", "Error purging completed job ", jobId,
e.getLocalizedMessage()), e);
}
// }
return jobInfos;
}
}
@Override
public JobInfos purgeCompletedJobForId(String id) {
return purgeCompletedJobForId(id, getJobsFile());
}
@Override
public void postConstruct() {
jobsFile = new File(serverEnvironment.getConfigDirPath(), JOBS_FILE);
pool = executorFactory.provide();
HashSet<File> persistedJobFiles = jobFileScanner.getJobFiles();
persistedJobFiles.add(jobsFile);
// Check if there are jobs.xml files which have completed jobs so that
// unique ids get generated
for (File jobfile : persistedJobFiles) {
if (jobfile != null) {
reapCompletedJobs(jobfile);
boolean dropInterruptedCommands = Boolean.valueOf(System.getProperty(SystemPropertyConstants.DROP_INTERRUPTED_COMMANDS));
Collection<CheckpointFilename> listed = checkpointHelper.listCheckpoints(jobfile.getParentFile());
for (CheckpointFilename cf : listed) {
if (dropInterruptedCommands) {
logger.info("Dropping checkpoint: " + cf.getFile());
deleteCheckpoint(cf.getParentDir(), cf.getJobId());
} else {
this.retryableJobsInfo.put(cf.getJobId(), cf);
}
}
}
}
events.register(this);
}
@Override
public File getJobsFile() {
return jobsFile;
}
public void addToCompletedJobs(CompletedJob job) {
completedJobsInfo.put(job.getId(), job);
}
public void removeFromCompletedJobs(String id) {
completedJobsInfo.remove(id);
}
public ConcurrentHashMap<String, CompletedJob> getCompletedJobsInfo() {
return completedJobsInfo;
}
public ConcurrentHashMap<String, CheckpointFilename> getRetryableJobsInfo() {
return retryableJobsInfo;
}
@Override
public void checkpoint(AdminCommandContext context, Serializable data) throws IOException {
checkpoint((AdminCommand) null, context);
if (data != null) {
checkpointAttachement(context.getJobId(), CHECKPOINT_MAINDATA, data);
}
}
@Override
public void checkpoint(AdminCommand command, AdminCommandContext context) throws IOException {
if (!StringUtils.ok(context.getJobId())) {
throw new IllegalArgumentException("Command is not managed");
}
Job job = get(context.getJobId());
if (job.getJobsFile() == null) {
job.setJobsFile(getJobsFile());
}
Checkpoint chkp = new Checkpoint(job, command, context);
checkpointHelper.save(chkp);
if (job instanceof AdminCommandInstanceImpl) {
((AdminCommandInstanceImpl) job).setState(AdminCommandState.State.RUNNING_RETRYABLE);
}
}
public void checkpointAttachement(String jobId, String attachId, Serializable data) throws IOException {
Job job = get(jobId);
if (job.getJobsFile() == null) {
job.setJobsFile(getJobsFile());
}
checkpointHelper.saveAttachment(data, job, attachId);
}
public <T extends Serializable> T loadCheckpointAttachement(String jobId, String attachId) throws IOException, ClassNotFoundException {
Job job = get(jobId);
if (job.getJobsFile() == null) {
job.setJobsFile(getJobsFile());
}
return checkpointHelper.loadAttachment(job, attachId);
}
@Override
public Serializable loadCheckpointData(String jobId) throws IOException, ClassNotFoundException {
return loadCheckpointAttachement(jobId, CHECKPOINT_MAINDATA);
}
public Checkpoint loadCheckpoint(String jobId, Payload.Outbound outbound) throws IOException, ClassNotFoundException {
Job job = get(jobId);
CheckpointFilename cf = null;
if (job == null) {
cf = getRetryableJobsInfo().get(jobId);
if (cf == null) {
cf = CheckpointFilename.createBasic(jobId, getJobsFile());
}
} else {
cf = CheckpointFilename.createBasic(job);
}
return loadCheckpoint(cf, outbound);
}
private Checkpoint loadCheckpoint(CheckpointFilename cf, Payload.Outbound outbound) throws IOException, ClassNotFoundException {
Checkpoint result = checkpointHelper.load(cf, outbound);
if (result != null) {
serviceLocator.inject(result.getJob());
serviceLocator.postConstruct(result.getJob());
if (result.getCommand() != null) {
serviceLocator.inject(result.getCommand());
serviceLocator.postConstruct(result.getCommand());
}
}
return result;
}
/*
* This method will look for completed jobs from the jobs.xml files and load the information in a local datastructure
* for faster access
*/
protected void reapCompletedJobs(File file) {
if (file != null && file.exists()) {
JobInfos jobInfos = getCompletedJobs(file);
if (jobInfos != null) {
for (JobInfo jobInfo : jobInfos.getJobInfoList()) {
addToCompletedJobs(new CompletedJob(jobInfo.jobId, jobInfo.commandCompletionDate, jobInfo.getJobsFile()));
}
}
}
}
@Override
public void event(@RestrictTo(EventTypes.SERVER_READY_NAME) Event event) {
if (event.is(EventTypes.SERVER_READY)) {
if (retryableJobsInfo.size() > 0) {
Runnable runnable = new Runnable() {
@Override
public void run() {
logger.fine("Restarting retryable jobs");
for (CheckpointFilename cf : retryableJobsInfo.values()) {
reexecuteJobFromCheckpoint(cf);
}
}
};
(new Thread(runnable)).start();
} else {
logger.fine("No retryable job found");
}
}
}
private void reexecuteJobFromCheckpoint(CheckpointFilename cf) {
Checkpoint checkpoint = null;
try {
RestPayloadImpl.Outbound outbound = new RestPayloadImpl.Outbound(true);
checkpoint = loadCheckpoint(cf, outbound);
} catch (Exception ex) {
logger.log(Level.WARNING, KernelLoggerInfo.exceptionLoadCheckpoint, ex);
}
if (checkpoint != null) {
logger.log(Level.INFO, KernelLoggerInfo.checkpointAutoResumeStart, new Object[] { checkpoint.getJob().getName() });
commandRunner.executeFromCheckpoint(checkpoint, false, new AdminCommandEventBrokerImpl());
ActionReport report = checkpoint.getContext().getActionReport();
logger.log(Level.INFO, KernelLoggerInfo.checkpointAutoResumeDone,
new Object[] { checkpoint.getJob().getName(), report.getActionExitCode(), report.getTopMessagePart() });
}
}
}