Debian Codegraph @

exporter/exporterhelper/internal/queue/queue.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package queue // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queue"
5
6 import (
7 "context"
8 "errors"
9
10 "go.opentelemetry.io/collector/component"
11 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
12 "go.opentelemetry.io/collector/pipeline"
13 )
14
15 // ReferenceCounter is an optional interface that can be implemented to provide a way for the request data
16 // to manage internal locally allocated memory and re-use across multiple requests, etc.
17 //
18 // The queue will only call Ref and Unref when requests are executed asynchronously, otherwise these
19 // funcs are not called.
20 type ReferenceCounter[T any] interface {
21 Ref(T)
22 Unref(T)
23 }
24
25 type Encoding[T any] interface {
26 // Marshal is a function that can marshal a request into bytes.
27 Marshal(context.Context, T) ([]byte, error)
28
29 // Unmarshal is a function that can unmarshal bytes into a request.
30 Unmarshal([]byte) (context.Context, T, error)
31 }
32
33 // ErrQueueIsFull is the error returned when an item is offered to the Queue and the queue is full and setup to
34 // not block.
35 // Experimental: This API is at the early stage of development and may change without backward compatibility
36 // until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved.
37 var ErrQueueIsFull = errors.New("sending queue is full")
38
39 // Done represents the callback that will be called when the read request is completely processed by the
40 // downstream components.
41 // Experimental: This API is at the early stage of development and may change without backward compatibility
42 // until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved.
43 type Done interface {
44 // OnDone needs to be called when processing of the queue item is done.
45 OnDone(error)
46 }
47
48 type ConsumeFunc[T any] func(context.Context, T, Done)
49
50 // Queue defines a producer-consumer exchange which can be backed by e.g. the memory-based ring buffer queue
51 // (boundedMemoryQueue) or via a disk-based queue (persistentQueue)
52 // Experimental: This API is at the early stage of development and may change without backward compatibility
53 // until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved.
54 type Queue[T any] interface {
55 component.Component
56 // Offer inserts the specified element into this queue if it is possible to do so immediately
57 // without violating capacity restrictions. If success returns no error.
58 // It returns ErrQueueIsFull if no space is currently available.
59 Offer(ctx context.Context, item T) error
60 // Size returns the current Size of the queue
61 Size() int64
62 // Capacity returns the capacity of the queue.
63 Capacity() int64
64 }
65
66 // Settings define internal parameters for a new Queue creation.
67 type Settings[T request.Request] struct {
68 SizerType request.SizerType
69 Capacity int64
70 NumConsumers int
71 WaitForResult bool
72 BlockOnOverflow bool
73 Signal pipeline.Signal
74 StorageID *component.ID
75 ReferenceCounter ReferenceCounter[T]
76 Encoding Encoding[T]
77 ID component.ID
78 Telemetry component.TelemetrySettings
79 }
80
81 func NewQueue[T request.Request](set Settings[T], next ConsumeFunc[T]) (Queue[T], error) {
82 q := newBaseQueue(set)
83 oq, err := newObsQueue(set, newAsyncQueue(q, set.NumConsumers, next, set.ReferenceCounter))
84 if err != nil {
85 return nil, err
86 }
87
88 return oq, nil
89 }
90
91 func newBaseQueue[T request.Request](set Settings[T]) readableQueue[T] {
92 // Configure memory queue or persistent based on the config.
93 if set.StorageID == nil {
94 return newMemoryQueue[T](set)
95 }
96
97 return newPersistentQueue[T](set)
98 }
99
100 // TODO: Investigate why linter "unused" fails if add a private "read" func on the Queue.
101 type readableQueue[T any] interface {
102 Queue[T]
103 // Read pulls the next available item from the queue along with its done callback. Once processing is
104 // finished, the done callback must be called to clean up the storage.
105 // The function blocks until an item is available or if the queue is stopped.
106 // If the queue is stopped returns false, otherwise true.
107 Read(context.Context) (context.Context, T, Done, bool)
108 }