Skip to content

Commit b9fb18e

Browse files
committed
fixes
1 parent 9f3b8d2 commit b9fb18e

File tree

5 files changed

+64
-127
lines changed

5 files changed

+64
-127
lines changed

src/sdam/server_description.ts

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Document, Long, ObjectId } from '../bson';
22
import type { MongoError } from '../error';
33
import { arrayStrictEqual, errorStrictEqual, HostAddress, now } from '../utils';
4-
import type { ClusterTime } from './common';
5-
import { ServerType } from './common';
4+
import { ClusterTime, ServerType } from './common';
65

76
const WRITABLE_SERVER_TYPES = new Set<ServerType>([
87
ServerType.RSPrimary,
@@ -50,31 +49,27 @@ export interface ServerDescriptionOptions {
5049
* @public
5150
*/
5251
export class ServerDescription {
53-
private _hostAddress: HostAddress;
54-
address: string;
55-
type: ServerType;
56-
hosts: string[];
57-
passives: string[];
58-
arbiters: string[];
59-
tags: TagSet;
60-
61-
error?: MongoError;
62-
topologyVersion?: TopologyVersion;
63-
minWireVersion: number;
64-
maxWireVersion: number;
65-
roundTripTime: number;
66-
lastUpdateTime: number;
67-
lastWriteDate: number;
68-
69-
me?: string;
70-
primary?: string;
71-
setName?: string;
72-
setVersion?: number;
73-
electionId?: ObjectId;
74-
logicalSessionTimeoutMinutes?: number;
75-
76-
// NOTE: does this belong here? It seems we should gossip the cluster time at the CMAP level
77-
$clusterTime?: ClusterTime;
52+
private readonly _hostAddress: HostAddress;
53+
readonly address: string;
54+
readonly type: ServerType;
55+
readonly hosts: string[];
56+
readonly passives: string[];
57+
readonly arbiters: string[];
58+
readonly tags: TagSet;
59+
readonly error: MongoError | undefined;
60+
readonly topologyVersion: TopologyVersion | undefined;
61+
readonly minWireVersion: number;
62+
readonly maxWireVersion: number;
63+
readonly roundTripTime: number;
64+
readonly lastUpdateTime: number;
65+
readonly lastWriteDate: number;
66+
readonly me: string | undefined;
67+
readonly primary: string | undefined;
68+
readonly setName: string | undefined;
69+
readonly setVersion: number | undefined;
70+
readonly electionId: ObjectId | undefined;
71+
readonly logicalSessionTimeoutMinutes: number | undefined;
72+
readonly $clusterTime: ClusterTime | undefined;
7873

7974
/**
8075
* Create a ServerDescription
@@ -83,7 +78,14 @@ export class ServerDescription {
8378
* @param address - The address of the server
8479
* @param hello - An optional hello response for this server
8580
*/
86-
constructor(address: HostAddress | string, hello?: Document, options?: ServerDescriptionOptions) {
81+
constructor(
82+
address: HostAddress | string,
83+
hello: Document = {},
84+
options: ServerDescriptionOptions = {}
85+
) {
86+
hello ??= {};
87+
options ??= {};
88+
8789
if (typeof address === 'string') {
8890
this._hostAddress = new HostAddress(address);
8991
this.address = this._hostAddress.toString();
@@ -102,43 +104,23 @@ export class ServerDescription {
102104
this.lastUpdateTime = now();
103105
this.lastWriteDate = hello?.lastWrite?.lastWriteDate ?? 0;
104106

107+
this.topologyVersion = undefined;
105108
if (options?.topologyVersion) {
106109
this.topologyVersion = options.topologyVersion;
107110
} else if (hello?.topologyVersion) {
108111
this.topologyVersion = hello.topologyVersion;
109112
}
110113

111-
if (options?.error) {
112-
this.error = options.error;
113-
}
114-
115-
if (hello?.primary) {
116-
this.primary = hello.primary;
117-
}
118-
119-
if (hello?.me) {
120-
this.me = hello.me.toLowerCase();
121-
}
122-
123-
if (hello?.setName) {
124-
this.setName = hello.setName;
125-
}
114+
this.error = options.error;
115+
this.primary = hello.primary;
116+
this.me = hello.me?.toLowerCase();
117+
this.setName = hello.setName;
118+
this.setVersion = hello.setVersion;
119+
this.electionId = hello.electionId;
120+
this.logicalSessionTimeoutMinutes = hello.logicalSessionTimeoutMinutes;
121+
this.$clusterTime = hello.$clusterTime;
126122

127-
if (hello?.setVersion) {
128-
this.setVersion = hello.setVersion;
129-
}
130-
131-
if (hello?.electionId) {
132-
this.electionId = hello.electionId;
133-
}
134-
135-
if (hello?.logicalSessionTimeoutMinutes) {
136-
this.logicalSessionTimeoutMinutes = hello.logicalSessionTimeoutMinutes;
137-
}
138-
139-
if (hello?.$clusterTime) {
140-
this.$clusterTime = hello.$clusterTime;
141-
}
123+
// Object.freeze(this); - Mock server and tests edit serverDescriptions...
142124
}
143125

144126
get hostAddress(): HostAddress {

src/sdam/topology_description.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ export interface TopologyDescriptionOptions {
3131
* @public
3232
*/
3333
export class TopologyDescription {
34-
type: TopologyType;
35-
setName: string | undefined;
36-
maxSetVersion: number | undefined;
37-
maxElectionId: ObjectId | undefined;
38-
servers: Map<string, ServerDescription>;
39-
stale: boolean;
40-
compatible: boolean;
41-
compatibilityError: string | undefined;
42-
logicalSessionTimeoutMinutes: number | undefined;
43-
heartbeatFrequencyMS: number;
44-
localThresholdMS: number;
45-
commonWireVersion: number | undefined;
34+
readonly type: TopologyType;
35+
readonly setName: string | undefined;
36+
readonly maxSetVersion: number | undefined;
37+
readonly maxElectionId: ObjectId | undefined;
38+
readonly servers: Map<string, ServerDescription>;
39+
readonly stale: boolean;
40+
readonly compatible: boolean;
41+
readonly compatibilityError: string | undefined;
42+
readonly logicalSessionTimeoutMinutes: number | undefined;
43+
readonly heartbeatFrequencyMS: number;
44+
readonly localThresholdMS: number;
45+
readonly commonWireVersion: number | undefined;
4646

4747
/**
4848
* Create a TopologyDescription

test/integration/server-discovery-and-monitoring/electionId_precedence.test.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/tools/spec-runner/matcher.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@ function generateMatchAndDiffSpecialCase(key, expectedObj, actualObj, metadata)
2424
const expected = expectedObj[key];
2525
const actual = actualObj[key];
2626

27-
if (expected === null) {
27+
if (expected == null) {
2828
if (key === 'readConcern') {
29-
// HACK: get around NODE-1889
29+
// TODO(NODE-1889): In our command construction, we are automatically adding a readConcern.afterClusterTime to every command that has a session.
30+
// The Causal Consistency spec only says to add afterClusterTime to read commands, and explicitly says to not add it to runCommand-like behavior.
3031
return {
3132
match: true,
3233
expected: SYMBOL_DOES_NOT_EXIST,
3334
actual: SYMBOL_DOES_NOT_EXIST
3435
};
3536
}
3637

37-
const match = !Object.prototype.hasOwnProperty.call(actualObj, key);
38+
const actualHasKey = Object.prototype.hasOwnProperty.call(actualObj, key);
39+
const actualIsNullish = actualObj[key] == null;
3840
return {
39-
match,
40-
expected: SYMBOL_DOES_NOT_EXIST,
41-
actual: match ? SYMBOL_DOES_NOT_EXIST : actual
41+
match: (actualHasKey && actualIsNullish) || !actualHasKey,
42+
expected,
43+
actual
4244
};
4345
}
4446

@@ -200,4 +202,7 @@ function matchMongoSpec(chai, utils) {
200202
};
201203
}
202204

203-
module.exports.default = matchMongoSpec;
205+
module.exports = {
206+
default: matchMongoSpec,
207+
generateMatchAndDiffMongoSpec: generateMatchAndDiff
208+
};

test/unit/assorted/server_discovery_and_monitoring.spec.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ function assertOutcomeExpectations(topology, events, outcome) {
286286
const omittedFields = findOmittedFields(expectedServer);
287287

288288
const actualServer = actualServers.get(serverName);
289-
// resultCheck(actualServer, expectedServer, null);
290289
expect(actualServer).to.matchMongoSpec(expectedServer);
291290

292291
if (omittedFields.length) {

0 commit comments

Comments
 (0)