You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by jx...@apache.org on 2017/06/23 20:07:22 UTC

[2/2] helix git commit: Add workflow configuration to allow or disallow assigning multiple jobs to one instance.

Add workflow configuration to allow or disallow assigning multiple jobs to one instance.

By default, it is not allowed that Helix assigns multiple jobs in one workflow to the same instances.
If it is set to be true, the instance can start executing multiple jobs in each workflow.

When application sets max tasks throttling for the participants, allowing overlapping assignment can maximize utilization.


Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/e35fe4ff
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/e35fe4ff
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/e35fe4ff

Branch: refs/heads/master
Commit: e35fe4fffc952f7ccae7bfa4cbf89ef75e404a53
Parents: 20685cf
Author: Jiajun Wang <jj...@linkedin.com>
Authored: Fri Jun 2 23:26:50 2017 -0700
Committer: Jiajun Wang <jj...@linkedin.com>
Committed: Fri Jun 23 12:06:17 2017 -0700

----------------------------------------------------------------------
 .../org/apache/helix/task/JobRebalancer.java    | 33 ++++++------
 .../org/apache/helix/task/WorkflowConfig.java   | 39 +++++++++++---
 .../helix/integration/task/TaskTestUtil.java    | 43 +++++++--------
 .../task/TestTaskRebalancerParallel.java        | 55 ++++++++++++++++++--
 4 files changed, 125 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/e35fe4ff/helix-core/src/main/java/org/apache/helix/task/JobRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/task/JobRebalancer.java b/helix-core/src/main/java/org/apache/helix/task/JobRebalancer.java
index d891d29..612c89a 100644
--- a/helix-core/src/main/java/org/apache/helix/task/JobRebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/task/JobRebalancer.java
@@ -170,22 +170,25 @@ public class JobRebalancer extends TaskRebalancer {
     return newAssignment;
   }
 
-  private Set<String> getInstancesAssignedToOtherJobs(String currentJobName,
+  private Set<String> getExcludedInstances(String currentJobName,
       WorkflowConfig workflowCfg) {
     Set<String> ret = new HashSet<String>();
-    for (String jobName : workflowCfg.getJobDag().getAllNodes()) {
-      if (jobName.equals(currentJobName)) {
-        continue;
-      }
-      JobContext jobContext = TaskUtil.getJobContext(_manager, jobName);
-      if (jobContext == null) {
-        continue;
-      }
-      for (int partition : jobContext.getPartitionSet()) {
-        TaskPartitionState partitionState = jobContext.getPartitionState(partition);
-        if (partitionState == TaskPartitionState.INIT ||
-            partitionState == TaskPartitionState.RUNNING) {
-          ret.add(jobContext.getAssignedParticipant(partition));
+
+    if (!workflowCfg.isAllowOverlapJobAssignment()) {
+      // exclude all instances that has been assigned other jobs' tasks
+      for (String jobName : workflowCfg.getJobDag().getAllNodes()) {
+        if (jobName.equals(currentJobName)) {
+          continue;
+        }
+        JobContext jobContext = TaskUtil.getJobContext(_manager, jobName);
+        if (jobContext == null) {
+          continue;
+        }
+        for (int partition : jobContext.getPartitionSet()) {
+          TaskPartitionState partitionState = jobContext.getPartitionState(partition);
+          if (partitionState == TaskPartitionState.INIT || partitionState == TaskPartitionState.RUNNING) {
+            ret.add(jobContext.getAssignedParticipant(partition));
+          }
         }
       }
     }
@@ -227,7 +230,7 @@ public class JobRebalancer extends TaskRebalancer {
     // Keeps a mapping of (partition) -> (instance, state)
     Map<Integer, PartitionAssignment> paMap = new TreeMap<Integer, PartitionAssignment>();
 
-    Set<String> excludedInstances = getInstancesAssignedToOtherJobs(jobResource, workflowConfig);
+    Set<String> excludedInstances = getExcludedInstances(jobResource, workflowConfig);
 
     // Process all the current assignments of tasks.
     TaskAssignmentCalculator taskAssignmentCal = getAssignmentCalulator(jobCfg);

http://git-wip-us.apache.org/repos/asf/helix/blob/e35fe4ff/helix-core/src/main/java/org/apache/helix/task/WorkflowConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/task/WorkflowConfig.java b/helix-core/src/main/java/org/apache/helix/task/WorkflowConfig.java
index 9f71e35..4c6e971 100644
--- a/helix-core/src/main/java/org/apache/helix/task/WorkflowConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/task/WorkflowConfig.java
@@ -61,7 +61,9 @@ public class  WorkflowConfig extends ResourceConfig {
     capacity,
     WorkflowType,
     JobTypes,
-    IsJobQueue
+    IsJobQueue,
+    /* Allow multiple jobs in this workflow to be assigned to a same instance or not */
+    AllowOverlapJobAssignment
   }
 
   /* Default values */
@@ -74,6 +76,7 @@ public class  WorkflowConfig extends ResourceConfig {
   public static final boolean DEFAULT_TERMINABLE = true;
   public static final boolean DEFAULT_JOB_QUEUE = false;
   public static final boolean DEFAULT_MONITOR_DISABLE = true;
+  public static final boolean DEFAULT_ALLOW_OVERLAP_JOB_ASSIGNMENT = false;
 
   public WorkflowConfig(HelixProperty property) {
     super(property.getRecord());
@@ -82,7 +85,7 @@ public class  WorkflowConfig extends ResourceConfig {
   public WorkflowConfig(WorkflowConfig cfg, String workflowId) {
     this(workflowId, cfg.getJobDag(), cfg.getParallelJobs(), cfg.getTargetState(), cfg.getExpiry(),
         cfg.getFailureThreshold(), cfg.isTerminable(), cfg.getScheduleConfig(), cfg.getCapacity(),
-        cfg.getWorkflowType(), cfg.isJobQueue(), cfg.getJobTypes());
+        cfg.getWorkflowType(), cfg.isJobQueue(), cfg.getJobTypes(), cfg.isAllowOverlapJobAssignment());
   }
 
   /* Member variables */
@@ -91,7 +94,7 @@ public class  WorkflowConfig extends ResourceConfig {
   protected WorkflowConfig(String workflowId, JobDag jobDag, int parallelJobs,
       TargetState targetState, long expiry, int failureThreshold, boolean terminable,
       ScheduleConfig scheduleConfig, int capacity, String workflowType, boolean isJobQueue,
-      Map<String, String> jobTypes) {
+      Map<String, String> jobTypes, boolean allowOverlapJobAssignment) {
     super(workflowId);
 
     putSimpleConfig(WorkflowConfigProperty.WorkflowID.name(), workflowId);
@@ -105,8 +108,8 @@ public class  WorkflowConfig extends ResourceConfig {
     putSimpleConfig(WorkflowConfigProperty.TargetState.name(), targetState.name());
     putSimpleConfig(WorkflowConfigProperty.Terminable.name(), String.valueOf(terminable));
     putSimpleConfig(WorkflowConfigProperty.IsJobQueue.name(), String.valueOf(isJobQueue));
-    putSimpleConfig(WorkflowConfigProperty.FailureThreshold.name(),
-        String.valueOf(failureThreshold));
+    putSimpleConfig(WorkflowConfigProperty.FailureThreshold.name(), String.valueOf(failureThreshold));
+    putSimpleConfig(WorkflowConfigProperty.AllowOverlapJobAssignment.name(), String.valueOf(allowOverlapJobAssignment));
 
     if (capacity > 0) {
       putSimpleConfig(WorkflowConfigProperty.capacity.name(), String.valueOf(capacity));
@@ -210,6 +213,11 @@ public class  WorkflowConfig extends ResourceConfig {
         WorkflowConfigProperty.JobTypes.name()) : null;
   }
 
+  public boolean isAllowOverlapJobAssignment() {
+    return _record.getBooleanField(WorkflowConfigProperty.AllowOverlapJobAssignment.name(),
+        DEFAULT_ALLOW_OVERLAP_JOB_ASSIGNMENT);
+  }
+
   public static SimpleDateFormat getDefaultDateFormat() {
     SimpleDateFormat defaultDateFormat = new SimpleDateFormat(
         "MM-dd-yyyy HH:mm:ss");
@@ -295,13 +303,14 @@ public class  WorkflowConfig extends ResourceConfig {
     private String _workflowType;
     private boolean _isJobQueue = DEFAULT_JOB_QUEUE;
     private Map<String, String> _jobTypes;
+    private boolean _allowOverlapJobAssignment = DEFAULT_ALLOW_OVERLAP_JOB_ASSIGNMENT;
 
     public WorkflowConfig build() {
       validate();
 
       return new WorkflowConfig(_workflowId, _taskDag, _parallelJobs, _targetState, _expiry,
           _failureThreshold, _isTerminable, _scheduleConfig, _capacity, _workflowType, _isJobQueue,
-          _jobTypes);
+          _jobTypes, _allowOverlapJobAssignment);
     }
 
     public Builder() {}
@@ -319,6 +328,7 @@ public class  WorkflowConfig extends ResourceConfig {
       _workflowType = workflowConfig.getWorkflowType();
       _isJobQueue = workflowConfig.isJobQueue();
       _jobTypes = workflowConfig.getJobTypes();
+      _allowOverlapJobAssignment = workflowConfig.isAllowOverlapJobAssignment();
     }
 
     public Builder setWorkflowId(String v) {
@@ -391,6 +401,17 @@ public class  WorkflowConfig extends ResourceConfig {
       return this;
     }
 
+    /**
+     * Set if allow multiple jobs in one workflow to be assigned on one instance.
+     * If not set, default configuration is false.
+     * @param allowOverlapJobAssignment true if allow overlap assignment
+     * @return This builder
+     */
+    public Builder setAllowOverlapJobAssignment(boolean allowOverlapJobAssignment) {
+      _allowOverlapJobAssignment = allowOverlapJobAssignment;
+      return this;
+    }
+
     public static Builder fromMap(Map<String, String> cfg) {
       Builder builder = new Builder();
       builder.setConfigMap(cfg);
@@ -450,6 +471,12 @@ public class  WorkflowConfig extends ResourceConfig {
       if (cfg.containsKey(WorkflowConfigProperty.IsJobQueue.name())) {
         setJobQueue(Boolean.parseBoolean(cfg.get(WorkflowConfigProperty.IsJobQueue.name())));
       }
+
+      if (cfg.containsKey(WorkflowConfigProperty.AllowOverlapJobAssignment.name())) {
+        setAllowOverlapJobAssignment(
+            Boolean.parseBoolean(cfg.get(WorkflowConfigProperty.AllowOverlapJobAssignment.name())));
+      }
+
       return this;
     }
 

http://git-wip-us.apache.org/repos/asf/helix/blob/e35fe4ff/helix-core/src/test/java/org/apache/helix/integration/task/TaskTestUtil.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/task/TaskTestUtil.java b/helix-core/src/test/java/org/apache/helix/integration/task/TaskTestUtil.java
index 02db97f..ce1d180 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/task/TaskTestUtil.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/task/TaskTestUtil.java
@@ -86,7 +86,7 @@ public class TaskTestUtil {
   }
 
   // 1. Different jobs in a same work flow is in RUNNING at the same time
-  // 2. No two jobs in the same work flow is in RUNNING at the same instance
+  // 2. When disallow overlap assignment, no two jobs in the same work flow is in RUNNING at the same instance
   public static boolean pollForWorkflowParallelState(TaskDriver driver, String workflowName)
       throws InterruptedException {
 
@@ -127,26 +127,27 @@ public class TaskTestUtil {
         }
       }
 
-      Set<String> instances = new HashSet<String>();
-      for (JobContext jobContext : jobContextList) {
-        for (int partition : jobContext.getPartitionSet()) {
-          String instance = jobContext.getAssignedParticipant(partition);
-          TaskPartitionState taskPartitionState = jobContext.getPartitionState(partition);
-
-          if (instance == null) {
-            continue;
-          }
-          if (taskPartitionState != TaskPartitionState.INIT &&
-              taskPartitionState != TaskPartitionState.RUNNING) {
-            continue;
-          }
-          if (instances.contains(instance)) {
-            return false;
-          }
-
-          TaskPartitionState state = jobContext.getPartitionState(partition);
-          if (state != TaskPartitionState.COMPLETED) {
-            instances.add(instance);
+      if (!workflowConfig.isAllowOverlapJobAssignment()) {
+        Set<String> instances = new HashSet<String>();
+        for (JobContext jobContext : jobContextList) {
+          for (int partition : jobContext.getPartitionSet()) {
+            String instance = jobContext.getAssignedParticipant(partition);
+            TaskPartitionState taskPartitionState = jobContext.getPartitionState(partition);
+
+            if (instance == null) {
+              continue;
+            }
+            if (taskPartitionState != TaskPartitionState.INIT && taskPartitionState != TaskPartitionState.RUNNING) {
+              continue;
+            }
+            if (instances.contains(instance)) {
+              return false;
+            }
+
+            TaskPartitionState state = jobContext.getPartitionState(partition);
+            if (state != TaskPartitionState.COMPLETED) {
+              instances.add(instance);
+            }
           }
         }
       }

http://git-wip-us.apache.org/repos/asf/helix/blob/e35fe4ff/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRebalancerParallel.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRebalancerParallel.java b/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRebalancerParallel.java
index 9f6b514..7da7b29 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRebalancerParallel.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRebalancerParallel.java
@@ -24,14 +24,19 @@ import java.util.Collections;
 import java.util.List;
 
 import org.apache.helix.TestHelper;
+import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.task.JobConfig;
 import org.apache.helix.task.JobQueue;
+import org.apache.helix.task.TaskConfig;
 import org.apache.helix.task.WorkflowConfig;
+import org.apache.helix.tools.ClusterVerifiers.ClusterLiveNodesVerifier;
 import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class TestTaskRebalancerParallel extends TaskTestBase {
+  final int PARALLEL_COUNT = 2;
+  final int TASK_COUNT = 30;
 
   @BeforeClass
   public void beforeClass() throws Exception {
@@ -39,13 +44,12 @@ public class TestTaskRebalancerParallel extends TaskTestBase {
     super.beforeClass();
   }
 
-  @Test public void test() throws Exception {
-    final int PARALLEL_COUNT = 2;
-
+  @Test public void testWhenDisallowOverlapJobAssignment() throws Exception {
     String queueName = TestHelper.getTestMethodName();
 
     WorkflowConfig.Builder cfgBuilder = new WorkflowConfig.Builder();
     cfgBuilder.setParallelJobs(PARALLEL_COUNT);
+    cfgBuilder.setAllowOverlapJobAssignment(false);
 
     JobQueue.Builder queueBuild =
         new JobQueue.Builder(queueName).setWorkflowConfig(cfgBuilder.build());
@@ -67,4 +71,49 @@ public class TestTaskRebalancerParallel extends TaskTestBase {
     Thread.sleep(2000);
     Assert.assertTrue(TaskTestUtil.pollForWorkflowParallelState(_driver, queueName));
   }
+
+  @Test (dependsOnMethods = {"testWhenDisallowOverlapJobAssignment"})
+  public void testWhenAllowOverlapJobAssignment() throws Exception {
+    // Disable all participants except one to enforce all assignment to be on one host
+    for (int i = 1; i < _numNodes; i++) {
+      _participants[i].syncStop();
+    }
+    ClusterLiveNodesVerifier verifier = new ClusterLiveNodesVerifier(_gZkClient, CLUSTER_NAME,
+        Collections.singletonList(_participants[0].getInstanceName()));
+    Assert.assertTrue(verifier.verify());
+
+    String queueName = TestHelper.getTestMethodName();
+
+    WorkflowConfig.Builder cfgBuilder = new WorkflowConfig.Builder();
+    cfgBuilder.setParallelJobs(PARALLEL_COUNT);
+    cfgBuilder.setAllowOverlapJobAssignment(true);
+
+    JobQueue.Builder queueBuild = new JobQueue.Builder(queueName).setWorkflowConfig(cfgBuilder.build());
+    JobQueue queue = queueBuild.build();
+    _driver.createQueue(queue);
+
+    // Create jobs that can be assigned to any instances
+    List<JobConfig.Builder> jobConfigBuilders = new ArrayList<JobConfig.Builder>();
+    for (int i = 0; i < PARALLEL_COUNT; i++) {
+      List<TaskConfig> taskConfigs = new ArrayList<TaskConfig>();
+      for (int j = 0; j < TASK_COUNT; j++) {
+        taskConfigs.add(
+            new TaskConfig.Builder().setTaskId("task_" + j).setCommand(MockTask.TASK_COMMAND)
+                .build());
+      }
+      jobConfigBuilders.add(new JobConfig.Builder().addTaskConfigs(taskConfigs));
+    }
+
+    _driver.stop(queueName);
+    for (int i = 0; i < jobConfigBuilders.size(); ++i) {
+      _driver.enqueueJob(queueName, "job_" + (i + 1), jobConfigBuilders.get(i));
+    }
+    _driver.resume(queueName);
+    Thread.sleep(2000);
+    Assert.assertTrue(TaskTestUtil.pollForWorkflowParallelState(_driver, queueName));
+
+    for (int i = 1; i < _numNodes; i++) {
+      _participants[i].syncStart();
+    }
+  }
 }