-
Notifications
You must be signed in to change notification settings - Fork 28.7k
SPARK-729: Closures not always serialized at capture time #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d5947b3
Ensure assertions in Graph.apply are asserted.
willb 21b4b06
Test cases for SPARK-897.
willb d8df3db
Adds proactive closure-serializablilty checking
willb 4ecf841
Make proactive serializability checking optional.
willb d6e8dd6
Don't check serializability of DStream transforms.
willb 12c63a7
Added tests for variable capture in closures
willb 8ee3ee7
Predictable closure environment capture
willb 12ef6e3
Skip proactive closure capture for runJob
willb 97e9d91
Split closure-serializability failure tests
willb 9b56ce0
Added array-element capture test
willb b3d9c86
Fixed style issues in tests
willb f4cafa0
Stylistic changes and cleanups
willb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -896,7 +896,9 @@ class SparkContext( | |
require(p >= 0 && p < rdd.partitions.size, s"Invalid partition requested: $p") | ||
} | ||
val callSite = getCallSite | ||
val cleanedFunc = clean(func) | ||
// There's no need to check this function for serializability, | ||
// since it will be run right away. | ||
val cleanedFunc = clean(func, false) | ||
logInfo("Starting job: " + callSite) | ||
val start = System.nanoTime | ||
dagScheduler.runJob(rdd, cleanedFunc, partitions, callSite, allowLocal, | ||
|
@@ -1026,14 +1028,18 @@ class SparkContext( | |
def cancelAllJobs() { | ||
dagScheduler.cancelAllJobs() | ||
} | ||
|
||
/** | ||
* Clean a closure to make it ready to serialized and send to tasks | ||
* (removes unreferenced variables in $outer's, updates REPL variables) | ||
* | ||
* @param f closure to be cleaned and optionally serialized | ||
* @param captureNow whether or not to serialize this closure and capture any free | ||
* variables immediately; defaults to true. If this is set and f is not serializable, | ||
* it will raise an exception. | ||
*/ | ||
private[spark] def clean[F <: AnyRef](f: F): F = { | ||
ClosureCleaner.clean(f) | ||
f | ||
private[spark] def clean[F <: AnyRef : ClassTag](f: F, captureNow: Boolean = true): F = { | ||
ClosureCleaner.clean(f, captureNow) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding two methods, just add a default argument to the first one ( |
||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
core/src/test/scala/org/apache/spark/serializer/ProactiveClosureSerializationSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.serializer; | ||
|
||
import java.io.NotSerializableException | ||
|
||
import org.scalatest.FunSuite | ||
|
||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.SparkException | ||
import org.apache.spark.SharedSparkContext | ||
|
||
/* A trivial (but unserializable) container for trivial functions */ | ||
class UnserializableClass { | ||
def op[T](x: T) = x.toString | ||
|
||
def pred[T](x: T) = x.toString.length % 2 == 0 | ||
} | ||
|
||
class ProactiveClosureSerializationSuite extends FunSuite with SharedSparkContext { | ||
|
||
def fixture = (sc.parallelize(0 until 1000).map(_.toString), new UnserializableClass) | ||
|
||
test("throws expected serialization exceptions on actions") { | ||
val (data, uc) = fixture | ||
|
||
val ex = intercept[SparkException] { | ||
data.map(uc.op(_)).count | ||
} | ||
|
||
assert(ex.getMessage.matches(".*Task not serializable.*")) | ||
} | ||
|
||
// There is probably a cleaner way to eliminate boilerplate here, but we're | ||
// iterating over a map from transformation names to functions that perform that | ||
// transformation on a given RDD, creating one test case for each | ||
|
||
for (transformation <- | ||
Map("map" -> map _, "flatMap" -> flatMap _, "filter" -> filter _, "mapWith" -> mapWith _, | ||
"mapPartitions" -> mapPartitions _, "mapPartitionsWithIndex" -> mapPartitionsWithIndex _, | ||
"mapPartitionsWithContext" -> mapPartitionsWithContext _, "filterWith" -> filterWith _)) { | ||
val (name, xf) = transformation | ||
|
||
test(s"$name transformations throw proactive serialization exceptions") { | ||
val (data, uc) = fixture | ||
|
||
val ex = intercept[SparkException] { | ||
xf(data, uc) | ||
} | ||
|
||
assert(ex.getMessage.matches(".*Task not serializable.*"), s"RDD.$name doesn't proactively throw NotSerializableException") | ||
} | ||
} | ||
|
||
def map(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.map(y => uc.op(y)) | ||
|
||
def mapWith(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.mapWith(x => x.toString)((x,y) => x + uc.op(y)) | ||
|
||
def flatMap(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.flatMap(y=>Seq(uc.op(y))) | ||
|
||
def filter(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.filter(y=>uc.pred(y)) | ||
|
||
def filterWith(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.filterWith(x => x.toString)((x,y) => uc.pred(y)) | ||
|
||
def mapPartitions(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.mapPartitions(_.map(y => uc.op(y))) | ||
|
||
def mapPartitionsWithIndex(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.mapPartitionsWithIndex((_, it) => it.map(y => uc.op(y))) | ||
|
||
def mapPartitionsWithContext(x: RDD[String], uc: UnserializableClass): RDD[String] = | ||
x.mapPartitionsWithContext((_, it) => it.map(y => uc.op(y))) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO you might as well clone it here too, because it could be modified in other threads while the job is running.