-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3c7d95
commit 1b1ad11
Showing
8 changed files
with
173 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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,18 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: exporterhelper | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Adds "disabled" queue which is used when the user sets up batching but not queuing. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [8122, 10368] | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
This file contains 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 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 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 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 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,87 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package exporterqueue // import "go.opentelemetry.io/collector/exporter/exporterqueue" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
// boundedQueue blocks insert until the batch containing the request is sent out. | ||
type disabledQueue[T any] struct { | ||
component.StartFunc | ||
*sizedQueue[disabledMemQueueEl[T]] | ||
|
||
mu sync.Mutex | ||
nextIndex uint64 | ||
doneCh map[uint64](chan error) | ||
} | ||
|
||
type disabledMemQueueEl[T any] struct { | ||
index uint64 | ||
req T | ||
} | ||
|
||
// QueueSettings defines internal parameters for boundedQueue creation. | ||
type disabledQueueSettings[T any] struct { | ||
sizer sizer[T] | ||
capacity int64 | ||
} | ||
|
||
type disabledQueueSizer[T any] struct { | ||
sizer sizer[T] | ||
} | ||
|
||
func (s disabledQueueSizer[T]) Sizeof(item disabledMemQueueEl[T]) int64 { | ||
return s.sizer.Sizeof(item.req) | ||
} | ||
|
||
// NewBoundedQueue constructs the new queue of specified capacity, and with an optional | ||
// callback for dropped items (e.g. useful to emit metrics). | ||
func NewDisabledQueue[T any](set disabledQueueSettings[T]) Queue[T] { | ||
return &disabledQueue[T]{ | ||
sizedQueue: newSizedQueue[disabledMemQueueEl[T]]( | ||
set.capacity, | ||
disabledQueueSizer[T]{sizer: set.sizer}, | ||
true /*blocking*/), | ||
doneCh: make(map[uint64](chan error)), | ||
} | ||
} | ||
|
||
// Offer is used by the producer to submit new item to the queue. It will block until OnProcessingFinished is called | ||
// on the request. | ||
func (q *disabledQueue[T]) Offer(ctx context.Context, req T) error { | ||
q.mu.Lock() | ||
index := q.nextIndex | ||
q.nextIndex++ | ||
done := make(chan error) | ||
q.doneCh[index] = done | ||
|
||
if err := q.sizedQueue.Offer( | ||
ctx, | ||
disabledMemQueueEl[T]{req: req, index: index}); err != nil { | ||
delete(q.doneCh, index) | ||
q.mu.Unlock() | ||
return err | ||
} | ||
q.mu.Unlock() | ||
err := <-done | ||
return err | ||
} | ||
|
||
func (q *disabledQueue[T]) Read(_ context.Context) (uint64, context.Context, T, bool) { | ||
ctx, item, ok := q.sizedQueue.pop() | ||
return item.index, ctx, item.req, ok | ||
} | ||
|
||
// OnProcessingFinished unblocks unblocks offer. | ||
func (q *disabledQueue[T]) OnProcessingFinished(index uint64, err error) { | ||
q.mu.Lock() | ||
defer q.mu.Unlock() | ||
|
||
q.doneCh[index] <- err | ||
delete(q.doneCh, index) | ||
} |
This file contains 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,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package exporterqueue | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBlockingMemoryQueue(t *testing.T) { | ||
var wg sync.WaitGroup | ||
q := NewDisabledQueue[string](disabledQueueSettings[string]{sizer: &requestSizer[string]{}, capacity: 1}) | ||
|
||
err := errors.New("This is an error") | ||
wg.Add(1) | ||
go func() { | ||
assert.EqualError(t, q.Offer(context.Background(), "a"), err.Error()) // Blocks until OnProcessingFinished is called | ||
wg.Done() | ||
}() | ||
|
||
index, ctx, req, ok := q.Read(context.Background()) | ||
for !ok { | ||
index, ctx, req, ok = q.Read(context.Background()) | ||
} | ||
|
||
require.Equal(t, uint64(0), index) | ||
require.Equal(t, context.Background(), ctx) | ||
require.Equal(t, "a", req) | ||
q.OnProcessingFinished(index, err) | ||
wg.Wait() | ||
} |
This file contains 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