read_preference.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright 2014 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. #pragma once
  15. #include <chrono>
  16. #include <cstdint>
  17. #include <memory>
  18. #include <string>
  19. #include <bsoncxx/document/view_or_value.hpp>
  20. #include <bsoncxx/stdx/optional.hpp>
  21. #include <mongocxx/options/transaction.hpp>
  22. #include <mongocxx/stdx.hpp>
  23. #include <mongocxx/config/prelude.hpp>
  24. namespace mongocxx {
  25. MONGOCXX_INLINE_NAMESPACE_BEGIN
  26. class client;
  27. class collection;
  28. class database;
  29. class uri;
  30. namespace events {
  31. class topology_description;
  32. }
  33. ///
  34. /// Class representing a preference for how the driver routes read operations to members of a
  35. /// replica set or to a sharded cluster.
  36. ///
  37. /// By default read operations are directed to the primary member in a replica set. Reading from the
  38. /// primary guarantees that read operations reflect the latest version of a document. However, by
  39. /// distributing some or all reads to secondary members of the replica set, you can improve read
  40. /// throughput or reduce latency for an application that does not require fully up-to-date data.
  41. ///
  42. /// Read preference can be broadly specified by setting a mode. It is also possible to
  43. /// set tags in the read preference for more granular control, and to target specific members of a
  44. /// replica set via attributes other than their current state as a primary or secondary node.
  45. /// Furthermore, it is also possible to set a staleness threshold, such that the read is limited to
  46. /// targeting secondaries whose staleness is less than or equal to the given threshold.
  47. ///
  48. /// Read preferences are ignored for direct connections to a single mongod instance. However,
  49. /// in order to perform read operations on a direct connection to a secondary member of a replica
  50. /// set, you must set a read preference that allows reading from secondaries.
  51. ///
  52. /// @see https://docs.mongodb.com/master/core/read-preference/
  53. ///
  54. class MONGOCXX_API read_preference {
  55. public:
  56. ///
  57. /// Determines which members in a replica set are acceptable to read from.
  58. ///
  59. /// @warning Read preference tags are not respected when the mode is set to primary.
  60. ///
  61. /// @warning All read preference modes except primary may return stale data because secondaries
  62. /// replicate operations from the primary with some delay. Ensure that your application
  63. /// can tolerate stale data if you choose to use a non-primary mode.
  64. ///
  65. /// @see https://docs.mongodb.com/master/core/read-preference/#read-preference-modes
  66. ///
  67. enum class read_mode : std::uint8_t {
  68. ///
  69. /// Only read from a primary node.
  70. ///
  71. k_primary,
  72. ///
  73. /// Prefer to read from a primary node.
  74. ///
  75. k_primary_preferred,
  76. ///
  77. /// Only read from secondary nodes.
  78. ///
  79. k_secondary,
  80. ///
  81. /// Prefer to read from secondary nodes.
  82. ///
  83. k_secondary_preferred,
  84. ///
  85. /// Read from the node with the lowest latency irrespective of state.
  86. ///
  87. k_nearest
  88. };
  89. ///
  90. /// Constructs a new read_preference with read_mode set to k_primary.
  91. ///
  92. read_preference();
  93. struct deprecated_tag {};
  94. ///
  95. /// Constructs a new read_preference.
  96. ///
  97. /// @param mode
  98. /// Specifies the read_mode.
  99. ///
  100. /// @deprecated The constructor with no arguments and the method mode() should be used.
  101. ///
  102. MONGOCXX_DEPRECATED read_preference(read_mode mode);
  103. read_preference(read_mode mode, deprecated_tag);
  104. ///
  105. /// Constructs a new read_preference with tags.
  106. ///
  107. /// @param mode
  108. /// A read_preference read_mode.
  109. /// @param tags
  110. /// A document representing tags to use for the read_preference.
  111. ///
  112. /// @see https://docs.mongodb.com/master/core/read-preference/#tag-sets
  113. ///
  114. /// @deprecated The tags() method should be used instead.
  115. ///
  116. MONGOCXX_DEPRECATED read_preference(read_mode mode, bsoncxx::document::view_or_value tags);
  117. read_preference(read_mode mode, bsoncxx::document::view_or_value tags, deprecated_tag);
  118. ///
  119. /// Copy constructs a read_preference.
  120. ///
  121. read_preference(const read_preference&);
  122. ///
  123. /// Copy assigns a read_preference.
  124. ///
  125. read_preference& operator=(const read_preference&);
  126. ///
  127. /// Move constructs a read_preference.
  128. ///
  129. read_preference(read_preference&&) noexcept;
  130. ///
  131. /// Move assigns a read_preference.
  132. ///
  133. read_preference& operator=(read_preference&&) noexcept;
  134. ///
  135. /// Destroys a read_preference.
  136. ///
  137. ~read_preference();
  138. ///
  139. /// Sets a new mode for this read_preference.
  140. ///
  141. /// @param mode
  142. /// The new read preference mode.
  143. ///
  144. /// @return
  145. /// A reference to the object on which this member function is being called. This facilitates
  146. /// method chaining.
  147. ///
  148. read_preference& mode(read_mode mode);
  149. ///
  150. /// Returns the current read_mode for this read_preference.
  151. ///
  152. /// @return The current read_mode.
  153. ///
  154. read_mode mode() const;
  155. ///
  156. /// Sets or updates the tags for this read_preference.
  157. ///
  158. /// @param tags
  159. /// Document representing the tags.
  160. ///
  161. /// @see https://docs.mongodb.com/master/core/read-preference/#tag-sets
  162. ///
  163. /// @return
  164. /// A reference to the object on which this member function is being called. This facilitates
  165. /// method chaining.
  166. ///
  167. read_preference& tags(bsoncxx::document::view_or_value tags);
  168. ///
  169. /// Returns the current tags for this read_preference.
  170. ///
  171. /// @return The optionally set current tags.
  172. ///
  173. /// @see https://docs.mongodb.com/master/core/read-preference/#tag-sets
  174. ///
  175. stdx::optional<bsoncxx::document::view> tags() const;
  176. ///
  177. /// Sets the max staleness setting for this read_preference. Secondary
  178. /// servers with an estimated lag greater than this value will be excluded
  179. /// from selection under modes that allow secondaries.
  180. ///
  181. /// Max staleness must be at least 90 seconds, and also at least
  182. /// the sum (in seconds) of the client's heartbeatFrequencyMS and the
  183. /// server's idle write period, which is 10 seconds. For general use,
  184. /// 90 seconds is the effective minimum. If less, an exception will be
  185. /// thrown when an operation is attempted.
  186. ///
  187. /// Max staleness may only be used with MongoDB version 3.4 or later.
  188. /// If used with an earlier version, an exception will be thrown when an
  189. /// operation is attempted.
  190. ///
  191. /// @note
  192. /// The max-staleness feature is designed to prevent badly-lagging
  193. /// servers from being selected. The staleness estimate is
  194. /// imprecise and shouldn't be used to try to select "up-to-date"
  195. /// secondaries.
  196. ///
  197. /// @param max_staleness
  198. /// The new max staleness setting. It must be positive.
  199. ///
  200. /// @return
  201. /// A reference to the object on which this member function is being called. This facilitates
  202. /// method chaining.
  203. ///
  204. /// @throws mongocxx::logic_error if the argument is invalid.
  205. ///
  206. read_preference& max_staleness(std::chrono::seconds max_staleness);
  207. ///
  208. /// Returns the current max staleness setting for this read_preference.
  209. ///
  210. /// @return The optionally current max staleness setting.
  211. ///
  212. stdx::optional<std::chrono::seconds> max_staleness() const;
  213. ///
  214. /// Sets the hedge document to be used for the read preference. Sharded clusters running MongoDB
  215. /// 4.4 or later can dispatch read operations in parallel, returning the result from the fastest
  216. /// host and cancelling the unfinished operations.
  217. ///
  218. /// This may be an empty document or a document of the form { enabled: <boolean> }.
  219. ///
  220. /// Hedged reads are automatically enabled in MongoDB 4.4+ when using a ``nearest`` read
  221. /// preference. To explicitly enable or disable hedging, the ``hedge`` document must be
  222. /// passed. An empty document uses server defaults to control hedging, but the ``enabled`` key
  223. /// may be set to ``true`` or ``false`` to explicitly enable or disable hedged reads.
  224. ///
  225. /// @param hedge
  226. /// The hedge document to set. For example, the document { enabled: true }.
  227. ///
  228. /// @return A reference to the object on which this member function is being called. This
  229. /// facilitates method chaining.
  230. ///
  231. read_preference& hedge(bsoncxx::document::view_or_value hedge);
  232. ///
  233. /// Gets the current hedge document to be used for the read preference.
  234. ///
  235. /// @return A hedge document if one was set.
  236. ///
  237. const stdx::optional<bsoncxx::document::view> hedge() const;
  238. private:
  239. friend client;
  240. friend collection;
  241. friend database;
  242. friend options::transaction;
  243. friend events::topology_description;
  244. friend uri;
  245. ///
  246. /// @{
  247. ///
  248. /// Compares two read_preference objects for (in)-equality.
  249. ///
  250. /// @relates: read_preference
  251. ///
  252. friend MONGOCXX_API bool MONGOCXX_CALL operator==(const read_preference&,
  253. const read_preference&);
  254. friend MONGOCXX_API bool MONGOCXX_CALL operator!=(const read_preference&,
  255. const read_preference&);
  256. ///
  257. /// @}
  258. ///
  259. class MONGOCXX_PRIVATE impl;
  260. MONGOCXX_PRIVATE read_preference(std::unique_ptr<impl>&& implementation);
  261. std::unique_ptr<impl> _impl;
  262. };
  263. MONGOCXX_INLINE_NAMESPACE_END
  264. } // namespace mongocxx
  265. #include <mongocxx/config/postlude.hpp>