perfsonar-dev - perfsonar: r5034 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service: configuration scheduler
Subject: perfsonar development work
List archive
perfsonar: r5034 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service: configuration scheduler
Chronological Thread
- From:
- To:
- Subject: perfsonar: r5034 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service: configuration scheduler
- Date: Fri, 6 Mar 2009 10:20:28 -0500
Author: mac
Date: 2009-03-06 10:20:27 -0500 (Fri, 06 Mar 2009)
New Revision: 5034
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/Actions.java
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/SchedulingComponent.java
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/Scheduler.java
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulingComponent.java
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SimpleScheduler.java
Log:
Changes in Scheduler.
Added optional separate "interval" for <action>
Some logging added
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/Actions.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/Actions.java
2009-03-06 10:15:54 UTC (rev 5033)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/Actions.java
2009-03-06 15:20:27 UTC (rev 5034)
@@ -7,9 +7,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
-import org.apache.log4j.Logger;
-
public class Actions extends Entry {
@@ -40,7 +38,7 @@
return ((Action)actions.get(name));
}
- public Map getActions() {
+ public Map<String,Action> getActions() {
return actions;
}
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/SchedulingComponent.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/SchedulingComponent.java
2009-03-06 10:15:54 UTC (rev 5033)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/configuration/SchedulingComponent.java
2009-03-06 15:20:27 UTC (rev 5034)
@@ -4,13 +4,9 @@
*/
package org.perfsonar.base2.service.configuration;
-import java.util.LinkedHashMap;
-import java.util.Map;
import org.perfsonar.base2.service.exceptions.PerfSONARException;
-import org.apache.log4j.Logger;
-
public abstract class SchedulingComponent extends Component {
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/Scheduler.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/Scheduler.java
2009-03-06 10:15:54 UTC (rev 5033)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/Scheduler.java
2009-03-06 15:20:27 UTC (rev 5034)
@@ -1,12 +1,10 @@
package org.perfsonar.base2.service.scheduler;
-import org.perfsonar.base2.service.configuration.Component;
-import org.perfsonar.base2.service.exceptions.PerfSONARException;
-
-
+/**
+ * Scheduler interface
+ */
public interface Scheduler {
-
public void start();
public void finish();
@@ -21,5 +19,4 @@
public void removeTask(SchedulerAction action);
-
} //Scheduler
\ No newline at end of file
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
2009-03-06 10:15:54 UTC (rev 5033)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
2009-03-06 15:20:27 UTC (rev 5034)
@@ -1,17 +1,20 @@
package org.perfsonar.base2.service.scheduler;
+import org.apache.log4j.Logger;
+
public class SchedulerTask {
-
+ protected static final Logger logger =
Logger.getLogger(SchedulerTask.class.getName());
+
// --------------------------------------------- Variables
- private long MIN_INTERVAL = 100; //ms
+ private long MIN_INTERVAL = 1000; //ms
private long interval = MIN_INTERVAL; //minimal interval (in ms)
between actions
- private long timestamp = 0; // last timestamp when action was performed
+ private long timestamp = 0; // last timestamp when action was
performed; 0 - never
private SchedulerAction action;
@@ -21,9 +24,13 @@
public SchedulerTask(long interval, SchedulerAction action) {
- if (interval<MIN_INTERVAL) this.interval = MIN_INTERVAL;
- else this.interval = interval;
-
+ if (interval<MIN_INTERVAL) {
+ logger.debug("Requested interval for action
["+action.getClass()+"] too low. Taking ["+MIN_INTERVAL+"] ms");
+ this.interval = MIN_INTERVAL;
+ } else {
+ logger.debug("Interval for action ["+action.getClass()+"] set to
["+interval+"] ms");
+ this.interval = interval;
+ }
this.action = action;
this.timestamp = 0;
@@ -56,12 +63,17 @@
public boolean runAction() {
long currentTimestamp = System.currentTimeMillis();
+
+ logger.debug("Scheduler Task ["+this.getClass()+"] - interval
["+interval+"]" );
//if timestamp older than current - interval
if ((timestamp + interval - currentTimestamp) <= 0) {
+ logger.debug("Scheduler run action ["+action.getClass()+"]" );
+
timestamp = currentTimestamp;
+ //TODO: to be changed - should be thread pool?
//run action in thread
new Thread() {
@Override
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulingComponent.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulingComponent.java
2009-03-06 10:15:54 UTC (rev 5033)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulingComponent.java
2009-03-06 15:20:27 UTC (rev 5034)
@@ -1,29 +1,27 @@
package org.perfsonar.base2.service.scheduler;
-import org.perfsonar.base2.service.exceptions.PerfSONARException;
-
-import org.apache.log4j.Logger;
-import java.util.Map;
import java.util.Collection;
-public class SchedulingComponent extends
org.perfsonar.base2.service.configuration.SchedulingComponent {
+import org.perfsonar.base2.service.configuration.Action;
+import org.perfsonar.base2.service.exceptions.PerfSONARException;
+public class SchedulingComponent extends
+ org.perfsonar.base2.service.configuration.SchedulingComponent {
// ----------------------------------------------------------- Variables
-
private Scheduler scheduler = null;
private String schedulerClassName = null;
private int interval;
- public static final int DEFAULT_INTERVAL = 10000; //milliseconds
+ public static final int DEFAULT_INTERVAL = 10000; // milliseconds
+
// ----------------------------------------------------------- Methods
-
@Override
public void init() throws PerfSONARException {
@@ -49,9 +47,9 @@
try {
interval = Integer.parseInt(intervalStr);
} catch (NumberFormatException nfex) {
- logger.warn(
- "Can not get correct interval value for scheduling from the
configuration. "
- + " Default value is taken.");
+ logger
+ .warn("Can not get correct interval value for scheduling
from the configuration. "
+ + " Default value is taken.");
interval = DEFAULT_INTERVAL;
}
}
@@ -60,10 +58,11 @@
protected Scheduler createScheduler() throws PerfSONARException {
try {
- scheduler =
(Scheduler)Class.forName(schedulerClassName).newInstance();
+ scheduler = (Scheduler) Class.forName(schedulerClassName)
+ .newInstance();
registerActions();
} catch (Exception e) {
- throw new PerfSONARException("wrong_data_storage",
+ throw new
PerfSONARException("error/scheduler/wrong_data_storage",
"\n Cannot instantiate Scheduler [" + e.getMessage());
}
@@ -77,8 +76,8 @@
scheduler.setInterval(interval);
scheduler.start();
} else
- logger.error(
- "Can not start the scheduler because it does not exist");
+ logger
+ .error("Can not start the scheduler because it does not
exist");
}
@@ -87,8 +86,7 @@
if (scheduler != null)
scheduler.finish();
else
- logger.warn(
- "Can not stop the scheduler because it does not exist");
+ logger.warn("Can not stop the scheduler because it does not
exist");
}
@@ -100,7 +98,7 @@
public void setScheduler(Scheduler scheduler) {
- this.scheduler = scheduler;
+ this.scheduler = scheduler;
}
@@ -112,23 +110,40 @@
public void setInterval(int interval) {
- this.interval = interval;
+ this.interval = interval;
}
public void registerActions() {
- logger.debug("start of registering schedulling tasks");
+ logger.debug("Start of registering schedulling tasks. Default
interval is ["+interval+"] ms");
if (scheduler != null) {
- Collection<org.perfsonar.base2.service.configuration.Action>
actions = getActions().getActions().values();
- for (org.perfsonar.base2.service.configuration.Action action:
actions) {
- scheduler.addTask(interval, (SchedulerAction)action);
- logger.debug("Action " + action.getName() + " registered to
the scheduler");
+
+ Collection<Action> actions = getActions().getActions().values();
+
+ for (Action action : actions) {
+ int actionInterval = this.interval;
+ try {
+ // get option interval from action and try to convert it
to
+ // int
+
+ logger.debug(action.getOptions());
+ actionInterval = Integer.parseInt(action.getOption(
+ "interval").getValue());
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ scheduler.addTask(actionInterval, (SchedulerAction) action);
+
+ logger.debug("Action [" + action.getName() + "] class ["
+ + action.getClass()
+ + "] registered to the scheduler with interval ["
+ + actionInterval + "]");
}
}
}
-
-} //SchedulingComponent
\ No newline at end of file
+} // SchedulingComponent
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SimpleScheduler.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SimpleScheduler.java
2009-03-06 10:15:54 UTC (rev 5033)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SimpleScheduler.java
2009-03-06 15:20:27 UTC (rev 5034)
@@ -1,15 +1,16 @@
package org.perfsonar.base2.service.scheduler;
-import org.perfsonar.base2.service.exceptions.PerfSONARException;
-
import java.util.Collections;
+import java.util.ConcurrentModificationException;
+import java.util.HashSet;
import java.util.Set;
-import java.util.HashSet;
-import java.util.ConcurrentModificationException;
import org.apache.log4j.Logger;
-
+/**
+ * Simple implementation of Scheduler for perfSONAR services
+ *
+ */
public class SimpleScheduler extends Thread implements Scheduler {
@@ -47,17 +48,20 @@
public void run() {
+ int iteration = 0;
paused = false;
- try { Thread.sleep(startDelayTime); } catch (Exception ex) {
logger.warn(ex.toString());}
+ try {
+ logger.info("Scheduler run. Waiting ["+startDelayTime+"] ms for
initialization of other components...");
+ Thread.sleep(startDelayTime);
+ } catch (Exception ex) { logger.warn(ex.toString());}
while (working) { //infinite loop until stopScheduler()
try {
- //Thread.sleep(interval);
-
//if paused - not schedule any tasks
if (!paused) {
+ logger.debug("Scheduler iteration [#"+iteration+"].
Running ["+tasks.size()+"] Scheduled Tasks");
// Let's protect iteration over tasks by
// corrupted iterator exception catcher,
// which prevents this thread from dieing
@@ -74,8 +78,12 @@
}
- Thread.sleep(interval);
+ //logger.debug("Scheduler waits ["+interval+"] ms");
+ Thread.sleep(interval); //wait
+
+ iteration++; //increase iteration counter
+
} catch (InterruptedException e) {
logger.warn("Sleep error");
}
- perfsonar: r5034 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service: configuration scheduler, svnlog, 03/06/2009
Archive powered by MHonArc 2.6.16.