Debian Codegraph @

exporter/exporterhelper/internal/queuebatch/multi_batcher.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package queuebatch // import "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queuebatch"
5 import (
6 "context"
7 "sync"
8
9 "go.uber.org/zap"
10
11 "go.opentelemetry.io/collector/component"
12 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/queue"
13 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
14 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/sender"
15 )
16
17 type multiBatcher struct {
18 cfg BatchConfig
19 wp *workerPool
20 sizer request.Sizer
21 partitioner Partitioner[request.Request]
22 mergeCtx func(context.Context, context.Context) context.Context
23 consumeFunc sender.SendFunc[request.Request]
24 shards sync.Map
25 logger *zap.Logger
26 }
27
28 func newMultiBatcher(
29 bCfg BatchConfig,
30 sizer request.Sizer,
31 wp *workerPool,
32 partitioner Partitioner[request.Request],
33 mergeCtx func(context.Context, context.Context) context.Context,
34 next sender.SendFunc[request.Request],
35 logger *zap.Logger,
36 ) *multiBatcher {
37 return &multiBatcher{
38 cfg: bCfg,
39 wp: wp,
40 sizer: sizer,
41 partitioner: partitioner,
42 mergeCtx: mergeCtx,
43 consumeFunc: next,
44 logger: logger,
45 }
46 }
47
48 func (mb *multiBatcher) getPartition(ctx context.Context, req request.Request) *partitionBatcher {
49 key := mb.partitioner.GetKey(ctx, req)
50 s, found := mb.shards.Load(key)
51 // Fast path, shard already created.
52 if found {
53 return s.(*partitionBatcher)
54 }
55
56 newS := newPartitionBatcher(mb.cfg, mb.sizer, mb.mergeCtx, mb.wp, mb.consumeFunc, mb.logger)
57 _ = newS.Start(ctx, nil)
58 s, loaded := mb.shards.LoadOrStore(key, newS)
59 // If not loaded, there was a race condition in adding the new shard. Shutdown the newly created shard.
60 if loaded {
61 _ = newS.Shutdown(ctx)
62 }
63 return s.(*partitionBatcher)
64 }
65
66 func (mb *multiBatcher) Start(context.Context, component.Host) error {
67 return nil
68 }
69
70 func (mb *multiBatcher) Consume(ctx context.Context, req request.Request, done queue.Done) {
71 shard := mb.getPartition(ctx, req)
72 shard.Consume(ctx, req, done)
73 }
74
75 func (mb *multiBatcher) Shutdown(ctx context.Context) error {
76 var wg sync.WaitGroup
77 mb.shards.Range(func(_, shard any) bool {
78 wg.Add(1)
79 go func() {
80 defer wg.Done()
81 _ = shard.(*partitionBatcher).Shutdown(ctx)
82 }()
83 return true
84 })
85 wg.Wait()
86 return nil
87 }