github.com/grafana/pyroscope@v1.18.0/pkg/metastore/compaction/compactor/compactor_config.go (about)

     1  package compactor
     2  
     3  import (
     4  	"flag"
     5  	"time"
     6  )
     7  
     8  type Config struct {
     9  	Levels []LevelConfig
    10  
    11  	CleanupBatchSize   int32
    12  	CleanupDelay       time.Duration
    13  	CleanupJobMinLevel int32
    14  	CleanupJobMaxLevel int32
    15  }
    16  
    17  type LevelConfig struct {
    18  	MaxBlocks uint
    19  	MaxAge    int64
    20  }
    21  
    22  func DefaultConfig() Config {
    23  	return Config{
    24  		Levels: []LevelConfig{
    25  			{MaxBlocks: 20, MaxAge: int64(1 * 36 * time.Second)},
    26  			{MaxBlocks: 10, MaxAge: int64(2 * 360 * time.Second)},
    27  			{MaxBlocks: 10, MaxAge: int64(3 * 3600 * time.Second)},
    28  		},
    29  
    30  		CleanupBatchSize:   2,
    31  		CleanupDelay:       15 * time.Minute,
    32  		CleanupJobMaxLevel: 1,
    33  		CleanupJobMinLevel: 0,
    34  	}
    35  }
    36  
    37  func (c *Config) RegisterFlagsWithPrefix(string, *flag.FlagSet) {
    38  	// NOTE(kolesnikovae): I'm not sure if making this configurable
    39  	// is a good idea; however, we might want to add a flag to tune
    40  	// the parameters based on e.g., segment size or max duration.
    41  	*c = DefaultConfig()
    42  }
    43  
    44  // exceedsSize is called after the block has been added to the batch.
    45  // If the function returns true, the batch is flushed to the global
    46  // queue and becomes available for compaction.
    47  func (c *Config) exceedsMaxSize(b *batch) bool {
    48  	return uint(b.size) >= c.maxBlocks(b.staged.key.level)
    49  }
    50  
    51  // exceedsAge reports whether the batch update time is older than the
    52  // maximum age for the level threshold. The function is used in two
    53  // cases: if the batch is not flushed to the global queue and is the
    54  // oldest one, or if the batch is flushed (and available to the planner)
    55  // but the job plan is not complete yet.
    56  func (c *Config) exceedsMaxAge(b *batch, now int64) bool {
    57  	if m := c.maxAge(b.staged.key.level); m > 0 {
    58  		age := now - b.createdAt
    59  		return age > m
    60  	}
    61  	return false
    62  }
    63  
    64  func (c *Config) maxBlocks(l uint32) uint {
    65  	if l < uint32(len(c.Levels)) {
    66  		return c.Levels[l].MaxBlocks
    67  	}
    68  	return 0
    69  }
    70  
    71  func (c *Config) maxAge(l uint32) int64 {
    72  	if l < uint32(len(c.Levels)) {
    73  		return c.Levels[l].MaxAge
    74  	}
    75  	return 0
    76  }
    77  
    78  func (c *Config) maxLevel() uint32 {
    79  	// Assuming that there is at least one level.
    80  	return uint32(len(c.Levels) - 1)
    81  }