Class: Mongo::Session
- Inherits:
-
Object
- Object
- Mongo::Session
- Extended by:
- Forwardable
- Includes:
- ClusterTime::Consumer, Loggable, Retryable
- Defined in:
- lib/mongo/session.rb,
lib/mongo/session/session_pool.rb,
lib/mongo/session/server_session.rb,
lib/mongo/session/server_session/dirtyable.rb
Overview
Session objects are not thread-safe. An application may use a session from only one thread or process at a time.
A logical session representing a set of sequential operations executed by an application that are related in some way.
Defined Under Namespace
Classes: ServerSession, SessionPool
Constant Summary collapse
- MISMATCHED_CLUSTER_ERROR_MSG =
Error message indicating that the session was retrieved from a client with a different cluster than that of the client through which it is currently being used.
'The configuration of the client used to create this session does not match that ' + 'of the client owning this operation. Please only use this session for operations through its parent ' + 'client.'
- SESSION_ENDED_ERROR_MSG =
Error message describing that the session cannot be used because it has already been ended.
'This session has ended and cannot be used. Please create a new one.'- SESSIONS_NOT_SUPPORTED =
Deprecated.
Error message describing that sessions are not supported by the server version.
'Sessions are not supported by the connected servers.'- NO_TRANSACTION_STATE =
The state of a session in which the last operation was not related to any transaction or no operations have yet occurred.
:no_transaction- STARTING_TRANSACTION_STATE =
The state of a session in which a user has initiated a transaction but no operations within the transactions have occurred yet.
:starting_transaction- TRANSACTION_IN_PROGRESS_STATE =
The state of a session in which a transaction has been started and at least one operation has occurred, but the transaction has not yet been committed or aborted.
:transaction_in_progress- TRANSACTION_COMMITTED_STATE =
The state of a session in which the last operation executed was a transaction commit.
:transaction_committed- TRANSACTION_ABORTED_STATE =
The state of a session in which the last operation executed was a transaction abort.
:transaction_aborted- UNLABELED_WRITE_CONCERN_CODES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[ 79, # UnknownReplWriteConcern 100, # CannotSatisfyWriteConcern, ].freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
The client through which this session was created.
- #cluster ⇒ Object readonly
-
#operation_time ⇒ BSON::Timestamp
readonly
The latest seen operation time for this session.
-
#options ⇒ Hash
readonly
The options for this session.
-
#pinned_connection_global_id ⇒ Integer | nil
readonly
private
The connection global id that this session is pinned to, if any.
-
#pinned_server ⇒ Server | nil
readonly
private
The server (which should be a mongos) that this session is pinned to, if any.
-
#recovery_token ⇒ BSON::Document | nil
private
Recovery token for the sharded transaction being executed on this session, if any.
-
#snapshot_timestamp ⇒ BSON::Timestamp | nil
The snapshot time for this session.
-
#with_transaction_deadline ⇒ Integer | nil
readonly
private
The deadline for the current transaction, if any.
Attributes included from ClusterTime::Consumer
Instance Method Summary collapse
-
#abort_transaction(options = nil) ⇒ Object
Abort the currently active transaction without making any changes to the database.
-
#aborting_transaction? ⇒ true | false
private
Whether the session is currently aborting a transaction.
-
#add_autocommit!(command) ⇒ Hash, BSON::Document
private
Add the autocommit field to a command document if applicable.
-
#add_start_transaction!(command) ⇒ Hash, BSON::Document
private
Add the startTransaction field to a command document if applicable.
-
#add_txn_num!(command) ⇒ Hash, BSON::Document
private
Add the transaction number to a command document if applicable.
-
#add_txn_opts!(command, _read, context) ⇒ Hash, BSON::Document
private
Add the transactions options if applicable.
-
#advance_operation_time(new_operation_time) ⇒ BSON::Timestamp
Advance the cached operation time for this session.
-
#causal_consistency_doc ⇒ Object
private
Returns causal consistency document if the last operation time is known and causal consistency is enabled, otherwise returns nil.
-
#commit_transaction(options = nil) ⇒ Object
Commit the currently active transaction on the session.
-
#committing_transaction? ⇒ true | false
private
Whether the session is currently committing a transaction.
-
#dirty!(mark = true) ⇒ Object
Sets the dirty state to the given value for the underlying server session.
-
#dirty? ⇒ true | false | nil
private
Whether the underlying server session is dirty.
-
#end_session ⇒ nil
End this session.
-
#ended? ⇒ true, false
Whether this session has ended.
-
#explicit? ⇒ true, false
Is this session an explicit one (i.e. user-created).
-
#implicit? ⇒ true, false
Is this session an implicit one (not user-created).
-
#in_transaction? ⇒ true | false
Whether or not the session is currently in a transaction.
-
#initialize(server_session, client, options = {}) ⇒ Session
constructor
private
Initialize a Session.
-
#inside_with_transaction? ⇒ Boolean
private
Whether we are currently inside a with_transaction block.
-
#inspect ⇒ String
Get a formatted string for use in inspection.
-
#materialize_if_needed ⇒ Session
private
If not already set, populate a session objects's server_session by checking out a session from the session pool.
- #materialized? ⇒ Boolean private
-
#next_txn_num ⇒ Integer
private
Increment and return the next transaction number.
-
#pin_to_connection(connection_global_id, connection: nil) ⇒ Object
private
Pins this session to the specified connection.
-
#pin_to_server(server) ⇒ Object
private
Pins this session to the specified server, which should be a mongos.
-
#process(result) ⇒ Operation::Result
private
Process a response from the server that used this session.
-
#retry_reads? ⇒ Boolean
private
Whether reads executed with this session can be retried according to the modern retryable reads specification.
-
#retry_writes? ⇒ true, false
Will writes executed with this session be retried.
-
#revert_to_starting_transaction! ⇒ Object
private
Reverts the session state to STARTING_TRANSACTION_STATE.
-
#session_id ⇒ BSON::Document
Get the server session id of this session, if the session has not been ended.
-
#snapshot? ⇒ true | false
Whether the session is configured for snapshot reads.
-
#start_transaction(options = nil) ⇒ Object
Places subsequent operations in this session into a new transaction.
- #starting_transaction? ⇒ Boolean private
-
#suppress_read_write_concern!(command) ⇒ Hash, BSON::Document
private
Remove the read concern and/or write concern from the command if not applicable.
-
#txn_num ⇒ Integer
Get the current transaction number.
-
#txn_options ⇒ Hash
on this session.
-
#txn_read_concern ⇒ Hash
private
Get the read concern the session will use when starting a transaction.
-
#txn_read_preference ⇒ Hash
Get the read preference the session will use in the currently active transaction.
-
#unpin(connection = nil) ⇒ Object
private
Unpins this session from the pinned server or connection, if the session was pinned.
-
#unpin_maybe(error, connection = nil) ⇒ Object
private
Unpins this session from the pinned server or connection, if the session was pinned and the specified exception instance and the session's transaction state require it to be unpinned.
-
#update_state! ⇒ Object
private
Update the state of the session due to a (non-commit and non-abort) operation being run.
-
#validate!(client) ⇒ Session
private
Validate the session for use by the specified client.
-
#validate_read_preference!(command) ⇒ Object
private
Ensure that the read preference of a command is primary.
-
#with_transaction(options = nil) ⇒ Object
Executes the provided block in a transaction, retrying as necessary.
Methods included from ClusterTime::Consumer
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Methods included from Retryable
#read_worker, #select_server, #with_overload_retry, #write_worker
Constructor Details
#initialize(server_session, client, options = {}) ⇒ Session
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Applications should use Client#start_session to begin a session. This constructor is for internal driver use only.
Initialize a Session.
A session can be explicit or implicit. Lifetime of explicit sessions is managed by the application - applications explicitly create such sessions and explicitly end them. Implicit sessions are created automatically by the driver, and their lifetime is managed by the driver.
When an implicit session is created, it cannot have a server session associated with it. The server session will be checked out of the session pool when an operation using this session is actually executed. When an explicit session is created, it must reference a server session that is already allocated.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mongo/session.rb', line 82 def initialize(server_session, client, = {}) if [:causal_consistency] && [:snapshot] raise ArgumentError, ':causal_consistency and :snapshot options cannot be both set on a session' end if [:snapshot_time] && ![:snapshot] raise ArgumentError, ':snapshot_time can only be set when :snapshot is true' end if [:snapshot_time] && ![:snapshot_time].is_a?(BSON::Timestamp) raise ArgumentError, ':snapshot_time must be a BSON::Timestamp' end if [:implicit] unless server_session.nil? raise ArgumentError, 'Implicit session cannot reference server session during construction' end elsif server_session.nil? raise ArgumentError, 'Explicit session must reference server session during construction' end @server_session = server_session = .dup # Implicit sessions only need the cluster and client options (never run # transactions), so avoid creating a Mongo::Client clone to prevent # memory leaks: use the original client directly instead. @client = [:implicit] ? client : client.use(:admin) @cluster = @client.cluster @options = .dup.freeze @cluster_time = nil @state = NO_TRANSACTION_STATE @with_transaction_deadline = nil @with_transaction_timeout_ms = nil @inside_with_transaction = false @snapshot_timestamp = [:snapshot_time] end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns The client through which this session was created.
128 129 130 |
# File 'lib/mongo/session.rb', line 128 def client @client end |
#cluster ⇒ Object (readonly)
130 131 132 |
# File 'lib/mongo/session.rb', line 130 def cluster @cluster end |
#operation_time ⇒ BSON::Timestamp (readonly)
Returns The latest seen operation time for this session.
141 142 143 |
# File 'lib/mongo/session.rb', line 141 def operation_time @operation_time end |
#options ⇒ Hash (readonly)
Returns The options for this session.
123 124 125 |
# File 'lib/mongo/session.rb', line 123 def @options end |
#pinned_connection_global_id ⇒ Integer | nil (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The connection global id that this session is pinned to, if any.
292 293 294 |
# File 'lib/mongo/session.rb', line 292 def pinned_connection_global_id @pinned_connection_global_id end |
#pinned_server ⇒ Server | nil (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The server (which should be a mongos) that this session is pinned to, if any.
286 287 288 |
# File 'lib/mongo/session.rb', line 286 def pinned_server @pinned_server end |
#recovery_token ⇒ BSON::Document | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Recovery token for the sharded transaction being executed on this session, if any.
298 299 300 |
# File 'lib/mongo/session.rb', line 298 def recovery_token @recovery_token end |
#snapshot_timestamp ⇒ BSON::Timestamp | nil
Returns The snapshot time for this session. nil if the session is not a snapshot session, or if it is a snapshot session for which no :snapshot_time option was provided and no read has yet captured atClusterTime from the server.
1287 1288 1289 |
# File 'lib/mongo/session.rb', line 1287 def @snapshot_timestamp end |
#with_transaction_deadline ⇒ Integer | nil (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The deadline for the current transaction, if any.
1303 1304 1305 |
# File 'lib/mongo/session.rb', line 1303 def with_transaction_deadline @with_transaction_deadline end |
Instance Method Details
#abort_transaction(options = nil) ⇒ Object
Abort the currently active transaction without making any changes to the database.
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 |
# File 'lib/mongo/session.rb', line 810 def abort_transaction( = nil) QueryCache.clear check_if_ended! check_if_no_transaction! if within_states?(TRANSACTION_COMMITTED_STATE) raise Mongo::Error::InvalidTransactionOperation.new( Mongo::Error::InvalidTransactionOperation.cannot_call_after_msg( :commitTransaction, :abortTransaction ) ) end if within_states?(TRANSACTION_ABORTED_STATE) raise Mongo::Error::InvalidTransactionOperation.new( Mongo::Error::InvalidTransactionOperation.cannot_call_twice_msg(:abortTransaction) ) end ||= {} begin unless starting_transaction? @aborting_transaction = true context = Operation::Context.new( client: @client, session: self, operation_timeouts: operation_timeouts() ) write_with_retry([:write_concern], ending_transaction: true, context: context) do |connection, txn_num, context| operation = Operation::Command.new( selector: { abortTransaction: 1 }, db_name: 'admin', session: self, txn_num: txn_num ) tracer.trace_operation(operation, context, op_name: 'abortTransaction') do operation.execute_with_connection(connection, context: context) end ensure unpin end end # Finish the transaction span before changing state tracer.finish_transaction_span(self) @state = TRANSACTION_ABORTED_STATE rescue Mongo::Error::InvalidTransactionOperation raise rescue Mongo::Error tracer.finish_transaction_span(self) @state = TRANSACTION_ABORTED_STATE rescue Exception tracer.finish_transaction_span(self) @state = TRANSACTION_ABORTED_STATE raise ensure @aborting_transaction = false end # No official return value, but return true so that in interactive # use the method hints that it succeeded. true end |
#aborting_transaction? ⇒ true | false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Whether the session is currently aborting a transaction.
906 907 908 |
# File 'lib/mongo/session.rb', line 906 def aborting_transaction? !!@aborting_transaction end |
#add_autocommit!(command) ⇒ Hash, BSON::Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add the autocommit field to a command document if applicable.
999 1000 1001 1002 1003 |
# File 'lib/mongo/session.rb', line 999 def add_autocommit!(command) command.tap do |c| c[:autocommit] = false if in_transaction? end end |
#add_start_transaction!(command) ⇒ Hash, BSON::Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add the startTransaction field to a command document if applicable.
1014 1015 1016 1017 1018 |
# File 'lib/mongo/session.rb', line 1014 def add_start_transaction!(command) command.tap do |c| c[:startTransaction] = true if starting_transaction? end end |
#add_txn_num!(command) ⇒ Hash, BSON::Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add the transaction number to a command document if applicable.
1029 1030 1031 1032 1033 |
# File 'lib/mongo/session.rb', line 1029 def add_txn_num!(command) command.tap do |c| c[:txnNumber] = BSON::Int64.new(@server_session.txn_num) if in_transaction? end end |
#add_txn_opts!(command, _read, context) ⇒ Hash, BSON::Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Add the transactions options if applicable.
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 |
# File 'lib/mongo/session.rb', line 1044 def add_txn_opts!(command, _read, context) command.tap do |c| # The read concern should be added to any command that starts a transaction. if starting_transaction? # https://jira.mongodb.org/browse/SPEC-1161: transaction's # read concern overrides collection/database/client read concerns, # even if transaction's read concern is not set. # Read concern here is the one sent to the server and may # include afterClusterTime. if (rc = c[:readConcern]) rc = rc.dup rc.delete(:level) end if txn_read_concern if rc rc.update(txn_read_concern) else rc = txn_read_concern.dup end end if rc.nil? || rc.empty? c.delete(:readConcern) else c[:readConcern] = Options::Mapper.transform_values_to_strings(rc) end end # We need to send the read concern level as a string rather than a symbol. c[:readConcern] = Options::Mapper.transform_values_to_strings(c[:readConcern]) if c[:readConcern] if c[:commitTransaction] && (max_time_ms = [:max_commit_time_ms]) c[:maxTimeMS] = max_time_ms end # The write concern should be added to any abortTransaction or commitTransaction command. if c[:abortTransaction] || c[:commitTransaction] if @already_committed wc = BSON::Document.new(c[:writeConcern] || txn_write_concern || {}) wc.merge!(w: :majority) wc[:wtimeout] ||= 10_000 c[:writeConcern] = wc elsif txn_write_concern c[:writeConcern] ||= txn_write_concern end end # A non-numeric write concern w value needs to be sent as a string rather than a symbol. if c[:writeConcern] && c[:writeConcern][:w] && c[:writeConcern][:w].is_a?(Symbol) c[:writeConcern][:w] = c[:writeConcern][:w].to_s end # Ignore wtimeout if csot c[:writeConcern]&.delete(:wtimeout) if context&.csot? # We must not send an empty (server default) write concern. c.delete(:writeConcern) if c[:writeConcern] && c[:writeConcern].empty? end end |
#advance_operation_time(new_operation_time) ⇒ BSON::Timestamp
Advance the cached operation time for this session.
1223 1224 1225 1226 1227 1228 1229 |
# File 'lib/mongo/session.rb', line 1223 def advance_operation_time(new_operation_time) @operation_time = if @operation_time [ @operation_time, new_operation_time ].max else new_operation_time end end |
#causal_consistency_doc ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns causal consistency document if the last operation time is known and causal consistency is enabled, otherwise returns nil.
1331 1332 1333 1334 1335 |
# File 'lib/mongo/session.rb', line 1331 def causal_consistency_doc return unless operation_time && causal_consistency? { afterClusterTime: operation_time } end |
#commit_transaction(options = nil) ⇒ Object
Commit the currently active transaction on the session.
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
# File 'lib/mongo/session.rb', line 724 def commit_transaction( = nil) QueryCache.clear check_if_ended! check_if_no_transaction! if within_states?(TRANSACTION_ABORTED_STATE) raise Mongo::Error::InvalidTransactionOperation.new( Mongo::Error::InvalidTransactionOperation.cannot_call_after_msg( :abortTransaction, :commitTransaction ) ) end ||= {} begin # If commitTransaction is called twice, we need to run the same commit # operation again, so we revert the session to the previous state. if within_states?(TRANSACTION_COMMITTED_STATE) @state = @last_commit_skipped ? STARTING_TRANSACTION_STATE : TRANSACTION_IN_PROGRESS_STATE @already_committed = true end if starting_transaction? @last_commit_skipped = true else @last_commit_skipped = false @committing_transaction = true write_concern = [:write_concern] || [:write_concern] write_concern = WriteConcern.get(write_concern) if write_concern && !write_concern.is_a?(WriteConcern::Base) context = Operation::Context.new( client: @client, session: self, operation_timeouts: operation_timeouts() ) write_with_retry(write_concern, ending_transaction: true, context: context) do |connection, txn_num, context| if context.retry? && !context.overload_only_retry? if write_concern wco = write_concern..merge(w: :majority) wco[:wtimeout] ||= 10_000 write_concern = WriteConcern.get(wco) else write_concern = WriteConcern.get(w: :majority, wtimeout: 10_000) end end spec = { selector: { commitTransaction: 1 }, db_name: 'admin', session: self, txn_num: txn_num, write_concern: write_concern, } operation = Operation::Command.new(spec) tracer.trace_operation(operation, context, op_name: 'commitTransaction') do operation.execute_with_connection(connection, context: context) end end end # Finish the transaction span before changing state tracer.finish_transaction_span(self) ensure @state = TRANSACTION_COMMITTED_STATE @committing_transaction = false end # No official return value, but return true so that in interactive # use the method hints that it succeeded. true end |
#committing_transaction? ⇒ true | false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Whether the session is currently committing a transaction.
898 899 900 |
# File 'lib/mongo/session.rb', line 898 def committing_transaction? !!@committing_transaction end |
#dirty!(mark = true) ⇒ Object
Sets the dirty state to the given value for the underlying server session. If there is no server session, this does nothing.
150 151 152 |
# File 'lib/mongo/session.rb', line 150 def dirty!(mark = true) @server_session&.dirty!(mark) end |
#dirty? ⇒ true | false | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the underlying server session is dirty. If no server session exists for this session, returns nil.
158 159 160 |
# File 'lib/mongo/session.rb', line 158 def dirty? @server_session&.dirty? end |
#end_session ⇒ nil
End this session.
If there is an in-progress transaction on this session, the transaction is aborted. The server session associated with this session is returned to the server session pool. Finally, this session is marked ended and is no longer usable.
If this session is already ended, this method does nothing.
Note that this method does not directly issue an endSessions command to this server, contrary to what its name might suggest.
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/mongo/session.rb', line 385 def end_session if !ended? && @client if within_states?(TRANSACTION_IN_PROGRESS_STATE) begin abort_transaction rescue Mongo::Error, Error::AuthError end end # Release any pinned connection (e.g. after a committed transaction # in load-balanced mode). unpin if pinned_connection_global_id cluster.session_pool.checkin(@server_session) if @server_session end ensure @server_session = nil @ended = true @client = nil end |
#ended? ⇒ true, false
Whether this session has ended.
255 256 257 |
# File 'lib/mongo/session.rb', line 255 def ended? !!@ended end |
#explicit? ⇒ true, false
Is this session an explicit one (i.e. user-created).
190 191 192 |
# File 'lib/mongo/session.rb', line 190 def explicit? !implicit? end |
#implicit? ⇒ true, false
Is this session an implicit one (not user-created).
178 179 180 |
# File 'lib/mongo/session.rb', line 178 def implicit? @implicit ||= !!(@options.key?(:implicit) && @options[:implicit] == true) end |
#in_transaction? ⇒ true | false
Whether or not the session is currently in a transaction.
890 891 892 |
# File 'lib/mongo/session.rb', line 890 def in_transaction? within_states?(STARTING_TRANSACTION_STATE, TRANSACTION_IN_PROGRESS_STATE) end |
#inside_with_transaction? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Whether we are currently inside a with_transaction block.
1307 1308 1309 |
# File 'lib/mongo/session.rb', line 1307 def inside_with_transaction? @inside_with_transaction end |
#inspect ⇒ String
Get a formatted string for use in inspection.
363 364 365 |
# File 'lib/mongo/session.rb', line 363 def inspect "#<Mongo::Session:0x#{object_id} session_id=#{session_id} options=#{@options}>" end |
#materialize_if_needed ⇒ Session
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If not already set, populate a session objects's server_session by checking out a session from the session pool.
1237 1238 1239 1240 1241 1242 1243 1244 1245 |
# File 'lib/mongo/session.rb', line 1237 def materialize_if_needed raise Error::SessionEnded if ended? return unless implicit? && !@server_session @server_session = cluster.session_pool.checkout self end |
#materialized? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
1248 1249 1250 1251 1252 |
# File 'lib/mongo/session.rb', line 1248 def materialized? raise Error::SessionEnded if ended? !@server_session.nil? end |
#next_txn_num ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Increment and return the next transaction number.
1263 1264 1265 1266 1267 |
# File 'lib/mongo/session.rb', line 1263 def next_txn_num raise Error::SessionEnded if ended? @server_session.next_txn_num end |
#pin_to_connection(connection_global_id, connection: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pins this session to the specified connection.
this session to.
932 933 934 935 936 937 |
# File 'lib/mongo/session.rb', line 932 def pin_to_connection(connection_global_id, connection: nil) raise ArgumentError, 'Cannot pin to a nil connection id' if connection_global_id.nil? @pinned_connection_global_id = connection_global_id @pinned_connection = connection end |
#pin_to_server(server) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Pins this session to the specified server, which should be a mongos.
915 916 917 918 919 920 921 922 923 |
# File 'lib/mongo/session.rb', line 915 def pin_to_server(server) raise ArgumentError, 'Cannot pin to a nil server' if server.nil? if Lint.enabled? && !server.mongos? raise Error::LintError, "Attempted to pin the session to server #{server.summary} which is not a mongos" end @pinned_server = server end |
#process(result) ⇒ Operation::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Process a response from the server that used this session.
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'lib/mongo/session.rb', line 1197 def process(result) unless implicit? set_operation_time(result) if (cluster_time_doc = result.cluster_time) advance_cluster_time(cluster_time_doc) end end @server_session.set_last_use! if (doc = result.reply && result.reply.documents.first) && doc[:recoveryToken] self.recovery_token = doc[:recoveryToken] end result end |
#retry_reads? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether reads executed with this session can be retried according to the modern retryable reads specification.
If this method returns true, the modern retryable reads have been requested by the application. If the server selected for a read operation supports modern retryable reads, they will be used for that particular operation. If the server selected for a read operation does not support modern retryable reads, the read will not be retried.
If this method returns false, legacy retryable reads have been requested by the application. Legacy retryable read logic will be used regardless of server version of the server(s) that the client is connected to. The number of read retries is given by :max_read_retries client option, which is 1 by default and can be set to 0 to disable legacy read retries.
210 211 212 |
# File 'lib/mongo/session.rb', line 210 def retry_reads? client.[:retry_reads] != false end |
#retry_writes? ⇒ true, false
Retryable writes are only available with sharded clusters, replica sets, or load-balanced topologies.
Will writes executed with this session be retried.
225 226 227 |
# File 'lib/mongo/session.rb', line 225 def retry_writes? !!client.[:retry_writes] && (cluster.replica_set? || cluster.sharded? || cluster.load_balanced?) end |
#revert_to_starting_transaction! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reverts the session state to STARTING_TRANSACTION_STATE. Called before retrying the first command in a transaction so that startTransaction: true is preserved on the retry.
1148 1149 1150 1151 1152 |
# File 'lib/mongo/session.rb', line 1148 def revert_to_starting_transaction! return unless within_states?(TRANSACTION_IN_PROGRESS_STATE) @state = STARTING_TRANSACTION_STATE end |
#session_id ⇒ BSON::Document
Get the server session id of this session, if the session has not been ended. If the session had been ended, raises Error::SessionEnded.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/mongo/session.rb', line 267 def session_id raise Error::SessionEnded if ended? # An explicit session will always have a session_id, because during # construction a server session must be provided. An implicit session # will not have a session_id until materialized, thus calls to # session_id might fail. An application should not have an opportunity # to experience this failure because an implicit session shouldn't be # accessible to applications due to its lifetime being constrained to # operation execution, which is done entirely by the driver. raise Error::SessionNotMaterialized unless materialized? @server_session.session_id end |
#snapshot? ⇒ true | false
Returns Whether the session is configured for snapshot reads.
134 135 136 |
# File 'lib/mongo/session.rb', line 134 def snapshot? !![:snapshot] end |
#start_transaction(options = nil) ⇒ Object
Places subsequent operations in this session into a new transaction.
Note that the transaction will not be started on the server until an operation is performed after start_transaction is called.
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'lib/mongo/session.rb', line 660 def start_transaction( = nil) check_transactions_supported! if Lint.validate_read_concern_option([:read_concern]) # # It would be handy to detect invalid read preferences here, but # # some of the spec tests require later detection of invalid read prefs. # # Maybe we can do this when lint mode is on. # mode = options[:read] && options[:read][:mode].to_s # if mode && mode != 'primary' # raise Mongo::Error::InvalidTransactionOperation.new( # "read preference in a transaction must be primary (requested: #{mode})" # ) # end end raise Mongo::Error::SnapshotSessionTransactionProhibited if snapshot? check_if_ended! if within_states?(STARTING_TRANSACTION_STATE, TRANSACTION_IN_PROGRESS_STATE) raise Mongo::Error::InvalidTransactionOperation.new( Mongo::Error::InvalidTransactionOperation::TRANSACTION_ALREADY_IN_PROGRESS ) end unpin next_txn_num @txn_options = (@options[:default_transaction_options] || {}).merge( || {}) if txn_write_concern && !WriteConcern.get(txn_write_concern).acknowledged? raise Mongo::Error::InvalidTransactionOperation.new( Mongo::Error::InvalidTransactionOperation::UNACKNOWLEDGED_WRITE_CONCERN ) end @state = STARTING_TRANSACTION_STATE @already_committed = false tracer.start_transaction_span(self) # This method has no explicit return value. # We could return nil here but true indicates to the user that the # operation succeeded. This is intended for interactive use. # Note that the return value is not documented. true end |
#starting_transaction? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
878 879 880 |
# File 'lib/mongo/session.rb', line 878 def starting_transaction? within_states?(STARTING_TRANSACTION_STATE) end |
#suppress_read_write_concern!(command) ⇒ Hash, BSON::Document
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove the read concern and/or write concern from the command if not applicable.
1112 1113 1114 1115 1116 1117 1118 1119 |
# File 'lib/mongo/session.rb', line 1112 def suppress_read_write_concern!(command) command.tap do |c| next unless in_transaction? c.delete(:readConcern) unless starting_transaction? c.delete(:writeConcern) unless c[:commitTransaction] || c[:abortTransaction] end end |
#txn_num ⇒ Integer
Get the current transaction number.
1277 1278 1279 1280 1281 |
# File 'lib/mongo/session.rb', line 1277 def txn_num raise Error::SessionEnded if ended? @server_session.txn_num end |
#txn_options ⇒ Hash
on this session.
166 167 168 |
# File 'lib/mongo/session.rb', line 166 def @txn_options or raise ArgumentError, 'There is no active transaction' end |
#txn_read_concern ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the read concern the session will use when starting a transaction.
This is a driver style hash with underscore keys.
1322 1323 1324 1325 |
# File 'lib/mongo/session.rb', line 1322 def txn_read_concern # Read concern is inherited from client but not db or collection. [:read_concern] || @client.read_concern end |
#txn_read_preference ⇒ Hash
Get the read preference the session will use in the currently active transaction.
This is a driver style hash with underscore keys.
240 241 242 243 244 245 |
# File 'lib/mongo/session.rb', line 240 def txn_read_preference rp = [:read] || @client.read_preference Mongo::Lint.validate_underscore_read_preference(rp) rp end |
#unpin(connection = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Unpins this session from the pinned server or connection, if the session was pinned.
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'lib/mongo/session.rb', line 945 def unpin(connection = nil) # Idempotent: if there is no pinned state to clear, do nothing. Nested # unpin_maybe handlers (e.g. in BulkWrite#execute_operation wrapping an # OpMsg execution that already calls unpin_maybe in its own do_execute) # can call this method twice for the same error; checking the connection # back into the pool a second time would raise from the pool. return if @pinned_server.nil? && @pinned_connection.nil? && @pinned_connection_global_id.nil? @pinned_server = nil @pinned_connection_global_id = nil conn = connection || @pinned_connection if conn conn.unpin(:transaction) # Only check the connection back into the pool if nothing else # still holds a pin on it (e.g. an open cursor). unless conn.pinned? conn.connection_pool.check_in(conn) end end @pinned_connection = nil end |
#unpin_maybe(error, connection = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Unpins this session from the pinned server or connection, if the session was pinned and the specified exception instance and the session's transaction state require it to be unpinned.
The exception instance should already have all of the labels set on it (both client- and server-side generated ones).
978 979 980 981 982 983 984 985 986 987 988 |
# File 'lib/mongo/session.rb', line 978 def unpin_maybe(error, connection = nil) if !within_states?(Session::NO_TRANSACTION_STATE) && error.label?('TransientTransactionError') unpin(connection) end if committing_transaction? && error.label?('UnknownTransactionCommitResult') unpin(connection) end end |
#update_state! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Update the state of the session due to a (non-commit and non-abort) operation being run.
1158 1159 1160 1161 1162 1163 1164 1165 |
# File 'lib/mongo/session.rb', line 1158 def update_state! case @state when STARTING_TRANSACTION_STATE @state = TRANSACTION_IN_PROGRESS_STATE when TRANSACTION_COMMITTED_STATE, TRANSACTION_ABORTED_STATE @state = NO_TRANSACTION_STATE end end |
#validate!(client) ⇒ Session
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Validate the session for use by the specified client.
The session must not be ended and must have been created by a client with the same cluster as the client that the session is to be used with.
1180 1181 1182 1183 1184 |
# File 'lib/mongo/session.rb', line 1180 def validate!(client) check_if_ended! check_matching_cluster!(client) self end |
#validate_read_preference!(command) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Ensure that the read preference of a command is primary.
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 |
# File 'lib/mongo/session.rb', line 1131 def validate_read_preference!(command) return unless in_transaction? return unless command['$readPreference'] mode = command['$readPreference']['mode'] || command['$readPreference'][:mode] return unless mode && mode != 'primary' raise Mongo::Error::InvalidTransactionOperation.new( "read preference in a transaction must be primary (requested: #{mode})" ) end |
#with_transaction(options = nil) ⇒ Object
with_transaction contains a loop, therefore the if with_transaction itself is placed in a loop, its block should not call next or break to control the outer loop because this will instead affect the loop in with_transaction. The driver will warn and abort the transaction if it detects this situation.
Executes the provided block in a transaction, retrying as necessary.
Returns the return value of the block.
Exact number of retries and when they are performed are implementation details of the driver; the provided block should be idempotent, and should be prepared to be called more than once. The driver may retry the commit command within an active transaction or it may repeat the transaction and invoke the block again, depending on the error encountered if any. Note also that the retries may be executed against different servers.
Transactions cannot be nested - InvalidTransactionOperation will be raised if this method is called when the session already has an active transaction.
Exceptions raised by the block which are not derived from Mongo::Error stop processing, abort the transaction and are propagated out of with_transaction. Exceptions derived from Mongo::Error may be handled by with_transaction, resulting in retries of the process.
Currently, with_transaction will retry commits and block invocations until at least 120 seconds have passed since with_transaction started executing. This timeout is not configurable and may change in a future driver version.
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/mongo/session.rb', line 458 def with_transaction( = nil) @inside_with_transaction = true @with_transaction_timeout_ms = &.dig(:timeout_ms) || @options[:default_timeout_ms] || @client.timeout_ms @with_transaction_deadline = calculate_with_transaction_deadline() # When CSOT is enabled we have a customer defined deadline; otherwise # fall back to the default deadline, 120 seconds. deadline = @with_transaction_deadline || (Utils.monotonic_time + 120) transaction_in_progress = false transaction_attempt = 0 last_error = nil overload_error_count = 0 overload_encountered = false loop do if transaction_attempt > 0 if overload_encountered delay = @client.retry_policy.backoff_delay(overload_error_count) if backoff_would_exceed_deadline?(deadline, delay) make_timeout_error_from(last_error, 'CSOT timeout expired waiting to retry withTransaction') end raise(last_error) unless @client.retry_policy.should_retry_overload?(overload_error_count, delay) sleep(delay) else backoff = backoff_seconds_for_retry(transaction_attempt) if backoff_would_exceed_deadline?(deadline, backoff) make_timeout_error_from(last_error, 'CSOT timeout expired waiting to retry withTransaction') end sleep(backoff) end end = {} [:write_concern] = [:write_concern] if start_transaction() transaction_in_progress = true transaction_attempt += 1 begin rv = yield self rescue Exception => e if within_states?(STARTING_TRANSACTION_STATE, TRANSACTION_IN_PROGRESS_STATE) log_warn("Aborting transaction due to #{e.class}: #{e}") # CSOT: if the deadline is already expired, clear it so that # abort_transaction uses a fresh timeout (not the expired deadline). # If the deadline is not yet expired, keep it so abort uses remaining time. @with_transaction_deadline = nil if @with_transaction_deadline && deadline_expired?(deadline) abort_transaction transaction_in_progress = false end if deadline_expired?(deadline) transaction_in_progress = false make_timeout_error_from(e, 'CSOT timeout expired during withTransaction callback') end if e.is_a?(Mongo::Error) && e.label?('TransientTransactionError') last_error = e if e.label?('SystemOverloadedError') overload_encountered = true overload_error_count += 1 elsif overload_encountered overload_error_count += 1 end next end raise else if within_states?(TRANSACTION_ABORTED_STATE, NO_TRANSACTION_STATE, TRANSACTION_COMMITTED_STATE) transaction_in_progress = false return rv end # CSOT: if the timeout has expired before we can commit, abort the # transaction instead and raise a client-side timeout error. if @with_transaction_deadline && deadline_expired?(deadline) transaction_in_progress = false @with_transaction_deadline = nil abort_transaction raise Mongo::Error::TimeoutError, 'CSOT timeout expired before transaction could be committed' end begin commit_transaction() transaction_in_progress = false return rv rescue Mongo::Error => e if e.label?('UnknownTransactionCommitResult') if deadline_expired?(deadline) || (e.is_a?(Error::OperationFailure::Family) && e.max_time_ms_expired?) transaction_in_progress = false raise unless @with_transaction_timeout_ms && deadline_expired?(deadline) make_timeout_error_from(e, 'CSOT timeout expired during withTransaction commit') end if e.label?('SystemOverloadedError') overload_encountered = true overload_error_count += 1 elsif overload_encountered overload_error_count += 1 end if overload_encountered delay = @client.retry_policy.backoff_delay(overload_error_count) if backoff_would_exceed_deadline?(deadline, delay) transaction_in_progress = false make_timeout_error_from(e, 'CSOT timeout expired during withTransaction commit') end unless @client.retry_policy.should_retry_overload?(overload_error_count, delay) transaction_in_progress = false raise end sleep(delay) end = case v = [:write_concern] when WriteConcern::Base v. when nil {} else v end [:write_concern] = .merge(w: :majority) retry elsif e.label?('TransientTransactionError') if Utils.monotonic_time >= deadline transaction_in_progress = false make_timeout_error_from(e, 'CSOT timeout expired during withTransaction commit') end last_error = e if e.label?('SystemOverloadedError') overload_encountered = true overload_error_count += 1 elsif overload_encountered overload_error_count += 1 end @state = NO_TRANSACTION_STATE next else transaction_in_progress = false raise end rescue Error::AuthError transaction_in_progress = false raise end end end # No official return value, but return true so that in interactive # use the method hints that it succeeded. true ensure if transaction_in_progress log_warn('with_transaction callback broke out of with_transaction loop, aborting transaction') begin abort_transaction rescue Error::OperationFailure::Family, Error::InvalidTransactionOperation end end @with_transaction_deadline = nil @with_transaction_timeout_ms = nil @inside_with_transaction = false end |