-
Notifications
You must be signed in to change notification settings - Fork 65
feat: withdrawal batcher #392
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
Draft
vertex451
wants to merge
5
commits into
main
Choose a base branch
from
artem/batching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4d98736
feat: withdrawal batcher
vertex451 43783ca
feat: added debug logging
vertex451 f3b4fa3
chore: addressed comments
vertex451 ef79040
chore: adjusted concurrent use check
vertex451 ef69b94
Merge branch 'main' of github.com:akash-network/provider into artem/b…
vertex451 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package provider | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "slices" | ||
| "time" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| aclient "pkg.akt.dev/go/node/client/v1beta3" | ||
| mtypes "pkg.akt.dev/go/node/market/v1" | ||
| mvbeta "pkg.akt.dev/go/node/market/v1beta5" | ||
| ) | ||
|
|
||
| // withdrawBatcher coalesces MsgWithdrawLease requests into single multi-msg | ||
| // transactions using opportunistic in-flight batching: | ||
| // | ||
| // - Idle: Flush fires a 1-msg TX immediately. | ||
| // - In-flight: subsequent Enqueue calls accumulate in pending. | ||
| // - On MarkDone: callers invoke Flush which drains up to maxMsgs from pending. | ||
| // | ||
| // Not safe for concurrent use. All methods except the internal broadcast | ||
|
cloud-j-luna marked this conversation as resolved.
Outdated
|
||
| // goroutine must be called from a single goroutine. | ||
| type withdrawBatcher struct { | ||
| tx aclient.TxClient | ||
| timeout time.Duration | ||
| maxMsgs int | ||
|
|
||
| pending []mtypes.LeaseID | ||
| inFlight bool | ||
| doneCh chan error | ||
| } | ||
|
|
||
| func newWithdrawBatcher(tx aclient.TxClient, timeout time.Duration, maxMsgs int) *withdrawBatcher { | ||
| if maxMsgs < 1 { | ||
| panic(fmt.Sprintf("withdrawBatcher: maxMsgs must be >= 1, got %d", maxMsgs)) | ||
| } | ||
| return &withdrawBatcher{ | ||
| tx: tx, | ||
| timeout: timeout, | ||
| maxMsgs: maxMsgs, | ||
| doneCh: make(chan error, 1), | ||
| } | ||
| } | ||
|
|
||
| // After an in-flight broadcast fails, items coalesced during the in-flight | ||
| // window remain in pending (run-loop skips re-flush on error for natural | ||
| // backoff). If the same lease re-triggers before pending drains, Enqueue must | ||
| // dedupe so the next batch doesn't carry a duplicate MsgWithdrawLease, which | ||
| // would risk failing the entire atomic tx on the second message. | ||
| func (b *withdrawBatcher) Enqueue(lid mtypes.LeaseID) { | ||
| if slices.Contains(b.pending, lid) { | ||
| return | ||
| } | ||
| b.pending = append(b.pending, lid) | ||
| } | ||
|
|
||
| // Remove drops a lease id from the pending batch. | ||
| // Does not affect an in-flight broadcast. | ||
| func (b *withdrawBatcher) Remove(lid mtypes.LeaseID) { | ||
| b.pending = slices.DeleteFunc(b.pending, func(p mtypes.LeaseID) bool { | ||
| return p == lid | ||
| }) | ||
| } | ||
|
|
||
| // InFlight reports whether a broadcast is currently running. | ||
| func (b *withdrawBatcher) InFlight() bool { | ||
| return b.inFlight | ||
| } | ||
|
|
||
| // Pending reports the number of queued lease ids not yet broadcast. | ||
| func (b *withdrawBatcher) Pending() int { | ||
| return len(b.pending) | ||
| } | ||
|
|
||
| // Flush starts a broadcast with up to maxMsgs pending lease ids when idle. | ||
| // Returns true if a broadcast was started, false if nothing to do or already in-flight. | ||
| func (b *withdrawBatcher) Flush(ctx context.Context) bool { | ||
| if b.inFlight || len(b.pending) == 0 { | ||
| return false | ||
| } | ||
|
|
||
| n := len(b.pending) | ||
| if n > b.maxMsgs { | ||
| n = b.maxMsgs | ||
| } | ||
|
cloud-j-luna marked this conversation as resolved.
Outdated
|
||
|
|
||
| batch := make([]mtypes.LeaseID, n) | ||
| copy(batch, b.pending[:n]) | ||
| b.pending = b.pending[n:] | ||
| b.inFlight = true | ||
|
|
||
| go func() { | ||
| err := b.broadcast(ctx, batch) | ||
| select { | ||
| case <-ctx.Done(): | ||
| case b.doneCh <- err: | ||
| } | ||
| }() | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| // Done returns a channel that delivers the broadcast result of each completed batch. | ||
| // Callers must invoke MarkDone after reading to unblock the next Flush. | ||
| func (b *withdrawBatcher) Done() <-chan error { | ||
| return b.doneCh | ||
| } | ||
|
|
||
| // MarkDone clears the in-flight flag. Must be called after reading Done(). | ||
| func (b *withdrawBatcher) MarkDone() { | ||
| b.inFlight = false | ||
| } | ||
|
|
||
| func (b *withdrawBatcher) broadcast(ctx context.Context, lids []mtypes.LeaseID) error { | ||
| if len(lids) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(ctx, b.timeout) | ||
| defer cancel() | ||
|
|
||
| msgs := make([]sdk.Msg, 0, len(lids)) | ||
| for _, lid := range lids { | ||
| msgs = append(msgs, &mvbeta.MsgWithdrawLease{ID: lid}) | ||
| } | ||
|
|
||
| _, err := b.tx.BroadcastMsgs(ctx, msgs, aclient.WithResultCodeAsError()) | ||
| return err | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This was moved to the withdrawal_batcher.go