-
Notifications
You must be signed in to change notification settings - Fork 25
INFOPLAT 3099 chip ingress batching #1756
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
Open
hendoxc
wants to merge
31
commits into
main
Choose a base branch
from
INFOPLAT-3099-chip-ingress-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.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c50e461
Adds chipingress batching client
hendoxc 92e5a61
Adds unit-tests
hendoxc 703498f
Removes copying of slice
hendoxc 7ac8912
Adds zap logger
hendoxc 24a80d6
Fixes mod file
hendoxc a005109
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 3cff2cc
FIxes linting
hendoxc 21925ff
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 503b2a2
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc b432d18
Merge branch 'INFOPLAT-3099-chip-ingress-batching' of github.com:smar…
hendoxc f4fadd3
Rename timeout vars
hendoxc 4f10dfa
Adds callback mechanism
hendoxc a19bff9
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc c717835
fixes linting
hendoxc cc94df3
Move callback execution into separate goroutine
hendoxc 6980605
check for shutdown
hendoxc d476bab
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 5960cb9
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 9341854
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 45474e7
Merge branch 'INFOPLAT-3099-chip-ingress-batching' of github.com:smar…
hendoxc ad4482f
Ensures all callbacks complete after .Stop
hendoxc 2ca60d3
Removes compression type
hendoxc 5670f84
Adjust configuration
hendoxc e8073bc
Return errors from queue message
hendoxc c7404c0
Adds buffer
hendoxc 13579fd
Correct shutdown protocol
hendoxc fbd9246
Fix linting errors
hendoxc cffe580
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 5745a0d
Merge branch 'main' into INFOPLAT-3099-chip-ingress-batching
hendoxc 86fb5df
Fixes linting
hendoxc ab780a1
Merge branch 'INFOPLAT-3099-chip-ingress-batching' of github.com:smar…
hendoxc 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
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||
| // Package batch provides a thread-safe batching client for chip ingress messages. | ||||||||||
| package batch | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "sync" | ||||||||||
|
|
||||||||||
| "github.com/smartcontractkit/chainlink-common/pkg/chipingress" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // messageBatch is a thread-safe buffer for accumulating messages before sending as a batch | ||||||||||
| type messageBatch struct { | ||||||||||
| messages []*messageWithCallback | ||||||||||
| mu sync.Mutex | ||||||||||
| } | ||||||||||
|
|
||||||||||
| type messageWithCallback struct { | ||||||||||
| event *chipingress.CloudEventPb | ||||||||||
| callback func(error) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // newMessageBatch creates a new messageBatch with the given initial capacity | ||||||||||
| func newMessageBatch(capacity int) *messageBatch { | ||||||||||
| return &messageBatch{ | ||||||||||
| messages: make([]*messageWithCallback, 0, capacity), | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Add appends a message to the batch | ||||||||||
| func (b *messageBatch) Add(msg *messageWithCallback) { | ||||||||||
| b.mu.Lock() | ||||||||||
| defer b.mu.Unlock() | ||||||||||
| b.messages = append(b.messages, msg) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Len returns the current number of messages in the batch | ||||||||||
| func (b *messageBatch) Len() int { | ||||||||||
| b.mu.Lock() | ||||||||||
| defer b.mu.Unlock() | ||||||||||
| return len(b.messages) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Clear removes all messages from the batch and returns a copy of them | ||||||||||
| func (b *messageBatch) Clear() []*messageWithCallback { | ||||||||||
| b.mu.Lock() | ||||||||||
| defer b.mu.Unlock() | ||||||||||
|
|
||||||||||
| if len(b.messages) == 0 { | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Make a copy | ||||||||||
| result := make([]*messageWithCallback, len(b.messages)) | ||||||||||
| copy(result, b.messages) | ||||||||||
|
|
||||||||||
| // Reset the internal slice | ||||||||||
| b.messages = b.messages[:0] | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to consider freeing this memory in some cases so an outlier capacity doesn't leave a large buffer in the background indefinitely? |
||||||||||
|
|
||||||||||
| return result | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Values returns a copy of the current messages without clearing them | ||||||||||
| func (b *messageBatch) Values() []*messageWithCallback { | ||||||||||
| b.mu.Lock() | ||||||||||
| defer b.mu.Unlock() | ||||||||||
|
|
||||||||||
| if len(b.messages) == 0 { | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| result := make([]*messageWithCallback, len(b.messages)) | ||||||||||
| copy(result, b.messages) | ||||||||||
| return result | ||||||||||
|
Comment on lines
+70
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
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,183 @@ | ||
| package batch | ||
|
|
||
| import ( | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/pkg/chipingress" | ||
| ) | ||
|
|
||
| func TestMessageBatch(t *testing.T) { | ||
| t.Run("newMessageBatch creates empty batch", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
| require.NotNil(t, batch) | ||
| assert.Equal(t, 0, batch.Len()) | ||
| }) | ||
|
|
||
| t.Run("Add appends messages", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
|
|
||
| msg1 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-1"}, | ||
| } | ||
| msg2 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-2"}, | ||
| } | ||
|
|
||
| batch.Add(msg1) | ||
| assert.Equal(t, 1, batch.Len()) | ||
|
|
||
| batch.Add(msg2) | ||
| assert.Equal(t, 2, batch.Len()) | ||
| }) | ||
|
|
||
| t.Run("Clear returns copy and empties batch", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
|
|
||
| msg1 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-1"}, | ||
| } | ||
| msg2 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-2"}, | ||
| } | ||
|
|
||
| batch.Add(msg1) | ||
| batch.Add(msg2) | ||
|
|
||
| result := batch.Clear() | ||
| require.NotNil(t, result) | ||
| assert.Len(t, result, 2) | ||
| assert.Equal(t, "test-1", result[0].event.Id) | ||
| assert.Equal(t, "test-2", result[1].event.Id) | ||
|
|
||
| assert.Equal(t, 0, batch.Len()) | ||
| }) | ||
|
|
||
| t.Run("Clear on empty batch returns nil", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
| result := batch.Clear() | ||
| assert.Nil(t, result) | ||
| }) | ||
|
|
||
| t.Run("Values returns copy without clearing", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
|
|
||
| msg1 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-1"}, | ||
| } | ||
| msg2 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-2"}, | ||
| } | ||
|
|
||
| batch.Add(msg1) | ||
| batch.Add(msg2) | ||
|
|
||
| result := batch.Values() | ||
| require.NotNil(t, result) | ||
| assert.Len(t, result, 2) | ||
| assert.Equal(t, "test-1", result[0].event.Id) | ||
| assert.Equal(t, "test-2", result[1].event.Id) | ||
|
|
||
| // Batch should still have messages after Values | ||
| assert.Equal(t, 2, batch.Len()) | ||
| }) | ||
|
|
||
| t.Run("Values on empty batch returns nil", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
| result := batch.Values() | ||
| assert.Nil(t, result) | ||
| }) | ||
|
|
||
| t.Run("Values returns slice copy with same pointers", func(t *testing.T) { | ||
| batch := newMessageBatch(10) | ||
|
|
||
| msg1 := &messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-1"}, | ||
| } | ||
|
|
||
| batch.Add(msg1) | ||
| result := batch.Values() | ||
| require.Len(t, result, 1) | ||
|
|
||
| assert.Equal(t, msg1, result[0]) | ||
| assert.Same(t, msg1.event, result[0].event) | ||
|
|
||
| batch.Add(&messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test-2"}, | ||
| }) | ||
| assert.Len(t, result, 1) | ||
| assert.Equal(t, 2, batch.Len()) | ||
| }) | ||
|
|
||
| t.Run("concurrent Add operations are safe", func(t *testing.T) { | ||
| batch := newMessageBatch(100) | ||
| var wg sync.WaitGroup | ||
| numGoroutines := 10 | ||
| messagesPerGoroutine := 10 | ||
|
|
||
| for range numGoroutines { | ||
| wg.Go(func() { | ||
| for range messagesPerGoroutine { | ||
| batch.Add(&messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test"}, | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| assert.Equal(t, numGoroutines*messagesPerGoroutine, batch.Len()) | ||
| }) | ||
|
|
||
| t.Run("concurrent Add and Clear operations are safe", func(t *testing.T) { | ||
| batch := newMessageBatch(100) | ||
| var wg sync.WaitGroup | ||
| numAdders := 5 | ||
| numClears := 3 | ||
|
|
||
| for range numAdders { | ||
| wg.Go(func() { | ||
| for range 20 { | ||
| batch.Add(&messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test"}, | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Start clearers | ||
| for range numClears { | ||
| wg.Go(func() { | ||
| for range 10 { | ||
| batch.Clear() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| assert.GreaterOrEqual(t, batch.Len(), 0) | ||
| }) | ||
|
|
||
| t.Run("concurrent Len operations are safe", func(t *testing.T) { | ||
| batch := newMessageBatch(100) | ||
| var wg sync.WaitGroup | ||
| for range 10 { | ||
| batch.Add(&messageWithCallback{ | ||
| event: &chipingress.CloudEventPb{Id: "test"}, | ||
| }) | ||
| } | ||
|
|
||
| for range 100 { | ||
| wg.Go(func() { | ||
| length := batch.Len() | ||
| assert.GreaterOrEqual(t, length, 0) | ||
| }) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| }) | ||
| } |
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.
Could clone here: