Skip to content

Commit 45848cc

Browse files
committed
Be explicit about batchSize = limit + 1.
1 parent 7e567a8 commit 45848cc

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

modules/module-mongodb-storage/src/storage/implementation/MongoCompactor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ export class MongoCompactor {
184184
}
185185
}
186186
],
187-
{ batchSize: this.moveBatchQueryLimit }
187+
{
188+
// batchSize is 1 more than limit to auto-close the cursor.
189+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
190+
batchSize: this.moveBatchQueryLimit + 1
191+
}
188192
);
189193
// We don't limit to a single batch here, since that often causes MongoDB to scan through more than it returns.
190194
// Instead, we load up to the limit.

modules/module-mongodb-storage/src/storage/implementation/MongoSyncBucketStorage.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ export class MongoSyncBucketStorage
403403
limit: batchLimit,
404404
// Increase batch size above the default 101, so that we can fill an entire batch in
405405
// one go.
406-
batchSize: batchLimit,
406+
// batchSize is 1 more than limit to auto-close the cursor.
407+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
408+
batchSize: batchLimit + 1,
407409
// Raw mode is returns an array of Buffer instead of parsed documents.
408410
// We use it so that:
409411
// 1. We can calculate the document size accurately without serializing again.
@@ -905,7 +907,9 @@ export class MongoSyncBucketStorage
905907
'_id.b': 1
906908
},
907909
limit: limit + 1,
908-
batchSize: limit + 1,
910+
// batchSize is 1 more than limit to auto-close the cursor.
911+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
912+
batchSize: limit + 2,
909913
singleBatch: true
910914
}
911915
)
@@ -935,7 +939,9 @@ export class MongoSyncBucketStorage
935939
lookup: 1
936940
},
937941
limit: limit + 1,
938-
batchSize: limit + 1,
942+
// batchSize is 1 more than limit to auto-close the cursor.
943+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
944+
batchSize: limit + 2,
939945
singleBatch: true
940946
}
941947
)

modules/module-mongodb-storage/src/storage/implementation/MongoWriteCheckpointAPI.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
111111
},
112112
{
113113
limit: limit + 1,
114-
batchSize: limit + 1,
114+
// batchSize is 1 more than limit to auto-close the cursor.
115+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
116+
batchSize: limit + 2,
115117
singleBatch: true
116118
}
117119
)
@@ -140,7 +142,9 @@ export class MongoWriteCheckpointAPI implements storage.WriteCheckpointAPI {
140142
},
141143
{
142144
limit: limit + 1,
143-
batchSize: limit + 1,
145+
// batchSize is 1 more than limit to auto-close the cursor.
146+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
147+
batchSize: limit + 2,
144148
singleBatch: true
145149
}
146150
)

modules/module-mongodb-storage/src/storage/implementation/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function generateSlotName(prefix: string, sync_rules_id: number) {
4141
* However, that makes `has_more` detection very difficult, since the cursor is always closed
4242
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
4343
*
44-
* For this to be effective, set batchSize = limit in the find command.
44+
* For this to be effective, set batchSize = limit + 1 in the find command.
4545
*/
4646
export async function readSingleBatch<T>(cursor: mongo.AbstractCursor<T>): Promise<{ data: T[]; hasMore: boolean }> {
4747
try {

modules/module-mongodb/src/replication/MongoSnapshotQuery.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export class ChunkedSnapshotQuery implements AsyncDisposable {
3838
const filter: mongo.Filter<mongo.Document> =
3939
this.lastKey == null ? {} : { $expr: { $gt: ['$_id', { $literal: this.lastKey }] } };
4040
cursor = this.collection.find(filter, {
41-
batchSize: this.batchSize,
4241
readConcern: 'majority',
4342
limit: this.batchSize,
43+
// batchSize is 1 more than limit to auto-close the cursor.
44+
// See https://github.com/mongodb/node-mongodb-native/pull/4580
45+
batchSize: this.batchSize + 1,
4446
sort: { _id: 1 }
4547
});
4648
newCursor = true;

0 commit comments

Comments
 (0)