@@ -2,6 +2,7 @@ package ingester
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"io"
7
8
"io/ioutil"
@@ -26,6 +27,7 @@ import (
26
27
"github.com/stretchr/testify/assert"
27
28
"github.com/stretchr/testify/mock"
28
29
"github.com/stretchr/testify/require"
30
+ "github.com/thanos-io/thanos/pkg/shipper"
29
31
"github.com/weaveworks/common/httpgrpc"
30
32
"github.com/weaveworks/common/middleware"
31
33
"github.com/weaveworks/common/user"
@@ -1294,6 +1296,8 @@ func TestIngester_shipBlocks(t *testing.T) {
1294
1296
}
1295
1297
1296
1298
type shipperMock struct {
1299
+ db * userTSDB
1300
+ uploaded int
1297
1301
mock.Mock
1298
1302
}
1299
1303
@@ -1769,7 +1773,7 @@ func TestIngester_CloseTSDBsOnShutdown(t *testing.T) {
1769
1773
require .Nil (t , db )
1770
1774
}
1771
1775
1772
- func TestIngesterV2BackfillPushAndQuery (t * testing.T ) {
1776
+ func TestIngesterV2BackfillCycle (t * testing.T ) {
1773
1777
cfg := defaultIngesterTestConfig ()
1774
1778
cfg .LifecyclerConfig .JoinAfter = 0
1775
1779
backfillLimit := 12 * time .Hour
@@ -1898,18 +1902,36 @@ func TestIngesterV2BackfillPushAndQuery(t *testing.T) {
1898
1902
1899
1903
// Compact old blocks partially to check
1900
1904
// * Compaction is happening properly.
1901
- // * We can still query it.
1905
+ // * We can still query it while we have a mix of compacted and uncompacted TSDBs .
1902
1906
1903
- // Check there are no blocks yet.
1907
+ // Check there are no blocks yet and attach and mock shipper .
1904
1908
userBuckets := i .TSDBState .backfillDBs .getBucketsForUser (userID )
1905
1909
for _ , bucket := range userBuckets .buckets {
1906
1910
require .Equal (t , 0 , len (bucket .db .Blocks ()))
1907
- m := & shipperMock {}
1908
- m .On ("Sync" , mock .Anything ).Return (0 , nil )
1911
+
1912
+ m := & shipperMock {
1913
+ db : bucket .db ,
1914
+ }
1915
+ m .On ("Sync" , mock .Anything ).Run (func (args mock.Arguments ) {
1916
+ var shipperMeta shipper.Meta
1917
+ shipperMeta .Version = shipper .MetaVersion1
1918
+ for _ , block := range m .db .Blocks () {
1919
+ shipperMeta .Uploaded = append (shipperMeta .Uploaded , block .Meta ().ULID )
1920
+ }
1921
+
1922
+ b , err := json .Marshal (& shipperMeta )
1923
+ if err != nil {
1924
+ return
1925
+ }
1926
+
1927
+ path := filepath .Join (m .db .Dir (), shipper .MetaFilename )
1928
+ _ = ioutil .WriteFile (path , b , os .ModePerm )
1929
+ m .uploaded = len (shipperMeta .Uploaded )
1930
+ }).Return (1 , nil )
1909
1931
bucket .db .shipper = m
1910
1932
}
1911
1933
1912
- // Compacting the oldest 2 buckets. They are <=97h, so compacting buckets upto 97.5h (current 100h minus 2 .5h).
1934
+ // Compacting the oldest 2 buckets. They are <=97h, so compacting buckets upto 97.5h (current 100h minus 97 .5h is the grace period ).
1913
1935
require .NoError (t , i .compactOldBackfillTSDBsAndShip (2 * time .Hour .Milliseconds ()+ 30 * time .Minute .Milliseconds ()))
1914
1936
for idx , bucket := range userBuckets .buckets {
1915
1937
if idx < 2 {
@@ -1921,4 +1943,30 @@ func TestIngesterV2BackfillPushAndQuery(t *testing.T) {
1921
1943
1922
1944
// We can still query compacted blocks.
1923
1945
testQuery ()
1946
+
1947
+ copiedBuckets := append ([]* tsdbBucket {}, userBuckets .buckets ... )
1948
+
1949
+ // Closing the old TSDBs and deleting them. Starting with the shipped blocks.
1950
+ // Closing is based on current time. Hence grace period is w.r.t. current time.
1951
+ nowTimeMs := time .Now ().Unix () * 1000
1952
+ gracePeriod := nowTimeMs - (97 * time .Hour .Milliseconds () + 30 * time .Minute .Milliseconds ())
1953
+ require .NoError (t , i .closeOldBackfillTSDBsAndDelete (gracePeriod ))
1954
+ require .Equal (t , 2 , len (userBuckets .buckets ))
1955
+ for idx , bucket := range userBuckets .buckets {
1956
+ require .Equal (t , 0 , len (bucket .db .Blocks ()), "bucket index %d" , idx )
1957
+ }
1958
+
1959
+ // Delete the pending TSDBs.
1960
+ gracePeriod = nowTimeMs - (100 * time .Hour .Milliseconds ())
1961
+ require .NoError (t , i .closeOldBackfillTSDBsAndDelete (gracePeriod ))
1962
+ require .Equal (t , 0 , len (userBuckets .buckets ))
1963
+
1964
+ // Check from the copied buckets if all of them had at 1 shipped block.
1965
+ // The last 2 buckets were not compacted explicitly before.
1966
+ for idx , buk := range copiedBuckets {
1967
+ s , ok := buk .db .shipper .(* shipperMock )
1968
+ require .True (t , ok )
1969
+ require .Equal (t , 1 , s .uploaded , "uploaded - bucket index %d" , idx )
1970
+ require .Equal (t , 1 , len (buk .db .Blocks ()), "blocks - bucket index %d" , idx )
1971
+ }
1924
1972
}
0 commit comments