github.com/thanos-io/thanos@v0.32.5/pkg/block/index_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package block
     5  
     6  import (
     7  	"context"
     8  	"math"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  
    13  	"github.com/go-kit/log"
    14  	"github.com/prometheus/prometheus/model/labels"
    15  	"github.com/prometheus/prometheus/tsdb"
    16  	"github.com/prometheus/prometheus/tsdb/chunks"
    17  	"github.com/prometheus/prometheus/tsdb/index"
    18  
    19  	"github.com/efficientgo/core/testutil"
    20  
    21  	"github.com/thanos-io/thanos/pkg/block/metadata"
    22  	"github.com/thanos-io/thanos/pkg/testutil/e2eutil"
    23  )
    24  
    25  func TestRewrite(t *testing.T) {
    26  	ctx := context.Background()
    27  
    28  	tmpDir := t.TempDir()
    29  
    30  	b, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
    31  		{{Name: "a", Value: "1"}},
    32  		{{Name: "a", Value: "2"}},
    33  		{{Name: "a", Value: "3"}},
    34  		{{Name: "a", Value: "4"}},
    35  		{{Name: "a", Value: "1"}, {Name: "b", Value: "1"}},
    36  	}, 150, 0, 1000, nil, 124, metadata.NoneFunc)
    37  	testutil.Ok(t, err)
    38  
    39  	ir, err := index.NewFileReader(filepath.Join(tmpDir, b.String(), IndexFilename))
    40  	testutil.Ok(t, err)
    41  
    42  	defer func() { testutil.Ok(t, ir.Close()) }()
    43  
    44  	cr, err := chunks.NewDirReader(filepath.Join(tmpDir, b.String(), "chunks"), nil)
    45  	testutil.Ok(t, err)
    46  
    47  	defer func() { testutil.Ok(t, cr.Close()) }()
    48  
    49  	m := &metadata.Meta{
    50  		BlockMeta: tsdb.BlockMeta{ULID: ULID(1)},
    51  		Thanos:    metadata.Thanos{},
    52  	}
    53  
    54  	testutil.Ok(t, os.MkdirAll(filepath.Join(tmpDir, m.ULID.String()), os.ModePerm))
    55  	iw, err := index.NewWriter(ctx, filepath.Join(tmpDir, m.ULID.String(), IndexFilename))
    56  	testutil.Ok(t, err)
    57  	defer iw.Close()
    58  
    59  	cw, err := chunks.NewWriter(filepath.Join(tmpDir, m.ULID.String()))
    60  	testutil.Ok(t, err)
    61  
    62  	defer cw.Close()
    63  
    64  	testutil.Ok(t, rewrite(log.NewNopLogger(), ir, cr, iw, cw, m, []ignoreFnType{func(mint, maxt int64, prev *chunks.Meta, curr *chunks.Meta) (bool, error) {
    65  		return curr.MaxTime == 696, nil
    66  	}}))
    67  
    68  	testutil.Ok(t, iw.Close())
    69  	testutil.Ok(t, cw.Close())
    70  
    71  	ir2, err := index.NewFileReader(filepath.Join(tmpDir, m.ULID.String(), IndexFilename))
    72  	testutil.Ok(t, err)
    73  
    74  	defer func() { testutil.Ok(t, ir2.Close()) }()
    75  
    76  	all, err := ir2.Postings(index.AllPostingsKey())
    77  	testutil.Ok(t, err)
    78  
    79  	for p := ir2.SortedPostings(all); p.Next(); {
    80  		var builder labels.ScratchBuilder
    81  		var chks []chunks.Meta
    82  
    83  		testutil.Ok(t, ir2.Series(p.At(), &builder, &chks))
    84  		testutil.Equals(t, 1, len(chks))
    85  	}
    86  }
    87  
    88  func TestGatherIndexHealthStatsReturnsOutOfOrderChunksErr(t *testing.T) {
    89  	blockDir := t.TempDir()
    90  
    91  	err := e2eutil.PutOutOfOrderIndex(blockDir, 0, math.MaxInt64)
    92  	testutil.Ok(t, err)
    93  
    94  	stats, err := GatherIndexHealthStats(log.NewLogfmtLogger(os.Stderr), blockDir+"/"+IndexFilename, 0, math.MaxInt64)
    95  
    96  	testutil.Ok(t, err)
    97  	testutil.Equals(t, 1, stats.OutOfOrderChunks)
    98  	testutil.NotOk(t, stats.OutOfOrderChunksErr())
    99  }