1
# Copyright 2014-2015 MongoDB, Inc.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
15
"""Parse a response to the 'ismaster' command."""
19
from bson.py3compat import imap
20
from pymongo import common
21
from pymongo.server_type import SERVER_TYPE
24
def _get_server_type(doc):
25
"""Determine the server type from an ismaster response."""
27
return SERVER_TYPE.Unknown
29
if doc.get('isreplicaset'):
30
return SERVER_TYPE.RSGhost
31
elif doc.get('setName'):
33
return SERVER_TYPE.RSOther
34
elif doc.get('ismaster'):
35
return SERVER_TYPE.RSPrimary
36
elif doc.get('secondary'):
37
return SERVER_TYPE.RSSecondary
38
elif doc.get('arbiterOnly'):
39
return SERVER_TYPE.RSArbiter
41
return SERVER_TYPE.RSOther
42
elif doc.get('msg') == 'isdbgrid':
43
return SERVER_TYPE.Mongos
45
return SERVER_TYPE.Standalone
48
class IsMaster(object):
49
__slots__ = ('_doc', '_server_type', '_is_writable', '_is_readable')
51
def __init__(self, doc):
52
"""Parse an ismaster response from the server."""
53
self._server_type = _get_server_type(doc)
55
self._is_writable = self._server_type in (
56
SERVER_TYPE.RSPrimary,
57
SERVER_TYPE.Standalone,
61
self.server_type == SERVER_TYPE.RSSecondary
65
def server_type(self):
66
return self._server_type
70
"""List of hosts, passives, and arbiters known to this server."""
71
return set(imap(common.clean_node, itertools.chain(
72
self._doc.get('hosts', []),
73
self._doc.get('passives', []),
74
self._doc.get('arbiters', []))))
78
"""Replica set member tags or empty dict."""
79
return self._doc.get('tags', {})
83
"""This server's opinion about who the primary is, or None."""
84
if self._doc.get('primary'):
85
return common.partition_node(self._doc['primary'])
90
def replica_set_name(self):
91
"""Replica set name or None."""
92
return self._doc.get('setName')
95
def max_bson_size(self):
96
return self._doc.get('maxBsonObjectSize', common.MAX_BSON_SIZE)
99
def max_message_size(self):
100
return self._doc.get('maxMessageSizeBytes', 2 * self.max_bson_size)
103
def max_write_batch_size(self):
104
return self._doc.get('maxWriteBatchSize', common.MAX_WRITE_BATCH_SIZE)
107
def min_wire_version(self):
108
return self._doc.get('minWireVersion', common.MIN_WIRE_VERSION)
111
def max_wire_version(self):
112
return self._doc.get('maxWireVersion', common.MAX_WIRE_VERSION)
115
def is_writable(self):
116
return self._is_writable
119
def is_readable(self):
120
return self._is_readable