Bookkeeping load manager
The bookkeeping load manager slows bookkeeping operations when the machine that runs the harvesting modules is under sustained load. This is useful while the harvesting scripts and the triple store run on the same virtual machine: reducing the rate of new SPARQL operations and processing jobs gives both workloads more time when CPU or I/O contention is high.
The implementation is in bookkeeping/load_manager.py. It is used by BookkeepingHandler for triple-store pacing and by ProcessingInterface when jobs are handed out. It does not change the number of already-running jobs and is separate from SPARQL error retries.
Load calculation
LoadManager reads the one-minute load average with psutil.getloadavg() and normalizes it by the number of CPUs available to the process:
normalized load = one-minute load average / CPU count
A normalized load of 1.0 means that, on average, there is one runnable process or process waiting for uninterruptible I/O per available CPU. Because load average includes more than CPU utilization on Linux, it can also reflect storage contention affecting the local triple store.
The default load levels are:
| Normalized load | Classification | Sleep factor |
|---|---|---|
Below 0.75 |
Normal | 1 |
At least 0.75, but below 1.0 |
High | 2 |
At least 1.0 |
Very high | 4 |
The factor is a multiplier, not a sleep duration. For example, a base pause of 0.25 seconds becomes 0.5 seconds under high load and 1.0 second under very high load.
CPU-count detection and override
The CPU count is needed to make the load average comparable between machines of different sizes. Automatic detection uses the following order:
- the number of CPUs in the current process's CPU-affinity set;
- the logical CPU count reported by
psutil.cpu_count(); 1as a safe fallback if no count can be detected.
CPU affinity is preferred because it can describe the CPUs actually available to the worker more accurately than a host-wide count. Virtualization and container configuration can nevertheless expose an incorrect host CPU count. In that situation, set the known number of CPUs explicitly:
from bookkeeping.processinginterface import ProcessingInterface
with ProcessingInterface(
config_file="bookkeeping.json",
module_name="validate_metadata",
load_cpu_count=4,
) as processing_interface:
...
The explicit value is authoritative. It must be a positive integer. Passing 0, a negative number, a Boolean, or a non-integer raises ValueError.
Sample caching
Load samples are cached for five seconds by default. All general-graph and source-graph handlers opened by one ProcessingInterface share the same LoadManager, so they also share its cached value. Concurrent calls are protected by a lock and do not perform duplicate refreshes within the cache period.
The cache duration can be changed when opening the interface:
with ProcessingInterface(
config_file="bookkeeping.json",
module_name="validate_metadata",
load_cache_seconds=10,
) as processing_interface:
...
A value of 0 disables caching and samples on every request. This is normally unnecessary because the underlying one-minute load average changes gradually.
LoadManager interface
The class can also be constructed directly:
from bookkeeping.load_manager import LoadManager
load_manager = LoadManager(
adjust_for_load=True,
cpu_count=4,
cache_seconds=5,
high_load_threshold=0.75,
very_high_load_threshold=1.0,
high_load_factor=2,
very_high_load_factor=4,
)
sleep_factor = load_manager.get_factor()
Constructor parameters:
| Parameter | Default | Meaning |
|---|---|---|
adjust_for_load |
True |
Enables sampling and load-dependent factors. When False, get_factor() always returns 1. |
cpu_count |
None |
Explicit CPU count. None enables automatic detection. |
cache_seconds |
5 |
Time for which a sampled factor is reused. |
high_load_threshold |
0.75 |
Normalized load at which the high factor starts. |
very_high_load_threshold |
1.0 |
Normalized load at which the very-high factor starts. It must be greater than the high threshold. |
high_load_factor |
2 |
Factor returned for high load. It must be at least 1. |
very_high_load_factor |
4 |
Factor returned for very high load. It must be at least the high factor. |
get_factor(force_refresh=False) returns the cached factor while the cache is valid. force_refresh=True requests a new sample regardless of the cache expiry. The cached_load_ratio property exposes the normalized value from the last successful sample, or None when no successful sample is available.
Integration with bookkeeping
SPARQL operations
BookkeepingHandler.execute_sparql() applies a base pause of 0.2 seconds after every successful query or update. The effective pause is:
0.2 seconds * BookkeepingHandler.sleep_multiplier * current load factor
The existing additional pause between components registered by register_package() is adjusted in the same way. A handler created directly, rather than by ProcessingInterface, creates an enabled LoadManager automatically. A caller that manages several handlers can inject one shared manager through the load_manager constructor parameter.
The transient SPARQL retry delays remain fixed at 1, 2, and 4 seconds. They are deliberately not multiplied by the load factor because they implement failure recovery and are bounded with Prefect's cancellation timing in mind.
Processing jobs
Before ProcessingInterface creates and hands out a normal or startup job, it applies a base pause of 0.25 seconds multiplied by the current factor. End-of-pipeline and end-of-source indicators are not jobs and do not receive this pause.
Load adjustment is enabled by default for ProcessingInterface, so existing modules receive it without changing their flow code. The interface creates one manager and passes it to every BookkeepingHandler it opens.
archivematica_parallel_ingest is the intentional exception. That flow passes adjust_for_load=False, so its shared manager always returns factor 1 and never samples system load. The ordinary base pauses still apply; they simply do not increase with load.
To disable adjustment for another module explicitly:
with ProcessingInterface(
config_file="bookkeeping.json",
module_name="example_module",
adjust_for_load=False,
) as processing_interface:
...
See also Bookkeeping processing interface for the complete job lifecycle.
Dependency and deployment
The load manager uses psutil, which is listed in the project's requirements.txt. Install the updated dependencies in every environment that executes harvesting modules:
pip install -r requirements.txt
If load adjustment is enabled but psutil is unavailable, or if a metric cannot be read, the manager logs a warning and safely falls back to factor 1. The warning is suppressed after its first occurrence until a measurement succeeds. Bookkeeping therefore remains operational, but it is not load-adjusted until the dependency or metric problem is resolved.
Operational limitations
- The metric describes the machine visible to the harvesting process. This is appropriate while the scripts and triple store share a VM, but it will not describe a triple store moved to another host.
- The one-minute average reacts to sustained load rather than very short spikes. The cached sampling interval adds at most a small additional delay to that response.
- Slowing job handout controls admission rate but does not reduce the number of jobs that are already running.
max_parallelremains the control for concurrent open processing jobs. - Memory pressure and SPARQL response latency are not currently part of the factor. If the triple store is moved elsewhere, endpoint latency, timeouts, and HTTP
429or503responses should be considered separately.