-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store: add consistency delay to fetch blocks
Signed-off-by: khyatisoneji <[email protected]>
- Loading branch information
1 parent
46a97fd
commit e9ecbd6
Showing
7 changed files
with
158 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package garbagecollector | ||
|
||
import ( | ||
"github.com/oklog/ulid" | ||
"github.com/pkg/errors" | ||
"github.com/thanos-io/thanos/pkg/block" | ||
"github.com/thanos-io/thanos/pkg/block/metadata" | ||
"github.com/thanos-io/thanos/pkg/compact/downsample" | ||
) | ||
|
||
type GarbageBlocksFinder struct{} | ||
|
||
func NewGarbageBlocksFinder() *GarbageBlocksFinder { | ||
return &GarbageBlocksFinder{} | ||
} | ||
|
||
func (s *GarbageBlocksFinder) GarbageBlocks(resolution int64, blocks map[ulid.ULID]*metadata.Meta) (ids []ulid.ULID, err error) { | ||
// Map each block to its highest priority parent. Initial blocks have themselves | ||
// in their source section, i.e. are their own parent. | ||
parents := map[ulid.ULID]ulid.ULID{} | ||
|
||
for id, meta := range blocks { | ||
// Skip any block that has a different resolution. | ||
if meta.Thanos.Downsample.Resolution != resolution { | ||
continue | ||
} | ||
|
||
// For each source block we contain, check whether we are the highest priority parent block. | ||
for _, sid := range meta.Compaction.Sources { | ||
pid, ok := parents[sid] | ||
// No parents for the source block so far. | ||
if !ok { | ||
parents[sid] = id | ||
continue | ||
} | ||
pmeta, ok := blocks[pid] | ||
if !ok { | ||
return nil, errors.Errorf("previous parent block %s not found", pid) | ||
} | ||
// The current block is the higher priority parent for the source if its | ||
// compaction level is higher than that of the previously set parent. | ||
// If compaction levels are equal, the more recent ULID wins. | ||
// | ||
// The ULID recency alone is not sufficient since races, e.g. induced | ||
// by downtime of garbage collection, may re-compact blocks that are | ||
// were already compacted into higher-level blocks multiple times. | ||
level, plevel := meta.Compaction.Level, pmeta.Compaction.Level | ||
|
||
if level > plevel || (level == plevel && id.Compare(pid) > 0) { | ||
parents[sid] = id | ||
} | ||
} | ||
} | ||
|
||
// A block can safely be deleted if they are not the highest priority parent for | ||
// any source block. | ||
topParents := map[ulid.ULID]struct{}{} | ||
for _, pid := range parents { | ||
topParents[pid] = struct{}{} | ||
} | ||
|
||
for id, meta := range blocks { | ||
// Skip any block that has a different resolution. | ||
if meta.Thanos.Downsample.Resolution != resolution { | ||
continue | ||
} | ||
if _, ok := topParents[id]; ok { | ||
continue | ||
} | ||
|
||
ids = append(ids, id) | ||
} | ||
return ids, nil | ||
} | ||
|
||
type GarbageBlocksFilter struct { | ||
garbageBlocksFinder *GarbageBlocksFinder | ||
} | ||
|
||
func NewGarbageBlocksFilter(garbageBlocksFinder *GarbageBlocksFinder) *GarbageBlocksFilter { | ||
return &GarbageBlocksFilter{ | ||
garbageBlocksFinder: garbageBlocksFinder, | ||
} | ||
} | ||
|
||
func (f *GarbageBlocksFilter) Filter(metas map[ulid.ULID]*metadata.Meta, synced block.GaugeLabeled, _ bool) { | ||
for _, res := range []int64{ | ||
downsample.ResLevel0, downsample.ResLevel1, downsample.ResLevel2, | ||
} { | ||
garbageIds, _ := f.garbageBlocksFinder.GarbageBlocks(res, metas) | ||
for _, id := range garbageIds { | ||
delete(metas, id) | ||
} | ||
} | ||
} |