|
| 1 | +# Early Compaction of Stale Series from the Head Block |
| 2 | + |
| 3 | +* **Owners:** |
| 4 | + * Ganesh Vernekar (@codesome) |
| 5 | + |
| 6 | +* **Implementation Status:** Not implemented |
| 7 | + |
| 8 | +* **Related Issues and PRs:** |
| 9 | + * https://github.com/prometheus/prometheus/issues/13616 |
| 10 | + |
| 11 | +> TL;DR: This document is proposing a way of tracking stale series and compacting them (i.e. remove from in-memory head block) early when there are a lot of stale series. |
| 12 | +
|
| 13 | +## Why |
| 14 | + |
| 15 | +During rollouts, it is common to change some common labels on the series (like pod name), creating a whole new set of series and turning the old series stale. There are other scenarios that cause this series rotation (i.e. create a new batch of series and turn the old ones stale). In default configuration, Prometheus performs head compaction every 2 hours. This head compaction is performed on the older 2hr of data when Prometheus is holding 3hrs of data (1.5x). Until then, it holds onto all the series (including stale) in the memory for up to last 3 hrs. |
| 16 | + |
| 17 | +While this is a problem for Prometheus of all sizes, it is a bigger headache for huge Prometheus instances (think 100s of gigs of memory) where there are eventual limits on how big the memory allocation can be and restarts on OOM or scale up take too long. The problem is exaggerated when there are multiple rollouts in a short span (“short” \= within 1-2 hours) and Prometheus accumulates a lot of stale series. With the huge spikes in memory, this essentially prohibits how fast you can roll out your code (even rollback of problematic code that causes cardinality explosion is difficult). |
| 18 | + |
| 19 | +Glossary: |
| 20 | +- Head / head block: The in-memory portion of the TSDB. |
| 21 | +- Stale series: A time series that stopped getting any new samples. |
| 22 | +- Head GC: Removing old time series data from the head block. |
| 23 | +- Head Compaction: Process of creating persistent data blocks out of series present in the head block and performing head GC. |
| 24 | + |
| 25 | +### Pitfalls of the current solution |
| 26 | + |
| 27 | +There is no mechanism to proactively get rid of stale series from the head block. Prometheus has to wait until the next compaction to get rid of them. |
| 28 | + |
| 29 | +## Goals |
| 30 | + |
| 31 | +* Have a simple and efficient mechanism in the TSDB to track and identify stale series. |
| 32 | +* Compact the stale series when they reach a certain configurable threshold (% of total series). |
| 33 | + |
| 34 | +## Non-Goals |
| 35 | + |
| 36 | +* Preventing cardinality at source |
| 37 | +* Detailing out how exactly we will compact the stale series once we have identified it. That will be some implementation detail when we get to it. |
| 38 | + |
| 39 | +## How |
| 40 | + |
| 41 | +### Tracking Stale Series |
| 42 | + |
| 43 | +Scraper already puts staleness markers (a [unique sample value](https://github.com/prometheus/prometheus/blob/c3276ea40c2241b85ee35da30048bb6fc4b6d63b/model/value/value.go#L28) to identify stale series) for series that stopped giving samples or targets that disappeared. We also store the [lastValue](https://github.com/prometheus/prometheus/blob/c3276ea40c2241b85ee35da30048bb6fc4b6d63b/tsdb/head.go#L2177) for every series, allowing us to identify stale series without any additional overhead in memory. While there can be edge cases (e.g. during restarts) where we missed putting staleness markers, this should cover most of the use cases while keeping the code very simple. |
| 44 | + |
| 45 | +We can keep a running counter that tracks how many series are stale at the moment. Incremented or decremented based on the incoming sample and the last sample of the series. |
| 46 | + |
| 47 | +### Compacting Stale Series |
| 48 | + |
| 49 | +We will have two thresholds to trigger stale series compaction, `p%` and `q%`, `q > p` (both indicating % of total series that are stale in the head). Both will be configurable and default to 0% (meaning stale series compaction is disabled). |
| 50 | + |
| 51 | +**Part 1** |
| 52 | + |
| 53 | +At a regular interval (say 15 mins), we check if the stale series have crossed p% of the total series. If it has, we trigger a compaction that simply flushes these stale series into a block and removes it from the Head block (can be more than one block if the series crosses the block boundary). We skip WAL truncation and m-map files truncation at this stage and let the usual compaction cycle handle it. How we drop these compacted series during WAL replay is TBD during implementation (may need a new WAL record or use tombstone records). |
| 54 | + |
| 55 | +Since these are stale series, there won’t be any races when compacting it in most cases. We will still lock the series and take required measures so that we don’t cause race with an incoming sample for any stale series. |
| 56 | + |
| 57 | +This way of compaction will make it more predictable when the stale series compaction happens. |
| 58 | + |
| 59 | +**Part 2** |
| 60 | + |
| 61 | +To be more reactive to sudden rise in stale series, we will perform the stale series compaction as soon as the stale series crosses the higher q% threshold. |
| 62 | + |
| 63 | +To avoid back to back stale series compactions, we can choose to have a cooldown period after a stale series compaction where it does not trigger again (e.g. 5-10 mins). |
| 64 | + |
| 65 | +Implementation detail: if the usual head compaction is about to happen very soon, we should skip the stale series compaction and simply wait for the usual head compaction. The buffer can be hardcoded. |
| 66 | + |
| 67 | +## Alternatives |
| 68 | + |
| 69 | +### Do nothing |
| 70 | + |
| 71 | +Pros: |
| 72 | + |
| 73 | +* No added complexity of dealing with potential races and other logic around compaction of stale series. |
| 74 | +* We avoid overlapping blocks created due to stale series which may have hurt some performance for queries (but at the end it is opt-in). |
| 75 | + |
| 76 | +Cons: |
| 77 | + |
| 78 | +* Prometheus remains limited on how well it can handle churn at large scale, making it a less attractive choice for some users. |
| 79 | + |
| 80 | +### Alternative for tracking stale series |
| 81 | + |
| 82 | +Consider when was the last sample scraped *in addition to* the above proposal. |
| 83 | + |
| 84 | +For edge cases where we did not put the staleness markers, we can look at the difference between the last sample timestamp of the series and the max time of the head block, and if it crosses a threshold, call it stale. For example a series did not get a sample for 5 mins (i.e. head’s max time is 5 mins more than series’ last sample timestamp). |
| 85 | + |
| 86 | +Pros over only the above proposal: |
| 87 | +* Covers the edge cases that is not caught by just staleness markers |
| 88 | + |
| 89 | +Cons over only the above proposal: |
| 90 | +* Will have to scan all series periodically to identify how many stale series we have. Can be expensive if we have too many series. |
| 91 | + |
| 92 | +Purely because of the added complexity of proposal 2, we can start with proposal 1 and consider proposal 2 as a follow up in the future. |
| 93 | + |
| 94 | +### Alternative for stale series compaction |
| 95 | + |
| 96 | +Only do either part 1 or part 2 from the above proposal. PS: doing both is not a big jump in work and probably a good tradeoff. |
| 97 | + |
| 98 | +## Action Plan |
| 99 | + |
| 100 | +- [ ] Implement staleness tracking with appropriate metrics. |
| 101 | +- [ ] Implement stale series compaction. |
| 102 | + |
| 103 | +# Future Consideration |
| 104 | + |
| 105 | +* Dynamic adjustment of the thresholds based on memory pressure. |
| 106 | + |
| 107 | +# Consensus |
| 108 | + |
0 commit comments