{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/25102714","id":25102714,"diff_hunk":"@@ -728,6 +728,63 @@ def doStuff(self, rdd):\n \n </div>\n \n+### Understanding closures\n+One of the harder things about Spark is understanding the scope and life cycle of variables and methods when executing code across a cluster. A frequent source of confusion is shown below - where we perform a common task (incrementing a counter from inside of a for-loop). In our example, we look at `foreach()` but this same scenario will apply to any other RDD operations that modify variables outside of their scope. \n+\n+#### Example\n+\n+Consider the naiive RDD element sum below which, behaves completely differently when running spark in `local` mode (e.g. via the shell) and when deploying a Spark application to a cluster (e.g. via spark-submit to YARN): \n+\n+<div class=\"codetabs\">\n+\n+<div data-lang=\"scala\"  markdown=\"1\">\n+{% highlight scala %}\n+var counter = 0\n+var rdd = sc.parallelize(data)\n+rdd.foreach(x => counter += x)\n+\n+println(\"Counter value: \" + counter)\n+{% endhighlight %}\n+</div>\n+\n+<div data-lang=\"java\"  markdown=\"1\">\n+{% highlight java %}\n+int counter = 0;\n+JavaRDD<Integer> rdd = sc.parallelize(data); \n+rdd.foreach(x -> counter += x;)\n+\n+println(\"Counter value: \" + counter)\n+{% endhighlight %}\n+</div>\n+\n+<div data-lang=\"python\"  markdown=\"1\">\n+{% highlight python %}\n+counter = 0\n+rdd = sc.parallelize(data)\n+rdd.foreach(lambda x => counter+= x)\n+\n+print(\"Counter value: \" + counter)\n+\n+{% endhighlight %}\n+</div>\n+\n+</div>\n+\n+#### Local vs. cluster modes\n+\n+In local mode, the above code will correctly sum the values within the rdd and store it in **counter**. This is because both the RDD and the variable **counter** are in the same memory on the driver node. \n+\n+However, in `cluster` mode, what happens is more complicated, and the above code will not work correctly. In `cluster` mode, Spark breaks up the processing of RDD operations into tasks - each of which is operated on by a seperate executor. Prior to execution, Spark computes the **closure**. The closure is those variables and methods which must be visible for the remote executor (running on a seperate worker node) to perform its computations on the RDD (in this case `foreach()`). This closure is serialized and sent to each executor. \n+\n+The problem here is that the variables within the closure sent to each executor are now copies and thus, when **counter** is referenced within the `foreach` function, it's no longer the **counter** on the driver node. There is still a **counter** in the memory of the driver node but this is no longer visible to the executors! The executors only sees the copy from the serialized closure. Thus, the final value of **counter** will still be zero since all operations on **counter** were referencing the value within the serialized closure.  \n+\n+The one exception to this is when the variable being modified is an `Accumulator`. Accumulators in Spark are used specifically to provide a mechanism for safely updating a variable when execution is split up across worker nodes in a cluster. The Accumulators section of this guide discusses these in more detail.  ","path":"docs/programming-guide.md","position":54,"original_position":54,"commit_id":"5dbbda5010c84d2f8a6010920b6376ed417ee2ce","original_commit_id":"5dbbda5010c84d2f8a6010920b6376ed417ee2ce","user":{"login":"JoshRosen","id":50748,"avatar_url":"https://avatars.githubusercontent.com/u/50748?v=3","gravatar_id":"","url":"https://api.github.com/users/JoshRosen","html_url":"https://github.com/JoshRosen","followers_url":"https://api.github.com/users/JoshRosen/followers","following_url":"https://api.github.com/users/JoshRosen/following{/other_user}","gists_url":"https://api.github.com/users/JoshRosen/gists{/gist_id}","starred_url":"https://api.github.com/users/JoshRosen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JoshRosen/subscriptions","organizations_url":"https://api.github.com/users/JoshRosen/orgs","repos_url":"https://api.github.com/users/JoshRosen/repos","events_url":"https://api.github.com/users/JoshRosen/events{/privacy}","received_events_url":"https://api.github.com/users/JoshRosen/received_events","type":"User","site_admin":false},"body":"This could actually link to the accumulators section.","created_at":"2015-02-20T21:03:21Z","updated_at":"2015-02-20T21:03:21Z","html_url":"https://github.com/apache/spark/pull/4696#discussion_r25102714","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/4696","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/25102714"},"html":{"href":"https://github.com/apache/spark/pull/4696#discussion_r25102714"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/4696"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/4696","id":29634909,"html_url":"https://github.com/apache/spark/pull/4696","diff_url":"https://github.com/apache/spark/pull/4696.diff","patch_url":"https://github.com/apache/spark/pull/4696.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/4696","number":4696,"state":"open","locked":false,"title":"[SPARK-4423] Improve foreach() documentation to avoid confusion between local- and cluster-mode behavior","user":{"login":"ilganeli","id":1483345,"avatar_url":"https://avatars.githubusercontent.com/u/1483345?v=3","gravatar_id":"","url":"https://api.github.com/users/ilganeli","html_url":"https://github.com/ilganeli","followers_url":"https://api.github.com/users/ilganeli/followers","following_url":"https://api.github.com/users/ilganeli/following{/other_user}","gists_url":"https://api.github.com/users/ilganeli/gists{/gist_id}","starred_url":"https://api.github.com/users/ilganeli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilganeli/subscriptions","organizations_url":"https://api.github.com/users/ilganeli/orgs","repos_url":"https://api.github.com/users/ilganeli/repos","events_url":"https://api.github.com/users/ilganeli/events{/privacy}","received_events_url":"https://api.github.com/users/ilganeli/received_events","type":"User","site_admin":false},"body":"Hi all - I've added a writeup on how closures work within Spark to help clarify the general case for this problem and similar problems. I hope this addresses the issue and would love any feedback. ","created_at":"2015-02-19T18:14:15Z","updated_at":"2015-02-20T21:03:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e75d001fd9e6ef46d5cb0adac1a89ffad2e23060","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/4696/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/4696/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments/{number}","comments_url":"https://api.github.com/repos/apache/spark/issues/4696/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/5dbbda5010c84d2f8a6010920b6376ed417ee2ce","head":{"label":"ilganeli:SPARK-4423","ref":"SPARK-4423","sha":"5dbbda5010c84d2f8a6010920b6376ed417ee2ce","user":{"login":"ilganeli","id":1483345,"avatar_url":"https://avatars.githubusercontent.com/u/1483345?v=3","gravatar_id":"","url":"https://api.github.com/users/ilganeli","html_url":"https://github.com/ilganeli","followers_url":"https://api.github.com/users/ilganeli/followers","following_url":"https://api.github.com/users/ilganeli/following{/other_user}","gists_url":"https://api.github.com/users/ilganeli/gists{/gist_id}","starred_url":"https://api.github.com/users/ilganeli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilganeli/subscriptions","organizations_url":"https://api.github.com/users/ilganeli/orgs","repos_url":"https://api.github.com/users/ilganeli/repos","events_url":"https://api.github.com/users/ilganeli/events{/privacy}","received_events_url":"https://api.github.com/users/ilganeli/received_events","type":"User","site_admin":false},"repo":{"id":24374828,"name":"spark","full_name":"ilganeli/spark","owner":{"login":"ilganeli","id":1483345,"avatar_url":"https://avatars.githubusercontent.com/u/1483345?v=3","gravatar_id":"","url":"https://api.github.com/users/ilganeli","html_url":"https://github.com/ilganeli","followers_url":"https://api.github.com/users/ilganeli/followers","following_url":"https://api.github.com/users/ilganeli/following{/other_user}","gists_url":"https://api.github.com/users/ilganeli/gists{/gist_id}","starred_url":"https://api.github.com/users/ilganeli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilganeli/subscriptions","organizations_url":"https://api.github.com/users/ilganeli/orgs","repos_url":"https://api.github.com/users/ilganeli/repos","events_url":"https://api.github.com/users/ilganeli/events{/privacy}","received_events_url":"https://api.github.com/users/ilganeli/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ilganeli/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/ilganeli/spark","forks_url":"https://api.github.com/repos/ilganeli/spark/forks","keys_url":"https://api.github.com/repos/ilganeli/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ilganeli/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ilganeli/spark/teams","hooks_url":"https://api.github.com/repos/ilganeli/spark/hooks","issue_events_url":"https://api.github.com/repos/ilganeli/spark/issues/events{/number}","events_url":"https://api.github.com/repos/ilganeli/spark/events","assignees_url":"https://api.github.com/repos/ilganeli/spark/assignees{/user}","branches_url":"https://api.github.com/repos/ilganeli/spark/branches{/branch}","tags_url":"https://api.github.com/repos/ilganeli/spark/tags","blobs_url":"https://api.github.com/repos/ilganeli/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ilganeli/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ilganeli/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/ilganeli/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ilganeli/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/ilganeli/spark/languages","stargazers_url":"https://api.github.com/repos/ilganeli/spark/stargazers","contributors_url":"https://api.github.com/repos/ilganeli/spark/contributors","subscribers_url":"https://api.github.com/repos/ilganeli/spark/subscribers","subscription_url":"https://api.github.com/repos/ilganeli/spark/subscription","commits_url":"https://api.github.com/repos/ilganeli/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/ilganeli/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/ilganeli/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/ilganeli/spark/issues/comments/{number}","contents_url":"https://api.github.com/repos/ilganeli/spark/contents/{+path}","compare_url":"https://api.github.com/repos/ilganeli/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ilganeli/spark/merges","archive_url":"https://api.github.com/repos/ilganeli/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ilganeli/spark/downloads","issues_url":"https://api.github.com/repos/ilganeli/spark/issues{/number}","pulls_url":"https://api.github.com/repos/ilganeli/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/ilganeli/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/ilganeli/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ilganeli/spark/labels{/name}","releases_url":"https://api.github.com/repos/ilganeli/spark/releases{/id}","created_at":"2014-09-23T14:30:54Z","updated_at":"2014-12-17T18:17:41Z","pushed_at":"2015-02-20T19:43:16Z","git_url":"git://github.com/ilganeli/spark.git","ssh_url":"git@github.com:ilganeli/spark.git","clone_url":"https://github.com/ilganeli/spark.git","svn_url":"https://github.com/ilganeli/spark","homepage":null,"size":90891,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"38e624a732b18e01ad2e7a499ce0bb0d7acdcdf6","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-02-20T12:41:35Z","pushed_at":"2015-02-20T10:31:48Z","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":1311774,"stargazers_count":2940,"watchers_count":2940,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":2563,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":263,"forks":2563,"open_issues":263,"watchers":2940,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/4696"},"html":{"href":"https://github.com/apache/spark/pull/4696"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/4696"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/4696/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/4696/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/4696/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/5dbbda5010c84d2f8a6010920b6376ed417ee2ce"}}}}