|
1 | 1 | import { expect } from 'chai';
|
2 | 2 |
|
3 |
| -import type { Collection, Db, MongoClient } from '../../mongodb'; |
4 |
| -import { skipBrokenAuthTestBeforeEachHook } from '../../tools/runner/hooks/configuration'; |
5 |
| - |
6 |
| -function ignoreNsNotFound(err) { |
7 |
| - if (!err.message.match(/ns not found/)) { |
8 |
| - throw err; |
9 |
| - } |
10 |
| -} |
11 |
| - |
12 |
| -function connectionCount(client) { |
13 |
| - return client |
14 |
| - .db() |
15 |
| - .admin() |
16 |
| - .serverStatus() |
17 |
| - .then(result => result.connections.totalCreated); |
18 |
| -} |
19 |
| - |
20 |
| -function expectPoolWasCleared(initialCount) { |
21 |
| - return count => expect(count).to.greaterThan(initialCount); |
22 |
| -} |
23 |
| - |
24 |
| -function expectPoolWasNotCleared(initialCount) { |
25 |
| - return count => expect(count).to.equal(initialCount); |
26 |
| -} |
27 |
| - |
28 |
| -// TODO: NODE-3819: Unskip flaky MacOS tests. |
29 |
| -// TODO: NODE-3903: check events as specified in the corresponding prose test description |
30 |
| -const maybeDescribe = process.platform === 'darwin' ? describe.skip : describe; |
31 |
| -maybeDescribe('Connections survive primary step down - prose', function () { |
| 3 | +import { |
| 4 | + type Collection, |
| 5 | + type ConnectionPoolClearedEvent, |
| 6 | + type FindCursor, |
| 7 | + type MongoClient, |
| 8 | + MONGODB_ERROR_CODES, |
| 9 | + MongoServerError, |
| 10 | + ReadPreference |
| 11 | +} from '../../mongodb'; |
| 12 | +import { type FailPoint } from '../../tools/utils'; |
| 13 | + |
| 14 | +describe('Connections Survive Primary Step Down - prose', function () { |
32 | 15 | let client: MongoClient;
|
33 |
| - let checkClient: MongoClient; |
34 |
| - let db: Db; |
35 | 16 | let collection: Collection;
|
| 17 | + let poolClearedEvents: ConnectionPoolClearedEvent[]; |
36 | 18 |
|
37 |
| - beforeEach( |
38 |
| - skipBrokenAuthTestBeforeEachHook({ |
39 |
| - skippedTests: [ |
40 |
| - 'getMore iteration', |
41 |
| - 'Not Primary - Keep Connection Pool', |
42 |
| - 'Not Primary - Reset Connection Pool', |
43 |
| - 'Shutdown in progress - Reset Connection Pool', |
44 |
| - 'Interrupted at shutdown - Reset Connection Pool' |
45 |
| - ] |
46 |
| - }) |
47 |
| - ); |
48 |
| - |
49 |
| - beforeEach(function () { |
50 |
| - const clientOptions = { |
51 |
| - maxPoolSize: 1, |
52 |
| - retryWrites: false, |
53 |
| - heartbeatFrequencyMS: 100 |
54 |
| - }; |
55 |
| - |
56 |
| - client = this.configuration.newClient(clientOptions); |
57 |
| - return client |
58 |
| - .db() |
59 |
| - .command({ ping: 1 }) |
60 |
| - .then(() => { |
61 |
| - const primary = Array.from(client.topology.description.servers.values()).filter( |
62 |
| - sd => sd.type === 'RSPrimary' |
63 |
| - )[0]; |
64 |
| - |
65 |
| - checkClient = this.configuration.newClient( |
66 |
| - `mongodb://${primary.address}/?directConnection=true`, |
67 |
| - clientOptions |
68 |
| - ); |
69 |
| - return checkClient.connect(); |
70 |
| - }) |
71 |
| - .then(() => { |
72 |
| - db = client.db('step-down'); |
73 |
| - collection = db.collection('step-down'); |
74 |
| - }) |
75 |
| - .then(() => collection.drop({ writeConcern: { w: 'majority' } })) |
76 |
| - .catch(ignoreNsNotFound) |
77 |
| - .then(() => db.createCollection('step-down', { writeConcern: { w: 'majority' } })); |
78 |
| - }); |
| 19 | + afterEach(() => client.close()); |
79 | 20 |
|
80 |
| - let deferred = []; |
81 |
| - afterEach(function () { |
82 |
| - return Promise.all(deferred.map(d => d())).then(() => { |
83 |
| - deferred = []; |
84 |
| - return Promise.all([client, checkClient].filter(x => !!x).map(client => client.close())); |
85 |
| - }); |
| 21 | + afterEach(async function () { |
| 22 | + const utilClient = this.configuration.newClient(); |
| 23 | + await utilClient.db('admin').command({ configureFailPoint: 'failCommand', mode: 'off' }); |
| 24 | + await utilClient.close(); |
| 25 | + poolClearedEvents = []; |
86 | 26 | });
|
87 | 27 |
|
88 |
| - it('getMore iteration', { |
89 |
| - metadata: { |
90 |
| - requires: { mongodb: '>=4.2.0', topology: 'replicaset' } |
91 |
| - }, |
92 |
| - |
93 |
| - test: function () { |
94 |
| - return collection |
95 |
| - .insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }], { |
96 |
| - writeConcern: { w: 'majority' } |
97 |
| - }) |
98 |
| - .then(result => expect(result.insertedCount).to.equal(5)) |
99 |
| - .then(() => { |
100 |
| - const cursor = collection.find({}, { batchSize: 2 }); |
101 |
| - deferred.push(() => cursor.close()); |
102 |
| - |
103 |
| - return cursor |
104 |
| - .next() |
105 |
| - .then(item => expect(item.a).to.equal(1)) |
106 |
| - .then(() => cursor.next()) |
107 |
| - .then(item => expect(item.a).to.equal(2)) |
108 |
| - .then(() => { |
109 |
| - return connectionCount(checkClient).then(initialConnectionCount => { |
110 |
| - return client |
111 |
| - .db('admin') |
112 |
| - .command({ replSetFreeze: 0 }, { readPreference: 'secondary' }) |
113 |
| - .then(result => expect(result).property('info').to.equal('unfreezing')) |
114 |
| - .then(() => |
115 |
| - client |
116 |
| - .db('admin') |
117 |
| - .command({ replSetStepDown: 30, force: true }, { readPreference: 'primary' }) |
118 |
| - ) |
119 |
| - .then(() => cursor.next()) |
120 |
| - .then(item => expect(item.a).to.equal(3)) |
121 |
| - .then(() => |
122 |
| - connectionCount(checkClient).then( |
123 |
| - expectPoolWasNotCleared(initialConnectionCount) |
124 |
| - ) |
125 |
| - ); |
126 |
| - }); |
127 |
| - }); |
128 |
| - }); |
129 |
| - } |
| 28 | + beforeEach(async function () { |
| 29 | + // For each test, make sure the following steps have been completed before running the actual test: |
| 30 | + |
| 31 | + // - Create a ``MongoClient`` with ``retryWrites=false`` |
| 32 | + client = this.configuration.newClient({ retryWrites: false }); |
| 33 | + // - Create a collection object from the ``MongoClient``, using ``step-down`` for the database and collection name. |
| 34 | + collection = client.db('step-down').collection('step-down'); |
| 35 | + // - Drop the test collection, using ``writeConcern`` "majority". |
| 36 | + await collection.drop({ writeConcern: { w: 'majority' } }).catch(() => null); |
| 37 | + // - Execute the "create" command to recreate the collection, using writeConcern: "majority". |
| 38 | + collection = await client |
| 39 | + .db('step-down') |
| 40 | + .createCollection('step-down', { writeConcern: { w: 'majority' } }); |
| 41 | + |
| 42 | + poolClearedEvents = []; |
| 43 | + client.on('connectionPoolCleared', poolClearEvent => poolClearedEvents.push(poolClearEvent)); |
130 | 44 | });
|
131 | 45 |
|
132 |
| - function runStepownScenario(errorCode, predicate) { |
133 |
| - return connectionCount(checkClient).then(initialConnectionCount => { |
134 |
| - return client |
| 46 | + context('getMore Iteration', { requires: { mongodb: '>4.2', topology: ['replicaset'] } }, () => { |
| 47 | + // This test requires a replica set with server version 4.2 or higher. |
| 48 | + |
| 49 | + let cursor: FindCursor; |
| 50 | + afterEach(() => cursor.close()); |
| 51 | + |
| 52 | + it('survives after primary step down', async () => { |
| 53 | + // - Insert 5 documents into a collection with a majority write concern. |
| 54 | + await collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }], { |
| 55 | + writeConcern: { w: 'majority' } |
| 56 | + }); |
| 57 | + // - Start a find operation on the collection with a batch size of 2, and retrieve the first batch of results. |
| 58 | + cursor = collection.find({}, { batchSize: 2 }); |
| 59 | + expect(await cursor.next()).to.have.property('a', 1); |
| 60 | + expect(await cursor.next()).to.have.property('a', 2); |
| 61 | + // - Send a `{replSetFreeze: 0}` command to any secondary and verify that the command succeeded. |
| 62 | + // This command will unfreeze (because it is set to zero) the secondary and ensure that it will be eligible to be elected immediately. |
| 63 | + await client |
135 | 64 | .db('admin')
|
136 |
| - .command({ |
137 |
| - configureFailPoint: 'failCommand', |
138 |
| - mode: { times: 1 }, |
139 |
| - data: { failCommands: ['insert'], errorCode } |
140 |
| - }) |
141 |
| - .then(() => { |
142 |
| - deferred.push(() => |
143 |
| - client.db('admin').command({ configureFailPoint: 'failCommand', mode: 'off' }) |
144 |
| - ); |
145 |
| - |
146 |
| - return collection.insertOne({ test: 1 }).then( |
147 |
| - () => Promise.reject(new Error('expected an error')), |
148 |
| - err => expect(err.code).to.equal(errorCode) |
149 |
| - ); |
150 |
| - }) |
151 |
| - .then(() => collection.insertOne({ test: 1 })) |
152 |
| - .then(() => connectionCount(checkClient).then(predicate(initialConnectionCount))); |
| 65 | + .command({ replSetFreeze: 0 }, { readPreference: ReadPreference.secondary }); |
| 66 | + // - Send a ``{replSetStepDown: 30, force: true}`` command to the current primary and verify that the command succeeded. |
| 67 | + await client.db('admin').command({ replSetStepDown: 5, force: true }); |
| 68 | + // - Retrieve the next batch of results from the cursor obtained in the find operation, and verify that this operation succeeded. |
| 69 | + expect(await cursor.next()).to.have.property('a', 3); |
| 70 | + // - If the driver implements the `CMAP`_ specification, verify that no new `PoolClearedEvent`_ has been |
| 71 | + // published. Otherwise verify that `connections.totalCreated`_ in `serverStatus`_ has not changed. |
| 72 | + expect(poolClearedEvents).to.be.empty; |
| 73 | + |
| 74 | + // Referenced python's implementation. Changes from spec: |
| 75 | + // replSetStepDown: 5 instead of 30 |
| 76 | + // Run these inserts to clear NotWritablePrimary issue |
| 77 | + |
| 78 | + // Attempt insertion to mark server description as stale and prevent a |
| 79 | + // NotPrimaryError on the subsequent operation. |
| 80 | + const error = await collection.insertOne({ a: 6 }).catch(error => error); |
| 81 | + expect(error) |
| 82 | + .to.be.instanceOf(MongoServerError) |
| 83 | + .to.have.property('code', MONGODB_ERROR_CODES.NotWritablePrimary); |
| 84 | + |
| 85 | + // Next insert should succeed on the new primary without clearing pool. |
| 86 | + await collection.insertOne({ a: 7 }); |
| 87 | + |
| 88 | + expect(poolClearedEvents).to.be.empty; |
153 | 89 | });
|
154 |
| - } |
155 |
| - |
156 |
| - it('Not Primary - Keep Connection Pool', { |
157 |
| - metadata: { |
158 |
| - requires: { mongodb: '>=4.2.0', topology: 'replicaset' } |
159 |
| - }, |
160 |
| - test: function () { |
161 |
| - return runStepownScenario(10107, expectPoolWasNotCleared); |
162 |
| - } |
163 | 90 | });
|
164 | 91 |
|
165 |
| - it('Not Primary - Reset Connection Pool', { |
166 |
| - metadata: { |
167 |
| - requires: { mongodb: '4.0.x', topology: 'replicaset' } |
168 |
| - }, |
169 |
| - test: function () { |
170 |
| - return runStepownScenario(10107, expectPoolWasCleared); |
| 92 | + context( |
| 93 | + 'Not Primary - Keep Connection Pool', |
| 94 | + { requires: { mongodb: '>4.2', topology: ['replicaset'] } }, |
| 95 | + () => { |
| 96 | + // This test requires a replica set with server version 4.2 or higher. |
| 97 | + |
| 98 | + // - Set the following fail point: ``{configureFailPoint: "failCommand", mode: {times: 1}, data: {failCommands: ["insert"], errorCode: 10107}}`` |
| 99 | + const failPoint: FailPoint = { |
| 100 | + configureFailPoint: 'failCommand', |
| 101 | + mode: { times: 1 }, |
| 102 | + data: { failCommands: ['insert'], errorCode: 10107 } |
| 103 | + }; |
| 104 | + |
| 105 | + it('survives after primary step down', async () => { |
| 106 | + await client.db('admin').command(failPoint); |
| 107 | + // - Execute an insert into the test collection of a ``{test: 1}`` document. |
| 108 | + const error = await collection.insertOne({ test: 1 }).catch(error => error); |
| 109 | + // - Verify that the insert failed with an operation failure with 10107 code. |
| 110 | + expect(error).to.be.instanceOf(MongoServerError).and.has.property('code', 10107); |
| 111 | + // - Execute an insert into the test collection of a ``{test: 1}`` document and verify that it succeeds. |
| 112 | + await collection.insertOne({ test: 1 }); |
| 113 | + // - If the driver implements the `CMAP`_ specification, verify that no new `PoolClearedEvent`_ has been |
| 114 | + // published. Otherwise verify that `connections.totalCreated`_ in `serverStatus`_ has not changed. |
| 115 | + expect(poolClearedEvents).to.be.empty; |
| 116 | + }); |
171 | 117 | }
|
172 |
| - }); |
| 118 | + ); |
173 | 119 |
|
174 |
| - it('Shutdown in progress - Reset Connection Pool', { |
175 |
| - metadata: { |
176 |
| - requires: { mongodb: '>=4.0.0', topology: 'replicaset' } |
177 |
| - }, |
178 |
| - test: function () { |
179 |
| - return runStepownScenario(91, expectPoolWasCleared); |
| 120 | + context( |
| 121 | + 'Not Primary - Reset Connection Pool', |
| 122 | + { requires: { mongodb: '>=4.0.0 <4.2.0', topology: ['replicaset'] } }, |
| 123 | + () => { |
| 124 | + // This test requires a replica set with server version 4.0. |
| 125 | + |
| 126 | + // - Set the following fail point: ``{configureFailPoint: "failCommand", mode: {times: 1}, data: {failCommands: ["insert"], errorCode: 10107}}`` |
| 127 | + const failPoint: FailPoint = { |
| 128 | + configureFailPoint: 'failCommand', |
| 129 | + mode: { times: 1 }, |
| 130 | + data: { failCommands: ['insert'], errorCode: 10107 } |
| 131 | + }; |
| 132 | + |
| 133 | + it('survives after primary step down', async () => { |
| 134 | + await client.db('admin').command(failPoint); |
| 135 | + // - Execute an insert into the test collection of a ``{test: 1}`` document. |
| 136 | + const error = await collection.insertOne({ test: 1 }).catch(error => error); |
| 137 | + // - Verify that the insert failed with an operation failure with 10107 code. |
| 138 | + expect(error).to.be.instanceOf(MongoServerError).and.has.property('code', 10107); |
| 139 | + // - If the driver implements the `CMAP`_ specification, verify that a `PoolClearedEvent`_ has been published |
| 140 | + expect(poolClearedEvents).to.have.lengthOf(1); |
| 141 | + // - Execute an insert into the test collection of a ``{test: 1}`` document and verify that it succeeds. |
| 142 | + await collection.insertOne({ test: 1 }); |
| 143 | + // - If the driver does NOT implement the `CMAP`_ specification, use the `serverStatus`_ command to verify `connections.totalCreated`_ has increased by 1. |
| 144 | + }); |
180 | 145 | }
|
181 |
| - }); |
| 146 | + ); |
182 | 147 |
|
183 |
| - it('Interrupted at shutdown - Reset Connection Pool', { |
184 |
| - metadata: { |
185 |
| - requires: { mongodb: '>=4.0.0', topology: 'replicaset' } |
186 |
| - }, |
187 |
| - test: function () { |
188 |
| - return runStepownScenario(11600, expectPoolWasCleared); |
| 148 | + context( |
| 149 | + 'Shutdown in progress - Reset Connection Pool', |
| 150 | + { requires: { mongodb: '>=4.0', topology: ['replicaset'] } }, |
| 151 | + () => { |
| 152 | + // This test should be run on all server versions >= 4.0. |
| 153 | + |
| 154 | + // - Set the following fail point: ``{configureFailPoint: "failCommand", mode: {times: 1}, data: {failCommands: ["insert"], errorCode: 91}}`` |
| 155 | + const failPoint: FailPoint = { |
| 156 | + configureFailPoint: 'failCommand', |
| 157 | + mode: { times: 1 }, |
| 158 | + data: { failCommands: ['insert'], errorCode: 91 } |
| 159 | + }; |
| 160 | + |
| 161 | + it('survives after primary step down', async () => { |
| 162 | + await client.db('admin').command(failPoint); |
| 163 | + // - Execute an insert into the test collection of a ``{test: 1}`` document. |
| 164 | + const error = await collection.insertOne({ test: 1 }).catch(error => error); |
| 165 | + // - Verify that the insert failed with an operation failure with 91 code. |
| 166 | + expect(error).to.be.instanceOf(MongoServerError).and.has.property('code', 91); |
| 167 | + // - If the driver implements the `CMAP`_ specification, verify that a `PoolClearedEvent`_ has been published |
| 168 | + expect(poolClearedEvents).to.have.lengthOf(1); |
| 169 | + // - Execute an insert into the test collection of a ``{test: 1}`` document and verify that it succeeds. |
| 170 | + await collection.insertOne({ test: 1 }); |
| 171 | + // - If the driver does NOT implement the `CMAP`_ specification, use the `serverStatus`_ command to verify `connections.totalCreated`_ has increased by 1. |
| 172 | + }); |
189 | 173 | }
|
190 |
| - }); |
| 174 | + ); |
| 175 | + |
| 176 | + context( |
| 177 | + 'Interrupted at shutdown - Reset Connection Pool', |
| 178 | + { requires: { mongodb: '>=4.0', topology: ['replicaset'] } }, |
| 179 | + () => { |
| 180 | + // This test should be run on all server versions >= 4.0. |
| 181 | + |
| 182 | + // - Set the following fail point: ``{configureFailPoint: "failCommand", mode: {times: 1}, data: {failCommands: ["insert"], errorCode: 11600}}`` |
| 183 | + const failPoint: FailPoint = { |
| 184 | + configureFailPoint: 'failCommand', |
| 185 | + mode: { times: 1 }, |
| 186 | + data: { failCommands: ['insert'], errorCode: 11600 } |
| 187 | + }; |
| 188 | + |
| 189 | + it('survives after primary step down', async () => { |
| 190 | + await client.db('admin').command(failPoint); |
| 191 | + // - Execute an insert into the test collection of a ``{test: 1}`` document. |
| 192 | + const error = await collection.insertOne({ test: 1 }).catch(error => error); |
| 193 | + // - Verify that the insert failed with an operation failure with 11600 code. |
| 194 | + expect(error).to.be.instanceOf(MongoServerError).and.has.property('code', 11600); |
| 195 | + // - If the driver implements the `CMAP`_ specification, verify that a `PoolClearedEvent`_ has been published |
| 196 | + expect(poolClearedEvents).to.have.lengthOf(1); |
| 197 | + // - Execute an insert into the test collection of a ``{test: 1}`` document and verify that it succeeds. |
| 198 | + await collection.insertOne({ test: 1 }); |
| 199 | + // - If the driver does NOT implement the `CMAP`_ specification, use the `serverStatus`_ command to verify `connections.totalCreated`_ has increased by 1. |
| 200 | + }); |
| 201 | + } |
| 202 | + ); |
191 | 203 | });
|
0 commit comments