2023-06-20 05:55:12 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
2024-05-12 14:33:29 +00:00
|
|
|
"context"
|
2023-06-20 05:55:12 +00:00
|
|
|
"fmt"
|
2024-06-28 17:14:29 +00:00
|
|
|
"math"
|
2024-05-28 17:29:41 +00:00
|
|
|
"reflect"
|
2024-06-27 12:18:42 +00:00
|
|
|
"sort"
|
2024-05-25 23:55:21 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2023-06-20 05:55:12 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStorageRunQuery(t *testing.T) {
|
2024-06-20 01:08:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
path := t.Name()
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
const tenantsCount = 11
|
|
|
|
const streamsPerTenant = 3
|
|
|
|
const blocksPerStream = 5
|
|
|
|
const rowsPerBlock = 7
|
|
|
|
|
|
|
|
sc := &StorageConfig{
|
|
|
|
Retention: 24 * time.Hour,
|
|
|
|
}
|
|
|
|
s := MustOpenStorage(path, sc)
|
|
|
|
|
|
|
|
// fill the storage with data
|
|
|
|
var allTenantIDs []TenantID
|
|
|
|
baseTimestamp := time.Now().UnixNano() - 3600*1e9
|
|
|
|
var fields []Field
|
|
|
|
streamTags := []string{
|
|
|
|
"job",
|
|
|
|
"instance",
|
|
|
|
}
|
|
|
|
for i := 0; i < tenantsCount; i++ {
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: uint32(i),
|
|
|
|
ProjectID: uint32(10*i + 1),
|
|
|
|
}
|
|
|
|
allTenantIDs = append(allTenantIDs, tenantID)
|
|
|
|
for j := 0; j < streamsPerTenant; j++ {
|
|
|
|
streamIDValue := fmt.Sprintf("stream_id=%d", j)
|
|
|
|
for k := 0; k < blocksPerStream; k++ {
|
2024-11-01 19:06:15 +00:00
|
|
|
lr := GetLogRows(streamTags, nil, nil, "")
|
2023-06-20 05:55:12 +00:00
|
|
|
for m := 0; m < rowsPerBlock; m++ {
|
|
|
|
timestamp := baseTimestamp + int64(m)*1e9 + int64(k)
|
|
|
|
// Append stream fields
|
|
|
|
fields = append(fields[:0], Field{
|
|
|
|
Name: "job",
|
|
|
|
Value: "foobar",
|
|
|
|
}, Field{
|
|
|
|
Name: "instance",
|
|
|
|
Value: fmt.Sprintf("host-%d:234", j),
|
|
|
|
})
|
|
|
|
// append the remaining fields
|
|
|
|
fields = append(fields, Field{
|
|
|
|
Name: "_msg",
|
|
|
|
Value: fmt.Sprintf("log message %d at block %d", m, k),
|
|
|
|
})
|
|
|
|
fields = append(fields, Field{
|
|
|
|
Name: "source-file",
|
|
|
|
Value: "/foo/bar/baz",
|
|
|
|
})
|
|
|
|
fields = append(fields, Field{
|
|
|
|
Name: "tenant.id",
|
|
|
|
Value: tenantID.String(),
|
|
|
|
})
|
|
|
|
fields = append(fields, Field{
|
|
|
|
Name: "stream-id",
|
|
|
|
Value: streamIDValue,
|
|
|
|
})
|
|
|
|
lr.MustAdd(tenantID, timestamp, fields)
|
|
|
|
}
|
2023-10-02 14:26:02 +00:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 05:55:12 +00:00
|
|
|
PutLogRows(lr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.debugFlush()
|
|
|
|
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery := func(t *testing.T, tenantIDs []TenantID, q *Query, writeBlock WriteBlockFunc) {
|
2024-05-20 02:08:30 +00:00
|
|
|
t.Helper()
|
|
|
|
err := s.RunQuery(context.Background(), tenantIDs, q, writeBlock)
|
|
|
|
if err != nil {
|
2024-06-28 17:14:29 +00:00
|
|
|
t.Fatalf("unexpected error returned from the query [%s]: %s", q, err)
|
2024-05-20 02:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
// run tests on the storage data
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("missing-tenant", func(t *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
q := mustParseQuery(`"log message"`)
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 0,
|
|
|
|
ProjectID: 0,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
panic(fmt.Errorf("unexpected match for %d rows", len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("missing-message-text", func(t *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
q := mustParseQuery(`foobar`)
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
panic(fmt.Errorf("unexpected match for %d rows", len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
})
|
|
|
|
t.Run("matching-tenant-id", func(t *testing.T) {
|
|
|
|
q := mustParseQuery(`tenant.id:*`)
|
|
|
|
for i := 0; i < tenantsCount; i++ {
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: uint32(i),
|
|
|
|
ProjectID: uint32(10*i + 1),
|
|
|
|
}
|
|
|
|
expectedTenantID := tenantID.String()
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, columns []BlockColumn) {
|
2023-06-20 05:55:12 +00:00
|
|
|
hasTenantIDColumn := false
|
|
|
|
var columnNames []string
|
|
|
|
for _, c := range columns {
|
|
|
|
if c.Name == "tenant.id" {
|
|
|
|
hasTenantIDColumn = true
|
2024-05-12 14:33:29 +00:00
|
|
|
if len(c.Values) != len(timestamps) {
|
|
|
|
panic(fmt.Errorf("unexpected number of rows in column %q; got %d; want %d", c.Name, len(c.Values), len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
for _, v := range c.Values {
|
|
|
|
if v != expectedTenantID {
|
|
|
|
panic(fmt.Errorf("unexpected tenant.id; got %s; want %s", v, expectedTenantID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
columnNames = append(columnNames, c.Name)
|
|
|
|
}
|
|
|
|
if !hasTenantIDColumn {
|
|
|
|
panic(fmt.Errorf("missing tenant.id column among columns: %q", columnNames))
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := streamsPerTenant * blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of matching rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-multiple-tenant-ids", func(t *testing.T) {
|
|
|
|
q := mustParseQuery(`"log message"`)
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, allTenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := tenantsCount * streamsPerTenant * blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of matching rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-in-filter", func(t *testing.T) {
|
|
|
|
q := mustParseQuery(`source-file:in(foobar,/foo/bar/baz)`)
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, allTenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := tenantsCount * streamsPerTenant * blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of matching rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("stream-filter-mismatch", func(t *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
q := mustParseQuery(`_stream:{job="foobar",instance=~"host-.+:2345"} log`)
|
2024-05-12 14:33:29 +00:00
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
panic(fmt.Errorf("unexpected match for %d rows", len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, allTenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
})
|
|
|
|
t.Run("matching-stream-id", func(t *testing.T) {
|
|
|
|
for i := 0; i < streamsPerTenant; i++ {
|
|
|
|
q := mustParseQuery(fmt.Sprintf(`log _stream:{job="foobar",instance="host-%d:234"} AND stream-id:*`, i))
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
|
|
|
expectedStreamID := fmt.Sprintf("stream_id=%d", i)
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, columns []BlockColumn) {
|
2023-06-20 05:55:12 +00:00
|
|
|
hasStreamIDColumn := false
|
|
|
|
var columnNames []string
|
|
|
|
for _, c := range columns {
|
|
|
|
if c.Name == "stream-id" {
|
|
|
|
hasStreamIDColumn = true
|
2024-05-12 14:33:29 +00:00
|
|
|
if len(c.Values) != len(timestamps) {
|
|
|
|
panic(fmt.Errorf("unexpected number of rows for column %q; got %d; want %d", c.Name, len(c.Values), len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
for _, v := range c.Values {
|
|
|
|
if v != expectedStreamID {
|
|
|
|
panic(fmt.Errorf("unexpected stream-id; got %s; want %s", v, expectedStreamID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
columnNames = append(columnNames, c.Name)
|
|
|
|
}
|
|
|
|
if !hasStreamIDColumn {
|
|
|
|
panic(fmt.Errorf("missing stream-id column among columns: %q", columnNames))
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows for stream %d; got %d; want %d", i, n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-multiple-stream-ids-with-re-filter", func(t *testing.T) {
|
|
|
|
q := mustParseQuery(`_msg:log _stream:{job="foobar",instance=~"host-[^:]+:234"} and re("message [02] at")`)
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := streamsPerTenant * blocksPerStream * 2
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-time-range", func(t *testing.T) {
|
|
|
|
minTimestamp := baseTimestamp + (rowsPerBlock-2)*1e9
|
|
|
|
maxTimestamp := baseTimestamp + (rowsPerBlock-1)*1e9 - 1
|
|
|
|
q := mustParseQuery(fmt.Sprintf(`_time:[%f,%f]`, float64(minTimestamp)/1e9, float64(maxTimestamp)/1e9))
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := streamsPerTenant * blocksPerStream
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-stream-id-with-time-range", func(t *testing.T) {
|
|
|
|
minTimestamp := baseTimestamp + (rowsPerBlock-2)*1e9
|
|
|
|
maxTimestamp := baseTimestamp + (rowsPerBlock-1)*1e9 - 1
|
|
|
|
q := mustParseQuery(fmt.Sprintf(`_time:[%f,%f] _stream:{job="foobar",instance="host-1:234"}`, float64(minTimestamp)/1e9, float64(maxTimestamp)/1e9))
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
rowsCountTotal.Add(uint32(len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
expectedRowsCount := blocksPerStream
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("matching-stream-id-missing-time-range", func(t *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
minTimestamp := baseTimestamp + (rowsPerBlock+1)*1e9
|
|
|
|
maxTimestamp := baseTimestamp + (rowsPerBlock+2)*1e9
|
|
|
|
q := mustParseQuery(fmt.Sprintf(`_stream:{job="foobar",instance="host-1:234"} _time:[%d, %d)`, minTimestamp/1e9, maxTimestamp/1e9))
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
panic(fmt.Errorf("unexpected match for %d rows", len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("missing-time-range", func(t *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
minTimestamp := baseTimestamp + (rowsPerBlock+1)*1e9
|
|
|
|
maxTimestamp := baseTimestamp + (rowsPerBlock+2)*1e9
|
|
|
|
q := mustParseQuery(fmt.Sprintf(`_time:[%d, %d)`, minTimestamp/1e9, maxTimestamp/1e9))
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
writeBlock := func(_ uint, timestamps []int64, _ []BlockColumn) {
|
|
|
|
panic(fmt.Errorf("unexpected match for %d rows", len(timestamps)))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
tenantIDs := []TenantID{tenantID}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, tenantIDs, q, writeBlock)
|
2023-06-20 05:55:12 +00:00
|
|
|
})
|
2024-05-28 17:29:41 +00:00
|
|
|
t.Run("field_names-all", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetFieldNames(context.Background(), allTenantIDs, q)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{"_msg", 1155},
|
|
|
|
{"_stream", 1155},
|
2024-06-20 01:08:07 +00:00
|
|
|
{"_stream_id", 1155},
|
2024-05-28 17:29:41 +00:00
|
|
|
{"_time", 1155},
|
|
|
|
{"instance", 1155},
|
|
|
|
{"job", 1155},
|
|
|
|
{"source-file", 1155},
|
|
|
|
{"stream-id", 1155},
|
|
|
|
{"tenant.id", 1155},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("field_names-some", func(t *testing.T) {
|
|
|
|
q := mustParseQuery(`_stream:{instance=~"host-1:.+"}`)
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetFieldNames(context.Background(), allTenantIDs, q)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{"_msg", 385},
|
|
|
|
{"_stream", 385},
|
2024-06-20 01:08:07 +00:00
|
|
|
{"_stream_id", 385},
|
2024-05-28 17:29:41 +00:00
|
|
|
{"_time", 385},
|
|
|
|
{"instance", 385},
|
|
|
|
{"job", 385},
|
|
|
|
{"source-file", 385},
|
|
|
|
{"stream-id", 385},
|
|
|
|
{"tenant.id", 385},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("field_values-nolimit", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetFieldValues(context.Background(), allTenantIDs, q, "_stream", 0)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{`{instance="host-0:234",job="foobar"}`, 385},
|
|
|
|
{`{instance="host-1:234",job="foobar"}`, 385},
|
|
|
|
{`{instance="host-2:234",job="foobar"}`, 385},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("field_values-limit", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetFieldValues(context.Background(), allTenantIDs, q, "_stream", 3)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-09-29 08:50:25 +00:00
|
|
|
{`{instance="host-0:234",job="foobar"}`, 385},
|
|
|
|
{`{instance="host-1:234",job="foobar"}`, 385},
|
|
|
|
{`{instance="host-2:234",job="foobar"}`, 385},
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("field_values-limit", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("instance:='host-1:234'")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetFieldValues(context.Background(), allTenantIDs, q, "_stream", 4)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{`{instance="host-1:234",job="foobar"}`, 385},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("stream_field_names", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetStreamFieldNames(context.Background(), allTenantIDs, q)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{"instance", 1155},
|
|
|
|
{"job", 1155},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("stream_field_values-nolimit", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetStreamFieldValues(context.Background(), allTenantIDs, q, "instance", 0)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{`host-0:234`, 385},
|
|
|
|
{`host-1:234`, 385},
|
|
|
|
{`host-2:234`, 385},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("stream_field_values-limit", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
|
|
|
values, err := s.GetStreamFieldValues(context.Background(), allTenantIDs, q, "instance", 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{`host-0:234`, 385},
|
|
|
|
{`host-1:234`, 385},
|
|
|
|
{`host-2:234`, 385},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(values, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", values, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("streams", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
2024-06-27 12:18:42 +00:00
|
|
|
results, err := s.GetStreams(context.Background(), allTenantIDs, q, 0)
|
2024-05-28 17:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:18:42 +00:00
|
|
|
resultsExpected := []ValueWithHits{
|
2024-05-28 17:29:41 +00:00
|
|
|
{`{instance="host-0:234",job="foobar"}`, 385},
|
|
|
|
{`{instance="host-1:234",job="foobar"}`, 385},
|
|
|
|
{`{instance="host-2:234",job="foobar"}`, 385},
|
|
|
|
}
|
2024-06-27 12:18:42 +00:00
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("stream_ids", func(t *testing.T) {
|
|
|
|
q := mustParseQuery("*")
|
|
|
|
results, err := s.GetStreamIDs(context.Background(), allTenantIDs, q, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the first 5 results with the smallest _stream_id value.
|
|
|
|
sort.Slice(results, func(i, j int) bool {
|
|
|
|
return results[i].Value < results[j].Value
|
|
|
|
})
|
|
|
|
results = results[:5]
|
|
|
|
|
|
|
|
resultsExpected := []ValueWithHits{
|
|
|
|
{"000000000000000140c1914be0226f8185f5b00551fb3b2d", 35},
|
|
|
|
{"000000000000000177edafcd46385c778b57476eb5b92233", 35},
|
|
|
|
{"0000000000000001f5b4cae620b5e85d6ef5f2107fe00274", 35},
|
|
|
|
{"000000010000000b40c1914be0226f8185f5b00551fb3b2d", 35},
|
|
|
|
{"000000010000000b77edafcd46385c778b57476eb5b92233", 35},
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(results, resultsExpected) {
|
|
|
|
t.Fatalf("unexpected result; got\n%v\nwant\n%v", results, resultsExpected)
|
2024-05-28 17:29:41 +00:00
|
|
|
}
|
|
|
|
})
|
2023-06-20 05:55:12 +00:00
|
|
|
|
2024-05-25 23:55:21 +00:00
|
|
|
// Run more complex tests
|
|
|
|
f := func(t *testing.T, query string, rowsExpected [][]Field) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
q := mustParseQuery(query)
|
|
|
|
var resultRowsLock sync.Mutex
|
|
|
|
var resultRows [][]Field
|
|
|
|
writeBlock := func(_ uint, _ []int64, bcs []BlockColumn) {
|
|
|
|
if len(bcs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(bcs[0].Values); i++ {
|
|
|
|
row := make([]Field, len(bcs))
|
|
|
|
for j, bc := range bcs {
|
|
|
|
row[j] = Field{
|
|
|
|
Name: strings.Clone(bc.Name),
|
|
|
|
Value: strings.Clone(bc.Values[i]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resultRowsLock.Lock()
|
|
|
|
resultRows = append(resultRows, row)
|
|
|
|
resultRowsLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2024-06-24 21:27:12 +00:00
|
|
|
mustRunQuery(t, allTenantIDs, q, writeBlock)
|
2024-05-25 23:55:21 +00:00
|
|
|
|
|
|
|
assertRowsEqual(t, resultRows, rowsExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("stats-count-total", func(t *testing.T) {
|
|
|
|
f(t, `* | stats count() rows`, [][]Field{
|
|
|
|
{
|
|
|
|
{"rows", "1155"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("_stream_id-filter", func(t *testing.T) {
|
|
|
|
f(t, `_stream_id:in(tenant.id:2 | fields _stream_id) | stats count() rows`, [][]Field{
|
|
|
|
{
|
|
|
|
{"rows", "105"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-05-25 23:55:21 +00:00
|
|
|
t.Run("in-filter-with-subquery-match", func(t *testing.T) {
|
|
|
|
f(t, `tenant.id:in(tenant.id:2 | fields tenant.id) | stats count() rows`, [][]Field{
|
|
|
|
{
|
|
|
|
{"rows", "105"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("in-filter-with-subquery-mismatch", func(t *testing.T) {
|
|
|
|
f(t, `tenant.id:in(tenant.id:23243 | fields tenant.id) | stats count() rows`, [][]Field{
|
|
|
|
{
|
|
|
|
{"rows", "0"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("conditional-stats", func(t *testing.T) {
|
|
|
|
f(t, `* | stats
|
|
|
|
count() rows_total,
|
|
|
|
count() if (stream-id:0) stream_0_rows,
|
|
|
|
count() if (stream-id:1123) stream_x_rows
|
|
|
|
`, [][]Field{
|
|
|
|
{
|
|
|
|
{"rows_total", "1155"},
|
|
|
|
{"stream_0_rows", "385"},
|
|
|
|
{"stream_x_rows", "0"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("in-filter-with-subquery-in-conditional-stats-mismatch", func(t *testing.T) {
|
|
|
|
f(t, `* | stats
|
|
|
|
count() rows_total,
|
|
|
|
count() if (tenant.id:in(tenant.id:3 | fields tenant.id)) rows_nonzero,
|
|
|
|
count() if (tenant.id:in(tenant.id:23243 | fields tenant.id)) rows_zero
|
|
|
|
`, [][]Field{
|
|
|
|
{
|
|
|
|
{"rows_total", "1155"},
|
|
|
|
{"rows_nonzero", "105"},
|
|
|
|
{"rows_zero", "0"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("pipe-extract", func(t *testing.T) {
|
2024-05-25 23:55:21 +00:00
|
|
|
f(t, `* | extract "host-<host>:" from instance | uniq (host) with hits | sort by (host)`, [][]Field{
|
|
|
|
{
|
|
|
|
{"host", "0"},
|
|
|
|
{"hits", "385"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"host", "1"},
|
|
|
|
{"hits", "385"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"host", "2"},
|
|
|
|
{"hits", "385"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("pipe-extract-if-filter-with-subquery", func(t *testing.T) {
|
2024-05-25 23:55:21 +00:00
|
|
|
f(t, `* | extract
|
|
|
|
if (tenant.id:in(tenant.id:(3 or 4) | fields tenant.id))
|
|
|
|
"host-<host>:" from instance
|
|
|
|
| filter host:~"1|2"
|
|
|
|
| uniq (tenant.id, host) with hits
|
|
|
|
| sort by (tenant.id, host)`, [][]Field{
|
|
|
|
{
|
|
|
|
{"tenant.id", "{accountID=3,projectID=31}"},
|
|
|
|
{"host", "1"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"tenant.id", "{accountID=3,projectID=31}"},
|
|
|
|
{"host", "2"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"tenant.id", "{accountID=4,projectID=41}"},
|
|
|
|
{"host", "1"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"tenant.id", "{accountID=4,projectID=41}"},
|
|
|
|
{"host", "2"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("pipe-extract-if-filter-with-subquery-non-empty-host", func(t *testing.T) {
|
2024-05-25 23:55:21 +00:00
|
|
|
f(t, `* | extract
|
|
|
|
if (tenant.id:in(tenant.id:3 | fields tenant.id))
|
|
|
|
"host-<host>:" from instance
|
|
|
|
| filter host:*
|
|
|
|
| uniq (host) with hits
|
|
|
|
| sort by (host)`, [][]Field{
|
|
|
|
{
|
|
|
|
{"host", "0"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"host", "1"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"host", "2"},
|
|
|
|
{"hits", "35"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-06-24 21:27:12 +00:00
|
|
|
t.Run("pipe-extract-if-filter-with-subquery-empty-host", func(t *testing.T) {
|
2024-05-25 23:55:21 +00:00
|
|
|
f(t, `* | extract
|
|
|
|
if (tenant.id:in(tenant.id:3 | fields tenant.id))
|
|
|
|
"host-<host>:" from instance
|
|
|
|
| filter host:""
|
|
|
|
| uniq (host) with hits
|
|
|
|
| sort by (host)`, [][]Field{
|
|
|
|
{
|
|
|
|
{"host", ""},
|
|
|
|
{"hits", "1050"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-06-28 17:14:29 +00:00
|
|
|
t.Run("stream_context-noop-1", func(t *testing.T) {
|
|
|
|
f(t, `"message 3 at block 1"
|
|
|
|
| stream_context before 0
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-27 09:15:43 +00:00
|
|
|
{"rows", "66"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-noop-2", func(t *testing.T) {
|
|
|
|
f(t, `"message 3 at block 1"
|
|
|
|
| stream_context before 0 after 0
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-27 09:15:43 +00:00
|
|
|
{"rows", "66"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-before-1", func(t *testing.T) {
|
|
|
|
f(t, `"message 3 at block 1"
|
|
|
|
| stream_context before 1
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-27 09:15:43 +00:00
|
|
|
{"rows", "99"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-after-1", func(t *testing.T) {
|
|
|
|
f(t, `"message 3 at block 1"
|
|
|
|
| stream_context after 1
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-27 09:15:43 +00:00
|
|
|
{"rows", "99"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-before-after-1", func(t *testing.T) {
|
|
|
|
f(t, `"message 3 at block 1"
|
|
|
|
| stream_context before 1 after 1
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-27 09:15:43 +00:00
|
|
|
{"rows", "132"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-before-1000", func(t *testing.T) {
|
|
|
|
f(t, `"message 4"
|
|
|
|
| stream_context before 1000
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-26 14:52:55 +00:00
|
|
|
{"rows", "990"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-after-1000", func(t *testing.T) {
|
|
|
|
f(t, `"message 4"
|
|
|
|
| stream_context after 1000
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-26 14:52:55 +00:00
|
|
|
{"rows", "660"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("stream_context-before-after-1000", func(t *testing.T) {
|
|
|
|
f(t, `"message 4"
|
|
|
|
| stream_context before 1000 after 1000
|
|
|
|
| stats count() rows`, [][]Field{
|
|
|
|
{
|
2024-09-26 14:52:55 +00:00
|
|
|
{"rows", "1320"},
|
2024-06-28 17:14:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-11-07 12:15:52 +00:00
|
|
|
t.Run("pipe-join", func(t *testing.T) {
|
2024-11-06 13:22:13 +00:00
|
|
|
f(t, `'message 5' | stats by (instance) count() x
|
|
|
|
| join on (instance) (
|
|
|
|
'block 0' instance:host-1 | stats by (instance)
|
|
|
|
count() total,
|
|
|
|
count_uniq(stream-id) streams,
|
|
|
|
count_uniq(stream-id) x
|
|
|
|
)`, [][]Field{
|
|
|
|
{
|
|
|
|
{"instance", "host-0:234"},
|
|
|
|
{"x", "55"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"instance", "host-2:234"},
|
|
|
|
{"x", "55"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"instance", "host-1:234"},
|
|
|
|
{"x", "55"},
|
|
|
|
{"total", "77"},
|
|
|
|
{"streams", "1"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-11-07 12:15:52 +00:00
|
|
|
t.Run("pipe-join-prefix", func(t *testing.T) {
|
|
|
|
f(t, `'message 5' | stats by (instance) count() x
|
|
|
|
| join on (instance) (
|
|
|
|
'block 0' instance:host-1 | stats by (instance)
|
|
|
|
count() total,
|
|
|
|
count_uniq(stream-id) streams,
|
|
|
|
count_uniq(stream-id) x
|
|
|
|
) prefix "abc."`, [][]Field{
|
|
|
|
{
|
|
|
|
{"instance", "host-0:234"},
|
|
|
|
{"x", "55"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"instance", "host-2:234"},
|
|
|
|
{"x", "55"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"instance", "host-1:234"},
|
|
|
|
{"x", "55"},
|
|
|
|
{"abc.total", "77"},
|
|
|
|
{"abc.streams", "1"},
|
|
|
|
{"abc.x", "1"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2024-05-25 23:55:21 +00:00
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
// Close the storage and delete its data
|
|
|
|
s.MustClose()
|
|
|
|
fs.MustRemoveAll(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustParseQuery(query string) *Query {
|
|
|
|
q, err := ParseQuery(query)
|
|
|
|
if err != nil {
|
2024-05-25 23:55:21 +00:00
|
|
|
panic(fmt.Errorf("BUG: cannot parse [%s]: %w", query, err))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageSearch(t *testing.T) {
|
2024-06-20 01:08:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
path := t.Name()
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
const tenantsCount = 11
|
|
|
|
const streamsPerTenant = 3
|
|
|
|
const blocksPerStream = 5
|
|
|
|
const rowsPerBlock = 7
|
|
|
|
|
|
|
|
sc := &StorageConfig{
|
|
|
|
Retention: 24 * time.Hour,
|
|
|
|
}
|
|
|
|
s := MustOpenStorage(path, sc)
|
|
|
|
|
|
|
|
// fill the storage with data.
|
|
|
|
var allTenantIDs []TenantID
|
|
|
|
baseTimestamp := time.Now().UnixNano() - 3600*1e9
|
|
|
|
var fields []Field
|
|
|
|
streamTags := []string{
|
|
|
|
"job",
|
|
|
|
"instance",
|
|
|
|
}
|
|
|
|
for i := 0; i < tenantsCount; i++ {
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: uint32(i),
|
|
|
|
ProjectID: uint32(10*i + 1),
|
|
|
|
}
|
|
|
|
allTenantIDs = append(allTenantIDs, tenantID)
|
|
|
|
for j := 0; j < streamsPerTenant; j++ {
|
|
|
|
for k := 0; k < blocksPerStream; k++ {
|
2024-11-01 19:06:15 +00:00
|
|
|
lr := GetLogRows(streamTags, nil, nil, "")
|
2023-06-20 05:55:12 +00:00
|
|
|
for m := 0; m < rowsPerBlock; m++ {
|
|
|
|
timestamp := baseTimestamp + int64(m)*1e9 + int64(k)
|
|
|
|
// Append stream fields
|
|
|
|
fields = append(fields[:0], Field{
|
|
|
|
Name: "job",
|
|
|
|
Value: "foobar",
|
|
|
|
}, Field{
|
|
|
|
Name: "instance",
|
|
|
|
Value: fmt.Sprintf("host-%d:234", j),
|
|
|
|
})
|
|
|
|
// append the remaining fields
|
|
|
|
fields = append(fields, Field{
|
|
|
|
Name: "_msg",
|
|
|
|
Value: fmt.Sprintf("log message %d at block %d", m, k),
|
|
|
|
})
|
|
|
|
fields = append(fields, Field{
|
|
|
|
Name: "source-file",
|
|
|
|
Value: "/foo/bar/baz",
|
|
|
|
})
|
|
|
|
lr.MustAdd(tenantID, timestamp, fields)
|
|
|
|
}
|
2023-10-02 14:26:02 +00:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 05:55:12 +00:00
|
|
|
PutLogRows(lr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.debugFlush()
|
|
|
|
|
|
|
|
// run tests on the filled storage
|
|
|
|
const workersCount = 3
|
|
|
|
|
|
|
|
getBaseFilter := func(minTimestamp, maxTimestamp int64, sf *StreamFilter) filter {
|
|
|
|
var filters []filter
|
2024-05-12 14:33:29 +00:00
|
|
|
filters = append(filters, &filterTime{
|
2023-06-20 05:55:12 +00:00
|
|
|
minTimestamp: minTimestamp,
|
|
|
|
maxTimestamp: maxTimestamp,
|
|
|
|
})
|
|
|
|
if sf != nil {
|
2024-05-12 14:33:29 +00:00
|
|
|
filters = append(filters, &filterStream{
|
2023-06-20 05:55:12 +00:00
|
|
|
f: sf,
|
|
|
|
})
|
|
|
|
}
|
2024-05-12 14:33:29 +00:00
|
|
|
return &filterAnd{
|
2023-06-20 05:55:12 +00:00
|
|
|
filters: filters,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 01:04:21 +00:00
|
|
|
t.Run("missing-tenant-smaller-than-existing", func(_ *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 0,
|
|
|
|
ProjectID: 0,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, nil)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, _ *blockResult) {
|
2023-06-20 05:55:12 +00:00
|
|
|
panic(fmt.Errorf("unexpected match"))
|
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
})
|
2024-06-25 01:04:21 +00:00
|
|
|
t.Run("missing-tenant-bigger-than-existing", func(_ *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: tenantsCount + 1,
|
|
|
|
ProjectID: 0,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, nil)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, _ *blockResult) {
|
2023-06-20 05:55:12 +00:00
|
|
|
panic(fmt.Errorf("unexpected match"))
|
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
})
|
2024-06-25 01:04:21 +00:00
|
|
|
t.Run("missing-tenant-middle", func(_ *testing.T) {
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 0,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, nil)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, _ *blockResult) {
|
2023-06-20 05:55:12 +00:00
|
|
|
panic(fmt.Errorf("unexpected match"))
|
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
})
|
|
|
|
t.Run("matching-tenant-id", func(t *testing.T) {
|
|
|
|
for i := 0; i < tenantsCount; i++ {
|
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: uint32(i),
|
|
|
|
ProjectID: uint32(10*i + 1),
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, nil)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
rowsCountTotal.Add(uint32(br.rowsLen))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
|
|
|
|
expectedRowsCount := streamsPerTenant * blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of matching rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-multiple-tenant-ids", func(t *testing.T) {
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, nil)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions(allTenantIDs, f, []string{"_msg"})
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
rowsCountTotal.Add(uint32(br.rowsLen))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
|
|
|
|
expectedRowsCount := tenantsCount * streamsPerTenant * blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of matching rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
2024-06-25 01:04:21 +00:00
|
|
|
t.Run("stream-filter-mismatch", func(_ *testing.T) {
|
2024-05-20 02:08:30 +00:00
|
|
|
sf := mustNewTestStreamFilter(`{job="foobar",instance=~"host-.+:2345"}`)
|
2023-06-20 05:55:12 +00:00
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, sf)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions(allTenantIDs, f, []string{"_msg"})
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, _ *blockResult) {
|
2023-06-20 05:55:12 +00:00
|
|
|
panic(fmt.Errorf("unexpected match"))
|
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
})
|
|
|
|
t.Run("matching-stream-id", func(t *testing.T) {
|
|
|
|
for i := 0; i < streamsPerTenant; i++ {
|
2024-05-20 02:08:30 +00:00
|
|
|
sf := mustNewTestStreamFilter(fmt.Sprintf(`{job="foobar",instance="host-%d:234"}`, i))
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, sf)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
rowsCountTotal.Add(uint32(br.rowsLen))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
|
|
|
|
expectedRowsCount := blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-multiple-stream-ids", func(t *testing.T) {
|
2024-05-20 02:08:30 +00:00
|
|
|
sf := mustNewTestStreamFilter(`{job="foobar",instance=~"host-[^:]+:234"}`)
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, sf)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
rowsCountTotal.Add(uint32(br.rowsLen))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
|
|
|
|
expectedRowsCount := streamsPerTenant * blocksPerStream * rowsPerBlock
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-multiple-stream-ids-with-re-filter", func(t *testing.T) {
|
2024-05-20 02:08:30 +00:00
|
|
|
sf := mustNewTestStreamFilter(`{job="foobar",instance=~"host-[^:]+:234"}`)
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp
|
|
|
|
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, sf)
|
2024-05-12 14:33:29 +00:00
|
|
|
f = &filterAnd{
|
2023-06-20 05:55:12 +00:00
|
|
|
filters: []filter{
|
|
|
|
f,
|
2024-05-12 14:33:29 +00:00
|
|
|
&filterRegexp{
|
2023-06-20 05:55:12 +00:00
|
|
|
fieldName: "_msg",
|
2024-05-24 01:06:55 +00:00
|
|
|
re: mustCompileRegex("message [02] at "),
|
2023-06-20 05:55:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
rowsCountTotal.Add(uint32(br.rowsLen))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
|
|
|
|
expectedRowsCount := streamsPerTenant * blocksPerStream * 2
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("matching-stream-id-smaller-time-range", func(t *testing.T) {
|
2024-05-20 02:08:30 +00:00
|
|
|
sf := mustNewTestStreamFilter(`{job="foobar",instance="host-1:234"}`)
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp + (rowsPerBlock-2)*1e9
|
|
|
|
maxTimestamp := baseTimestamp + (rowsPerBlock-1)*1e9 - 1
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, sf)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-05-12 14:33:29 +00:00
|
|
|
var rowsCountTotal atomic.Uint32
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, br *blockResult) {
|
2024-09-25 14:16:53 +00:00
|
|
|
rowsCountTotal.Add(uint32(br.rowsLen))
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
|
|
|
|
expectedRowsCount := blocksPerStream
|
2024-05-12 14:33:29 +00:00
|
|
|
if n := rowsCountTotal.Load(); n != uint32(expectedRowsCount) {
|
2024-02-23 21:46:10 +00:00
|
|
|
t.Fatalf("unexpected number of rows; got %d; want %d", n, expectedRowsCount)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
})
|
2024-06-25 01:04:21 +00:00
|
|
|
t.Run("matching-stream-id-missing-time-range", func(_ *testing.T) {
|
2024-05-20 02:08:30 +00:00
|
|
|
sf := mustNewTestStreamFilter(`{job="foobar",instance="host-1:234"}`)
|
2023-06-20 05:55:12 +00:00
|
|
|
tenantID := TenantID{
|
|
|
|
AccountID: 1,
|
|
|
|
ProjectID: 11,
|
|
|
|
}
|
|
|
|
minTimestamp := baseTimestamp + (rowsPerBlock+1)*1e9
|
|
|
|
maxTimestamp := baseTimestamp + (rowsPerBlock+2)*1e9
|
|
|
|
f := getBaseFilter(minTimestamp, maxTimestamp, sf)
|
2024-06-28 17:14:29 +00:00
|
|
|
so := newTestGenericSearchOptions([]TenantID{tenantID}, f, []string{"_msg"})
|
2024-04-02 20:16:24 +00:00
|
|
|
processBlock := func(_ uint, _ *blockResult) {
|
2023-06-20 05:55:12 +00:00
|
|
|
panic(fmt.Errorf("unexpected match"))
|
|
|
|
}
|
|
|
|
s.search(workersCount, so, nil, processBlock)
|
|
|
|
})
|
|
|
|
|
|
|
|
s.MustClose()
|
|
|
|
fs.MustRemoveAll(path)
|
|
|
|
}
|
2024-05-22 19:01:20 +00:00
|
|
|
|
2024-05-25 19:36:16 +00:00
|
|
|
func TestParseStreamFieldsSuccess(t *testing.T) {
|
2024-06-20 01:08:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2024-05-22 19:01:20 +00:00
|
|
|
f := func(s, resultExpected string) {
|
|
|
|
t.Helper()
|
|
|
|
|
2024-05-25 19:36:16 +00:00
|
|
|
labels, err := parseStreamFields(nil, s)
|
2024-05-22 19:01:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2024-06-03 22:59:25 +00:00
|
|
|
result := MarshalFieldsToJSON(nil, labels)
|
2024-05-22 19:01:20 +00:00
|
|
|
if string(result) != resultExpected {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%s\nwant\n%s", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f(`{}`, `{}`)
|
|
|
|
f(`{foo="bar"}`, `{"foo":"bar"}`)
|
|
|
|
f(`{a="b",c="d"}`, `{"a":"b","c":"d"}`)
|
|
|
|
f(`{a="a=,b\"c}",b="d"}`, `{"a":"a=,b\"c}","b":"d"}`)
|
|
|
|
}
|
2024-06-28 17:14:29 +00:00
|
|
|
|
|
|
|
func newTestGenericSearchOptions(tenantIDs []TenantID, f filter, neededColumns []string) *genericSearchOptions {
|
|
|
|
return &genericSearchOptions{
|
|
|
|
tenantIDs: tenantIDs,
|
|
|
|
minTimestamp: math.MinInt64,
|
|
|
|
maxTimestamp: math.MaxInt64,
|
|
|
|
filter: f,
|
|
|
|
neededColumnNames: neededColumns,
|
|
|
|
}
|
|
|
|
}
|