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
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.application.process;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.application.config.AppSettings;
import org.sonar.process.ProcessId;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public class ManagedProcessHandler {
public static final long DEFAULT_WATCHER_DELAY_MS = 500L;
private static final Logger LOG = LoggerFactory.getLogger(ManagedProcessHandler.class);
private final ProcessId processId;
private final ManagedProcessLifecycle lifecycle;
private final List<ManagedProcessEventListener> eventListeners;
private final Timeout stopTimeout;
private final Timeout hardStopTimeout;
private final long watcherDelayMs;
private final AppSettings appSettings;
private ManagedProcess process;
private StreamGobbler stdOutGobbler;
private StreamGobbler stdErrGobbler;
private final StopWatcher stopWatcher;
private final EventWatcher eventWatcher;
// keep flag so that the operational event is sent only once
// to listeners
private boolean operational = false;
private ManagedProcessHandler(Builder builder) {
this.processId = requireNonNull(builder.processId, "processId can't be null");
this.lifecycle = new ManagedProcessLifecycle(this.processId, builder.lifecycleListeners);
this.eventListeners = builder.eventListeners;
this.stopTimeout = builder.stopTimeout;
this.hardStopTimeout = builder.hardStopTimeout;
this.watcherDelayMs = builder.watcherDelayMs;
this.stopWatcher = new StopWatcher();
this.eventWatcher = new EventWatcher();
this.appSettings = builder.settings;
}
public boolean start(Supplier<ManagedProcess> commandLauncher) {
if (!lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STARTING)) {
// has already been started
return false;
}
try {
this.process = commandLauncher.get();
} catch (RuntimeException e) {
LOG.error("Failed to launch process [{}]", processId.getHumanReadableName(), e);
lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STOPPING);
finalizeStop();
throw e;
}
this.stdOutGobbler = new StreamGobbler(process.getInputStream(), appSettings, processId.getKey());
this.stdOutGobbler.start();
this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), appSettings, processId.getKey());
this.stdErrGobbler.start();
this.stopWatcher.start();
this.eventWatcher.start();
// Could be improved by checking the status "up" in shared memory.
// Not a problem so far as this state is not used by listeners.
lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STARTED);
return true;
}
public ProcessId getProcessId() {
return processId;
}
ManagedProcessLifecycle.State getState() {
return lifecycle.getState();
}
public void stop() throws InterruptedException {
if (lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STOPPING)) {
stopImpl();
if (process != null && process.isAlive()) {
LOG.info("{} failed to stop in a graceful fashion. Hard stopping it.", processId.getHumanReadableName());
hardStop();
} else {
// enforce stop and clean-up even if process has been quickly stopped
finalizeStop();
}
} else {
// already stopping or stopped
waitForDown();
}
}
/**
* Sends kill signal and awaits termination. No guarantee that process is gracefully terminated (=shutdown hooks
* executed). It depends on OS.
*/
public void hardStop() throws InterruptedException {
if (lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.HARD_STOPPING)) {
hardStopImpl();
if (process != null && process.isAlive()) {
LOG.info("{} failed to stop in a quick fashion. Killing it.", processId.getHumanReadableName());
}
// enforce stop and clean-up even if process has been quickly stopped
finalizeStop();
} else {
// already stopping or stopped
waitForDown();
}
}
private void waitForDown() {
while (process != null && process.isAlive()) {
try {
process.waitFor();
} catch (InterruptedException ignored) {
// ignore, waiting for process to stop
Thread.currentThread().interrupt();
}
}
}
private void stopImpl() throws InterruptedException {
if (process == null) {
return;
}
try {
process.askForStop();
process.waitFor(stopTimeout.getDuration(), stopTimeout.getUnit());
} catch (InterruptedException e) {
// can't wait for the termination of process. Let's assume it's down.
throw rethrowWithWarn(e, format("Interrupted while stopping process %s", processId));
} catch (Throwable e) {
LOG.error("Failed asking for graceful stop of process {}", processId, e);
}
}
private void hardStopImpl() throws InterruptedException {
if (process == null) {
return;
}
try {
process.askForHardStop();
process.waitFor(hardStopTimeout.getDuration(), hardStopTimeout.getUnit());
} catch (InterruptedException e) {
// can't wait for the termination of process. Let's assume it's down.
throw rethrowWithWarn(e,
format("Interrupted while hard stopping process %s (currentThread=%s)", processId, Thread.currentThread().getName()));
} catch (Throwable e) {
LOG.error("Failed while asking for hard stop of process {}", processId, e);
}
}
private static InterruptedException rethrowWithWarn(InterruptedException e, String errorMessage) {
LOG.warn(errorMessage, e);
Thread.currentThread().interrupt();
return new InterruptedException(errorMessage);
}
private void finalizeStop() {
if (!lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.FINALIZE_STOPPING)) {
return;
}
interrupt(eventWatcher);
interrupt(stopWatcher);
if (process != null) {
process.destroyForcibly();
waitForDown();
process.closeStreams();
}
if (stdOutGobbler != null) {
StreamGobbler.waitUntilFinish(stdOutGobbler);
stdOutGobbler.interrupt();
}
if (stdErrGobbler != null) {
StreamGobbler.waitUntilFinish(stdErrGobbler);
stdErrGobbler.interrupt();
}
// will trigger state listeners
lifecycle.tryToMoveTo(ManagedProcessLifecycle.State.STOPPED);
}
private static void interrupt(@Nullable Thread thread) {
Thread currentThread = Thread.currentThread();
// prevent current thread from interrupting itself
if (thread != null && currentThread != thread) {
thread.interrupt();
LOG.trace("{} interrupted {}", currentThread.getName(), thread.getName(), new Exception("(capturing stack trace for debugging purpose)"));
}
}
void refreshState() {
if (process.isAlive()) {
if (!operational && process.isOperational()) {
operational = true;
eventListeners.forEach(l -> l.onManagedProcessEvent(processId, ManagedProcessEventListener.Type.OPERATIONAL));
}
if (process.askedForRestart()) {
process.acknowledgeAskForRestart();
eventListeners.forEach(l -> l.onManagedProcessEvent(processId, ManagedProcessEventListener.Type.ASK_FOR_RESTART));
}
}
}
@Override
public String toString() {
return format("Process[%s]", processId.getHumanReadableName());
}
/**
* This thread blocks as long as the monitored process is physically alive.
* It avoids from executing {@link Process#exitValue()} at a fixed rate :
* <ul>
* <li>no usage of exception for flow control. Indeed {@link Process#exitValue()} throws an exception
* if process is alive. There's no method <code>Process#isAlive()</code></li>
* <li>no delay, instantaneous notification that process is down</li>
* </ul>
*/
private class StopWatcher extends Thread {
StopWatcher() {
// this name is different than Thread#toString(), which includes name, priority
// and thread group
// -> do not override toString()
super(format("StopWatcher[%s]", processId.getHumanReadableName()));
}
@Override
public void run() {
try {
process.waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// stop watching process
}
// since process is already stopped, this will only finalize the stop sequence
// call hardStop() rather than finalizeStop() directly because hardStop() checks lifeCycle state and this
// avoid running to concurrent stop finalization pieces of code
try {
hardStop();
} catch (InterruptedException e) {
LOG.debug("Interrupted while stopping [{}] after process ended", processId.getHumanReadableName(), e);
Thread.currentThread().interrupt();
}
}
}
private class EventWatcher extends Thread {
EventWatcher() {
// this name is different than Thread#toString(), which includes name, priority
// and thread group
// -> do not override toString()
super(format("EventWatcher[%s]", processId.getHumanReadableName()));
}
@Override
public void run() {
try {
while (process.isAlive()) {
refreshState();
Thread.sleep(watcherDelayMs);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public static Builder builder(ProcessId processId) {
return new Builder(processId);
}
public static class Builder {
private final ProcessId processId;
private final List<ManagedProcessEventListener> eventListeners = new ArrayList<>();
private final List<ProcessLifecycleListener> lifecycleListeners = new ArrayList<>();
private long watcherDelayMs = DEFAULT_WATCHER_DELAY_MS;
private Timeout stopTimeout;
private Timeout hardStopTimeout;
private AppSettings settings;
private Builder(ProcessId processId) {
this.processId = processId;
}
public Builder addEventListener(ManagedProcessEventListener listener) {
this.eventListeners.add(listener);
return this;
}
public Builder addProcessLifecycleListener(ProcessLifecycleListener listener) {
this.lifecycleListeners.add(listener);
return this;
}
/**
* Default delay is {@link #DEFAULT_WATCHER_DELAY_MS}
*/
public Builder setWatcherDelayMs(long l) {
this.watcherDelayMs = l;
return this;
}
public Builder setStopTimeout(Timeout stopTimeout) {
this.stopTimeout = ensureStopTimeoutNonNull(stopTimeout);
return this;
}
public Builder setHardStopTimeout(Timeout hardStopTimeout) {
this.hardStopTimeout = ensureHardStopTimeoutNonNull(hardStopTimeout);
return this;
}
private static Timeout ensureStopTimeoutNonNull(Timeout stopTimeout) {
return requireNonNull(stopTimeout, "stopTimeout can't be null");
}
private static Timeout ensureHardStopTimeoutNonNull(Timeout hardStopTimeout) {
return requireNonNull(hardStopTimeout, "hardStopTimeout can't be null");
}
public ManagedProcessHandler build() {
ensureStopTimeoutNonNull(this.stopTimeout);
ensureHardStopTimeoutNonNull(this.hardStopTimeout);
return new ManagedProcessHandler(this);
}
public Builder setAppSettings(AppSettings settings) {
this.settings = settings;
return this;
}
}
public static final class Timeout {
private final long duration;
private final TimeUnit timeoutUnit;
private Timeout(long duration, TimeUnit unit) {
this.duration = duration;
this.timeoutUnit = Objects.requireNonNull(unit, "unit can't be null");
}
public static Timeout newTimeout(long duration, TimeUnit unit) {
return new Timeout(duration, unit);
}
public long getDuration() {
return duration;
}
public TimeUnit getUnit() {
return timeoutUnit;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Timeout timeout = (Timeout) o;
return duration == timeout.duration && timeoutUnit == timeout.timeoutUnit;
}
@Override
public int hashCode() {
return Objects.hash(duration, timeoutUnit);
}
}
}
|