forked from apache/tika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchProcessDriverCLI.java
402 lines (357 loc) · 14.8 KB
/
BatchProcessDriverCLI.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
/*
* 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 org.apache.tika.batch;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BatchProcessDriverCLI {
/**
* This relies on an special exit values of 254 (do not restart),
* 0 ended correctly, 253 ended with exception (do restart)
*/
public static final int PROCESS_RESTART_EXIT_CODE = 253;
//make sure this is above 255 to avoid stopping on system errors
//that is, if there is a system error (e.g. 143), you
//should restart the process.
public static final int PROCESS_NO_RESTART_EXIT_CODE = 254;
public static final int PROCESS_COMPLETED_SUCCESSFULLY = 0;
private static final Logger LOG = LoggerFactory.getLogger(BatchProcessDriverCLI.class);
private final InterruptWatcher interruptWatcher = new InterruptWatcher(System.in);
private final Thread interruptWatcherThread = new Thread(interruptWatcher);
private final String[] commandLine;
private int maxProcessRestarts = -1;
private long pulseMillis = 1000;
//how many times to wait pulseMillis milliseconds if a restart
//message has been received through stdout, but the
//forked process has not yet exited
private int waitNumLoopsAfterRestartMessage = 60;
private int loopsAfterRestartMessageReceived = 0;
private volatile boolean userInterrupted = false;
private boolean receivedRestartMsg = false;
private Process process = null;
private StreamGobbler errorWatcher = null;
private StreamGobbler outGobbler = null;
private InterruptWriter interruptWriter = null;
private Thread errorWatcherThread = null;
private Thread outGobblerThread = null;
private Thread interruptWriterThread = null;
private int numRestarts = 0;
private boolean redirectForkedProcessToStdOut = true;
public BatchProcessDriverCLI(String[] commandLine) {
this.commandLine = tryToReadMaxRestarts(commandLine);
}
public static void main(String[] args) throws Exception {
final BatchProcessDriverCLI runner = new BatchProcessDriverCLI(args);
//make absolutely certain that the forked process is terminated
Runtime.getRuntime().addShutdownHook(new Thread(runner::stop));
runner.execute();
System.out.println("FSBatchProcessDriver has gracefully completed");
System.exit(0);
}
private String[] tryToReadMaxRestarts(String[] commandLine) {
List<String> args = new ArrayList<>();
for (int i = 0; i < commandLine.length; i++) {
String arg = commandLine[i];
if (arg.equals("-maxRestarts")) {
if (i == commandLine.length - 1) {
throw new IllegalArgumentException(
"Must specify an integer after \"-maxRestarts\"");
}
String restartNumString = commandLine[i + 1];
try {
maxProcessRestarts = Integer.parseInt(restartNumString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Must specify an integer after \"-maxRestarts\" arg.");
}
i++;
} else {
args.add(arg);
}
}
return args.toArray(new String[0]);
}
public void execute() throws Exception {
interruptWatcherThread.setDaemon(true);
interruptWatcherThread.start();
LOG.info("about to start driver");
start();
while (!userInterrupted) {
Integer exit = null;
try {
LOG.trace("about to check exit value");
exit = process.exitValue();
LOG.info("The forked process has finished with an exit value of: {}", exit);
stop();
} catch (IllegalThreadStateException e) {
//hasn't exited
LOG.trace("process has not exited; IllegalThreadStateException");
}
LOG.trace("Before sleep: exit={} receivedRestartMsg={}", exit, receivedRestartMsg);
//Even if the process has exited,
//wait just a little bit to make sure that
//mustRestart hasn't been set to true
try {
Thread.sleep(pulseMillis);
} catch (InterruptedException e) {
LOG.trace("interrupted exception during sleep");
}
LOG.trace("After sleep: exit={} receivedRestartMsg={}", exit, receivedRestartMsg);
//if we've gotten the message via stdout to restart
//but the process hasn't exited yet, give it another
//chance
if (receivedRestartMsg && exit == null &&
loopsAfterRestartMessageReceived <= waitNumLoopsAfterRestartMessage) {
loopsAfterRestartMessageReceived++;
LOG.warn("Must restart, still not exited; loops after restart: {}",
loopsAfterRestartMessageReceived);
continue;
}
if (loopsAfterRestartMessageReceived > waitNumLoopsAfterRestartMessage) {
LOG.trace("About to try to restart because: exit={} receivedRestartMsg={}", exit,
receivedRestartMsg);
LOG.warn("Restarting after exceeded wait loops waiting for exit: {}",
loopsAfterRestartMessageReceived);
boolean restarted = restart(exit, receivedRestartMsg);
if (!restarted) {
break;
}
} else if (exit != null && exit != BatchProcessDriverCLI.PROCESS_NO_RESTART_EXIT_CODE &&
exit != BatchProcessDriverCLI.PROCESS_COMPLETED_SUCCESSFULLY) {
LOG.trace("About to try to restart because: exit={} receivedRestartMsg={}", exit,
receivedRestartMsg);
if (exit == BatchProcessDriverCLI.PROCESS_RESTART_EXIT_CODE) {
LOG.info("Restarting on expected restart code");
} else {
LOG.warn("Restarting on unexpected restart code: {}", exit);
}
boolean restarted = restart(exit, receivedRestartMsg);
if (!restarted) {
break;
}
} else if (exit != null && (exit == PROCESS_COMPLETED_SUCCESSFULLY ||
exit == BatchProcessDriverCLI.PROCESS_NO_RESTART_EXIT_CODE)) {
LOG.trace("Will not restart: {}", exit);
break;
}
}
LOG.trace("about to call shutdown driver now");
shutdownDriverNow();
LOG.info("Process driver has completed");
}
private void shutdownDriverNow() {
if (process != null) {
for (int i = 0; i < 60; i++) {
LOG.trace("trying to shut down: {}", i);
try {
int exit = process.exitValue();
LOG.trace("trying to stop: {}", exit);
stop();
interruptWatcherThread.interrupt();
return;
} catch (IllegalThreadStateException e) {
//hasn't exited
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//swallow
}
}
LOG.error("Process didn't stop after 60 seconds after shutdown. " +
"I am forcefully terminating it.");
}
interruptWatcherThread.interrupt();
}
public int getNumRestarts() {
return numRestarts;
}
public boolean isUserInterrupted() {
return userInterrupted;
}
/**
* Tries to restart (stop and then start) the forked process
*
* @return whether or not this was successful, will be false if numRestarts >= maxProcessRestarts
* @throws Exception
*/
private boolean restart(Integer exitValue, boolean receivedRestartMsg) throws Exception {
if (maxProcessRestarts > -1 && numRestarts >= maxProcessRestarts) {
LOG.warn("Hit the maximum number of process restarts. Driver is shutting down now.");
stop();
return false;
}
LOG.warn("Must restart process (exitValue={} numRestarts={} receivedRestartMessage={})",
exitValue, numRestarts, receivedRestartMsg);
stop();
start();
numRestarts++;
loopsAfterRestartMessageReceived = 0;
return true;
}
private void stop() {
if (process != null) {
LOG.trace("destroying a non-null process");
process.destroyForcibly();
}
receivedRestartMsg = false;
//interrupt the writer thread first
interruptWriterThread.interrupt();
errorWatcher.stopGobblingAndDie();
outGobbler.stopGobblingAndDie();
errorWatcherThread.interrupt();
outGobblerThread.interrupt();
}
private void start() throws Exception {
ProcessBuilder builder = new ProcessBuilder(commandLine);
process = builder.start();
errorWatcher = new StreamWatcher(process.getErrorStream());
errorWatcherThread = new Thread(errorWatcher);
errorWatcherThread.start();
outGobbler = new StreamGobbler(process.getInputStream());
outGobblerThread = new Thread(outGobbler);
outGobblerThread.start();
interruptWriter = new InterruptWriter(process.getOutputStream());
interruptWriterThread = new Thread(interruptWriter);
interruptWriterThread.start();
}
/**
* Typically only used for testing. This determines whether or not
* to redirect forked process's stdOut to driver's stdout
*
* @param redirectForkedProcessToStdOut should the driver redirect the child's stdout
*/
public void setRedirectForkedProcessToStdOut(boolean redirectForkedProcessToStdOut) {
this.redirectForkedProcessToStdOut = redirectForkedProcessToStdOut;
}
/**
* Class to watch stdin from the driver for anything that is typed.
* This will currently cause an interrupt if anything followed by
* a return key is entered. We may want to add an "Are you sure?" dialogue.
*/
private class InterruptWatcher implements Runnable {
private BufferedReader reader;
private InterruptWatcher(InputStream is) {
reader = new BufferedReader(new InputStreamReader(is, UTF_8));
}
@Override
public void run() {
try {
//this will block.
//as soon as it reads anything,
//set userInterrupted to true and stop
reader.readLine();
userInterrupted = true;
} catch (IOException e) {
//swallow
}
}
}
/**
* Class that writes to the forked process
* to force an interrupt in the forked process.
*/
private class InterruptWriter implements Runnable {
private final Writer writer;
private InterruptWriter(OutputStream os) {
this.writer = new OutputStreamWriter(os, UTF_8);
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(500);
if (userInterrupted) {
writer.write(String.format(Locale.ENGLISH, "Ave atque vale!%n"));
writer.flush();
}
}
} catch (IOException e) {
//swallow
} catch (InterruptedException e) {
//job is done, ok
}
}
}
private class StreamGobbler implements Runnable {
//plagiarized from org.apache.oodt's StreamGobbler
protected final BufferedReader reader;
protected boolean running = true;
private StreamGobbler(InputStream is) {
this.reader =
new BufferedReader(new InputStreamReader(new BufferedInputStream(is), UTF_8));
}
@Override
public void run() {
String line = null;
try {
LOG.trace("gobbler starting to read");
while ((line = reader.readLine()) != null && this.running) {
if (redirectForkedProcessToStdOut) {
System.out.println("BatchProcess:" + line);
}
}
} catch (IOException e) {
LOG.trace("gobbler io exception");
//swallow ioe
}
LOG.trace("gobbler done");
}
private void stopGobblingAndDie() {
LOG.trace("stop gobbling");
running = false;
IOUtils.closeQuietly(reader);
}
}
private class StreamWatcher extends StreamGobbler implements Runnable {
//plagiarized from org.apache.oodt's StreamGobbler
private StreamWatcher(InputStream is) {
super(is);
}
@Override
public void run() {
String line = null;
try {
LOG.trace("watcher starting to read");
while ((line = reader.readLine()) != null && this.running) {
if (line.startsWith(
BatchProcess.BATCH_CONSTANTS.BATCH_PROCESS_FATAL_MUST_RESTART
.toString())) {
receivedRestartMsg = true;
}
LOG.info("BatchProcess: " + line);
}
} catch (IOException e) {
LOG.trace("watcher io exception");
//swallow ioe
}
LOG.trace("watcher done");
}
}
}