MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestIngester_UserTSDB_BlocksToDelete

Function TestIngester_UserTSDB_BlocksToDelete

pkg/ingester/ingester_test.go:7078–7264  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

7076}
7077
7078func TestIngester_UserTSDB_BlocksToDelete(t *testing.T) {
7079 tempDir := t.TempDir()
7080 db, err := tsdb.Open(tempDir, promslog.NewNopLogger(), prometheus.NewPedanticRegistry(), &tsdb.Options{}, nil)
7081 require.NoError(t, err)
7082
7083 t.Run("should delete all block beyond block retention period and were shipped", func(t *testing.T) {
7084 currentTime := time.Now()
7085 var blocks []*tsdb.Block
7086 block1 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-3*time.Hour).UnixMilli(), currentTime.Add(-2*time.Hour).UnixMilli())
7087 blocks = append(blocks, block1)
7088 block2 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-4*time.Hour).UnixMilli(), currentTime.Add(-3*time.Hour).UnixMilli())
7089 blocks = append(blocks, block2)
7090 block3 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-5*time.Hour).UnixMilli(), currentTime.Add(-4*time.Hour).UnixMilli())
7091 blocks = append(blocks, block3)
7092 block4 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-6*time.Hour).UnixMilli(), currentTime.Add(-5*time.Hour).UnixMilli())
7093 blocks = append(blocks, block4)
7094
7095 shippedBlocks := map[ulid.ULID]struct{}{
7096 block1.Meta().ULID: {},
7097 block2.Meta().ULID: {},
7098 block3.Meta().ULID: {},
7099 block4.Meta().ULID: {},
7100 }
7101 userDB := &userTSDB{
7102 db: db,
7103 shipper: &shipperMock{},
7104 shippedBlocks: shippedBlocks,
7105 blockRetentionPeriod: 2 * time.Hour.Milliseconds(),
7106 }
7107
7108 blocksToDelete := userDB.blocksToDelete(blocks)
7109 require.Equal(t, 4, len(blocksToDelete))
7110 require.Contains(t, blocksToDelete, block1.Meta().ULID)
7111 require.Contains(t, blocksToDelete, block2.Meta().ULID)
7112 require.Contains(t, blocksToDelete, block3.Meta().ULID)
7113 require.Contains(t, blocksToDelete, block4.Meta().ULID)
7114 })
7115
7116 t.Run("should not delete not-shipped block even it is beyond block retention period", func(t *testing.T) {
7117 currentTime := time.Now()
7118 var blocks []*tsdb.Block
7119 block1 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-3*time.Hour).UnixMilli(), currentTime.Add(-2*time.Hour).UnixMilli())
7120 blocks = append(blocks, block1)
7121 block2 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-4*time.Hour).UnixMilli(), currentTime.Add(-3*time.Hour).UnixMilli())
7122 blocks = append(blocks, block2)
7123 block3 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-5*time.Hour).UnixMilli(), currentTime.Add(-4*time.Hour).UnixMilli())
7124 blocks = append(blocks, block3)
7125 block4 := CreateBlock(t, context.Background(), tempDir, currentTime.Add(-6*time.Hour).UnixMilli(), currentTime.Add(-5*time.Hour).UnixMilli())
7126 blocks = append(blocks, block4)
7127
7128 shippedBlocks := map[ulid.ULID]struct{}{
7129 block1.Meta().ULID: {},
7130 block2.Meta().ULID: {},
7131 block3.Meta().ULID: {},
7132 }
7133 userDB := &userTSDB{
7134 db: db,
7135 shipper: &shipperMock{},

Callers

nothing calls this directly

Calls 5

blocksToDeleteMethod · 0.95
CreateBlockFunction · 0.70
RunMethod · 0.65
EqualMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected