{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/43720999","id":43720999,"diff_hunk":"@@ -0,0 +1,260 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.spark.sql.execution\n+\n+import java.util.{Map => JMap, HashMap => JHashMap}\n+\n+import scala.collection.mutable.ArrayBuffer\n+\n+import org.apache.spark.{Logging, SimpleFutureAction, ShuffleDependency, MapOutputStatistics}\n+import org.apache.spark.rdd.RDD\n+import org.apache.spark.sql.catalyst.InternalRow\n+\n+/**\n+ * A coordinator used to determines how we shuffle data between stages generated by Spark SQL.\n+ * Right now, the work of this coordinator is to determine the number of post-shuffle partitions\n+ * for a stage that needs to fetch shuffle data from one or multiple stages.\n+ *\n+ * A coordinator is constructed with three parameters, `numExchanges`,\n+ * `targetPostShuffleInputSize`, and `minNumPostShufflePartitions`.\n+ *  - `numExchanges` is used to indicated that how many [[Exchange]]s that will be registered to\n+ *    this coordinator. So, when we start to do any actual work, we have a way to make sure that\n+ *    we have got expected number of [[Exchange]]s.\n+ *  - `targetPostShuffleInputSize` is the targeted size of a post-shuffle partition's\n+ *    input data size. With this parameter, we can estimate the number of post-shuffle partitions.\n+ *    This parameter is configured through\n+ *    `spark.sql.adaptive.shuffle.targetPostShuffleInputSize`.\n+ *  - `minNumPostShufflePartitions` is an optional parameter. If it is defined, this coordinator\n+ *    will try to make sure that there are at least `minNumPostShufflePartitions` post-shuffle\n+ *    partitions.\n+ *\n+ * The workflow of this coordinator is described as follows:\n+ *  - Before the execution of a [[SparkPlan]], for an [[Exchange]] operator,\n+ *    if an [[ExchangeCoordinator]] is assigned to it, it registers itself to this coordinator.\n+ *    This happens in the `doPrepare` method.\n+ *  - Once we start to execute a physical plan, an [[Exchange]] registered to this coordinator will\n+ *    call `postShuffleRDD` to get its corresponding post-shuffle [[ShuffledRowRDD]].\n+ *    If this coordinator has made the decision on how to shuffle data, this [[Exchange]] will\n+ *    immediately get its corresponding post-shuffle [[ShuffledRowRDD]].\n+ *  - If this coordinator has not made the decision on how to shuffle data, it will ask those\n+ *    registered [[Exchange]]s to submit their pre-shuffle stages. Then, based on the the size\n+ *    statistics of pre-shuffle partitions, this coordinator will determine the number of\n+ *    post-shuffle partitions and pack multiple pre-shuffle partitions with continuous indices\n+ *    to a single post-shuffle partition whenever necessary.\n+ *  - Finally, this coordinator will create post-shuffle [[ShuffledRowRDD]]s for all registered\n+ *    [[Exchange]]s. So, when an [[Exchange]] calls `postShuffleRDD`, this coordinator can\n+ *    lookup the corresponding [[RDD]].\n+ *\n+ * The strategy used to determine the number of post-shuffle partitions is described as follows.\n+ * To determine the number of post-shuffle partitions, we have a target input size for a\n+ * post-shuffle partition. Once we have size statistics of pre-shuffle partitions from stages\n+ * corresponding to the registered [[Exchange]]s, we will do a pass of those statistics and\n+ * pack pre-shuffle partitions with continuous indices to a single post-shuffle partition until\n+ * the size of a post-shuffle partition is equal or greater than the target size.\n+ * For example, we have two stages with the following pre-shuffle partition size statistics:\n+ * stage 1: [100 MB, 20 MB, 100 MB, 10MB, 30 MB]\n+ * stage 2: [10 MB,  10 MB, 70 MB,  5 MB, 5 MB]\n+ * assuming the target input size is 128 MB, we will have three post-shuffle partitions,\n+ * which are:\n+ *  - post-shuffle partition 0: pre-shuffle partition 0 and 1\n+ *  - post-shuffle partition 1: pre-shuffle partition 2\n+ *  - post-shuffle partition 2: pre-shuffle partition 3 and 4\n+ */\n+private[sql] class ExchangeCoordinator(\n+    numExchanges: Int,\n+    advisoryTargetPostShuffleInputSize: Long,\n+    minNumPostShufflePartitions: Option[Int] = None)\n+  extends Logging {\n+\n+  // The registered Exchange operators.\n+  private[this] val exchanges = ArrayBuffer[Exchange]()\n+\n+  // This map is used to lookup the post-shuffle ShuffledRowRDD for an Exchange operator.\n+  private[this] val postShuffleRDDs: JMap[Exchange, ShuffledRowRDD] =\n+    new JHashMap[Exchange, ShuffledRowRDD](numExchanges)\n+\n+  // A boolean that indicates if this coordinator has made decision on how to shuffle data.\n+  // This variable will only be updated by doEstimationIfNecessary, which is protected by\n+  // synchronized.\n+  @volatile private[this] var estimated: Boolean = false\n+\n+  /**\n+   * Registers an [[Exchange]] operator to this coordinator. This method is only allowed to be\n+   * called in the `doPrepare` method of an [[Exchange]] operator.\n+   */\n+  def registerExchange(exchange: Exchange): Unit = synchronized {\n+    exchanges += exchange\n+  }\n+\n+  def isEstimated: Boolean = estimated\n+\n+  /**\n+   * Estimates partition start indices for post-shuffle partitions based on\n+   * mapOutputStatistics provided by all pre-shuffle stages.\n+   */\n+  private[sql] def estimatePartitionStartIndices(\n+      mapOutputStatistics: Array[MapOutputStatistics]): Array[Int] = {\n+    // If we have mapOutputStatistics.length <= numExchange, it is because we do not submit\n+    // a stage when the number of partitions of this dependency is 0.\n+    assert(mapOutputStatistics.length <= numExchanges)\n+\n+    // If minNumPostShufflePartitions is defined, it is possible that we need to use a\n+    // value less than advisoryTargetPostShuffleInputSize as the target input size of\n+    // a post shuffle task.\n+    val targetPostShuffleInputSize = minNumPostShufflePartitions match {\n+      case Some(numPartitions) =>\n+        val totalPostShuffleInputSize = mapOutputStatistics.map(_.bytesByPartitionId.sum).sum\n+        // The max at here is to make sure that when we have an empty table, we\n+        // only have a single post-shuffle partition.\n+        val maxPostShuffleInputSize =\n+          math.max(math.ceil(totalPostShuffleInputSize / numPartitions.toDouble).toLong, 16)\n+        math.min(maxPostShuffleInputSize, advisoryTargetPostShuffleInputSize)\n+\n+      case None => advisoryTargetPostShuffleInputSize\n+    }\n+\n+    logInfo(\n+      s\"advisoryTargetPostShuffleInputSize: $advisoryTargetPostShuffleInputSize, \" +\n+      s\"targetPostShuffleInputSize $targetPostShuffleInputSize.\")\n+\n+    // Make sure we do get the same number of pre-shuffle partitions for those stages.\n+    val distinctNumPreShufflePartitions =\n+      mapOutputStatistics.map(stats => stats.bytesByPartitionId.length).distinct\n+    assert(\n+      distinctNumPreShufflePartitions.length == 1,\n+      \"There should be only one distinct value of the number pre-shuffle partitions \" +","path":"sql/core/src/main/scala/org/apache/spark/sql/execution/ExchangeCoordinator.scala","position":140,"original_position":140,"commit_id":"60e371e7d0d973745ed40fa3870e9446490224de","original_commit_id":"60e371e7d0d973745ed40fa3870e9446490224de","user":{"login":"yhuai","id":2072857,"avatar_url":"https://avatars.githubusercontent.com/u/2072857?v=3","gravatar_id":"","url":"https://api.github.com/users/yhuai","html_url":"https://github.com/yhuai","followers_url":"https://api.github.com/users/yhuai/followers","following_url":"https://api.github.com/users/yhuai/following{/other_user}","gists_url":"https://api.github.com/users/yhuai/gists{/gist_id}","starred_url":"https://api.github.com/users/yhuai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yhuai/subscriptions","organizations_url":"https://api.github.com/users/yhuai/orgs","repos_url":"https://api.github.com/users/yhuai/repos","events_url":"https://api.github.com/users/yhuai/events{/privacy}","received_events_url":"https://api.github.com/users/yhuai/received_events","type":"User","site_admin":false},"body":"I see. `distinctNumPreShufflePartitions` at here is the number of map output partitions. For example, if we use hash partitioning with 100 partitions, this value will be 100. Then, after the estimation, we may end up with 10 coalesced partitions.","created_at":"2015-11-03T07:11:35Z","updated_at":"2015-11-03T07:11:35Z","html_url":"https://github.com/apache/spark/pull/9276#discussion_r43720999","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/9276","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/43720999"},"html":{"href":"https://github.com/apache/spark/pull/9276#discussion_r43720999"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/9276"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/9276","id":48704050,"html_url":"https://github.com/apache/spark/pull/9276","diff_url":"https://github.com/apache/spark/pull/9276.diff","patch_url":"https://github.com/apache/spark/pull/9276.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/9276","number":9276,"state":"open","locked":false,"title":"[SPARK-9858][SPARK-9859][SPARK-9861][SQL] Add an ExchangeCoordinator to estimate the number of post-shuffle partitions for aggregates and joins","user":{"login":"yhuai","id":2072857,"avatar_url":"https://avatars.githubusercontent.com/u/2072857?v=3","gravatar_id":"","url":"https://api.github.com/users/yhuai","html_url":"https://github.com/yhuai","followers_url":"https://api.github.com/users/yhuai/followers","following_url":"https://api.github.com/users/yhuai/following{/other_user}","gists_url":"https://api.github.com/users/yhuai/gists{/gist_id}","starred_url":"https://api.github.com/users/yhuai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yhuai/subscriptions","organizations_url":"https://api.github.com/users/yhuai/orgs","repos_url":"https://api.github.com/users/yhuai/repos","events_url":"https://api.github.com/users/yhuai/events{/privacy}","received_events_url":"https://api.github.com/users/yhuai/received_events","type":"User","site_admin":false},"body":"https://issues.apache.org/jira/browse/SPARK-9858\r\nhttps://issues.apache.org/jira/browse/SPARK-9859\r\nhttps://issues.apache.org/jira/browse/SPARK-9861","created_at":"2015-10-26T05:58:26Z","updated_at":"2015-11-03T07:11:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1919bebc3f59e9de3bfab0e38c2aedd57a398144","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/9276/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/9276/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/9276/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/60e371e7d0d973745ed40fa3870e9446490224de","head":{"label":"yhuai:numReducer","ref":"numReducer","sha":"60e371e7d0d973745ed40fa3870e9446490224de","user":{"login":"yhuai","id":2072857,"avatar_url":"https://avatars.githubusercontent.com/u/2072857?v=3","gravatar_id":"","url":"https://api.github.com/users/yhuai","html_url":"https://github.com/yhuai","followers_url":"https://api.github.com/users/yhuai/followers","following_url":"https://api.github.com/users/yhuai/following{/other_user}","gists_url":"https://api.github.com/users/yhuai/gists{/gist_id}","starred_url":"https://api.github.com/users/yhuai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yhuai/subscriptions","organizations_url":"https://api.github.com/users/yhuai/orgs","repos_url":"https://api.github.com/users/yhuai/repos","events_url":"https://api.github.com/users/yhuai/events{/privacy}","received_events_url":"https://api.github.com/users/yhuai/received_events","type":"User","site_admin":false},"repo":{"id":17964653,"name":"spark","full_name":"yhuai/spark","owner":{"login":"yhuai","id":2072857,"avatar_url":"https://avatars.githubusercontent.com/u/2072857?v=3","gravatar_id":"","url":"https://api.github.com/users/yhuai","html_url":"https://github.com/yhuai","followers_url":"https://api.github.com/users/yhuai/followers","following_url":"https://api.github.com/users/yhuai/following{/other_user}","gists_url":"https://api.github.com/users/yhuai/gists{/gist_id}","starred_url":"https://api.github.com/users/yhuai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yhuai/subscriptions","organizations_url":"https://api.github.com/users/yhuai/orgs","repos_url":"https://api.github.com/users/yhuai/repos","events_url":"https://api.github.com/users/yhuai/events{/privacy}","received_events_url":"https://api.github.com/users/yhuai/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/yhuai/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/yhuai/spark","forks_url":"https://api.github.com/repos/yhuai/spark/forks","keys_url":"https://api.github.com/repos/yhuai/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/yhuai/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/yhuai/spark/teams","hooks_url":"https://api.github.com/repos/yhuai/spark/hooks","issue_events_url":"https://api.github.com/repos/yhuai/spark/issues/events{/number}","events_url":"https://api.github.com/repos/yhuai/spark/events","assignees_url":"https://api.github.com/repos/yhuai/spark/assignees{/user}","branches_url":"https://api.github.com/repos/yhuai/spark/branches{/branch}","tags_url":"https://api.github.com/repos/yhuai/spark/tags","blobs_url":"https://api.github.com/repos/yhuai/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/yhuai/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/yhuai/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/yhuai/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/yhuai/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/yhuai/spark/languages","stargazers_url":"https://api.github.com/repos/yhuai/spark/stargazers","contributors_url":"https://api.github.com/repos/yhuai/spark/contributors","subscribers_url":"https://api.github.com/repos/yhuai/spark/subscribers","subscription_url":"https://api.github.com/repos/yhuai/spark/subscription","commits_url":"https://api.github.com/repos/yhuai/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/yhuai/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/yhuai/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/yhuai/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/yhuai/spark/contents/{+path}","compare_url":"https://api.github.com/repos/yhuai/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/yhuai/spark/merges","archive_url":"https://api.github.com/repos/yhuai/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/yhuai/spark/downloads","issues_url":"https://api.github.com/repos/yhuai/spark/issues{/number}","pulls_url":"https://api.github.com/repos/yhuai/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/yhuai/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/yhuai/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/yhuai/spark/labels{/name}","releases_url":"https://api.github.com/repos/yhuai/spark/releases{/id}","created_at":"2014-03-21T01:38:52Z","updated_at":"2015-05-07T05:56:12Z","pushed_at":"2015-11-03T05:39:29Z","git_url":"git://github.com/yhuai/spark.git","ssh_url":"git@github.com:yhuai/spark.git","clone_url":"https://github.com/yhuai/spark.git","svn_url":"https://github.com/yhuai/spark","homepage":null,"size":131011,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"2cef1bb0b560a03aa7308f694b0c66347b90c9ea","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-11-03T06:41:48Z","pushed_at":"2015-11-03T07:11:17Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1650578,"stargazers_count":5988,"watchers_count":5988,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5580,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":375,"forks":5580,"open_issues":375,"watchers":5988,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/9276"},"html":{"href":"https://github.com/apache/spark/pull/9276"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/9276"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/9276/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/9276/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/9276/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/60e371e7d0d973745ed40fa3870e9446490224de"}}}}