Skip to content

Commit 15df58d

Browse files
committed
Merge pull request #647 from estolfo/RUBY-912-election-id
RUBY-912 Make sure that max_election_id is never reset to nil
2 parents a57062e + 92e5f28 commit 15df58d

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

lib/mongo/cluster/topology/replica_set.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def elect_primary(description, servers)
6767
server.description.unknown!
6868
end
6969
end
70-
@max_election_id = description.election_id
70+
update_max_election_id(description)
7171
end
7272
else
7373
log_warn([
@@ -219,6 +219,12 @@ def standalone_discovered; self; end
219219

220220
private
221221

222+
def update_max_election_id(description)
223+
if description.election_id && description.election_id > @max_election_id
224+
@max_election_id = description.election_id
225+
end
226+
end
227+
222228
def detect_stale_primary!(description)
223229
if description.election_id && description.election_id < @max_election_id
224230
description.unknown!

spec/support/sdam/rs/null_election_id.yml

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
description: "Primary with no electionId, then a primary with electionId"
1+
description: "Primaries with and without electionIds"
22

33
uri: "mongodb://a/?replicaSet=rs"
44

@@ -10,7 +10,7 @@ phases: [
1010
["a:27017", {
1111
ok: 1,
1212
ismaster: true,
13-
hosts: ["a:27017", "b:27017"],
13+
hosts: ["a:27017", "b:27017", "c:27017"],
1414
setName: "rs"
1515
}]
1616
],
@@ -26,11 +26,15 @@ phases: [
2626
type: "Unknown",
2727
setName: ,
2828
electionId:
29+
},
30+
"c:27017": {
31+
type: "Unknown",
32+
setName: ,
33+
electionId:
2934
}
3035
},
3136
topologyType: "ReplicaSetWithPrimary",
3237
setName: "rs",
33-
maxElectionId:
3438
}
3539
},
3640

@@ -40,9 +44,9 @@ phases: [
4044
["b:27017", {
4145
ok: 1,
4246
ismaster: true,
43-
hosts: ["a:27017", "b:27017"],
47+
hosts: ["a:27017", "b:27017", "c:27017"],
4448
setName: "rs",
45-
electionId: {"$oid": "000000000000000000000001"}
49+
electionId: {"$oid": "000000000000000000000002"}
4650
}]
4751
],
4852

@@ -56,12 +60,16 @@ phases: [
5660
"b:27017": {
5761
type: "RSPrimary",
5862
setName: "rs",
59-
electionId: {"$oid": "000000000000000000000001"}
63+
electionId: {"$oid": "000000000000000000000002"}
64+
},
65+
"c:27017": {
66+
type: "Unknown",
67+
setName: ,
68+
electionId:
6069
}
6170
},
6271
topologyType: "ReplicaSetWithPrimary",
6372
setName: "rs",
64-
maxElectionId: {"$oid": "000000000000000000000001"}
6573
}
6674
},
6775

@@ -71,7 +79,7 @@ phases: [
7179
["a:27017", {
7280
ok: 1,
7381
ismaster: true,
74-
hosts: ["a:27017", "b:27017"],
82+
hosts: ["a:27017", "b:27017", "c:27017"],
7583
setName: "rs"
7684
}]
7785
],
@@ -86,12 +94,51 @@ phases: [
8694
type: "Unknown",
8795
setName: ,
8896
electionId:
97+
},
98+
"c:27017": {
99+
type: "Unknown",
100+
setName: ,
101+
electionId:
102+
}
103+
},
104+
topologyType: "ReplicaSetWithPrimary",
105+
setName: "rs",
106+
}
107+
},
108+
109+
# But we remember A's electionId, so when we finally hear from C
110+
# claiming it is primary, we ignore it due to its outdated electionId
111+
{
112+
responses: [
113+
["c:27017", {
114+
ok: 1,
115+
ismaster: true,
116+
hosts: ["a:27017", "b:27017", "c:27017"],
117+
setName: "rs",
118+
electionId: {"$oid": "000000000000000000000001"}
119+
}]
120+
],
121+
outcome: {
122+
servers: {
123+
# Still primary.
124+
"a:27017": {
125+
type: "RSPrimary",
126+
setName: "rs",
127+
electionId:
128+
},
129+
"b:27017": {
130+
type: "Unknown",
131+
setName: ,
132+
electionId:
133+
},
134+
"c:27017": {
135+
type: "Unknown",
136+
setName: ,
137+
electionId:
89138
}
90139
},
91140
topologyType: "ReplicaSetWithPrimary",
92141
setName: "rs",
93-
# But we remember old electionId.
94-
maxElectionId: {"$oid": "000000000000000000000001"}
95142
}
96143
}
97144
]

0 commit comments

Comments
 (0)