Skip to content

Commit edbb852

Browse files
committed
CSOT
1 parent a388439 commit edbb852

File tree

11 files changed

+474
-26
lines changed

11 files changed

+474
-26
lines changed

source/connect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ Connect to MongoDB
2525
Create a MongoClient </connect/mongoclient>
2626
Choose a Connection Target </connect/connection-targets>
2727
Connection Options </connect/connection-options>
28+
Limit Execution Time </connect/csot>
2829
Enable TLS </connect/tls>
2930
Stable API </connect/stable-api>
3031

3132
.. /connect/network-compression
3233
.. /connect/server-selection
33-
.. /connect/csot
3434

3535
Overview
3636
--------

source/connect/connection-options.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ Timeouts
149149

150150
| **Connection URI**: ``timeoutMs=10000``
151151

152-
* - **socketTimeoutMS**
153-
- | The time in milliseconds to attempt a send or receive on a
154-
connection before the attempt times out.
152+
* - **socketTimeoutMS** *(deprecated)*
153+
- | This option is deprecated. You can configure this timeout by
154+
| setting the the :ref:`client-level timeout <kotlin-sync-csot>`
155+
| instead.
156+
|
157+
| Milliseconds to attempt a send or receive on a connection
158+
| before the attempt times out.
155159
|
156160
| **Data Type**: integer
157161
| **Default**: no timeout

source/connect/csot.txt

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
.. _kotlin-sync-csot:
2+
3+
===========================
4+
Limit Server Execution Time
5+
===========================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: error, blocking, thread, task, code example
19+
20+
Overview
21+
--------
22+
23+
When you use the {+driver-short+} to perform a server operation, you can also
24+
limit the amount of time in which the server can finish the operation. To do so,
25+
specify a **client-side operation timeout (CSOT)**. The timeout applies to all
26+
steps needed to complete the operation, including server selection, connection
27+
checkout, and server-side execution. When the timeout expires, the
28+
{+driver-short+} raises a timeout exception.
29+
30+
.. note:: Experimental Feature
31+
32+
The CSOT feature is experimental and might change in future driver
33+
releases.
34+
35+
timeoutMS Option
36+
----------------
37+
38+
To specify a timeout when connecting to a MongoDB deployment, set the
39+
``timeoutMS`` connection option to the timeout length in milliseconds. You can
40+
set the ``timeoutMS`` option in the following ways:
41+
42+
- Calling the ``timeout()`` method from the
43+
``MongoClientSettings.Builder`` class
44+
- Setting the ``timeoutMS`` parameter in your connection string
45+
46+
The following code examples set a client-level timeout of ``200`` milliseconds.
47+
Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection
48+
String` tab to see the corresponding code.
49+
50+
.. tabs::
51+
52+
.. tab:: MongoClientSettings
53+
:tabid: mongoclientsettings
54+
55+
.. literalinclude:: /includes/connect/CsotExample.kt
56+
:language: java
57+
:start-after: start-mongoclientsettings
58+
:end-before: end-mongoclientsettings
59+
:dedent:
60+
:emphasize-lines: 3
61+
62+
.. tab:: Connection String
63+
:tabid: connection-string
64+
65+
.. literalinclude:: /includes/connect/CsotExample.kt
66+
:language: java
67+
:start-after: start-string
68+
:end-before: end-string
69+
:dedent:
70+
71+
Accepted Timeout Values
72+
~~~~~~~~~~~~~~~~~~~~~~~
73+
74+
The following table describes the timeout behavior corresponding to the
75+
accepted values for ``timeoutMS``:
76+
77+
.. list-table::
78+
:header-rows: 1
79+
:widths: 25 75
80+
81+
* - Value
82+
- Behavior
83+
84+
* - Positive integer
85+
- Sets the timeout to use for operation completion.
86+
87+
* - ``0``
88+
- Specifies that operations never time out.
89+
90+
* - ``null`` or unset
91+
- | Defers the timeout behavior to the following settings:
92+
93+
- :manual:`waitQueueTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.waitQueueTimeoutMS>`
94+
- :manual:`socketTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.socketTimeoutMS>`
95+
- :manual:`wTimeoutMS </reference/connection-string-options/#mongodb-urioption-urioption.wtimeoutMS>`
96+
- :manual:`maxTimeMS </reference/method/cursor.maxTimeMS/>`
97+
- `maxCommitTimeMS <{+core-api+}/TransactionOptions.Builder.html#maxCommitTime(java.lang.Long,java.util.concurrent.TimeUnit)>`__
98+
99+
| These settings are deprecated and are ignored if you set ``timeoutMS``.
100+
101+
If you specify the ``timeoutMS`` option, the driver automatically applies the
102+
specified timeout to each server operation. The following code example specifies
103+
a timeout of ``200`` milliseconds at the client level, and then calls the
104+
``MongoCollection.insertOne()`` method:
105+
106+
.. literalinclude:: /includes/connect/CsotExample.kt
107+
:language: java
108+
:start-after: start-operation-timeout
109+
:end-before: end-operation-timeout
110+
:dedent:
111+
112+
Timeout Inheritance
113+
~~~~~~~~~~~~~~~~~~~
114+
115+
When you specify the ``timeoutMS`` option, the driver applies the timeout
116+
according to the same inheritance behaviors as the other {+driver-short+} options.
117+
The following table describes how the timeout value is inherited at each level:
118+
119+
.. list-table::
120+
:header-rows: 1
121+
:widths: 30 70
122+
123+
* - Level
124+
- Inheritance Description
125+
126+
* - Operation
127+
- Takes the highest precedence and overrides the timeout
128+
options that you set at any other level.
129+
130+
* - Transaction
131+
- Takes precedence over the timeout value that you set at the session,
132+
collection, database, or client level.
133+
134+
* - Session
135+
- Applies to all transactions and operations within
136+
that session, unless you set a different timeout value at those
137+
levels.
138+
139+
* - Database
140+
- Applies to all sessions and operations within that
141+
database, unless you set a different timeout value at those
142+
levels.
143+
144+
* - Collection
145+
- Applies to all sessions and operations on that
146+
collection, unless you set a different timeout value at those
147+
levels.
148+
149+
* - Client
150+
- Applies to all databases, collections, sessions, transactions, and
151+
operations within that client that do not otherwise specify
152+
``timeoutMS``.
153+
154+
For more information on overrides and specific options, see the following
155+
:ref:`kotlin-sync-csot-overrides` section.
156+
157+
.. _kotlin-sync-csot-overrides:
158+
159+
Overrides
160+
---------
161+
162+
The {+driver-short+} supports various levels of configuration to control the
163+
behavior and performance of database operations.
164+
165+
You can specify a ``timeoutMS`` option at a more specific level to override the
166+
client-level configuration. The table in the preceding section describes
167+
the levels at which you can specify a timeout setting. This allows you
168+
to customize timeouts based on the needs of individual operations.
169+
170+
The following example demonstrates how a collection-level timeout
171+
configuration can override a client-level timeout configuration:
172+
173+
.. literalinclude:: /includes/connect/CsotExample.kt
174+
:language: java
175+
:start-after: start-override
176+
:end-before: end-override
177+
:dedent:
178+
:emphasize-lines: 10
179+
180+
.. _kotlin-sync-csot-transaction:
181+
182+
Transactions
183+
~~~~~~~~~~~~
184+
185+
When you create a new `ClientSession <{+driver-api+}/-client-session/index.html>`__
186+
instance to implement a transaction, use the ``defaultTimeout()`` method
187+
when building a ``ClientSessionOptions`` instance. You can use this
188+
option to specify the timeout for the following methods:
189+
190+
- `commitTransaction() <{+driver-api+}/-client-session/commit-transaction.html>`__
191+
- `abortTransaction() <{+driver-api+}/-client-session/abort-transaction.html>`__
192+
- `withTransaction() <{+driver-api+}/-client-session/with-transaction.html>`__
193+
- `close() <{+core-api+}/session/ClientSession.html#close()>`__
194+
195+
The following code demonstrates how to set the ``defaultTimeout`` when
196+
instantiating a ``ClientSession``:
197+
198+
.. literalinclude:: /includes/connect/CsotExample.kt
199+
:language: java
200+
:start-after: start-session-timeout
201+
:end-before: end-session-timeout
202+
:dedent:
203+
204+
If you do not specify the ``defaultTimeout``, the driver uses the timeout
205+
value set on the parent ``MongoClient``.
206+
207+
You can also set a transaction-level timeout by calling the ``timeout()``
208+
method when building a ``TransactionOptions`` instance. Setting this
209+
option applies a timeout to all operations performed in the scope of the
210+
transaction:
211+
212+
.. literalinclude:: /includes/connect/CsotExample.kt
213+
:language: java
214+
:start-after: start-transaction-timeout
215+
:end-before: end-transaction-timeout
216+
:dedent:
217+
218+
To learn more about transactions, see the
219+
:ref:`kotlin-sync-write-transactions` guide.
220+
221+
Client Encryption
222+
~~~~~~~~~~~~~~~~~
223+
224+
When you use Client-Side Field Level Encryption (CSFLE), the driver uses the
225+
``timeoutMS`` option to limit the time allowed for encryption and decryption
226+
operations. You can set a timeout option for your ``ClientEncryption``
227+
instance by calling the ``timeout()`` method when building a
228+
``ClientEncryptionSettings`` instance.
229+
230+
If you specify the timeout when you construct a
231+
``ClientEncryption`` instance, the timeout controls the lifetime of all operations
232+
performed on that instance. If you do not provide a timeout when
233+
instantiating ``ClientEncryption``, the instance
234+
inherits the timeout setting from the ``MongoClient`` used in the
235+
``ClientEncryption`` constructor.
236+
237+
If you set ``timeoutMS`` both on the client and directly in
238+
``ClientEncryption``, the value provided to ``ClientEncryption`` takes
239+
precedence.
240+
241+
.. _kotlin-sync-csot-cursor:
242+
243+
Cursors
244+
-------
245+
246+
Cursors offer configurable timeout settings when using the CSOT feature. You can
247+
adjust cursor handling by configuring either the cursor lifetime or cursor
248+
iteration mode. To configure the timeout mode, use the ``timeoutMode()``
249+
method when performing any operation that returns an ``Iterable``.
250+
251+
For operations that create cursors, the timeout setting can either cap the
252+
lifetime of the cursor or be applied separately to the original
253+
operation and all subsequent calls.
254+
255+
.. note:: Inherited Timeout
256+
257+
Setting a cursor timeout mode requires that you set a timeout either
258+
in the ``MongoClientSettings``, on ``MongoDatabase``, or on
259+
``MongoCollection``.
260+
261+
To learn more about cursors, see the :ref:`kotlin-sync-cursors` guide.
262+
263+
Cursor Lifetime Mode
264+
~~~~~~~~~~~~~~~~~~~~
265+
266+
The cursor lifetime mode uses the timeout setting to limit the entire lifetime of a
267+
cursor. In this mode, your application must initialize the cursor, complete
268+
all calls to the cursor methods, and return all documents within the specified
269+
time limit. Otherwise, the cursor's lifetime expires and the driver
270+
raises a timeout error.
271+
272+
When you close a cursor by calling the ``close()`` method, the
273+
timeout resets for the ``killCursors`` command to ensure server-side resources are
274+
cleaned up.
275+
276+
The following example shows how to set a cursor timeout to ensure that
277+
the cursor is initialized and all documents are retrieved within the
278+
inherited timeout:
279+
280+
.. literalinclude:: /includes/connect/CsotExample.kt
281+
:language: java
282+
:start-after: start-cursor-lifetime
283+
:end-before: end-cursor-lifetime
284+
:dedent:
285+
:emphasize-lines: 3
286+
287+
Cursor Iteration Mode
288+
~~~~~~~~~~~~~~~~~~~~~
289+
290+
The cursor iteration mode sets the timeout to limit each call to
291+
the ``next()``, ``hasNext()``, and ``tryNext()`` methods. The timeout refreshes
292+
after each call completes. This is the default mode for all tailable cursors,
293+
such as the tailable cursors returned by the ``find()`` method on capped
294+
collections or change streams.
295+
296+
The following code example iterates over documents in the ``db.people`` collection
297+
by using a cursor with the ``ITERATION`` timeout mode, and then retrieves
298+
and prints the ``name`` field value for each document:
299+
300+
.. literalinclude:: /includes/connect/CsotExample.kt
301+
:language: java
302+
:start-after: start-cursor-iteration
303+
:end-before: end-cursor-iteration
304+
:dedent:
305+
:emphasize-lines: 3
306+
307+
API Documentation
308+
-----------------
309+
310+
To learn more about using timeouts with the {+driver-short+}, see the following
311+
API documentation:
312+
313+
- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__
314+
- `MongoClientSettings.Builder.timeout() <{+core-api+}/MongoClientSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
315+
- `MongoCollection.withTimeout() <{+driver-api+}/-mongo-collection/with-timeout.html>`__
316+
- `ClientSessionOptions.Builder.defaultTimeout() <{+core-api+}/ClientSessionOptions.Builder.html#defaultTimeout(long,java.util.concurrent.TimeUnit)>`__
317+
- `TransactionOptions.Builder.timeout() <{+core-api+}/TransactionOptions.Builder.html#timeout(java.lang.Long,java.util.concurrent.TimeUnit)>`__
318+
- `ClientEncryptionSettings.Builder.timeout() <{+core-api+}/ClientEncryptionSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
319+
- `FindIterable.timeoutMode() <{+driver-api+}/-find-iterable/timeout-mode.html>`__
320+
- `TimeoutMode <{+core-api+}/client/cursor/TimeoutMode.html>`__

0 commit comments

Comments
 (0)