Batching correctness issues: deadlock, dropped items, and size accounting
Component(s)
exporter/exporterhelper
What happened?
While looking for performance improvements in the exporterhelper batching path, I ran into three correctness bugs. I reproduced all three by running the code, and the repros are included below.
The batching migration makes exporterhelper the default batching implementation, so more pipelines will hit these over time.
Describe the bug
1. Deadlock when num_consumers is 1 with metadata_keys configured
With a partitioner and a single worker, the worker pool can wedge permanently.
multi_batcher.go registers an LRU eviction callback that calls workerPool.execute(pb.shutdownInternal). That callback fires inside partitions.Add, which runs while mb.lock is held. workerPool.execute takes its semaphore token synchronously in the caller and only releases it in the spawned goroutine's defer. With one worker, the goroutine running shutdownInternal already holds the only token, and the nested execute inside that same call blocks waiting for a token it already owns.
An LRU eviction at the configured partition.cache_size triggers it. num_consumers: 1 with metadata_keys set produces newWorkerPool(1), and nothing in Config.Validate rejects that combination.
The failure is silent. Consume returns normally, but the pool is dead. Every later flush blocks forever, and the collector keeps accepting telemetry it can never send.
Steps to reproduce
Same setup as TestMultiBatcher_CacheSizeEviction, but with newWorkerPool(1) and CacheSize: 2. Two consumes create two partitions, and a third evicts the first. Consume returns, and any later workerPool.execute never returns.
What did you expect to see?
workerPool.execute should either have a free worker available or block until one is. The evictions and flushes should all complete.
What did you see instead?
The first eviction's goroutine blocks forever on a token it already holds, so the pool never has a free worker again.
There is a related note in the test suite today:
// Evicting a partition flushes it through the worker pool, which in turn needs
// a worker to send the pending batch, so more than one worker is required.
ba, err := newMultiBatcher(cfg, request.NewItemsSizer(), newWorkerPool(2), ...)That requirement is real, but right now it's only worked around in the test.
This one needs metadata_keys to be configured, so it only affects pipelines using a partitioner.
2. Trailing items dropped when one item exceeds the batch byte size
With the bytes sizer, one container that can't fit causes every later container that would fit to be dropped too.
In extractTraces, when a resource is larger than the remaining capacity, capacityLeft is set to 0 unconditionally and scanning stops. If nothing was extracted yet, the destination comes back empty, split reports the request as undroppable, and the rest of the request is discarded.
Steps to reproduce
Traces: one ResourceSpans with three scope groups. The first has a span carrying a 4 KiB attribute. The other two are single small spans. maxSize is 500 bytes.
What did you expect to see?
The oversized group should be refused, and the two small groups after it should still be emitted.
What did you see instead?
huge group marshals to 4132 bytes, maxSize=500
err = "one span size is greater than max size, dropping items: 3"
emitted 0 requests carrying 0 spansThe same shape reproduces for metrics, with two small metrics following an oversized one. In the metrics case the reported dropped count is also wrong: it says 0.
#15937 fixed this for logs. logs_batch.go now returns early only for the oversized container and keeps scanning:
if srcRL.ScopeLogs().Len() == 0 {
removedSize += rlSize
return true
}
removedSize += rlSize - sz.DeltaSize(rawRlSize-extRlSize)
return falsetraces_batch.go and metrics_batch.go have the same unconditional capacityLeft = 0, so they likely need the equivalent change.
This one needs the bytes sizer, and the default sizer is items, so it only affects pipelines that opt into byte-based sizing.
3. Profiles sizer mixes samples and profiles
ProfilesCountSizer reports the request-level size in samples but the per-element sizes in profiles, so max_size isn't honoured.
func (s *ProfilesCountSizer) ProfilesSize(pd pprofile.Profiles) int {
return pd.SampleCount() // samples
}
func (s *ProfilesCountSizer) ResourceProfilesSize(rp pprofile.ResourceProfiles) int {
count += rp.ScopeProfiles().At(k).Profiles().Len() // profiles
}
func (s *ProfilesCountSizer) ProfileSize(_ pprofile.Profile) int {
return 1 // one profile
}extractProfiles starts with a sample budget and subtracts profile counts. A single profile holding 1000 samples costs 1 against that budget, so it gets moved whole.
Steps to reproduce
One profile carrying 1000 samples, maxSize of 50 with the items sizer.
What did you expect to see?
Batches capped at 50 samples.
What did you see instead?
payload: 1 profile carrying 1000 samples; maxSize=50
err = "one sample size is greater than max size, dropping items: 0"
emitted 1 request carrying 1000 samplesThat is 20x over budget, and the error reports 0 dropped items while the oversized request goes out.
Existing tests don't catch this because testdata.GenerateProfiles(n) builds n profiles with exactly one sample each, so samples and profiles are always equal in every fixture. A test with more than one sample per profile is needed.
This is the items sizer, which is the default, so it affects any profiles exporter that sets max_size above zero. That makes this the most broadly exposed of the three.
Additional context
None of these are performance issues, though they came out of a performance review. I haven't attempted fixes because two of them need decisions I don't think I should make without maintainer input. The deadlock fix needs careful handling of work moved outside mb.lock, and the sizer fix changes what max_size counts, which could affect existing user tuning.
I also looked into a suspected goroutine leak in the idle-partition removal path but couldn't reproduce it, so I'm not reporting it.
Collector version
v0.162.0, 78b98e74f
Environment information
Environment
OS: (e.g., "Ubuntu 20.04") Compiler(if manually compiled): (e.g., "go 14.2")
OpenTelemetry Collector configuration
Log output
Additional context
No response
Tip
React with to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.
Source: open-telemetry/opentelemetry-collector