Skip to content

[SPARK-55476][PYTHON] Refactor broadcast variable protocol - #54258

Closed
gaogaotiantian wants to merge 1 commit into
apache:masterfrom
gaogaotiantian:refactor-broadcast
Closed

[SPARK-55476][PYTHON] Refactor broadcast variable protocol#54258
gaogaotiantian wants to merge 1 commit into
apache:masterfrom
gaogaotiantian:refactor-broadcast

Conversation

@gaogaotiantian

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Refactored the broadcast variable protocol from random value being sent to a json-based structured message.

Why are the changes needed?

It's part of the effort to make protocol between JVM and the workers more stable and easier to understand.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Tried it on some relevant tests locally and they passed. Need CI to validate the rest.

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

No.

@HyukjinKwon HyukjinKwon changed the title [SPARK-55476] Refactor broadcast variable protocol [SPARK-55476][PYTHON] Refactor broadcast variable protocol Feb 12, 2026
needs_broadcast_decryption_server = read_bool(infile)
num_broadcast_variables = read_int(infile)
if needs_broadcast_decryption_server:
data = json.loads(utf8_deserializer.loads(infile))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How would we use this info for debugging? I feel like you can just log it here instead of JSON ser/de

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.

I did not quite get the question. We are not using it for debugging. This is part of the protocol. We need this from JVM.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you mind describing how we're going to use this in the PR description?

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.

Sorry use what? This is part of the JVM <-> Python worker protocol. We are not adding any new features. JVM used to send broadcast variable information integer by integer (with some strings in the middle) to Python. Now instead of that raw fragile protocol, we send all of the broadcast variable information in a JSON string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am trying to understand why we need this. Is this to purely make the protocol more stable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If that's the case, I am not super supportive of this change. This could impact jobs like Structured Streaming (with micro batches) or ML jobs that disable spark.python.worker.reuse (which happen often in practice to work around any problem by having long living daemon worker). Considering the overhead vs benefit, I would prefer to just leave it.

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.

Performance impact is a big red herring. This change introduced two kinds of "overhead":

  • CPU time to encode/decode json
  • Extra bytes through the JVM/worker network (it's on the same machine)

Decoding a small json string takes about 1us. It's probably on the same range on scala side. Local network runs at least 10Gbps, an extra 100 bytes takes about 0.1us.

That's the overhead we introduce for every UDF run.

Currently, without reuse-worker, each worker takes about a few hundred ms to spawn. I made an optimization a few weeks ago that eliminated 100-200ms per spawn for reused worker and no one even notice it.

1us is 0.01% of 100ms. That's literally nothing. If we care about 1us, we have serious issues with our current UDF. I can get a lot of 1us from our current code if that's what we need to make our protocol more stable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My point is that the benefit is not super compelling. If we plan to refactor the whole protocol or sth, yeah probably we should go ahead. But doing this alone doesn't look worthwhile to me.

If we do want to refactor the whole protocol, we should better have a bigger picture.

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.

This is part of the effort to refactor the whole protocol. I'm doing it piece by piece so eventually we can have a structured message from JVM. Ideally a multi-phased message. All the initialization (probably the UDF definition) should be sent in a single message. The message should be relatively resistant to new changes. For example it won't stuck if we decide to add something new. Or it should report a clear error when the message is not following the protocol.

json is good in a sense that, if we decide to add something else to broadcast variable protocol, it's easy. If we did it wrong, we can find it quickly too. Otherwise we had to be really careful about where to insert the read_long and if we did not do it correctly, the worker could stuck at an arbitrary point.

This refactor actually eliminated some unnecessary fields. Our old protocol is too fragile that no one is willing to touch it. I want to gradually convert it to a more structured way. A single switch is a bit too dangerous.

@github-actions

Copy link
Copy Markdown

We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants