Repository navigation
Conversation
…Queue and batch-drain in ConnectionImpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary & Bottleneck
java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java(L547-L570,L601-L619,L1030)java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryResultImpl.java(L195-L225)ConnectionImplusedLinkedBlockingDequeforbufferFvl,bufferRow,pageCache, andrpcResponseQueue. Everybuffer.put(row)allocated a 24-byteLinkedBlockingDeque$Nodeobject on the heap and acquired a single sharedReentrantLockcontended by both the producer and consumer threads.BigQueryResultImpl.BigQueryResultSet.next()calledbuffer.take()one row at a time, acquiring and releasing the queue lock on every single row.Changes
ArrayBlockingQueuering buffers inConnectionImpl:LinkedBlockingDequewithArrayBlockingQueueforbufferFvl,bufferRow,pageCache, andrpcResponseQueueintableDataList,processQueryResponseResults, andhighThroughPutRead.BigQueryResultSet.next():ArrayDeque<T> localBatch(capacity 256) inBigQueryResultSet.localBatchis empty,next()takes the first available element viabuffer.take()and immediately callsbuffer.drainTo(localBatch, 256)under a single lock acquisition. Subsequentnext()calls serve rows fromlocalBatchwithout lock contention.Expected Impact
LinkedBlockingDeque$Nodeallocations (24 MB per 1M rows) by pre-allocating a contiguous ring buffer array.ReentrantLockacquisitions inResultSet.next()by up to 256x when buffered rows are available.Verification
mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=BigQueryResultImplTest,ConnectionImplTest(26 tests run, 0 failures).BigQueryResultImplTest.testResultSetBatchDrainingPreservesFifoOrderAndEofto verify FIFO ordering and EOF handling across 600 rows (spanning multiple 256-row drain batches).