[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [collector/master] Implements 19776 part4: enable a single run.



commit 8b48ab75b1cc7bd9f52fdc101d31e245a5939174
Author: iwakeh <iwakeh@xxxxxxxxxxxxxx>
Date:   Mon Aug 8 17:00:26 2016 +0200

    Implements 19776 part4: enable a single run.
---
 build.xml                                          |  5 +++-
 .../java/org/torproject/collector/conf/Key.java    |  1 +
 .../org/torproject/collector/cron/Scheduler.java   | 35 +++++++++++++---------
 src/main/resources/collector.properties            |  4 +++
 .../collector/conf/ConfigurationTest.java          |  2 +-
 .../org/torproject/collector/cron/Counter.java     | 26 ++++++++++++++++
 .../torproject/collector/cron/SchedulerTest.java   | 21 ++++++++++++-
 7 files changed, 77 insertions(+), 17 deletions(-)

diff --git a/build.xml b/build.xml
index d21726e..e06bb99 100644
--- a/build.xml
+++ b/build.xml
@@ -240,7 +240,10 @@
     </cobertura-report>
     <cobertura-check totallinerate="5" totalbranchrate="1" >
       <regex pattern="org.torproject.collector.conf.*" branchrate="100" linerate="100"/>
-      <regex pattern="org.torproject.collector.cron" branchrate="66" linerate="80" />
+      <regex pattern="org.torproject.collector.cron.CollecTorMain"
+             branchrate="50" linerate="63" />
+      <regex pattern="org.torproject.collector.cron.Scheduler"
+             branchrate="75" linerate="82" />
       <regex pattern="org.torproject.collector.Main" branchrate="66" linerate="94" />
     </cobertura-check>
   </target>
diff --git a/src/main/java/org/torproject/collector/conf/Key.java b/src/main/java/org/torproject/collector/conf/Key.java
index d8c87ac..a3a9aa2 100644
--- a/src/main/java/org/torproject/collector/conf/Key.java
+++ b/src/main/java/org/torproject/collector/conf/Key.java
@@ -9,6 +9,7 @@ import java.nio.file.Path;
  */
 public enum Key {
 
+  RunOnce(Boolean.class),
   ExitlistOutputDirectory(Path.class),
   ExitlistUrl(URL.class),
   InstanceBaseUrl(String.class),
diff --git a/src/main/java/org/torproject/collector/cron/Scheduler.java b/src/main/java/org/torproject/collector/cron/Scheduler.java
index 25490aa..66fd7be 100644
--- a/src/main/java/org/torproject/collector/cron/Scheduler.java
+++ b/src/main/java/org/torproject/collector/cron/Scheduler.java
@@ -60,7 +60,7 @@ public class Scheduler implements ThreadFactory {
           String prefix = ctmEntry.getKey().name().replace(ACTIVATED, "");
           CollecTorMain ctm = ctmEntry.getValue()
               .getConstructor(Configuration.class).newInstance(conf);
-          scheduleExecutions(ctm,
+          scheduleExecutions(conf.getBool(Key.RunOnce), ctm,
               conf.getInt(Key.valueOf(prefix + OFFSETMIN)),
               conf.getInt(Key.valueOf(prefix + PERIODMIN)));
         }
@@ -76,19 +76,26 @@ public class Scheduler implements ThreadFactory {
 
   private static final long MILLIS_IN_A_MINUTE = 60_000L;
 
-  private void scheduleExecutions(CollecTorMain ctm, int offset, int period) {
-    this.log.info("Periodic updater started for " + ctm.getClass().getName()
-        + "; offset=" + offset + ", period=" + period + ".");
-    long periodMillis = period * MILLIS_IN_A_MINUTE;
-    long initialDelayMillis = computeInitialDelayMillis(
-        System.currentTimeMillis(), offset * MILLIS_IN_A_MINUTE, periodMillis);
-
-    /* Run after initialDelay delay and then every period min. */
-    log.info("Periodic updater will first run in {} and then every {} minutes.",
-        initialDelayMillis < MILLIS_IN_A_MINUTE ? "under 1 minute"
-        : (initialDelayMillis / MILLIS_IN_A_MINUTE) + " minute(s)", period);
-    this.scheduler.scheduleAtFixedRate(ctm, initialDelayMillis, periodMillis,
-        TimeUnit.MILLISECONDS);
+  private void scheduleExecutions(boolean runOnce, CollecTorMain ctm,
+      int offset, int period) {
+    if (runOnce) {
+      this.log.info("Single run for " + ctm.getClass().getName() + ".");
+      this.scheduler.execute(ctm);
+    } else {
+      this.log.info("Periodic updater started for " + ctm.getClass().getName()
+          + "; offset=" + offset + ", period=" + period + ".");
+      long periodMillis = period * MILLIS_IN_A_MINUTE;
+      long initialDelayMillis = computeInitialDelayMillis(
+          System.currentTimeMillis(), offset * MILLIS_IN_A_MINUTE, periodMillis);
+
+      /* Run after initialDelay delay and then every period min. */
+      log.info("Periodic updater will first run in {} and then every {} "
+          + "minutes.", initialDelayMillis < MILLIS_IN_A_MINUTE
+          ? "under 1 minute"
+          : (initialDelayMillis / MILLIS_IN_A_MINUTE) + " minute(s)", period);
+      this.scheduler.scheduleAtFixedRate(ctm, initialDelayMillis, periodMillis,
+          TimeUnit.MILLISECONDS);
+    }
   }
 
   protected static long computeInitialDelayMillis(long currentMillis,
diff --git a/src/main/resources/collector.properties b/src/main/resources/collector.properties
index 61309d0..9ba4c3e 100644
--- a/src/main/resources/collector.properties
+++ b/src/main/resources/collector.properties
@@ -1,6 +1,10 @@
 ######## Collector Properties
 #
 ######## Run Configuration ########
+# If RunOnce=true, the activated modules below will only be
+# run one time and without any delay.
+# Make sure only to run non-interfering modules together.
+RunOnce = false
 ## the following defines, if this module is activated
 BridgedescsActivated = false
 # period in minutes
diff --git a/src/test/java/org/torproject/collector/conf/ConfigurationTest.java b/src/test/java/org/torproject/collector/conf/ConfigurationTest.java
index 8671ef4..2055ae1 100644
--- a/src/test/java/org/torproject/collector/conf/ConfigurationTest.java
+++ b/src/test/java/org/torproject/collector/conf/ConfigurationTest.java
@@ -22,7 +22,7 @@ public class ConfigurationTest {
   public void testKeyCount() throws Exception {
     assertEquals("The number of properties keys in enum Key changed."
         + "\n This test class should be adapted.",
-        48, Key.values().length);
+        49, Key.values().length);
   }
 
   @Test()
diff --git a/src/test/java/org/torproject/collector/cron/Counter.java b/src/test/java/org/torproject/collector/cron/Counter.java
new file mode 100644
index 0000000..3b404b2
--- /dev/null
+++ b/src/test/java/org/torproject/collector/cron/Counter.java
@@ -0,0 +1,26 @@
+package org.torproject.collector.cron;
+
+import org.torproject.collector.conf.Configuration;
+import org.torproject.collector.conf.ConfigurationException;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class Counter extends CollecTorMain {
+
+  static AtomicInteger count = new AtomicInteger(0);
+
+  public Counter(Configuration c) {
+    super(c);
+  }
+
+  @Override
+  public void startProcessing() throws ConfigurationException {
+    count.getAndIncrement();
+  }
+
+  @Override
+  public String module() {
+    return "counter";
+  }
+}
+
diff --git a/src/test/java/org/torproject/collector/cron/SchedulerTest.java b/src/test/java/org/torproject/collector/cron/SchedulerTest.java
index a1b9af1..9ed1ecf 100644
--- a/src/test/java/org/torproject/collector/cron/SchedulerTest.java
+++ b/src/test/java/org/torproject/collector/cron/SchedulerTest.java
@@ -57,7 +57,6 @@ public class SchedulerTest {
         schedulerField.get(Scheduler.getInstance());
     assertTrue(stpe.getQueue().isEmpty());
     Scheduler.getInstance().scheduleModuleRuns(ctms, conf);
-    Scheduler.getInstance().shutdownScheduler();
   }
 
   @Test()
@@ -71,5 +70,25 @@ public class SchedulerTest {
     assertEquals(60_009L,
         Scheduler.computeInitialDelayMillis(299_991L, 60_000L, 300_000L));
   }
+
+  @Test()
+  public void testRunOnce() throws Exception {
+    Map<Key, Class<? extends CollecTorMain>> ctms = new HashMap<>();
+    Configuration conf = new Configuration();
+    conf.load(new ByteArrayInputStream(runConfigProperties.getBytes()));
+    conf.setProperty(Key.RunOnce.name(), "true");
+    ctms.put(Key.TorperfActivated, Counter.class);
+    ctms.put(Key.BridgedescsActivated, Counter.class);
+    ctms.put(Key.RelaydescsActivated, Counter.class);
+    ctms.put(Key.ExitlistsActivated, Counter.class);
+    ctms.put(Key.UpdateindexActivated, Counter.class);
+    Field schedulerField = Scheduler.class.getDeclaredField("scheduler");
+    schedulerField.setAccessible(true);
+    ScheduledThreadPoolExecutor stpe = (ScheduledThreadPoolExecutor)
+        schedulerField.get(Scheduler.getInstance());
+    Scheduler.getInstance().scheduleModuleRuns(ctms, conf);
+    Scheduler.getInstance().shutdownScheduler();
+    assertEquals(5, Counter.count.get());
+  }
 }
 

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits