github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/writer/packer_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 "bytes" 21 "fmt" 22 "os" 23 "time" 24 25 jsoniter "github.com/json-iterator/go" 26 log "github.com/sirupsen/logrus" 27 28 "github.com/stretchr/testify/assert" 29 30 "testing" 31 32 "github.com/siglens/siglens/pkg/config" 33 . "github.com/siglens/siglens/pkg/segment/structs" 34 . "github.com/siglens/siglens/pkg/segment/utils" 35 bbp "github.com/valyala/bytebufferpool" 36 ) 37 38 func TestBlockSumEncodeDecode(t *testing.T) { 39 rangeIndex = map[string]*Numbers{} 40 cases := []struct { 41 input string 42 }{ 43 { // case#1 44 `{ 45 "highTS": 1639219919769, 46 "lowTs": 1639219912421, 47 "recCount": 2 48 }`, 49 }, 50 { 51 //case#4 52 `{ 53 "highTS": 1639219919769, 54 "lowTs": 1639219912421, 55 "recCount": 0 56 }`, 57 }, 58 } 59 60 for i, test := range cases { 61 62 record_json := &BlockSummary{} 63 var json = jsoniter.ConfigCompatibleWithStandardLibrary 64 decoder := json.NewDecoder(bytes.NewReader([]byte(test.input))) 65 decoder.UseNumber() 66 err := decoder.Decode(&record_json) 67 if err != nil { 68 t.Errorf("testid: %d: Failed to parse json err:%v", i+1, err) 69 continue 70 } 71 72 encoded := make([]byte, WIP_SIZE) 73 bmh := &BlockMetadataHolder{ 74 ColumnBlockOffset: make(map[string]int64), 75 ColumnBlockLen: make(map[string]uint32), 76 } 77 bmh.ColumnBlockOffset["mycol"] = 29 78 bmh.ColumnBlockLen["mycol"] = 22 79 80 packedLen, _, err := EncodeBlocksum(bmh, record_json, encoded, 23) 81 82 t.Logf("encoded len: %v, origlen=%v", packedLen, len(test.input)) 83 84 assert.Nil(t, err) 85 86 t.Logf("input record_json=%v", record_json) 87 88 } 89 } 90 func TestRecordEncodeDecode(t *testing.T) { 91 config.InitializeTestingConfig() 92 defer os.RemoveAll(config.GetDataPath()) 93 cases := []struct { 94 input []byte 95 }{ 96 { // case#1 97 []byte(`{ 98 "a":"val1", 99 "b":1.456, 100 "c":true, 101 "d":"John", 102 "e":null 103 }`, 104 )}, 105 { // case#2 106 []byte(`{ 107 "a": 123456789012345678 108 }`, 109 )}, 110 { 111 //case#3 112 []byte(`{ 113 "f":-128, 114 "g":-2147483649 115 }`, 116 )}, 117 { 118 //case#4 119 []byte(`{ 120 "a":"val1", 121 "b":1.456, 122 "c":true, 123 "d":"John", 124 "e":null, 125 "f":-12, 126 "g":51456, 127 "h":7551456, 128 "i":13887551456, 129 "j":12, 130 "k":-200, 131 "l":-7551456, 132 "m":-3887551456, 133 "n":-1.323232 134 }`, 135 )}, 136 { 137 //case#5 138 []byte(`{ 139 "n":-1.323232, 140 "o":-12343435565.323232 141 }`, 142 )}, 143 } 144 for i, test := range cases { 145 cTime := uint64(time.Now().UnixMilli()) 146 sId := fmt.Sprintf("test-%d", i) 147 segstore, err := getSegStore(sId, cTime, "test", 0) 148 if err != nil { 149 log.Errorf("AddEntryToInMemBuf, getSegstore err=%v", err) 150 t.Errorf("failed to get segstore! %v", err) 151 } 152 tsKey := config.GetTimeStampKey() 153 maxIdx, _, err := segstore.EncodeColumns(test.input, cTime, &tsKey, SIGNAL_EVENTS) 154 155 t.Logf("encoded len: %v, origlen=%v", maxIdx, len(test.input)) 156 157 assert.Nil(t, err) 158 assert.GreaterOrEqual(t, maxIdx, uint32(0)) 159 colWips := allSegStores[sId].wipBlock.colWips 160 for key, colwip := range colWips { 161 val, _, _ := GetCvalFromRec(colwip.cbuf[colwip.cstartidx:colwip.cbufidx], 29) 162 log.Infof("recNum %+v col %+v:%+v. type %+v", i, key, val, val.Dtype) 163 } 164 } 165 } 166 167 func TestJaegerRecordEncodeDecode(t *testing.T) { 168 config.InitializeTestingConfig() 169 defer os.RemoveAll(config.GetDataPath()) 170 cases := []struct { 171 input []byte 172 }{ 173 { // case#1 174 []byte(`{"tags": [ 175 { 176 "key": "sampler.type", 177 "type": "string", 178 "value": "const" 179 }, 180 { 181 "key": "sampler.param", 182 "type": "bool", 183 "value": "true" 184 }, 185 { 186 "key": "http.status_code", 187 "type": "int64", 188 "value": "200" 189 }, 190 { 191 "key": "component", 192 "type": "string", 193 "value": "gRPC" 194 }, 195 { 196 "key": "retry_no", 197 "type": "int64", 198 "value": "1" 199 } 200 201 ], 202 "logs": [ 203 { 204 "timestamp": 1670445474307949, 205 "fields": [ 206 { 207 "key": "event", 208 "type": "string", 209 "value": "Searching for nearby drivers" 210 }, 211 { 212 "key": "level", 213 "type": "string", 214 "value": "info" 215 }, 216 { 217 "key": "location", 218 "type": "string", 219 "value": "577,322" 220 } 221 ] 222 }, 223 { 224 "timestamp": 1670445474370633, 225 "fields": [ 226 { 227 "key": "event", 228 "type": "string", 229 "value": "Retrying GetDriver after error" 230 }, 231 { 232 "key": "level", 233 "type": "string", 234 "value": "error" 235 }, 236 { 237 "key": "retry_no", 238 "type": "int64", 239 "value": "1" 240 }, 241 { 242 "key": "error", 243 "type": "string", 244 "value": "redis timeout" 245 } 246 247 248 ] 249 }], 250 }`, 251 )}, 252 } 253 for i, test := range cases { 254 cTime := uint64(time.Now().UnixMilli()) 255 sId := fmt.Sprintf("test-%d", i) 256 segstore, err := getSegStore(sId, cTime, "test", 0) 257 if err != nil { 258 log.Errorf("AddEntryToInMemBuf, getSegstore err=%v", err) 259 t.Errorf("failed to get segstore! %v", err) 260 } 261 tsKey := config.GetTimeStampKey() 262 maxIdx, _, err := segstore.EncodeColumns(test.input, cTime, &tsKey, SIGNAL_JAEGER_TRACES) 263 264 t.Logf("encoded len: %v, origlen=%v", maxIdx, len(test.input)) 265 266 assert.Nil(t, err) 267 assert.GreaterOrEqual(t, maxIdx, uint32(0)) 268 colWips := allSegStores[sId].wipBlock.colWips 269 for key, colwip := range colWips { 270 val, _, _ := GetCvalFromRec(colwip.cbuf[colwip.cstartidx:colwip.cbufidx], 29) 271 log.Infof("recNum %+v col %+v:%+v. type %+v", i, key, val, val.Dtype) 272 } 273 } 274 } 275 276 func TestTimestampRollups(t *testing.T) { 277 278 wipBlock := createMockTsRollupWipBlock("data/test-segkey") 279 280 // top-of-day validations 281 expectedData := make(map[uint64]uint16) 282 expectedData[1652227200000] = 412 283 expectedData[1652140800000] = 588 284 assert.Equal(t, len(expectedData), len(wipBlock.todRollup)) 285 for bkey, brup := range wipBlock.todRollup { 286 actualmrcount := uint16(brup.MatchedRes.GetNumberOfSetBits()) 287 expectedmrcount := expectedData[bkey] 288 assert.Equal(t, expectedmrcount, actualmrcount, "expectedmrcount=%v, actualmrcount=%v, bkey=%v", 289 expectedmrcount, actualmrcount, bkey) 290 } 291 292 // top-of-hour validations 293 expectedData = make(map[uint64]uint16) 294 expectedData[1652220000000] = 88 295 expectedData[1652223600000] = 500 296 expectedData[1652227200000] = 412 297 assert.Equal(t, len(expectedData), len(wipBlock.tohRollup)) 298 for bkey, brup := range wipBlock.tohRollup { 299 actualmrcount := uint16(brup.MatchedRes.GetNumberOfSetBits()) 300 expectedmrcount := expectedData[bkey] 301 assert.Equal(t, expectedmrcount, actualmrcount, "expectedmrcount=%v, actualmrcount=%v, bkey=%v", 302 expectedmrcount, actualmrcount, bkey) 303 } 304 305 // top-of-min validations 306 expectedData = make(map[uint64]uint16) 307 expectedData[1652224380000] = 8 308 expectedData[1652226300000] = 9 309 expectedData[1652226480000] = 9 310 expectedData[1652227140000] = 8 311 expectedData[1652227440000] = 8 312 expectedData[1652230020000] = 8 313 expectedData[1652223780000] = 9 314 expectedData[1652226060000] = 8 315 expectedData[1652228700000] = 8 316 expectedData[1652225220000] = 9 317 expectedData[1652227260000] = 8 318 expectedData[1652223240000] = 9 319 expectedData[1652225760000] = 9 320 expectedData[1652226120000] = 9 321 expectedData[1652227620000] = 8 322 expectedData[1652229900000] = 9 323 expectedData[1652225820000] = 8 324 expectedData[1652225880000] = 8 325 expectedData[1652226840000] = 9 326 expectedData[1652226900000] = 8 327 expectedData[1652227020000] = 9 328 expectedData[1652229960000] = 8 329 expectedData[1652227740000] = 9 330 expectedData[1652224440000] = 8 331 expectedData[1652226180000] = 8 332 expectedData[1652228340000] = 8 333 expectedData[1652229780000] = 8 334 expectedData[1652223540000] = 8 335 expectedData[1652223720000] = 8 336 expectedData[1652227680000] = 8 337 expectedData[1652228520000] = 8 338 expectedData[1652228640000] = 9 339 expectedData[1652227500000] = 8 340 expectedData[1652229600000] = 8 341 expectedData[1652224200000] = 8 342 expectedData[1652227080000] = 8 343 expectedData[1652227380000] = 9 344 expectedData[1652228220000] = 8 345 expectedData[1652228460000] = 9 346 expectedData[1652228820000] = 9 347 expectedData[1652222940000] = 5 348 expectedData[1652223000000] = 8 349 expectedData[1652224740000] = 8 350 expectedData[1652225700000] = 8 351 expectedData[1652228160000] = 8 352 expectedData[1652229360000] = 9 353 expectedData[1652224620000] = 8 354 expectedData[1652227980000] = 8 355 expectedData[1652228100000] = 9 356 expectedData[1652228940000] = 8 357 expectedData[1652229660000] = 8 358 expectedData[1652227920000] = 9 359 expectedData[1652224800000] = 8 360 expectedData[1652225460000] = 8 361 expectedData[1652225940000] = 9 362 expectedData[1652227320000] = 8 363 expectedData[1652229120000] = 8 364 expectedData[1652229300000] = 8 365 expectedData[1652224140000] = 9 366 expectedData[1652224260000] = 8 367 expectedData[1652225040000] = 9 368 expectedData[1652225520000] = 8 369 expectedData[1652226720000] = 8 370 expectedData[1652229240000] = 8 371 expectedData[1652224080000] = 8 372 expectedData[1652228760000] = 8 373 expectedData[1652229060000] = 8 374 expectedData[1652230080000] = 9 375 expectedData[1652224920000] = 8 376 expectedData[1652223120000] = 8 377 expectedData[1652224560000] = 8 378 expectedData[1652226660000] = 9 379 expectedData[1652230140000] = 3 380 expectedData[1652229420000] = 8 381 expectedData[1652224320000] = 9 382 expectedData[1652226360000] = 8 383 expectedData[1652226540000] = 8 384 expectedData[1652226780000] = 8 385 expectedData[1652227200000] = 9 386 expectedData[1652227560000] = 9 387 expectedData[1652226960000] = 8 388 expectedData[1652223480000] = 8 389 expectedData[1652223660000] = 8 390 expectedData[1652224860000] = 9 391 expectedData[1652225280000] = 8 392 expectedData[1652225340000] = 8 393 expectedData[1652226000000] = 8 394 expectedData[1652224020000] = 8 395 expectedData[1652228400000] = 8 396 expectedData[1652228580000] = 8 397 expectedData[1652229000000] = 9 398 expectedData[1652223840000] = 8 399 expectedData[1652224500000] = 9 400 expectedData[1652225100000] = 8 401 expectedData[1652223900000] = 8 402 expectedData[1652226240000] = 8 403 expectedData[1652225580000] = 9 404 expectedData[1652225640000] = 8 405 expectedData[1652227800000] = 8 406 expectedData[1652227860000] = 8 407 expectedData[1652228880000] = 8 408 expectedData[1652229840000] = 8 409 expectedData[1652223600000] = 9 410 expectedData[1652225400000] = 9 411 expectedData[1652226420000] = 8 412 expectedData[1652223180000] = 8 413 expectedData[1652226600000] = 8 414 expectedData[1652229480000] = 8 415 expectedData[1652229720000] = 9 416 expectedData[1652223360000] = 8 417 expectedData[1652224680000] = 9 418 expectedData[1652224980000] = 8 419 expectedData[1652225160000] = 8 420 expectedData[1652228040000] = 8 421 expectedData[1652229540000] = 9 422 expectedData[1652223060000] = 9 423 expectedData[1652223300000] = 8 424 expectedData[1652223420000] = 9 425 expectedData[1652223960000] = 9 426 expectedData[1652228280000] = 9 427 expectedData[1652229180000] = 9 428 assert.Equal(t, len(expectedData), len(wipBlock.tomRollup)) 429 for bkey, brup := range wipBlock.tomRollup { 430 actualmrcount := uint16(brup.MatchedRes.GetNumberOfSetBits()) 431 expectedmrcount := expectedData[bkey] 432 assert.Equal(t, expectedmrcount, actualmrcount, "expectedmrcount=%v, actualmrcount=%v, bkey=%v", 433 expectedmrcount, actualmrcount, bkey) 434 } 435 } 436 437 func Test_addSegStatsStr(t *testing.T) { 438 cname := "mycol1" 439 sst := make(map[string]*SegStats) 440 numRecs := uint64(2000) 441 442 bb := bbp.Get() 443 444 for i := uint64(0); i < numRecs; i++ { 445 addSegStatsStr(sst, cname, fmt.Sprintf("%v", i), bb) 446 } 447 448 assert.Equal(t, numRecs, sst[cname].Count) 449 } 450 451 func Test_addSegStatsNums(t *testing.T) { 452 453 cname := "mycol1" 454 sst := make(map[string]*SegStats) 455 bb := bbp.Get() 456 457 addSegStatsNums(sst, cname, SS_UINT64, 0, uint64(2345), 0, "2345", bb) 458 assert.NotEqual(t, SS_DT_FLOAT, sst[cname].NumStats.Min.Ntype) 459 assert.Equal(t, int64(2345), sst[cname].NumStats.Min.IntgrVal) 460 461 addSegStatsNums(sst, cname, SS_FLOAT64, 0, 0, float64(345.1), "345.1", bb) 462 assert.Equal(t, SS_DT_FLOAT, sst[cname].NumStats.Min.Ntype) 463 assert.Equal(t, float64(345.1), sst[cname].NumStats.Min.FloatVal) 464 465 assert.Equal(t, SS_DT_FLOAT, sst[cname].NumStats.Sum.Ntype) 466 assert.Equal(t, float64(345.1+2345), sst[cname].NumStats.Sum.FloatVal) 467 468 }