client_session.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2017-present 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 <functional>
  16. #include <memory>
  17. #include <bsoncxx/document/view.hpp>
  18. #include <bsoncxx/stdx/optional.hpp>
  19. #include <mongocxx/options/client_session.hpp>
  20. #include <mongocxx/config/prelude.hpp>
  21. namespace mongocxx {
  22. MONGOCXX_INLINE_NAMESPACE_BEGIN
  23. class client;
  24. ///
  25. /// Use a session for a sequence of operations, optionally with causal consistency.
  26. ///
  27. /// Note that client_session is not thread-safe. See
  28. /// https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/thread-safety/ for more details.
  29. ///
  30. /// @see http://dochub.mongodb.org/core/causal-consistency
  31. ///
  32. class MONGOCXX_API client_session {
  33. public:
  34. ///
  35. /// Move constructs a session.
  36. ///
  37. client_session(client_session&&) noexcept;
  38. ///
  39. /// Move assigns a session.
  40. ///
  41. client_session& operator=(client_session&&) noexcept;
  42. client_session(const client_session&) = delete;
  43. client_session& operator=(const client_session&) = delete;
  44. ///
  45. /// Ends and destroys the session.
  46. ///
  47. ~client_session() noexcept;
  48. ///
  49. /// Gets the client that started this session.
  50. ///
  51. const class client& client() const noexcept;
  52. ///
  53. /// Gets the options this session was created with.
  54. ///
  55. const options::client_session& options() const noexcept;
  56. ///
  57. /// Get the server-side "logical session ID" associated with this session, as a BSON document.
  58. /// This view is invalid after the session is destroyed.
  59. ///
  60. bsoncxx::document::view id() const noexcept;
  61. ///
  62. /// Get the session's clusterTime, as a BSON document. This is an opaque value suitable for
  63. /// passing to advance_cluster_time(). The document is empty if the session has
  64. /// not been used for any operation and you have not called advance_cluster_time().
  65. /// This view is invalid after the session is destroyed.
  66. ///
  67. bsoncxx::document::view cluster_time() const noexcept;
  68. ///
  69. /// Get the session's operationTime, as a BSON timestamp. This is an opaque value suitable for
  70. /// passing to advance_operation_time(). The timestamp is zero if the session has not been used
  71. /// for any operation and you have not called advance_operation_time().
  72. ///
  73. bsoncxx::types::b_timestamp operation_time() const noexcept;
  74. ///
  75. /// Get the server_id the session is pinned to. The server_id is zero if the session is not
  76. /// pinned to a server.
  77. std::uint32_t server_id() const noexcept;
  78. ///
  79. /// Advance the cluster time for a session. Has an effect only if the new cluster time is
  80. /// greater than the session's current cluster time.
  81. ///
  82. /// Use advance_operation_time() and advance_cluster_time() to copy the operationTime and
  83. /// clusterTime from another session, ensuring subsequent operations in this session are
  84. /// causally consistent with the last operation in the other session.
  85. ///
  86. void advance_cluster_time(const bsoncxx::document::view& cluster_time);
  87. ///
  88. /// Advance the session's operation time, expressed as a BSON timestamp. Has an effect only if
  89. /// the new operation time is greater than the session's current operation time.
  90. ///
  91. /// Use advance_operation_time() and advance_cluster_time() to copy the operationTime and
  92. /// clusterTime from another session, ensuring subsequent operations in this session are
  93. /// causally consistent with the last operation in the other session.
  94. ///
  95. void advance_operation_time(const bsoncxx::types::b_timestamp& operation_time);
  96. ///
  97. /// Starts a transaction on the current client session.
  98. ///
  99. /// @param transaction_opts (optional)
  100. /// The options to use in the transaction.
  101. ///
  102. /// @throws mongocxx::operation_exception if the options are misconfigured, if there are network
  103. /// or other transient failures, or if there are other errors such as a session with a
  104. /// transaction already in progress.
  105. ///
  106. void start_transaction(const stdx::optional<options::transaction>& transaction_opts = {});
  107. ///
  108. /// Commits a transaction on the current client session.
  109. ///
  110. /// @throws mongocxx::operation_exception if the options are misconfigured, if there are network
  111. /// or other transient failures, or if there are other errors such as a session with no
  112. /// transaction in progress.
  113. ///
  114. void commit_transaction();
  115. ///
  116. /// Aborts a transaction on the current client session.
  117. ///
  118. /// @throws mongocxx::operation_exception if the options are misconfigured or if there are
  119. /// other errors such as a session with no transaction in progress.
  120. ///
  121. void abort_transaction();
  122. ///
  123. /// Helper to run a user-provided callback within a transaction.
  124. ///
  125. /// This method will start a new transaction on this client session,
  126. /// run the callback, then commit the transaction. If it cannot commit
  127. /// the transaction, the entire sequence may be retried, and the callback
  128. /// may be run multiple times.
  129. ///
  130. /// If the user callback calls driver methods that run operations against the
  131. /// server that can throw an operation_exception (ex: collection::insert_one),
  132. /// the user callback should allow those exceptions to propagate up the stack
  133. /// so they can be caught and processed by the with_transaction helper.
  134. ///
  135. /// @param cb
  136. /// The callback to run inside of a transaction.
  137. /// @param opts (optional)
  138. /// The options to use to run the transaction.
  139. ///
  140. /// @throws mongocxx::operation_exception if there are errors completing the
  141. /// transaction.
  142. ///
  143. using with_transaction_cb = std::function<void MONGOCXX_CALL(client_session*)>;
  144. void with_transaction(with_transaction_cb cb, options::transaction opts = {});
  145. private:
  146. friend class bulk_write;
  147. friend class client;
  148. friend class collection;
  149. friend class database;
  150. friend class index_view;
  151. class MONGOCXX_PRIVATE impl;
  152. // "class client" distinguishes from client() method above.
  153. MONGOCXX_PRIVATE client_session(const class client* client,
  154. const options::client_session& options);
  155. MONGOCXX_PRIVATE impl& _get_impl();
  156. MONGOCXX_PRIVATE const impl& _get_impl() const;
  157. std::unique_ptr<impl> _impl;
  158. };
  159. MONGOCXX_INLINE_NAMESPACE_END
  160. } // namespace mongocxx
  161. #include <mongocxx/config/postlude.hpp>
  162. ///
  163. /// @example examples/mongocxx/client_session.cpp
  164. /// Use a mongocxx::client_session for a sequence of operations with causal consistency.
  165. ///