{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/61655071","id":61655071,"diff_hunk":"@@ -0,0 +1,391 @@\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\n+\n+import java.{lang => jl}\n+import java.io.ObjectInputStream\n+import java.util.concurrent.atomic.AtomicLong\n+import javax.annotation.concurrent.GuardedBy\n+\n+import org.apache.spark.scheduler.AccumulableInfo\n+import org.apache.spark.util.Utils\n+\n+\n+private[spark] case class AccumulatorMetadata(\n+    id: Long,\n+    name: Option[String],\n+    countFailedValues: Boolean) extends Serializable\n+\n+\n+/**\n+ * The base class for accumulators, that can accumulate inputs of type `IN`, and produce output of\n+ * type `OUT`.\n+ */\n+abstract class NewAccumulator[IN, OUT] extends Serializable {\n+  private[spark] var metadata: AccumulatorMetadata = _\n+  private[this] var atDriverSide = true\n+\n+  private[spark] def register(\n+      sc: SparkContext,\n+      name: Option[String] = None,\n+      countFailedValues: Boolean = false): Unit = {\n+    if (this.metadata != null) {\n+      throw new IllegalStateException(\"Cannot register an Accumulator twice.\")\n+    }\n+    this.metadata = AccumulatorMetadata(AccumulatorContext.newId(), name, countFailedValues)\n+    AccumulatorContext.register(this)\n+    sc.cleaner.foreach(_.registerAccumulatorForCleanup(this))\n+  }\n+\n+  /**\n+   * Returns true if this accumulator has been registered.  Note that all accumulators must be\n+   * registered before ues, or it will throw exception.\n+   */\n+  final def isRegistered: Boolean =\n+    metadata != null && AccumulatorContext.originals.containsKey(metadata.id)\n+\n+  private def assertMetadataNotNull(): Unit = {\n+    if (metadata == null) {\n+      throw new IllegalAccessError(\"The metadata of this accumulator has not been assigned yet.\")\n+    }\n+  }\n+\n+  /**\n+   * Returns the id of this accumulator, can only be called after registration.\n+   */\n+  final def id: Long = {\n+    assertMetadataNotNull()\n+    metadata.id\n+  }\n+\n+  /**\n+   * Returns the name of this accumulator, can only be called after registration.\n+   */\n+  final def name: Option[String] = {\n+    assertMetadataNotNull()\n+    metadata.name\n+  }\n+\n+  /**\n+   * Whether to accumulate values from failed tasks. This is set to true for system and time\n+   * metrics like serialization time or bytes spilled, and false for things with absolute values\n+   * like number of input rows.  This should be used for internal metrics only.\n+   */\n+  private[spark] final def countFailedValues: Boolean = {\n+    assertMetadataNotNull()\n+    metadata.countFailedValues\n+  }\n+\n+  /**\n+   * Creates an [[AccumulableInfo]] representation of this [[NewAccumulator]] with the provided\n+   * values.\n+   */\n+  private[spark] def toInfo(update: Option[Any], value: Option[Any]): AccumulableInfo = {\n+    val isInternal = name.exists(_.startsWith(InternalAccumulator.METRICS_PREFIX))\n+    new AccumulableInfo(id, name, update, value, isInternal, countFailedValues)\n+  }\n+\n+  final private[spark] def isAtDriverSide: Boolean = atDriverSide\n+\n+  /**\n+   * Tells if this accumulator is zero value or not. e.g. for a counter accumulator, 0 is zero\n+   * value; for a list accumulator, Nil is zero value.\n+   */\n+  def isZero(): Boolean\n+\n+  /**\n+   * Creates a new copy of this accumulator, which is zero value. i.e. call `isZero` on the copy\n+   * must return true.\n+   */\n+  def copyAndReset(): NewAccumulator[IN, OUT]\n+\n+  /**\n+   * Takes the inputs and accumulates. e.g. it can be a simple `+=` for counter accumulator.\n+   */\n+  def add(v: IN): Unit\n+\n+  /**\n+   * Merges another same-type accumulator into this one and update its state, i.e. this should be\n+   * merge-in-place.\n+   */\n+  def merge(other: NewAccumulator[IN, OUT]): Unit\n+\n+  /**\n+   * Access this accumulator's current value; only allowed on driver.\n+   */\n+  final def value: OUT = {\n+    if (atDriverSide) {\n+      localValue\n+    } else {\n+      throw new UnsupportedOperationException(\"Can't read accumulator value in task\")\n+    }\n+  }\n+\n+  /**\n+   * Defines the current value of this accumulator.\n+   *\n+   * This is NOT the global value of the accumulator.  To get the global value after a\n+   * completed operation on the dataset, call `value`.\n+   */\n+  def localValue: OUT\n+\n+  // Called by Java when serializing an object\n+  final protected def writeReplace(): Any = {\n+    if (atDriverSide) {\n+      if (!isRegistered) {\n+        throw new UnsupportedOperationException(\n+          \"Accumulator must be registered before send to executor\")\n+      }\n+      val copy = copyAndReset()\n+      assert(copy.isZero(), \"copyAndReset must return a zero value copy\")\n+      copy.metadata = metadata\n+      copy\n+    } else {\n+      this\n+    }\n+  }\n+\n+  // Called by Java when deserializing an object\n+  private def readObject(in: ObjectInputStream): Unit = Utils.tryOrIOException {\n+    in.defaultReadObject()\n+    if (atDriverSide) {\n+      atDriverSide = false\n+\n+      // Automatically register the accumulator when it is deserialized with the task closure.\n+      // This is for external accumulators and internal ones that do not represent task level\n+      // metrics, e.g. internal SQL metrics, which are per-operator.\n+      val taskContext = TaskContext.get()\n+      if (taskContext != null) {\n+        taskContext.registerAccumulator(this)\n+      }\n+    } else {\n+      atDriverSide = true","path":"core/src/main/scala/org/apache/spark/NewAccumulator.scala","position":177,"original_position":177,"commit_id":"124568b3eeb7e0a657b2fbe4f54bb85543b7ffa3","original_commit_id":"124568b3eeb7e0a657b2fbe4f54bb85543b7ffa3","user":{"login":"tedyu","id":235188,"avatar_url":"https://avatars.githubusercontent.com/u/235188?v=3","gravatar_id":"","url":"https://api.github.com/users/tedyu","html_url":"https://github.com/tedyu","followers_url":"https://api.github.com/users/tedyu/followers","following_url":"https://api.github.com/users/tedyu/following{/other_user}","gists_url":"https://api.github.com/users/tedyu/gists{/gist_id}","starred_url":"https://api.github.com/users/tedyu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tedyu/subscriptions","organizations_url":"https://api.github.com/users/tedyu/orgs","repos_url":"https://api.github.com/users/tedyu/repos","events_url":"https://api.github.com/users/tedyu/events{/privacy}","received_events_url":"https://api.github.com/users/tedyu/received_events","type":"User","site_admin":false},"body":"Why is this assignment needed ?","created_at":"2016-04-29T22:52:33Z","updated_at":"2016-04-29T22:52:33Z","html_url":"https://github.com/apache/spark/pull/12612#discussion_r61655071","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/12612","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/61655071"},"html":{"href":"https://github.com/apache/spark/pull/12612#discussion_r61655071"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/12612"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/12612","id":67524556,"html_url":"https://github.com/apache/spark/pull/12612","diff_url":"https://github.com/apache/spark/pull/12612.diff","patch_url":"https://github.com/apache/spark/pull/12612.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/12612","number":12612,"state":"closed","locked":false,"title":"[SPARK-14654][CORE] New accumulator API","user":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nThis PR introduces a new accumulator API  which is much simpler than before:\r\n\r\n1. the type hierarchy is simplified, now we only have an `Accumulator` class\r\n2. Combine `initialValue` and `zeroValue` concepts into just one concept: `zeroValue`\r\n3. there in only one `register` method, the accumulator registration and cleanup registration are combined.\r\n4. the `id`,`name` and `countFailedValues` are combined into an `AccumulatorMetadata`, and is provided during registration.\r\n\r\n`SQLMetric` is a good example to show the simplicity of this new API.\r\n\r\nWhat we break:\r\n\r\n1. no `setValue` anymore. In the new API, the intermedia type can be different from the result type, it's very hard to implement a general `setValue`\r\n2. accumulator can't be serialized before registered.\r\n\r\nProblems need to be addressed in follow-ups:\r\n\r\n1. with this new API, `AccumulatorInfo` doesn't make a lot of sense, the partial output is not partial updates, we need to expose the intermediate value.\r\n2. `ExceptionFailure` should not carry the accumulator updates. Why do users care about accumulator updates for failed cases? It looks like we only use this feature to update the internal metrics, how about we sending a heartbeat to update internal metrics after the failure event?\r\n3. the public event `SparkListenerTaskEnd` carries a `TaskMetrics`. Ideally this `TaskMetrics` don't need to carry external accumulators, as the only method of `TaskMetrics` that can access external accumulators is `private[spark]`. However, `SQLListener` use it to retrieve sql metrics.\r\n\r\n## How was this patch tested?\r\n\r\nexisting tests","created_at":"2016-04-22T14:55:27Z","updated_at":"2016-04-29T22:52:34Z","closed_at":"2016-04-28T07:28:28Z","merged_at":null,"merge_commit_sha":"5c32f0a49077ea933424bdf022da24027065c45b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/12612/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/12612/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/12612/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/124568b3eeb7e0a657b2fbe4f54bb85543b7ffa3","head":{"label":"cloud-fan:acc","ref":"acc","sha":"124568b3eeb7e0a657b2fbe4f54bb85543b7ffa3","user":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"repo":{"id":19689699,"name":"spark","full_name":"cloud-fan/spark","owner":{"login":"cloud-fan","id":3182036,"avatar_url":"https://avatars.githubusercontent.com/u/3182036?v=3","gravatar_id":"","url":"https://api.github.com/users/cloud-fan","html_url":"https://github.com/cloud-fan","followers_url":"https://api.github.com/users/cloud-fan/followers","following_url":"https://api.github.com/users/cloud-fan/following{/other_user}","gists_url":"https://api.github.com/users/cloud-fan/gists{/gist_id}","starred_url":"https://api.github.com/users/cloud-fan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloud-fan/subscriptions","organizations_url":"https://api.github.com/users/cloud-fan/orgs","repos_url":"https://api.github.com/users/cloud-fan/repos","events_url":"https://api.github.com/users/cloud-fan/events{/privacy}","received_events_url":"https://api.github.com/users/cloud-fan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cloud-fan/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/cloud-fan/spark","forks_url":"https://api.github.com/repos/cloud-fan/spark/forks","keys_url":"https://api.github.com/repos/cloud-fan/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cloud-fan/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cloud-fan/spark/teams","hooks_url":"https://api.github.com/repos/cloud-fan/spark/hooks","issue_events_url":"https://api.github.com/repos/cloud-fan/spark/issues/events{/number}","events_url":"https://api.github.com/repos/cloud-fan/spark/events","assignees_url":"https://api.github.com/repos/cloud-fan/spark/assignees{/user}","branches_url":"https://api.github.com/repos/cloud-fan/spark/branches{/branch}","tags_url":"https://api.github.com/repos/cloud-fan/spark/tags","blobs_url":"https://api.github.com/repos/cloud-fan/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cloud-fan/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cloud-fan/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/cloud-fan/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cloud-fan/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/cloud-fan/spark/languages","stargazers_url":"https://api.github.com/repos/cloud-fan/spark/stargazers","contributors_url":"https://api.github.com/repos/cloud-fan/spark/contributors","subscribers_url":"https://api.github.com/repos/cloud-fan/spark/subscribers","subscription_url":"https://api.github.com/repos/cloud-fan/spark/subscription","commits_url":"https://api.github.com/repos/cloud-fan/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/cloud-fan/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/cloud-fan/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/cloud-fan/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/cloud-fan/spark/contents/{+path}","compare_url":"https://api.github.com/repos/cloud-fan/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cloud-fan/spark/merges","archive_url":"https://api.github.com/repos/cloud-fan/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cloud-fan/spark/downloads","issues_url":"https://api.github.com/repos/cloud-fan/spark/issues{/number}","pulls_url":"https://api.github.com/repos/cloud-fan/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/cloud-fan/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/cloud-fan/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cloud-fan/spark/labels{/name}","releases_url":"https://api.github.com/repos/cloud-fan/spark/releases{/id}","deployments_url":"https://api.github.com/repos/cloud-fan/spark/deployments","created_at":"2014-05-12T07:32:26Z","updated_at":"2016-01-26T22:38:29Z","pushed_at":"2016-04-29T06:57:00Z","git_url":"git://github.com/cloud-fan/spark.git","ssh_url":"git@github.com:cloud-fan/spark.git","clone_url":"https://github.com/cloud-fan/spark.git","svn_url":"https://github.com/cloud-fan/spark","homepage":null,"size":162582,"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":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"c5443560b7784473555e930a1ae65bf814ac27e5","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}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2016-04-29T22:37:01Z","pushed_at":"2016-04-29T22:48:05Z","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":473299,"stargazers_count":8354,"watchers_count":8354,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":7736,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":423,"forks":7736,"open_issues":423,"watchers":8354,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/12612"},"html":{"href":"https://github.com/apache/spark/pull/12612"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/12612"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/12612/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/12612/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/12612/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/124568b3eeb7e0a657b2fbe4f54bb85543b7ffa3"}}}}