github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/shipper/index/compactor/util.go (about) 1 package compactor 2 3 import ( 4 "strconv" 5 "testing" 6 "time" 7 "unsafe" 8 9 "github.com/prometheus/common/model" 10 "github.com/prometheus/prometheus/model/labels" 11 "github.com/stretchr/testify/require" 12 13 "github.com/grafana/loki/pkg/chunkenc" 14 ingesterclient "github.com/grafana/loki/pkg/ingester/client" 15 "github.com/grafana/loki/pkg/logproto" 16 "github.com/grafana/loki/pkg/storage/chunk" 17 ) 18 19 // unsafeGetString is like yolostring but with a meaningful name 20 func unsafeGetString(buf []byte) string { 21 return *((*string)(unsafe.Pointer(&buf))) 22 } 23 24 func createChunk(t testing.TB, userID string, lbs labels.Labels, from model.Time, through model.Time) chunk.Chunk { 25 t.Helper() 26 const ( 27 targetSize = 1500 * 1024 28 blockSize = 256 * 1024 29 ) 30 labelsBuilder := labels.NewBuilder(lbs) 31 labelsBuilder.Set(labels.MetricName, "logs") 32 metric := labelsBuilder.Labels() 33 fp := ingesterclient.Fingerprint(lbs) 34 chunkEnc := chunkenc.NewMemChunk(chunkenc.EncSnappy, chunkenc.UnorderedHeadBlockFmt, blockSize, targetSize) 35 36 for ts := from; !ts.After(through); ts = ts.Add(1 * time.Minute) { 37 require.NoError(t, chunkEnc.Append(&logproto.Entry{ 38 Timestamp: ts.Time(), 39 Line: ts.String(), 40 })) 41 } 42 43 require.NoError(t, chunkEnc.Close()) 44 c := chunk.NewChunk(userID, fp, metric, chunkenc.NewFacade(chunkEnc, blockSize, targetSize), from, through) 45 require.NoError(t, c.Encode()) 46 return c 47 } 48 49 // ExtractIntervalFromTableName gives back the time interval for which the table is expected to hold the chunks index. 50 func ExtractIntervalFromTableName(tableName string) model.Interval { 51 interval := model.Interval{ 52 Start: 0, 53 End: model.Now(), 54 } 55 tableNumber, err := strconv.ParseInt(tableName[len(tableName)-5:], 10, 64) 56 if err != nil { 57 return interval 58 } 59 60 interval.Start = model.TimeFromUnix(tableNumber * 86400) 61 // subtract a millisecond here so that interval only covers a single table since adding 24 hours ends up covering the start time of next table as well. 62 interval.End = interval.Start.Add(24*time.Hour) - 1 63 return interval 64 }