Skip to content

perf/refactor: throughput improvements, memory optimizations, and code quality enhancements - #6

Merged
yuzone merged 12 commits into
mainfrom
fix/nested-timestamp-field-conversion
Mar 27, 2026
Merged

perf/refactor: throughput improvements, memory optimizations, and code quality enhancements#6
yuzone merged 12 commits into
mainfrom
fix/nested-timestamp-field-conversion

Conversation

@yuzone

@yuzone yuzone commented Mar 27, 2026

Copy link
Copy Markdown
Owner

Summary

This PR bundles a series of performance improvements, bug fixes, and code quality
refactors identified through production throughput analysis of the Fluent Bit
BigQuery WriteAPI sink plugin.

yuzone and others added 12 commits March 27, 2026 17:55
…tion variable

gomonkey's ApplyFunc for output.NewDecoder was unreliable on ARM64 macOS
when tests ran sequentially in the same process. The patch occasionally
failed to intercept calls, causing checks.createDecoder to stay at 0 and
TestFLBPluginFlushCtxErrorHandling to fail non-deterministically.

Fix: introduce a var newDecoder function variable in out_writeapi.go
(matching the pattern already used for getClient, getWriter,
getFLBPluginContext, pluginGetResult, isReady). Replace all four
gomonkey.ApplyFunc(output.NewDecoder, ...) calls in tests with direct
assignment to newDecoder, with deferred restore. This eliminates the
ARM64 code-patching dependency entirely.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…location

binaryData ([][]byte) was re-allocated with make([][]byte, 0, 256) on
every FLBPluginFlushCtx call. Move it to outputConfig so the underlying
array is reused across flushes. Nil out retained elements before each
reset so the GC can reclaim the previous batch's byte slices.
FLBPluginExitCtx called checkAllStreamResponses with waitForResponse=false,
which skips any in-flight response that has not yet arrived. This could
cause data loss if AppendRows results were still pending when the plugin
exited.

Fix: pass waitForResponse=true so pluginGetResult blocks on each queued
result, fully draining the queue before finalizeCloseAllStreams runs.

Also switch FLBPluginExitCtx to use the injectable getFLBPluginContext
variable (consistent with FLBPluginFlushCtx) to enable unit testing
without gomonkey.

Add TestFLBPluginExitCtxDrainsPendingResponses to verify that responses
marked as not-yet-ready are still drained during exit. Fix
MockManagedStream.Close and MockManagedWriterClient.Close to use their
CloseFunc fields when set, avoiding nil-pointer panics in tests that
call ExitCtx.
- Annotate const block (out_writeapi.go) with explanations for
  chunkSizeLimit, queueRequestDefault, queueByteDefault,
  queueRequestScalingPercent, maxNumStreamsPerInstance,
  minQueueRequests, maxUnixSeconds, and flushTimeoutSecDefault.
- Fix "Unsuccesful" → "Unsuccessful" in sendRequestRetries comment.
…age conversion

Add toSubMessage(val, md, cache) as the single authoritative path for
converting either map[string]interface{} or map[interface{}]interface{}
values into a populated *dynamicpb.Message.

Replace the duplicated type-switch/assertion blocks in populateMessage,
rawPopulateMessage, setRepeatedField, and rawSetRepeatedField with calls
to the new helper. rawSetRepeatedField becomes identical to
setRepeatedField after this change and is therefore deleted; its one
call site in rawPopulateMessage now calls setRepeatedField directly.
…nFlushCtx

Change all internal functions (buildStream, sendRequest*, createNew-
StreamDynamicScaling, finalizeCloseAllStreams) from **outputConfig to
*outputConfig, eliminating triple-dereference patterns throughout.
Remove the configPointer intermediate variable in FLBPluginInit.
Convert buildStream to an injectable var (consistent with newDecoder,
getClient, etc.) and migrate TestFLBPluginFlushCtxDynamicScaling off
gomonkey.ApplyFunc to fix ARM64 reliability.

Extract flushChunk (stream selection + send + offset update) and
decodeAndSerializeRecords (record loop + mid-chunk flush) from
FLBPluginFlushCtx, reducing it from 95 to 34 lines.
@yuzone
yuzone merged commit bdf33fb into main Mar 27, 2026
1 check passed
@yuzone
yuzone deleted the fix/nested-timestamp-field-conversion branch March 27, 2026 12:50
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