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

Build 24h blocks for older OOO data #751

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
27 changes: 23 additions & 4 deletions tsdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,6 @@ func (db *DB) compactOOOHead(ctx context.Context) error {
return fmt.Errorf("truncate ooo wbl: %w", err)
}
}

codesome marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Expand All @@ -1503,14 +1502,34 @@ func (db *DB) compactOOO(dest string, oooHead *OOOCompactionHead) (_ []ulid.ULID

meta := &BlockMeta{}
meta.Compaction.SetOutOfOrder()
for t := blockSize * (oooHeadMint / blockSize); t <= oooHeadMaxt; t += blockSize {
mint, maxt := t, t+blockSize
runCompaction := func(mint, maxt int64) error {
// Block intervals are half-open: [b.MinTime, b.MaxTime). Block intervals are always +1 than the total samples it includes.
uids, err := db.compactor.Write(dest, oooHead.CloneForTimeRange(mint, maxt-1), mint, maxt, meta)
if err != nil {
return nil, err
return err
}
ulids = append(ulids, uids...)
return nil
}

day := 24 * time.Hour.Milliseconds()
maxtFor24hBlock := day * (db.Head().MaxTime() / day)

// 24h blocks for data that is for the previous days
for t := day * (oooHeadMint / day); t < maxtFor24hBlock; t += day {
if err := runCompaction(t, t+day); err != nil {
return nil, err
}
}

oooStart := oooHeadMint
if oooStart < maxtFor24hBlock {
oooStart = maxtFor24hBlock
}
for t := blockSize * (oooStart / blockSize); t <= oooHeadMaxt; t += blockSize {
if err := runCompaction(t, t+blockSize); err != nil {
return nil, err
}
}

if len(ulids) == 0 {
Expand Down
75 changes: 74 additions & 1 deletion tsdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7528,7 +7528,7 @@ func testOutOfOrderRuntimeConfig(t *testing.T, scenario sampleTypeScenario) {
require.Positive(t, size)

require.Empty(t, db.Blocks())
require.NoError(t, db.compactOOOHead(ctx))
require.NoError(t, db.CompactOOOHead(ctx))
require.NotEmpty(t, db.Blocks())

// WBL is empty.
Expand Down Expand Up @@ -9087,3 +9087,76 @@ func TestBlockClosingBlockedDuringRemoteRead(t *testing.T) {
case <-blockClosed:
}
}

func TestBiggerBlocksForOldOOOData(t *testing.T) {
var (
ctx = context.Background()
lbls = labels.FromStrings("foo", "bar")

day = 24 * time.Hour.Milliseconds()
hour = time.Hour.Milliseconds()

currTs = time.Now().UnixMilli()
currDayStart = day * (currTs / day)

expOOOSamples []chunks.Sample
)

opts := DefaultOptions()
opts.OutOfOrderTimeWindow = 10 * day
db := openTestDB(t, opts, nil)
db.DisableCompactions()
t.Cleanup(func() {
require.NoError(t, db.Close())
})

// 1 in-order sample.
app := db.Appender(ctx)
inOrderTs := currDayStart + (6 * hour)
ref, err := app.Append(0, lbls, inOrderTs, float64(inOrderTs))
require.NoError(t, err)
require.NoError(t, app.Commit())

// OOO samples till 5 days ago.
for ts := currDayStart - (5 * day); ts < inOrderTs; ts += hour {
app = db.Appender(ctx)
_, err := app.Append(ref, lbls, ts, float64(ts))
require.NoError(t, err)
require.NoError(t, app.Commit())
expOOOSamples = append(expOOOSamples, sample{t: ts, f: float64(ts)})
}

require.Empty(t, db.Blocks())
require.NoError(t, db.CompactOOOHead(ctx))
// 5 OOO blocks from the last 5 days + 3 OOO blocks for the 6h of curr day (2h blocks)
require.Len(t, db.Blocks(), 8)

// Check that blocks are alright.
// Move all the blocks to a new DB and check for all OOO samples
// getting into the new DB and the old DB only has the in-order sample.
newDB := openTestDB(t, opts, nil)
t.Cleanup(func() {
require.NoError(t, newDB.Close())
})
for _, b := range db.Blocks() {
err := os.Rename(b.Dir(), path.Join(newDB.Dir(), b.Meta().ULID.String()))
require.NoError(t, err)
}

require.NoError(t, db.reloadBlocks())
require.NoError(t, newDB.reloadBlocks())
require.Empty(t, db.Blocks())
require.Len(t, newDB.Blocks(), 8)

// Only in-order sample in the old DB.
querier, err := db.Querier(inOrderTs-6*day, inOrderTs+1)
require.NoError(t, err)
seriesSet := query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar"))
require.Equal(t, map[string][]chunks.Sample{`{foo="bar"}`: {sample{t: inOrderTs, f: float64(inOrderTs)}}}, seriesSet)

// All OOO samples in the new DB.
querier, err = newDB.Querier(inOrderTs-6*day, inOrderTs+1)
require.NoError(t, err)
seriesSet = query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar"))
require.Equal(t, map[string][]chunks.Sample{`{foo="bar"}`: expOOOSamples}, seriesSet)
}
Loading