Skip to content

Commit a8f67d6

Browse files
sharded <4.4 support
1 parent 242c12d commit a8f67d6

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

src/cmap/connect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
MIN_SUPPORTED_SERVER_VERSION,
3232
MIN_SUPPORTED_WIRE_VERSION
3333
} from './wire_protocol/constants';
34+
import { isSharded } from './wire_protocol/shared';
3435

3536
/** @public */
3637
export type Stream = Socket | TLSSocket;
@@ -164,7 +165,7 @@ export async function performInitialHandshake(
164165
} catch (error) {
165166
if (error instanceof MongoError) {
166167
error.addErrorLabel(MongoErrorLabel.HandshakeError);
167-
if (needsRetryableWriteLabel(error, response.maxWireVersion)) {
168+
if (needsRetryableWriteLabel(error, response.maxWireVersion, isSharded(conn))) {
168169
error.addErrorLabel(MongoErrorLabel.RetryableWriteError);
169170
}
170171
}

src/error.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,11 +1215,15 @@ const RETRYABLE_READ_ERROR_CODES = new Set<number>([
12151215
// see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
12161216
const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES;
12171217

1218-
export function needsRetryableWriteLabel(error: Error, maxWireVersion: number): boolean {
1218+
export function needsRetryableWriteLabel(
1219+
error: Error,
1220+
maxWireVersion: number,
1221+
isSharded: boolean
1222+
): boolean {
12191223
// pre-4.4 server, then the driver adds an error label for every valid case
12201224
// execute operation will only inspect the label, code/message logic is handled here
12211225
if (error instanceof MongoNetworkError) {
1222-
return true;
1226+
return !isSharded;
12231227
}
12241228

12251229
if (error instanceof MongoError) {
@@ -1235,7 +1239,9 @@ export function needsRetryableWriteLabel(error: Error, maxWireVersion: number):
12351239
}
12361240

12371241
if (error instanceof MongoWriteConcernError) {
1238-
return RETRYABLE_WRITE_ERROR_CODES.has(error.result?.code ?? error.code ?? 0);
1242+
return isSharded && maxWireVersion < 9
1243+
? false
1244+
: RETRYABLE_WRITE_ERROR_CODES.has(error.result?.code ?? error.code ?? 0);
12391245
}
12401246

12411247
if (error instanceof MongoError && typeof error.code === 'number') {

src/sdam/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '../cmap/connection_pool';
99
import { PoolClearedError } from '../cmap/errors';
1010
import { type MongoDBResponseConstructor } from '../cmap/wire_protocol/responses';
11+
import { isSharded } from '../cmap/wire_protocol/shared';
1112
import {
1213
APM_EVENTS,
1314
CLOSED,
@@ -453,7 +454,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
453454
} else {
454455
if (
455456
(isRetryableWritesEnabled(this.topology) || isTransactionCommand(cmd)) &&
456-
needsRetryableWriteLabel(error, maxWireVersion(this)) &&
457+
needsRetryableWriteLabel(error, maxWireVersion(this), isSharded(this)) &&
457458
!inActiveTransaction(session, cmd)
458459
) {
459460
error.addErrorLabel(MongoErrorLabel.RetryableWriteError);

src/sessions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
391391

392392
const topologyMaxWireVersion = maxWireVersion(this.client.topology);
393393
if (
394-
isSharded(this.client.topology) &&
394+
is(this.client.topology) &&
395395
topologyMaxWireVersion != null &&
396396
topologyMaxWireVersion < minWireVersionForShardedTransactions
397397
) {

0 commit comments

Comments
 (0)