Skip to content

Commit 7b42a4b

Browse files
comandeo-mongoneilshwekyp-mongo
authored
MONGOID-5445 Add load_async to criteria (#5454)
Co-authored-by: Neil Shweky <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 9695803 commit 7b42a4b

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

source/reference/configuration.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ for details on driver options.
265265
# if the database name is not explicitly defined. (default: nil)
266266
app_name: MyApplicationName
267267

268+
# Type of executor for queries scheduled using ``load_async`` method.
269+
#
270+
# There are two possible values for this option:
271+
#
272+
# - :immediate - Queries will be immediately executed on a current thread.
273+
# This is the default option.
274+
# - :global_thread_pool - Queries will be executed asynchronously in
275+
# background using a thread pool.
276+
#async_query_executor: :immediate
277+
268278
# Mark belongs_to associations as required by default, so that saving a
269279
# model with a missing belongs_to association will trigger a validation
270280
# error. (default: true)
@@ -358,6 +368,11 @@ for details on driver options.
358368
# Raise an exception when a field is redefined. (default: false)
359369
duplicate_fields_exception: false
360370

371+
# Defines how many asynchronous queries can be executed concurrently.
372+
# This option should be set only if `async_query_executor` option is set
373+
# to `:global_thread_pool`.
374+
#global_executor_concurrency: nil
375+
361376
# Include the root model name in json serialization. (default: false)
362377
include_root_in_json: false
363378

source/reference/queries.txt

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2419,5 +2419,72 @@ will query the database and separately cache their results.
24192419
To use the cached results, call ``all.to_a.first`` on the model class.
24202420

24212421

2422-
.. _legacy-query-cache-limitations:
2422+
.. _load-async:
24232423

2424+
Asynchronous Queries
2425+
====================
2426+
2427+
Mongoid allows running database queries asynchronously in the background.
2428+
This can be beneficial when there is a need to get documents from different
2429+
collections.
2430+
2431+
In order to schedule an asynchronous query call the ``load_async`` method on a
2432+
``Criteria``:
2433+
2434+
.. code-block:: ruby
2435+
2436+
class PagesController < ApplicationController
2437+
def index
2438+
@active_bands = Band.where(active: true).load_async
2439+
@best_events = Event.best.load_async
2440+
@public_articles = Article.where(public: true).load_async
2441+
end
2442+
end
2443+
2444+
In the above example three queries will be scheduled for asynchronous execution.
2445+
Results of the queries can be later accessed as usual:
2446+
2447+
.. code-block:: html
2448+
2449+
<ul>
2450+
<%- @active_bands.each do -%>
2451+
<li><%= band.name %></li>
2452+
<%- end -%>
2453+
</ul>
2454+
2455+
Even if a query is scheduled for asynchronous execution, it might be executed
2456+
synchronously on the caller's thread. There are three possible scenarios depending
2457+
on when the query results are being accessed:
2458+
2459+
#. If the scheduled asynchronous task has been already executed, the results are returned.
2460+
#. If the task has been started, but not finished yet, the caller's thread blocks until the task is finished.
2461+
#. If the task has not been started yet, it is removed from the execution queue, and the query is executed synchronously on the caller's thread.
2462+
2463+
.. note::
2464+
2465+
Even though ``load_async`` method returns a ``Criteria`` object, you should not
2466+
do any operations on this object except accessing query results. The query is
2467+
scheduled for execution immediately after calling ``load_async``, therefore
2468+
later changes to the `Criteria`` object may not be applied.
2469+
2470+
2471+
Configuring asynchronous query execution
2472+
----------------------------------------
2473+
2474+
Asynchronous queries are disabled by default. When asynchronous queries are
2475+
disabled, ``load_async`` will execute the query immediately on the current thread,
2476+
blocking as necessary. Therefore, calling ``load_async`` on criteria in this case
2477+
is roughly the equivalent of calling ``to_a`` to force query execution.
2478+
2479+
In order to enable asynchronous query execution, the following config options
2480+
must be set:
2481+
2482+
.. code-block:: yaml
2483+
2484+
development:
2485+
...
2486+
options:
2487+
# Execute asynchronous queries using a global thread pool.
2488+
async_query_executor: :global_thread_pool
2489+
# Number of threads in the pool. The default is 4.
2490+
# global_executor_concurrency: 4

source/release-notes/mongoid-8.1.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ The complete list of releases is available `on GitHub
1717
please consult GitHub releases for detailed release notes and JIRA for
1818
the complete list of issues fixed in each release, including bug fixes.
1919

20+
Added ``load_async`` method on ``Criteria`` to asynchronously load documents
21+
----------------------------------------------------------------------------
22+
23+
The new ``load_async`` method on ``Criteria`` allows :ref:`running database queries asynchronously <load-async>`.
24+
2025

2126
Added ``attribute_before_last_save``, ``saved_change_to_attribute``, ``saved_change_to_attribute?``, and ``will_save_change_to_attribute?`` methods
2227
----------------------------------------------------------------------------------------------------------------------------------------------------
@@ -245,8 +250,8 @@ for more details.
245250
Added ``readonly!`` method and ``legacy_readonly`` feature flag
246251
---------------------------------------------------------------
247252

248-
Mongoid 8.1 changes the meaning of read-only documents. In Mongoid 8.1 with
249-
this feature flag turned off, a document becomes read-only when calling the
253+
Mongoid 8.1 changes the meaning of read-only documents. In Mongoid 8.1 with
254+
this feature flag turned off, a document becomes read-only when calling the
250255
``readonly!`` method:
251256

252257
.. code:: ruby
@@ -261,8 +266,8 @@ this feature flag turned off, a document becomes read-only when calling the
261266
With this feature flag turned off, a ``ReadonlyDocument`` error will be
262267
raised when destroying or deleting, as well as when saving or updating.
263268

264-
Prior to Mongoid 8.1 and in 8.1 with the ``legacy_readonly`` feature flag
265-
turned on, documents become read-only when they are projected (i.e. using
269+
Prior to Mongoid 8.1 and in 8.1 with the ``legacy_readonly`` feature flag
270+
turned on, documents become read-only when they are projected (i.e. using
266271
``#only`` or ``#without``).
267272

268273
.. code:: ruby
@@ -278,7 +283,7 @@ turned on, documents become read-only when they are projected (i.e. using
278283
band.destroy # => raises ReadonlyDocument error
279284

280285
Note that with this feature flag on, a ``ReadonlyDocument`` error will only be
281-
raised when destroying or deleting, and not on saving or updating. See the
286+
raised when destroying or deleting, and not on saving or updating. See the
282287
section on :ref:`Read-only Documents <readonly-documents>` for more details.
283288

284289

0 commit comments

Comments
 (0)