Skip to content

Commit 52cb78e

Browse files
committed
fix: use slices.Contains() instead of loop
Signed-off-by: Joel Verezhak <[email protected]>
1 parent 1904feb commit 52cb78e

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

convert/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ type converter struct {
214214

215215
labelPageBufferSize int
216216
chunkPageBufferSize int
217-
217+
218218
sourceBlocks []string // ULIDs of source TSDB blocks
219219
}
220220

convert/lineage.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package convert
66

77
import (
88
"log/slog"
9+
"slices"
910
"time"
1011

1112
"github.com/thanos-io/thanos/pkg/block/metadata"
@@ -31,25 +32,23 @@ func (blc *BlockLineageChecker) IsRedundantConversion(
3132
parquetMetas map[string]schema.Meta,
3233
) bool {
3334
blockULID := tsdbBlock.ULID.String()
34-
35+
3536
// Check if this exact TSDB block has already been used as source for any parquet block
3637
for _, parquetMeta := range parquetMetas {
37-
for _, sourceBlock := range parquetMeta.SourceBlocks {
38-
if sourceBlock == blockULID {
39-
blc.logger.Info("Block already converted",
40-
"tsdb_block", blockULID,
41-
"parquet_block", parquetMeta.Name,
42-
"day", day.Format("2006-01-02"))
43-
return true
44-
}
38+
if slices.Contains(parquetMeta.SourceBlocks, blockULID) {
39+
blc.logger.Info("Block already converted",
40+
"tsdb_block", blockULID,
41+
"parquet_block", parquetMeta.Name,
42+
"day", day.Format("2006-01-02"))
43+
return true
4544
}
4645
}
47-
46+
4847
// Check if this is a compacted block and its constituent blocks have been converted
4948
if len(tsdbBlock.BlockMeta.Compaction.Parents) > 0 {
5049
return blc.isCompactedBlockRedundant(tsdbBlock, day, parquetMetas)
5150
}
52-
51+
5352
return false
5453
}
5554

@@ -64,29 +63,29 @@ func (blc *BlockLineageChecker) isCompactedBlockRedundant(
6463
for _, parent := range compactedBlock.BlockMeta.Compaction.Parents {
6564
parentULIDs[parent.ULID.String()] = false // false = not found in parquet blocks
6665
}
67-
66+
6867
// Check if all parent blocks have been used as sources for parquet blocks
6968
// covering the same day
7069
dayStart := util.BeginOfDay(day)
7170
dayEnd := util.EndOfDay(day)
72-
71+
7372
for _, parquetMeta := range parquetMetas {
7473
// Only consider parquet blocks that overlap with the day we're checking
7574
parquetStart := time.UnixMilli(parquetMeta.Mint)
7675
parquetEnd := time.UnixMilli(parquetMeta.Maxt)
77-
76+
7877
if parquetEnd.Before(dayStart) || parquetStart.After(dayEnd) {
7978
continue // No overlap with the day
8079
}
81-
80+
8281
// Mark any parent blocks found in this parquet block's sources
8382
for _, sourceBlock := range parquetMeta.SourceBlocks {
8483
if _, exists := parentULIDs[sourceBlock]; exists {
8584
parentULIDs[sourceBlock] = true
8685
}
8786
}
8887
}
89-
88+
9089
// Check if all parent blocks covering this day have been converted
9190
allParentsConverted := true
9291
for parentULID, found := range parentULIDs {
@@ -99,15 +98,15 @@ func (blc *BlockLineageChecker) isCompactedBlockRedundant(
9998
allParentsConverted = false
10099
}
101100
}
102-
101+
103102
if allParentsConverted && len(parentULIDs) > 0 {
104103
blc.logger.Info("Compacted block is redundant - all parent blocks already converted",
105104
"compacted_block", compactedBlock.ULID.String(),
106105
"parent_count", len(parentULIDs),
107106
"day", day.Format("2006-01-02"))
108107
return true
109108
}
110-
109+
111110
return false
112111
}
113112

@@ -122,24 +121,21 @@ func (blc *BlockLineageChecker) GetConflictingParquetBlocks(
122121
blockULID := tsdbBlock.ULID.String()
123122
dayStart := util.BeginOfDay(day)
124123
dayEnd := util.EndOfDay(day)
125-
124+
126125
for parquetName, parquetMeta := range parquetMetas {
127126
// Check if parquet block overlaps with the day
128127
parquetStart := time.UnixMilli(parquetMeta.Mint)
129128
parquetEnd := time.UnixMilli(parquetMeta.Maxt)
130-
129+
131130
if parquetEnd.Before(dayStart) || parquetStart.After(dayEnd) {
132131
continue // No overlap
133132
}
134-
133+
135134
// Check if this parquet block was created from the same TSDB block
136-
for _, sourceBlock := range parquetMeta.SourceBlocks {
137-
if sourceBlock == blockULID {
138-
conflicts = append(conflicts, parquetName)
139-
break
140-
}
135+
if slices.Contains(parquetMeta.SourceBlocks, blockULID) {
136+
conflicts = append(conflicts, parquetName)
141137
}
142138
}
143-
139+
144140
return conflicts
145141
}

convert/plan.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ type Plan struct {
2222

2323
type Planner struct {
2424
// do not create parquet blocks that are younger then this
25-
notAfter time.Time
26-
lineageChecker *BlockLineageChecker
27-
skipRedundant bool
25+
notAfter time.Time
26+
lineageChecker *BlockLineageChecker
27+
skipRedundant bool
2828
}
2929

3030
func NewPlannerWithOptions(notAfter time.Time, skipRedundant bool) Planner {
@@ -124,18 +124,18 @@ func (p Planner) overlappingBlockMetasWithLineage(
124124
parquetMetas map[string]schema.Meta,
125125
) []metadata.Meta {
126126
candidates := overlappingBlockMetas(metas, date)
127-
127+
128128
if !p.skipRedundant {
129129
return candidates // Return all candidates if lineage checking is disabled
130130
}
131-
131+
132132
filtered := make([]metadata.Meta, 0)
133-
133+
134134
for _, meta := range candidates {
135135
if !p.lineageChecker.IsRedundantConversion(meta, date, parquetMetas) {
136136
filtered = append(filtered, meta)
137137
}
138138
}
139-
139+
140140
return filtered
141141
}

0 commit comments

Comments
 (0)