(t *testing.T)
| 3492 | } |
| 3493 | |
| 3494 | func TestOneCheckpointPerCompactCall(t *testing.T) { |
| 3495 | t.Parallel() |
| 3496 | blockRange := int64(1000) |
| 3497 | opts := &Options{ |
| 3498 | RetentionDuration: blockRange * 1000, |
| 3499 | NoLockfile: true, |
| 3500 | MinBlockDuration: blockRange, |
| 3501 | MaxBlockDuration: blockRange, |
| 3502 | } |
| 3503 | |
| 3504 | ctx := context.Background() |
| 3505 | |
| 3506 | db := newTestDB(t, withOpts(opts)) |
| 3507 | db.DisableCompactions() |
| 3508 | |
| 3509 | // Case 1: Lot's of uncompacted data in Head. |
| 3510 | |
| 3511 | lbls := labels.FromStrings("foo_d", "choco_bar") |
| 3512 | // Append samples spanning 59 block ranges. |
| 3513 | app := db.Appender(context.Background()) |
| 3514 | for i := range int64(60) { |
| 3515 | _, err := app.Append(0, lbls, blockRange*i, rand.Float64()) |
| 3516 | require.NoError(t, err) |
| 3517 | _, err = app.Append(0, lbls, (blockRange*i)+blockRange/2, rand.Float64()) |
| 3518 | require.NoError(t, err) |
| 3519 | // Rotate the WAL file so that there is >3 files for checkpoint to happen. |
| 3520 | _, err = db.head.wal.NextSegment() |
| 3521 | require.NoError(t, err) |
| 3522 | } |
| 3523 | require.NoError(t, app.Commit()) |
| 3524 | |
| 3525 | // Check the existing WAL files. |
| 3526 | first, last, err := wlog.Segments(db.head.wal.Dir()) |
| 3527 | require.NoError(t, err) |
| 3528 | require.Equal(t, 0, first) |
| 3529 | require.Equal(t, 60, last) |
| 3530 | |
| 3531 | require.Equal(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) |
| 3532 | require.NoError(t, db.Compact(ctx)) |
| 3533 | require.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) |
| 3534 | |
| 3535 | // As the data spans for 59 blocks, 58 go to disk and 1 remains in Head. |
| 3536 | require.Len(t, db.Blocks(), 58) |
| 3537 | // Though WAL was truncated only once, head should be truncated after each compaction. |
| 3538 | require.Equal(t, 58.0, prom_testutil.ToFloat64(db.head.metrics.headTruncateTotal)) |
| 3539 | |
| 3540 | // The compaction should have only truncated first 2/3 of WAL (while also rotating the files). |
| 3541 | first, last, err = wlog.Segments(db.head.wal.Dir()) |
| 3542 | require.NoError(t, err) |
| 3543 | require.Equal(t, 40, first) |
| 3544 | require.Equal(t, 61, last) |
| 3545 | |
| 3546 | // The first checkpoint would be for first 2/3rd of WAL, hence till 39. |
| 3547 | // That should be the last checkpoint. |
| 3548 | _, cno, err := wlog.LastCheckpoint(db.head.wal.Dir()) |
| 3549 | require.NoError(t, err) |
| 3550 | require.Equal(t, 39, cno) |
| 3551 |
nothing calls this directly
no test coverage detected
searching dependent graphs…