Principles
ApplySrv Thread Count Calculation Model
The OceanBase tenant-level dynamic thread pool calculates the number of threads for each module using the following formula:
thread_cnt = max(1, dynamic_factor × tenant_unit_max_cpu)
tenant_unit_max_cpu comes from the tenant Unit configuration, and dynamic_factor is the dynamic factor specified at module registration.
In the native OceanBase 4.2.5.6 source code, ApplySrv registers its own thread pool in ObLogApplyService::init through the macro MTL_REGISTER_THREAD_DYNAMIC(0.5, tg_id_) (see src/logservice/applyservice/ob_log_apply_service.cpp), using a fixed factor of 0.5. In this case, the tenant dynamic thread table thread_dynamic_factor_map_ (defined in src/share/rc/ob_tenant_base.h) stores only the factor as a ThreadDynamicNode → double mapping, without carrying an upper limit, which is equivalent to max_thread_cnt = INT64_MAX (that is, no upper limit).
This feature introduces the following upgrades:
- Adds a ThreadDynamicConfig structure that carries both the factor and the upper limit (replacing the original double value):
struct ThreadDynamicConfig { double dynamic_factor_; int64_t max_thread_cnt_; }; - Adds an interface with an upper limit, MTL_REGISTER_THREAD_DYNAMIC_WITH_LIMIT(factor, th, max_thread_cnt), to which ApplySrv switches to perform registration.
- Adds a calc_dynamic_thread_count calculation function to upgrade the thread count calculation to a capped model:
thread_cnt = max(1, min(dynamic_factor × tenant_unit_max_cpu, max_thread_cnt))
Parameter Semantics and Compatibility Strategy
A new tenant-level parameter log_apply_thread_factor is added (the value ranges from 0 to 5, defaults to 0, and is dynamically effective).
Parameter Value |
Calculation Factor |
Thread Upper Limit |
Description |
|---|---|---|---|
0 (default) |
0.5 (compatible value) |
INT64_MAX (no upper limit) |
Fully compatible with the native behavior; the number of ApplySrv threads grows linearly with CPU core count without a cap. |
> 0 |
User-defined value |
128 |
User-defined factor, with the number of threads capped at 128. |
That is, when the parameter is 0, the behavior is exactly the same as the native version and does not affect existing clusters; when it is set to a non-zero value, the new model is used for calculation and the number of threads is capped.
Dynamic Activation and Instant Adjustment
This parameter is DYNAMIC_EFFECTIVE. After modification, there is no need to restart the OBServer. The changed paths are as follows:
- A user runs alter system set log_apply_thread_factor = X tenant = ....
- ObMultiTenant::update_tenant_config (src/observer/omt/ob_multi_tenant.cpp) triggers the newly added update_log_apply_thread_config.
- This function calls ObLogApplyService::reload_thread_config (src/logservice/applyservice/ob_log_apply_service.cpp) to update the thread pool configuration.
- Through the newly added macro MTL_UPDATE_THREAD_DYNAMIC_IMMEDIATELY, it calls ObTenantBase::update_module_thread_dynamic_immediately, and finally adjusts the number of threads immediately through TG_SET_THREAD_CNT_IMMEDIATELY and ObSimpleDynamicThreadPool::set_thread_count_immediately, instead of relying on the gradual scaling of the dynamic thread pool.
To ensure concurrency security, the feature introduces the thread_dynamic_lock_ and thread_config_lock_ mutexes in ObTenantBase and ObLogApplyService respectively to protect configuration reads and writes and thread count changes.
Application Scenarios
- For large-specification tenants (with a large unit_max_cpu), the number of ApplySrv threads is excessive, and you want to reduce thread overhead by capping it.
- The replay concurrency needs to be adjusted based on different service workloads (for example, increasing the factor in write-heavy and read-light scenarios, and decreasing the factor in CPU core-constrained scenarios).
- Existing clusters require compatibility and the optimization is enabled only for some tenants.