@@ -2,8 +2,6 @@ package querier
2
2
3
3
import (
4
4
"context"
5
- "crypto/rand"
6
- "encoding/json"
7
5
"fmt"
8
6
"io/ioutil"
9
7
"os"
@@ -21,13 +19,13 @@ import (
21
19
"github.com/stretchr/testify/assert"
22
20
"github.com/stretchr/testify/mock"
23
21
"github.com/stretchr/testify/require"
24
- "github.com/thanos-io/thanos/pkg/block/metadata"
25
22
"github.com/thanos-io/thanos/pkg/objstore"
26
23
27
24
"github.com/cortexproject/cortex/pkg/storage/bucket"
28
25
"github.com/cortexproject/cortex/pkg/storage/bucket/filesystem"
29
26
cortex_tsdb "github.com/cortexproject/cortex/pkg/storage/tsdb"
30
27
"github.com/cortexproject/cortex/pkg/storage/tsdb/bucketindex"
28
+ cortex_testutil "github.com/cortexproject/cortex/pkg/storage/tsdb/testutil"
31
29
"github.com/cortexproject/cortex/pkg/util/services"
32
30
)
33
31
@@ -36,10 +34,10 @@ func TestBlocksScanner_InitialScan(t *testing.T) {
36
34
s , bucket , _ , reg , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
37
35
defer cleanup ()
38
36
39
- user1Block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
40
- user1Block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
41
- user2Block1 := mockStorageBlock (t , bucket , "user-2" , 10 , 20 )
42
- user2Mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-2" , user2Block1 ))
37
+ user1Block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
38
+ user1Block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
39
+ user2Block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-2" , 10 , 20 )
40
+ user2Mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-2" , user2Block1 ))
43
41
44
42
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
45
43
@@ -224,9 +222,9 @@ func TestBlocksScanner_PeriodicScanFindsNewUser(t *testing.T) {
224
222
require .Equal (t , 0 , len (blocks ))
225
223
assert .Empty (t , deletionMarks )
226
224
227
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
228
- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
229
- mark2 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-1" , block2 ))
225
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
226
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
227
+ mark2 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-1" , block2 ))
230
228
231
229
// Trigger a periodic sync
232
230
require .NoError (t , s .scan (ctx ))
@@ -248,7 +246,7 @@ func TestBlocksScanner_PeriodicScanFindsNewBlock(t *testing.T) {
248
246
s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
249
247
defer cleanup ()
250
248
251
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
249
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
252
250
253
251
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
254
252
@@ -259,7 +257,7 @@ func TestBlocksScanner_PeriodicScanFindsNewBlock(t *testing.T) {
259
257
assert .WithinDuration (t , time .Now (), blocks [0 ].GetUploadedAt (), 5 * time .Second )
260
258
assert .Empty (t , deletionMarks )
261
259
262
- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
260
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
263
261
264
262
// Trigger a periodic sync
265
263
require .NoError (t , s .scan (ctx ))
@@ -279,8 +277,8 @@ func TestBlocksScanner_PeriodicScanFindsBlockMarkedForDeletion(t *testing.T) {
279
277
s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
280
278
defer cleanup ()
281
279
282
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
283
- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
280
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
281
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
284
282
285
283
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
286
284
@@ -291,7 +289,7 @@ func TestBlocksScanner_PeriodicScanFindsBlockMarkedForDeletion(t *testing.T) {
291
289
assert .Equal (t , block1 .ULID , blocks [1 ].ID )
292
290
assert .Empty (t , deletionMarks )
293
291
294
- mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-1" , block1 ))
292
+ mark1 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-1" , block1 ))
295
293
296
294
// Trigger a periodic sync
297
295
require .NoError (t , s .scan (ctx ))
@@ -311,8 +309,8 @@ func TestBlocksScanner_PeriodicScanFindsDeletedBlock(t *testing.T) {
311
309
s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
312
310
defer cleanup ()
313
311
314
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
315
- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
312
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
313
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
316
314
317
315
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
318
316
@@ -340,8 +338,8 @@ func TestBlocksScanner_PeriodicScanFindsDeletedUser(t *testing.T) {
340
338
s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
341
339
defer cleanup ()
342
340
343
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
344
- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
341
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
342
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
345
343
346
344
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
347
345
@@ -368,8 +366,8 @@ func TestBlocksScanner_PeriodicScanFindsUserWhichWasPreviouslyDeleted(t *testing
368
366
s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
369
367
defer cleanup ()
370
368
371
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 20 )
372
- block2 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
369
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 20 )
370
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
373
371
374
372
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
375
373
@@ -390,7 +388,7 @@ func TestBlocksScanner_PeriodicScanFindsUserWhichWasPreviouslyDeleted(t *testing
390
388
require .Equal (t , 0 , len (blocks ))
391
389
assert .Empty (t , deletionMarks )
392
390
393
- block3 := mockStorageBlock (t , bucket , "user-1" , 30 , 40 )
391
+ block3 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 30 , 40 )
394
392
395
393
// Trigger a periodic sync
396
394
require .NoError (t , s .scan (ctx ))
@@ -407,11 +405,11 @@ func TestBlocksScanner_GetBlocks(t *testing.T) {
407
405
s , bucket , _ , _ , cleanup := prepareBlocksScanner (t , prepareBlocksScannerConfig ())
408
406
defer cleanup ()
409
407
410
- block1 := mockStorageBlock (t , bucket , "user-1" , 10 , 15 )
411
- block2 := mockStorageBlock (t , bucket , "user-1" , 12 , 20 )
412
- block3 := mockStorageBlock (t , bucket , "user-1" , 20 , 30 )
413
- block4 := mockStorageBlock (t , bucket , "user-1" , 30 , 40 )
414
- mark3 := bucketindex .BlockDeletionMarkFromThanosMarker (mockStorageDeletionMark (t , bucket , "user-1" , block3 ))
408
+ block1 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 10 , 15 )
409
+ block2 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 12 , 20 )
410
+ block3 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 20 , 30 )
411
+ block4 := cortex_testutil . MockStorageBlock (t , bucket , "user-1" , 30 , 40 )
412
+ mark3 := bucketindex .BlockDeletionMarkFromThanosMarker (cortex_testutil . MockStorageDeletionMark (t , bucket , "user-1" , block3 ))
415
413
416
414
require .NoError (t , services .StartAndAwaitRunning (ctx , s ))
417
415
@@ -523,50 +521,3 @@ func prepareBlocksScannerConfig() BlocksScannerConfig {
523
521
IgnoreDeletionMarksDelay : time .Hour ,
524
522
}
525
523
}
526
-
527
- func mockStorageBlock (t * testing.T , bucket objstore.Bucket , userID string , minT , maxT int64 ) tsdb.BlockMeta {
528
- // Generate a block ID whose timestamp matches the maxT (for simplicity we assume it
529
- // has been compacted and shipped in zero time, even if not realistic).
530
- id := ulid .MustNew (uint64 (maxT ), rand .Reader )
531
-
532
- meta := tsdb.BlockMeta {
533
- Version : 1 ,
534
- ULID : id ,
535
- MinTime : minT ,
536
- MaxTime : maxT ,
537
- Compaction : tsdb.BlockMetaCompaction {
538
- Level : 1 ,
539
- Sources : []ulid.ULID {id },
540
- },
541
- }
542
-
543
- metaContent , err := json .Marshal (meta )
544
- if err != nil {
545
- panic ("failed to marshal mocked block meta" )
546
- }
547
-
548
- metaContentReader := strings .NewReader (string (metaContent ))
549
- metaPath := fmt .Sprintf ("%s/%s/meta.json" , userID , id .String ())
550
- require .NoError (t , bucket .Upload (context .Background (), metaPath , metaContentReader ))
551
-
552
- return meta
553
- }
554
-
555
- func mockStorageDeletionMark (t * testing.T , bucket objstore.Bucket , userID string , meta tsdb.BlockMeta ) * metadata.DeletionMark {
556
- mark := metadata.DeletionMark {
557
- ID : meta .ULID ,
558
- DeletionTime : time .Now ().Add (- time .Minute ).Unix (),
559
- Version : metadata .DeletionMarkVersion1 ,
560
- }
561
-
562
- markContent , err := json .Marshal (mark )
563
- if err != nil {
564
- panic ("failed to marshal mocked block meta" )
565
- }
566
-
567
- markContentReader := strings .NewReader (string (markContent ))
568
- markPath := fmt .Sprintf ("%s/%s/%s" , userID , meta .ULID .String (), metadata .DeletionMarkFilename )
569
- require .NoError (t , bucket .Upload (context .Background (), markPath , markContentReader ))
570
-
571
- return & mark
572
- }
0 commit comments