title: How to assgin spark task balance?
body: <p>My spark is installed in CDH5 5.8.0 and run its application in yarn. There are 5 servers in the cluster. One server is for resource manager. The other four servers are node manager. Each server has 2 core and 8G memory.</p>
  
  <p>The spark application main logic is not complex: Query table from postgres db. Do some business for each record and finally save result to db. Here is main code:</p>
  
  <pre><code>String columnName="id";
  long lowerBound=1;
  long upperBound=100000;
  int numPartitions=20;
  String tableBasic="select * from table1 order by id";
  DataFrame dfBasic = sqlContext.read().jdbc(JDBC_URL, tableBasic, columnName, lowerBound, upperBound,numPartitions, dbProperties);
  JavaRDD&lt;EntityResult&gt; rddResult = dfBasic.javaRDD().flatMap(new FlatMapFunction&lt;Row, Result&gt;() {
              public Iterable&lt;Result&gt; call(Row row) {
                  List&lt;Result&gt; list = new ArrayList&lt;Result&gt;();
                  ........
                  return list;
              }
          });
  DataFrame saveDF = sqlContext.createDataFrame(rddResult, Result.class);
  saveDF = saveDF.select("id", "column 1", "column 2",);
  saveDF.write().mode(SaveMode.Append).jdbc(SQL_CONNECTION_URL, "table2", dbProperties);
  </code></pre>
  
  <p>I use this command to submit application to yarn:</p>
  
  <pre><code>spark-submit --master yarn-cluster --executor-memory 6G --executor-cores 2 --driver-memory 6G --conf spark.default.parallelism=90 --conf spark.storage.memoryFraction=0.4 --conf spark.shuffle.memoryFraction=0.4 --conf spark.executor.memory=3G --class com.Main1 jar1-0.0.1.jar
  </code></pre>
  
  <p>There are 7 executors and 20 partitions. When the table records is small, for example less than 200000, the 20 active tasks can assign to the 7 executors balance, like this:
  <a href="http://i.stack.imgur.com/xJXKn.jpg" rel="nofollow">Assign task averagely</a></p>
  
  <p>But when the table records is huge, for example 1000000, the task will not assign averagely. There is always one executor run long time, the other executors run shortly. Some executors can't assign task. Like this:
  <a href="http://i.stack.imgur.com/9LBnq.jpg" rel="nofollow">enter image description here</a></p>