-
Notifications
You must be signed in to change notification settings - Fork 193
feat(gossipsub): Add MessageBatch #607
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
base: master
Are you sure you want to change the base?
Conversation
a15aa24
to
8e804a5
Compare
to support batch publishing messages
8e804a5
to
4059300
Compare
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.
There's quite a bit of indirection here.
- There's a top-level
NewMessageBatch
API that accepts a gossipsub router. This seems inverted. Intuitively, I'd expect to initiate a message batch from a router (similar to badger'sNewWriteBatch
). It seems more idiomatic to me. - The
MessageBatch
keeps track of pending RPCs, which are added by theGossipSubRouter
. To propagate itself through the call stack, it sets an anonymousPublishOption
wrapping itself. - There's special casing in several spots in the
GossipSubRouter
to identify if this is a batch, unwrap the object, and do special things on it like queue the RPCs instead of actually sending them. This results in the paradox that a call to the router's gossipsubPublish
doesn't actually publish anything under this mode. - The fact that a
Message
now embeds amessageBatch
is semantically counterintuitive.
All in all, I think this code is (a) hard to maintain, and (b) it exposes a confusing public API. I'm curious what alternative APIs you considered. I'd imagine this dance is a last resort to make it work under the current design/implementation constraints.
Did you consider:
- Extending the PubSub, PubSubRouter, GossipSubRouter, etc. type hierarchy with
NewBatch
andPublishBatch
methods? - Reorganizing the reusable code under
GossipSubRouter#Publish
so the relevant parts can be reused fromPublishBatch
?
I think this would simplify the whole thing.
For what it's worth, in @cskiraly's the EthResearch post, he hinted at a distinction between message batches and queuing/diffusion disciplines. I think that distinction was lost a bit later, but I'd like to recover it here.
In my head:
- A message batch is no more than an organizational unit to bundle a set of messages that the router learns about at once (without atomicity guarantees, hence not a Transaction). It does not imply a concrete queuing/diffusion/scheduling discipline.
- The user should provide a queuing/diffusion/scheduling discipline when calling
PublishBatch
, in the form of a function or an interface implementation. This discipline encapsulates the conversion to RPCs and handles their subsequent dispatch. In other words, the code that currently lives underMessageBatch#Publish
should be modular in itself.
On (a), you're the reviewer so I'll defer to you. I agree there are subtleties here that may introduce footguns in the future example. On (b), I disagree about this being a confusing public API. To be clear, this is the api users use: batch, err := NewMessageBatch(pubsub)
// Handle err
for _, msg := range msgs
err := batch.Add(ctx, topic, msg)
// Handle err
}
batch.Publish() Whether this is Whether this is
I did, but I feared it would end up as a larger refactor. It may be worth it anyways as it may generally improve the codebase and remove future footguns. I'll open a new PR along these lines.
An earlier draft of this PR allowed users to define the publish strategy. I tested via simulation the rarest message first strategy and the "shuffle" strategy from #602. The rarest first performed better (which intuitively makes sense). To that end, I chose to keep the API simpler by only using the rarest-first strategy. We can always make this configurable, but I think it would be a mistake to prematurely add this extension point now. This is not to say that the current rarest-first implementation is optimal, just that the inputs to the optimal solution may be non-obvious and defining an extension point now when we only have n=1 options is premature. |
Please hold your horses in the larger refactor. |
Consider my horses held. I'll explore a small refactor that hopes to remove some indirection here. |
@MarcoPolo I'd love to see a version of this PR with the refactor. Happy to pair on it if you'd like! Re: Re: queuing/dispatch disciplines, even if we only support the "prioritize rarest" one at the moment, it still makes sense to introduce the abstraction as long as we have some confidence that it can withstand future disciplines going forward. Deliberate API design signals intentionality and makes a difference in shaping how APIs evolve. However, if you feel strongly against this, I can live without it. I agree with @vyzo that we don't want a major refactor here, but introducing this feature cleanly (in an already complex and organic codebase) is a win. |
c0c96e6
to
7cc3ef8
Compare
I made significant changes to the design. Thanks @sukunrt and @raulk for the feedback. I think this approach is clearer. I'd recommend initially reviewing with whitespace changes hidden ( Care was taken to ensure batched messages and normal messages go through as much as the same code flows as possible. Some refactors along the way worth highlighting:
|
to support batch publishing messages
Replaces #602.
Batch publishing lets the system know there are multiple related messages to be published so it can prioritize sending different messages before sending copies of messages. For example, with the default API, when you publish two messages A and B, under the hood A gets sent to D=8 peers first, before B gets sent out. With this MessageBatch api we can now send one copy of A and then one copy of B before sending multiple copies.
When a node has bandwidth constraints relative to the messages it is publishing this improves dissemination time.
For more context see this post: https://ethresear.ch/t/improving-das-performance-with-gossipsub-batch-publishing/21713