-
Notifications
You must be signed in to change notification settings - Fork 361
/
SystemResourceMonitors.java
465 lines (425 loc) · 11.3 KB
/
SystemResourceMonitors.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
/********************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
********************************************************************************/
package org.eclipse.californium.elements.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Monitors for system resource.
*
* Check systems resource frequently for changes.
*
* @since 3.8
*/
public class SystemResourceMonitors {
/**
* Logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(SystemResourceMonitors.class);
/**
* List of system resource jobs.
*/
private final List<SystemResourceJob> resources = new ArrayList<>();
/**
* Scheduler for jobs.
*/
private final ScheduledExecutorService scheduler;
/**
* State indicator. {@code true} after {@link #start()}, {@code false} after
* {@link #stop()} or before {@link #start()}.
*/
private final AtomicBoolean running = new AtomicBoolean();
/**
* Creates instance.
*
* @param scheduler scheduler to schedule checking jobs.
*/
public SystemResourceMonitors(ScheduledExecutorService scheduler) {
this.scheduler = scheduler;
}
/**
* Add system resource monitor.
*
* @param name name of monitor
* @param interval interval for checks
* @param unit time unit for checks
* @param resource system resource monitor used for check.
* @return created check-job.
* @throws NullPointerException if name or resource is {@code null}.
* @throws IllegalArgumentException if interval is {@code <= 0}.
*/
public SystemResourceJob addMonitor(String name, long interval, TimeUnit unit, SystemResourceMonitor resource) {
if (resource == null) {
throw new NullPointerException("Resource monitor must not be null!");
}
if (name == null) {
throw new NullPointerException("Name must not be null!");
}
if (interval <= 0) {
throw new IllegalArgumentException("Interval must be > 0!");
}
SystemResourceJob job = new SystemResourceJob(scheduler, name, interval, unit, resource);
resources.add(job);
if (running.get()) {
job.start();
} else {
LOGGER.info("{} added scheduled in {} {}.", name, interval, unit);
}
return job;
}
/**
* Add system resource monitor.
*
* @param name name of monitor
* @param interval interval for checks. If {@code <= 0} no check-job is
* added.
* @param unit time unit for checks
* @param resource system resource monitor used for check. If {@code null},
* no check-job is added.
* @return created check-job, or {@code null}, if the provided resource was
* {@code null}.
* @throws NullPointerException if name is {@code null}.
* @since 3.12
*/
public SystemResourceJob addOptionalMonitor(String name, long interval, TimeUnit unit,
SystemResourceMonitor resource) {
if (resource != null && interval > 0) {
return addMonitor(name, interval, unit, resource);
} else {
return null;
}
}
/**
* Remove system resource monitor job.
*
* @param job system resource monitor check-job to remove
* @return {@code true}, if removed, {@code false}, if not contained.
*/
public boolean remove(SystemResourceJob job) {
if (resources.remove(job)) {
job.stop();
return true;
}
return false;
}
/**
* Remove all system resource monitor check-jobs by name.
*
* @param name name of check-jobs, or {@code null} for all check-jobs.
* @return {@code true}, if at least one check-job is removed,
* {@code false}, if no check-job is removed.
*/
public boolean remove(String name) {
boolean removed = false;
Iterator<SystemResourceJob> iterator = resources.iterator();
while (iterator.hasNext()) {
SystemResourceJob job = iterator.next();
if (name == null || name.equals(job.name)) {
job.stop();
iterator.remove();
removed = true;
}
}
return removed;
}
/**
* Start checks.
*/
public void start() {
if (running.compareAndSet(false, true)) {
for (SystemResourceJob job : resources) {
job.start();
}
LOGGER.info("System resource monitor started!");
}
}
/**
* Stop checks.
*/
public void stop() {
if (running.compareAndSet(true, false)) {
for (SystemResourceJob job : resources) {
job.stop();
}
LOGGER.info("System resource monitor stopped!");
}
}
/**
* Apply checks.
*
* @param name name of check-jobs, or {@code null} for all check-jobs.
*/
public void checkNow(String name) {
for (SystemResourceJob job : resources) {
if (name == null || name.equals(job.name)) {
job.checkNow();
}
}
LOGGER.info("System resource monitor checked now!");
}
/**
* Interface to report result.
*/
public interface SystemResourceCheckReady {
/**
* Report finished check-job.
*
* @param stop {@code true}, stop check-job. {@code false}, schedule
* next check.
*/
void ready(boolean stop);
}
/**
* Job to check system resource changes.
*/
public static class SystemResourceJob implements Runnable, SystemResourceCheckReady {
/**
* Name of job.
*/
private final String name;
/**
* Interval of check-job.
*/
private final long interval;
/**
* Time unit of interval.
*/
private final TimeUnit unit;
/**
* System resource monitor.
*/
private final SystemResourceMonitor resource;
/**
* Scheduler for check-job interval.
*/
private final ScheduledExecutorService scheduler;
/**
* Future of scheduled job.
*/
private final AtomicReference<ScheduledFuture<?>> scheduled = new AtomicReference<>();
/**
* Indicates pending check.
*/
private final AtomicBoolean pending = new AtomicBoolean();
/**
* Create check-job.
*
* @param scheduler scheduler to be used.
* @param name name of job
* @param interval interval of checks
* @param unit unit of interval
* @param resource system resource monitor used for check.
*/
private SystemResourceJob(ScheduledExecutorService scheduler, String name, long interval, TimeUnit unit,
SystemResourceMonitor resource) {
this.scheduler = scheduler;
this.name = name;
this.interval = interval;
this.unit = unit;
this.resource = resource;
}
/**
* Stop check-job.
*/
private synchronized void stop() {
ScheduledFuture<?> future = scheduled.getAndSet(null);
if (future != null) {
future.cancel(false);
}
}
/**
* Start check-job.
*/
private synchronized void start() {
ScheduledFuture<?> future = scheduled.getAndSet(scheduler.schedule(this, interval, unit));
if (future != null) {
future.cancel(false);
}
LOGGER.info("{} check scheduled in {} {}.", name, interval, unit);
}
/**
* Execute check-job now.
*/
private void checkNow() {
if (pending.compareAndSet(false, true)) {
try {
ScheduledFuture<?> future = scheduled.get();
if (future != null) {
future.cancel(false);
}
resource.checkForUpdate(this);
} catch (RuntimeException ex) {
LOGGER.info("{} unexpected error!", name, ex);
ready(false);
}
}
}
/**
* Execute scheduled check-job.
*/
private void check() {
if (pending.compareAndSet(false, true)) {
try {
LOGGER.info("{} check for update!", name);
resource.checkForUpdate(this);
} catch (RuntimeException ex) {
LOGGER.info("{} unexpected error!", name, ex);
ready(false);
}
} else {
LOGGER.info("{} check for update pending!", name);
}
}
@Override
public void run() {
check();
}
@Override
public void ready(boolean stop) {
pending.set(false);
if (stop) {
LOGGER.info("{} reports finished!", name);
} else {
LOGGER.info("{} reports ready!", name);
}
synchronized (this) {
ScheduledFuture<?> future = scheduled.get();
if (future != null) {
if (stop) {
future.cancel(false);
} else {
start();
}
}
}
}
}
/**
* System resource monitor.
*/
public interface SystemResourceMonitor {
/**
* Check resource for update.
*
* @param ready {@code true}
*/
void checkForUpdate(SystemResourceCheckReady ready);
}
/**
* File monitor.
*
* Checks for changes in length and last modified date.
*/
public static abstract class FileMonitor implements SystemResourceMonitor {
/**
* Monitored values.
*/
public static class MonitoredValues {
private final long lastModified;
private final long length;
private MonitoredValues(long lastModified, long length) {
this.lastModified = lastModified;
this.length = length;
}
/**
* Check monitored values.
*
* @param other other monitored values
* @return {@code true}, if other monitored values differ,
* {@code false}, if other monitored values are equal.
*/
private boolean check(MonitoredValues other) {
return lastModified != other.lastModified || length != other.length;
}
}
/**
* File to monitor.
*/
private final File file;
/**
* Last monitored values when reading the file.
*/
private volatile MonitoredValues values;
/**
* Create file monitor.
*
* @param name name of file to monitor
*/
public FileMonitor(String name) {
this(new File(name));
}
/**
* Create file monitor.
*
* @param file file to monitor
*/
public FileMonitor(File file) {
this.file = file;
this.values = readMonitoredValues();
}
/**
* Read monitored values.
*
* @return monitored values.
*/
private MonitoredValues readMonitoredValues() {
long length = file.length();
long lastModified = file.lastModified();
return new MonitoredValues(lastModified, length);
}
@Override
public void checkForUpdate(SystemResourceCheckReady ready) {
MonitoredValues values = readMonitoredValues();
if (this.values.check(values)) {
try {
LOGGER.info("File {} changed!", file);
update(values, ready);
} catch (RuntimeException ex) {
ready(values);
throw ex;
}
} else {
LOGGER.info("File {} unchanged.", file);
ready.ready(false);
}
}
/**
* Report update ready.
*
* @param values monitored values passed to {@link #update}.
* {@code null}, if monitored values should not be updated.
*/
protected void ready(MonitoredValues values) {
if (values != null) {
this.values = values;
}
}
/**
* Called, when monitored values differs.
*
* @param values different monitored values
* @param ready callback to report finished update
*/
protected abstract void update(MonitoredValues values, SystemResourceCheckReady ready);
}
}