Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit e700b79

Browse files
committed
feat(cluster-time): ensure clusterTime makes it to outgoing commands
NODE-1088
1 parent 3bfac0b commit e700b79

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/connection/pool.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ var DESTROYED = 'destroyed';
3131

3232
var _id = 0;
3333

34+
function supportsClusterTime(topology) {
35+
return topology.ismaster == null ? false : topology.ismaster.maxWireVersion >= 6;
36+
}
37+
3438
/**
3539
* Creates a new Pool instance
3640
* @class
@@ -1137,6 +1141,16 @@ Pool.prototype.write = function(commands, options, cb) {
11371141
// Get the requestId
11381142
operation.requestId = commands[commands.length - 1].requestId;
11391143

1144+
if (supportsClusterTime(this.topology) && this.topology.clusterTime) {
1145+
commands.forEach(command => {
1146+
if (command instanceof Query) {
1147+
command.query.$clusterTime = this.topology.clusterTime;
1148+
} else {
1149+
command.$clusterTime = this.topology.clusterTime;
1150+
}
1151+
});
1152+
}
1153+
11401154
// Prepare the operation buffer
11411155
serializeCommands(self, commands, [], function(err, serializedCommands) {
11421156
if (err) throw err;

test/tests/unit/single/sessions_tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,40 @@ describe('Sessions (Single)', function() {
8383
}
8484
});
8585

86+
it('should send `clusterTime` on outgoing messages', {
87+
metadata: { requires: { topology: 'single' } },
88+
test: function(done) {
89+
const clusterTime = genClusterTime(Date.now());
90+
let sentIsMaster = false;
91+
test.server.setMessageHandler(request => {
92+
if (sentIsMaster) {
93+
expect(request.document.$clusterTime).to.eql(clusterTime);
94+
request.reply({ ok: 1 });
95+
return;
96+
}
97+
98+
sentIsMaster = true;
99+
request.reply(
100+
assign({}, mock.DEFAULT_ISMASTER, {
101+
maxWireVersion: 6,
102+
$clusterTime: clusterTime
103+
})
104+
);
105+
});
106+
107+
const client = new Server(test.server.address());
108+
client.on('error', done);
109+
client.once('connect', () => {
110+
client.command('admin.$cmd', { ping: 1 }, err => {
111+
expect(err).to.not.exist;
112+
done();
113+
});
114+
});
115+
116+
client.connect();
117+
}
118+
});
119+
86120
it('should default `logicalSessionTimeoutMinutes` to `null`', {
87121
metadata: { requires: { topology: 'single' } },
88122
test: function() {

0 commit comments

Comments
 (0)