Skip to content

perf: minimize streamMu lock scope in sendRequestDefault - #10

Merged
yuzone merged 1 commit into
mainfrom
perf/minimize-stream-mu-lock-scope
Apr 7, 2026
Merged

perf: minimize streamMu lock scope in sendRequestDefault#10
yuzone merged 1 commit into
mainfrom
perf/minimize-stream-mu-lock-scope

Conversation

@yuzone

@yuzone yuzone commented Apr 3, 2026

Copy link
Copy Markdown
Owner

Summary

Previously, sendRequestDefault held streamMu for the entire duration
of AppendRows, blocking all concurrent flushes whenever the inflight
queue (Max_Queue_Requests / Max_Queue_Bytes) was saturated.

This PR narrows the critical section so that the lock is acquired only
for the slice access and result append, allowing AppendRows to execute
concurrently without holding the lock.

Changes

  • Introduced three helper methods on outputConfig:
    • getStream(index) — acquires streamMu only to read the stream pointer
    • appendResult(stream, result) — acquires streamMu only to append the result
    • leastLoadedStreamIndex() — acquires streamMu only to find the least-loaded stream
  • Replaced the broad lock in sendRequestDefault and flushChunk with these helpers

AppendRows on a managed writer's DefaultStream can block when the
inflight queue (Max_Queue_Requests / Max_Queue_Bytes) is saturated.
Holding streamMu for the entire duration serialized all concurrent
flush calls on the same output instance, directly contributing to
the 30s latency spikes observed under peak input load.

Changes:
- Add getStream(), appendResult(), leastLoadedStreamIndex() methods
  on outputConfig to encapsulate the lock/unlock boilerplate and
  narrow each critical section to slice access only.
- Refactor sendRequestDefault to call AppendRows outside streamMu,
  using getStream() to fetch the stream pointer and appendResult()
  to enqueue the result after the call returns.
- Refactor flushChunk to use leastLoadedStreamIndex() (no behavior
  change, removes the explicit Lock/Unlock pair).

AppendRows on managedwriter.ManagedStream is goroutine-safe; the
lock is no longer needed to protect the call itself. For the
at-least-once (DefaultStream) path there is no stream rebuilding,
so the *streamConfig pointer obtained under the lock remains valid
after the lock is released.
@yuzone yuzone changed the title perf/fix: minimize streamMu lock scope in sendRequestDefault and return FLB_RETRY on failure perf: minimize streamMu lock scope in sendRequestDefault Apr 3, 2026
@yuzone
yuzone force-pushed the perf/minimize-stream-mu-lock-scope branch from 1503a7f to 8dc75fb Compare April 3, 2026 09:45
@yuzone
yuzone merged commit b8f4b68 into main Apr 7, 2026
1 check passed
@yuzone
yuzone deleted the perf/minimize-stream-mu-lock-scope branch April 7, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant