-
Notifications
You must be signed in to change notification settings - Fork 131
/
SystemCell.java
424 lines (375 loc) · 14.7 KB
/
SystemCell.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
package dmg.cells.nucleus;
import static java.util.stream.Collectors.toMap;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.MoreExecutors;
import dmg.util.AuthorizedString;
import dmg.util.command.Argument;
import dmg.util.command.Command;
import dmg.util.command.Option;
import dmg.util.logback.FilterShell;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import jdk.jfr.Configuration;
import jdk.jfr.Recording;
import org.apache.curator.framework.CuratorFramework;
import org.dcache.alarms.AlarmMarkerFactory;
import org.dcache.alarms.PredefinedAlarm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.remoting.RemoteProxyFailureException;
/**
* @author Patrick Fuhrmann
* @version 0.1, 15 Feb 1998
*/
public class SystemCell
extends CellAdapter
implements Thread.UncaughtExceptionHandler {
private static final Logger _log = LoggerFactory.getLogger(SystemCell.class);
private final CellShell _cellShell;
private final CellNucleus _nucleus;
private volatile Recording recording;
private int _packetsReceived,
_packetsAnswered,
_packetsForwarded,
_packetsReplied,
_exceptionCounter;
private final Runtime _runtime = Runtime.getRuntime();
private final Semaphore _shutdownLock = new Semaphore(0);
private class TheKiller extends Thread {
@Override
public void run() {
_log.info("Running shutdown sequence");
kill();
_log.info("Kill done, waiting for shutdown lock");
try {
_shutdownLock.acquire();
} catch (Exception ee) {
}
_log.info("Killer done");
}
}
public static SystemCell create(String cellDomainName,
CuratorFramework curatorFramework, Optional<String> zone,
SerializationHandler.Serializer serializer) {
CellNucleus.initCellGlue(cellDomainName, curatorFramework, zone, serializer);
return new SystemCell();
}
protected SystemCell() {
super("System", "System", "");
_nucleus = getNucleus();
_cellShell = new CellShell(getNucleus());
}
@Override
protected void starting() {
/* We start the curator here to get the right context for the curator threads.
*/
CellNucleus.startCurator();
_cellShell.addCommandListener(this);
_cellShell.addCommandListener(new LogbackShell());
_cellShell.addCommandListener(new FilterShell(_nucleus.getLoggingThresholds()));
_cellShell.addCommandListener(_cellShell.new HelpCommands());
useInterpreter(false);
_runtime.addShutdownHook(new TheKiller());
}
@Override
protected void started() {
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void stopped() {
shutdownSystem();
CellNucleus.shutdownCellGlue();
_log.info("Opening shutdown lock");
_shutdownLock.release();
System.exit(0);
}
//
// interface from Cell
//
public String toString() {
long fm = _runtime.freeMemory();
long tm = _runtime.totalMemory();
return getCellDomainName() +
":IOrec=" + _packetsReceived +
";IOexc=" + _exceptionCounter +
";MEM=" + (tm - fm);
}
@Command(name = "get hostname", hint = "show this dCache-domain hostname",
description = "Returns the hostname of the computer this (dCache) " +
"domain is running at. The hostname returned can be either " +
"the fully qualified domain name for this IP address " +
"or just 'localhost', if the local host name could not" +
" be resolved into an address.")
public class GetHostnameCommand implements Callable<String> {
@Override
public String call() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException ex) {
return "localhost";
}
}
}
@Command(name = "jfr start", hint = "Starts Java flight recorder.",
description = "Starts Java flight recorder. The JFR configuration can be specified as optional"
+ " parameter: either as one of JVM pre-defined configuration names 'default' or 'profile', or"
+ " as an absolute path to a custom configuration."
)
public class StartJFR implements Callable<String> {
@Argument(usage = "Predefined JFR configuration.", metaVar = "configuration", required = false, valueSpec = "[default|profile|<path>]")
String config = "default";
@Override
public String call() throws Exception {
if (recording != null) {
return "Another record in progress.";
}
var configuration = config.startsWith("/") ?
Configuration.create(Path.of(config)) : Configuration.getConfiguration(config);
recording = new Recording(configuration);
recording.setName(getCellDomainName());
recording.start();
return "enabled with config: " + configuration.getName();
}
}
@Command(name = "jfr stop", hint = "Stops Java flight recorder", description = "Stops JFR")
public class StopJFR implements Callable<String> {
@Option(name = "out", usage = "Path where to write flight recorder output")
String outFileName;
@Override
public String call() throws Exception {
recording.stop();
try {
var outFile =
outFileName == null ? Files.createTempFile(getCellDomainName() + "_", ".jfr")
: Path.of(outFileName);
recording.dump(outFile);
return "recorded into " + outFile.toAbsolutePath();
} finally {
recording = null;
}
}
}
private void shutdownSystem() {
List<String> names = _nucleus.getCellNames();
List<String> nonSystem = new ArrayList<>(names.size());
List<String> system = new ArrayList<>(names.size());
for (String name : names) {
CellInfo info = _nucleus.getCellInfo(name);
if (info == null) {
continue;
}
String cellName = info.getCellName();
if (cellName.equals("System")) {
// Don't kill the system cell
} else if (info.getCellType().equals("System")) {
system.add(cellName);
} else {
nonSystem.add(cellName);
}
}
_log.info("Will try to shutdown non-system cells {}", nonSystem);
shutdownCells(nonSystem, 5000, 10000);
_log.info("Will try to shutdown remaining cells {}", system);
shutdownCells(system, 5000, 10000);
}
/**
* Shuts down named cells. The method will block until the cells are dead or until a timeout has
* occurred.
*
* @param cells List of names of cells to kill.
* @param softTimeout Timeout in milliseconds to wait until we log the cells we are waiting for
* @param hardTimeout Timeout in milliseconds to wait until we log stack traces and give up
*/
private void shutdownCells(List<String> cells, long softTimeout, long hardTimeout) {
/* We log the completion of cell shutdown from a listener.
*/
final long start = System.currentTimeMillis();
Function<String, Runnable> listeners =
name -> () -> {
long time = System.currentTimeMillis() - start;
if (time > softTimeout) {
_log.warn("Killed {} in {} ms", name, time);
} else {
_log.info("Killed {}", name);
}
};
/* Kill all the cells.
*/
Map<String, CompletableFuture<?>> futures = cells.stream()
.collect(toMap(name -> name, _nucleus::kill));
/* And attach the listener.
*/
futures.forEach((name, future) -> future.thenRunAsync(listeners.apply(name),
MoreExecutors.directExecutor()));
/* Now wait.
*/
try {
CompletableFuture<Void>[] futuresAsArray = futures.values()
.toArray(CompletableFuture[]::new);
try {
CompletableFuture.allOf(futuresAsArray).get(softTimeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
futures.forEach((name, future) -> {
if (!future.isDone()) {
_log.warn("Still waiting for {} to shut down.", name);
}
});
CompletableFuture.allOf(futuresAsArray)
.get(hardTimeout - softTimeout, TimeUnit.MILLISECONDS);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (TimeoutException e) {
futures.forEach((name, future) -> {
if (!future.isDone()) {
CellNucleus.listThreadGroupOf(name);
}
});
CellNucleus.listKillerThreadGroup();
} catch (ExecutionException e) {
_log.error("Unexpected exception during shutdown.", e.getCause());
}
}
@Override
public void getInfo(PrintWriter pw) {
pw.append(" CellDomainName = ").println(getCellDomainName());
pw.append(" Zone = ").println(_nucleus.getZone().orElse("(none)"));
pw.append(" Message payload serializer = ").println(_nucleus.getMsgSerialization());
pw.format(" I/O rcv=%d;asw=%d;frw=%d;rpy=%d;exc=%d\n",
_packetsReceived, _packetsAnswered, _packetsForwarded,
_packetsReplied, _exceptionCounter);
long fm = _runtime.freeMemory();
long tm = _runtime.totalMemory();
pw.format(" Memory : tot=%d;free=%d;used=%d\n", tm, fm, tm - fm);
pw.println(" Cells (Threads)");
for (String name : _nucleus.getCellNames()) {
pw.append(" ").append(name).append("(");
Thread[] threads = _nucleus.getThreads(name);
if (threads != null) {
boolean first = true;
for (Thread thread : threads) {
pw.print(thread.getName());
if (first) {
first = false;
} else {
pw.print(",");
}
}
}
pw.println(")");
}
}
@Override
public void messageToForward(CellMessage msg) {
try {
sendMessage(msg);
_packetsForwarded++;
} catch (RuntimeException eee) {
_exceptionCounter++;
}
}
@Override
public void messageArrived(CellMessage msg) {
_log.info("Message arrived : {}", msg);
_packetsReceived++;
if (msg.isReply()) {
_log.warn("Seems to a bounce : {}", msg);
return;
}
Object obj = msg.getMessageObject();
Serializable reply;
if (obj instanceof String) {
String command = (String) obj;
if (command.isEmpty()) {
return;
}
_log.info("Command: {}", command);
if (command.equals("xyzzy")) {
reply = "Nothing happens.";
} else {
reply = _cellShell.objectCommand2(command);
}
} else if (obj instanceof AuthorizedString) {
AuthorizedString as = (AuthorizedString) obj;
String command = as.toString();
if (command.length() < 1) {
return;
}
_log.info("Command(p={}) : {}", as.getAuthorizedPrincipal(), command);
reply = _cellShell.objectCommand2(command);
} else {
return;
}
_log.debug("Reply : {}", reply);
_packetsAnswered++;
try {
if (reply instanceof Reply) {
((Reply) reply).deliver(this, msg);
} else {
msg.revertDirection();
msg.setMessageObject(reply);
sendMessage(msg);
_log.debug("Sending : {}", msg);
}
_packetsReplied++;
} catch (RuntimeException e) {
_exceptionCounter++;
}
}
@Override
public void uncaughtException(Thread t, Throwable e) {
/* In case of fatal errors we shut down. The wrapper script
* will restart the domain. Notice that there is no guarantee
* that the fatal error will not reoccur during shutdown and
* in that case the shutdown may fail. We may want to consider
* refining the shutdown logic such that in recovers if the
* fatal error reoccurs.
*/
if (e instanceof VirtualMachineError) {
kill();
_log.error(AlarmMarkerFactory.getMarker(PredefinedAlarm.FATAL_JVM_ERROR,
getCellDomainName(),
getCellName()),
"Restarting due to fatal JVM error: {}", e.toString());
return;
}
/*
* The RemotePoolMonitor wraps interrupted exceptions in a
* runtime exception. These should not cause a stack trace to
* be printed.
*/
if (e instanceof RemoteProxyFailureException &&
e.getCause() instanceof InterruptedException) {
_log.warn("{} interrupted.", t.getName());
return;
}
Throwable root = Throwables.getRootCause(e);
if (root instanceof FileNotFoundException) {
if (root.getMessage().contains("Too many open files")) {
_log.error(AlarmMarkerFactory.getMarker(PredefinedAlarm.OUT_OF_FILE_DESCRIPTORS,
getCellDomainName(),
getCellName()),
"Uncaught exception in thread " + t.getName(),
e);
return;
}
}
_log.error("Uncaught exception in thread " + t.getName(), e);
}
}