MCPcopy
hub / github.com/prometheus/prometheus / TestDelayedCompaction

Function TestDelayedCompaction

tsdb/compact_test.go:1989–2111  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1987}
1988
1989func TestDelayedCompaction(t *testing.T) {
1990 delay := 5 * time.Second
1991 label := labels.FromStrings("foo", "bar")
1992
1993 appendSamples := func(t *testing.T, db *DB, timestamps ...int64) {
1994 app := db.Appender(context.Background())
1995 for _, ts := range timestamps {
1996 _, err := app.Append(0, label, ts, 0)
1997 require.NoError(t, err)
1998 }
1999 require.NoError(t, app.Commit())
2000 }
2001
2002 compactorRanCount := func(db *DB) float64 {
2003 return prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.Ran)
2004 }
2005
2006 t.Run("delay not enabled", func(t *testing.T) {
2007 t.Parallel()
2008 db := newTestDB(t, withRngs(10))
2009
2010 compactAndCheck := func() {
2011 start := time.Now()
2012 db.Compact(context.Background())
2013 require.False(t, db.head.compactable())
2014 require.Less(t, time.Since(start), delay)
2015 }
2016
2017 db.DisableCompactions()
2018 appendSamples(t, db, 0, 11, 21)
2019 compactAndCheck()
2020 require.Equal(t, 1.0, compactorRanCount(db))
2021
2022 db.DisableCompactions()
2023 appendSamples(t, db, 31, 41)
2024 compactAndCheck()
2025 require.Equal(t, 3.0, compactorRanCount(db))
2026 })
2027
2028 t.Run("delay enabled", func(t *testing.T) {
2029 synctest.Test(t, func(t *testing.T) {
2030 db := newTestDB(t, withOpts(&Options{CompactionDelay: delay}), withRngs(10))
2031
2032 getDelayStart := func() time.Time {
2033 db.cmtx.Lock()
2034 defer db.cmtx.Unlock()
2035 return db.timeWhenCompactionDelayStarted
2036 }
2037
2038 // triggerAutoCompaction sends a compaction trigger and waits for it to be processed.
2039 triggerAutoCompaction := func() {
2040 db.compactc <- struct{}{}
2041 synctest.Wait()
2042 }
2043
2044 // First compaction: expect 1 block.
2045 db.DisableCompactions()
2046 appendSamples(t, db, 0, 11, 21)

Callers

nothing calls this directly

Calls 15

FromStringsFunction · 0.92
newTestDBFunction · 0.85
withRngsFunction · 0.85
withOptsFunction · 0.85
NewRangeHeadFunction · 0.85
compactableMethod · 0.80
DisableCompactionsMethod · 0.80
LockMethod · 0.80
EnableCompactionsMethod · 0.80
CompactHeadMethod · 0.80
HeadMethod · 0.80
AppenderMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…