Skip to content

Add opt-in cgroup-aware minion_memory_headroom / minion_memory_max (#69884) - #70038

Merged
dwoz merged 2 commits into
saltstack:3006.xfrom
dwoz:dwoz/fix/69884-memory-headroom-configurable
Aug 18, 2026
Merged

dwoz merged 2 commits into
saltstack:3006.xfrom
dwoz:dwoz/fix/69884-memory-headroom-configurable

Conversation

@dwoz

@dwoz dwoz commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #69884.

salt/minion.py::_has_memory_headroom hard-codes psutil.virtual_memory().percent > 95 — a system-wide check that reserves 100 GB on a 2 TB host and uses the wrong reference on cgroup-limited minions.

Adds two opt-in minion config options:

  • minion_memory_headroom — required free headroom. Accepts either a percentage string ("5%") or an absolute size ("5G", "500M", int bytes).
  • minion_memory_max — explicit override for the reference "total memory available to this minion" (int bytes or size string).

Reference-memory resolution precedence:

  1. minion_memory_max if set.
  2. cgroup v2 memory.max (with memory.current for used).
  3. cgroup v1 memory.limit_in_bytes (with memory.usage_in_bytes).
  4. psutil.virtual_memory().total (with .used).

Cgroup detection is silent-fallback: missing / unreadable / malformed files log at DEBUG and drop to the next tier. No D-Bus / systemd dependency — the cgroup file the systemd unit writes to is already the source of truth.

When both new opts are unset the arithmetic reduces to psutil.virtual_memory().percent > 95, so no minion changes behavior on upgrade (LTS opt-in only, per #69443 / #69597 precedent).

New tests (40 cases) under tests/pytests/unit/test_minion_memory_headroom.py cover the parser, cgroup v1/v2 detection with a synthetic tmp_path cgroupfs, and the full 12-case behavior matrix (legacy default, config-only, cgroup-only, cgroup+config precedence, unlimited sentinels, permission-denied fallback, no-psutil, bogus input).

Adds opt-in minion_memory_headroom (accepts "5%" / "5G" / int bytes) and
minion_memory_max (bytes / size string) config options with silent
cgroup v1 / v2 detection. When either opt is set, the reference "total
memory available" is resolved from config > cgroup-v2 > cgroup-v1 >
psutil.virtual_memory().total, and used bytes come from the matching
tier. When neither opt is set the check preserves the legacy
psutil.virtual_memory().percent > 95 behavior byte-for-byte, so no
minion changes behavior on upgrade.

Refs saltstack#69884
Complements the unit-level matrix in
tests/pytests/unit/test_minion_memory_headroom.py by booting a real
minion daemon with each opt combination and driving the runtime check
via a subprocess that loads the minion's on-disk config through
salt.config.minion_config and calls Minion._has_memory_headroom.

Four scenarios:
  * config round-trip via salt-call --local config.get
  * default preserved (no opts -> legacy True on any healthy host)
  * config override -> deterministic True (huge max, tiny reserve)
  * config override -> deterministic False (tiny max, full reserve)

All scenarios use deterministic config values so no system-memory
pressure is required.

Refs saltstack#69884
dwoz added a commit to dwoz/salt that referenced this pull request Aug 15, 2026
Covers the new salt.utils.memory helpers and the two enforcement paths
introduced in the preceding commit:

* tests/pytests/unit/utils/test_memory.py (new): 51 cases spanning
  parse_size, parse_headroom, _read_cgroup_file, _parse_self_cgroup,
  _detect_cgroup_memory, resolve_memory_reference precedence chain,
  and the full has_memory_headroom matrix (both opts unset -> True;
  psutil missing -> True; over-limit -> False + WARNING log;
  bogus headroom -> 5% fallback; exceptions swallowed).  Mirrors the
  fixture shape from saltstack#70038's tests/pytests/unit/test_minion_memory_headroom.py.

* tests/pytests/unit/channel/test_server.py: 6 cases covering the
  MasterPubServerChannel._ep_memory_gate contract -- no-opt is a permit,
  set-from-start is a no-op, cleared blocks publish_payload,
  mid-await set releases it, and the PeriodicCallback closure toggles
  the gate based on has_memory_headroom's return value.

* tests/pytests/unit/transport/test_zeromq.py: 7 cases covering
  MWorkerQueue -- the non-pooled zmq_device warning fires (and only
  fires) when an opt is set; pooled zmq_device_pooled skips ROUTER
  recv_multipart when has_memory_headroom is False, always drains
  DEALER responses (worker replies must flow), admits when headroom
  is OK, caches the check per event_publisher_memory_check_interval,
  and never calls the check on the default (unset-opts) path.
@dwoz
dwoz merged commit 3c620bc into saltstack:3006.x Aug 18, 2026
428 checks passed
@dwoz
dwoz deleted the dwoz/fix/69884-memory-headroom-configurable branch November 13, 2026 08:29

This branch was previously deployed

1 inactive deployment
ci — 1ae9e230 Deployed Aug 14, 2026 by dwoz via Build Onedir Packages / RPM (x86_64) #26304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants