server_selectors.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright 2014-2016 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. """Criteria to select some ServerDescriptions from a TopologyDescription."""
  15. from pymongo.server_type import SERVER_TYPE
  16. class Selection(object):
  17. """Input or output of a server selector function."""
  18. @classmethod
  19. def from_topology_description(cls, topology_description):
  20. known_servers = topology_description.known_servers
  21. primary = None
  22. for sd in known_servers:
  23. if sd.server_type == SERVER_TYPE.RSPrimary:
  24. primary = sd
  25. break
  26. return Selection(topology_description,
  27. topology_description.known_servers,
  28. topology_description.common_wire_version,
  29. primary)
  30. def __init__(self,
  31. topology_description,
  32. server_descriptions,
  33. common_wire_version,
  34. primary):
  35. self.topology_description = topology_description
  36. self.server_descriptions = server_descriptions
  37. self.primary = primary
  38. self.common_wire_version = common_wire_version
  39. def with_server_descriptions(self, server_descriptions):
  40. return Selection(self.topology_description,
  41. server_descriptions,
  42. self.common_wire_version,
  43. self.primary)
  44. def secondary_with_max_last_write_date(self):
  45. secondaries = secondary_server_selector(self)
  46. if secondaries.server_descriptions:
  47. return max(secondaries.server_descriptions,
  48. key=lambda sd: sd.last_write_date)
  49. @property
  50. def primary_selection(self):
  51. primaries = [self.primary] if self.primary else []
  52. return self.with_server_descriptions(primaries)
  53. @property
  54. def heartbeat_frequency(self):
  55. return self.topology_description.heartbeat_frequency
  56. @property
  57. def topology_type(self):
  58. return self.topology_description.topology_type
  59. def __bool__(self):
  60. return bool(self.server_descriptions)
  61. __nonzero__ = __bool__ # Python 2.
  62. def __getitem__(self, item):
  63. return self.server_descriptions[item]
  64. def any_server_selector(selection):
  65. return selection
  66. def readable_server_selector(selection):
  67. return selection.with_server_descriptions(
  68. [s for s in selection.server_descriptions if s.is_readable])
  69. def writable_server_selector(selection):
  70. return selection.with_server_descriptions(
  71. [s for s in selection.server_descriptions if s.is_writable])
  72. def secondary_server_selector(selection):
  73. return selection.with_server_descriptions(
  74. [s for s in selection.server_descriptions
  75. if s.server_type == SERVER_TYPE.RSSecondary])
  76. def arbiter_server_selector(selection):
  77. return selection.with_server_descriptions(
  78. [s for s in selection.server_descriptions
  79. if s.server_type == SERVER_TYPE.RSArbiter])
  80. def writable_preferred_server_selector(selection):
  81. """Like PrimaryPreferred but doesn't use tags or latency."""
  82. return (writable_server_selector(selection) or
  83. secondary_server_selector(selection))
  84. def apply_single_tag_set(tag_set, selection):
  85. """All servers matching one tag set.
  86. A tag set is a dict. A server matches if its tags are a superset:
  87. A server tagged {'a': '1', 'b': '2'} matches the tag set {'a': '1'}.
  88. The empty tag set {} matches any server.
  89. """
  90. def tags_match(server_tags):
  91. for key, value in tag_set.items():
  92. if key not in server_tags or server_tags[key] != value:
  93. return False
  94. return True
  95. return selection.with_server_descriptions(
  96. [s for s in selection.server_descriptions if tags_match(s.tags)])
  97. def apply_tag_sets(tag_sets, selection):
  98. """All servers match a list of tag sets.
  99. tag_sets is a list of dicts. The empty tag set {} matches any server,
  100. and may be provided at the end of the list as a fallback. So
  101. [{'a': 'value'}, {}] expresses a preference for servers tagged
  102. {'a': 'value'}, but accepts any server if none matches the first
  103. preference.
  104. """
  105. for tag_set in tag_sets:
  106. with_tag_set = apply_single_tag_set(tag_set, selection)
  107. if with_tag_set:
  108. return with_tag_set
  109. return selection.with_server_descriptions([])
  110. def secondary_with_tags_server_selector(tag_sets, selection):
  111. """All near-enough secondaries matching the tag sets."""
  112. return apply_tag_sets(tag_sets, secondary_server_selector(selection))
  113. def member_with_tags_server_selector(tag_sets, selection):
  114. """All near-enough members matching the tag sets."""
  115. return apply_tag_sets(tag_sets, readable_server_selector(selection))