Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blooms): Ship chunkrefs in task payload #13677

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only download each TSDB once
  • Loading branch information
salvacorts committed Jul 26, 2024
commit 7d9c55889ce35d25384a403c429633c2a99d9715
24 changes: 10 additions & 14 deletions pkg/bloombuild/common/tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
Expand All @@ -30,6 +29,11 @@ const (
gzipExtension = ".gz"
)

type ClosableForSeries interface {
sharding.ForSeries
Close() error
}

type TSDBStore interface {
UsersForPeriod(ctx context.Context, table config.DayTable) ([]string, error)
ResolveTSDBs(ctx context.Context, table config.DayTable, tenant string) ([]tsdb.SingleTenantTSDBIdentifier, error)
Expand All @@ -38,8 +42,7 @@ type TSDBStore interface {
table config.DayTable,
tenant string,
id tsdb.Identifier,
bounds v1.FingerprintBounds,
) (iter.Iterator[*v1.Series], error)
) (ClosableForSeries, error)
}

// BloomTSDBStore is a wrapper around the storage.Client interface which
Expand Down Expand Up @@ -90,8 +93,7 @@ func (b *BloomTSDBStore) LoadTSDB(
table config.DayTable,
tenant string,
id tsdb.Identifier,
bounds v1.FingerprintBounds,
) (iter.Iterator[*v1.Series], error) {
) (ClosableForSeries, error) {
withCompression := id.Name() + gzipExtension

data, err := b.storage.GetUserFile(ctx, table.Addr(), tenant, withCompression)
Expand All @@ -118,13 +120,8 @@ func (b *BloomTSDBStore) LoadTSDB(
}

idx := tsdb.NewTSDBIndex(reader)
defer func() {
if err := idx.Close(); err != nil {
level.Error(b.logger).Log("msg", "failed to close index", "err", err)
}
}()

return NewTSDBSeriesIter(ctx, tenant, idx, bounds)
return idx, nil
}

func NewTSDBSeriesIter(ctx context.Context, user string, f sharding.ForSeries, bounds v1.FingerprintBounds) (iter.Iterator[*v1.Series], error) {
Expand Down Expand Up @@ -251,12 +248,11 @@ func (s *TSDBStores) LoadTSDB(
table config.DayTable,
tenant string,
id tsdb.Identifier,
bounds v1.FingerprintBounds,
) (iter.Iterator[*v1.Series], error) {
) (ClosableForSeries, error) {
store, err := s.storeForPeriod(table.DayTime)
if err != nil {
return nil, err
}

return store.LoadTSDB(ctx, table, tenant, id, bounds)
return store.LoadTSDB(ctx, table, tenant, id)
}
20 changes: 18 additions & 2 deletions pkg/bloombuild/planner/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func (p *Planner) findOutdatedGaps(
return nil, nil
}

work, err := blockPlansForGaps(ctx, tenant, table, p.tsdbStore, tsdbsWithGaps, metas)
work, err := blockPlansForGaps(ctx, tenant, table, p.tsdbStore, tsdbsWithGaps, metas, logger)
if err != nil {
level.Error(logger).Log("msg", "failed to create plan", "err", err)
return nil, fmt.Errorf("failed to create plan: %w", err)
Expand Down Expand Up @@ -750,10 +750,26 @@ func blockPlansForGaps(
store common.TSDBStore,
tsdbs []tsdbGaps,
metas []bloomshipper.Meta,
logger log.Logger,
) ([]blockPlan, error) {
plans := make([]blockPlan, 0, len(tsdbs))

openTSDBs := make([]common.ClosableForSeries, 0, len(tsdbs))
defer func() {
for _, tsdb := range openTSDBs {
if err := tsdb.Close(); err != nil {
level.Error(logger).Log("msg", "failed to close index", "err", err)
}
}
}()

for _, idx := range tsdbs {
tsdb, err := store.LoadTSDB(ctx, table, tenant, idx.tsdb)
if err != nil {
return nil, fmt.Errorf("failed to load tsdb: %w", err)
}
openTSDBs = append(openTSDBs, tsdb)

plan := blockPlan{
tsdb: idx.tsdb,
gaps: make([]protos.Gap, 0, len(idx.gaps)),
Expand All @@ -764,7 +780,7 @@ func blockPlansForGaps(
Bounds: gap,
}

seriesItr, err := store.LoadTSDB(ctx, table, tenant, idx.tsdb, gap)
seriesItr, err := common.NewTSDBSeriesIter(ctx, tenant, tsdb, gap)
if err != nil {
return nil, fmt.Errorf("failed to load series from TSDB for gap (%s): %w", gap.String(), err)
}
Expand Down
36 changes: 33 additions & 3 deletions pkg/bloombuild/planner/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"google.golang.org/grpc"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper"
bloomshipperconfig "github.com/grafana/loki/v3/pkg/storage/stores/shipper/bloomshipper/config"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb"
"github.com/grafana/loki/v3/pkg/storage/stores/shipper/indexshipper/tsdb/index"
"github.com/grafana/loki/v3/pkg/storage/types"
"github.com/grafana/loki/v3/pkg/util/mempool"
)
Expand Down Expand Up @@ -394,6 +396,7 @@ func Test_blockPlansForGaps(t *testing.T) {
tsdbStore,
gaps,
tc.metas,
log.NewNopLogger(),
)
if tc.err {
require.Error(t, err)
Expand Down Expand Up @@ -433,14 +436,41 @@ func newFakeTsdbStore(series []*v1.Series) *fakeTsdbStore {
}
}

func (f *fakeTsdbStore) LoadTSDB(_ context.Context, _ config.DayTable, _ string, _ tsdb.Identifier, bounds v1.FingerprintBounds) (iter.Iterator[*v1.Series], error) {
func (f *fakeTsdbStore) LoadTSDB(_ context.Context, _ config.DayTable, _ string, _ tsdb.Identifier) (common.ClosableForSeries, error) {
return &fakeForSeries{series: f.series}, nil
}

type fakeForSeries struct {
series []*v1.Series
}

func (f fakeForSeries) ForSeries(_ context.Context, _ string, ff index.FingerprintFilter, _ model.Time, _ model.Time, fn func(labels.Labels, model.Fingerprint, []index.ChunkMeta) (stop bool), _ ...*labels.Matcher) error {
overlapping := make([]*v1.Series, 0, len(f.series))
for _, s := range f.series {
if bounds.Match(s.Fingerprint) {
if ff.Match(s.Fingerprint) {
overlapping = append(overlapping, s)
}
}
return iter.NewSliceIter(overlapping), nil

for _, s := range overlapping {
chunks := make([]index.ChunkMeta, 0, len(s.Chunks))
for _, c := range s.Chunks {
chunks = append(chunks, index.ChunkMeta{
MinTime: int64(c.From),
MaxTime: int64(c.Through),
Checksum: c.Checksum,
})
}

if fn(labels.EmptyLabels(), s.Fingerprint, chunks) {
break
}
}
return nil
}

func (f fakeForSeries) Close() error {
return nil
}

func createTasks(n int, resultsCh chan *protos.TaskResult) []*QueueTask {
Expand Down
Loading