Skip to content

Commit 0a3e00d

Browse files
committed
JAVA-2083: Ignore failed ismaster response in server monitor
1 parent 62b0a8b commit 0a3e00d

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

src/main/com/mongodb/ServerMonitor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ private Set<String> listToSet(final List<String> list) {
272272
}
273273
}
274274

275-
private static ServerType getServerType(final BasicDBObject isMasterResult) {
275+
static ServerType getServerType(final CommandResult isMasterResult) {
276+
if (!isMasterResult.ok()) {
277+
return ServerType.Unknown;
278+
}
279+
276280
if (isReplicaSetMember(isMasterResult)) {
277281

278282
if (isMasterResult.getBoolean("hidden", false)) {

src/test/com/mongodb/ServerMonitorSpecification.groovy

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,52 @@ class ServerMonitorSpecification extends FunctionalSpecification {
160160
}
161161

162162
def 'should report correct server type'() {
163+
given:
164+
CommandResult isMasterCommandResult = new CommandResult(new ServerAddress())
165+
isMasterCommandResult.putAll(response)
166+
163167
expect:
164-
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
165-
.append('hidden', true)
166-
.append('secondary', true)) == ServerType.ReplicaSetOther
167-
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
168-
.append('ismaster', true)) == ServerType.ReplicaSetPrimary
169-
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
170-
.append('secondary', true)) == ServerType.ReplicaSetSecondary
171-
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
172-
.append('arbiterOnly', true)) == ServerType.ReplicaSetArbiter
173-
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
174-
.append('hosts', ['server1:27017'])) == ServerType.ReplicaSetOther
175-
ServerMonitor.getServerType(new BasicDBObject('isreplicaset', true)) == ServerType.ReplicaSetGhost
176-
ServerMonitor.getServerType(new BasicDBObject()) == ServerType.StandAlone
177-
ServerMonitor.getServerType(new BasicDBObject('msg', 'isdbgrid')) == ServerType.ShardRouter
178-
ServerMonitor.getServerType(new BasicDBObject('msg', 'whatever')) == ServerType.StandAlone
168+
ServerMonitor.getServerType(isMasterCommandResult) == serverType
169+
170+
where:
171+
response << [
172+
new BasicDBObject('ok', 0),
173+
new BasicDBObject('ok', 1)
174+
.append('setName', 'test')
175+
.append('hidden', true)
176+
.append('secondary', true),
177+
new BasicDBObject('ok', 1)
178+
.append('setName', 'test')
179+
.append('ismaster', true),
180+
new BasicDBObject('ok', 1)
181+
.append('setName', 'test')
182+
.append('secondary', true),
183+
new BasicDBObject('ok', 1)
184+
.append('setName', 'test')
185+
.append('arbiterOnly', true),
186+
new BasicDBObject('ok', 1)
187+
.append('setName', 'test')
188+
.append('hosts', ['server1:27017']),
189+
new BasicDBObject('ok', 1)
190+
.append('isreplicaset', true),
191+
new BasicDBObject('ok', 1),
192+
new BasicDBObject('ok', 1)
193+
.append('msg', 'isdbgrid'),
194+
new BasicDBObject('ok', 1)
195+
.append('msg', 'whatever')
196+
]
197+
serverType << [
198+
ServerType.Unknown,
199+
ServerType.ReplicaSetOther,
200+
ServerType.ReplicaSetPrimary,
201+
ServerType.ReplicaSetSecondary,
202+
ServerType.ReplicaSetArbiter,
203+
ServerType.ReplicaSetOther,
204+
ServerType.ReplicaSetGhost,
205+
ServerType.StandAlone,
206+
ServerType.ShardRouter,
207+
ServerType.StandAlone
208+
]
179209
}
180210

181211
def initializeServerMonitor(ServerAddress address) {

0 commit comments

Comments
 (0)