hanahmily commented on a change in pull request #45: URL: https://github.com/apache/skywalking-banyandb/pull/45#discussion_r704329426
########## File path: banyand/stream/stream_query_test.go ########## @@ -0,0 +1,532 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package stream + +import ( + "bytes" + "embed" + _ "embed" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "sort" + "strconv" + "testing" + "time" + + "github.com/golang/protobuf/jsonpb" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/types/known/timestamppb" + + "github.com/apache/skywalking-banyandb/api/common" + commonv2 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v2" + databasev2 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v2" + modelv2 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v2" + streamv2 "github.com/apache/skywalking-banyandb/api/proto/banyandb/stream/v2" + "github.com/apache/skywalking-banyandb/banyand/tsdb" + "github.com/apache/skywalking-banyandb/pkg/convert" + "github.com/apache/skywalking-banyandb/pkg/index" + "github.com/apache/skywalking-banyandb/pkg/partition" +) + +type shardStruct struct { + id common.ShardID + location []string + elements []string +} + +type shardsForTest []shardStruct + +func Test_Stream_SelectShard(t *testing.T) { + tester := assert.New(t) + s, deferFunc := setup(tester) + defer deferFunc() + _ = setupQueryData(tester, "multiple_shards.json", s) + tests := []struct { + name string + entity tsdb.Entity + wantShardNum int + wantErr bool + }{ + { + name: "all shards", + wantShardNum: 2, + }, + { + name: "select a shard", + entity: tsdb.Entity{tsdb.Entry("webapp_id"), tsdb.Entry("10.0.0.1_id"), convert.Int64ToBytes(0)}, + wantShardNum: 1, + }, + { + name: "select shards", + entity: tsdb.Entity{tsdb.Entry("webapp_id"), tsdb.AnyEntry, convert.Int64ToBytes(0)}, + wantShardNum: 2, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + shards, err := s.Shards(tt.entity) + if tt.wantErr { + tester.Error(err) + return + } + tester.NoError(err) + tester.Equal(tt.wantShardNum, len(shards)) + }) + } + +} + +func Test_Stream_Series(t *testing.T) { + tester := assert.New(t) + s, deferFunc := setup(tester) + defer deferFunc() + baseTime := setupQueryData(tester, "multiple_shards.json", s) + tests := []struct { + name string + args queryOpts + want shardsForTest + wantErr bool + }{ + { + name: "all", + args: queryOpts{ + entity: tsdb.Entity{tsdb.AnyEntry, tsdb.AnyEntry, tsdb.AnyEntry}, + timeRange: tsdb.NewTimeRangeDuration(baseTime, 1*time.Hour), + }, + want: shardsForTest{ + { + id: 0, + location: []string{"series_12243341348514563931", "data_flow_0"}, + elements: []string{"1"}, + }, + { + id: 0, + location: []string{"series_1671844747554927007", "data_flow_0"}, + elements: []string{"2"}, + }, + { + id: 1, + location: []string{"series_2374367181827824198", "data_flow_0"}, + elements: []string{"5", "3"}, + }, + { + id: 1, + location: []string{"series_8429137420168685297", "data_flow_0"}, + elements: []string{"4"}, + }, + }, + }, + + { + name: "time range", + args: queryOpts{ + entity: tsdb.Entity{tsdb.AnyEntry, tsdb.AnyEntry, tsdb.AnyEntry}, + timeRange: tsdb.NewTimeRangeDuration(baseTime.Add(1500*time.Millisecond), 1*time.Hour), + }, + want: shardsForTest{ + { + id: 0, + location: []string{"series_12243341348514563931", "data_flow_0"}, + }, + { + id: 0, + location: []string{"series_1671844747554927007", "data_flow_0"}, + }, + { + id: 1, + location: []string{"series_2374367181827824198", "data_flow_0"}, + elements: []string{"5"}, + }, + { + id: 1, + location: []string{"series_8429137420168685297", "data_flow_0"}, + elements: []string{"4"}, + }, + }, + }, + { + name: "find series by service_id and instance_id", + args: queryOpts{ + entity: tsdb.Entity{tsdb.Entry("webapp_id"), tsdb.Entry("10.0.0.1_id"), tsdb.AnyEntry}, + timeRange: tsdb.NewTimeRangeDuration(baseTime, 1*time.Hour), + }, + want: shardsForTest{ + { + id: 0, + location: []string{"series_12243341348514563931", "data_flow_0"}, + elements: []string{"1"}, + }, + { + id: 1, + location: []string{"series_2374367181827824198", "data_flow_0"}, + elements: []string{"5", "3"}, + }, + }, + }, + { + name: "find a series", + args: queryOpts{ + entity: tsdb.Entity{tsdb.Entry("webapp_id"), tsdb.Entry("10.0.0.1_id"), convert.Uint64ToBytes(1)}, + timeRange: tsdb.NewTimeRangeDuration(baseTime, 1*time.Hour), + }, + want: shardsForTest{ + { + id: 1, + location: []string{"series_2374367181827824198", "data_flow_0"}, + elements: []string{"5", "3"}, + }, + }, + }, + { + name: "filter", + args: queryOpts{ + entity: tsdb.Entity{tsdb.AnyEntry, tsdb.AnyEntry, tsdb.AnyEntry}, + timeRange: tsdb.NewTimeRangeDuration(baseTime, 1*time.Hour), + buildFn: func(builder tsdb.SeekerBuilder) { + builder.Filter(&databasev2.IndexRule{ + Metadata: &commonv2.Metadata{ + Name: "endpoint_id", + Group: "default", + }, + Tags: []string{"endpoint_id"}, + Type: databasev2.IndexRule_TYPE_INVERTED, + Location: databasev2.IndexRule_LOCATION_SERIES, + }, tsdb.Condition{ + "endpoint_id": []index.ConditionValue{ + { + Op: modelv2.Condition_BINARY_OP_EQ, + Values: [][]byte{[]byte("/home_id")}, + }, + }, + }) + }, + }, + want: shardsForTest{ + { + id: 0, + location: []string{"series_12243341348514563931", "data_flow_0"}, + elements: []string{"1"}, + }, + { + id: 0, + location: []string{"series_1671844747554927007", "data_flow_0"}, + }, + { + id: 1, + location: []string{"series_2374367181827824198", "data_flow_0"}, + elements: []string{"3"}, + }, + { + id: 1, + location: []string{"series_8429137420168685297", "data_flow_0"}, + }, + }, + }, + { + name: "order by duration", + args: queryOpts{ + entity: tsdb.Entity{tsdb.AnyEntry, tsdb.AnyEntry, tsdb.AnyEntry}, + timeRange: tsdb.NewTimeRangeDuration(baseTime, 1*time.Hour), + buildFn: func(builder tsdb.SeekerBuilder) { + builder.OrderByIndex(&databasev2.IndexRule{ + Metadata: &commonv2.Metadata{ + Name: "duration", + Group: "default", + }, + Tags: []string{"duration"}, + Type: databasev2.IndexRule_TYPE_TREE, + Location: databasev2.IndexRule_LOCATION_SERIES, + }, modelv2.QueryOrder_SORT_ASC) + }, + }, + want: shardsForTest{ + { + id: 0, + location: []string{"series_12243341348514563931", "data_flow_0"}, + elements: []string{"1"}, + }, + { + id: 0, + location: []string{"series_1671844747554927007", "data_flow_0"}, + elements: []string{"2"}, + }, + { + id: 1, + location: []string{"series_2374367181827824198", "data_flow_0"}, + elements: []string{"3", "5"}, + }, + { + id: 1, + location: []string{"series_8429137420168685297", "data_flow_0"}, + elements: []string{"4"}, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := queryData(tester, s, tt.args) + if tt.wantErr { + tester.Error(err) + return + } + tester.NoError(err) + sort.SliceStable(got, func(i, j int) bool { + a := got[i] + b := got[j] + if a.id > b.id { + return false + } + for i, al := range a.location { + bl := b.location[i] + if bytes.Compare([]byte(al), []byte(bl)) > 0 { + return false + } + } + return true + }) + tester.Equal(tt.want, got) + }) + } + +} + +func Test_Stream_Global_Index(t *testing.T) { + tester := assert.New(t) + s, deferFunc := setup(tester) + defer deferFunc() + _ = setupQueryData(tester, "global_index.json", s) + tests := []struct { + name string + traceID string + wantTraceSegmentNum int + wantErr bool + }{ + { + name: "trace id is 1", + traceID: "1", + wantTraceSegmentNum: 2, + }, + { + name: "trace id is 2", + traceID: "2", + wantTraceSegmentNum: 3, + }, + { + name: "unknown trace id", + traceID: "foo", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + shards, errShards := s.Shards(nil) + tester.NoError(errShards) + err := func() error { + for _, shard := range shards { + itemIDs, err := shard.Index().Seek(index.Field{ Review comment: it belongs to a segment in a shard. It means the global indices could be updated from time to time. Segment and block are the internal objects, query module could query a segment-attracted index, the global index, from a shard. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
