github.com/grafana/pyroscope@v1.18.0/pkg/compactor/split_merge_planner.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  // Provenance-includes-location: https://github.com/grafana/mimir/blob/main/pkg/compactor/split_merge_planner.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: The Cortex Authors.
     5  
     6  package compactor
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  
    12  	"github.com/grafana/pyroscope/pkg/phlaredb/block"
    13  )
    14  
    15  type SplitAndMergePlanner struct {
    16  	ranges []int64
    17  }
    18  
    19  func NewSplitAndMergePlanner(ranges []int64) *SplitAndMergePlanner {
    20  	return &SplitAndMergePlanner{
    21  		ranges: ranges,
    22  	}
    23  }
    24  
    25  // Plan implements compact.Planner.
    26  func (c *SplitAndMergePlanner) Plan(_ context.Context, metasByMinTime []*block.Meta) ([]*block.Meta, error) {
    27  	// The split-and-merge grouper creates single groups of blocks that are expected to be
    28  	// compacted together, so there's no real planning to do here (reason why this function is
    29  	// just a pass-through). However, we want to run extra checks before proceeding.
    30  	if len(metasByMinTime) == 0 {
    31  		return metasByMinTime, nil
    32  	}
    33  
    34  	// Ensure all blocks fits within the largest range. This is a double check
    35  	// to ensure there's no bug in the previous blocks grouping, given this Plan()
    36  	// is just a pass-through.
    37  	largestRange := c.ranges[len(c.ranges)-1]
    38  	rangeStart := getRangeStart(metasByMinTime[0], largestRange)
    39  	rangeEnd := rangeStart + largestRange
    40  
    41  	for _, b := range metasByMinTime {
    42  		if int64(b.MinTime) < rangeStart || int64(b.MaxTime) > rangeEnd {
    43  			return nil, fmt.Errorf("block %s with time range %d:%d is outside the largest expected range %d:%d",
    44  				b.ULID.String(),
    45  				b.MinTime,
    46  				b.MaxTime,
    47  				rangeStart,
    48  				rangeEnd)
    49  		}
    50  	}
    51  
    52  	return metasByMinTime, nil
    53  }