Skip to content

SPARK-2282: Reuse Socket for sending accumulator updates to Pyspark - #1503

Closed
aarondav wants to merge 1 commit into
apache:masterfrom
aarondav:accum
Closed

aarondav wants to merge 1 commit into
apache:masterfrom
aarondav:accum

Conversation

@aarondav

Copy link
Copy Markdown
Contributor

Prior to this change, every PySpark task completion opened a new socket to the accumulator server, passed its updates through, and then quit. I'm not entirely sure why PySpark always sends accumulator updates, but regardless this causes a very rapid buildup of ephemeral TCP connections that remain in the TCP_WAIT state for around a minute before being cleaned up.

Rather than trying to allow these sockets to be cleaned up faster, this patch simply reuses the connection between tasks completions (since they're fed updates in a single-threaded manner by the DAGScheduler anyway).

The only tricky part here was making sure that the AccumulatorServer was able to shutdown in a timely manner (i.e., stop polling for new data), and this was accomplished via minor feats of magic.

I have confirmed that this patch eliminates the buildup of ephemeral sockets due to the accumulator updates. However, I did note that there were still significant sockets being created against the PySpark daemon port, but my machine was not able to create enough sockets fast enough to fail. This may not be the last time we've seen this issue, though.

Prior to this change, every PySpark task completion opened a new
socket to the accumulator server, passed its updates through, and
then quit. I'm not entirely sure why PySpark always sends accumulator
updates, but regardless this causes a very rapid buildup of ephemeral
TCP connections that remain in the TCP_WAIT state for around a minute
before being cleaned up.

Rather than trying to allow these sockets to be cleaned up faster, this
patch simply reuses the connection between tasks completions (since they're
fed updates in a single-threaded manner by the DAGScheduler anyway).

The only tricky part here was making sure that the AccumulatorServer was
able to shutdown in a timely manner (i.e., stop polling for new data), and
this was accomplished via minor feats of magic.
@SparkQA

SparkQA commented Jul 20, 2014

Copy link
Copy Markdown

QA tests have started for PR 1503. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/16884/consoleFull

@SparkQA

SparkQA commented Jul 21, 2014

Copy link
Copy Markdown

QA results for PR 1503:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds the following public classes (experimental):
class AccumulatorServer(SocketServer.TCPServer):

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/16884/consoleFull

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this socket will be closed when it's garbage-collected? There's only one of these sockets per Python SparkContext, so this shouldn't cause problems.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket will be closed on the Python side when the AccumulatorServer is shutdown().

@JoshRosen

Copy link
Copy Markdown
Contributor

Why does Python need to poll to check whether the socket is still open?

@aarondav

Copy link
Copy Markdown
Contributor Author

The Python side polls to see whether it should shutdown due to an external signal, not whether or not the socket is still open.

@JoshRosen

Copy link
Copy Markdown
Contributor

I've merged this. Thanks!

@asfgit asfgit closed this in ef4ff00 Jul 31, 2014
xiliu82 pushed a commit to xiliu82/spark that referenced this pull request Sep 4, 2014
Prior to this change, every PySpark task completion opened a new socket to the accumulator server, passed its updates through, and then quit. I'm not entirely sure why PySpark always sends accumulator updates, but regardless this causes a very rapid buildup of ephemeral TCP connections that remain in the TCP_WAIT state for around a minute before being cleaned up.

Rather than trying to allow these sockets to be cleaned up faster, this patch simply reuses the connection between tasks completions (since they're fed updates in a single-threaded manner by the DAGScheduler anyway).

The only tricky part here was making sure that the AccumulatorServer was able to shutdown in a timely manner (i.e., stop polling for new data), and this was accomplished via minor feats of magic.

I have confirmed that this patch eliminates the buildup of ephemeral sockets due to the accumulator updates. However, I did note that there were still significant sockets being created against the PySpark daemon port, but my machine was not able to create enough sockets fast enough to fail. This may not be the last time we've seen this issue, though.

Author: Aaron Davidson <aaron@databricks.com>

Closes apache#1503 from aarondav/accum and squashes the following commits:

b3e12f7 [Aaron Davidson] SPARK-2282: Reuse Socket for sending accumulator updates to Pyspark
MaxGekk added a commit to MaxGekk/spark that referenced this pull request Nov 11, 2026
### What changes were proposed in this pull request?

`VarkaCoverageCompositionFuzzSuite` checks that its shrinker works by planting a bug and expecting a wide random projection to fail. The planted bug is the `misdescribeAdd` emit option, which makes the emitter describe a `date_add` wrongly, so a kernel that holds an `AddDays` fails with a `NoSuchMethodError` in the generated class. The helper `shrunkPlanted` draws ten wide projections (150 to 300 coverage rows each), turns the option on, takes the first draw that fails and shrinks it. Three tests use it: the shrink test, the "smaller case in the message" test, and the "listed signature is reported as known" test.

This PR changes only `shrunkPlanted`: before it plants the bug, a new helper `startAtDateAdd` rotates each draw so that it starts at its first entry whose root expression is a `DateAdd`. The entries are the same and keep their cyclic order; only the starting point moves, and the case's label says so. The rotation applies only under `severalKernels=false`; at the defaults, and for a draw with no root-level `DateAdd`, the draw is left as it was, so the default run still checks that the shrinker finds a culprit wherever the draw put it.

### Why are the changes needed?

Whether a draw can fail depends on the emit option `severalKernels`. With it on (the default), the compiler serves a projection that mixes lanes with one kernel per lane, so some kernel always holds the `date_add` entries and the bug shows. With it off, as the option matrix runs it, one kernel holds one lane: the lane of the first entry the projection meets. Every entry on the other lane is declined to the row path, out of any kernel. `date_add` works on the int lane (a `DATE` is an int), so a draw whose first entry is on the long lane puts no `AddDays` in the kernel, and the planted bug cannot fail it.

So under `severalKernels=false` the test depends on the lane of each draw's first entry, which is a matter of which rows the coverage table has and in what order the seed picks them. Adding coverage rows shifts every draw. The TIMESTAMP_NTZ rows on an open branch did exactly that: there, 16 of 20 wide draws start on the long lane, all ten that `shrunkPlanted` tries among them, and the three tests failed in the option matrix with "misdescribeAdd: no failing wide case in 10 draws".

With several kernels off, a draw rotated to start at a `date_add` has its kernel on the int lane, so the kernel holds that `date_add` and the planted bug fails whatever rows the table gains later. At the defaults no rotation is needed: a kernel per lane already holds the `date_add` entries.

**This does not fail on master today.** On master the three tests pass under `severalKernels=false` at the suite's default seed (20260925): at least one of master's ten draws still starts on the int lane. The failure needs the coverage rows that open branch adds. This PR is a robustness fix that removes the dependence before those rows land, so it can merge on its own.

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

No. Only a test helper changes.

### How was this patch tested?

`build/sbt 'catalyst/testOnly *VarkaCoverageCompositionFuzzSuite'`, with and without `set Test / javaOptions += "-Dvarka.matrix.config=severalKernels=false"`:

- master, `severalKernels=false`: 16 passed, 2 canceled (by design under a non-default configuration: a C2-code test and the wide test the skip list names), 0 failed; the planted draw shrank from 214 entries to 1.
- this PR, defaults: 18 passed, 0 failed.
- this PR, `severalKernels=false`: 16 passed, 2 canceled, 0 failed; the planted draw shrank to one `date_add`.

Scalastyle over the catalyst tests and `dev/varka_errorprone.sh` are clean.

One `/code-review high` pass found no bug and confirmed the premise: with several kernels off the kernel's lane is fixed by entry 0, and a lone `date_add` cannot be declined on size. Fixed from it: the rotation is gated on `severalKernels=false` so the default run keeps its coverage of culprits deep in the list; the label notes the rotation, since replaying `drawWide` gives the unrotated order; the rotation is a named, documented helper. Left: a draw with no root-level `DateAdd` stays unrotated (most draws hold one, and the test still fails loudly if all ten miss); matching the Catalyst `DateAdd` rather than the compiled `AddDays` (no coverage row lowers a `DateAdd` to anything else today); `collectFirst` accepting any failure signature and the three tests recomputing the same shrink (both predate this change).

### Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Opus 5.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants