aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kevi...@apache.org
Subject [1/2] git commit: Refactor CronScheduler interfaces to use Guava Service.
Date Wed, 05 Feb 2014 17:49:36 GMT
Updated Branches:
  refs/heads/kts/AURORA-132 676f2b151 -> 874ac7eb5 (forced update)


Refactor CronScheduler interfaces to use Guava Service.


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/33d2c76b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/33d2c76b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/33d2c76b

Branch: refs/heads/kts/AURORA-132
Commit: 33d2c76b93d7da8924bfbfad48bb1e39b58a1337
Parents: 80106ee
Author: Kevin Sweeney <kevints@apache.org>
Authored: Wed Feb 5 09:37:19 2014 -0800
Committer: Kevin Sweeney <kevints@apache.org>
Committed: Wed Feb 5 09:38:18 2014 -0800

----------------------------------------------------------------------
 .../aurora/scheduler/cron/CronScheduler.java    | 23 ++----------------
 .../scheduler/cron/noop/NoopCronScheduler.java  | 24 +++++++++----------
 .../scheduler/cron/testing/AbstractCronIT.java  | 25 +++++++++-----------
 .../aurora/scheduler/state/CronJobManager.java  | 10 ++++----
 .../aurora/scheduler/cron/noop/NoopCronIT.java  |  4 ++--
 .../scheduler/state/CronJobManagerTest.java     |  9 ++++---
 6 files changed, 38 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/33d2c76b/src/main/java/org/apache/aurora/scheduler/cron/CronScheduler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/cron/CronScheduler.java b/src/main/java/org/apache/aurora/scheduler/cron/CronScheduler.java
index 34e1749..56e9950 100644
--- a/src/main/java/org/apache/aurora/scheduler/cron/CronScheduler.java
+++ b/src/main/java/org/apache/aurora/scheduler/cron/CronScheduler.java
@@ -18,11 +18,12 @@ package org.apache.aurora.scheduler.cron;
 import javax.annotation.Nullable;
 
 import com.google.common.base.Optional;
+import com.google.common.util.concurrent.Service;
 
 /**
  * An execution manager that executes work on a cron schedule.
  */
-public interface CronScheduler {
+public interface CronScheduler extends Service {
   /**
    * Schedules a task on a cron schedule.
    *
@@ -53,26 +54,6 @@ public interface CronScheduler {
   Optional<String> getSchedule(String key) throws IllegalStateException;
 
   /**
-   * Block until fully initialized. It is an error to call start twice. Prior to calling
start,
-   * all other methods of this interface may throw {@link IllegalStateException}. The underlying
-   * implementation should not spawn threads or connect to databases prior to invocation
of
-   * {@link #start()}.
-   *
-   * @throws IllegalStateException If called twice.
-   */
-  void start() throws IllegalStateException;
-
-  /**
-   * Block until stopped. Generally this means that underlying resources are freed, threads
are
-   * terminated, and any bookkeeping state is persisted. If {@link #stop()} has already been
called
-   * by another thread, {@link #stop()} either blocks until completion or returns immediately.
-   *
-   * @throws CronException If there was a problem stopping the scheduler, for example if
it was not
-   *                       started.
-   */
-  void stop() throws CronException;
-
-  /**
    * Checks to see if the scheduler would be accepted by the underlying scheduler.
    *
    * @param schedule Cron scheduler to validate.

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/33d2c76b/src/main/java/org/apache/aurora/scheduler/cron/noop/NoopCronScheduler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/cron/noop/NoopCronScheduler.java b/src/main/java/org/apache/aurora/scheduler/cron/noop/NoopCronScheduler.java
index bbee5ef..a31551c 100644
--- a/src/main/java/org/apache/aurora/scheduler/cron/noop/NoopCronScheduler.java
+++ b/src/main/java/org/apache/aurora/scheduler/cron/noop/NoopCronScheduler.java
@@ -23,8 +23,8 @@ import javax.annotation.Nullable;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.Sets;
+import com.google.common.util.concurrent.AbstractIdleService;
 
-import org.apache.aurora.scheduler.cron.CronException;
 import org.apache.aurora.scheduler.cron.CronScheduler;
 
 /**
@@ -35,13 +35,23 @@ import org.apache.aurora.scheduler.cron.CronScheduler;
  * This class exists as a short term hack to get around a license compatibility issue - Real
  * Implementation (TM) coming soon.
  */
-class NoopCronScheduler implements CronScheduler {
+class NoopCronScheduler extends AbstractIdleService implements CronScheduler {
   private static final Logger LOG = Logger.getLogger(NoopCronScheduler.class.getName());
 
   // Keep a list of schedules we've seen.
   private final Set<String> schedules = Collections.synchronizedSet(Sets.<String>newHashSet());
 
   @Override
+  public void startUp() throws Exception {
+    LOG.warning("NO-OP cron scheduler is in use. Cron jobs submitted will not be triggered!");
+  }
+
+  @Override
+  public void shutDown() {
+    // No-op.
+  }
+
+  @Override
   public String schedule(String schedule, Runnable task) {
     schedules.add(schedule);
 
@@ -66,16 +76,6 @@ class NoopCronScheduler implements CronScheduler {
   }
 
   @Override
-  public void start() throws IllegalStateException {
-    LOG.warning("NO-OP cron scheduler is in use. Cron jobs submitted will not be triggered!");
-  }
-
-  @Override
-  public void stop() throws CronException {
-    // No-op.
-  }
-
-  @Override
   public boolean isValidSchedule(@Nullable String schedule) {
     // Accept everything.
     return schedule != null;

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/33d2c76b/src/main/java/org/apache/aurora/scheduler/cron/testing/AbstractCronIT.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/cron/testing/AbstractCronIT.java b/src/main/java/org/apache/aurora/scheduler/cron/testing/AbstractCronIT.java
index 54b6fab..175400a 100644
--- a/src/main/java/org/apache/aurora/scheduler/cron/testing/AbstractCronIT.java
+++ b/src/main/java/org/apache/aurora/scheduler/cron/testing/AbstractCronIT.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright 2013 Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +15,7 @@
  */
 package org.apache.aurora.scheduler.cron.testing;
 
+import java.util.Collection;
 import java.util.concurrent.CountDownLatch;
 
 import com.twitter.common.testing.easymock.EasyMockTest;
@@ -23,7 +24,6 @@ import org.apache.aurora.scheduler.cron.CronPredictor;
 import org.apache.aurora.scheduler.cron.CronScheduler;
 import org.junit.Test;
 
-import static org.apache.aurora.gen.test.testConstants.VALID_CRON_SCHEDULES;
 import static org.junit.Assert.assertTrue;
 
 /**
@@ -36,14 +36,14 @@ public abstract class AbstractCronIT extends EasyMockTest {
   protected abstract CronScheduler makeCronScheduler() throws Exception;
 
   /**
-   * Child should configure expectations for a scheduler start.
+   * Child should return cron expressions that are expected to pass validation.
    */
-  protected abstract void expectStartCronScheduler();
+  protected abstract Collection<String> getValidCronSchedules();
 
   /**
-   * Child should configure expectations for a scheduler stop.
+   * Child should return a "wildcard" cron expression that executes at every possible moment.
    */
-  protected abstract void expectStopCronScheduler();
+  protected abstract String getWildcardCronSchedule();
 
   /**
    * Child should return an instance of the {@link CronPredictor} under test here.
@@ -54,20 +54,17 @@ public abstract class AbstractCronIT extends EasyMockTest {
   public void testCronSchedulerLifecycle() throws Exception {
     CronScheduler scheduler = makeCronScheduler();
 
-    expectStartCronScheduler();
-    expectStopCronScheduler();
-
     control.replay();
 
-    scheduler.start();
+    scheduler.startAsync().awaitRunning();
     final CountDownLatch cronRan = new CountDownLatch(1);
-    scheduler.schedule("* * * * *", new Runnable() {
+    scheduler.schedule(getWildcardCronSchedule(), new Runnable() {
       @Override public void run() {
         cronRan.countDown();
       }
     });
     cronRan.await();
-    scheduler.stop();
+    scheduler.stopAsync().awaitTerminated();
   }
 
   @Test
@@ -75,7 +72,7 @@ public abstract class AbstractCronIT extends EasyMockTest {
     control.replay();
 
     CronPredictor cronPredictor = makeCronPredictor();
-    for (String schedule : VALID_CRON_SCHEDULES) {
+    for (String schedule : getValidCronSchedules()) {
       cronPredictor.predictNextRun(schedule);
     }
   }
@@ -86,7 +83,7 @@ public abstract class AbstractCronIT extends EasyMockTest {
 
     control.replay();
 
-    for (String schedule : VALID_CRON_SCHEDULES) {
+    for (String schedule : getValidCronSchedules()) {
       assertTrue(String.format("Cron schedule %s should validate.", schedule),
           cron.isValidSchedule(schedule));
     }

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/33d2c76b/src/main/java/org/apache/aurora/scheduler/state/CronJobManager.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/state/CronJobManager.java b/src/main/java/org/apache/aurora/scheduler/state/CronJobManager.java
index 371addf..6c74348 100644
--- a/src/main/java/org/apache/aurora/scheduler/state/CronJobManager.java
+++ b/src/main/java/org/apache/aurora/scheduler/state/CronJobManager.java
@@ -40,7 +40,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.twitter.common.application.ShutdownRegistry;
 import com.twitter.common.args.Arg;
 import com.twitter.common.args.CmdLine;
-import com.twitter.common.base.ExceptionalCommand;
+import com.twitter.common.base.Command;
 import com.twitter.common.base.Supplier;
 import com.twitter.common.quantity.Amount;
 import com.twitter.common.quantity.Time;
@@ -165,10 +165,10 @@ public class CronJobManager extends JobManager implements EventSubscriber
{
    */
   @Subscribe
   public void schedulerActive(SchedulerActive schedulerActive) {
-    cron.start();
-    shutdownRegistry.addAction(new ExceptionalCommand<CronException>() {
-      @Override public void execute() throws CronException {
-        cron.stop();
+    cron.startAsync().awaitRunning();
+    shutdownRegistry.addAction(new Command() {
+      @Override public void execute() {
+        cron.stopAsync().awaitTerminated();
       }
     });
 

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/33d2c76b/src/test/java/org/apache/aurora/scheduler/cron/noop/NoopCronIT.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/cron/noop/NoopCronIT.java b/src/test/java/org/apache/aurora/scheduler/cron/noop/NoopCronIT.java
index 566fb47..b218437 100644
--- a/src/test/java/org/apache/aurora/scheduler/cron/noop/NoopCronIT.java
+++ b/src/test/java/org/apache/aurora/scheduler/cron/noop/NoopCronIT.java
@@ -41,8 +41,8 @@ public class NoopCronIT {
 
   @Test
   public void testLifecycle() throws Exception {
-    cronScheduler.start();
-    cronScheduler.stop();
+    cronScheduler.startAsync().awaitRunning();
+    cronScheduler.stopAsync().awaitTerminated();
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/33d2c76b/src/test/java/org/apache/aurora/scheduler/state/CronJobManagerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/state/CronJobManagerTest.java b/src/test/java/org/apache/aurora/scheduler/state/CronJobManagerTest.java
index 684e239..86ef6d5 100644
--- a/src/test/java/org/apache/aurora/scheduler/state/CronJobManagerTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/state/CronJobManagerTest.java
@@ -140,7 +140,8 @@ public class CronJobManagerTest extends EasyMockTest {
 
   @Test
   public void testPubsubWiring() throws Exception {
-    cronScheduler.start();
+    expect(cronScheduler.startAsync()).andReturn(cronScheduler);
+    cronScheduler.awaitRunning();
     shutdownRegistry.addAction(EasyMock.<ExceptionalCommand<?>>anyObject());
     expect(storageUtil.jobStore.fetchJobs(MANAGER_KEY))
         .andReturn(ImmutableList.<IJobConfiguration>of());
@@ -351,7 +352,8 @@ public class CronJobManagerTest extends EasyMockTest {
   public void testInvalidStoredJob() throws Exception {
     // Invalid jobs are left alone, but doesn't halt operation.
 
-    cronScheduler.start();
+    expect(cronScheduler.startAsync()).andReturn(cronScheduler);
+    cronScheduler.awaitRunning();
     shutdownRegistry.addAction(EasyMock.<ExceptionalCommand<?>>anyObject());
 
     IJobConfiguration jobA =
@@ -371,7 +373,8 @@ public class CronJobManagerTest extends EasyMockTest {
   public void testJobStoredTwice() throws Exception {
     // Simulate an inconsistent storage that contains two cron jobs under the same key.
 
-    cronScheduler.start();
+    expect(cronScheduler.startAsync()).andReturn(cronScheduler);
+    cronScheduler.awaitRunning();
     shutdownRegistry.addAction(EasyMock.<ExceptionalCommand<?>>anyObject());
 
     IJobConfiguration jobA =


Mime
View raw message