Apache Spark - A unified analytics engine for large-scale data processing https://spark.apache.org/
Find a file
yangjie01 e7a99762a1 [SPARK-55887][CONNECT][FOLLOWUP] Fix flaky SparkConnectServiceSuite limit/tail tests
### What changes were proposed in this pull request?
This PR is intended to fix the flaky test in `SparkConnectServiceSuite` reported by zhengruifeng

hey occasionally (~5%) fails with
```

❯ test 'SPARK-55887: Use executeCollect for tail to avoid full scan'
        org.scalatest.exceptions.TestFailedException: 0 was not greater than or equal to 1
      at org.scalatest.Assertions.newAssertionFailedException(Assertions.scala:472)
      at org.scalatest.Assertions.newAssertionFailedException$(Assertions.scala:471)
      at org.scalatest.Assertions$.newAssertionFailedException(Assertions.scala:1231)
      at org.scalatest.Assertions$AssertionsHelper.macroAssert(Assertions.scala:1295)
      at org.apache.spark.sql.connect.planner.SparkConnectServiceSuite.$anonfun$new$72(SparkConnectServiceSuite.scala:1234)
      at org.scalatest.enablers.Timed$anon$1.timeoutAfter(Timed.scala:127)
      at org.scalatest.concurrent.TimeLimits$.failAfterImpl(TimeLimits.scala:282)
      at org.scalatest.concurrent.TimeLimits.failAfter(TimeLimits.scala:231)
      at org.scalatest.concurrent.TimeLimits.failAfter$(TimeLimits.scala:230)
      at org.apache.spark.SparkFunSuite.failAfter(SparkFunSuite.scala:77)
      at org.apache.spark.SparkFunSuite.testWithTimeout(SparkFunSuite.scala:480)
      at org.apache.spark.SparkFunSuite.$anonfun$test$1(SparkFunSuite.scala:266)
      at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
      at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
      at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
      at org.scalatest.Transformer.apply(Transformer.scala:22)
      at org.scalatest.Transformer.apply(Transformer.scala:20)
      at org.scalatest.funsuite.AnyFunSuiteLike$anon$1.apply(AnyFunSuiteLike.scala:226)
      at org.scalatest.funsuite.AnyFunSuiteLike$anon$1.apply(AnyFunSuiteLike.scala:224)
      at org.apache.spark.SparkFunSuite.runWithCredentials(SparkFunSuite.scala:545)
      at org.apache.spark.SparkFunSuite.withFixture(SparkFunSuite.scala:159)
      at org.scalatest.funsuite.AnyFunSuiteLike.invokeWithFixture$1(AnyFunSuiteLike.scala:224)
      at org.scalatest.funsuite.AnyFunSuiteLike.$anonfun$runTest$1(AnyFunSuiteLike.scala:236)
      at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
      at org.scalatest.funsuite.AnyFunSuiteLike.runTest(AnyFunSuiteLike.scala:236)
      at org.scalatest.funsuite.AnyFunSuiteLike.runTest$(AnyFunSuiteLike.scala:218)
      at org.apache.spark.SparkFunSuite.org$scalatest$BeforeAndAfterEach$super$runTest(SparkFunSuite.scala:77)
      at org.scalatest.BeforeAndAfterEach.runTest(BeforeAndAfterEach.scala:234)
      at org.scalatest.BeforeAndAfterEach.runTest$(BeforeAndAfterEach.scala:227)
      at org.apache.spark.SparkFunSuite.runTest(SparkFunSuite.scala:77)
      at org.scalatest.funsuite.AnyFunSuiteLike.$anonfun$runTests$1(AnyFunSuiteLike.scala:269)
      at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:413)
      at scala.collection.immutable.List.foreach(List.scala:334)
      at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
      at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:396)
      at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:475)
      at org.scalatest.funsuite.AnyFunSuiteLike.runTests(AnyFunSuiteLike.scala:269)
      at org.scalatest.funsuite.AnyFunSuiteLike.runTests$(AnyFunSuiteLike.scala:268)
      at org.scalatest.funsuite.AnyFunSuite.runTests(AnyFunSuite.scala:1564)
      at org.scalatest.Suite.run(Suite.scala:1114)
      at org.scalatest.Suite.run$(Suite.scala:1096)
      at org.scalatest.funsuite.AnyFunSuite.org$scalatest$funsuite$AnyFunSuiteLike$super$run(AnyFunSuite.scala:1564)
      at org.scalatest.funsuite.AnyFunSuiteLike.$anonfun$run$1(AnyFunSuiteLike.scala:273)
      at org.scalatest.SuperEngine.runImpl(Engine.scala:535)
      at org.scalatest.funsuite.AnyFunSuiteLike.run(AnyFunSuiteLike.scala:273)
      at org.scalatest.funsuite.AnyFunSuiteLike.run$(AnyFunSuiteLike.scala:272)
      at org.apache.spark.SparkFunSuite.org$scalatest$BeforeAndAfterAll$super$run(SparkFunSuite.scala:77)
      at org.scalatest.BeforeAndAfterAll.liftedTree1$1(BeforeAndAfterAll.scala:213)
      at org.scalatest.BeforeAndAfterAll.run(BeforeAndAfterAll.scala:210)
      at org.scalatest.BeforeAndAfterAll.run$(BeforeAndAfterAll.scala:208)
      at org.apache.spark.SparkFunSuite.run(SparkFunSuite.scala:517)
      at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:47)
      at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13(Runner.scala:1321)
      at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13$adapted(Runner.scala:1315)
      at scala.collection.immutable.List.foreach(List.scala:334)
      at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1315)
      at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:992)
      at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:970)
      at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1481)
      at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:970)
      at org.scalatest.tools.Runner$.run(Runner.scala:798)
      at com.databricks.scalatest.ScalaTestRunnerWrapper$.main(ScalaTestRunnerWrapper.scala:56)
      at com.databricks.scalatest.ScalaTestRunnerWrapper.main(ScalaTestRunnerWrapper.scala)

```

the main changes as follows:
- Use `AtomicInteger` instead of `var` for thread-safe task counting in `SparkListener.onTaskEnd`, which runs on a different thread
- Add `volatile` to the `done` flag shared between the main thread and the `StreamObserver` callback
- Call `listenerBus.waitUntilEmpty()` before asserting task counts to ensure all listener events have been processed

### Why are the changes needed?
The tests added in SPARK-55887 have race conditions that can cause flaky failures:
1. `numTasks` is incremented from the listener bus thread but read from the test thread without synchronization — a plain `var` is not thread-safe
2. The `done` flag is written by the `StreamObserver.onCompleted` callback but read by the main thread — without `volatile`, the main thread may not see the update
3. Task count assertions can execute before the listener bus finishes delivering all `onTaskEnd` events, leading to undercounted tasks

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
- Pass GitHub Actions

### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 4.6

Closes #54904 from LuciferYang/SPARK-55887-FOLLOWUP.

Authored-by: yangjie01 <yangjie01@baidu.com>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
2026-03-19 09:32:29 -07:00
.github
.mvn
assembly
bin
binder
build
common [SPARK-55948][SQL] Add DSv2 CDC connector API, analyzer resolution, and SQL CHANGES clause 2026-03-18 15:49:27 -07:00
conf
connector [SPARK-56043][SQL] Wrap NullPointerException from Avro 1.12.x ParseContext.resolve() in SchemaParseException 2026-03-19 07:23:52 +09:00
core
data
dev
docs [SPARK-55948][SQL] Add DSv2 CDC connector API, analyzer resolution, and SQL CHANGES clause 2026-03-18 15:49:27 -07:00
examples
graphx
hadoop-cloud
launcher
licenses
licenses-binary
mllib
mllib-local
project
python [SPARK-56075][PYTHON] Remove a batch of dead python error classes 2026-03-19 09:31:03 -07:00
R
repl
resource-managers
sbin
sql [SPARK-55887][CONNECT][FOLLOWUP] Fix flaky SparkConnectServiceSuite limit/tail tests 2026-03-19 09:32:29 -07:00
streaming
tools
ui-test
.asf.yaml
.gitattributes
.gitignore
.nojekyll
.pre-commit-config.yaml
.sbtopts
AGENTS.md
CONTRIBUTING.md
LICENSE
LICENSE-binary
NOTICE
NOTICE-binary
pom.xml
pyproject.toml
README.md
scalastyle-config.xml

Apache Spark

Spark is a unified analytics engine for large-scale data processing. It provides high-level APIs in Scala, Java, Python, and R (Deprecated), and an optimized engine that supports general computation graphs for data analysis. It also supports a rich set of higher-level tools including Spark SQL for SQL and DataFrames, pandas API on Spark for pandas workloads, MLlib for machine learning, GraphX for graph processing, and Structured Streaming for stream processing.

License Maven Central Java GitHub Actions Build PySpark Coverage PyPI Downloads

Online Documentation

You can find the latest Spark documentation, including a programming guide, on the project web page. This README file only contains basic setup instructions.

Build Pipeline Status

Branch Status
master GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
branch-4.1 GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
branch-4.0 GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
GitHub Actions Build
branch-3.5 GitHub Actions Build
GitHub Actions Build

Building Spark

Spark is built using Apache Maven. To build Spark and its example programs, run:

./build/mvn -DskipTests clean package

(You do not need to do this if you downloaded a pre-built package.)

More detailed documentation is available from the project site, at "Building Spark".

For general development tips, including info on developing Spark using an IDE, see "Useful Developer Tools".

Interactive Scala Shell

The easiest way to start using Spark is through the Scala shell:

./bin/spark-shell

Try the following command, which should return 1,000,000,000:

scala> spark.range(1000 * 1000 * 1000).count()

Interactive Python Shell

Alternatively, if you prefer Python, you can use the Python shell:

./bin/pyspark

And run the following command, which should also return 1,000,000,000:

>>> spark.range(1000 * 1000 * 1000).count()

Example Programs

Spark also comes with several sample programs in the examples directory. To run one of them, use ./bin/run-example <class> [params]. For example:

./bin/run-example SparkPi

will run the Pi example locally.

You can set the MASTER environment variable when running examples to submit examples to a cluster. This can be spark:// URL, "yarn" to run on YARN, and "local" to run locally with one thread, or "local[N]" to run locally with N threads. You can also use an abbreviated class name if the class is in the examples package. For instance:

MASTER=spark://host:7077 ./bin/run-example SparkPi

Many of the example programs print usage help if no params are given.

Running Tests

Testing first requires building Spark. Once Spark is built, tests can be run using:

./dev/run-tests

Please see the guidance on how to run tests for a module, or individual tests.

There is also a Kubernetes integration test, see resource-managers/kubernetes/integration-tests/README.md

A Note About Hadoop Versions

Spark uses the Hadoop core library to talk to HDFS and other Hadoop-supported storage systems. Because the protocols have changed in different versions of Hadoop, you must build Spark against the same version that your cluster runs.

Please refer to the build documentation at "Specifying the Hadoop Version and Enabling YARN" for detailed guidance on building for a particular distribution of Hadoop, including building for particular Hive and Hive Thriftserver distributions.

Configuration

Please refer to the Configuration Guide in the online documentation for an overview on how to configure Spark.

Contributing

Please review the Contribution to Spark guide for information on how to get started contributing to the project.