Skip to content

RUBY-912 Make sure that max_election_id is never reset to nil #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/mongo/cluster/topology/replica_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def elect_primary(description, servers)
server.description.unknown!
end
end
@max_election_id = description.election_id
update_max_election_id(description)
end
else
log_warn([
Expand Down Expand Up @@ -219,6 +219,12 @@ def standalone_discovered; self; end

private

def update_max_election_id(description)
if description.election_id && description.election_id > @max_election_id
@max_election_id = description.election_id
end
end

def detect_stale_primary!(description)
if description.election_id && description.election_id < @max_election_id
description.unknown!
Expand Down
67 changes: 57 additions & 10 deletions spec/support/sdam/rs/null_election_id.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description: "Primary with no electionId, then a primary with electionId"
description: "Primaries with and without electionIds"

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

Expand All @@ -10,7 +10,7 @@ phases: [
["a:27017", {
ok: 1,
ismaster: true,
hosts: ["a:27017", "b:27017"],
hosts: ["a:27017", "b:27017", "c:27017"],
setName: "rs"
}]
],
Expand All @@ -26,11 +26,15 @@ phases: [
type: "Unknown",
setName: ,
electionId:
},
"c:27017": {
type: "Unknown",
setName: ,
electionId:
}
},
topologyType: "ReplicaSetWithPrimary",
setName: "rs",
maxElectionId:
}
},

Expand All @@ -40,9 +44,9 @@ phases: [
["b:27017", {
ok: 1,
ismaster: true,
hosts: ["a:27017", "b:27017"],
hosts: ["a:27017", "b:27017", "c:27017"],
setName: "rs",
electionId: {"$oid": "000000000000000000000001"}
electionId: {"$oid": "000000000000000000000002"}
}]
],

Expand All @@ -56,12 +60,16 @@ phases: [
"b:27017": {
type: "RSPrimary",
setName: "rs",
electionId: {"$oid": "000000000000000000000001"}
electionId: {"$oid": "000000000000000000000002"}
},
"c:27017": {
type: "Unknown",
setName: ,
electionId:
}
},
topologyType: "ReplicaSetWithPrimary",
setName: "rs",
maxElectionId: {"$oid": "000000000000000000000001"}
}
},

Expand All @@ -71,7 +79,7 @@ phases: [
["a:27017", {
ok: 1,
ismaster: true,
hosts: ["a:27017", "b:27017"],
hosts: ["a:27017", "b:27017", "c:27017"],
setName: "rs"
}]
],
Expand All @@ -86,12 +94,51 @@ phases: [
type: "Unknown",
setName: ,
electionId:
},
"c:27017": {
type: "Unknown",
setName: ,
electionId:
}
},
topologyType: "ReplicaSetWithPrimary",
setName: "rs",
}
},

# But we remember A's electionId, so when we finally hear from C
# claiming it is primary, we ignore it due to its outdated electionId
{
responses: [
["c:27017", {
ok: 1,
ismaster: true,
hosts: ["a:27017", "b:27017", "c:27017"],
setName: "rs",
electionId: {"$oid": "000000000000000000000001"}
}]
],
outcome: {
servers: {
# Still primary.
"a:27017": {
type: "RSPrimary",
setName: "rs",
electionId:
},
"b:27017": {
type: "Unknown",
setName: ,
electionId:
},
"c:27017": {
type: "Unknown",
setName: ,
electionId:
}
},
topologyType: "ReplicaSetWithPrimary",
setName: "rs",
# But we remember old electionId.
maxElectionId: {"$oid": "000000000000000000000001"}
}
}
]