Skip to content

Commit b5ddb65

Browse files
committed
Reject too large elements to the queue
Without this change, the requests without deadline will block forever until the space is available which will never be. Signed-off-by: Bogdan Drutu <[email protected]>
1 parent cf18559 commit b5ddb65

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

.chloggen/fix-too-large-size.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: exporterhelper
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Reject elements larger than the queue capacity
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12847]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: []

exporter/exporterhelper/internal/queuebatch/memory_queue.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ var blockingDonePool = sync.Pool{
2020
},
2121
}
2222

23-
var errInvalidSize = errors.New("invalid element size")
23+
var (
24+
errInvalidSize = errors.New("invalid element size")
25+
errSizeTooLarge = errors.New("element size too large")
26+
)
2427

2528
// memoryQueueSettings defines internal parameters for boundedMemoryQueue creation.
2629
type memoryQueueSettings[T any] struct {
@@ -74,6 +77,11 @@ func (mq *memoryQueue[T]) Offer(ctx context.Context, el T) error {
7477
return errInvalidSize
7578
}
7679

80+
// If element larger than the capacity, will never been able to add it.
81+
if elSize > mq.cap {
82+
return errSizeTooLarge
83+
}
84+
7785
done, err := mq.add(ctx, el, elSize)
7886
if err != nil {
7987
return err

exporter/exporterhelper/internal/queuebatch/memory_queue_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func TestMemoryQueueOfferInvalidSize(t *testing.T) {
9797
require.ErrorIs(t, q.Offer(context.Background(), -1), errInvalidSize)
9898
}
9999

100+
func TestMemoryQueueRejectOverCapacityElements(t *testing.T) {
101+
q := newMemoryQueue[int64](memoryQueueSettings[int64]{sizer: sizerInt64{}, capacity: 7, blockOnOverflow: true})
102+
require.ErrorIs(t, q.Offer(context.Background(), 8), errSizeTooLarge)
103+
}
104+
100105
func TestMemoryQueueOfferZeroSize(t *testing.T) {
101106
q := newMemoryQueue[int64](memoryQueueSettings[int64]{sizer: sizerInt64{}, capacity: 1})
102107
require.NoError(t, q.Offer(context.Background(), 0))
@@ -106,7 +111,8 @@ func TestMemoryQueueOfferZeroSize(t *testing.T) {
106111
}
107112

108113
func TestMemoryQueueZeroCapacity(t *testing.T) {
109-
q := newMemoryQueue[int64](memoryQueueSettings[int64]{sizer: sizerInt64{}, capacity: 0})
114+
q := newMemoryQueue[int64](memoryQueueSettings[int64]{sizer: sizerInt64{}, capacity: 1})
115+
require.NoError(t, q.Offer(context.Background(), 1))
110116
require.ErrorIs(t, q.Offer(context.Background(), 1), ErrQueueIsFull)
111117
require.NoError(t, q.Shutdown(context.Background()))
112118
}

0 commit comments

Comments
 (0)