Skip to content

GODRIVER-3858 message size exceeds limit of 48000000#2382

Open
qingyang-hu wants to merge 3 commits intomongodb:release/2.5from
qingyang-hu:godriver3858
Open

GODRIVER-3858 message size exceeds limit of 48000000#2382
qingyang-hu wants to merge 3 commits intomongodb:release/2.5from
qingyang-hu:godriver3858

Conversation

@qingyang-hu
Copy link
Copy Markdown
Contributor

@qingyang-hu qingyang-hu commented May 7, 2026

GODRIVER-3858

Summary

Include OP_MSG head when calculating the size.

Commits a954620 and 806fe4e are from the external PR #2360 credit to @koenno.

Added a prose test in 18348e1 for better coverage.

Background & Motivation

size should not be 0 at very beginning because there are already some bytes written to dst therefore the size should be initialized with dst length.

@mongodb-drivers-pr-bot
Copy link
Copy Markdown
Contributor

mongodb-drivers-pr-bot Bot commented May 7, 2026

🧪 Performance Results

Commit SHA: 18348e1

The following benchmark tests for version 69fd6bafdab28c0007badb59 had statistically significant changes (i.e., |z-score| > 1.96):

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkBSONDeepDocumentEncoding ops_per_second_min -40.8908 2145.2967 Avg: 3629.3815
Med: 3623.0645
Stdev: 593.1155
0.7911 -2.5022
BenchmarkSingleFindOneByID total_bytes_allocated 9.6262 115774704.0000 Avg: 105608628.5079
Med: 106584392.0000
Stdev: 4871812.9438
0.7372 2.0867
BenchmarkSingleFindOneByID total_mem_allocs 8.9796 1854026.0000 Avg: 1701259.2947
Med: 1713327.0000
Stdev: 71236.4112
0.7405 2.1445
BenchmarkSmallDocInsertOne total_bytes_allocated 6.8402 39315648.0000 Avg: 36798539.7565
Med: 36919344.0000
Stdev: 1245504.2210
0.7462 2.0210
BenchmarkSmallDocInsertOne total_mem_allocs 6.4446 506275.0000 Avg: 475622.7917
Med: 475714.5000
Stdev: 13299.3694
0.7683 2.3048
BenchmarkSingleFindOneByID ops_per_second_max 6.1937 5067.1396 Avg: 4771.6018
Med: 4756.7856
Stdev: 122.5533
0.7725 2.4115
BenchmarkSmallDocInsertOne ops_per_second_max 6.1028 6728.5695 Avg: 6341.5545
Med: 6320.4538
Stdev: 147.2180
0.7933 2.6289
BenchmarkSmallDocInsertOne ops_per_second_med 5.9893 6150.5154 Avg: 5802.9597
Med: 5810.4233
Stdev: 133.3438
0.7862 2.6065
BenchmarkLargeDocInsertOne ops_per_second_max 5.9060 6701.4247 Avg: 6327.7122
Med: 6313.7691
Stdev: 151.3302
0.7902 2.4695
BenchmarkSmallDocInsertOne ns_per_op -5.7445 170102.0000 Avg: 180469.0909
Med: 180182.0000
Stdev: 3669.0504
0.8050 -2.8256
BenchmarkBSONDeepDocumentDecoding total_mem_allocs 5.6000 13896373.0000 Avg: 13159447.2373
Med: 13165173.0000
Stdev: 218949.1066
0.8374 3.3657
BenchmarkBSONDeepDocumentDecoding total_bytes_allocated 5.5886 301873536.0000 Avg: 285896027.3220
Med: 286018008.0000
Stdev: 4745558.2346
0.8374 3.3668
BenchmarkBSONFullDocumentDecoding total_bytes_allocated 5.5664 433784320.0000 Avg: 410911475.4576
Med: 412060336.0000
Stdev: 8854274.5930
0.7879 2.5833
BenchmarkBSONFullDocumentDecoding total_mem_allocs 5.5465 10481850.0000 Avg: 9931026.4831
Med: 9960095.0000
Stdev: 212799.7050
0.7886 2.5885
BenchmarkLargeDocInsertOne ops_per_second_med 5.4930 6096.5944 Avg: 5779.1485
Med: 5781.0486
Stdev: 130.9128
0.7727 2.4249
BenchmarkLargeDocInsertOne total_mem_allocs 5.3772 503886.0000 Avg: 478173.8673
Med: 477739.5000
Stdev: 13036.1387
0.7316 1.9724
BenchmarkBSONDeepDocumentDecoding ops_per_second_med 5.1844 17567.5913 Avg: 16701.7010
Med: 16659.7253
Stdev: 264.7338
0.8318 3.2708
BenchmarkBSONFlatDocumentDecoding total_mem_allocs 5.1481 11185691.0000 Avg: 10638037.9237
Med: 10651631.5000
Stdev: 223888.6342
0.7736 2.4461
BenchmarkBSONFlatDocumentDecoding total_bytes_allocated 5.1361 439857352.0000 Avg: 418369373.8983
Med: 418904028.0000
Stdev: 8788193.3396
0.7735 2.4451
BenchmarkBSONFlatDocumentDecoding ns_per_op -5.1322 49138.0000 Avg: 51796.2627
Med: 51734.5000
Stdev: 1113.1541
0.7679 -2.3880
BenchmarkBSONFlatDocumentDecoding ops_per_second_med 5.0515 21807.3971 Avg: 20758.7635
Med: 20741.5090
Stdev: 399.4480
0.7897 2.6252
BenchmarkBSONDeepDocumentDecoding ops_per_second_max 4.8329 18094.3075 Avg: 17260.1397
Med: 17196.9046
Stdev: 253.9298
0.8377 3.2850
BenchmarkLargeDocInsertOne ns_per_op -4.8256 172151.0000 Avg: 180879.4691
Med: 180918.0000
Stdev: 3841.9565
0.7556 -2.2719
BenchmarkBSONFlatDocumentDecoding ops_per_second_max 4.7094 22654.6748 Avg: 21635.7552
Med: 21623.9594
Stdev: 339.3098
0.8176 3.0029
BenchmarkBSONFullDocumentDecoding ops_per_second_med 4.6844 15265.3111 Avg: 14582.2225
Med: 14624.2674
Stdev: 279.0530
0.7824 2.4479
BenchmarkBSONDeepDocumentDecoding ns_per_op -4.6790 60373.0000 Avg: 63336.5339
Med: 63365.0000
Stdev: 1053.0432
0.8034 -2.8143
BenchmarkBSONFullDocumentEncoding total_mem_allocs 3.9980 1613660.0000 Avg: 1551625.7797
Med: 1553400.0000
Stdev: 25255.0248
0.7743 2.4563
BenchmarkBSONFullDocumentEncoding ns_per_op -3.9752 22218.0000 Avg: 23137.7797
Med: 23157.5000
Stdev: 383.3565
0.7728 -2.3993
BenchmarkBSONFullDocumentEncoding total_bytes_allocated 3.9414 279007224.0000 Avg: 268427531.7288
Med: 268711352.0000
Stdev: 4338089.0265
0.7728 2.4388
BenchmarkBSONFullDocumentDecoding ops_per_second_max 3.8202 15769.8858 Avg: 15189.6121
Med: 15173.2406
Stdev: 196.7499
0.8302 2.9493
BenchmarkBSONFullDocumentEncoding ops_per_second_med 3.6687 48225.3086 Avg: 46518.6556
Med: 46557.1024
Stdev: 688.3170
0.7829 2.4795
BenchmarkMultiFindMany ops_per_second_med 3.1706 3787878.7879 Avg: 3671469.9127
Med: 3676470.5882
Stdev: 15989.6723
0.9295 7.2803
BenchmarkBSONFullDocumentEncoding ops_per_second_max 3.0340 49270.7923 Avg: 47819.9232
Med: 47749.7913
Stdev: 624.8909
0.7781 2.3218
BenchmarkMultiFindMany ops_per_second_max 2.7505 4347826.0870 Avg: 4231439.3216
Med: 4237288.1356
Stdev: 54296.1944
0.7659 2.1436
BenchmarkSingleRunCommand allocated_bytes_per_op -0.3454 12190.0000 Avg: 12232.2500
Med: 12234.0000
Stdev: 4.9198
0.9509 -8.5877
BenchmarkLargeDocInsertOne allocated_bytes_per_op -0.1919 5683.0000 Avg: 5693.9286
Med: 5695.0000
Stdev: 4.3050
0.8459 -2.5386
BenchmarkSmallDocInsertOne allocated_bytes_per_op -0.1594 5685.0000 Avg: 5694.0769
Med: 5695.0000
Stdev: 3.8614
0.8375 -2.3507
BenchmarkBSONFlatDocumentDecoding allocated_bytes_per_op -0.0123 18050.0000 Avg: 18052.2288
Med: 18052.0000
Stdev: 0.7997
0.8150 -2.7871

For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.

@mongodb-drivers-pr-bot
Copy link
Copy Markdown
Contributor

API Change Report

No changes found!

@qingyang-hu qingyang-hu force-pushed the godriver3858 branch 3 times, most recently from 889b4ff to eac6ff1 Compare May 8, 2026 04:31
@qingyang-hu qingyang-hu changed the base branch from master to release/2.5 May 8, 2026 05:42
@qingyang-hu qingyang-hu marked this pull request as ready for review May 8, 2026 13:04
@qingyang-hu qingyang-hu requested a review from a team as a code owner May 8, 2026 13:04
@qingyang-hu qingyang-hu requested review from Copilot and matthewdale May 8, 2026 13:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes batch size accounting during OP_MSG batching so the already-written bytes in the destination buffer (e.g., OP_MSG header/body) are included when deciding how many documents can fit, preventing “message size exceeds limit” errors during large batched writes.

Changes:

  • Initialize batch size using the current dst length in AppendBatchSequence and AppendBatchArray.
  • Update unit tests to reflect the corrected size calculation behavior.
  • Add an integration prose test that exercises InsertMany near maxMessageSizeBytes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
x/mongo/driver/batches.go Fixes size initialization so batching decisions include bytes already appended to dst.
x/mongo/driver/batches_test.go Adjusts size limits in unit tests to match the corrected batching logic.
internal/integration/crud_prose_test.go Adds an integration test intended to cover near-max message size InsertMany batching.
Comments suppressed due to low confidence (1)

x/mongo/driver/batches.go:83

  • AppendBatchArray's size accounting only adds len(doc) per element. The actual bytes appended by bsoncore.AppendDocumentElement also include the element header (type byte + cstring key) and AppendArrayEnd appends a trailing 0x00. As a result, this function can construct a batch whose encoded size exceeds totalSize (notably when totalSize is cryptMaxBsonObjectSize during auto-encryption). Consider including the per-element header bytes (based on strconv.Itoa(n)) and the final terminator in the size check, or perform the size check using the prospective encoded length after append.
	aidx, dst := bsoncore.AppendArrayElementStart(dst, b.Identifier)
	size := len(dst)
	var n int
	for i := b.offset; i < len(b.Documents); i++ {
		if n == maxCount {
			break
		}
		doc := b.Documents[i]
		size += len(doc)
		if size > totalSize {
			break
		}
		dst = bsoncore.AppendDocumentElement(dst, strconv.Itoa(n), doc)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1032 to +1035
var docs []any
limit := hello.MaxBsonObjectSize - 30
for need := hello.MaxMessageSizeBytes - 350; need > 0; need -= limit {
if need >= limit {
})
}

func TestBatchSize(t *testing.T) {
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.

3 participants