fix(prometheus): flush expired slab memory in exporter timer#13195
Open
sihyeonn wants to merge 1 commit intoapache:masterfrom
Open
fix(prometheus): flush expired slab memory in exporter timer#13195sihyeonn wants to merge 1 commit intoapache:masterfrom
sihyeonn wants to merge 1 commit intoapache:masterfrom
Conversation
When metrics are registered with an `expire` value, nginx's slab allocator marks entries as logically expired but does not return the underlying slab pages to the free-space pool automatically. As a result, `free_space_bytes` decreases monotonically over time even when many time series have expired, because slabs are only reclaimed when a flush is explicitly requested. Call `dict:flush_expired(1000)` in `exporter_timer` (which runs every `refresh_interval`, defaulting to 15 s) so that expired slabs are reclaimed promptly. The `max_count=1000` argument bounds the write-lock hold time to a few milliseconds per call, avoiding any noticeable impact on worker request processing. Fixes the pattern where `apisix_shared_dict_free_space_bytes` for `prometheus-metrics` decreases continuously until the dict is exhausted, even though active time-series counts fluctuate normally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When metrics are configured with an
expirevalue, nginx's slab allocator marks entries as logically expired but does not automatically return the underlying slab pages to the free-space pool. As a result,apisix_shared_dict_free_space_bytesforprometheus-metricsdecreases monotonically over time — slabs are only reclaimed when explicitly flushed.Root Cause
ngx.shared.DICT:flush_expired()must be called explicitly to reclaim slab memory from expired entries. Without it:expireseconds)free_space_bytestrends toward zero regardless of actual active time-series countThis can be observed by comparing
free_space_byteswith the active time-series count: the count fluctuates (e.g. drops significantly during low-traffic periods) while free space never recovers — even after most entries have expired.Fix
Call
dict:flush_expired(1000)insideexporter_timer, which already runs everyrefresh_interval(default 15 s) in the privileged agent process.Why
max_count=1000: Without a limit, a single flush call could hold the shared-dict write lock for an extended time if many expired entries have accumulated. Limiting to 1000 per cycle keeps the lock time well under 10 ms in practice, while remaining entries are flushed in subsequent timer ticks (every 15 s).The call runs in the privileged agent process, which is separate from worker request-handling processes, so the brief write-lock has minimal impact on request throughput.
Checklist
expiremetric configurationrefresh_intervalsetting