github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/writer/unrotatedquery_test.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package writer
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"strconv"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/google/uuid"
    28  	"github.com/siglens/siglens/pkg/config"
    29  	"github.com/siglens/siglens/pkg/querytracker"
    30  	"github.com/siglens/siglens/pkg/segment/structs"
    31  	"github.com/siglens/siglens/pkg/segment/utils"
    32  	log "github.com/sirupsen/logrus"
    33  	"github.com/stretchr/testify/assert"
    34  )
    35  
    36  func Test_writePQSFiles(t *testing.T) {
    37  	config.InitializeTestingConfig()
    38  	config.SetPQSEnabled(true)
    39  	InitWriterNode()
    40  	numBatch := 10
    41  	numRec := 100
    42  	numStreams := 10
    43  
    44  	value1, _ := utils.CreateDtypeEnclosure("batch-0", 0)
    45  	query := &structs.SearchQuery{
    46  		ExpressionFilter: &structs.SearchExpression{
    47  			LeftSearchInput:  &structs.SearchExpressionInput{ColumnName: "col3"},
    48  			FilterOp:         utils.Equals,
    49  			RightSearchInput: &structs.SearchExpressionInput{ColumnValue: value1},
    50  		},
    51  		SearchType: structs.SimpleExpression,
    52  	}
    53  
    54  	node := &structs.SearchNode{
    55  		AndSearchConditions: &structs.SearchCondition{
    56  			SearchQueries: []*structs.SearchQuery{query},
    57  		},
    58  		NodeType: structs.ColumnValueQuery,
    59  	}
    60  	node.AddQueryInfoForNode()
    61  
    62  	querytracker.UpdateQTUsage([]string{"test"}, node, nil)
    63  
    64  	for batch := 0; batch < numBatch; batch++ {
    65  		for rec := 0; rec < numRec; rec++ {
    66  			record := make(map[string]interface{})
    67  			record["col1"] = "abc"
    68  			record["col2"] = strconv.Itoa(rec)
    69  			record["col3"] = "batch-" + strconv.Itoa(batch%2)
    70  			record["col4"] = uuid.New().String()
    71  			record["timestamp"] = uint64(rec)
    72  
    73  			for stremNum := 0; stremNum < numStreams; stremNum++ {
    74  				record["col5"] = strconv.Itoa(stremNum)
    75  				streamid := fmt.Sprintf("stream-%d", stremNum)
    76  				raw, _ := json.Marshal(record)
    77  				err := AddEntryToInMemBuf(streamid, raw, uint64(rec), "test", 10, false, utils.SIGNAL_EVENTS, 0)
    78  				assert.Nil(t, err)
    79  			}
    80  		}
    81  
    82  		sleep := time.Duration(1 * time.Millisecond)
    83  		time.Sleep(sleep)
    84  		FlushWipBufferToFile(&sleep)
    85  	}
    86  
    87  	assert.Greaterf(t, TotalUnrotatedMetadataSizeBytes, uint64(0), "data in unrotated metadata == 0")
    88  	assert.Len(t, AllUnrotatedSegmentInfo, numStreams)
    89  	numLoadedUnrotated, totalUnrotated := GetUnrotatedMetadataInfo()
    90  	assert.Equal(t, numLoadedUnrotated, totalUnrotated)
    91  
    92  	pqid := querytracker.GetHashForQuery(node)
    93  	for segKey, usi := range AllUnrotatedSegmentInfo {
    94  		log.Infof("segkey is %+v", segKey)
    95  		spqmr, ok := usi.unrotatedPQSResults[pqid]
    96  		assert.True(t, ok, "unrotatedPQSResults[pqid] should exist")
    97  		assert.Equal(t, spqmr.GetNumBlocks(), uint16(numBatch))
    98  		for i := uint16(0); i < uint16(numBatch); i++ {
    99  			blkRes, ok := spqmr.GetBlockResults(i)
   100  			assert.True(t, ok, "blkRes should exist")
   101  			if i%2 == 0 {
   102  				assert.Equal(t, blkRes.GetNumberOfSetBits(), uint(numRec))
   103  			} else {
   104  				assert.Equal(t, blkRes.GetNumberOfSetBits(), uint(0))
   105  			}
   106  		}
   107  
   108  		assert.Len(t, usi.blockSummaries, numBatch)
   109  		assert.Len(t, usi.allColumns, 6)
   110  		assert.Len(t, usi.unrotatedBlockCmis, numBatch)
   111  		assert.Greater(t, usi.searchMetadataSize, uint64(0))
   112  		assert.Greater(t, usi.cmiSize, uint64(0))
   113  		assert.Equal(t, usi.TableName, "test")
   114  	}
   115  
   116  	os.RemoveAll(config.GetDataPath())
   117  }