Skip to content
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

[SPARK-27234][SS][PYTHON] Use InheritableThreadLocal for current epoch in EpochTracker (to support Python UDFs) #24946

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ class ContinuousCoalesceRDD(
context.getLocalProperty(ContinuousExecution.START_EPOCH_KEY).toLong)
while (!context.isInterrupted() && !context.isCompleted()) {
writer.write(prev.compute(prevSplit, context).asInstanceOf[Iterator[UnsafeRow]])
// Note that current epoch is a non-inheritable thread local, so each writer thread
// can properly increment its own epoch without affecting the main task thread.
// Note that current epoch is a inheritable thread local but makes another instance,
// so each writer thread can properly increment its own epoch without affecting
// the main task thread.
EpochTracker.incrementCurrentEpoch()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ import java.util.concurrent.atomic.AtomicLong
object EpochTracker {
// The current epoch. Note that this is a shared reference; ContinuousWriteRDD.compute() will
// update the underlying AtomicLong as it finishes epochs. Other code should only read the value.
private val currentEpoch: ThreadLocal[AtomicLong] = new ThreadLocal[AtomicLong] {
override def initialValue() = new AtomicLong(-1)
private val currentEpoch: InheritableThreadLocal[AtomicLong] = {
new InheritableThreadLocal[AtomicLong] {
override protected def childValue(parent: AtomicLong): AtomicLong = {
// Note: make another instance so that changes in the parent epoch aren't reflected in
// those in the children threads. This is required at `ContinuousCoalesceRDD`.
new AtomicLong(parent.get)
}
override def initialValue() = new AtomicLong(-1)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ContinuousSuiteBase extends StreamTest {
}

class ContinuousSuite extends ContinuousSuiteBase {
import IntegratedUDFTestUtils._
import testImplicits._

test("basic") {
Expand Down Expand Up @@ -252,6 +253,26 @@ class ContinuousSuite extends ContinuousSuiteBase {
assert(expected.map(Row(_)).subsetOf(results.toSet),
s"Result set ${results.toSet} are not a superset of $expected!")
}

Seq(TestScalaUDF("udf"), TestPythonUDF("udf"), TestScalarPandasUDF("udf")).foreach { udf =>
test(s"continuous mode with various UDFs - ${udf.prettyName}") {
assume(
shouldTestScalarPandasUDFs && udf.isInstanceOf[TestScalarPandasUDF] ||
shouldTestPythonUDFs && udf.isInstanceOf[TestPythonUDF] ||
udf.isInstanceOf[TestScalaUDF])

val input = ContinuousMemoryStream[Int]
val df = input.toDF()

testStream(df.select(udf(df("value")).cast("int")))(
AddData(input, 0, 1, 2),
CheckAnswer(0, 1, 2),
StopStream,
AddData(input, 3, 4, 5),
StartStream(),
CheckAnswer(0, 1, 2, 3, 4, 5))
}
}
}

class ContinuousStressSuite extends ContinuousSuiteBase {
Expand Down