Skip to content

Commit 792aee2

Browse files
committed
CSOT
(cherry picked from commit a35899b) (cherry picked from commit 2e0472c)
1 parent b2093d0 commit 792aee2

File tree

6 files changed

+461
-9
lines changed

6 files changed

+461
-9
lines changed

source/connect-to-mongo/connection-options.txt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,14 @@ Connection Configuration
143143
| **Default**: ``null``
144144
| **Connection URI Example**: ``maxLifeTimeMS=6000``
145145

146-
* - **socketTimeoutMS**
147-
- | Sets the number of milliseconds a recieve on a socket can take before timing out. If an
148-
| operation doesn't finish in the specified time, the {+driver-short+} raises a timeout exception.
146+
* - **socketTimeoutMS** *(deprecated)*
147+
- | This option is deprecated. You can configure this timeout by
148+
| setting the the :ref:`client-level timeout <javars-csot>`
149+
| instead.
150+
|
151+
| Milliseconds a recieve on a socket can take
152+
| before timing out. If an operation doesn't finish in the
153+
| specified time, the {+driver-short+} raises a timeout exception.
149154
|
150155
| **Data Type**: ``Integer``
151156
| **Default**: ``null``
@@ -206,6 +211,11 @@ Connection Pool Configuration
206211

207212
* - **maxPoolSize**
208213
- | Sets the maximum number of connections in the connection pool.
214+
If an operation needs a new connection while the connection pool has
215+
``maxPoolSize`` connections open, the new operation waits for a
216+
new connection to open. To limit this waiting time, use the
217+
single timeout setting. To learn more, see the
218+
:ref:`javars-csot` guide.
209219
|
210220
| **Data Type**: ``Integer``
211221
| **Default**: ``null``
@@ -218,8 +228,12 @@ Connection Pool Configuration
218228
| **Default**: ``null``
219229
| **Connection URI Example**: ``minPoolSize=3``
220230

221-
* - **waitQueueTimeoutMS**
222-
- | Sets the maximum amount of time to wait in milliseconds before either
231+
* - **waitQueueTimeoutMS** *(deprecated)*
232+
- | This option is deprecated. You can configure this timeout by
233+
| setting the the :ref:`client-level timeout <javars-csot>`
234+
| instead.
235+
|
236+
| Maximum amount of time to wait in milliseconds before either
223237
| an in-use connection becomes available or a connection is created and
224238
| starts becoming established.
225239
|
@@ -520,13 +534,17 @@ Write Concern Configuration
520534
| **Default**: ``null``
521535
| **Connection URI Example**: ``w=60``
522536

523-
* - **wtimeoutMS**
524-
- | If set, The driver adds ``{ wtimeout : ms }`` to all write commands. If
537+
* - **wtimeoutMS** *(deprecated)*
538+
- | This option is deprecated. You can configure this timeout by
539+
| setting the the :ref:`client-level timeout <javars-csot>`
540+
| instead.
541+
|
542+
| If set, The driver adds ``{ wtimeout : ms }`` to all write commands. If
525543
| set, implies ``safe=true``. This option is used in combination with ``w``.
526544
|
527545
| **Data Type**: ``Integer``
528546
| **Default**: ``null``
529547
| **Connection URI Example**: ``wtimeoutMS=6000``
530548

531549
For more information about the connection options in this section, see the
532-
:ref:`<java-rs-databases-coll>` guide.
550+
:ref:`<java-rs-databases-coll>` guide.

source/connect-to-mongo/csot.txt

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
.. _java-rs-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-rs-csot-overrides` section.
156+
157+
.. _java-rs-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-rs-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-rs-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-rs-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 a results that
249+
inherits from the ``Publisher`` interface.
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:`java-rs-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.java
281+
:language: java
282+
:start-after: start-cursor-lifetime
283+
:end-before: end-cursor-lifetime
284+
:dedent:
285+
:emphasize-lines: 3
286+
287+
API Documentation
288+
-----------------
289+
290+
To learn more about using timeouts with the {+driver-short+}, see the following
291+
API documentation:
292+
293+
- `MongoClientSettings <{+core-api+}/MongoClientSettings.html>`__
294+
- `MongoClientSettings.Builder.timeout() <{+core-api+}/MongoClientSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
295+
- `MongoCollection.withTimeout() <{+driver-api+}/MongoCollection.html#withTimeout(long,java.util.concurrent.TimeUnit)>`__
296+
- `ClientSessionOptions.Builder.defaultTimeout() <{+core-api+}/ClientSessionOptions.Builder.html#defaultTimeout(long,java.util.concurrent.TimeUnit)>`__
297+
- `TransactionOptions.Builder.timeout() <{+core-api+}/TransactionOptions.Builder.html#timeout(java.lang.Long,java.util.concurrent.TimeUnit)>`__
298+
- `ClientEncryptionSettings.Builder.timeout() <{+core-api+}/ClientEncryptionSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
299+
- `FindIterable.timeoutMode() <{+driver-api+}/FindPublisher.html#timeoutMode(com.mongodb.client.cursor.TimeoutMode)>`__
300+
- `TimeoutMode <{+core-api+}/client/cursor/TimeoutMode.html>`__

source/connect.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Connect to MongoDB
2525
Create a MongoClient </connect-to-mongo/create-a-mongo-client/>
2626
Choose a Connection Target </connect-to-mongo/choose-connection-target/>
2727
Connection URI Options </connect-to-mongo/connection-options/>
28+
Limit Execution Time </connect-to-mongo/csot>
2829
TLS/SSL </connect-to-mongo/tls/>
2930
Compress Network Traffic </connect-to-mongo/network-compression>
3031
Stable API </connect-to-mongo/stable-api/>

0 commit comments

Comments
 (0)