ThreadDumpCollector.java
17.1 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.inventory.Format
* org.apache.felix.inventory.InventoryPrinter
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.ConfigurationPolicy
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.PropertyOption
* org.apache.felix.scr.annotations.Reference
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.commons.scheduler.ScheduleOptions
* org.apache.sling.commons.scheduler.Scheduler
* org.osgi.framework.BundleContext
* org.osgi.framework.InvalidSyntaxException
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.threaddump.impl;
import com.adobe.granite.threaddump.impl.BaseThreadDumpManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.FieldPosition;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipOutputStream;
import org.apache.felix.inventory.Format;
import org.apache.felix.inventory.InventoryPrinter;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyOption;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, name="com.adobe.granite.threaddump.ThreadDumpCollector", label="Granite Thread Dumps Collector", description="Collects and persists Java thread dumps inside compressed GZipped files", policy=ConfigurationPolicy.OPTIONAL)
@Properties(value={@Property(name="scheduler.period", longValue={60}, label="Schedule", description="Interval (in number of seconds) between each thread dump - collector will not be executed if this property value is missing"), @Property(name="scheduler.concurrent", boolValue={0}, label="Concurrent", description="Concurrency is not supported at this version, modifying this parameter has no effect in this version!"), @Property(name="scheduler.runOn", options={@PropertyOption(value="Each node", name="SINGLE"), @PropertyOption(value="Leader only", name="LEADER")}, label="Cluster", description="Set 'Each node' to execute this service on multiple nodes within a cluster, 'Leader only' otherwise")})
public final class ThreadDumpCollector
extends BaseThreadDumpManager
implements Runnable {
private static final String USER_NAME_SYSTEM_PROPERTY = "user.name";
private static final String END_OF_DUMP = "\n<EndOfDump>\n\n";
private static final int DEFAULT_DUMPS_PER_FILE_VALUE = 10;
private static final int DEFAULT_MAX_BACKUP_DAYS = 7;
private static final boolean DEFAULT_ENABLE_GZIP_COMPRESSION_VALUE = true;
private static final boolean DEFAULT_ENABLE_DIRECTORIES_COMPRESSION_VALUE = true;
private static final boolean DEFAULT_ENABLE_JSTACK = false;
@Property(label="Enable/Disable", description="Enable or disable the Thread Dumps collection", boolValue={1})
public static String ENABLED = "granite.threaddump.enabled";
@Property(label="Dumps per file", description="Number of dumps to be stored in each file", intValue={10})
public static String DUMPS_PER_FILE = "granite.threaddump.dumpsPerFile";
@Property(label="GZIP Compression", description="Flag to enable/disable GZIP compression on dump files", boolValue={1})
public static String ENABLE_GZIP_COMPRESSION = "granite.threaddump.enableGzipCompression";
@Property(label="Directories Compression", description="Flag to enable/disable ZIP compression on daily dump directories", boolValue={1})
public static String ENABLE_DIRECTORIES_COMPRESSION = "granite.threaddump.enableDirectoriesCompression";
@Property(label="Enable JStack", description="Use native JStack JDK application to perform the thread dump", boolValue={0})
public static String ENABLE_JSTACK = "granite.threaddump.enableJStack";
@Property(label="Max Backup Days", description="The maximum number of backup files/directories to keep around", intValue={7})
public static String MAX_BACKUP_DAYS = "granite.threaddump.maxBackupDays";
private final MessageFormat threadDumpFilterFormat = new MessageFormat("(felix.inventory.printer.name={0,choice,0#|1#jstack-}threaddump)");
private final MessageFormat dumpFileFormat = new MessageFormat("{0,date,yyyyMMdd}{1}threaddump.txt{2,choice,0#|1#.gz}");
private final MessageFormat rolledFileFormat = new MessageFormat("{0}{1}{2}.{3,date,HHmmss}.txt{4,choice,0#|1#.gz}");
private final MessageFormat compressedDirectoryFormat = new MessageFormat("{0,date,yyyyMMdd}{1,choice,0#|1#.zip}");
private final Logger logger;
@Reference
private Scheduler scheduler;
private int dumpsPerFile;
private boolean gzipCompressionEnabled;
private boolean directoriesCompressionEnabled;
private int currentDumpsPerFile;
private int maxBackupDays;
private InventoryPrinter currentThreadDumper;
private Calendar lastCheck;
private GZIPOutputStream currentGzipCompressor;
private PrintWriter currentWriter;
private boolean deactivated;
public ThreadDumpCollector() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.currentDumpsPerFile = 0;
this.deactivated = false;
}
@Activate
protected void activate(ComponentContext context) {
super.activate(context);
Dictionary contextProperties = context.getProperties();
boolean enabled = OsgiUtil.toBoolean(contextProperties.get(ENABLED), (boolean)true);
this.dumpsPerFile = OsgiUtil.toInteger(contextProperties.get(DUMPS_PER_FILE), (int)10);
this.gzipCompressionEnabled = OsgiUtil.toBoolean(contextProperties.get(ENABLE_GZIP_COMPRESSION), (boolean)true);
this.directoriesCompressionEnabled = OsgiUtil.toBoolean(contextProperties.get(ENABLE_DIRECTORIES_COMPRESSION), (boolean)true);
this.maxBackupDays = OsgiUtil.toInteger(contextProperties.get(MAX_BACKUP_DAYS), (int)7);
if (enabled) {
boolean enableJstack = OsgiUtil.toBoolean(contextProperties.get(ENABLE_JSTACK), (boolean)false);
Object[] arrobject = new Object[1];
arrobject[0] = enableJstack ? 1 : 0;
String threadDumpFilter = ThreadDumpCollector.format(this.threadDumpFilterFormat, arrobject);
BundleContext bundleContext = context.getBundleContext();
try {
Collection printers = bundleContext.getServiceReferences(InventoryPrinter.class, threadDumpFilter);
if (printers.isEmpty()) {
this.logger.error("Impossible to retrieve org.apache.felix.inventory.InventoryPrinterservice service reference using filter {}, please check the platform to see if services are correctly registered", (Object)threadDumpFilter);
} else {
this.currentThreadDumper = (InventoryPrinter)bundleContext.getService((ServiceReference)printers.iterator().next());
}
}
catch (InvalidSyntaxException e) {
this.logger.error("Impossible to access to org.apache.felix.inventory.InventoryPrinterservice references", (Throwable)e);
}
long period = OsgiUtil.toLong(contextProperties.get("scheduler.period"), (long)-1);
String runOn = OsgiUtil.toString(contextProperties.get("scheduler.runOn"), (String)"LEADER");
boolean onLeaderOnly = "LEADER".equals(runOn);
boolean canRunConcurrently = OsgiUtil.toBoolean(contextProperties.get("scheduler.concurrent"), (boolean)false);
this.scheduler.schedule((Object)this, this.scheduler.NOW(-1, period).onLeaderOnly(onLeaderOnly).canRunConcurrently(canRunConcurrently).name(this.getClass().getName()));
this.logger.info("Service successfully configured {storingDirectory: {}, dumpsPerFile: {}, gzipCompressionEnabled: {}}", new Object[]{this.getStoringDirectory(), this.dumpsPerFile, this.gzipCompressionEnabled});
} else {
this.logger.info("Service no enabled, it will not dump Threads on the file system");
}
}
@Deactivate
protected void deactivate() {
this.deactivated = true;
this.scheduler.unschedule(this.getClass().getName());
this.logger.info("Service disabled, Threads will not dumped on the file system.");
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void run() {
if (this.currentThreadDumper == null) {
this.logger.error("No org.apache.felix.inventory.InventoryPrinterservice service available, impossible to perform the dump");
return;
}
if (this.deactivated) {
this.closeCurrentOpenStreams();
this.currentGzipCompressor = null;
this.currentWriter = null;
this.deactivated = false;
}
Calendar currentDay = Calendar.getInstance();
String dumpFileName = ThreadDumpCollector.format(this.dumpFileFormat, currentDay.getTime(), File.separator, this.gzipCompressionEnabled());
File dumpFile = new File(this.getStoringDirectory(), dumpFileName);
File parent = dumpFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
this.logger.warn("Impossible to create the {} directory(ies), please check the current user {} has enough File System privileges, execution is skipped.", new Object[]{parent, System.getProperty("user.name")});
return;
}
try {
if (!dumpFile.exists() || this.currentWriter == null) {
this.openCompressedStreams(dumpFile);
} else if (!ThreadDumpCollector.areTheSameDay(this.lastCheck, currentDay)) {
this.cleanMaxBackupDays(currentDay);
this.compressLastCheckedDirectory();
this.rollWriter(dumpFile);
}
this.currentThreadDumper.print(this.currentWriter, Format.TEXT, false);
this.currentWriter.append("\n<EndOfDump>\n\n");
this.currentWriter.flush();
if (this.dumpsPerFile == ++this.currentDumpsPerFile) {
this.rollWriter(dumpFile);
}
}
catch (IOException e) {
this.logger.warn("An error occurred while dumping threads, see root causes.", (Throwable)e);
}
finally {
this.lastCheck = currentDay;
}
}
protected void cleanMaxBackupDays(Calendar current) {
Calendar start = (Calendar)current.clone();
start.set(5, start.get(5) - this.maxBackupDays);
this.logger.info("Cleaning backup(s) older than {} days, starting from {}...", (Object)this.maxBackupDays, (Object)start.getTime());
this.deleteBackup(start);
this.logger.info("All backup(s) successfully removed.");
}
private void deleteBackup(Calendar current) {
String dirName = ThreadDumpCollector.format(this.compressedDirectoryFormat, current.getTime(), 0);
File toBeRemoved = new File(this.getStoringDirectory(), dirName);
if (!toBeRemoved.exists()) {
String compressedFileName = ThreadDumpCollector.format(this.compressedDirectoryFormat, current.getTime(), 1);
toBeRemoved = new File(this.getStoringDirectory(), compressedFileName);
}
if (toBeRemoved.exists()) {
this.logger.info("Cleaning old backup stored in {}...", (Object)toBeRemoved);
ThreadDumpCollector.delete(toBeRemoved);
current.set(5, current.get(5) - 1);
this.deleteBackup(current);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void compressLastCheckedDirectory() {
if (!this.directoriesCompressionEnabled) {
return;
}
String sourceFileName = ThreadDumpCollector.format(this.compressedDirectoryFormat, this.lastCheck.getTime(), 0);
File source = new File(this.getStoringDirectory(), sourceFileName);
if (!source.exists()) {
return;
}
String targetFileName = ThreadDumpCollector.format(this.compressedDirectoryFormat, this.lastCheck.getTime(), 1);
File target = new File(this.getStoringDirectory(), targetFileName);
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(target);
zos = new ZipOutputStream(fos);
ThreadDumpCollector.addToZip(zos, this.getStoringDirectory(), source);
zos.finish();
}
catch (IOException e) {
this.logger.error("An error occurred while compressing the {} directory to the {} file, see causing errors: {}", new Object[]{source, target, e});
}
finally {
ThreadDumpCollector.closeQuietly(fos);
ThreadDumpCollector.closeQuietly(zos);
ThreadDumpCollector.delete(source);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void rollWriter(File source) throws IOException {
this.closeCurrentOpenStreams();
String sourceFileName = source.getName();
sourceFileName = sourceFileName.substring(0, sourceFileName.indexOf(46));
String targetFileName = ThreadDumpCollector.format(this.rolledFileFormat, source.getParent(), File.separator, sourceFileName, Calendar.getInstance().getTime(), this.gzipCompressionEnabled());
File target = new File(targetFileName);
if (!source.renameTo(target)) {
this.logger.debug("Impossible to move {} file to {} using renameTo() API, falling back to manual copy.", new Object[]{source, target});
boolean error = false;
try {
ThreadDumpCollector.copyContent(source, new FileOutputStream(target), true);
}
catch (IOException e) {
this.logger.warn("An error occurred while moving {} content to {}, please see the stacktrace: {}", new Object[]{source, target, e});
error = true;
}
finally {
if (!error) {
source.delete();
}
}
}
this.currentDumpsPerFile = 0;
this.openCompressedStreams(source);
}
private void openCompressedStreams(File target) throws IOException {
FileOutputStream fos = new FileOutputStream(target, true);
if (this.gzipCompressionEnabled) {
this.currentGzipCompressor = new GZIPOutputStream(fos);
this.currentWriter = new PrintWriter(this.currentGzipCompressor, true);
} else {
this.currentGzipCompressor = null;
this.currentWriter = new PrintWriter(fos, true);
}
}
private void closeCurrentOpenStreams() {
if (this.currentGzipCompressor != null) {
try {
this.currentGzipCompressor.finish();
}
catch (IOException e) {
this.logger.warn("An error occurred while finalizing gzip compression, see thrown exceptions", (Throwable)e);
}
}
ThreadDumpCollector.closeQuietly(this.currentGzipCompressor);
ThreadDumpCollector.closeQuietly(this.currentWriter);
}
private int gzipCompressionEnabled() {
if (this.gzipCompressionEnabled) {
return 1;
}
return 0;
}
private static /* varargs */ String format(MessageFormat format, Object ... arguments) {
StringBuffer result = new StringBuffer();
format.format(arguments, result, new FieldPosition(0));
return result.toString();
}
private static boolean areTheSameDay(Calendar lastCheck, Calendar currentDay) {
if (lastCheck == null) {
return true;
}
return lastCheck.get(1) == currentDay.get(1) && lastCheck.get(6) == currentDay.get(6);
}
private static void delete(File file) {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
ThreadDumpCollector.delete(child);
}
}
file.delete();
}
protected void bindScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
protected void unbindScheduler(Scheduler scheduler) {
if (this.scheduler == scheduler) {
this.scheduler = null;
}
}
}