MCPcopy
hub / github.com/prometheus/prometheus / TestDeleteCompactionBlockAfterFailedReload

Function TestDeleteCompactionBlockAfterFailedReload

tsdb/compact_test.go:1447–1516  ·  view source on GitHub ↗

TestDeleteCompactionBlockAfterFailedReload ensures that a failed reloadBlocks immediately after a compaction deletes the resulting block to avoid creating blocks with the same time range.

(t *testing.T)

Source from the content-addressed store, hash-verified

1445// TestDeleteCompactionBlockAfterFailedReload ensures that a failed reloadBlocks immediately after a compaction
1446// deletes the resulting block to avoid creating blocks with the same time range.
1447func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) {
1448 t.Parallel()
1449 tests := map[string]func(*DB) int{
1450 "Test Head Compaction": func(db *DB) int {
1451 rangeToTriggerCompaction := db.compactor.(*LeveledCompactor).ranges[0]/2*3 - 1
1452 defaultLabel := labels.FromStrings("foo", "bar")
1453
1454 // Add some data to the head that is enough to trigger a compaction.
1455 app := db.Appender(context.Background())
1456 _, err := app.Append(0, defaultLabel, 1, 0)
1457 require.NoError(t, err)
1458 _, err = app.Append(0, defaultLabel, 2, 0)
1459 require.NoError(t, err)
1460 _, err = app.Append(0, defaultLabel, 3+rangeToTriggerCompaction, 0)
1461 require.NoError(t, err)
1462 require.NoError(t, app.Commit())
1463
1464 return 0
1465 },
1466 "Test Block Compaction": func(db *DB) int {
1467 blocks := []*BlockMeta{
1468 {MinTime: 0, MaxTime: 100},
1469 {MinTime: 100, MaxTime: 150},
1470 {MinTime: 150, MaxTime: 200},
1471 }
1472 for _, m := range blocks {
1473 createBlock(t, db.Dir(), genSeries(1, 1, m.MinTime, m.MaxTime))
1474 }
1475 require.NoError(t, db.reload())
1476 require.Len(t, db.Blocks(), len(blocks), "unexpected block count after a reloadBlocks")
1477
1478 return len(blocks)
1479 },
1480 }
1481
1482 for title, bootStrap := range tests {
1483 t.Run(title, func(t *testing.T) {
1484 ctx := context.Background()
1485
1486 db := newTestDB(t, withRngs(1, 100))
1487 db.DisableCompactions()
1488
1489 expBlocks := bootStrap(db)
1490
1491 // Create a block that will trigger the reloadBlocks to fail.
1492 blockPath := createBlock(t, db.Dir(), genSeries(1, 1, 200, 300))
1493 lastBlockIndex := path.Join(blockPath, indexFilename)
1494 actBlocks, err := blockDirs(db.Dir())
1495 require.NoError(t, err)
1496 require.Equal(t, expBlocks, len(actBlocks)-1) // -1 to exclude the corrupted block.
1497 require.NoError(t, os.RemoveAll(lastBlockIndex)) // Corrupt the block by removing the index file.
1498
1499 require.Equal(t, 0.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "initial 'failed db reloadBlocks' count metrics mismatch")
1500 require.Equal(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.Ran), "initial `compactions` count metric mismatch")
1501 require.Equal(t, 0.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "initial `compactions failed` count metric mismatch")
1502
1503 // Do the compaction and check the metrics.
1504 // Compaction should succeed, but the reloadBlocks should fail and

Callers

nothing calls this directly

Calls 15

FromStringsFunction · 0.92
createBlockFunction · 0.85
genSeriesFunction · 0.85
newTestDBFunction · 0.85
withRngsFunction · 0.85
blockDirsFunction · 0.85
DisableCompactionsMethod · 0.80
AppenderMethod · 0.65
AppendMethod · 0.65
CommitMethod · 0.65
LenMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…