Skip to content

Commit 50fc112

Browse files
committed
chore: prepare for BSON fix
1 parent 57cdffc commit 50fc112

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

src/cmap/connect.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,31 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
390390
const connectEvent = useTLS ? 'secureConnect' : 'connect';
391391
socket
392392
.once(connectEvent, () => resolve(socket))
393-
.once('error', error => reject(connectionFailureError('error', error)))
393+
.once('error', cause =>
394+
reject(new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause }))
395+
)
394396
.once('timeout', () => {
395397
reject(
396398
new MongoNetworkTimeoutError(
397399
`Socket '${connectEvent}' timed out after ${(performance.now() - start) | 0}ms (connectTimeoutMS: ${connectTimeoutMS})`
398400
)
399401
);
400402
})
401-
.once('close', () => reject(connectionFailureError('close')));
403+
.once('close', () =>
404+
reject(
405+
new MongoNetworkError(
406+
`Socket closed after ${(performance.now() - start) | 0} during connection establishment`
407+
)
408+
)
409+
);
402410

403411
if (options.cancellationToken != null) {
404-
cancellationHandler = () => reject(connectionFailureError('cancel'));
412+
cancellationHandler = () =>
413+
reject(
414+
new MongoNetworkError(
415+
`Socket connection establishment was cancelled after ${(performance.now() - start) | 0}`
416+
)
417+
);
405418
options.cancellationToken.once('cancel', cancellationHandler);
406419
}
407420
}
@@ -454,9 +467,11 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
454467

455468
socks ??= loadSocks();
456469

470+
let existingSocket: Stream;
471+
457472
try {
458473
// Then, establish the Socks5 proxy connection:
459-
const { socket } = await socks.SocksClient.createConnection({
474+
const connection = await socks.SocksClient.createConnection({
460475
existing_socket: rawSocket,
461476
timeout: options.connectTimeoutMS,
462477
command: 'connect',
@@ -473,35 +488,12 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
473488
password: options.proxyPassword || undefined
474489
}
475490
});
476-
477-
// Finally, now treat the resulting duplex stream as the
478-
// socket over which we send and receive wire protocol messages:
479-
return await makeSocket({
480-
...options,
481-
existingSocket: socket,
482-
proxyHost: undefined
483-
});
484-
} catch (error) {
485-
throw connectionFailureError('error', error);
491+
existingSocket = connection.socket;
492+
} catch (cause) {
493+
throw new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
486494
}
487-
}
488495

489-
function connectionFailureError(type: 'error', cause: Error): MongoNetworkError;
490-
function connectionFailureError(type: 'close' | 'timeout' | 'cancel'): MongoNetworkError;
491-
function connectionFailureError(
492-
type: 'error' | 'close' | 'timeout' | 'cancel',
493-
cause?: Error
494-
): MongoNetworkError {
495-
switch (type) {
496-
case 'error':
497-
return new MongoNetworkError(MongoError.buildErrorMessage(cause), { cause });
498-
case 'timeout':
499-
return new MongoNetworkTimeoutError('connection timed out');
500-
case 'close':
501-
return new MongoNetworkError('connection closed');
502-
case 'cancel':
503-
return new MongoNetworkError('connection establishment was cancelled');
504-
default:
505-
return new MongoNetworkError('unknown network error');
506-
}
496+
// Finally, now treat the resulting duplex stream as the
497+
// socket over which we send and receive wire protocol messages:
498+
return await makeSocket({ ...options, existingSocket, proxyHost: undefined });
507499
}

test/benchmarks/driverBench/common.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const { Readable } = require('stream');
66
const { pipeline } = require('stream/promises');
77
const child_process = require('child_process');
88

9+
/**
10+
* The path to the MongoDB Node.js driver.
11+
* This MUST be set to the directory the driver is installed in
12+
* NOT the file "lib/index.js" that is the driver's export.
13+
*/
914
const MONGODB_DRIVER_PATH = (() => {
1015
let driverPath = process.env.MONGODB_DRIVER_PATH;
1116
if (!driverPath?.length) {
@@ -16,8 +21,13 @@ const MONGODB_DRIVER_PATH = (() => {
1621

1722
const { MongoClient, GridFSBucket } = require(MONGODB_DRIVER_PATH);
1823

24+
/** Grab the version from the package.json */
1925
const { version: MONGODB_DRIVER_VERSION } = require(path.join(MONGODB_DRIVER_PATH, 'package.json'));
2026

27+
/**
28+
* Use git to optionally determine the git revision,
29+
* but the benchmarks could be run against an npm installed version so this should be allowed to fail
30+
*/
2131
const MONGODB_DRIVER_REVISION = (() => {
2232
try {
2333
return child_process
@@ -31,8 +41,20 @@ const MONGODB_DRIVER_REVISION = (() => {
3141
}
3242
})();
3343

44+
/**
45+
* Find the BSON dependency inside the driver PATH given and grab the version from the package.json.
46+
*/
3447
const MONGODB_BSON_PATH = path.join(MONGODB_DRIVER_PATH, 'node_modules', 'bson');
3548
const { version: MONGODB_BSON_VERSION } = require(path.join(MONGODB_BSON_PATH, 'package.json'));
49+
50+
/**
51+
* If you need to test BSON changes, you should clone, checkout and build BSON.
52+
* run: `npm link` with no arguments to register the link.
53+
* Then in the driver you are testing run `npm link bson` to use your local build.
54+
*
55+
* This will symlink the BSON into the driver's node_modules directory. So here
56+
* we can find the revision of the BSON we are testing against if .git exists.
57+
*/
3658
const MONGODB_BSON_REVISION = (() => {
3759
if (!fs.existsSync(path.join(MONGODB_BSON_PATH, '.git'))) {
3860
return 'installed from npm';

test/benchmarks/driverBench/index.js

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const MongoBench = require('../mongoBench');
44
const os = require('node:os');
5-
const util = require('node:util');
65
const process = require('node:process');
76

87
const Runner = MongoBench.Runner;
@@ -11,18 +10,8 @@ let bsonType = 'js-bson';
1110
// TODO(NODE-4606): test against different driver configurations in CI
1211

1312
const { writeFile } = require('fs/promises');
14-
const {
15-
makeParallelBenchmarks /* makeSingleBench, makeMultiBench */
16-
} = require('../mongoBench/suites');
17-
const {
18-
MONGODB_CLIENT_OPTIONS,
19-
MONGODB_DRIVER_PATH,
20-
MONGODB_DRIVER_VERSION,
21-
MONGODB_DRIVER_REVISION,
22-
MONGODB_BSON_PATH,
23-
MONGODB_BSON_VERSION,
24-
MONGODB_BSON_REVISION
25-
} = require('./common');
13+
const { makeParallelBenchmarks, makeSingleBench, makeMultiBench } = require('../mongoBench/suites');
14+
const { MONGODB_CLIENT_OPTIONS } = require('./common');
2615

2716
const hw = os.cpus();
2817
const ram = os.totalmem() / 1024 ** 3;
@@ -35,10 +24,7 @@ const systemInfo = () =>
3524
`- arch: ${os.arch()}`,
3625
`- os: ${process.platform} (${os.release()})`,
3726
`- ram: ${platform.ram}`,
38-
`- node: ${process.version}`,
39-
`- driver: ${MONGODB_DRIVER_VERSION} (${MONGODB_DRIVER_REVISION}): ${MONGODB_DRIVER_PATH}`,
40-
` - options ${util.inspect(MONGODB_CLIENT_OPTIONS)}`,
41-
`- bson: ${MONGODB_BSON_VERSION} (${MONGODB_BSON_REVISION}): (${MONGODB_BSON_PATH})\n`
27+
`- node: ${process.version}\n`
4228
].join('\n');
4329
console.log(systemInfo());
4430

@@ -47,19 +33,23 @@ function average(arr) {
4733
}
4834

4935
const benchmarkRunner = new Runner()
50-
// .suite('singleBench', suite => makeSingleBench(suite))
51-
// .suite('multiBench', suite => makeMultiBench(suite))
36+
.suite('singleBench', suite => makeSingleBench(suite))
37+
.suite('multiBench', suite => makeMultiBench(suite))
5238
.suite('parallel', suite => makeParallelBenchmarks(suite));
5339

5440
benchmarkRunner
5541
.run()
5642
.then(microBench => {
57-
// const singleBench = average([
58-
// microBench.singleBench.findOne,
59-
// microBench.singleBench.smallDocInsertOne,
60-
// microBench.singleBench.largeDocInsertOne
61-
// ]);
62-
// const multiBench = average(Object.values(microBench.multiBench));
43+
const singleBench = average([
44+
microBench.singleBench.findOne,
45+
microBench.singleBench.smallDocInsertOne,
46+
microBench.singleBench.largeDocInsertOne
47+
]);
48+
const multiBench = average(Object.values(microBench.multiBench));
49+
50+
// ldjsonMultiFileUpload and ldjsonMultiFileExport cause connection errors.
51+
// While we investigate, we will use the last known good values:
52+
// https://spruce.mongodb.com/task/mongo_node_driver_next_performance_tests_run_spec_benchmark_tests_node_server_4bc3e500b6f0e8ab01f052c4a1bfb782d6a29b4e_f168e1328f821bbda265e024cc91ae54_24_11_18_15_37_24/logs?execution=0
6353

6454
const parallelBench = average([
6555
microBench.parallel.ldjsonMultiFileUpload,
@@ -69,27 +59,27 @@ benchmarkRunner
6959
]);
7060

7161
const readBench = average([
72-
// microBench.singleBench.findOne,
73-
// microBench.multiBench.findManyAndEmptyCursor,
74-
// microBench.multiBench.gridFsDownload,
62+
microBench.singleBench.findOne,
63+
microBench.multiBench.findManyAndEmptyCursor,
64+
microBench.multiBench.gridFsDownload,
7565
microBench.parallel.gridfsMultiFileDownload,
7666
microBench.parallel.ldjsonMultiFileExport
7767
]);
7868
const writeBench = average([
79-
// microBench.singleBench.smallDocInsertOne,
80-
// microBench.singleBench.largeDocInsertOne,
81-
// microBench.multiBench.smallDocBulkInsert,
82-
// microBench.multiBench.largeDocBulkInsert,
83-
// microBench.multiBench.gridFsUpload,
69+
microBench.singleBench.smallDocInsertOne,
70+
microBench.singleBench.largeDocInsertOne,
71+
microBench.multiBench.smallDocBulkInsert,
72+
microBench.multiBench.largeDocBulkInsert,
73+
microBench.multiBench.gridFsUpload,
8474
microBench.parallel.ldjsonMultiFileUpload,
8575
microBench.parallel.gridfsMultiFileUpload
8676
]);
8777

8878
const driverBench = average([readBench, writeBench]);
8979

9080
const benchmarkResults = {
91-
// singleBench,
92-
// multiBench,
81+
singleBench,
82+
multiBench,
9383
parallelBench,
9484
readBench,
9585
writeBench,
@@ -123,6 +113,6 @@ benchmarkRunner
123113
return writeFile('results.json', results);
124114
})
125115
.catch(err => {
126-
console.error('failure: ', err.name, err.message, err.stack);
116+
console.error('failure: ', err.name, err.message);
127117
process.exit(1);
128118
});

0 commit comments

Comments
 (0)