GitHub PullRequestReviewCommentEvent at 2014-07-25 21:47:39
Repository: spark
Actor: mateiz
Payload: {"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/15426305","id":15426305,"diff_hunk":"@@ -105,24 +108,91 @@ class RangePartitioner[K : Ordering : ClassTag, V](\n \n   private var ordering = implicitly[Ordering[K]]\n \n+  @transient private[spark] var singlePass = true // for unit tests\n+\n   // An array of upper bounds for the first (partitions - 1) partitions\n   private var rangeBounds: Array[K] = {\n     if (partitions == 1) {\n-      Array()\n+      Array.empty\n     } else {\n-      val rddSize = rdd.count()\n-      val maxSampleSize = partitions * 20.0\n-      val frac = math.min(maxSampleSize / math.max(rddSize, 1), 1.0)\n-      val rddSample = rdd.sample(false, frac, 1).map(_._1).collect().sorted\n-      if (rddSample.length == 0) {\n-        Array()\n+      // This is the sample size we need to have roughly balanced output partitions.\n+      val sampleSize = 20.0 * partitions\n+      // Assume the input partitions are roughly balanced and over-sample a little bit.\n+      val sampleSizePerPartition = math.ceil(3.0 * sampleSize / rdd.partitions.size).toInt\n+      val shift = rdd.id\n+      val classTagK = classTag[K]\n+      val sketch = rdd.mapPartitionsWithIndex { (idx, iter) =>\n+        val seed = byteswap32(idx + shift)\n+        val (sample, n) = SamplingUtils.reservoirSampleAndCount(\n+          iter.map(_._1), sampleSizePerPartition, seed)(classTagK)\n+        Iterator((idx, n, sample))\n+      }.collect()\n+      var numItems = 0L\n+      sketch.foreach { case (_, n, _) =>\n+        numItems += n\n+      }","path":"core/src/main/scala/org/apache/spark/Partitioner.scala","position":49,"original_position":49,"commit_id":"c436d30143bc1efe2637edf57b785220ec3795e3","original_commit_id":"c436d30143bc1efe2637edf57b785220ec3795e3","user":{"login":"mateiz","id":228859,"avatar_url":"https://avatars.githubusercontent.com/u/228859?","gravatar_id":"17d3d634fc898bf3ae19444450af6805","url":"https://api.github.com/users/mateiz","html_url":"https://github.com/mateiz","followers_url":"https://api.github.com/users/mateiz/followers","following_url":"https://api.github.com/users/mateiz/following{/other_user}","gists_url":"https://api.github.com/users/mateiz/gists{/gist_id}","starred_url":"https://api.github.com/users/mateiz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mateiz/subscriptions","organizations_url":"https://api.github.com/users/mateiz/orgs","repos_url":"https://api.github.com/users/mateiz/repos","events_url":"https://api.github.com/users/mateiz/events{/privacy}","received_events_url":"https://api.github.com/users/mateiz/received_events","type":"User","site_admin":false},"body":"You can replace this with `val numItems = sketch.map(_._2).sum`","created_at":"2014-07-25T21:47:39Z","updated_at":"2014-07-25T21:47:39Z","html_url":"https://github.com/apache/spark/pull/1562#discussion_r15426305","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/1562","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/15426305"},"html":{"href":"https://github.com/apache/spark/pull/1562#discussion_r15426305"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/1562"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/1562","id":18815293,"html_url":"https://github.com/apache/spark/pull/1562","diff_url":"https://github.com/apache/spark/pull/1562.diff","patch_url":"https://github.com/apache/spark/pull/1562.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/1562","number":1562,"state":"open","title":"[SPARK-2568] RangePartitioner should run only one job if data is balanced","user":{"login":"mengxr","id":829644,"avatar_url":"https://avatars.githubusercontent.com/u/829644?","gravatar_id":"0e16fce91950d1b5a312649751ff1b14","url":"https://api.github.com/users/mengxr","html_url":"https://github.com/mengxr","followers_url":"https://api.github.com/users/mengxr/followers","following_url":"https://api.github.com/users/mengxr/following{/other_user}","gists_url":"https://api.github.com/users/mengxr/gists{/gist_id}","starred_url":"https://api.github.com/users/mengxr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mengxr/subscriptions","organizations_url":"https://api.github.com/users/mengxr/orgs","repos_url":"https://api.github.com/users/mengxr/repos","events_url":"https://api.github.com/users/mengxr/events{/privacy}","received_events_url":"https://api.github.com/users/mengxr/received_events","type":"User","site_admin":false},"body":"As of Spark 1.0, RangePartitioner goes through data twice: once to compute the count and once to do sampling. As a result, to do sortByKey, Spark goes through data 3 times (once to count, once to sample, and once to sort).\r\n\r\n`RangePartitioner` should go through data only once, collecting samples from input partitions as well as counting. If the data is balanced, this should give us a good sketch. If we see big partitions, we re-sample from them in order to collect enough items.\r\n\r\nThe downside is that we need to collect more from each partition in the first pass. An alternative solution is caching the intermediate result and decide whether to fetch the data after.\r\n","created_at":"2014-07-24T02:53:05Z","updated_at":"2014-07-25T21:47:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3437e463cdb0b28cfc05269a12b9f5d64dd6b407","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/1562/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/1562/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments/{number}","comments_url":"https://api.github.com/repos/apache/spark/issues/1562/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/c436d30143bc1efe2637edf57b785220ec3795e3","head":{"label":"mengxr:range-partitioner","ref":"range-partitioner","sha":"c436d30143bc1efe2637edf57b785220ec3795e3","user":{"login":"mengxr","id":829644,"avatar_url":"https://avatars.githubusercontent.com/u/829644?","gravatar_id":"0e16fce91950d1b5a312649751ff1b14","url":"https://api.github.com/users/mengxr","html_url":"https://github.com/mengxr","followers_url":"https://api.github.com/users/mengxr/followers","following_url":"https://api.github.com/users/mengxr/following{/other_user}","gists_url":"https://api.github.com/users/mengxr/gists{/gist_id}","starred_url":"https://api.github.com/users/mengxr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mengxr/subscriptions","organizations_url":"https://api.github.com/users/mengxr/orgs","repos_url":"https://api.github.com/users/mengxr/repos","events_url":"https://api.github.com/users/mengxr/events{/privacy}","received_events_url":"https://api.github.com/users/mengxr/received_events","type":"User","site_admin":false},"repo":{"id":17278743,"name":"spark","full_name":"mengxr/spark","owner":{"login":"mengxr","id":829644,"avatar_url":"https://avatars.githubusercontent.com/u/829644?","gravatar_id":"0e16fce91950d1b5a312649751ff1b14","url":"https://api.github.com/users/mengxr","html_url":"https://github.com/mengxr","followers_url":"https://api.github.com/users/mengxr/followers","following_url":"https://api.github.com/users/mengxr/following{/other_user}","gists_url":"https://api.github.com/users/mengxr/gists{/gist_id}","starred_url":"https://api.github.com/users/mengxr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mengxr/subscriptions","organizations_url":"https://api.github.com/users/mengxr/orgs","repos_url":"https://api.github.com/users/mengxr/repos","events_url":"https://api.github.com/users/mengxr/events{/privacy}","received_events_url":"https://api.github.com/users/mengxr/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mengxr/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/mengxr/spark","forks_url":"https://api.github.com/repos/mengxr/spark/forks","keys_url":"https://api.github.com/repos/mengxr/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mengxr/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mengxr/spark/teams","hooks_url":"https://api.github.com/repos/mengxr/spark/hooks","issue_events_url":"https://api.github.com/repos/mengxr/spark/issues/events{/number}","events_url":"https://api.github.com/repos/mengxr/spark/events","assignees_url":"https://api.github.com/repos/mengxr/spark/assignees{/user}","branches_url":"https://api.github.com/repos/mengxr/spark/branches{/branch}","tags_url":"https://api.github.com/repos/mengxr/spark/tags","blobs_url":"https://api.github.com/repos/mengxr/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mengxr/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mengxr/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/mengxr/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mengxr/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/mengxr/spark/languages","stargazers_url":"https://api.github.com/repos/mengxr/spark/stargazers","contributors_url":"https://api.github.com/repos/mengxr/spark/contributors","subscribers_url":"https://api.github.com/repos/mengxr/spark/subscribers","subscription_url":"https://api.github.com/repos/mengxr/spark/subscription","commits_url":"https://api.github.com/repos/mengxr/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/mengxr/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/mengxr/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/mengxr/spark/issues/comments/{number}","contents_url":"https://api.github.com/repos/mengxr/spark/contents/{+path}","compare_url":"https://api.github.com/repos/mengxr/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mengxr/spark/merges","archive_url":"https://api.github.com/repos/mengxr/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mengxr/spark/downloads","issues_url":"https://api.github.com/repos/mengxr/spark/issues{/number}","pulls_url":"https://api.github.com/repos/mengxr/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/mengxr/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/mengxr/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mengxr/spark/labels{/name}","releases_url":"https://api.github.com/repos/mengxr/spark/releases{/id}","created_at":"2014-02-28T07:27:59Z","updated_at":"2014-07-06T06:42:59Z","pushed_at":"2014-07-25T06:10:23Z","git_url":"git://github.com/mengxr/spark.git","ssh_url":"git@github.com:mengxr/spark.git","clone_url":"https://github.com/mengxr/spark.git","svn_url":"https://github.com/mengxr/spark","homepage":null,"size":75523,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"60f0ae3d87c84fd96e1f4d0abf5be1f51870e7ab","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?","gravatar_id":"a676c0bf448fcd49f588249ead719b4c","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?","gravatar_id":"a676c0bf448fcd49f588249ead719b4c","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":"2014-07-25T04:52:49Z","pushed_at":"2014-07-25T21:35:12Z","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":2385028,"stargazers_count":897,"watchers_count":897,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"forks_count":932,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":281,"forks":932,"open_issues":281,"watchers":897,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/1562"},"html":{"href":"https://github.com/apache/spark/pull/1562"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/1562"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/1562/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/1562/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/1562/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/c436d30143bc1efe2637edf57b785220ec3795e3"}}}}