Skip to content

Commit 57cdd29

Browse files
WIP
1 parent ab16489 commit 57cdd29

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

src/operations/execute_operation.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from '../utils';
3939
import { AggregateOperation } from './aggregate';
4040
import { AbstractOperation, Aspect } from './operation';
41+
import { RunCommandOperation } from './run_command';
4142

4243
const MMAPv1_RETRY_WRITES_ERROR_CODE = MONGODB_ERROR_CODES.IllegalOperation;
4344
const MMAPv1_RETRY_WRITES_ERROR_MESSAGE =
@@ -256,15 +257,15 @@ async function executeOperationWithRetries<
256257

257258
let maxAttempts =
258259
(operation.maxAttempts ?? willRetry) ? (timeoutContext.csotEnabled() ? Infinity : 2) : 1;
260+
const shouldRetry = operation.hasAspect(Aspect.READ_OPERATION) && topology.s.options.retryReads || (operation.hasAspect(Aspect.WRITE_OPERATION) || operation instanceof RunCommandOperation) && topology.s.options.retryWrites;
259261

260262
for (
261263
let attempt = 0;
262264
attempt < maxAttempts;
263265
attempt++,
264-
maxAttempts =
265-
willRetry && previousOperationError?.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)
266-
? 6
267-
: maxAttempts
266+
maxAttempts = shouldRetry && previousOperationError?.hasErrorLabel(MongoErrorLabel.SystemOverloadedError)
267+
? 6
268+
: maxAttempts
268269
) {
269270
if (previousOperationError) {
270271
if (hasWriteAspect && previousOperationError.code === MMAPv1_RETRY_WRITES_ERROR_CODE) {
@@ -349,9 +350,9 @@ async function executeOperationWithRetries<
349350
topology.tokenBucket.deposit(
350351
isRetry
351352
? // on successful retry, deposit the retry cost + the refresh rate.
352-
TOKEN_REFRESH_RATE + RETRY_COST
353+
TOKEN_REFRESH_RATE + RETRY_COST
353354
: // otherwise, just deposit the refresh rate.
354-
TOKEN_REFRESH_RATE
355+
TOKEN_REFRESH_RATE
355356
);
356357
return operation.handleOk(result);
357358
} catch (error) {

src/sessions.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
import type { MongoClient, MongoOptions } from './mongo_client';
2626
import { TypedEventEmitter } from './mongo_types';
2727
import { executeOperation } from './operations/execute_operation';
28-
import { RetryAttemptContext } from './operations/operation';
2928
import { RunCommandOperation } from './operations/run_command';
3029
import { ReadConcernLevel } from './read_concern';
3130
import { ReadPreference } from './read_preference';
@@ -105,8 +104,7 @@ export interface EndSessionOptions {
105104
*/
106105
export class ClientSession
107106
extends TypedEventEmitter<ClientSessionEvents>
108-
implements AsyncDisposable
109-
{
107+
implements AsyncDisposable {
110108
/** @internal */
111109
client: MongoClient;
112110
/** @internal */
@@ -494,23 +492,22 @@ export class ClientSession
494492
command.recoveryToken = this.transaction.recoveryToken;
495493
}
496494

497-
const retryContext = new RetryAttemptContext(5);
498495

499496
const operation = new RunCommandOperation(new MongoDBNamespace('admin'), command, {
500497
session: this,
501498
readPreference: ReadPreference.primary,
502499
bypassPinningCheck: true
503500
});
504-
operation.attempts = retryContext;
501+
operation.maxAttempts = 5;
505502

506503
const timeoutContext =
507504
this.timeoutContext ??
508505
(typeof timeoutMS === 'number'
509506
? TimeoutContext.create({
510-
serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS,
511-
socketTimeoutMS: this.clientOptions.socketTimeoutMS,
512-
timeoutMS
513-
})
507+
serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS,
508+
socketTimeoutMS: this.clientOptions.socketTimeoutMS,
509+
timeoutMS
510+
})
514511
: null);
515512

516513
try {
@@ -531,7 +528,7 @@ export class ClientSession
531528
readPreference: ReadPreference.primary,
532529
bypassPinningCheck: true
533530
});
534-
op.attempts = retryContext;
531+
op.maxAttempts = operation.maxAttempts;
535532
await executeOperation(this.client, op, timeoutContext);
536533
return;
537534
} catch (retryCommitError) {
@@ -612,10 +609,10 @@ export class ClientSession
612609
const timeoutContext =
613610
timeoutMS != null
614611
? TimeoutContext.create({
615-
timeoutMS,
616-
serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS,
617-
socketTimeoutMS: this.clientOptions.socketTimeoutMS
618-
})
612+
timeoutMS,
613+
serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS,
614+
socketTimeoutMS: this.clientOptions.socketTimeoutMS
615+
})
619616
: null;
620617

621618
const wc = this.transaction.options.writeConcern ?? this.clientOptions?.writeConcern;
@@ -728,10 +725,10 @@ export class ClientSession
728725
this.timeoutContext =
729726
timeoutMS != null
730727
? TimeoutContext.create({
731-
timeoutMS,
732-
serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS,
733-
socketTimeoutMS: this.clientOptions.socketTimeoutMS
734-
})
728+
timeoutMS,
729+
serverSelectionTimeoutMS: this.clientOptions.serverSelectionTimeoutMS,
730+
socketTimeoutMS: this.clientOptions.socketTimeoutMS
731+
})
735732
: null;
736733

737734
// 1. Record the current monotonic time, which will be used to enforce the 120-second timeout before later retry attempts.

0 commit comments

Comments
 (0)