@ -1,7 +1,9 @@
package bloomshipper
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
@ -13,6 +15,8 @@ import (
"testing"
"time"
v1 "github.com/grafana/loki/pkg/storage/bloom/v1"
aws_io "github.com/aws/smithy-go/io"
"github.com/google/uuid"
"github.com/prometheus/common/model"
@ -183,6 +187,8 @@ func Test_BloomClient_GetBlocks(t *testing.T) {
secondBlockData := createBlockFile ( t , secondBlockFullPath )
require . FileExists ( t , firstBlockFullPath )
require . FileExists ( t , secondBlockFullPath )
rootDir := filepath . Join ( fsNamedStores [ "folder-1" ] . Directory , "bloom" )
defer os . RemoveAll ( rootDir )
firstBlockRef := BlockRef {
Ref : Ref {
@ -212,7 +218,7 @@ func Test_BloomClient_GetBlocks(t *testing.T) {
blocksToDownload := [ ] BlockRef { firstBlockRef , secondBlockRef }
blocksCh , errorsCh := shipper . GetBlocks ( context . Background ( ) , blocksToDownload )
blocks := make ( map [ string ] string )
blocks := make ( map [ string ] [ ] byte )
func ( ) {
timout := time . After ( 5 * time . Second )
for {
@ -226,13 +232,14 @@ func Test_BloomClient_GetBlocks(t *testing.T) {
if ! ok {
return
}
blockData , err := io . ReadAll ( block . Data )
blockData , err := io . ReadAll ( block . Bloom Data)
require . NoError ( t , err )
blocks [ block . BlockRef . BlockPath ] = string ( blockData )
blocks [ block . BlockRef . BlockPath ] = blockData
}
}
} ( )
defer os . RemoveAll ( "./bloom" )
firstBlockActualData , exists := blocks [ firstBlockRef . BlockPath ]
require . Truef ( t , exists , "data for the first block must be present in the results: %+v" , blocks )
@ -245,9 +252,42 @@ func Test_BloomClient_GetBlocks(t *testing.T) {
require . Len ( t , blocks , 2 )
}
func extractFileFromTGZ ( tarGzData [ ] byte , targetFileName string ) [ ] byte {
gzReader , err := gzip . NewReader ( bytes . NewReader ( tarGzData ) )
if err != nil {
return nil
}
defer gzReader . Close ( )
tarReader := tar . NewReader ( gzReader )
for {
header , err := tarReader . Next ( )
if err == io . EOF {
break
}
if err != nil {
return nil
}
if header . Name == targetFileName {
buffer := new ( bytes . Buffer )
if _ , err := io . Copy ( buffer , tarReader ) ; err != nil {
return nil
}
return buffer . Bytes ( )
}
}
return nil
}
func Test_BloomClient_PutBlocks ( t * testing . T ) {
shipper := createShipper ( t )
blockForFirstFolderData := "data1"
indexForFirstFolderData := "index1"
blockForFirstFolder := Block {
BlockRef : BlockRef {
Ref : Ref {
@ -261,10 +301,12 @@ func Test_BloomClient_PutBlocks(t *testing.T) {
} ,
IndexPath : uuid . New ( ) . String ( ) ,
} ,
Data : aws_io . ReadSeekNopCloser { ReadSeeker : bytes . NewReader ( [ ] byte ( blockForFirstFolderData ) ) } ,
BloomData : aws_io . ReadSeekNopCloser { ReadSeeker : bytes . NewReader ( [ ] byte ( blockForFirstFolderData ) ) } ,
IndexData : aws_io . ReadSeekNopCloser { ReadSeeker : bytes . NewReader ( [ ] byte ( indexForFirstFolderData ) ) } ,
}
blockForSecondFolderData := "data2"
indexForSecondFolderData := "index2"
blockForSecondFolder := Block {
BlockRef : BlockRef {
Ref : Ref {
@ -278,7 +320,8 @@ func Test_BloomClient_PutBlocks(t *testing.T) {
} ,
IndexPath : uuid . New ( ) . String ( ) ,
} ,
Data : aws_io . ReadSeekNopCloser { ReadSeeker : bytes . NewReader ( [ ] byte ( blockForSecondFolderData ) ) } ,
BloomData : aws_io . ReadSeekNopCloser { ReadSeeker : bytes . NewReader ( [ ] byte ( blockForSecondFolderData ) ) } ,
IndexData : aws_io . ReadSeekNopCloser { ReadSeeker : bytes . NewReader ( [ ] byte ( indexForSecondFolderData ) ) } ,
}
results , err := shipper . PutBlocks ( context . Background ( ) , [ ] Block { blockForFirstFolder , blockForSecondFolder } )
@ -300,7 +343,7 @@ func Test_BloomClient_PutBlocks(t *testing.T) {
require . FileExists ( t , savedFilePath )
savedData , err := os . ReadFile ( savedFilePath )
require . NoError ( t , err )
require . Equal ( t , blockForFirstFolderData , string ( savedData ) )
require . Equal ( t , blockForFirstFolderData , string ( extractFileFromTGZ ( savedData , "bloom" ) ) )
secondResultBlock := results [ 1 ]
path = secondResultBlock . BlockPath
@ -319,7 +362,7 @@ func Test_BloomClient_PutBlocks(t *testing.T) {
require . FileExists ( t , savedFilePath )
savedData , err = os . ReadFile ( savedFilePath )
require . NoError ( t , err )
require . Equal ( t , blockForSecondFolderData , string ( savedData ) )
require . Equal ( t , blockForSecondFolderData , string ( extractFileFromTGZ ( savedData , "bloom" ) ) )
}
func Test_BloomClient_DeleteBlocks ( t * testing . T ) {
@ -364,13 +407,19 @@ func Test_BloomClient_DeleteBlocks(t *testing.T) {
require . NoFileExists ( t , block2Path )
}
func createBlockFile ( t * testing . T , path string ) string {
func createBlockFile ( t * testing . T , path string ) [ ] byte {
err := os . MkdirAll ( path [ : strings . LastIndex ( path , "/" ) ] , 0755 )
require . NoError ( t , err )
fileContent := uuid . NewString ( )
err = os . WriteFile ( path , [ ] byte ( fileContent ) , 0700 )
bloomContent := [ ] byte ( uuid . NewString ( ) )
indexContent := [ ] byte ( uuid . NewString ( ) )
outputFile , err := os . Create ( path )
require . NoError ( t , err )
byteReader := v1 . NewByteReader ( bytes . NewBuffer ( indexContent ) , bytes . NewBuffer ( bloomContent ) )
err = v1 . TarGzMemory ( outputFile , byteReader )
require . NoError ( t , err )
err = outputFile . Close ( )
require . NoError ( t , err )
return fileContent
return bloom Content
}
func Test_TablesByPeriod ( t * testing . T ) {