settings.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright 2014-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # 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
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. """Represent MongoClient's configuration."""
  15. import threading
  16. import traceback
  17. from bson.objectid import ObjectId
  18. from pymongo import common, monitor, pool
  19. from pymongo.common import LOCAL_THRESHOLD_MS, SERVER_SELECTION_TIMEOUT
  20. from pymongo.errors import ConfigurationError
  21. from pymongo.pool import PoolOptions
  22. from pymongo.server_description import ServerDescription
  23. from pymongo.topology_description import TOPOLOGY_TYPE
  24. class TopologySettings(object):
  25. def __init__(self,
  26. seeds=None,
  27. replica_set_name=None,
  28. pool_class=None,
  29. pool_options=None,
  30. monitor_class=None,
  31. condition_class=None,
  32. local_threshold_ms=LOCAL_THRESHOLD_MS,
  33. server_selection_timeout=SERVER_SELECTION_TIMEOUT,
  34. heartbeat_frequency=common.HEARTBEAT_FREQUENCY,
  35. server_selector=None,
  36. fqdn=None,
  37. direct_connection=None,
  38. load_balanced=None):
  39. """Represent MongoClient's configuration.
  40. Take a list of (host, port) pairs and optional replica set name.
  41. """
  42. if heartbeat_frequency < common.MIN_HEARTBEAT_INTERVAL:
  43. raise ConfigurationError(
  44. "heartbeatFrequencyMS cannot be less than %d" % (
  45. common.MIN_HEARTBEAT_INTERVAL * 1000,))
  46. self._seeds = seeds or [('localhost', 27017)]
  47. self._replica_set_name = replica_set_name
  48. self._pool_class = pool_class or pool.Pool
  49. self._pool_options = pool_options or PoolOptions()
  50. self._monitor_class = monitor_class or monitor.Monitor
  51. self._condition_class = condition_class or threading.Condition
  52. self._local_threshold_ms = local_threshold_ms
  53. self._server_selection_timeout = server_selection_timeout
  54. self._server_selector = server_selector
  55. self._fqdn = fqdn
  56. self._heartbeat_frequency = heartbeat_frequency
  57. if direct_connection is None:
  58. self._direct = (len(self._seeds) == 1 and not self.replica_set_name)
  59. else:
  60. self._direct = direct_connection
  61. self._load_balanced = load_balanced
  62. self._topology_id = ObjectId()
  63. # Store the allocation traceback to catch unclosed clients in the
  64. # test suite.
  65. self._stack = ''.join(traceback.format_stack())
  66. @property
  67. def seeds(self):
  68. """List of server addresses."""
  69. return self._seeds
  70. @property
  71. def replica_set_name(self):
  72. return self._replica_set_name
  73. @property
  74. def pool_class(self):
  75. return self._pool_class
  76. @property
  77. def pool_options(self):
  78. return self._pool_options
  79. @property
  80. def monitor_class(self):
  81. return self._monitor_class
  82. @property
  83. def condition_class(self):
  84. return self._condition_class
  85. @property
  86. def local_threshold_ms(self):
  87. return self._local_threshold_ms
  88. @property
  89. def server_selection_timeout(self):
  90. return self._server_selection_timeout
  91. @property
  92. def server_selector(self):
  93. return self._server_selector
  94. @property
  95. def heartbeat_frequency(self):
  96. return self._heartbeat_frequency
  97. @property
  98. def fqdn(self):
  99. return self._fqdn
  100. @property
  101. def direct(self):
  102. """Connect directly to a single server, or use a set of servers?
  103. True if there is one seed and no replica_set_name.
  104. """
  105. return self._direct
  106. @property
  107. def load_balanced(self):
  108. """True if the client was configured to connect to a load balancer."""
  109. return self._load_balanced
  110. def get_topology_type(self):
  111. if self.load_balanced:
  112. return TOPOLOGY_TYPE.LoadBalanced
  113. elif self.direct:
  114. return TOPOLOGY_TYPE.Single
  115. elif self.replica_set_name is not None:
  116. return TOPOLOGY_TYPE.ReplicaSetNoPrimary
  117. else:
  118. return TOPOLOGY_TYPE.Unknown
  119. def get_server_descriptions(self):
  120. """Initial dict of (address, ServerDescription) for all seeds."""
  121. return dict([
  122. (address, ServerDescription(address))
  123. for address in self.seeds])