1
1
import type { ObjectId } from '../bson' ;
2
2
import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants' ;
3
- import { MongoError , MongoRuntimeError } from '../error' ;
3
+ import { MongoRuntimeError , MongoServerError } from '../error' ;
4
4
import { compareObjectId , shuffle } from '../utils' ;
5
5
import { ServerType , TopologyType } from './common' ;
6
6
import { ServerDescription } from './server_description' ;
@@ -32,29 +32,29 @@ export interface TopologyDescriptionOptions {
32
32
*/
33
33
export class TopologyDescription {
34
34
type : TopologyType ;
35
- setName ? : string ;
36
- maxSetVersion ? : number ;
37
- maxElectionId ? : ObjectId ;
35
+ setName : string | null ;
36
+ maxSetVersion : number | null ;
37
+ maxElectionId : ObjectId | null ;
38
38
servers : Map < string , ServerDescription > ;
39
39
stale : boolean ;
40
40
compatible : boolean ;
41
41
compatibilityError ?: string ;
42
- logicalSessionTimeoutMinutes ? : number ;
42
+ logicalSessionTimeoutMinutes : number | null ;
43
43
heartbeatFrequencyMS : number ;
44
44
localThresholdMS : number ;
45
- commonWireVersion ? : number ;
45
+ commonWireVersion : number ;
46
46
47
47
/**
48
48
* Create a TopologyDescription
49
49
*/
50
50
constructor (
51
51
topologyType : TopologyType ,
52
- serverDescriptions ? : Map < string , ServerDescription > ,
53
- setName ? : string ,
54
- maxSetVersion ? : number ,
55
- maxElectionId ? : ObjectId ,
56
- commonWireVersion ? : number ,
57
- options ? : TopologyDescriptionOptions
52
+ serverDescriptions : Map < string , ServerDescription > | null = null ,
53
+ setName : string | null = null ,
54
+ maxSetVersion : number | null = null ,
55
+ maxElectionId : ObjectId | null = null ,
56
+ commonWireVersion : number | null = null ,
57
+ options : TopologyDescriptionOptions | null = null
58
58
) {
59
59
options = options ?? { } ;
60
60
@@ -64,22 +64,10 @@ export class TopologyDescription {
64
64
this . compatible = true ;
65
65
this . heartbeatFrequencyMS = options . heartbeatFrequencyMS ?? 0 ;
66
66
this . localThresholdMS = options . localThresholdMS ?? 15 ;
67
-
68
- if ( setName ) {
69
- this . setName = setName ;
70
- }
71
-
72
- if ( maxSetVersion ) {
73
- this . maxSetVersion = maxSetVersion ;
74
- }
75
-
76
- if ( maxElectionId ) {
77
- this . maxElectionId = maxElectionId ;
78
- }
79
-
80
- if ( commonWireVersion ) {
81
- this . commonWireVersion = commonWireVersion ;
82
- }
67
+ this . setName = setName ?? null ;
68
+ this . maxElectionId = maxElectionId ?? null ;
69
+ this . maxSetVersion = maxSetVersion ?? null ;
70
+ this . commonWireVersion = commonWireVersion ?? 0 ;
83
71
84
72
// determine server compatibility
85
73
for ( const serverDescription of this . servers . values ( ) ) {
@@ -108,12 +96,12 @@ export class TopologyDescription {
108
96
// value among ServerDescriptions of all data-bearing server types. If any have a null
109
97
// logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be
110
98
// set to null.
111
- this . logicalSessionTimeoutMinutes = undefined ;
99
+ this . logicalSessionTimeoutMinutes = null ;
112
100
for ( const [ , server ] of this . servers ) {
113
101
if ( server . isReadable ) {
114
102
if ( server . logicalSessionTimeoutMinutes == null ) {
115
103
// If any of the servers have a null logicalSessionsTimeout, then the whole topology does
116
- this . logicalSessionTimeoutMinutes = undefined ;
104
+ this . logicalSessionTimeoutMinutes = null ;
117
105
break ;
118
106
}
119
107
@@ -200,11 +188,6 @@ export class TopologyDescription {
200
188
// potentially mutated values
201
189
let { type : topologyType , setName, maxSetVersion, maxElectionId, commonWireVersion } = this ;
202
190
203
- if ( serverDescription . setName && setName && serverDescription . setName !== setName ) {
204
- // TODO(NODE-4159): servers with an incorrect setName should be removed not marked Unknown
205
- serverDescription = new ServerDescription ( address , undefined ) ;
206
- }
207
-
208
191
const serverType = serverDescription . type ;
209
192
const serverDescriptions = new Map ( this . servers ) ;
210
193
@@ -217,6 +200,19 @@ export class TopologyDescription {
217
200
}
218
201
}
219
202
203
+ if (
204
+ typeof serverDescription . setName === 'string' &&
205
+ typeof setName === 'string' &&
206
+ serverDescription . setName !== setName
207
+ ) {
208
+ if ( topologyType === TopologyType . Single ) {
209
+ // "Single" Topology with setName mismatch is direct connection usage, mark unknown do not remove
210
+ serverDescription = new ServerDescription ( address ) ;
211
+ } else {
212
+ serverDescriptions . delete ( address ) ;
213
+ }
214
+ }
215
+
220
216
// update the actual server description
221
217
serverDescriptions . set ( address , serverDescription ) ;
222
218
@@ -311,15 +307,16 @@ export class TopologyDescription {
311
307
) ;
312
308
}
313
309
314
- get error ( ) : MongoError | undefined {
310
+ get error ( ) : MongoServerError | null {
315
311
const descriptionsWithError = Array . from ( this . servers . values ( ) ) . filter (
316
312
( sd : ServerDescription ) => sd . error
317
313
) ;
318
314
319
315
if ( descriptionsWithError . length > 0 ) {
320
316
return descriptionsWithError [ 0 ] . error ;
321
317
}
322
- return ;
318
+
319
+ return null ;
323
320
}
324
321
325
322
/**
@@ -366,10 +363,10 @@ function topologyTypeForServerType(serverType: ServerType): TopologyType {
366
363
function updateRsFromPrimary (
367
364
serverDescriptions : Map < string , ServerDescription > ,
368
365
serverDescription : ServerDescription ,
369
- setName ? : string ,
370
- maxSetVersion ? : number ,
371
- maxElectionId ? : ObjectId
372
- ) : [ TopologyType , string ? , number ? , ObjectId ? ] {
366
+ setName : string | null = null ,
367
+ maxSetVersion : number | null = null ,
368
+ maxElectionId : ObjectId | null = null
369
+ ) : [ TopologyType , string | null , number | null , ObjectId | null ] {
373
370
setName = setName || serverDescription . setName ;
374
371
if ( setName !== serverDescription . setName ) {
375
372
serverDescriptions . delete ( serverDescription . address ) ;
@@ -436,7 +433,7 @@ function updateRsFromPrimary(
436
433
function updateRsWithPrimaryFromMember (
437
434
serverDescriptions : Map < string , ServerDescription > ,
438
435
serverDescription : ServerDescription ,
439
- setName ? : string
436
+ setName : string | null = null
440
437
) : TopologyType {
441
438
if ( setName == null ) {
442
439
// TODO(NODE-3483): should be an appropriate runtime error
@@ -456,10 +453,10 @@ function updateRsWithPrimaryFromMember(
456
453
function updateRsNoPrimaryFromMember (
457
454
serverDescriptions : Map < string , ServerDescription > ,
458
455
serverDescription : ServerDescription ,
459
- setName ? : string
460
- ) : [ TopologyType , string ? ] {
456
+ setName : string | null = null
457
+ ) : [ TopologyType , string | null ] {
461
458
const topologyType = TopologyType . ReplicaSetNoPrimary ;
462
- setName = setName || serverDescription . setName ;
459
+ setName = setName ?? serverDescription . setName ;
463
460
if ( setName !== serverDescription . setName ) {
464
461
serverDescriptions . delete ( serverDescription . address ) ;
465
462
return [ topologyType , setName ] ;
0 commit comments