github.com/grafana/pyroscope@v1.18.0/pkg/compactor/split_merge_planner_test.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_test.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  	"testing"
    12  
    13  	"github.com/oklog/ulid/v2"
    14  	"github.com/stretchr/testify/assert"
    15  
    16  	"github.com/grafana/pyroscope/pkg/phlaredb/block"
    17  )
    18  
    19  func TestSplitAndMergePlanner_Plan(t *testing.T) {
    20  	block1 := ulid.MustNew(1, nil)
    21  	block2 := ulid.MustNew(2, nil)
    22  	block3 := ulid.MustNew(3, nil)
    23  
    24  	tests := map[string]struct {
    25  		ranges          []int64
    26  		blocksByMinTime []*block.Meta
    27  		expectedErr     error
    28  	}{
    29  		"no blocks": {
    30  			ranges:          []int64{20, 40, 60},
    31  			blocksByMinTime: []*block.Meta{},
    32  		},
    33  		"a source block is larger then the largest range": {
    34  			ranges: []int64{20, 40, 60},
    35  			blocksByMinTime: []*block.Meta{
    36  				{ULID: block1, MinTime: 0, MaxTime: 20, Version: block.MetaVersion3},
    37  				{ULID: block2, MinTime: 10, MaxTime: 80, Version: block.MetaVersion3},
    38  				{ULID: block3, MinTime: 12, MaxTime: 15, Version: block.MetaVersion3},
    39  			},
    40  			expectedErr: fmt.Errorf("block %s with time range 10:80 is outside the largest expected range 0:60",
    41  				block2.String()),
    42  		},
    43  		"source blocks are smaller then the largest range but compacted block is larger": {
    44  			ranges: []int64{20, 40, 60},
    45  			blocksByMinTime: []*block.Meta{
    46  				{ULID: block1, MinTime: 10, MaxTime: 20, Version: block.MetaVersion3},
    47  				{ULID: block2, MinTime: 30, MaxTime: 40, Version: block.MetaVersion3},
    48  				{ULID: block3, MinTime: 50, MaxTime: 70, Version: block.MetaVersion3},
    49  			},
    50  			expectedErr: fmt.Errorf("block %s with time range 50:70 is outside the largest expected range 0:60",
    51  				block3.String()),
    52  		},
    53  		"source blocks and compacted block are smaller then the largest range but misaligned": {
    54  			ranges: []int64{20, 40, 60},
    55  			blocksByMinTime: []*block.Meta{
    56  				{ULID: block1, MinTime: 50, MaxTime: 70, Version: block.MetaVersion3},
    57  				{ULID: block2, MinTime: 70, MaxTime: 80, Version: block.MetaVersion3},
    58  			},
    59  			expectedErr: fmt.Errorf("block %s with time range 50:70 is outside the largest expected range 0:60",
    60  				block1.String()),
    61  		},
    62  		"blocks fit within the largest range": {
    63  			ranges: []int64{20, 40, 60},
    64  			blocksByMinTime: []*block.Meta{
    65  				{ULID: block1, MinTime: 10, MaxTime: 20, Version: block.MetaVersion3},
    66  				{ULID: block2, MinTime: 20, MaxTime: 40, Version: block.MetaVersion3},
    67  				{ULID: block3, MinTime: 20, MaxTime: 60, Version: block.MetaVersion3},
    68  			},
    69  		},
    70  	}
    71  
    72  	for testName, testData := range tests {
    73  		t.Run(testName, func(t *testing.T) {
    74  			c := NewSplitAndMergePlanner(testData.ranges)
    75  			actual, err := c.Plan(context.Background(), testData.blocksByMinTime)
    76  			assert.Equal(t, testData.expectedErr, err)
    77  
    78  			if testData.expectedErr == nil {
    79  				// Since the planner is a pass-through we do expect to get the same input blocks on success.
    80  				assert.Equal(t, testData.blocksByMinTime, actual)
    81  			}
    82  		})
    83  	}
    84  }