File tree Expand file tree Collapse file tree 6 files changed +353
-69
lines changed Expand file tree Collapse file tree 6 files changed +353
-69
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,7 @@ export interface CommandOptions extends BSONSerializeOptions {
97
97
session ?: ClientSession ;
98
98
documentsReturnedIn ?: string ;
99
99
noResponse ?: boolean ;
100
+ omitReadPreference ?: boolean ;
100
101
101
102
// FIXME: NODE-2802
102
103
willRetryWrite ?: boolean ;
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import type { Server } from '../sdam/server';
10
10
import type { BSONSerializeOptions , Document } from '../bson' ;
11
11
import type { ReadConcernLike } from './../read_concern' ;
12
12
import { Explain , ExplainOptions } from '../explain' ;
13
+ import { MIN_SECONDARY_WRITE_WIRE_VERSION } from '../sdam/server_selection' ;
13
14
14
15
const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5 ;
15
16
@@ -126,6 +127,10 @@ export abstract class CommandOperation<T> extends AbstractOperation<T> {
126
127
Object . assign ( cmd , { readConcern : this . readConcern } ) ;
127
128
}
128
129
130
+ if ( this . trySecondaryWrite && serverWireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION ) {
131
+ options . omitReadPreference = true ;
132
+ }
133
+
129
134
if ( options . collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION ) {
130
135
callback (
131
136
new MongoCompatibilityError (
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ export interface OperationOptions extends BSONSerializeOptions {
31
31
32
32
/** @internal Hints to `executeOperation` that this operation should not unpin on an ended transaction */
33
33
bypassPinningCheck ?: boolean ;
34
+ omitReadPreference ?: boolean ;
34
35
}
35
36
36
37
/** @internal */
@@ -49,7 +50,7 @@ export abstract class AbstractOperation<TResult = any> {
49
50
readPreference : ReadPreference ;
50
51
server ! : Server ;
51
52
bypassPinningCheck : boolean ;
52
- trySecondaryWrite = false ;
53
+ trySecondaryWrite : boolean ;
53
54
54
55
// BSON serialization options
55
56
bsonOptions ?: BSONSerializeOptions ;
@@ -73,6 +74,7 @@ export abstract class AbstractOperation<TResult = any> {
73
74
74
75
this . options = options ;
75
76
this . bypassPinningCheck = ! ! options . bypassPinningCheck ;
77
+ this . trySecondaryWrite = false ;
76
78
}
77
79
78
80
abstract execute ( server : Server , session : ClientSession , callback : Callback < TResult > ) : void ;
Original file line number Diff line number Diff line change @@ -299,6 +299,14 @@ export class Server extends TypedEventEmitter<ServerEvents> {
299
299
// Clone the options
300
300
const finalOptions = Object . assign ( { } , options , { wireProtocolCommand : false } ) ;
301
301
302
+ // There are cases where we need to flag the read preference not to get sent in
303
+ // the command, such as pre-5.0 servers attempting to perform an aggregate write
304
+ // with a non-primary read preference. In this case the effective read preference
305
+ // (primary) is not the same as the provided and must be removed completely.
306
+ if ( finalOptions . omitReadPreference ) {
307
+ delete finalOptions . readPreference ;
308
+ }
309
+
302
310
// error if collation not supported
303
311
if ( collationNotSupported ( this , cmd ) ) {
304
312
callback ( new MongoCompatibilityError ( `Server ${ this . name } does not support collation` ) ) ;
Original file line number Diff line number Diff line change @@ -43,8 +43,6 @@ export function secondaryWritableServerSelector(
43
43
// If server version >= 5.0...
44
44
// - If read preference is supplied, use that.
45
45
// - If no read preference is supplied, use primary.
46
- /* eslint no-console: 0 */
47
- console . log ( 'select' , readPreference , wireVersion ) ;
48
46
if ( ! readPreference || ( wireVersion && wireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION ) ) {
49
47
return readPreferenceServerSelector ( ReadPreference . primary ) ;
50
48
}
You can’t perform that action at this time.
0 commit comments