@@ -7057,3 +7057,59 @@ func TestAbortBlockCompactions(t *testing.T) {
7057
7057
require .True (t , db .head .compactable (), "head should be compactable" )
7058
7058
require .Equal (t , 4 , compactions , "expected 4 compactions to be completed" )
7059
7059
}
7060
+
7061
+ func TestCompactHeadWithoutTruncation (t * testing.T ) {
7062
+ setupDB := func () * DB {
7063
+ db := openTestDB (t , nil , nil )
7064
+ t .Cleanup (func () {
7065
+ require .NoError (t , db .Close ())
7066
+ })
7067
+ db .DisableCompactions ()
7068
+
7069
+ // Add samples to the head.
7070
+ lbls := labels .FromStrings ("foo" , "bar" )
7071
+ app := db .Appender (context .Background ())
7072
+ _ , err := app .Append (0 , lbls , 0 , 0 )
7073
+ require .NoError (t , err )
7074
+ _ , err = app .Append (0 , lbls , DefaultBlockDuration / 2 , float64 (DefaultBlockDuration / 2 ))
7075
+ require .NoError (t , err )
7076
+ _ , err = app .Append (0 , lbls , DefaultBlockDuration - 1 , float64 (DefaultBlockDuration - 1 ))
7077
+ require .NoError (t , err )
7078
+ _ , err = app .Append (0 , lbls , 2 * DefaultBlockDuration , float64 (2 * DefaultBlockDuration ))
7079
+ require .NoError (t , err )
7080
+ require .NoError (t , app .Commit ())
7081
+
7082
+ return db
7083
+ }
7084
+
7085
+ testQuery := func (db * DB , expSamples []chunks.Sample ) {
7086
+ rh := NewRangeHead (db .Head (), math .MinInt64 , math .MaxInt64 )
7087
+ q , err := NewBlockQuerier (rh , math .MinInt64 , math .MaxInt64 )
7088
+ require .NoError (t , err )
7089
+ ss := query (t , q , labels .MustNewMatcher (labels .MatchEqual , "foo" , "bar" ))
7090
+ require .Equal (t , map [string ][]chunks.Sample {`{foo="bar"}` : expSamples }, ss )
7091
+ }
7092
+
7093
+ { // Compact the head with truncation.
7094
+ db := setupDB ()
7095
+ rh := NewRangeHead (db .Head (), 0 , DefaultBlockDuration - 1 )
7096
+ require .NoError (t , db .CompactHead (rh ))
7097
+ // Samples got truncated from the head.
7098
+ testQuery (db , []chunks.Sample {
7099
+ sample {t : 2 * DefaultBlockDuration , f : float64 (2 * DefaultBlockDuration )},
7100
+ })
7101
+ }
7102
+
7103
+ { // Compact the head without truncation.
7104
+ db := setupDB ()
7105
+ rh := NewRangeHead (db .Head (), 0 , DefaultBlockDuration - 1 )
7106
+ require .NoError (t , db .CompactHeadWithoutTruncation (rh ))
7107
+ // All samples still exist in the head.
7108
+ testQuery (db , []chunks.Sample {
7109
+ sample {t : 0 , f : 0 },
7110
+ sample {t : DefaultBlockDuration / 2 , f : float64 (DefaultBlockDuration / 2 )},
7111
+ sample {t : DefaultBlockDuration - 1 , f : float64 (DefaultBlockDuration - 1 )},
7112
+ sample {t : 2 * DefaultBlockDuration , f : float64 (2 * DefaultBlockDuration )},
7113
+ })
7114
+ }
7115
+ }
0 commit comments