Commit 33a23a79 authored by Costin Grigoras's avatar Costin Grigoras
Browse files

Send a last reporting on shutdown, followed by zeroes for *_R

parent f860ee69
......@@ -172,12 +172,23 @@ public class Xrootd extends Protocol {
final ProcessWithTimeout timeout = new ProcessWithTimeout(p, pBuilder);
if (timeout.waitFor(15, TimeUnit.SECONDS) && timeout.exitValue() == 0) {
final String version = timeout.getStdout().toString();
final String version = timeout.getStdout().toString().trim();
logger.log(Level.FINE, "Local Xrootd version is " + version);
if (version.indexOf('.') > 0) {
String tok = version.substring(0, version.indexOf('.')).trim();
if (version.indexOf('.') > 0)
newerThan4 = version.substring(0, version.indexOf('.')).compareTo("v4") >= 0;
while (tok.startsWith("v") || tok.startsWith("\""))
tok = tok.substring(1);
try {
newerThan4 = Integer.parseInt(tok) >= 4;
}
catch (final NumberFormatException nfe) {
logger.log(Level.WARNING, "Unrecognized xrootd version string: " + version, nfe);
}
}
logger.log(Level.FINE, "Local Xrootd version is " + version + ", newer than 4: " + newerThan4);
}
}
catch (final IOException | InterruptedException ie) {
......
......@@ -5,10 +5,10 @@ import java.util.concurrent.atomic.AtomicLong;
/**
* Access counters
*
*
* @author costing
*/
public final class CacheMonitor implements MonitoringObject {
public final class CacheMonitor implements MonitoringObject, DerivedDataProducer {
private final String name;
private final AtomicLong hits = new AtomicLong(0);
......@@ -23,7 +23,7 @@ public final class CacheMonitor implements MonitoringObject {
/**
* Default constructor
*
*
* @param name
*/
public CacheMonitor(final String name) {
......@@ -32,7 +32,7 @@ public final class CacheMonitor implements MonitoringObject {
/**
* Increment the number of hits
*
*
* @return new absolute value of hits
*/
public long incrementHits() {
......@@ -50,7 +50,7 @@ public final class CacheMonitor implements MonitoringObject {
/**
* Increment the number of misses
*
*
* @return new absolute value of misses
*/
public long incrementMisses() {
......@@ -68,7 +68,7 @@ public final class CacheMonitor implements MonitoringObject {
/*
* (non-Javadoc)
*
*
* @see alien.monitoring.MonitoringObject#fillValues(java.util.Vector, java.util.Vector)
*/
@Override
......@@ -158,7 +158,7 @@ public final class CacheMonitor implements MonitoringObject {
/*
* (non-Javadoc)
*
*
* @see java.lang.Object#toString()
*/
@Override
......
......@@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicLong;
*
* @author costing
*/
public final class Counter implements MonitoringObject {
public final class Counter implements MonitoringObject, DerivedDataProducer {
private final AtomicLong counter = new AtomicLong(0);
private long oldValue = 0;
......
package alien.monitoring;
/**
* @author costing
* @since 2021-09-15
*/
public interface DerivedDataProducer {
// empty interface definition
}
......@@ -9,7 +9,7 @@ import java.util.Vector;
* @author costing
* @since Nov 10, 2010
*/
public class Measurement implements MonitoringObject {
public class Measurement implements MonitoringObject, DerivedDataProducer {
private final String name;
......
......@@ -83,7 +83,7 @@ public class Monitor implements Runnable {
if (jobNumber < 0) {
final String pattern = MonitorFactory.getConfigString(component, "node_name",
component.startsWith("alien.site.") ? "${hostname}:${pid}" : "${hostname}");
String temp = Format.replace(pattern, "${hostname}", ConfigUtils.getLocalHostname());
final String temp = Format.replace(pattern, "${hostname}", ConfigUtils.getLocalHostname());
nodeName = Format.replace(temp, "${pid}", String.valueOf(MonitorFactory.getSelfProcessID()));
}
else {
......@@ -145,7 +145,7 @@ public class Monitor implements Runnable {
/**
* Set a new value for a monitoring key
*
*
* @param key what to modify
* @param obj the new value, or <code>null</code> to remove existing associations
* @return the previous value associated to this key
......@@ -275,9 +275,7 @@ public class Monitor implements Runnable {
@Override
public void run() {
final MonitorDataSender sender = MonitorFactory.getMonitorDataSender();
if (sender.isEmpty())
if (!MonitorFactory.canRun())
return;
final List<Object> values = new ArrayList<>();
......@@ -305,17 +303,31 @@ public class Monitor implements Runnable {
sendResults(values);
callMonitoringObjects(false);
}
void callMonitoringObjects(final boolean lastCall) {
if (monitoringObjects.size() > 0) {
final Vector<String> paramNames = new Vector<>(monitoringObjects.size());
final Vector<Object> paramValues = new Vector<>(monitoringObjects.size());
for (final MonitoringObject mo : monitoringObjects.values())
mo.fillValues(paramNames, paramValues);
for (final MonitoringObject mo : monitoringObjects.values()) {
if (!lastCall || mo instanceof DerivedDataProducer)
mo.fillValues(paramNames, paramValues);
}
sendParameters(paramNames, paramValues);
}
}
boolean hasAnyDerivedDataProducer() {
for (final MonitoringObject mo : monitoringObjects.values())
if (mo instanceof DerivedDataProducer)
return true;
return false;
}
/**
* Send a bunch of results
*
......
......@@ -2,7 +2,9 @@ package alien.monitoring;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
......@@ -81,6 +83,13 @@ public final class MonitorFactory {
t.setDaemon(true);
t.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdown();
}
});
}
private MonitorFactory() {
......@@ -129,17 +138,69 @@ public final class MonitorFactory {
/**
* Cancel a monitoring task
*
*
* @param m the Monitor object to stop calling periodically
*/
public static void stopMonitor(final Monitor m) {
synchronized (monitors) {
monitors.values().remove(m);
}
m.future.cancel(false);
if (canRun()) {
// send once more all the parameters
m.run();
if (m.hasAnyDerivedDataProducer()) {
try {
Thread.sleep(100);
}
catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.callMonitoringObjects(true);
}
}
}
private static void shutdown() {
final List<Monitor> toCleanup = new ArrayList<>(monitors.size());
synchronized (monitors) {
toCleanup.addAll(monitors.values());
monitors.clear();
}
for (final Monitor m : toCleanup)
m.future.cancel(false);
if (canRun()) {
final List<Monitor> derivedProducers = new ArrayList<>(toCleanup.size());
for (final Monitor m : toCleanup) {
m.run();
if (m.hasAnyDerivedDataProducer())
derivedProducers.add(m);
}
if (derivedProducers.size() > 0) {
try {
Thread.sleep(1000);
}
catch (final InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (final Monitor m : derivedProducers)
m.callMonitoringObjects(true);
}
}
}
/**
* Enable periodic sending of background host monitoring
*/
......@@ -465,4 +526,31 @@ public final class MonitorFactory {
return selfProcessID;
}
static boolean canRun() {
final MonitorDataSender sender = getMonitorDataSender();
if (sender.isEmpty())
return false;
return true;
}
/**
* Debug method
*
* @param args
* @throws InterruptedException
*/
public static void main(final String[] args) throws InterruptedException {
final Monitor m = getMonitor(MonitorFactory.class.getCanonicalName());
for (int i = 0; i < 15; i++) {
m.addMeasurement("key", 1);
Thread.sleep(1000);
System.err.print(".");
}
System.err.println("\nExiting");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment