| 1 | <!-- groonga-command --> |
| 2 | <!-- database: n_workers --> |
| 3 | |
| 4 | # Parallel execution |
| 5 | |
| 6 | ## Summary |
| 7 | |
| 8 | Groonga executes serially by default. |
| 9 | However, by specifying the option you can execute in parallel. |
| 10 | |
| 11 | The next section shows you how to set up for parallel execution. |
| 12 | Please read the notes before using this option. |
| 13 | |
| 14 | ## How to use |
| 15 | |
| 16 | ### Set per Groonga command |
| 17 | |
| 18 | Only works for the command to be executed. |
| 19 | |
| 20 | #### Specified by `--n_workers` option of Groonga command |
| 21 | |
| 22 | Groonga command example: |
| 23 | |
| 24 | ``` |
| 25 | load --table Data --n_workers -1 |
| 26 | [ |
| 27 | {"_key", "value"} |
| 28 | ] |
| 29 | ``` |
| 30 | |
| 31 | ### Set the default value |
| 32 | |
| 33 | If you set a default value, you do not need to specify it for each Groonga command. |
| 34 | The default value is used for all Groonga commands. |
| 35 | |
| 36 | #### Specified by `--default-n-workers` option of `groonga` executable file |
| 37 | |
| 38 | Execution example: |
| 39 | |
| 40 | ```console |
| 41 | $ groonga --default-n-workers -1 DB_PATH status |
| 42 | ``` |
| 43 | |
| 44 | #### Specified by environment variable `GRN_N_WORKERS_DEFAULT` |
| 45 | |
| 46 | Execution example: |
| 47 | |
| 48 | ```console |
| 49 | $ GRN_N_WORKERS_DEFAULT=-1 groonga DB_PATH status |
| 50 | ``` |
| 51 | |
| 52 | ### Available Values |
| 53 | |
| 54 | You can set the number of parallels. |
| 55 | If you specify `-1` or `2` or more, it will execute in parallel. |
| 56 | |
| 57 | ```{eval-rst} |
| 58 | .. list-table:: |
| 59 | :header-rows: 1 |
| 60 | |
| 61 | * - n_workers |
| 62 | - Behavior |
| 63 | * - When specifying ``0`` or ``1`` |
| 64 | - Execute in serial. |
| 65 | * - When specifying ``2`` or more |
| 66 | - Execute in parallel with at most the specified number of threads. |
| 67 | * - When specifying ``-1`` |
| 68 | - Execute in parallel with the threads of at most the number of CPU cores. |
| 69 | ``` |
| 70 | |
| 71 | ## Check the settings |
| 72 | |
| 73 | You can check it by the value of `n_workers` and `default_n_workers` in the {doc}`/reference/commands/status` command. |
| 74 | |
| 75 | <!-- groonga-command --> |
| 76 | |
| 77 | ```{include} ../../example/reference/command/n_workers/status.md |
| 78 | status |
| 79 | ``` |
| 80 | |
| 81 | `n_workers` is per Groonga command value. `default_n_workers` is the default value. |
| 82 | |
| 83 | ## Notes |
| 84 | |
| 85 | ### Apache Arrow is required |
| 86 | |
| 87 | This feature requires that Apache Arrow is enabled in Groonga. |
| 88 | |
| 89 | It depends on package provider whether Apache Arrow is enabled or not. |
| 90 | |
| 91 | To check whether Apache Arrow is enabled, you can use {doc}`/reference/commands/status` command that show the result of `apache_arrow` is `true` or not. |
| 92 | |
| 93 | ### For use as a daemon process |
| 94 | |
| 95 | For example, consider using {doc}`/reference/executables/groonga-server-http` on a system with 6 CPUs. |
| 96 | |
| 97 | {doc}`/reference/executables/groonga-server-http` allocates 1 thread (= 1 CPU) for each request. |
| 98 | |
| 99 | When the average number of concurrent connections is 6, there are no free CPU resources because 6 CPUs are already in use. |
| 100 | All the CPU is used to process each request. |
| 101 | |
| 102 | When the average number of concurrent connections is 2, there are 4 free CPU resources because only 2 CPUs are already in use. |
| 103 | When specifying `2` for `n_workers`, it uses at most 3 CPUs, including the thread for processing requests. |
| 104 | Therefore, if two requests to Groonga process with `2` specified for `n_workers` are requested at the same time, |
| 105 | they will use at most 6 CPUs in total and will be processed fastly by using all of the resources. |
| 106 | When specifying greater than `2`, the degree of parallelism can be higher than the CPU resources, so it may actually slow down the execution time. |
| 107 | |
| 108 | ## Parallel execution support |
| 109 | |
| 110 | - {ref}`offline-index-construction` |
| 111 | - {doc}`/reference/commands/load` |
| 112 | - {ref}`select <select-n-workers>` |