azkaban-aplcache

Details

diff --git a/azkaban-common/src/main/java/azkaban/scheduler/TriggerBasedScheduleLoader.java b/azkaban-common/src/main/java/azkaban/scheduler/TriggerBasedScheduleLoader.java
index e1bcf81..9f7d5a3 100644
--- a/azkaban-common/src/main/java/azkaban/scheduler/TriggerBasedScheduleLoader.java
+++ b/azkaban-common/src/main/java/azkaban/scheduler/TriggerBasedScheduleLoader.java
@@ -54,10 +54,17 @@ public class TriggerBasedScheduleLoader implements ScheduleLoader {
     Condition triggerCondition = createTriggerCondition(s);
     Condition expireCondition = createExpireCondition(s);
     List<TriggerAction> actions = createActions(s);
-    Trigger t =
-        new Trigger(s.getScheduleId(), s.getLastModifyTime(),
-            s.getSubmitTime(), s.getSubmitUser(), triggerSource,
-            triggerCondition, expireCondition, actions);
+
+    Trigger t = new Trigger.TriggerBuilder(s.getSubmitUser(),
+        triggerSource,
+        triggerCondition,
+        expireCondition,
+        actions)
+        .setSubmitTime(s.getSubmitTime())
+        .setLastModifyTime(s.getLastModifyTime())
+        .setId(s.getScheduleId())
+        .build();
+
     if (s.isRecurring()) {
       t.setResetOnTrigger(true);
     } else {
diff --git a/azkaban-common/src/main/java/azkaban/trigger/builtin/ExecuteFlowAction.java b/azkaban-common/src/main/java/azkaban/trigger/builtin/ExecuteFlowAction.java
index 3419235..db2dc53 100644
--- a/azkaban-common/src/main/java/azkaban/trigger/builtin/ExecuteFlowAction.java
+++ b/azkaban-common/src/main/java/azkaban/trigger/builtin/ExecuteFlowAction.java
@@ -283,9 +283,12 @@ public class ExecuteFlowAction implements TriggerAction {
             actions.add(killAct);
           }
         }
-        Trigger slaTrigger =
-            new Trigger("azkaban_sla", "azkaban", triggerCond, expireCond,
-                actions);
+        Trigger slaTrigger = new Trigger.TriggerBuilder("azkaban_sla",
+                                                        "azkaban",
+                                                        triggerCond,
+                                                        expireCond,
+                                                        actions).build();
+
         slaTrigger.getInfo().put("monitored.finished.execution",
             String.valueOf(execId));
         slaTrigger.setResetOnTrigger(false);
diff --git a/azkaban-common/src/main/java/azkaban/trigger/Trigger.java b/azkaban-common/src/main/java/azkaban/trigger/Trigger.java
index edde6d3..5768beb 100644
--- a/azkaban-common/src/main/java/azkaban/trigger/Trigger.java
+++ b/azkaban-common/src/main/java/azkaban/trigger/Trigger.java
@@ -27,21 +27,24 @@ import org.joda.time.DateTime;
 
 import azkaban.utils.JSONUtils;
 
+import static java.util.Objects.*;
+
+
 public class Trigger {
 
   private static Logger logger = Logger.getLogger(Trigger.class);
 
   private int triggerId = -1;
   private long lastModifyTime;
-  private long submitTime;
-  private String submitUser;
-  private String source;
+  private final long submitTime;
+  private final String submitUser;
+  private final String source;
   private TriggerStatus status = TriggerStatus.READY;
 
-  private Condition triggerCondition;
-  private Condition expireCondition;
-  private List<TriggerAction> actions;
-  private List<TriggerAction> expireActions;
+  private final Condition triggerCondition;
+  private final Condition expireCondition;
+  private final List<TriggerAction> actions;
+  private final List<TriggerAction> expireActions;
 
   private Map<String, Object> info = new HashMap<String, Object>();
   private Map<String, Object> context = new HashMap<String, Object>();
@@ -120,79 +123,95 @@ public class Trigger {
     this.context = context;
   }
 
-  public Trigger(long lastModifyTime, long submitTime, String submitUser,
-      String source, Condition triggerCondition, Condition expireCondition,
-      List<TriggerAction> actions, List<TriggerAction> expireActions,
-      Map<String, Object> info, Map<String, Object> context) {
-    this.lastModifyTime = lastModifyTime;
-    this.submitTime = submitTime;
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = expireActions;
-    this.info = info;
-    this.context = context;
-  }
 
-  public Trigger(long lastModifyTime, long submitTime, String submitUser,
-      String source, Condition triggerCondition, Condition expireCondition,
-      List<TriggerAction> actions, List<TriggerAction> expireActions) {
-    this.lastModifyTime = lastModifyTime;
-    this.submitTime = submitTime;
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = expireActions;
-  }
+  public static class TriggerBuilder {
+    private int triggerId = -1;
+    private long lastModifyTime;
+    private long submitTime;
+    private final String submitUser;
+    private final String source;
+    private final TriggerStatus status = TriggerStatus.READY;
+
+    private final Condition triggerCondition;
+    private final List<TriggerAction> actions;
+    private final Condition expireCondition;
+    private List<TriggerAction> expireActions = new ArrayList<>();
+
+    private Map<String, Object> info = new HashMap<String, Object>();
+    private Map<String, Object> context = new HashMap<String, Object>();
+
+    public TriggerBuilder(String submitUser,
+                          String source,
+                          Condition triggerCondition,
+                          Condition expireCondition,
+                          List<TriggerAction> actions) {
+      this.submitUser = submitUser;
+      this.source = source;
+      this.triggerCondition = triggerCondition;
+      this.actions = actions;
+      this.expireCondition = expireCondition;
+      long now = DateTime.now().getMillis();
+      this.submitTime = now;
+      this.lastModifyTime = now;
+    }
 
-  public Trigger(String submitUser, String source, Condition triggerCondition,
-      Condition expireCondition, List<TriggerAction> actions,
-      List<TriggerAction> expireActions) {
-    this.lastModifyTime = DateTime.now().getMillis();
-    this.submitTime = DateTime.now().getMillis();
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = expireActions;
-  }
+    public TriggerBuilder setId(int id) {
+      this.triggerId = id;
+      return this;
+    }
 
-  public Trigger(String submitUser, String source, Condition triggerCondition,
-      Condition expireCondition, List<TriggerAction> actions) {
-    this.lastModifyTime = DateTime.now().getMillis();
-    this.submitTime = DateTime.now().getMillis();
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = new ArrayList<TriggerAction>();
-  }
+    public TriggerBuilder setSubmitTime(long time) {
+      this.submitTime = time;
+      return this;
+    }
 
-  public Trigger(long lastModifyTime, long submitTime, String submitUser,
-      String source, Condition triggerCondition, Condition expireCondition,
-      List<TriggerAction> actions) {
-    this.lastModifyTime = lastModifyTime;
-    this.submitTime = submitTime;
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = new ArrayList<TriggerAction>();
+    public TriggerBuilder setLastModifyTime(long time) {
+      this.lastModifyTime = time;
+      return this;
+    }
+
+    public TriggerBuilder setExpireActions(List<TriggerAction> actions) {
+      this.expireActions = actions;
+      return this;
+    }
+
+    public TriggerBuilder setInfo(Map<String, Object> info) {
+      this.info = info;
+      return this;
+    }
+
+    public TriggerBuilder setContext(Map<String, Object> context) {
+      this.context = context;
+      return this;
+    }
+
+    public Trigger build() {
+      return new Trigger(triggerId,
+                        lastModifyTime,
+                        submitTime,
+                        submitUser,
+                        source,
+                        triggerCondition,
+                        expireCondition,
+                        actions,
+                        expireActions,
+                        info,
+                        context);
+    }
   }
 
-  public Trigger(int triggerId, long lastModifyTime, long submitTime,
+  private Trigger(int triggerId, long lastModifyTime, long submitTime,
       String submitUser, String source, Condition triggerCondition,
       Condition expireCondition, List<TriggerAction> actions,
       List<TriggerAction> expireActions, Map<String, Object> info,
       Map<String, Object> context) {
-    this.triggerId = triggerId;
+    requireNonNull(submitUser);
+    requireNonNull(source);
+    requireNonNull(triggerCondition);
+    requireNonNull(expireActions);
+    requireNonNull(info);
+    requireNonNull(context);
+
     this.lastModifyTime = lastModifyTime;
     this.submitTime = submitTime;
     this.submitUser = submitUser;
@@ -200,40 +219,12 @@ public class Trigger {
     this.triggerCondition = triggerCondition;
     this.expireCondition = expireCondition;
     this.actions = actions;
+    this.triggerId = triggerId;
     this.expireActions = expireActions;
     this.info = info;
     this.context = context;
   }
 
-  public Trigger(int triggerId, long lastModifyTime, long submitTime,
-      String submitUser, String source, Condition triggerCondition,
-      Condition expireCondition, List<TriggerAction> actions,
-      List<TriggerAction> expireActions) {
-    this.triggerId = triggerId;
-    this.lastModifyTime = lastModifyTime;
-    this.submitTime = submitTime;
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = expireActions;
-  }
-
-  public Trigger(int triggerId, long lastModifyTime, long submitTime,
-      String submitUser, String source, Condition triggerCondition,
-      Condition expireCondition, List<TriggerAction> actions) {
-    this.triggerId = triggerId;
-    this.lastModifyTime = lastModifyTime;
-    this.submitTime = submitTime;
-    this.submitUser = submitUser;
-    this.source = source;
-    this.triggerCondition = triggerCondition;
-    this.expireCondition = expireCondition;
-    this.actions = actions;
-    this.expireActions = new ArrayList<TriggerAction>();
-  }
-
   public static synchronized void setActionTypeLoader(ActionTypeLoader loader) {
     Trigger.actionTypeLoader = loader;
   }
@@ -402,10 +393,19 @@ public class Trigger {
         action.setContext(context);
       }
 
-      trigger =
-          new Trigger(triggerId, lastModifyTime, submitTime, submitUser,
-              source, triggerCond, expireCond, actions, expireActions, info,
-              context);
+      trigger = new Trigger.TriggerBuilder("azkaban",
+          source,
+          triggerCond,
+          expireCond,
+          actions)
+          .setId(triggerId)
+          .setLastModifyTime(lastModifyTime)
+          .setSubmitTime(submitTime)
+          .setExpireActions(expireActions)
+          .setInfo(info)
+          .setContext(context)
+          .build();
+
       trigger.setResetOnExpire(resetOnExpire);
       trigger.setResetOnTrigger(resetOnTrigger);
       trigger.setStatus(status);
@@ -436,7 +436,11 @@ public class Trigger {
     for (ConditionChecker checker : expireCondition.getCheckers().values()) {
       checker.stopChecker();
     }
+  }
 
+  @Override
+  public String toString() {
+    return "Trigger Id: " + getTriggerId() + ", Description: " + getDescription();
   }
 
 }
diff --git a/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerImplTest.java b/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerImplTest.java
index aae137c..8a982f5 100644
--- a/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerImplTest.java
+++ b/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerImplTest.java
@@ -26,9 +26,7 @@ import azkaban.trigger.builtin.BasicTimeChecker;
 import azkaban.trigger.builtin.ExecuteFlowAction;
 import azkaban.utils.Props;
 import azkaban.utils.Utils;
-import com.google.common.io.Resources;
 import java.io.File;
-import java.net.URL;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -37,10 +35,8 @@ import java.util.Map;
 import org.apache.commons.dbutils.QueryRunner;
 import org.joda.time.DateTime;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -153,9 +149,14 @@ public class JdbcTriggerImplTest {
         new ExecuteFlowAction("executeAction", 1, projName, flowName,
             "azkaban", new ExecutionOptions(), null);
     actions.add(action);
-    Trigger t =
-        new Trigger(now.getMillis(), now.getMillis(), "azkaban", source,
-            triggerCond, expireCond, actions);
+
+    Trigger t = new Trigger.TriggerBuilder("azkaban",
+        source,
+        triggerCond,
+        expireCond,
+        actions)
+        .build();
+
     return t;
   }
 
diff --git a/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerLoaderTest.java b/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerLoaderTest.java
index b55a2ba..4599124 100644
--- a/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerLoaderTest.java
+++ b/azkaban-common/src/test/java/azkaban/trigger/JdbcTriggerLoaderTest.java
@@ -218,9 +218,13 @@ public class JdbcTriggerLoaderTest {
         new ExecuteFlowAction("executeAction", 1, projName, flowName,
             "azkaban", new ExecutionOptions(), null);
     actions.add(action);
-    Trigger t =
-        new Trigger(now.getMillis(), now.getMillis(), "azkaban", source,
-            triggerCond, expireCond, actions);
+
+    Trigger t = new Trigger.TriggerBuilder("azkaban",
+        source,
+        triggerCond,
+        expireCond,
+        actions).build();
+
     return t;
   }
 
diff --git a/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerDeadlockTest.java b/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerDeadlockTest.java
index b5d3076..b7303eb 100644
--- a/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerDeadlockTest.java
+++ b/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerDeadlockTest.java
@@ -27,7 +27,6 @@ import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 
-import azkaban.alert.Alerter;
 import azkaban.executor.ExecutorLoader;
 import azkaban.executor.ExecutorManager;
 import azkaban.executor.ExecutorManagerException;
@@ -172,7 +171,12 @@ public class TriggerManagerDeadlockTest {
     Condition expireCond = new Condition(expireCheckers, expireExpr);
 
     Trigger t =
-        new Trigger("azkaban", "azkabanTest", triggerCond, expireCond, actions);
+        new Trigger.TriggerBuilder("azkaban",
+            "azkabanTest",
+            triggerCond,
+            expireCond,
+            actions).build();
+
     return t;
   }
 
@@ -198,7 +202,12 @@ public class TriggerManagerDeadlockTest {
     Condition expireCond = new Condition(expireCheckers, expireExpr);
 
     Trigger t =
-        new Trigger("azkaban", "azkabanTest", triggerCond, expireCond, actions);
+        new Trigger.TriggerBuilder("azkaban",
+                                   "azkabanTest",
+                                   triggerCond,
+                                   expireCond,
+                                   actions).build();
+
     return t;
   }
 
diff --git a/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerTest.java b/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerTest.java
index 197fc14..33577fe 100644
--- a/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerTest.java
+++ b/azkaban-common/src/test/java/azkaban/trigger/TriggerManagerTest.java
@@ -21,8 +21,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.joda.time.DateTime;
-
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
@@ -182,9 +180,12 @@ public class TriggerManagerTest {
     Condition triggerCond = new Condition(checkers, expr);
     Condition expireCond = new Condition(checkers, expr);
 
-    Trigger fakeTrigger =
-        new Trigger(DateTime.now().getMillis(), DateTime.now().getMillis(),
-            "azkaban", source, triggerCond, expireCond, actions);
+    Trigger fakeTrigger = new Trigger.TriggerBuilder("azkaban",
+        source,
+        triggerCond,
+        expireCond,
+        actions).build();
+
     fakeTrigger.setResetOnTrigger(true);
     fakeTrigger.setResetOnExpire(true);
 
diff --git a/azkaban-common/src/test/java/azkaban/trigger/TriggerTest.java b/azkaban-common/src/test/java/azkaban/trigger/TriggerTest.java
index 595461f..96f61fe 100644
--- a/azkaban-common/src/test/java/azkaban/trigger/TriggerTest.java
+++ b/azkaban-common/src/test/java/azkaban/trigger/TriggerTest.java
@@ -69,9 +69,12 @@ public class TriggerTest {
         new ExecuteFlowAction("executeAction", 1, "testProj", "testFlow",
             "azkaban", new ExecutionOptions(), null);
     actions.add(action);
-    Trigger t =
-        new Trigger(now.getMillis(), now.getMillis(), "azkaban", "test",
-            triggerCond, expireCond, actions);
+
+    Trigger t = new Trigger.TriggerBuilder("azkaban",
+        "test",
+        triggerCond,
+        expireCond,
+        actions).build();
 
     File temp = File.createTempFile("temptest", "temptest");
     temp.deleteOnExit();