Skip to content

Commit

Permalink
JAVA-2083: Ignore failed ismaster response in server monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
jyemin committed Jan 13, 2016
1 parent 62b0a8b commit 0a3e00d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/main/com/mongodb/ServerMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ private Set<String> listToSet(final List<String> list) {
}
}

private static ServerType getServerType(final BasicDBObject isMasterResult) {
static ServerType getServerType(final CommandResult isMasterResult) {
if (!isMasterResult.ok()) {
return ServerType.Unknown;
}

if (isReplicaSetMember(isMasterResult)) {

if (isMasterResult.getBoolean("hidden", false)) {
Expand Down
60 changes: 45 additions & 15 deletions src/test/com/mongodb/ServerMonitorSpecification.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,52 @@ class ServerMonitorSpecification extends FunctionalSpecification {
}

def 'should report correct server type'() {
given:
CommandResult isMasterCommandResult = new CommandResult(new ServerAddress())
isMasterCommandResult.putAll(response)

expect:
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
.append('hidden', true)
.append('secondary', true)) == ServerType.ReplicaSetOther
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
.append('ismaster', true)) == ServerType.ReplicaSetPrimary
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
.append('secondary', true)) == ServerType.ReplicaSetSecondary
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
.append('arbiterOnly', true)) == ServerType.ReplicaSetArbiter
ServerMonitor.getServerType(new BasicDBObject('setName', 'test')
.append('hosts', ['server1:27017'])) == ServerType.ReplicaSetOther
ServerMonitor.getServerType(new BasicDBObject('isreplicaset', true)) == ServerType.ReplicaSetGhost
ServerMonitor.getServerType(new BasicDBObject()) == ServerType.StandAlone
ServerMonitor.getServerType(new BasicDBObject('msg', 'isdbgrid')) == ServerType.ShardRouter
ServerMonitor.getServerType(new BasicDBObject('msg', 'whatever')) == ServerType.StandAlone
ServerMonitor.getServerType(isMasterCommandResult) == serverType

where:
response << [
new BasicDBObject('ok', 0),
new BasicDBObject('ok', 1)
.append('setName', 'test')
.append('hidden', true)
.append('secondary', true),
new BasicDBObject('ok', 1)
.append('setName', 'test')
.append('ismaster', true),
new BasicDBObject('ok', 1)
.append('setName', 'test')
.append('secondary', true),
new BasicDBObject('ok', 1)
.append('setName', 'test')
.append('arbiterOnly', true),
new BasicDBObject('ok', 1)
.append('setName', 'test')
.append('hosts', ['server1:27017']),
new BasicDBObject('ok', 1)
.append('isreplicaset', true),
new BasicDBObject('ok', 1),
new BasicDBObject('ok', 1)
.append('msg', 'isdbgrid'),
new BasicDBObject('ok', 1)
.append('msg', 'whatever')
]
serverType << [
ServerType.Unknown,
ServerType.ReplicaSetOther,
ServerType.ReplicaSetPrimary,
ServerType.ReplicaSetSecondary,
ServerType.ReplicaSetArbiter,
ServerType.ReplicaSetOther,
ServerType.ReplicaSetGhost,
ServerType.StandAlone,
ServerType.ShardRouter,
ServerType.StandAlone
]
}

def initializeServerMonitor(ServerAddress address) {
Expand Down

0 comments on commit 0a3e00d

Please sign in to comment.