Skip to content

Commit a5c8fb3

Browse files
committed
fix(NODE-3521): remove session support checks
1 parent 446da95 commit a5c8fb3

File tree

7 files changed

+12
-165
lines changed

7 files changed

+12
-165
lines changed

src/cmap/connection.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
402402
if (deprecationErrors != null) finalCmd.apiDeprecationErrors = deprecationErrors;
403403
}
404404

405-
if (hasSessionSupport(this) && session) {
405+
if (session) {
406406
if (
407407
session.clusterTime &&
408408
clusterTime &&
@@ -683,12 +683,6 @@ export class CryptoConnection extends Connection {
683683
}
684684
}
685685

686-
/** @internal */
687-
export function hasSessionSupport(conn: Connection): boolean {
688-
const description = conn.description;
689-
return description.logicalSessionTimeoutMinutes != null || !!description.loadBalanced;
690-
}
691-
692686
function supportsOpMsg(conn: Connection) {
693687
const description = conn.description;
694688
if (description == null) {

src/cursor/abstract_cursor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ function next<T>(cursor: AbstractCursor, blocking: boolean, callback: Callback<T
653653

654654
if (cursorId == null) {
655655
// All cursors must operate within a session, one must be made implicitly if not explicitly provided
656-
if (cursor[kSession] == null && cursor[kTopology].hasSessionSupport()) {
656+
if (cursor[kSession] == null) {
657657
cursor[kSession] = cursor[kTopology].startSession({ owner: cursor, explicit: false });
658658
}
659659

src/operations/execute_operation.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,13 @@ export function executeOperation<
8989
// that are not explicitly provided with a session.
9090
let session: ClientSession | undefined = operation.session;
9191
let owner: symbol | undefined;
92-
if (topology.hasSessionSupport()) {
93-
if (session == null) {
94-
owner = Symbol();
95-
session = topology.startSession({ owner, explicit: false });
96-
} else if (session.hasEnded) {
97-
return cb(new MongoExpiredSessionError('Use of expired sessions is not permitted'));
98-
} else if (session.snapshotEnabled && !topology.capabilities.supportsSnapshotReads) {
99-
return cb(new MongoCompatibilityError('Snapshot reads require MongoDB 5.0 or later'));
100-
}
101-
} else if (session) {
102-
// If the user passed an explicit session and we are still, after server selection,
103-
// trying to run against a topology that doesn't support sessions we error out.
104-
return cb(new MongoCompatibilityError('Current topology does not support sessions'));
92+
if (session == null) {
93+
owner = Symbol();
94+
session = topology.startSession({ owner, explicit: false });
95+
} else if (session.hasEnded) {
96+
return cb(new MongoExpiredSessionError('Use of expired sessions is not permitted'));
97+
} else if (session.snapshotEnabled && !topology.capabilities.supportsSnapshotReads) {
98+
return cb(new MongoCompatibilityError('Snapshot reads require MongoDB 5.0 or later'));
10599
}
106100

107101
try {

src/sdam/topology.ts

-7
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,6 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
641641
return !this.description.hasDataBearingServers;
642642
}
643643

644-
/**
645-
* @returns Whether sessions are supported on the current topology
646-
*/
647-
hasSessionSupport(): boolean {
648-
return this.loadBalanced || this.description.logicalSessionTimeoutMinutes != null;
649-
}
650-
651644
/** Start a logical session */
652645
startSession(options: ClientSessionOptions, clientOptions?: MongoOptions): ClientSession {
653646
const session = new ClientSession(this, this.s.sessionPool, options, clientOptions);

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export function executeLegacyOperation(
229229
let session: ClientSession;
230230
let opOptions: any;
231231
let owner: any;
232-
if (!options.skipSessions && topology.hasSessionSupport()) {
232+
if (!options.skipSessions) {
233233
opOptions = args[args.length - 2];
234234
if (opOptions == null || opOptions.session == null) {
235235
owner = Symbol();

test/unit/assorted/sessions_client.test.js

+1-89
Original file line numberDiff line numberDiff line change
@@ -16,95 +16,7 @@ describe('Sessions - client/unit', function () {
1616
});
1717
});
1818

19-
it('should not throw a synchronous exception if sessions are not supported', function () {
20-
test.server.setMessageHandler(request => {
21-
var doc = request.document;
22-
if (isHello(doc)) {
23-
request.reply(Object.assign({}, mock.HELLO));
24-
} else if (doc.endSessions) {
25-
request.reply({ ok: 1 });
26-
}
27-
});
28-
29-
const client = new MongoClient(`mongodb://${test.server.uri()}/test`);
30-
return client.connect().then(() => {
31-
expect(() => client.startSession()).to.not.throw(
32-
'Current topology does not support sessions'
33-
);
34-
return client.close();
35-
});
36-
});
37-
38-
it('should throw an exception if sessions are not supported on some servers', function () {
39-
const replicaSetMock = new ReplSetFixture();
40-
let testClient;
41-
return replicaSetMock
42-
.setup({ doNotInitHandlers: true })
43-
.then(() => {
44-
replicaSetMock.firstSecondaryServer.setMessageHandler(request => {
45-
var doc = request.document;
46-
if (isHello(doc)) {
47-
const hello = replicaSetMock.firstSecondaryStates[0];
48-
hello.logicalSessionTimeoutMinutes = 20;
49-
request.reply(hello);
50-
} else if (doc.endSessions) {
51-
request.reply({ ok: 1 });
52-
}
53-
});
54-
55-
replicaSetMock.secondSecondaryServer.setMessageHandler(request => {
56-
var doc = request.document;
57-
if (isHello(doc)) {
58-
const hello = replicaSetMock.secondSecondaryStates[0];
59-
hello.logicalSessionTimeoutMinutes = 10;
60-
request.reply(hello);
61-
} else if (doc.endSessions) {
62-
request.reply({ ok: 1 });
63-
}
64-
});
65-
66-
replicaSetMock.arbiterServer.setMessageHandler(request => {
67-
var doc = request.document;
68-
if (isHello(doc)) {
69-
const hello = replicaSetMock.arbiterStates[0];
70-
hello.logicalSessionTimeoutMinutes = 30;
71-
request.reply(hello);
72-
} else if (doc.endSessions) {
73-
request.reply({ ok: 1 });
74-
}
75-
});
76-
77-
replicaSetMock.primaryServer.setMessageHandler(request => {
78-
var doc = request.document;
79-
if (isHello(doc)) {
80-
const hello = replicaSetMock.primaryStates[0];
81-
hello.logicalSessionTimeoutMinutes = null;
82-
request.reply(hello);
83-
} else if (doc.endSessions) {
84-
request.reply({ ok: 1 });
85-
}
86-
});
87-
88-
return replicaSetMock.uri();
89-
})
90-
.then(uri => {
91-
testClient = new MongoClient(uri);
92-
return testClient.connect();
93-
})
94-
.then(client => {
95-
const session = client.startSession();
96-
return client.db().collection('t').insertOne({ a: 1 }, { session });
97-
})
98-
.then(() => {
99-
expect.fail('Expected an error to be thrown about not supporting sessions');
100-
})
101-
.catch(error => {
102-
expect(error.message).to.equal('Current topology does not support sessions');
103-
})
104-
.finally(() => (testClient ? testClient.close() : null));
105-
});
106-
107-
it('should return a client session when requested if the topology supports it', function (done) {
19+
it('should return a client session when requested', function (done) {
10820
test.server.setMessageHandler(request => {
10921
var doc = request.document;
11022
if (isHello(doc)) {

test/unit/cmap/connection.test.js

+1-47
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
const mock = require('../../tools/mongodb-mock/index');
44
const { connect } = require('../../../src/cmap/connect');
5-
const { Connection, hasSessionSupport } = require('../../../src/cmap/connection');
5+
const { Connection } = require('../../../src/cmap/connection');
66
const { expect } = require('chai');
7-
const { Socket } = require('net');
87
const { ns, isHello } = require('../../../src/utils');
98
const { getSymbolFrom } = require('../../tools/utils');
109

@@ -107,49 +106,4 @@ describe('Connection - unit/cmap', function () {
107106
done();
108107
});
109108
});
110-
111-
describe('.hasSessionSupport', function () {
112-
let connection;
113-
const stream = new Socket();
114-
115-
context('when logicalSessionTimeoutMinutes is present', function () {
116-
beforeEach(function () {
117-
connection = new Connection(stream, {
118-
hostAddress: server.hostAddress(),
119-
logicalSessionTimeoutMinutes: 5
120-
});
121-
});
122-
123-
it('returns true', function () {
124-
expect(hasSessionSupport(connection)).to.be.true;
125-
});
126-
});
127-
128-
context('when logicalSessionTimeoutMinutes is not present', function () {
129-
context('when in load balancing mode', function () {
130-
beforeEach(function () {
131-
connection = new Connection(stream, {
132-
hostAddress: server.hostAddress(),
133-
loadBalanced: true
134-
});
135-
});
136-
137-
it('returns true', function () {
138-
expect(hasSessionSupport(connection)).to.be.true;
139-
});
140-
});
141-
142-
context('when not in load balancing mode', function () {
143-
beforeEach(function () {
144-
connection = new Connection(stream, {
145-
hostAddress: server.hostAddress()
146-
});
147-
});
148-
149-
it('returns false', function () {
150-
expect(hasSessionSupport(connection)).to.be.false;
151-
});
152-
});
153-
});
154-
});
155109
});

0 commit comments

Comments
 (0)