|
1 | 1 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ |
2 | 2 | import { expect } from 'chai'; |
3 | 3 |
|
4 | | -import { type Collection, type MongoClient } from '../../mongodb'; |
| 4 | +import { |
| 5 | + type Collection, |
| 6 | + type CommandFailedEvent, |
| 7 | + type CommandSucceededEvent, |
| 8 | + type MongoClient |
| 9 | +} from '../../mongodb'; |
| 10 | +import { filterForCommands } from '../shared'; |
5 | 11 |
|
6 | 12 | describe('Retryable Reads Spec Prose', () => { |
7 | 13 | let client: MongoClient, failPointName; |
@@ -136,4 +142,141 @@ describe('Retryable Reads Spec Prose', () => { |
136 | 142 | } |
137 | 143 | }); |
138 | 144 | }); |
| 145 | + |
| 146 | + describe('Retrying Reads in a Replica Set', () => { |
| 147 | + // These tests verify that server deprioritization on replica sets only occurs |
| 148 | + // for SystemOverloadedError errors. |
| 149 | + |
| 150 | + const TEST_METADATA: MongoDBMetadataUI = { |
| 151 | + requires: { mongodb: '>=4.4', topology: 'replicaset' } |
| 152 | + }; |
| 153 | + |
| 154 | + describe('Retryable Reads Caused by Overload Errors Are Retried on a Different Server', () => { |
| 155 | + let client: MongoClient; |
| 156 | + const commandFailedEvents: CommandFailedEvent[] = []; |
| 157 | + const commandSucceededEvents: CommandSucceededEvent[] = []; |
| 158 | + |
| 159 | + beforeEach(async function () { |
| 160 | + // 1. Create a client `client` with `retryReads=true`, `readPreference=primaryPreferred`, and command event monitoring |
| 161 | + // enabled. |
| 162 | + client = this.configuration.newClient({ |
| 163 | + retryReads: true, |
| 164 | + readPreference: 'primaryPreferred', |
| 165 | + monitorCommands: true |
| 166 | + }); |
| 167 | + |
| 168 | + client.on('commandFailed', filterForCommands('find', commandFailedEvents)); |
| 169 | + client.on('commandSucceeded', filterForCommands('find', commandSucceededEvents)); |
| 170 | + |
| 171 | + await client.connect(); |
| 172 | + |
| 173 | + /* |
| 174 | + * 2. Configure the following fail point for `client`: |
| 175 | + { |
| 176 | + configureFailPoint: "failCommand", |
| 177 | + mode: { times: 1 }, |
| 178 | + data: { |
| 179 | + failCommands: ["find"], |
| 180 | + errorLabels: ["RetryableError", "SystemOverloadedError"] |
| 181 | + errorCode: 6 |
| 182 | + } |
| 183 | + } |
| 184 | + * */ |
| 185 | + await client.db('admin').command({ |
| 186 | + configureFailPoint: 'failCommand', |
| 187 | + mode: { times: 1 }, |
| 188 | + data: { |
| 189 | + failCommands: ['find'], |
| 190 | + errorCode: 6, |
| 191 | + errorLabels: ['RetryableError', 'SystemOverloadedError'] |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + // 3. Reset the command event monitor to clear the failpoint command from its stored events. |
| 196 | + commandFailedEvents.length = 0; |
| 197 | + commandSucceededEvents.length = 0; |
| 198 | + }); |
| 199 | + |
| 200 | + afterEach(async function () { |
| 201 | + await client?.db('admin').command({ configureFailPoint: 'failCommand', mode: 'off' }); |
| 202 | + await client?.close(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('retries on a different server when SystemOverloadedError', TEST_METADATA, async () => { |
| 206 | + // 4. Execute a `find` command with `client`. |
| 207 | + await client.db('test').collection('test').find().toArray(); |
| 208 | + |
| 209 | + // 5. Assert that one failed command event and one successful command event occurred. |
| 210 | + expect(commandFailedEvents).to.have.lengthOf(1); |
| 211 | + expect(commandSucceededEvents).to.have.lengthOf(1); |
| 212 | + |
| 213 | + // 6. Assert that both events occurred on different servers. |
| 214 | + expect(commandFailedEvents[0].address).to.not.equal(commandSucceededEvents[0].address); |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + describe('Retryable Reads Caused by Non-Overload Errors Are Retried on the Same Server', () => { |
| 219 | + let client: MongoClient; |
| 220 | + const commandFailedEvents: CommandFailedEvent[] = []; |
| 221 | + const commandSucceededEvents: CommandSucceededEvent[] = []; |
| 222 | + |
| 223 | + beforeEach(async function () { |
| 224 | + // 1. Create a client `client` with `retryReads=true`, `readPreference=primaryPreferred`, and command event monitoring |
| 225 | + // enabled. |
| 226 | + client = this.configuration.newClient({ |
| 227 | + retryReads: true, |
| 228 | + readPreference: 'primaryPreferred', |
| 229 | + monitorCommands: true |
| 230 | + }); |
| 231 | + |
| 232 | + client.on('commandFailed', filterForCommands('find', commandFailedEvents)); |
| 233 | + client.on('commandSucceeded', filterForCommands('find', commandSucceededEvents)); |
| 234 | + |
| 235 | + await client.connect(); |
| 236 | + |
| 237 | + /* |
| 238 | + * 2. Configure the following fail point for `client`: |
| 239 | + { |
| 240 | + configureFailPoint: "failCommand", |
| 241 | + mode: { times: 1 }, |
| 242 | + data: { |
| 243 | + failCommands: ["find"], |
| 244 | + errorLabels: ["RetryableError"] |
| 245 | + errorCode: 6 |
| 246 | + } |
| 247 | + } |
| 248 | + * */ |
| 249 | + await client.db('admin').command({ |
| 250 | + configureFailPoint: 'failCommand', |
| 251 | + mode: { times: 1 }, |
| 252 | + data: { |
| 253 | + failCommands: ['find'], |
| 254 | + errorCode: 6, |
| 255 | + errorLabels: ['RetryableError'] |
| 256 | + } |
| 257 | + }); |
| 258 | + |
| 259 | + // 3. Reset the command event monitor to clear the failpoint command from its stored events. |
| 260 | + commandFailedEvents.length = 0; |
| 261 | + commandSucceededEvents.length = 0; |
| 262 | + }); |
| 263 | + |
| 264 | + afterEach(async function () { |
| 265 | + await client?.db('admin').command({ configureFailPoint: 'failCommand', mode: 'off' }); |
| 266 | + await client?.close(); |
| 267 | + }); |
| 268 | + |
| 269 | + it('retries on the same server when no SystemOverloadedError', TEST_METADATA, async () => { |
| 270 | + // 4. Execute a `find` command with `client`. |
| 271 | + await client.db('test').collection('test').find().toArray(); |
| 272 | + |
| 273 | + // 5. Assert that one failed command event and one successful command event occurred. |
| 274 | + expect(commandFailedEvents).to.have.lengthOf(1); |
| 275 | + expect(commandSucceededEvents).to.have.lengthOf(1); |
| 276 | + |
| 277 | + // 6. Assert that both events occurred on the same server. |
| 278 | + expect(commandFailedEvents[0].address).to.equal(commandSucceededEvents[0].address); |
| 279 | + }); |
| 280 | + }); |
| 281 | + }); |
139 | 282 | }); |
0 commit comments