Skip to content

Commit ddf151d

Browse files
committed
fix: only use ServerDescription equality to prevent event emission
SDAM updates should _always_ be processed, an ServerDescription equality should only be used to prevent superfluous event emission NODE-2474
1 parent 5fb0264 commit ddf151d

File tree

7 files changed

+516
-21
lines changed

7 files changed

+516
-21
lines changed

lib/sdam/topology.js

+25-21
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,10 @@ class Topology extends EventEmitter {
507507
}
508508

509509
// If we already know all the information contained in this updated description, then
510-
// we don't need to update anything or emit SDAM events
511-
if (previousServerDescription && previousServerDescription.equals(serverDescription)) {
512-
return;
513-
}
510+
// we don't need to emit SDAM events, but still need to update the description, in order
511+
// to keep client-tracked attributes like last update time and round trip time up to date
512+
const equalDescriptions =
513+
previousServerDescription && previousServerDescription.equals(serverDescription);
514514

515515
// first update the TopologyDescription
516516
this.s.description = this.s.description.update(serverDescription);
@@ -520,15 +520,17 @@ class Topology extends EventEmitter {
520520
}
521521

522522
// emit monitoring events for this change
523-
this.emit(
524-
'serverDescriptionChanged',
525-
new ServerDescriptionChangedEvent(
526-
this.s.id,
527-
serverDescription.address,
528-
previousServerDescription,
529-
this.s.description.servers.get(serverDescription.address)
530-
)
531-
);
523+
if (!equalDescriptions) {
524+
this.emit(
525+
'serverDescriptionChanged',
526+
new ServerDescriptionChangedEvent(
527+
this.s.id,
528+
serverDescription.address,
529+
previousServerDescription,
530+
this.s.description.servers.get(serverDescription.address)
531+
)
532+
);
533+
}
532534

533535
// update server list from updated descriptions
534536
updateServers(this, serverDescription);
@@ -538,14 +540,16 @@ class Topology extends EventEmitter {
538540
processWaitQueue(this);
539541
}
540542

541-
this.emit(
542-
'topologyDescriptionChanged',
543-
new TopologyDescriptionChangedEvent(
544-
this.s.id,
545-
previousTopologyDescription,
546-
this.s.description
547-
)
548-
);
543+
if (!equalDescriptions) {
544+
this.emit(
545+
'topologyDescriptionChanged',
546+
new TopologyDescriptionChangedEvent(
547+
this.s.id,
548+
previousTopologyDescription,
549+
this.s.description
550+
)
551+
);
552+
}
549553
}
550554

551555
auth(credentials, callback) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"description": "Primary mismatched me is not removed",
3+
"uri": "mongodb://localhost:27017,localhost:27018/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"localhost:27017",
9+
{
10+
"ok": 1,
11+
"hosts": [
12+
"localhost:27017",
13+
"localhost:27018"
14+
],
15+
"ismaster": true,
16+
"setName": "rs",
17+
"primary": "localhost:27017",
18+
"me": "a:27017",
19+
"minWireVersion": 0,
20+
"maxWireVersion": 7
21+
}
22+
]
23+
],
24+
"outcome": {
25+
"servers": {
26+
"localhost:27017": {
27+
"type": "RSPrimary",
28+
"setName": "rs"
29+
},
30+
"localhost:27018": {
31+
"type": "Unknown",
32+
"setName": null
33+
}
34+
},
35+
"topologyType": "ReplicaSetWithPrimary",
36+
"logicalSessionTimeoutMinutes": null,
37+
"setName": "rs"
38+
}
39+
},
40+
{
41+
"responses": [
42+
[
43+
"localhost:27018",
44+
{
45+
"ok": 1,
46+
"hosts": [
47+
"localhost:27017",
48+
"localhost:27018"
49+
],
50+
"ismaster": false,
51+
"secondary": true,
52+
"setName": "rs",
53+
"primary": "localhost:27017",
54+
"me": "localhost:27018",
55+
"minWireVersion": 0,
56+
"maxWireVersion": 7
57+
}
58+
]
59+
],
60+
"outcome": {
61+
"servers": {
62+
"localhost:27017": {
63+
"type": "RSPrimary",
64+
"setName": "rs"
65+
},
66+
"localhost:27018": {
67+
"type": "RSSecondary",
68+
"setName": "rs"
69+
}
70+
},
71+
"topologyType": "ReplicaSetWithPrimary",
72+
"logicalSessionTimeoutMinutes": null,
73+
"setName": "rs"
74+
}
75+
}
76+
]
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
description: Primary mismatched me is not removed
2+
uri: mongodb://localhost:27017,localhost:27018/?replicaSet=rs
3+
4+
phases: [
5+
{
6+
responses: [
7+
["localhost:27017", {
8+
ok: 1,
9+
hosts: [
10+
"localhost:27017",
11+
"localhost:27018"
12+
],
13+
ismaster: true,
14+
setName: "rs",
15+
primary: "localhost:27017",
16+
# me does not match the primary responder's address, but the server
17+
# is still added because we don't me mismatch check the primary and all
18+
# servers from a primary ismaster are added to the working server set
19+
me: "a:27017",
20+
minWireVersion: 0,
21+
maxWireVersion: 7
22+
}]
23+
],
24+
outcome: {
25+
servers: {
26+
"localhost:27017": {
27+
type: "RSPrimary",
28+
setName: "rs"
29+
},
30+
"localhost:27018": {
31+
type: "Unknown",
32+
setName: null
33+
}
34+
},
35+
topologyType: "ReplicaSetWithPrimary",
36+
logicalSessionTimeoutMinutes: null,
37+
setName: "rs"
38+
}
39+
},
40+
{
41+
responses: [
42+
["localhost:27018", {
43+
ok: 1,
44+
hosts: [
45+
"localhost:27017",
46+
"localhost:27018"
47+
],
48+
ismaster: false,
49+
secondary: true,
50+
setName: "rs",
51+
primary: "localhost:27017",
52+
me: "localhost:27018",
53+
minWireVersion: 0,
54+
maxWireVersion: 7
55+
}]
56+
],
57+
outcome: {
58+
servers: {
59+
"localhost:27017": {
60+
type: "RSPrimary",
61+
setName: "rs"
62+
},
63+
"localhost:27018": {
64+
type: "RSSecondary",
65+
setName: "rs"
66+
}
67+
},
68+
topologyType: "ReplicaSetWithPrimary",
69+
logicalSessionTimeoutMinutes: null,
70+
setName: "rs"
71+
}
72+
}
73+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"description": "Repeated ismaster response must be processed",
3+
"uri": "mongodb://a,b/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"a:27017",
9+
{
10+
"ok": 1,
11+
"ismaster": false,
12+
"secondary": true,
13+
"hidden": true,
14+
"hosts": [
15+
"a:27017",
16+
"c:27017"
17+
],
18+
"setName": "rs",
19+
"minWireVersion": 0,
20+
"maxWireVersion": 6
21+
}
22+
]
23+
],
24+
"outcome": {
25+
"servers": {
26+
"a:27017": {
27+
"type": "RSOther",
28+
"setName": "rs"
29+
},
30+
"b:27017": {
31+
"type": "Unknown"
32+
},
33+
"c:27017": {
34+
"type": "Unknown"
35+
}
36+
},
37+
"topologyType": "ReplicaSetNoPrimary",
38+
"logicalSessionTimeoutMinutes": null,
39+
"setName": "rs"
40+
}
41+
},
42+
{
43+
"responses": [
44+
[
45+
"c:27017",
46+
{
47+
"ok": 1,
48+
"ismaster": true,
49+
"minWireVersion": 0,
50+
"maxWireVersion": 6
51+
}
52+
]
53+
],
54+
"outcome": {
55+
"servers": {
56+
"a:27017": {
57+
"type": "RSOther",
58+
"setName": "rs"
59+
},
60+
"b:27017": {
61+
"type": "Unknown"
62+
}
63+
},
64+
"topologyType": "ReplicaSetNoPrimary",
65+
"logicalSessionTimeoutMinutes": null,
66+
"setName": "rs"
67+
}
68+
},
69+
{
70+
"responses": [
71+
[
72+
"a:27017",
73+
{
74+
"ok": 1,
75+
"ismaster": false,
76+
"secondary": true,
77+
"hidden": true,
78+
"hosts": [
79+
"a:27017",
80+
"c:27017"
81+
],
82+
"setName": "rs",
83+
"minWireVersion": 0,
84+
"maxWireVersion": 6
85+
}
86+
]
87+
],
88+
"outcome": {
89+
"servers": {
90+
"a:27017": {
91+
"type": "RSOther",
92+
"setName": "rs"
93+
},
94+
"b:27017": {
95+
"type": "Unknown"
96+
},
97+
"c:27017": {
98+
"type": "Unknown"
99+
}
100+
},
101+
"topologyType": "ReplicaSetNoPrimary",
102+
"logicalSessionTimeoutMinutes": null,
103+
"setName": "rs"
104+
}
105+
},
106+
{
107+
"responses": [
108+
[
109+
"c:27017",
110+
{
111+
"ok": 1,
112+
"ismaster": true,
113+
"hosts": [
114+
"a:27017",
115+
"c:27017"
116+
],
117+
"setName": "rs",
118+
"minWireVersion": 0,
119+
"maxWireVersion": 6
120+
}
121+
]
122+
],
123+
"outcome": {
124+
"servers": {
125+
"a:27017": {
126+
"type": "RSOther",
127+
"setName": "rs"
128+
},
129+
"c:27017": {
130+
"type": "RSPrimary",
131+
"setName": "rs"
132+
}
133+
},
134+
"topologyType": "ReplicaSetWithPrimary",
135+
"logicalSessionTimeoutMinutes": null,
136+
"setName": "rs"
137+
}
138+
}
139+
]
140+
}

0 commit comments

Comments
 (0)