|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package exporterhelper // import "go.opentelemetry.io/collector/exporter/exporterhelper" |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "sync" |
| 9 | + "sync/atomic" |
| 10 | + "time" |
| 11 | + |
| 12 | + "go.uber.org/zap" |
| 13 | + |
| 14 | + "go.opentelemetry.io/collector/component" |
| 15 | + "go.opentelemetry.io/collector/exporter" |
| 16 | + "go.opentelemetry.io/collector/exporter/exporterbatcher" |
| 17 | +) |
| 18 | + |
| 19 | +// batchSender is a component that places requests into batches before passing them to the downstream senders. |
| 20 | +// Batches are sent out with any of the following conditions: |
| 21 | +// - batch size reaches cfg.SendBatchSize |
| 22 | +// - cfg.Timeout is elapsed since the timestamp when the previous batch was sent out. |
| 23 | +// - concurrencyLimit is reached. |
| 24 | +type batchSender struct { |
| 25 | + baseRequestSender |
| 26 | + cfg exporterbatcher.Config |
| 27 | + mergeFunc exporterbatcher.BatchMergeFunc[Request] |
| 28 | + mergeSplitFunc exporterbatcher.BatchMergeSplitFunc[Request] |
| 29 | + |
| 30 | + // concurrencyLimit is the maximum number of goroutines that can be created by the batcher. |
| 31 | + // If this number is reached and all the goroutines are busy, the batch will be sent right away. |
| 32 | + // Populated from the number of queue consumers if queue is enabled. |
| 33 | + concurrencyLimit uint64 |
| 34 | + activeRequests atomic.Uint64 |
| 35 | + |
| 36 | + resetTimerCh chan struct{} |
| 37 | + |
| 38 | + mu sync.Mutex |
| 39 | + activeBatch *batch |
| 40 | + |
| 41 | + logger *zap.Logger |
| 42 | + |
| 43 | + shutdownCh chan struct{} |
| 44 | + stopped *atomic.Bool |
| 45 | +} |
| 46 | + |
| 47 | +// newBatchSender returns a new batch consumer component. |
| 48 | +func newBatchSender(cfg exporterbatcher.Config, set exporter.CreateSettings) *batchSender { |
| 49 | + bs := &batchSender{ |
| 50 | + activeBatch: newEmptyBatch(), |
| 51 | + cfg: cfg, |
| 52 | + logger: set.Logger, |
| 53 | + shutdownCh: make(chan struct{}), |
| 54 | + stopped: &atomic.Bool{}, |
| 55 | + resetTimerCh: make(chan struct{}), |
| 56 | + } |
| 57 | + return bs |
| 58 | +} |
| 59 | + |
| 60 | +func (bs *batchSender) Start(_ context.Context, _ component.Host) error { |
| 61 | + timer := time.NewTimer(bs.cfg.FlushTimeout) |
| 62 | + go func() { |
| 63 | + for { |
| 64 | + select { |
| 65 | + case <-bs.shutdownCh: |
| 66 | + bs.mu.Lock() |
| 67 | + if bs.activeBatch.request != nil { |
| 68 | + bs.exportActiveBatch() |
| 69 | + } |
| 70 | + bs.mu.Unlock() |
| 71 | + if !timer.Stop() { |
| 72 | + <-timer.C |
| 73 | + } |
| 74 | + return |
| 75 | + case <-timer.C: |
| 76 | + bs.mu.Lock() |
| 77 | + if bs.activeBatch.request != nil { |
| 78 | + bs.exportActiveBatch() |
| 79 | + } |
| 80 | + bs.mu.Unlock() |
| 81 | + timer.Reset(bs.cfg.FlushTimeout) |
| 82 | + case <-bs.resetTimerCh: |
| 83 | + if !timer.Stop() { |
| 84 | + <-timer.C |
| 85 | + } |
| 86 | + timer.Reset(bs.cfg.FlushTimeout) |
| 87 | + } |
| 88 | + } |
| 89 | + }() |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +type batch struct { |
| 95 | + ctx context.Context |
| 96 | + request Request |
| 97 | + done chan struct{} |
| 98 | + err error |
| 99 | +} |
| 100 | + |
| 101 | +func newEmptyBatch() *batch { |
| 102 | + return &batch{ |
| 103 | + ctx: context.Background(), |
| 104 | + done: make(chan struct{}), |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// exportActiveBatch exports the active batch asynchronously and replaces it with a new one. |
| 109 | +// Caller must hold the lock. |
| 110 | +func (bs *batchSender) exportActiveBatch() { |
| 111 | + go func(b *batch) { |
| 112 | + b.err = b.request.Export(b.ctx) |
| 113 | + close(b.done) |
| 114 | + }(bs.activeBatch) |
| 115 | + bs.activeBatch = newEmptyBatch() |
| 116 | +} |
| 117 | + |
| 118 | +// isActiveBatchReady returns true if the active batch is ready to be exported. |
| 119 | +// The batch is ready if it has reached the minimum size or the concurrency limit is reached. |
| 120 | +// Caller must hold the lock. |
| 121 | +func (bs *batchSender) isActiveBatchReady() bool { |
| 122 | + return bs.activeBatch.request.ItemsCount() >= bs.cfg.MinSizeItems || |
| 123 | + (bs.concurrencyLimit > 0 && bs.activeRequests.Load() >= bs.concurrencyLimit) |
| 124 | +} |
| 125 | + |
| 126 | +func (bs *batchSender) send(ctx context.Context, req Request) error { |
| 127 | + // Stopped batch sender should act as pass-through to allow the queue to be drained. |
| 128 | + if bs.stopped.Load() { |
| 129 | + return bs.nextSender.send(ctx, req) |
| 130 | + } |
| 131 | + |
| 132 | + bs.activeRequests.Add(1) |
| 133 | + defer bs.activeRequests.Add(^uint64(0)) |
| 134 | + |
| 135 | + if bs.cfg.MaxSizeItems > 0 { |
| 136 | + return bs.sendMergeSplitBatch(ctx, req) |
| 137 | + } |
| 138 | + return bs.sendMergeBatch(ctx, req) |
| 139 | +} |
| 140 | + |
| 141 | +// sendMergeSplitBatch sends the request to the batch which may be split into multiple requests. |
| 142 | +func (bs *batchSender) sendMergeSplitBatch(ctx context.Context, req Request) error { |
| 143 | + bs.mu.Lock() |
| 144 | + |
| 145 | + reqs, err := bs.mergeSplitFunc(ctx, bs.cfg.MaxSizeConfig, bs.activeBatch.request, req) |
| 146 | + if err != nil || len(reqs) == 0 { |
| 147 | + bs.mu.Unlock() |
| 148 | + return err |
| 149 | + } |
| 150 | + if len(reqs) == 1 || bs.activeBatch.request != nil { |
| 151 | + bs.updateActiveBatch(ctx, reqs[0]) |
| 152 | + batch := bs.activeBatch |
| 153 | + if bs.isActiveBatchReady() || len(reqs) > 1 { |
| 154 | + bs.exportActiveBatch() |
| 155 | + bs.resetTimerCh <- struct{}{} |
| 156 | + } |
| 157 | + bs.mu.Unlock() |
| 158 | + <-batch.done |
| 159 | + if batch.err != nil { |
| 160 | + return batch.err |
| 161 | + } |
| 162 | + reqs = reqs[1:] |
| 163 | + } else { |
| 164 | + bs.mu.Unlock() |
| 165 | + } |
| 166 | + |
| 167 | + // Intentionally do not put the last request in the active batch to not block it. |
| 168 | + // TODO: Consider including the partial request in the error to avoid double publishing. |
| 169 | + for _, r := range reqs { |
| 170 | + if err := r.Export(ctx); err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +// sendMergeBatch sends the request to the batch and waits for the batch to be exported. |
| 178 | +func (bs *batchSender) sendMergeBatch(ctx context.Context, req Request) error { |
| 179 | + bs.mu.Lock() |
| 180 | + if bs.activeBatch.request != nil { |
| 181 | + var err error |
| 182 | + req, err = bs.mergeFunc(ctx, bs.activeBatch.request, req) |
| 183 | + if err != nil { |
| 184 | + bs.mu.Unlock() |
| 185 | + return err |
| 186 | + } |
| 187 | + } |
| 188 | + bs.updateActiveBatch(ctx, req) |
| 189 | + batch := bs.activeBatch |
| 190 | + if bs.isActiveBatchReady() { |
| 191 | + bs.exportActiveBatch() |
| 192 | + bs.resetTimerCh <- struct{}{} |
| 193 | + } |
| 194 | + bs.mu.Unlock() |
| 195 | + <-batch.done |
| 196 | + return batch.err |
| 197 | +} |
| 198 | + |
| 199 | +// updateActiveBatch update the active batch to the new merged request and context. |
| 200 | +// The context is only set once and is not updated after the first call. |
| 201 | +// Merging the context would be complex and require an additional goroutine to handle the context cancellation. |
| 202 | +// We take the approach of using the context from the first request since it's likely to have the shortest timeout. |
| 203 | +func (bs *batchSender) updateActiveBatch(ctx context.Context, req Request) { |
| 204 | + if bs.activeBatch.request == nil { |
| 205 | + bs.activeBatch.ctx = ctx |
| 206 | + } |
| 207 | + bs.activeBatch.request = req |
| 208 | +} |
| 209 | + |
| 210 | +func (bs *batchSender) Shutdown(context.Context) error { |
| 211 | + bs.stopped.Store(true) |
| 212 | + close(bs.shutdownCh) |
| 213 | + // Wait for the active requests to finish. |
| 214 | + for bs.activeRequests.Load() > 0 { |
| 215 | + time.Sleep(10 * time.Millisecond) |
| 216 | + } |
| 217 | + return nil |
| 218 | +} |
0 commit comments