-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[exporter][batcher] Multi-batch support - Version 2 #12760
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
sfc-gh-sili
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
sfc-gh-sili:sili-metadata-key-3
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.
+285
−24
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
92 changes: 92 additions & 0 deletions
92
exporter/exporterhelper/internal/queuebatch/multi_batcher.go
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,92 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package queuebatch // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch" | ||
import ( | ||
"context" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/sender" | ||
) | ||
|
||
type multiBatcher struct { | ||
cfg BatchConfig | ||
workerPool *chan struct{} | ||
sizerType request.SizerType | ||
sizer request.Sizer[request.Request] | ||
partitioner Partitioner[request.Request] | ||
consumeFunc sender.SendFunc[request.Request] | ||
|
||
shardMapMu sync.Mutex | ||
shards map[string]*defaultBatcher | ||
} | ||
|
||
func newMultiBatcher(bCfg BatchConfig, bSet batcherSettings[request.Request]) *multiBatcher { | ||
// TODO: Determine what is the right behavior for this in combination with async queue. | ||
var workerPool chan struct{} | ||
if bSet.maxWorkers != 0 { | ||
workerPool = make(chan struct{}, bSet.maxWorkers) | ||
for i := 0; i < bSet.maxWorkers; i++ { | ||
workerPool <- struct{}{} | ||
} | ||
} | ||
return &multiBatcher{ | ||
cfg: bCfg, | ||
workerPool: &workerPool, | ||
sizerType: bSet.sizerType, | ||
sizer: bSet.sizer, | ||
partitioner: bSet.partitioner, | ||
consumeFunc: bSet.next, | ||
shardMapMu: sync.Mutex{}, | ||
shards: make(map[string]*defaultBatcher), | ||
} | ||
} | ||
|
||
func (qb *multiBatcher) getShard(ctx context.Context, req request.Request) *defaultBatcher { | ||
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 have another batcher at all ( |
||
key := qb.partitioner.GetKey(ctx, req) | ||
|
||
qb.shardMapMu.Lock() | ||
defer qb.shardMapMu.Unlock() | ||
|
||
s, ok := qb.shards[key] | ||
if !ok { | ||
s = &defaultBatcher{ | ||
cfg: qb.cfg, | ||
workerPool: qb.workerPool, | ||
sizerType: qb.sizerType, | ||
sizer: qb.sizer, | ||
consumeFunc: qb.consumeFunc, | ||
stopWG: sync.WaitGroup{}, | ||
shutdownCh: make(chan struct{}, 1), | ||
} | ||
qb.shards[key] = s | ||
_ = s.Start(ctx, nil) | ||
} | ||
return s | ||
} | ||
|
||
func (qb *multiBatcher) Start(_ context.Context, _ component.Host) error { | ||
return nil | ||
} | ||
|
||
func (qb *multiBatcher) Consume(ctx context.Context, req request.Request, done Done) { | ||
shard := qb.getShard(ctx, req) | ||
shard.Consume(ctx, req, done) | ||
} | ||
|
||
func (qb *multiBatcher) Shutdown(ctx context.Context) error { | ||
qb.shardMapMu.Lock() | ||
defer qb.shardMapMu.Unlock() | ||
stopWG := sync.WaitGroup{} | ||
for _, shard := range qb.shards { | ||
stopWG.Add(1) | ||
go func() { | ||
_ = shard.Shutdown(ctx) | ||
stopWG.Done() | ||
}() | ||
} | ||
stopWG.Wait() | ||
return nil | ||
} |
113 changes: 113 additions & 0 deletions
113
exporter/exporterhelper/internal/queuebatch/multi_batcher_test.go
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,113 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package queuebatch | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/requesttest" | ||
) | ||
|
||
func TestMultiBatcher_NoTimeout(t *testing.T) { | ||
cfg := BatchConfig{ | ||
FlushTimeout: 0, | ||
MinSize: 10, | ||
} | ||
sink := requesttest.NewSink() | ||
|
||
type partitionKey struct{} | ||
|
||
ba := newMultiBatcher(cfg, batcherSettings[request.Request]{ | ||
sizerType: request.SizerTypeItems, | ||
sizer: request.NewItemsSizer(), | ||
partitioner: NewPartitioner(func(ctx context.Context, _ request.Request) string { | ||
return ctx.Value(partitionKey{}).(string) | ||
}), | ||
next: sink.Export, | ||
maxWorkers: 1, | ||
}) | ||
|
||
require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) | ||
t.Cleanup(func() { | ||
require.NoError(t, ba.Shutdown(context.Background())) | ||
}) | ||
|
||
done := newFakeDone() | ||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) | ||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) | ||
|
||
// Neither batch should be flushed since they haven't reached min threshold. | ||
assert.Equal(t, 0, sink.RequestsCount()) | ||
assert.Equal(t, 0, sink.ItemsCount()) | ||
|
||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) | ||
|
||
assert.Eventually(t, func() bool { | ||
return sink.RequestsCount() == 1 && sink.ItemsCount() == 16 | ||
}, 500*time.Millisecond, 10*time.Millisecond) | ||
|
||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) | ||
|
||
assert.Eventually(t, func() bool { | ||
return sink.RequestsCount() == 2 && sink.ItemsCount() == 28 | ||
}, 500*time.Millisecond, 10*time.Millisecond) | ||
|
||
// Check that done callback is called for the right amount of times. | ||
assert.EqualValues(t, 0, done.errors.Load()) | ||
assert.EqualValues(t, 4, done.success.Load()) | ||
|
||
require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) | ||
} | ||
|
||
func TestMultiBatcher_Timeout(t *testing.T) { | ||
cfg := BatchConfig{ | ||
FlushTimeout: 100 * time.Millisecond, | ||
MinSize: 100, | ||
} | ||
sink := requesttest.NewSink() | ||
|
||
type partitionKey struct{} | ||
|
||
ba := newMultiBatcher(cfg, batcherSettings[request.Request]{ | ||
sizerType: request.SizerTypeItems, | ||
sizer: request.NewItemsSizer(), | ||
partitioner: NewPartitioner(func(ctx context.Context, _ request.Request) string { | ||
return ctx.Value(partitionKey{}).(string) | ||
}), | ||
next: sink.Export, | ||
maxWorkers: 1, | ||
}) | ||
|
||
require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) | ||
t.Cleanup(func() { | ||
require.NoError(t, ba.Shutdown(context.Background())) | ||
}) | ||
|
||
done := newFakeDone() | ||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) | ||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) | ||
|
||
// Neither batch should be flushed since they haven't reached min threshold. | ||
assert.Equal(t, 0, sink.RequestsCount()) | ||
assert.Equal(t, 0, sink.ItemsCount()) | ||
|
||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) | ||
ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) | ||
|
||
assert.Eventually(t, func() bool { | ||
return sink.RequestsCount() == 2 && sink.ItemsCount() == 28 | ||
}, 1*time.Second, 10*time.Millisecond) | ||
// Check that done callback is called for the right amount of times. | ||
assert.EqualValues(t, 0, done.errors.Load()) | ||
assert.EqualValues(t, 4, done.success.Load()) | ||
|
||
require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) | ||
} |
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
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.
What question(s) are there? I'm not sure I'm following.
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 comment is duplicated from
newDefaultBatcher()
. IIUC, this is related to how goroutines are allocated when async queue and the batcher are used together. Right now:AsyncQueue
owns a goroutine pool of size n that is in charge of reading from the queue and callingBatcher::consume()
.Batcher::consume()
is in charge of appending the new item to the batch and invoking the flushing goroutine if neededBatcher
can allocate up to m goroutines for dispatching the active batch.Both
n
andm
come from the same config fieldsending_queue::num_consumers
, but it does not necessarily make sense use the same number of goroutines for "reading from queue" and "dispatching the request'