mirror of https://github.com/grafana/loki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
906 lines
19 KiB
906 lines
19 KiB
![]()
6 years ago
|
package storage
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
![]()
6 years ago
|
"fmt"
|
||
![]()
6 years ago
|
"testing"
|
||
|
"time"
|
||
|
|
||
![]()
5 years ago
|
"github.com/cortexproject/cortex/pkg/chunk"
|
||
|
"github.com/pkg/errors"
|
||
![]()
5 years ago
|
"github.com/prometheus/common/model"
|
||
![]()
5 years ago
|
"github.com/prometheus/prometheus/pkg/labels"
|
||
![]()
5 years ago
|
"github.com/prometheus/prometheus/promql"
|
||
![]()
5 years ago
|
"github.com/stretchr/testify/require"
|
||
![]()
5 years ago
|
"github.com/weaveworks/common/user"
|
||
![]()
5 years ago
|
|
||
![]()
6 years ago
|
"github.com/grafana/loki/pkg/chunkenc"
|
||
|
"github.com/grafana/loki/pkg/iter"
|
||
|
"github.com/grafana/loki/pkg/logproto"
|
||
![]()
5 years ago
|
"github.com/grafana/loki/pkg/logql"
|
||
|
"github.com/grafana/loki/pkg/logql/stats"
|
||
![]()
6 years ago
|
)
|
||
|
|
||
|
func Test_newBatchChunkIterator(t *testing.T) {
|
||
|
|
||
|
tests := map[string]struct {
|
||
![]()
5 years ago
|
chunks []*LazyChunk
|
||
![]()
5 years ago
|
expected []logproto.Stream
|
||
![]()
6 years ago
|
matchers string
|
||
|
start, end time.Time
|
||
|
direction logproto.Direction
|
||
![]()
6 years ago
|
batchSize int
|
||
![]()
6 years ago
|
}{
|
||
|
"forward with overlap": {
|
||
![]()
5 years ago
|
[]*LazyChunk{
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(4 * time.Millisecond),
|
||
|
Line: "5",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
6 years ago
|
{
|
||
![]()
6 years ago
|
Labels: fooLabels,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
fooLabelsWithName,
|
||
|
from, from.Add(4 * time.Millisecond),
|
||
|
logproto.FORWARD,
|
||
|
2,
|
||
|
},
|
||
|
"forward with overlapping non-continuous entries": {
|
||
![]()
5 years ago
|
[]*LazyChunk{
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
6 years ago
|
{
|
||
|
Labels: fooLabels,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
![]()
6 years ago
|
fooLabelsWithName,
|
||
![]()
6 years ago
|
from, from.Add(3 * time.Millisecond),
|
||
|
logproto.FORWARD,
|
||
![]()
6 years ago
|
2,
|
||
![]()
6 years ago
|
},
|
||
|
"backward with overlap": {
|
||
![]()
5 years ago
|
[]*LazyChunk{
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(4 * time.Millisecond),
|
||
|
Line: "5",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
![]()
6 years ago
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
6 years ago
|
{
|
||
|
Labels: fooLabels,
|
||
|
Entries: []logproto.Entry{
|
||
![]()
6 years ago
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
![]()
6 years ago
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
![]()
6 years ago
|
fooLabelsWithName,
|
||
|
from, from.Add(4 * time.Millisecond),
|
||
![]()
6 years ago
|
logproto.BACKWARD,
|
||
![]()
6 years ago
|
2,
|
||
![]()
6 years ago
|
},
|
||
![]()
6 years ago
|
"backward with overlapping non-continuous entries": {
|
||
![]()
5 years ago
|
[]*LazyChunk{
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(0 * time.Millisecond),
|
||
|
Line: "0",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(1 * time.Millisecond),
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(6 * time.Millisecond),
|
||
|
Line: "6",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(5 * time.Millisecond),
|
||
|
Line: "5",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(4 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(7 * time.Millisecond),
|
||
|
Line: "7",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
6 years ago
|
{
|
||
![]()
6 years ago
|
Labels: fooLabels,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(7 * time.Millisecond),
|
||
|
Line: "7",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(6 * time.Millisecond),
|
||
|
Line: "6",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(5 * time.Millisecond),
|
||
|
Line: "5",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(4 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(1 * time.Millisecond),
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(0 * time.Millisecond),
|
||
|
Line: "0",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
fooLabelsWithName,
|
||
|
from, from.Add(8 * time.Millisecond),
|
||
|
logproto.BACKWARD,
|
||
|
2,
|
||
|
},
|
||
|
"forward without overlap": {
|
||
![]()
5 years ago
|
[]*LazyChunk{
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
6 years ago
|
{
|
||
|
Labels: fooLabels,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
![]()
6 years ago
|
fooLabelsWithName,
|
||
![]()
6 years ago
|
from, from.Add(3 * time.Millisecond),
|
||
|
logproto.FORWARD,
|
||
![]()
6 years ago
|
2,
|
||
![]()
6 years ago
|
},
|
||
|
"backward without overlap": {
|
||
![]()
5 years ago
|
[]*LazyChunk{
|
||
![]()
6 years ago
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
newLazyChunk(logproto.Stream{
|
||
![]()
6 years ago
|
Labels: fooLabelsWithName,
|
||
![]()
6 years ago
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
}),
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
6 years ago
|
{
|
||
|
Labels: fooLabels,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
![]()
6 years ago
|
fooLabelsWithName,
|
||
![]()
6 years ago
|
from, from.Add(3 * time.Millisecond),
|
||
|
logproto.BACKWARD,
|
||
![]()
6 years ago
|
2,
|
||
![]()
6 years ago
|
},
|
||
|
}
|
||
|
|
||
|
for name, tt := range tests {
|
||
|
tt := tt
|
||
|
t.Run(name, func(t *testing.T) {
|
||
![]()
5 years ago
|
it := newBatchChunkIterator(context.Background(), tt.chunks, tt.batchSize, newMatchers(tt.matchers), nil, newQuery("", tt.start, tt.end, tt.direction, nil))
|
||
![]()
6 years ago
|
streams, _, err := iter.ReadBatch(it, 1000)
|
||
|
_ = it.Close()
|
||
|
if err != nil {
|
||
|
t.Fatalf("error reading batch %s", err)
|
||
|
}
|
||
|
|
||
|
assertStream(t, tt.expected, streams.Streams)
|
||
|
|
||
|
})
|
||
|
}
|
||
![]()
6 years ago
|
}
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
func TestPartitionOverlappingchunks(t *testing.T) {
|
||
|
var (
|
||
|
oneThroughFour = newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
two = newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(1 * time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
three = newLazyChunk(logproto.Stream{
|
||
|
Labels: fooLabelsWithName,
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
)
|
||
|
|
||
|
for i, tc := range []struct {
|
||
![]()
5 years ago
|
input []*LazyChunk
|
||
|
expected [][]*LazyChunk
|
||
![]()
6 years ago
|
}{
|
||
|
{
|
||
![]()
5 years ago
|
input: []*LazyChunk{
|
||
![]()
6 years ago
|
oneThroughFour,
|
||
|
two,
|
||
|
three,
|
||
|
},
|
||
![]()
5 years ago
|
expected: [][]*LazyChunk{
|
||
![]()
6 years ago
|
{oneThroughFour},
|
||
|
{two, three},
|
||
![]()
6 years ago
|
},
|
||
|
},
|
||
|
{
|
||
![]()
5 years ago
|
input: []*LazyChunk{
|
||
![]()
6 years ago
|
two,
|
||
|
oneThroughFour,
|
||
|
three,
|
||
|
},
|
||
![]()
5 years ago
|
expected: [][]*LazyChunk{
|
||
![]()
6 years ago
|
{oneThroughFour},
|
||
|
{two, three},
|
||
![]()
6 years ago
|
},
|
||
|
},
|
||
|
{
|
||
![]()
5 years ago
|
input: []*LazyChunk{
|
||
![]()
6 years ago
|
two,
|
||
|
two,
|
||
|
three,
|
||
|
three,
|
||
|
},
|
||
![]()
5 years ago
|
expected: [][]*LazyChunk{
|
||
![]()
6 years ago
|
{two, three},
|
||
|
{two, three},
|
||
![]()
6 years ago
|
},
|
||
|
},
|
||
|
} {
|
||
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||
|
out := partitionOverlappingChunks(tc.input)
|
||
|
require.Equal(t, tc.expected, out)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
5 years ago
|
func TestBuildHeapIterator(t *testing.T) {
|
||
|
var (
|
||
|
firstChunk = newLazyChunk(logproto.Stream{
|
||
|
Labels: "{foo=\"bar\"}",
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
secondChunk = newLazyInvalidChunk(logproto.Stream{
|
||
|
Labels: "{foo=\"bar\"}",
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(3 * time.Millisecond),
|
||
|
Line: "4",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(4 * time.Millisecond),
|
||
|
Line: "5",
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
thirdChunk = newLazyChunk(logproto.Stream{
|
||
|
Labels: "{foo=\"bar\"}",
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from.Add(5 * time.Millisecond),
|
||
|
Line: "6",
|
||
|
},
|
||
|
},
|
||
|
})
|
||
|
)
|
||
|
|
||
|
for i, tc := range []struct {
|
||
![]()
5 years ago
|
input [][]*LazyChunk
|
||
![]()
5 years ago
|
expected []logproto.Stream
|
||
![]()
5 years ago
|
}{
|
||
|
{
|
||
![]()
5 years ago
|
[][]*LazyChunk{
|
||
![]()
5 years ago
|
{firstChunk},
|
||
|
{thirdChunk},
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
5 years ago
|
{
|
||
|
Labels: "{foo=\"bar\"}",
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(5 * time.Millisecond),
|
||
|
Line: "6",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
{
|
||
![]()
5 years ago
|
[][]*LazyChunk{
|
||
![]()
5 years ago
|
{secondChunk},
|
||
|
{firstChunk, thirdChunk},
|
||
|
},
|
||
![]()
5 years ago
|
[]logproto.Stream{
|
||
![]()
5 years ago
|
{
|
||
|
Labels: "{foo=\"bar\"}",
|
||
|
Entries: []logproto.Entry{
|
||
|
{
|
||
|
Timestamp: from,
|
||
|
Line: "1",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(time.Millisecond),
|
||
|
Line: "2",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(2 * time.Millisecond),
|
||
|
Line: "3",
|
||
|
},
|
||
|
{
|
||
|
Timestamp: from.Add(5 * time.Millisecond),
|
||
|
Line: "6",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
} {
|
||
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||
|
ctx = user.InjectOrgID(context.Background(), "test-user")
|
||
![]()
5 years ago
|
b := &batchChunkIterator{
|
||
|
ctx: ctx,
|
||
|
req: &logproto.QueryRequest{Direction: logproto.FORWARD},
|
||
|
labels: map[model.Fingerprint]string{},
|
||
|
}
|
||
|
it, err := b.buildHeapIterator(tc.input, from, from.Add(6*time.Millisecond), nil)
|
||
![]()
5 years ago
|
if err != nil {
|
||
|
t.Errorf("buildHeapIterator error = %v", err)
|
||
|
return
|
||
|
}
|
||
![]()
5 years ago
|
req := newQuery("{foo=\"bar\"}", from, from.Add(6*time.Millisecond), logproto.FORWARD, nil)
|
||
![]()
5 years ago
|
streams, _, err := iter.ReadBatch(it, req.Limit)
|
||
|
_ = it.Close()
|
||
|
if err != nil {
|
||
|
t.Fatalf("error reading batch %s", err)
|
||
|
}
|
||
|
assertStream(t, tc.expected, streams.Streams)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
6 years ago
|
func TestDropLabels(t *testing.T) {
|
||
|
|
||
|
for i, tc := range []struct {
|
||
|
ls labels.Labels
|
||
|
drop []string
|
||
|
expected labels.Labels
|
||
|
}{
|
||
|
{
|
||
|
ls: labels.Labels{
|
||
|
labels.Label{
|
||
|
Name: "a",
|
||
|
Value: "1",
|
||
|
},
|
||
|
labels.Label{
|
||
|
Name: "b",
|
||
|
Value: "2",
|
||
|
},
|
||
|
labels.Label{
|
||
|
Name: "c",
|
||
|
Value: "3",
|
||
|
},
|
||
|
},
|
||
|
drop: []string{"b"},
|
||
|
expected: labels.Labels{
|
||
|
labels.Label{
|
||
|
Name: "a",
|
||
|
Value: "1",
|
||
|
},
|
||
|
labels.Label{
|
||
|
Name: "c",
|
||
|
Value: "3",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
} {
|
||
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||
|
dropped := dropLabels(tc.ls, tc.drop...)
|
||
|
require.Equal(t, tc.expected, dropped)
|
||
|
})
|
||
|
}
|
||
![]()
6 years ago
|
}
|
||
![]()
5 years ago
|
|
||
|
func Test_IsInvalidChunkError(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
err error
|
||
|
expectedResult bool
|
||
|
}{
|
||
|
{
|
||
|
"invalid chunk cheksum error from cortex",
|
||
|
promql.ErrStorage{Err: chunk.ErrInvalidChecksum},
|
||
|
true,
|
||
|
},
|
||
|
{
|
||
|
"invalid chunk cheksum error from loki",
|
||
|
promql.ErrStorage{Err: chunkenc.ErrInvalidChecksum},
|
||
|
true,
|
||
|
},
|
||
|
{
|
||
|
"cache error",
|
||
|
promql.ErrStorage{Err: errors.New("error fetching from cache")},
|
||
|
false,
|
||
|
},
|
||
|
{
|
||
|
"no error from cortex or loki",
|
||
|
nil,
|
||
|
false,
|
||
|
},
|
||
|
}
|
||
|
for _, tc := range tests {
|
||
|
result := isInvalidChunkError(tc.err)
|
||
|
require.Equal(t, tc.expectedResult, result)
|
||
|
}
|
||
|
}
|
||
![]()
5 years ago
|
|
||
|
var entry logproto.Entry
|
||
|
|
||
|
func Benchmark_store_OverlappingChunks(b *testing.B) {
|
||
|
b.ReportAllocs()
|
||
|
st := &store{
|
||
|
cfg: Config{
|
||
|
MaxChunkBatchSize: 50,
|
||
|
},
|
||
|
Store: newMockChunkStore(newOverlappingStreams(200, 200)),
|
||
|
}
|
||
|
b.ResetTimer()
|
||
|
ctx := user.InjectOrgID(stats.NewContext(context.Background()), "fake")
|
||
|
start := time.Now()
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
it, err := st.LazyQuery(ctx, logql.SelectParams{QueryRequest: &logproto.QueryRequest{
|
||
|
Selector: `{foo="bar"}`,
|
||
|
Direction: logproto.BACKWARD,
|
||
|
Limit: 0,
|
||
|
Shards: nil,
|
||
|
Start: time.Unix(0, 1),
|
||
|
End: time.Unix(0, time.Now().UnixNano()),
|
||
|
}})
|
||
|
if err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
for it.Next() {
|
||
|
entry = it.Entry()
|
||
|
}
|
||
|
if err := it.Close(); err != nil {
|
||
|
b.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
r := stats.Snapshot(ctx, time.Since(start))
|
||
|
b.Log("Total chunks:" + fmt.Sprintf("%d", r.Store.TotalChunksRef))
|
||
|
b.Log("Total bytes decompressed:" + fmt.Sprintf("%d", r.Store.DecompressedBytes))
|
||
|
}
|
||
|
|
||
|
func newOverlappingStreams(streamCount int, entryCount int) []*logproto.Stream {
|
||
|
streams := make([]*logproto.Stream, streamCount)
|
||
|
for i := range streams {
|
||
|
streams[i] = &logproto.Stream{
|
||
|
Labels: fmt.Sprintf(`{foo="bar",id="%d"}`, i),
|
||
|
Entries: make([]logproto.Entry, entryCount),
|
||
|
}
|
||
|
for j := range streams[i].Entries {
|
||
|
streams[i].Entries[j] = logproto.Entry{
|
||
|
Timestamp: time.Unix(0, int64(1+j)),
|
||
|
Line: "a very compressible log line duh",
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return streams
|
||
|
}
|