github.com/thanos-io/thanos@v0.32.5/docs/operating/compactor-backlog.md (about)

     1  # Troubleshoot Compactor Backlog
     2  
     3  The compactor is one of the most important components in Thanos. It is responsible for doing compaction, downsampling, and retention of the data in the object storage.
     4  
     5  When your system contains a lot of block producers (Sidecar, Rule, Receiver, etc) or the scale is large, the compactor might not be able to keep up with the data producing rate and it falls behind, which causes a lot of backlogged work. This document will help you to troubleshoot the backlog compaction issue and how to scale the compactor.
     6  
     7  ## Detect the backlog
     8  
     9  Self-monitoring for the monitoring system is important. We highly recommend you set up the Thanos Grafana dashboards and alerts to monitor the Thanos components. Without self-monitoring, it is hard to detect the issue and fix the problems.
    10  
    11  If you find these issues in your own Thanos deployment, then your compactor might be backlogged and needs to scale more:
    12  1. The long-term query in your Grafana dashboard is much slower than usual.
    13  2. The compactor stops downsampling blocks and `thanos_compact_downsample_total` metric doesn't increase.
    14  3. The compactor stops retention. If the retention period is set to 1 month but you can still see older blocks from the bucket UI then that's the case.
    15  4. `thanos_compact_iterations_total` metric doesn't increase or the rate is very low.
    16  
    17  In the current implementation, the compactor will perform compaction, downsampling, and retention phases in order, which means that if the compaction work is not finished, the downsampling and retention phase won't start. So that's why you will find symptom 2 and 3 mentioned above.
    18  
    19  For symptom 4, `thanos_compact_iterations_total` metric doesn't increase means that the Thanos compactor is currently working on the current compaction iteration and cannot finish it in a long time. It is very similar to the case of a message queue. The producers are the components that upload blocks to your object storage. And the compactor is the consumer to deal with the jobs from producers. If the data production rate is higher than the processing rate of the consumer, then the compactor will fall behind.
    20  
    21  ### Compactor progress metrics
    22  
    23  Since the Thanos v0.24 release, four new metrics `thanos_compact_todo_compactions`, `thanos_compact_todo_compaction_blocks`, `thanos_compact_todo_downsample_blocks` and `thanos_compact_todo_deletion_blocks` are added to show the compaction, downsampling and retention progress and backlog.
    24  
    25  `thanos_compact_todo_compactions`: The number of planned compactions. `thanos_compact_todo_compaction_blocks`: The number of blocks that are planned to be compacted. `thanos_compact_todo_downsample_blocks`: The number of blocks that are queued to be downsampled. `thanos_compact_todo_deletion_blocks`: The number of blocks that are queued for retention.
    26  
    27  To use these metrics, for example you can use `sum(thanos_compact_todo_compactions)` to get the overall compaction backlog or use `sum(thanos_compact_todo_compactions) by (group)` to get which compaction group is the slowest one.
    28  
    29  ![compaction-progress](../img/compaction_progress_metrics.png)
    30  
    31  This feature works by syncing block metadata from the object storage every 5 minutes by default and then simulating the compaction planning process to calculate how much work needs to be done. You can change the default 5m interval by setting `compact.progress-interval` flag or disable it by setting `compact.progress-interval=0`.
    32  
    33  ## Solutions
    34  
    35  ### Scale the compactor
    36  
    37  To prevent the compactor from falling behind, you can scale the compactor.
    38  
    39  To scale the compactor vertically, you can give it more CPU and memory resources. It also has two flags `compact.concurrency` and `downsample.concurrency` to control the number of workers to do compaction and downsampling.
    40  
    41  To scale the compactor horizontally, you can run multiple compactor instances with different `min-time` and `max-time` flags so that each compactor will only work on the blocks that are within the time range.
    42  
    43  Another way of horizontal scaling is to run multiple compactors by sharding. You can use the relabel config to shard the compactors by external labels like `cluster` so that blocks with the same external label will be processed by the same compactor.
    44  
    45  ### Bucket tools
    46  
    47  There are some bucket tools that can help you to troubleshoot and solve this issue.
    48  
    49  You can use `bucket ls`, `bucket inspect` and `bucket web` UI to view your current blocks.
    50  
    51  If a lot of blocks are older than the retention period and the compactor is not performing retention, then to clean them up, you can do the following:
    52  1. Stop the current running compactor instances to avoid data race
    53  2. Run `thanos tools bucket retention` command to perform retention on your blocks directly. This step won't delete your blocks right away. It just adds deletion markers to the blocks for retention.
    54  3. You can restart the compactor instances now and they will clean up the blocks with deletion markers after some period (by default 2d).
    55  4. You can also choose to not restart the compactor instances and just run `thanos tools bucket cleanup --delete-delay=0s` to delete those marked blocks right away.