Skip to content

Commit 133da16

Browse files
authored
DOCSP-35923: csot page (#657)
* DOCSP-35923: csot page * small fix * api docs + fixes * txn code examples * deprecations * list fix * fix * MW + MK PR fixes * MK tech review 2
1 parent 6f002e7 commit 133da16

File tree

11 files changed

+494
-24
lines changed

11 files changed

+494
-24
lines changed

source/connection/specify-connection-options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Specify Connection Options
2222
.. toctree::
2323

2424
Stable API </connection/specify-connection-options/stable-api>
25+
Limit Execution Time </connection/specify-connection-options/csot>
2526
Connection Pools </connection/specify-connection-options/connection-pools>
2627
Cluster Settings </connection/specify-connection-options/cluster-settings>
2728
Server Settings </connection/specify-connection-options/server-settings>
@@ -38,6 +39,7 @@ This section describes the MongoDB connection and authentication options
3839
available in the {+driver-short+}. You can specify the following connection options:
3940

4041
- :ref:`Stable API <stable-api-java>`
42+
- :ref:`Limit Server Execution Time <java-csot>`
4143
- :ref:`Connection Pools <java-connection-pools>`
4244
- :ref:`Cluster Settings <mcs-cluster-settings>`
4345
- :ref:`Server Settings <mcs-server-settings>`

source/connection/specify-connection-options/connection-pools.txt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ see the corresponding syntax:
8787

8888
* - ``maxPoolSize``
8989

90-
- Maximum number of connections opened in the pool. When the
91-
connection pool reaches the maximum number of connections, new
92-
connections wait up until to the value of
93-
``waitQueueTimeoutMS``.
90+
- Maximum number of connections opened in the pool. If an
91+
operation needs a new connection while the connection pool has
92+
``maxPoolSize`` connections open, the new
93+
operation waits for a new connection to open. To limit this
94+
waiting time, use the single timeout setting. To learn more,
95+
see the :ref:`java-csot` guide.
9496

9597
*Default:* ``100``
9698

@@ -102,11 +104,15 @@ see the corresponding syntax:
102104

103105
*Default*: ``0``
104106

105-
* - ``waitQueueTimeoutMS``
107+
* - ``waitQueueTimeoutMS`` *(deprecated)*
106108

107-
- Maximum wait time in milliseconds that an operation can wait for
109+
- This option is deprecated. You can configure this timeout by
110+
setting the the :ref:`client-level timeout <java-csot>`
111+
instead.
112+
113+
Maximum wait time in milliseconds that an operation can wait for
108114
a connection to become available. A value of ``0`` means there
109-
is no limit.
115+
is no limit.
110116

111117
*Default*: ``120000`` (120 seconds)
112118

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
.. _java-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.java
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.java
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.java
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:`java-csot-overrides` section.
156+
157+
.. _java-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.java
174+
:language: java
175+
:start-after: start-override
176+
:end-before: end-override
177+
:dedent:
178+
:emphasize-lines: 10
179+
180+
.. _java-csot-transaction:
181+
182+
Transactions
183+
~~~~~~~~~~~~
184+
185+
When you create a new `ClientSession <{+driver-api+}/ClientSession.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+}/ClientSession.html#commitTransaction()>`__
191+
- `abortTransaction() <{+driver-api+}/ClientSession.html#abortTransaction()>`__
192+
- `withTransaction() <{+driver-api+}/ClientSession.html#withTransaction(com.mongodb.client.TransactionBody)>`__
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.java
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.java
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 :ref:`java-fundamentals-transactions` guide.
219+
220+
Client Encryption
221+
~~~~~~~~~~~~~~~~~
222+
223+
When you use Client-Side Field Level Encryption (CSFLE), the driver uses the
224+
``timeoutMS`` option to limit the time allowed for encryption and decryption
225+
operations. You can set a timeout option for your ``ClientEncryption``
226+
instance by calling the ``timeout()`` method when building a
227+
``ClientEncryptionSettings`` instance.
228+
229+
If you specify the timeout when you construct a
230+
``ClientEncryption`` instance, the timeout controls the lifetime of all operations
231+
performed on that instance. If you do not provide a timeout when
232+
instantiating ``ClientEncryption``, the instance
233+
inherits the timeout setting from the ``MongoClient`` used in the
234+
``ClientEncryption`` constructor.
235+
236+
If you set ``timeoutMS`` both on the client and directly in
237+
``ClientEncryption``, the value provided to ``ClientEncryption`` takes
238+
precedence.
239+
240+
.. _java-csot-cursor:
241+
242+
Cursors
243+
-------
244+
245+
Cursors offer configurable timeout settings when using the CSOT feature. You can
246+
adjust cursor handling by configuring either the cursor lifetime or cursor
247+
iteration mode. To configure the timeout mode, use the ``timeoutMode()``
248+
method when performing any operation that returns an ``Iterable``.
249+
250+
For operations that create cursors, the timeout setting can either cap the
251+
lifetime of the cursor or be applied separately to the original
252+
operation and all subsequent calls.
253+
254+
.. note:: Inherited Timeout
255+
256+
Setting a cursor timeout mode requires that you set a timeout either
257+
in the ``MongoClientSettings``, on ``MongoDatabase``, or on
258+
``MongoCollection``.
259+
260+
To learn more about cursors, see the :ref:`java-fundamentals-cursor` guide.
261+
262+
Cursor Lifetime Mode
263+
~~~~~~~~~~~~~~~~~~~~
264+
265+
The cursor lifetime mode uses the timeout setting to limit the entire lifetime of a
266+
cursor. In this mode, your application must initialize the cursor, complete
267+
all calls to the cursor methods, and return all documents within the specified
268+
time limit. Otherwise, the cursor's lifetime expires and the driver
269+
raises a timeout error.
270+
271+
When you close a cursor by calling the ``close()`` method, the
272+
timeout resets for the ``killCursors`` command to ensure server-side resources are
273+
cleaned up.
274+
275+
The following example shows how to set a cursor timeout to ensure that
276+
the cursor is initialized and all documents are retrieved within the
277+
inherited timeout:
278+
279+
.. literalinclude:: /includes/connect/CsotExample.java
280+
:language: java
281+
:start-after: start-cursor-lifetime
282+
:end-before: end-cursor-lifetime
283+
:dedent:
284+
:emphasize-lines: 3
285+
286+
Cursor Iteration Mode
287+
~~~~~~~~~~~~~~~~~~~~~
288+
289+
The cursor iteration mode sets the timeout to limit each call to
290+
the ``next()``, ``hasNext()``, and ``tryNext()`` methods. The timeout refreshes
291+
after each call completes. This is the default mode for all tailable cursors,
292+
such as the tailable cursors returned by the ``find()`` method on capped
293+
collections or change streams.
294+
295+
The following code example iterates over documents in the ``db.people`` collection
296+
by using a cursor with the ``ITERATION`` timeout mode, and then retrieves
297+
and prints the ``name`` field value for each document:
298+
299+
.. literalinclude:: /includes/connect/CsotExample.java
300+
:language: java
301+
:start-after: start-cursor-iteration
302+
:end-before: end-cursor-iteration
303+
:dedent:
304+
:emphasize-lines: 3
305+
306+
API Documentation
307+
-----------------
308+
309+
To learn more about using timeouts with the {+driver-short+}, see the following
310+
API documentation:
311+
312+
- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__
313+
- `MongoClientSettings.Builder.timeout() <{+core-api+}/MongoClientSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
314+
- `MongoCollection.withTimeout() <{+driver-api+}/MongoCollection.html#withTimeout(long,java.util.concurrent.TimeUnit)>`__
315+
- `ClientSessionOptions.Builder.defaultTimeout() <{+core-api+}/ClientSessionOptions.Builder.html#defaultTimeout(long,java.util.concurrent.TimeUnit)>`__
316+
- `TransactionOptions.Builder.timeout() <{+core-api+}/TransactionOptions.Builder.html#timeout(java.lang.Long,java.util.concurrent.TimeUnit)>`__
317+
- `ClientEncryptionSettings.Builder.timeout() <{+core-api+}/ClientEncryptionSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
318+
- `FindIterable.timeoutMode() <{+driver-api+}/FindIterable.html#timeoutMode(com.mongodb.client.cursor.TimeoutMode)>`__
319+
- `TimeoutMode <{+core-api+}/client/cursor/TimeoutMode.html>`__

source/connection/specify-connection-options/stable-api.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
{+stable-api+}
66
==============
77

8-
9-
108
.. contents:: On this page
119
:local:
1210
:backlinks: none
@@ -147,4 +145,3 @@ API Documentation:
147145

148146
- `strict() <{+core-api+}/ServerApi.Builder.html#strict(boolean)>`__
149147
- `deprecationErrors() <{+core-api+}/ServerApi.Builder.html#>`__
150-

source/crud/compound-operations.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ following options:
8383

8484
- Exclude the ``_id`` field from the found document with a projection.
8585
- Specify an upsert, which inserts the document specified by the query filter if no documents match the query.
86-
- Set a maximum execution time of 5 seconds for this operation on the MongoDB
87-
instance. If the operation takes longer, the ``findOneAndUpdate()`` method
88-
will throw a ``MongoExecutionTimeoutException``.
8986

9087
.. literalinclude:: /includes/fundamentals/code-snippets/CompoundOperatorsIndividualExamples.java
9188
:language: java

0 commit comments

Comments
 (0)