server_description.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2014-2015 MongoDB, Inc.
  2. #
  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
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  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.
  14. """Represent one server in the topology."""
  15. from pymongo.server_type import SERVER_TYPE
  16. from pymongo.ismaster import IsMaster
  17. class ServerDescription(object):
  18. """Immutable representation of one server.
  19. :Parameters:
  20. - `address`: A (host, port) pair
  21. - `ismaster`: Optional IsMaster instance
  22. - `round_trip_time`: Optional float
  23. - `error`: Optional, the last error attempting to connect to the server
  24. """
  25. __slots__ = (
  26. '_address', '_server_type', '_all_hosts', '_tags', '_replica_set_name',
  27. '_primary', '_max_bson_size', '_max_message_size',
  28. '_max_write_batch_size', '_min_wire_version', '_max_wire_version',
  29. '_round_trip_time', '_is_writable', '_is_readable', '_error',
  30. '_election_id')
  31. def __init__(
  32. self,
  33. address,
  34. ismaster=None,
  35. round_trip_time=None,
  36. error=None):
  37. self._address = address
  38. if not ismaster:
  39. ismaster = IsMaster({})
  40. self._server_type = ismaster.server_type
  41. self._all_hosts = ismaster.all_hosts
  42. self._tags = ismaster.tags
  43. self._replica_set_name = ismaster.replica_set_name
  44. self._primary = ismaster.primary
  45. self._max_bson_size = ismaster.max_bson_size
  46. self._max_message_size = ismaster.max_message_size
  47. self._max_write_batch_size = ismaster.max_write_batch_size
  48. self._min_wire_version = ismaster.min_wire_version
  49. self._max_wire_version = ismaster.max_wire_version
  50. self._election_id = ismaster.election_id
  51. self._is_writable = ismaster.is_writable
  52. self._is_readable = ismaster.is_readable
  53. self._round_trip_time = round_trip_time
  54. self._error = error
  55. @property
  56. def address(self):
  57. return self._address
  58. @property
  59. def server_type(self):
  60. return self._server_type
  61. @property
  62. def all_hosts(self):
  63. """List of hosts, passives, and arbiters known to this server."""
  64. return self._all_hosts
  65. @property
  66. def tags(self):
  67. return self._tags
  68. @property
  69. def replica_set_name(self):
  70. """Replica set name or None."""
  71. return self._replica_set_name
  72. @property
  73. def primary(self):
  74. """This server's opinion about who the primary is, or None."""
  75. return self._primary
  76. @property
  77. def max_bson_size(self):
  78. return self._max_bson_size
  79. @property
  80. def max_message_size(self):
  81. return self._max_message_size
  82. @property
  83. def max_write_batch_size(self):
  84. return self._max_write_batch_size
  85. @property
  86. def min_wire_version(self):
  87. return self._min_wire_version
  88. @property
  89. def max_wire_version(self):
  90. return self._max_wire_version
  91. @property
  92. def election_id(self):
  93. return self._election_id
  94. @property
  95. def round_trip_time(self):
  96. """The current average latency or None."""
  97. # This override is for unittesting only!
  98. if self._address in self._host_to_round_trip_time:
  99. return self._host_to_round_trip_time[self._address]
  100. return self._round_trip_time
  101. @property
  102. def error(self):
  103. """The last error attempting to connect to the server, or None."""
  104. return self._error
  105. @property
  106. def is_writable(self):
  107. return self._is_writable
  108. @property
  109. def is_readable(self):
  110. return self._is_readable
  111. @property
  112. def is_server_type_known(self):
  113. return self.server_type != SERVER_TYPE.Unknown
  114. # For unittesting only. Use under no circumstances!
  115. _host_to_round_trip_time = {}