11import type { ObjectId } from '../bson' ;
22import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants' ;
3- import { MongoError , MongoRuntimeError } from '../error' ;
3+ import { MongoRuntimeError , MongoServerError } from '../error' ;
44import { compareObjectId , shuffle } from '../utils' ;
55import { ServerType , TopologyType } from './common' ;
66import { ServerDescription } from './server_description' ;
@@ -32,29 +32,29 @@ export interface TopologyDescriptionOptions {
3232 */
3333export class TopologyDescription {
3434 type : TopologyType ;
35- setName ? : string ;
36- maxSetVersion ? : number ;
37- maxElectionId ? : ObjectId ;
35+ setName : string | null ;
36+ maxSetVersion : number | null ;
37+ maxElectionId : ObjectId | null ;
3838 servers : Map < string , ServerDescription > ;
3939 stale : boolean ;
4040 compatible : boolean ;
4141 compatibilityError ?: string ;
42- logicalSessionTimeoutMinutes ? : number ;
42+ logicalSessionTimeoutMinutes : number | null ;
4343 heartbeatFrequencyMS : number ;
4444 localThresholdMS : number ;
45- commonWireVersion ? : number ;
45+ commonWireVersion : number ;
4646
4747 /**
4848 * Create a TopologyDescription
4949 */
5050 constructor (
5151 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
5858 ) {
5959 options = options ?? { } ;
6060
@@ -64,22 +64,10 @@ export class TopologyDescription {
6464 this . compatible = true ;
6565 this . heartbeatFrequencyMS = options . heartbeatFrequencyMS ?? 0 ;
6666 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 ;
8371
8472 // determine server compatibility
8573 for ( const serverDescription of this . servers . values ( ) ) {
@@ -108,12 +96,12 @@ export class TopologyDescription {
10896 // value among ServerDescriptions of all data-bearing server types. If any have a null
10997 // logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be
11098 // set to null.
111- this . logicalSessionTimeoutMinutes = undefined ;
99+ this . logicalSessionTimeoutMinutes = null ;
112100 for ( const [ , server ] of this . servers ) {
113101 if ( server . isReadable ) {
114102 if ( server . logicalSessionTimeoutMinutes == null ) {
115103 // If any of the servers have a null logicalSessionsTimeout, then the whole topology does
116- this . logicalSessionTimeoutMinutes = undefined ;
104+ this . logicalSessionTimeoutMinutes = null ;
117105 break ;
118106 }
119107
@@ -200,11 +188,6 @@ export class TopologyDescription {
200188 // potentially mutated values
201189 let { type : topologyType , setName, maxSetVersion, maxElectionId, commonWireVersion } = this ;
202190
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-
208191 const serverType = serverDescription . type ;
209192 const serverDescriptions = new Map ( this . servers ) ;
210193
@@ -217,6 +200,19 @@ export class TopologyDescription {
217200 }
218201 }
219202
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+
220216 // update the actual server description
221217 serverDescriptions . set ( address , serverDescription ) ;
222218
@@ -311,15 +307,16 @@ export class TopologyDescription {
311307 ) ;
312308 }
313309
314- get error ( ) : MongoError | undefined {
310+ get error ( ) : MongoServerError | null {
315311 const descriptionsWithError = Array . from ( this . servers . values ( ) ) . filter (
316312 ( sd : ServerDescription ) => sd . error
317313 ) ;
318314
319315 if ( descriptionsWithError . length > 0 ) {
320316 return descriptionsWithError [ 0 ] . error ;
321317 }
322- return ;
318+
319+ return null ;
323320 }
324321
325322 /**
@@ -366,10 +363,10 @@ function topologyTypeForServerType(serverType: ServerType): TopologyType {
366363function updateRsFromPrimary (
367364 serverDescriptions : Map < string , ServerDescription > ,
368365 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 ] {
373370 setName = setName || serverDescription . setName ;
374371 if ( setName !== serverDescription . setName ) {
375372 serverDescriptions . delete ( serverDescription . address ) ;
@@ -436,7 +433,7 @@ function updateRsFromPrimary(
436433function updateRsWithPrimaryFromMember (
437434 serverDescriptions : Map < string , ServerDescription > ,
438435 serverDescription : ServerDescription ,
439- setName ? : string
436+ setName : string | null = null
440437) : TopologyType {
441438 if ( setName == null ) {
442439 // TODO(NODE-3483): should be an appropriate runtime error
@@ -456,10 +453,10 @@ function updateRsWithPrimaryFromMember(
456453function updateRsNoPrimaryFromMember (
457454 serverDescriptions : Map < string , ServerDescription > ,
458455 serverDescription : ServerDescription ,
459- setName ? : string
460- ) : [ TopologyType , string ? ] {
456+ setName : string | null = null
457+ ) : [ TopologyType , string | null ] {
461458 const topologyType = TopologyType . ReplicaSetNoPrimary ;
462- setName = setName || serverDescription . setName ;
459+ setName = setName ?? serverDescription . setName ;
463460 if ( setName !== serverDescription . setName ) {
464461 serverDescriptions . delete ( serverDescription . address ) ;
465462 return [ topologyType , setName ] ;
0 commit comments