Commit 0a9f6280 authored by Andre Freyssinet's avatar Andre Freyssinet

JORAM-331: Configuration of the module checking the server status.

parent 35ca6c14
/*
* Copyright (C) 2001 - 2014 ScalAgent Distributed Technologies
* Copyright (C) 2001 - 2019 ScalAgent Distributed Technologies
* Copyright (C) 2004 France Telecom R&D
* Copyright (C) 1996 - 2000 BULL
* Copyright (C) 1996 - 2000 INRIA
......@@ -37,6 +37,8 @@ import java.util.Hashtable;
import java.util.Properties;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
......@@ -362,7 +364,7 @@ public final class AgentServer {
private static short serverId = NULL_ID;
private static Logger logmon = null;
private static Logger logmon = Debug.getLogger(AgentServer.class.getName());
/**
* Name of property allowing to configure the directory to search the XML
......@@ -424,6 +426,24 @@ public final class AgentServer {
*/
public final static String DEFAULT_A3CMLWRP = "fr.dyade.aaa.agent.conf.A3CMLSaxWrapper";
/**
* Name of property allowing to configure the checking period of the server (in ms).
* The corresponding value is the period of time beyond which an error is thrown and the
* registered listeners are called.
* The activation period is 5 times lower, this value cannot be less than 10s.
* <p>
* This property can be fixed either from XML configuration file or Java launching command.
*/
public final static String CFG_CHECK_PERIOD_PROPERTY = "fr.dyade.aaa.agent.check.period";
static CheckServerTask checkServerTask = null;
public static void registerCheckServerListener(CheckServerListener listener) {
if (checkServerTask == null)
logmon.log(BasicLevel.ERROR, getName() + " cannot register CheckServerListener.");
checkServerTask.registerListener(listener);
}
static ThreadGroup tgroup = null;
public static ThreadGroup getThreadGroup() {
......@@ -1474,6 +1494,11 @@ public final class AgentServer {
throw new Exception(t.getMessage());
}
long dterr = getLong(CFG_CHECK_PERIOD_PROPERTY, -1);
if (dterr > 0) {
checkServerTask = new CheckServerTask(dterr);
}
synchronized(status) {
status.value = Status.INITIALIZED;
}
......@@ -1540,6 +1565,10 @@ public final class AgentServer {
// FileMonitoringTimerTask fmtt = new FileMonitoringTimerTask(timer, 1000L, props, "server" + serverId + ".csv");
// logmon.log(BasicLevel.FATAL, getName() + " initialize FileMonitoringTimerTask Ok");
if (checkServerTask != null) {
logmon.log(BasicLevel.WARN, getName() + ", schedule checkServer for " + checkServerTask.dtwarn + "ms.");
timer.scheduleAtFixedRate(checkServerTask, checkServerTask.dtwarn, checkServerTask.dtwarn);
}
} catch (Exception exc) {
logmon.log(BasicLevel.ERROR, getName() + "Cannot start", exc);
synchronized(status) {
......@@ -1780,3 +1809,145 @@ public final class AgentServer {
}
}
}
class CheckServerTask extends TimerTask {
private static Logger logmon = Debug.getLogger(AgentServer.class.getName());
long dtwarn, dterr;
long lastThrow = -1L;
AgentEngine engine = null;
Transaction transaction = null;
Vector<CheckServerListener> listeners = null;
CheckServerTask(long dterr) {
engine = AgentServer.getEngine();
// waitingMessage = (engine.getNbWaitingMessages() > 0);
transaction = AgentServer.getTransaction();
if (dterr < 10000) {
dterr = 10000;
logmon.log(BasicLevel.ERROR, AgentServer.getName() + " checkServer period cannot be less than 10s.");
}
this.dtwarn = dterr / 5;
this.dterr = dterr;
listeners = new Vector<CheckServerListener>();
}
public void registerListener(CheckServerListener listener) {
logmon.log(BasicLevel.WARN, AgentServer.getName() + " registers listener for CheckServer.");
listeners.addElement(listener);
}
private void throwListeners() {
for (CheckServerListener listener : listeners) {
logmon.log(BasicLevel.WARN, AgentServer.getName() + " calls listener for CheckServer.");
listener.onServerCheckError();
}
}
@Override
public void run() {
long now = System.currentTimeMillis();
if ((engine == null) || (transaction == null)) {
// Log an error and throw an alert
logmon.log(BasicLevel.ERROR, AgentServer.getName() + " not initialized.");
throwListeners();
}
if ((checkEngine(now) || checkTransaction(now)) && ((now - lastThrow) > dterr)) {
throwListeners();
lastThrow = now;
}
}
long lastEngineChangeDate = -1L;
long lastEngineWarnDate = -1L;
long reactCount = 0;
private boolean idleEngine() {
return (engine.getRunningAgent() == null) && (engine.getNbWaitingMessages() == 0);
}
private boolean checkEngine(long now) {
logmon.log(BasicLevel.WARN,
AgentServer.getName() + ".checkEngine():" + engine.getNbReactions() + ", " + reactCount);
if (engine.getNbReactions() > reactCount) {
reactCount = engine.getNbReactions();
lastEngineChangeDate = now;
return false;
}
if (! engine.isRunning()) {
// The engine does not run, just log a warn if needed
if (((now - lastEngineChangeDate) > dtwarn) && ((now - lastEngineWarnDate) > dtwarn)) {
logmon.log(BasicLevel.WARN, AgentServer.getName() + ", engine is not started.");
lastEngineWarnDate = now;
}
return false;
}
if (idleEngine()) {
// The engine was idle, just log a warn if needed
if (((now - lastEngineChangeDate) > dtwarn) && ((now - lastEngineWarnDate) > dtwarn)) {
logmon.log(BasicLevel.WARN, AgentServer.getName() + ", engine is idle.");
lastEngineWarnDate = now;
}
return false;
}
if (((now - lastEngineChangeDate) > dterr) && ((now - lastEngineWarnDate) > dtwarn)) {
// Log an error and throw an alert
logmon.log(BasicLevel.ERROR, AgentServer.getName() + ", the engine doesn't progress since " + new Date(lastEngineChangeDate));
lastEngineWarnDate = now;
return true;
} else if (((now - lastEngineChangeDate) > dtwarn) && ((now - lastEngineWarnDate) > dtwarn)) {
// Log a warning
logmon.log(BasicLevel.WARN, AgentServer.getName() + ", the engine doesn't progress since " + new Date(lastEngineChangeDate));
lastEngineWarnDate = now;
return false;
}
return false;
}
int commitCount = 0;
long lastTransactionChangeDate = -1L;
long lastTransactionWarnDate = -1L;
private boolean checkTransaction(long now) {
logmon.log(BasicLevel.WARN,
AgentServer.getName() + ".checkTransaction()" + transaction.getCommitCount() + ", " + commitCount + "," + transaction.getPhase());
if (transaction.getCommitCount() > commitCount) {
commitCount = transaction.getCommitCount();
lastTransactionChangeDate = now;
return false;
}
if (transaction.getPhase() == Transaction.FREE) {
lastTransactionChangeDate = now;
return false;
}
if (((now - lastTransactionChangeDate) > dterr) && ((now - lastTransactionWarnDate) > dtwarn)) {
// Log an error and throw an alert
logmon.log(BasicLevel.ERROR,
AgentServer.getName() + ", Transaction doesn't progress since " + new Date(lastTransactionChangeDate));
lastTransactionWarnDate = now;
} else if (((now - lastTransactionChangeDate) > dtwarn) && ((now - lastTransactionWarnDate) > dtwarn)) {
// Log a warning
logmon.log(BasicLevel.WARN,
AgentServer.getName() + ", Transaction doesn't progress since " + new Date(lastTransactionChangeDate));
lastTransactionWarnDate = now;
}
return true;
}
}
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