github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/memtable_predicate_extractor_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package embedded_test 15 16 import ( 17 "context" 18 "regexp" 19 "sort" 20 "time" 21 22 "github.com/whtcorpsinc/BerolinaSQL" 23 . "github.com/whtcorpsinc/check" 24 causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded" 25 "github.com/whtcorpsinc/milevadb/causetstore/mockstore" 26 "github.com/whtcorpsinc/milevadb/ekv" 27 "github.com/whtcorpsinc/milevadb/petri" 28 "github.com/whtcorpsinc/milevadb/soliton/hint" 29 "github.com/whtcorpsinc/milevadb/soliton/set" 30 "github.com/whtcorpsinc/milevadb/stochastik" 31 ) 32 33 var _ = Suite(&extractorSuite{}) 34 35 type extractorSuite struct { 36 causetstore ekv.CausetStorage 37 dom *petri.Petri 38 } 39 40 func (s *extractorSuite) SetUpSuite(c *C) { 41 causetstore, err := mockstore.NewMockStore() 42 c.Assert(err, IsNil) 43 c.Assert(causetstore, NotNil) 44 45 stochastik.SetSchemaLease(0) 46 stochastik.DisableStats4Test() 47 dom, err := stochastik.BootstrapStochastik(causetstore) 48 c.Assert(err, IsNil) 49 c.Assert(dom, NotNil) 50 51 s.causetstore = causetstore 52 s.dom = dom 53 } 54 55 func (s *extractorSuite) TearDownSuite(c *C) { 56 s.dom.Close() 57 s.causetstore.Close() 58 } 59 60 func (s *extractorSuite) getLogicalMemBlock(c *C, se stochastik.Stochastik, BerolinaSQL *BerolinaSQL.BerolinaSQL, allegrosql string) *causetembedded.LogicalMemBlock { 61 stmt, err := BerolinaSQL.ParseOneStmt(allegrosql, "", "") 62 c.Assert(err, IsNil) 63 64 ctx := context.Background() 65 builder := causetembedded.NewCausetBuilder(se, s.dom.SchemaReplicant(), &hint.BlockHintProcessor{}) 66 plan, err := builder.Build(ctx, stmt) 67 c.Assert(err, IsNil) 68 69 logicalCauset, err := causetembedded.LogicalOptimize(ctx, builder.GetOptFlag(), plan.(causetembedded.LogicalCauset)) 70 c.Assert(err, IsNil) 71 72 // Obtain the leaf plan 73 leafCauset := logicalCauset 74 for len(leafCauset.Children()) > 0 { 75 leafCauset = leafCauset.Children()[0] 76 } 77 78 logicalMemBlock := leafCauset.(*causetembedded.LogicalMemBlock) 79 return logicalMemBlock 80 } 81 82 func (s *extractorSuite) TestClusterConfigBlockExtractor(c *C) { 83 se, err := stochastik.CreateStochastik4Test(s.causetstore) 84 c.Assert(err, IsNil) 85 86 BerolinaSQL := BerolinaSQL.New() 87 var cases = []struct { 88 allegrosql string 89 nodeTypes set.StringSet 90 instances set.StringSet 91 skipRequest bool 92 }{ 93 { 94 allegrosql: "select * from information_schema.cluster_config", 95 nodeTypes: nil, 96 instances: nil, 97 }, 98 { 99 allegrosql: "select * from information_schema.cluster_config where type='einsteindb'", 100 nodeTypes: set.NewStringSet("einsteindb"), 101 instances: set.NewStringSet(), 102 }, 103 { 104 allegrosql: "select * from information_schema.cluster_config where 'einsteindb'=type", 105 nodeTypes: set.NewStringSet("einsteindb"), 106 instances: set.NewStringSet(), 107 }, 108 { 109 allegrosql: "select * from information_schema.cluster_config where 'EinsteinDB'=type", 110 nodeTypes: set.NewStringSet("einsteindb"), 111 instances: set.NewStringSet(), 112 }, 113 { 114 allegrosql: "select * from information_schema.cluster_config where 'einsteindb'=type", 115 nodeTypes: set.NewStringSet("einsteindb"), 116 instances: set.NewStringSet(), 117 }, 118 { 119 allegrosql: "select * from information_schema.cluster_config where 'EinsteinDB'=type or type='milevadb'", 120 nodeTypes: set.NewStringSet("einsteindb", "milevadb"), 121 instances: set.NewStringSet(), 122 }, 123 { 124 allegrosql: "select * from information_schema.cluster_config where 'EinsteinDB'=type or type='milevadb' or type='fidel'", 125 nodeTypes: set.NewStringSet("einsteindb", "milevadb", "fidel"), 126 instances: set.NewStringSet(), 127 }, 128 { 129 allegrosql: "select * from information_schema.cluster_config where (type='milevadb' or type='fidel') and (instance='123.1.1.2:1234' or instance='123.1.1.4:1234')", 130 nodeTypes: set.NewStringSet("milevadb", "fidel"), 131 instances: set.NewStringSet("123.1.1.2:1234", "123.1.1.4:1234"), 132 }, 133 { 134 allegrosql: "select * from information_schema.cluster_config where type in ('einsteindb', 'fidel')", 135 nodeTypes: set.NewStringSet("einsteindb", "fidel"), 136 instances: set.NewStringSet(), 137 }, 138 { 139 allegrosql: "select * from information_schema.cluster_config where type in ('einsteindb', 'fidel') and instance='123.1.1.2:1234'", 140 nodeTypes: set.NewStringSet("einsteindb", "fidel"), 141 instances: set.NewStringSet("123.1.1.2:1234"), 142 }, 143 { 144 allegrosql: "select * from information_schema.cluster_config where type in ('einsteindb', 'fidel') and instance in ('123.1.1.2:1234', '123.1.1.4:1234')", 145 nodeTypes: set.NewStringSet("einsteindb", "fidel"), 146 instances: set.NewStringSet("123.1.1.2:1234", "123.1.1.4:1234"), 147 }, 148 { 149 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and instance in ('123.1.1.2:1234', '123.1.1.4:1234')", 150 nodeTypes: set.NewStringSet("einsteindb"), 151 instances: set.NewStringSet("123.1.1.2:1234", "123.1.1.4:1234"), 152 }, 153 { 154 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and instance='123.1.1.4:1234'", 155 nodeTypes: set.NewStringSet("einsteindb"), 156 instances: set.NewStringSet("123.1.1.4:1234"), 157 }, 158 { 159 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and instance='123.1.1.4:1234'", 160 nodeTypes: set.NewStringSet("einsteindb"), 161 instances: set.NewStringSet("123.1.1.4:1234"), 162 }, 163 { 164 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and instance='cNs2dm.einsteindb.whtcorpsinc.com:1234'", 165 nodeTypes: set.NewStringSet("einsteindb"), 166 instances: set.NewStringSet("cNs2dm.einsteindb.whtcorpsinc.com:1234"), 167 }, 168 { 169 allegrosql: "select * from information_schema.cluster_config where type='EinsteinDB' and instance='cNs2dm.einsteindb.whtcorpsinc.com:1234'", 170 nodeTypes: set.NewStringSet("einsteindb"), 171 instances: set.NewStringSet("cNs2dm.einsteindb.whtcorpsinc.com:1234"), 172 }, 173 { 174 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and type='fidel'", 175 nodeTypes: set.NewStringSet(), 176 instances: set.NewStringSet(), 177 skipRequest: true, 178 }, 179 { 180 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and type in ('fidel', 'einsteindb')", 181 nodeTypes: set.NewStringSet("einsteindb"), 182 instances: set.NewStringSet(), 183 }, 184 { 185 allegrosql: "select * from information_schema.cluster_config where type='einsteindb' and type in ('fidel', 'milevadb')", 186 nodeTypes: set.NewStringSet(), 187 instances: set.NewStringSet(), 188 skipRequest: true, 189 }, 190 { 191 allegrosql: "select * from information_schema.cluster_config where type in ('einsteindb', 'milevadb') and type in ('fidel', 'milevadb')", 192 nodeTypes: set.NewStringSet("milevadb"), 193 instances: set.NewStringSet(), 194 }, 195 { 196 allegrosql: "select * from information_schema.cluster_config where instance='123.1.1.4:1234' and instance='123.1.1.5:1234'", 197 nodeTypes: set.NewStringSet(), 198 instances: set.NewStringSet(), 199 skipRequest: true, 200 }, 201 { 202 allegrosql: "select * from information_schema.cluster_config where instance='123.1.1.4:1234' and instance in ('123.1.1.5:1234', '123.1.1.4:1234')", 203 nodeTypes: set.NewStringSet(), 204 instances: set.NewStringSet("123.1.1.4:1234"), 205 }, 206 { 207 allegrosql: "select * from information_schema.cluster_config where instance='123.1.1.4:1234' and instance in ('123.1.1.5:1234', '123.1.1.6:1234')", 208 nodeTypes: set.NewStringSet(), 209 instances: set.NewStringSet(), 210 skipRequest: true, 211 }, 212 { 213 allegrosql: "select * from information_schema.cluster_config where instance in ('123.1.1.5:1234', '123.1.1.4:1234') and instance in ('123.1.1.5:1234', '123.1.1.6:1234')", 214 nodeTypes: set.NewStringSet(), 215 instances: set.NewStringSet("123.1.1.5:1234"), 216 }, 217 { 218 allegrosql: `select * from information_schema.cluster_config 219 where instance in ('123.1.1.5:1234', '123.1.1.4:1234') 220 and instance in ('123.1.1.5:1234', '123.1.1.6:1234') 221 and type in ('einsteindb', 'milevadb') 222 and type in ('fidel', 'milevadb')`, 223 nodeTypes: set.NewStringSet("milevadb"), 224 instances: set.NewStringSet("123.1.1.5:1234"), 225 }, 226 { 227 allegrosql: `select * from information_schema.cluster_config 228 where instance in ('123.1.1.5:1234', '123.1.1.4:1234') 229 and instance in ('123.1.1.5:1234', '123.1.1.6:1234') 230 and instance in ('123.1.1.6:1234', '123.1.1.7:1234') 231 and instance in ('123.1.1.7:1234', '123.1.1.8:1234')`, 232 nodeTypes: set.NewStringSet(), 233 instances: set.NewStringSet(), 234 skipRequest: true, 235 }, 236 } 237 for _, ca := range cases { 238 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 239 c.Assert(logicalMemBlock.Extractor, NotNil) 240 241 clusterConfigExtractor := logicalMemBlock.Extractor.(*causetembedded.ClusterBlockExtractor) 242 c.Assert(clusterConfigExtractor.NodeTypes, DeepEquals, ca.nodeTypes, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 243 c.Assert(clusterConfigExtractor.Instances, DeepEquals, ca.instances, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 244 c.Assert(clusterConfigExtractor.SkipRequest, DeepEquals, ca.skipRequest, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 245 } 246 } 247 248 func timestamp(c *C, s string) int64 { 249 t, err := time.ParseInLocation("2006-01-02 15:04:05.999", s, time.Local) 250 c.Assert(err, IsNil) 251 return t.UnixNano() / int64(time.Millisecond) 252 } 253 254 func (s *extractorSuite) TestClusterLogBlockExtractor(c *C) { 255 se, err := stochastik.CreateStochastik4Test(s.causetstore) 256 c.Assert(err, IsNil) 257 258 BerolinaSQL := BerolinaSQL.New() 259 var cases = []struct { 260 allegrosql string 261 nodeTypes set.StringSet 262 instances set.StringSet 263 skipRequest bool 264 startTime, endTime int64 265 patterns []string 266 level set.StringSet 267 }{ 268 { 269 allegrosql: "select * from information_schema.cluster_log", 270 nodeTypes: nil, 271 instances: nil, 272 }, 273 { 274 // Test for invalid time. 275 allegrosql: "select * from information_schema.cluster_log where time='2020-10-10 10::10'", 276 nodeTypes: set.NewStringSet(), 277 instances: set.NewStringSet(), 278 }, 279 { 280 allegrosql: "select * from information_schema.cluster_log where type='einsteindb'", 281 nodeTypes: set.NewStringSet("einsteindb"), 282 instances: set.NewStringSet(), 283 }, 284 { 285 allegrosql: "select * from information_schema.cluster_log where 'einsteindb'=type", 286 nodeTypes: set.NewStringSet("einsteindb"), 287 instances: set.NewStringSet(), 288 }, 289 { 290 allegrosql: "select * from information_schema.cluster_log where 'EinsteinDB'=type", 291 nodeTypes: set.NewStringSet("einsteindb"), 292 instances: set.NewStringSet(), 293 }, 294 { 295 allegrosql: "select * from information_schema.cluster_log where 'EinsteinDB'=type or type='milevadb'", 296 nodeTypes: set.NewStringSet("einsteindb", "milevadb"), 297 instances: set.NewStringSet(), 298 }, 299 { 300 allegrosql: "select * from information_schema.cluster_log where 'EinsteinDB'=type or type='milevadb' or type='fidel'", 301 nodeTypes: set.NewStringSet("einsteindb", "milevadb", "fidel"), 302 instances: set.NewStringSet(), 303 }, 304 { 305 allegrosql: "select * from information_schema.cluster_log where (type='milevadb' or type='fidel') and (instance='123.1.1.2:1234' or instance='123.1.1.4:1234')", 306 nodeTypes: set.NewStringSet("milevadb", "fidel"), 307 instances: set.NewStringSet("123.1.1.2:1234", "123.1.1.4:1234"), 308 }, 309 { 310 allegrosql: "select * from information_schema.cluster_log where type in ('einsteindb', 'fidel')", 311 nodeTypes: set.NewStringSet("einsteindb", "fidel"), 312 instances: set.NewStringSet(), 313 }, 314 { 315 allegrosql: "select * from information_schema.cluster_log where type in ('einsteindb', 'fidel') and instance='123.1.1.2:1234'", 316 nodeTypes: set.NewStringSet("einsteindb", "fidel"), 317 instances: set.NewStringSet("123.1.1.2:1234"), 318 }, 319 { 320 allegrosql: "select * from information_schema.cluster_log where type in ('einsteindb', 'fidel') and instance in ('123.1.1.2:1234', '123.1.1.4:1234')", 321 nodeTypes: set.NewStringSet("einsteindb", "fidel"), 322 instances: set.NewStringSet("123.1.1.2:1234", "123.1.1.4:1234"), 323 }, 324 { 325 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and instance in ('123.1.1.2:1234', '123.1.1.4:1234')", 326 nodeTypes: set.NewStringSet("einsteindb"), 327 instances: set.NewStringSet("123.1.1.2:1234", "123.1.1.4:1234"), 328 }, 329 { 330 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and instance='123.1.1.4:1234'", 331 nodeTypes: set.NewStringSet("einsteindb"), 332 instances: set.NewStringSet("123.1.1.4:1234"), 333 }, 334 { 335 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and instance='123.1.1.4:1234'", 336 nodeTypes: set.NewStringSet("einsteindb"), 337 instances: set.NewStringSet("123.1.1.4:1234"), 338 }, 339 { 340 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and instance='cNs2dm.einsteindb.whtcorpsinc.com:1234'", 341 nodeTypes: set.NewStringSet("einsteindb"), 342 instances: set.NewStringSet("cNs2dm.einsteindb.whtcorpsinc.com:1234"), 343 }, 344 { 345 allegrosql: "select * from information_schema.cluster_log where type='EinsteinDB' and instance='cNs2dm.einsteindb.whtcorpsinc.com:1234'", 346 nodeTypes: set.NewStringSet("einsteindb"), 347 instances: set.NewStringSet("cNs2dm.einsteindb.whtcorpsinc.com:1234"), 348 }, 349 { 350 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and type='fidel'", 351 nodeTypes: set.NewStringSet(), 352 instances: set.NewStringSet(), 353 skipRequest: true, 354 }, 355 { 356 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and type in ('fidel', 'einsteindb')", 357 nodeTypes: set.NewStringSet("einsteindb"), 358 instances: set.NewStringSet(), 359 }, 360 { 361 allegrosql: "select * from information_schema.cluster_log where type='einsteindb' and type in ('fidel', 'milevadb')", 362 nodeTypes: set.NewStringSet(), 363 instances: set.NewStringSet(), 364 skipRequest: true, 365 }, 366 { 367 allegrosql: "select * from information_schema.cluster_log where type in ('einsteindb', 'milevadb') and type in ('fidel', 'milevadb')", 368 nodeTypes: set.NewStringSet("milevadb"), 369 instances: set.NewStringSet(), 370 }, 371 { 372 allegrosql: "select * from information_schema.cluster_log where instance='123.1.1.4:1234' and instance='123.1.1.5:1234'", 373 nodeTypes: set.NewStringSet(), 374 instances: set.NewStringSet(), 375 skipRequest: true, 376 }, 377 { 378 allegrosql: "select * from information_schema.cluster_log where instance='123.1.1.4:1234' and instance in ('123.1.1.5:1234', '123.1.1.4:1234')", 379 nodeTypes: set.NewStringSet(), 380 instances: set.NewStringSet("123.1.1.4:1234"), 381 }, 382 { 383 allegrosql: "select * from information_schema.cluster_log where instance='123.1.1.4:1234' and instance in ('123.1.1.5:1234', '123.1.1.6:1234')", 384 nodeTypes: set.NewStringSet(), 385 instances: set.NewStringSet(), 386 skipRequest: true, 387 }, 388 { 389 allegrosql: "select * from information_schema.cluster_log where instance in ('123.1.1.5:1234', '123.1.1.4:1234') and instance in ('123.1.1.5:1234', '123.1.1.6:1234')", 390 nodeTypes: set.NewStringSet(), 391 instances: set.NewStringSet("123.1.1.5:1234"), 392 }, 393 { 394 allegrosql: `select * from information_schema.cluster_log 395 where instance in ('123.1.1.5:1234', '123.1.1.4:1234') 396 and instance in ('123.1.1.5:1234', '123.1.1.6:1234') 397 and type in ('einsteindb', 'milevadb') 398 and type in ('fidel', 'milevadb')`, 399 nodeTypes: set.NewStringSet("milevadb"), 400 instances: set.NewStringSet("123.1.1.5:1234"), 401 }, 402 { 403 allegrosql: `select * from information_schema.cluster_log 404 where instance in ('123.1.1.5:1234', '123.1.1.4:1234') 405 and instance in ('123.1.1.5:1234', '123.1.1.6:1234') 406 and instance in ('123.1.1.6:1234', '123.1.1.7:1234') 407 and instance in ('123.1.1.7:1234', '123.1.1.8:1234')`, 408 nodeTypes: set.NewStringSet(), 409 instances: set.NewStringSet(), 410 skipRequest: true, 411 }, 412 { 413 allegrosql: "select * from information_schema.cluster_log where time='2020-10-10 10:10:10'", 414 nodeTypes: set.NewStringSet(), 415 instances: set.NewStringSet(), 416 startTime: timestamp(c, "2020-10-10 10:10:10"), 417 endTime: timestamp(c, "2020-10-10 10:10:10"), 418 }, 419 { 420 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-10 10:10:10' and time<='2020-10-11 10:10:10'", 421 nodeTypes: set.NewStringSet(), 422 instances: set.NewStringSet(), 423 startTime: timestamp(c, "2020-10-10 10:10:10"), 424 endTime: timestamp(c, "2020-10-11 10:10:10"), 425 }, 426 { 427 allegrosql: "select * from information_schema.cluster_log where time>'2020-10-10 10:10:10' and time<'2020-10-11 10:10:10'", 428 nodeTypes: set.NewStringSet(), 429 instances: set.NewStringSet(), 430 startTime: timestamp(c, "2020-10-10 10:10:10") + 1, 431 endTime: timestamp(c, "2020-10-11 10:10:10") - 1, 432 }, 433 { 434 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-10 10:10:10' and time<'2020-10-11 10:10:10'", 435 nodeTypes: set.NewStringSet(), 436 instances: set.NewStringSet(), 437 startTime: timestamp(c, "2020-10-10 10:10:10"), 438 endTime: timestamp(c, "2020-10-11 10:10:10") - 1, 439 }, 440 { 441 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-12 10:10:10' and time<'2020-10-11 10:10:10'", 442 nodeTypes: set.NewStringSet(), 443 instances: set.NewStringSet(), 444 startTime: timestamp(c, "2020-10-12 10:10:10"), 445 endTime: timestamp(c, "2020-10-11 10:10:10") - 1, 446 skipRequest: true, 447 }, 448 { 449 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-10 10:10:10'", 450 nodeTypes: set.NewStringSet(), 451 instances: set.NewStringSet(), 452 startTime: timestamp(c, "2020-10-10 10:10:10"), 453 }, 454 { 455 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-10 10:10:10' and time>='2020-10-11 10:10:10' and time>='2020-10-12 10:10:10'", 456 nodeTypes: set.NewStringSet(), 457 instances: set.NewStringSet(), 458 startTime: timestamp(c, "2020-10-12 10:10:10"), 459 }, 460 { 461 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-10 10:10:10' and time>='2020-10-11 10:10:10' and time>='2020-10-12 10:10:10' and time='2020-10-13 10:10:10'", 462 nodeTypes: set.NewStringSet(), 463 instances: set.NewStringSet(), 464 startTime: timestamp(c, "2020-10-13 10:10:10"), 465 endTime: timestamp(c, "2020-10-13 10:10:10"), 466 }, 467 { 468 allegrosql: "select * from information_schema.cluster_log where time<='2020-10-10 10:10:10' and time='2020-10-13 10:10:10'", 469 nodeTypes: set.NewStringSet(), 470 instances: set.NewStringSet(), 471 startTime: timestamp(c, "2020-10-13 10:10:10"), 472 endTime: timestamp(c, "2020-10-10 10:10:10"), 473 skipRequest: true, 474 }, 475 { 476 allegrosql: "select * from information_schema.cluster_log where time='2020-10-10 10:10:10' and time<='2020-10-13 10:10:10'", 477 nodeTypes: set.NewStringSet(), 478 instances: set.NewStringSet(), 479 startTime: timestamp(c, "2020-10-10 10:10:10"), 480 endTime: timestamp(c, "2020-10-10 10:10:10"), 481 }, 482 { 483 allegrosql: "select * from information_schema.cluster_log where time>='2020-10-10 10:10:10' and message like '%a%'", 484 nodeTypes: set.NewStringSet(), 485 instances: set.NewStringSet(), 486 startTime: timestamp(c, "2020-10-10 10:10:10"), 487 patterns: []string{".*a.*"}, 488 }, 489 { 490 allegrosql: "select * from information_schema.cluster_log where message like '%a%' and message regexp '^b'", 491 nodeTypes: set.NewStringSet(), 492 instances: set.NewStringSet(), 493 patterns: []string{".*a.*", "^b"}, 494 }, 495 { 496 allegrosql: "select * from information_schema.cluster_log where message='gc'", 497 nodeTypes: set.NewStringSet(), 498 instances: set.NewStringSet(), 499 patterns: []string{"^gc$"}, 500 }, 501 { 502 allegrosql: "select * from information_schema.cluster_log where message='.*txn.*'", 503 nodeTypes: set.NewStringSet(), 504 instances: set.NewStringSet(), 505 patterns: []string{"^" + regexp.QuoteMeta(".*txn.*") + "$"}, 506 }, 507 { 508 allegrosql: `select * from information_schema.cluster_log 509 where instance in ('123.1.1.5:1234', '123.1.1.4:1234') 510 and (type='milevadb' or type='fidel') 511 and message like '%interlock%' 512 and message regexp '.*txn=123.*' 513 and level in ('debug', 'info', 'ERROR')`, 514 nodeTypes: set.NewStringSet("milevadb", "fidel"), 515 instances: set.NewStringSet("123.1.1.5:1234", "123.1.1.4:1234"), 516 level: set.NewStringSet("debug", "info", "error"), 517 patterns: []string{".*interlock.*", ".*txn=123.*"}, 518 }, 519 { 520 allegrosql: "select * from information_schema.cluster_log where (message regexp '.*fidel.*' or message regexp '.*milevadb.*' or message like '%einsteindb%')", 521 nodeTypes: set.NewStringSet(), 522 instances: set.NewStringSet(), 523 patterns: []string{".*fidel.*|.*milevadb.*|.*einsteindb.*"}, 524 }, 525 { 526 allegrosql: "select * from information_schema.cluster_log where (level = 'debug' or level = 'ERROR')", 527 nodeTypes: set.NewStringSet(), 528 instances: set.NewStringSet(), 529 level: set.NewStringSet("debug", "error"), 530 }, 531 } 532 for _, ca := range cases { 533 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 534 c.Assert(logicalMemBlock.Extractor, NotNil) 535 536 clusterConfigExtractor := logicalMemBlock.Extractor.(*causetembedded.ClusterLogBlockExtractor) 537 c.Assert(clusterConfigExtractor.NodeTypes, DeepEquals, ca.nodeTypes, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 538 c.Assert(clusterConfigExtractor.Instances, DeepEquals, ca.instances, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 539 c.Assert(clusterConfigExtractor.SkipRequest, DeepEquals, ca.skipRequest, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 540 if ca.startTime > 0 { 541 c.Assert(clusterConfigExtractor.StartTime, Equals, ca.startTime, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 542 } 543 if ca.endTime > 0 { 544 c.Assert(clusterConfigExtractor.EndTime, Equals, ca.endTime, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 545 } 546 c.Assert(clusterConfigExtractor.Patterns, DeepEquals, ca.patterns, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 547 if len(ca.level) > 0 { 548 c.Assert(clusterConfigExtractor.LogLevels, DeepEquals, ca.level, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 549 } 550 } 551 } 552 553 func (s *extractorSuite) TestMetricBlockExtractor(c *C) { 554 se, err := stochastik.CreateStochastik4Test(s.causetstore) 555 c.Assert(err, IsNil) 556 557 parseTime := func(c *C, s string) time.Time { 558 t, err := time.ParseInLocation(causetembedded.MetricBlockTimeFormat, s, time.Local) 559 c.Assert(err, IsNil) 560 return t 561 } 562 563 BerolinaSQL := BerolinaSQL.New() 564 var cases = []struct { 565 allegrosql string 566 skipRequest bool 567 startTime, endTime time.Time 568 labelConditions map[string]set.StringSet 569 quantiles []float64 570 promQL string 571 }{ 572 { 573 allegrosql: "select * from metrics_schema.milevadb_query_duration", 574 promQL: "histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))", 575 }, 576 { 577 allegrosql: "select * from metrics_schema.milevadb_query_duration where instance='127.0.0.1:10080'", 578 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{instance="127.0.0.1:10080"}[60s])) by (le,sql_type,instance))`, 579 labelConditions: map[string]set.StringSet{ 580 "instance": set.NewStringSet("127.0.0.1:10080"), 581 }, 582 }, 583 { 584 allegrosql: "select * from metrics_schema.milevadb_query_duration where instance='127.0.0.1:10080' or instance='127.0.0.1:10081'", 585 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{instance=~"127.0.0.1:10080|127.0.0.1:10081"}[60s])) by (le,sql_type,instance))`, 586 labelConditions: map[string]set.StringSet{ 587 "instance": set.NewStringSet("127.0.0.1:10080", "127.0.0.1:10081"), 588 }, 589 }, 590 { 591 allegrosql: "select * from metrics_schema.milevadb_query_duration where instance='127.0.0.1:10080' and sql_type='general'", 592 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{instance="127.0.0.1:10080",sql_type="general"}[60s])) by (le,sql_type,instance))`, 593 labelConditions: map[string]set.StringSet{ 594 "instance": set.NewStringSet("127.0.0.1:10080"), 595 "sql_type": set.NewStringSet("general"), 596 }, 597 }, 598 { 599 allegrosql: "select * from metrics_schema.milevadb_query_duration where instance='127.0.0.1:10080' or sql_type='general'", 600 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))`, 601 }, 602 { 603 allegrosql: "select * from metrics_schema.milevadb_query_duration where instance='127.0.0.1:10080' and sql_type='UFIDelate' and time='2020-10-10 10:10:10'", 604 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{instance="127.0.0.1:10080",sql_type="UFIDelate"}[60s])) by (le,sql_type,instance))`, 605 labelConditions: map[string]set.StringSet{ 606 "instance": set.NewStringSet("127.0.0.1:10080"), 607 "sql_type": set.NewStringSet("UFIDelate"), 608 }, 609 startTime: parseTime(c, "2020-10-10 10:10:10"), 610 endTime: parseTime(c, "2020-10-10 10:10:10"), 611 }, 612 { 613 allegrosql: "select * from metrics_schema.milevadb_query_duration where time>'2020-10-10 10:10:10' and time<'2020-10-11 10:10:10'", 614 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))`, 615 startTime: parseTime(c, "2020-10-10 10:10:10.001"), 616 endTime: parseTime(c, "2020-10-11 10:10:09.999"), 617 }, 618 { 619 allegrosql: "select * from metrics_schema.milevadb_query_duration where time>='2020-10-10 10:10:10'", 620 promQL: `histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))`, 621 startTime: parseTime(c, "2020-10-10 10:10:10"), 622 endTime: parseTime(c, "2020-10-10 10:20:10"), 623 }, 624 { 625 allegrosql: "select * from metrics_schema.milevadb_query_duration where time>='2020-10-10 10:10:10' and time<='2020-10-09 10:10:10'", 626 promQL: "", 627 startTime: parseTime(c, "2020-10-10 10:10:10"), 628 endTime: parseTime(c, "2020-10-09 10:10:10"), 629 skipRequest: true, 630 }, 631 { 632 allegrosql: "select * from metrics_schema.milevadb_query_duration where time<='2020-10-09 10:10:10'", 633 promQL: "histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))", 634 startTime: parseTime(c, "2020-10-09 10:00:10"), 635 endTime: parseTime(c, "2020-10-09 10:10:10"), 636 }, 637 { 638 allegrosql: "select * from metrics_schema.milevadb_query_duration where quantile=0.9 or quantile=0.8", 639 promQL: "histogram_quantile(0.8, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))," + 640 "histogram_quantile(0.9, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))", 641 quantiles: []float64{0.8, 0.9}, 642 }, 643 { 644 allegrosql: "select * from metrics_schema.milevadb_query_duration where quantile=0", 645 promQL: "histogram_quantile(0, sum(rate(milevadb_server_handle_query_duration_seconds_bucket{}[60s])) by (le,sql_type,instance))", 646 quantiles: []float64{0}, 647 }, 648 } 649 se.GetStochastikVars().StmtCtx.TimeZone = time.Local 650 for _, ca := range cases { 651 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 652 c.Assert(logicalMemBlock.Extractor, NotNil) 653 metricBlockExtractor := logicalMemBlock.Extractor.(*causetembedded.MetricBlockExtractor) 654 if len(ca.labelConditions) > 0 { 655 c.Assert(metricBlockExtractor.LabelConditions, DeepEquals, ca.labelConditions, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 656 } 657 c.Assert(metricBlockExtractor.SkipRequest, DeepEquals, ca.skipRequest, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 658 if len(metricBlockExtractor.Quantiles) > 0 { 659 c.Assert(metricBlockExtractor.Quantiles, DeepEquals, ca.quantiles) 660 } 661 if !ca.skipRequest { 662 promQL := metricBlockExtractor.GetMetricBlockPromQL(se, "milevadb_query_duration") 663 c.Assert(promQL, DeepEquals, ca.promQL, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 664 start, end := metricBlockExtractor.StartTime, metricBlockExtractor.EndTime 665 c.Assert(start.UnixNano() <= end.UnixNano(), IsTrue) 666 if ca.startTime.Unix() > 0 { 667 c.Assert(metricBlockExtractor.StartTime, DeepEquals, ca.startTime, Commentf("ALLEGROALLEGROSQL: %v, start_time: %v", ca.allegrosql, metricBlockExtractor.StartTime)) 668 } 669 if ca.endTime.Unix() > 0 { 670 c.Assert(metricBlockExtractor.EndTime, DeepEquals, ca.endTime, Commentf("ALLEGROALLEGROSQL: %v, end_time: %v", ca.allegrosql, metricBlockExtractor.EndTime)) 671 } 672 } 673 } 674 } 675 676 func (s *extractorSuite) TestMetricsSummaryBlockExtractor(c *C) { 677 se, err := stochastik.CreateStochastik4Test(s.causetstore) 678 c.Assert(err, IsNil) 679 680 var cases = []struct { 681 allegrosql string 682 names set.StringSet 683 quantiles []float64 684 skipRequest bool 685 }{ 686 { 687 allegrosql: "select * from information_schema.metrics_summary", 688 }, 689 { 690 allegrosql: "select * from information_schema.metrics_summary where quantile='0.999'", 691 quantiles: []float64{0.999}, 692 }, 693 { 694 allegrosql: "select * from information_schema.metrics_summary where '0.999'=quantile or quantile='0.95'", 695 quantiles: []float64{0.999, 0.95}, 696 }, 697 { 698 allegrosql: "select * from information_schema.metrics_summary where '0.999'=quantile or quantile='0.95' or quantile='0.99'", 699 quantiles: []float64{0.999, 0.95, 0.99}, 700 }, 701 { 702 allegrosql: "select * from information_schema.metrics_summary where (quantile='0.95' or quantile='0.99') and (metrics_name='metric_name3' or metrics_name='metric_name1')", 703 quantiles: []float64{0.95, 0.99}, 704 names: set.NewStringSet("metric_name3", "metric_name1"), 705 }, 706 { 707 allegrosql: "select * from information_schema.metrics_summary where quantile in ('0.999', '0.99')", 708 quantiles: []float64{0.999, 0.99}, 709 }, 710 { 711 allegrosql: "select * from information_schema.metrics_summary where quantile in ('0.999', '0.99') and metrics_name='metric_name1'", 712 quantiles: []float64{0.999, 0.99}, 713 names: set.NewStringSet("metric_name1"), 714 }, 715 { 716 allegrosql: "select * from information_schema.metrics_summary where quantile in ('0.999', '0.99') and metrics_name in ('metric_name1', 'metric_name2')", 717 quantiles: []float64{0.999, 0.99}, 718 names: set.NewStringSet("metric_name1", "metric_name2"), 719 }, 720 { 721 allegrosql: "select * from information_schema.metrics_summary where quantile='0.999' and metrics_name in ('metric_name1', 'metric_name2')", 722 quantiles: []float64{0.999}, 723 names: set.NewStringSet("metric_name1", "metric_name2"), 724 }, 725 { 726 allegrosql: "select * from information_schema.metrics_summary where quantile='0.999' and metrics_name='metric_NAME3'", 727 quantiles: []float64{0.999}, 728 names: set.NewStringSet("metric_name3"), 729 }, 730 { 731 allegrosql: "select * from information_schema.metrics_summary where quantile='0.999' and quantile in ('0.99', '0.999')", 732 quantiles: []float64{0.999}, 733 }, 734 { 735 allegrosql: "select * from information_schema.metrics_summary where quantile in ('0.999', '0.95') and quantile in ('0.99', '0.95')", 736 quantiles: []float64{0.95}, 737 }, 738 { 739 allegrosql: `select * from information_schema.metrics_summary 740 where metrics_name in ('metric_name1', 'metric_name4') 741 and metrics_name in ('metric_name5', 'metric_name4') 742 and quantile in ('0.999', '0.95') 743 and quantile in ('0.99', '0.95') 744 and quantile in (0.80, 0.90)`, 745 skipRequest: true, 746 }, 747 { 748 allegrosql: `select * from information_schema.metrics_summary 749 where metrics_name in ('metric_name1', 'metric_name4') 750 and metrics_name in ('metric_name5', 'metric_name4') 751 and metrics_name in ('metric_name5', 'metric_name1') 752 and metrics_name in ('metric_name1', 'metric_name3')`, 753 skipRequest: true, 754 }, 755 } 756 BerolinaSQL := BerolinaSQL.New() 757 for _, ca := range cases { 758 sort.Float64s(ca.quantiles) 759 760 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 761 c.Assert(logicalMemBlock.Extractor, NotNil) 762 763 extractor := logicalMemBlock.Extractor.(*causetembedded.MetricSummaryBlockExtractor) 764 if len(ca.quantiles) > 0 { 765 c.Assert(extractor.Quantiles, DeepEquals, ca.quantiles, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 766 } 767 if len(ca.names) > 0 { 768 c.Assert(extractor.MetricsNames, DeepEquals, ca.names, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 769 } 770 c.Assert(extractor.SkipRequest, Equals, ca.skipRequest, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 771 } 772 } 773 774 func (s *extractorSuite) TestInspectionResultBlockExtractor(c *C) { 775 se, err := stochastik.CreateStochastik4Test(s.causetstore) 776 c.Assert(err, IsNil) 777 778 var cases = []struct { 779 allegrosql string 780 rules set.StringSet 781 items set.StringSet 782 skipInspection bool 783 }{ 784 { 785 allegrosql: "select * from information_schema.inspection_result", 786 }, 787 { 788 allegrosql: "select * from information_schema.inspection_result where rule='dbs'", 789 rules: set.NewStringSet("dbs"), 790 }, 791 { 792 allegrosql: "select * from information_schema.inspection_result where 'dbs'=rule", 793 rules: set.NewStringSet("dbs"), 794 }, 795 { 796 allegrosql: "select * from information_schema.inspection_result where 'dbs'=rule", 797 rules: set.NewStringSet("dbs"), 798 }, 799 { 800 allegrosql: "select * from information_schema.inspection_result where 'dbs'=rule or rule='config'", 801 rules: set.NewStringSet("dbs", "config"), 802 }, 803 { 804 allegrosql: "select * from information_schema.inspection_result where 'dbs'=rule or rule='config' or rule='slow_query'", 805 rules: set.NewStringSet("dbs", "config", "slow_query"), 806 }, 807 { 808 allegrosql: "select * from information_schema.inspection_result where (rule='config' or rule='slow_query') and (item='dbs.tenant' or item='dbs.lease')", 809 rules: set.NewStringSet("config", "slow_query"), 810 items: set.NewStringSet("dbs.tenant", "dbs.lease"), 811 }, 812 { 813 allegrosql: "select * from information_schema.inspection_result where rule in ('dbs', 'slow_query')", 814 rules: set.NewStringSet("dbs", "slow_query"), 815 }, 816 { 817 allegrosql: "select * from information_schema.inspection_result where rule in ('dbs', 'slow_query') and item='dbs.lease'", 818 rules: set.NewStringSet("dbs", "slow_query"), 819 items: set.NewStringSet("dbs.lease"), 820 }, 821 { 822 allegrosql: "select * from information_schema.inspection_result where rule in ('dbs', 'slow_query') and item in ('dbs.lease', '123.1.1.4:1234')", 823 rules: set.NewStringSet("dbs", "slow_query"), 824 items: set.NewStringSet("dbs.lease", "123.1.1.4:1234"), 825 }, 826 { 827 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and item in ('dbs.lease', '123.1.1.4:1234')", 828 rules: set.NewStringSet("dbs"), 829 items: set.NewStringSet("dbs.lease", "123.1.1.4:1234"), 830 }, 831 { 832 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and item='123.1.1.4:1234'", 833 rules: set.NewStringSet("dbs"), 834 items: set.NewStringSet("123.1.1.4:1234"), 835 }, 836 { 837 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and item='123.1.1.4:1234'", 838 rules: set.NewStringSet("dbs"), 839 items: set.NewStringSet("123.1.1.4:1234"), 840 }, 841 { 842 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and item='DBS.lease'", 843 rules: set.NewStringSet("dbs"), 844 items: set.NewStringSet("dbs.lease"), 845 }, 846 { 847 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and item='dbs.OWNER'", 848 rules: set.NewStringSet("dbs"), 849 items: set.NewStringSet("dbs.tenant"), 850 }, 851 { 852 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and rule='slow_query'", 853 skipInspection: true, 854 }, 855 { 856 allegrosql: "select * from information_schema.inspection_result where rule='dbs' and rule in ('slow_query', 'dbs')", 857 rules: set.NewStringSet("dbs"), 858 }, 859 { 860 allegrosql: "select * from information_schema.inspection_result where rule in ('dbs', 'config') and rule in ('slow_query', 'config')", 861 rules: set.NewStringSet("config"), 862 }, 863 { 864 allegrosql: "select * from information_schema.inspection_result where item='dbs.lease' and item='raftstore.threadpool'", 865 skipInspection: true, 866 }, 867 { 868 allegrosql: "select * from information_schema.inspection_result where item='raftstore.threadpool' and item in ('raftstore.threadpool', 'dbs.lease')", 869 items: set.NewStringSet("raftstore.threadpool"), 870 }, 871 { 872 allegrosql: "select * from information_schema.inspection_result where item='raftstore.threadpool' and item in ('dbs.lease', 'scheduler.limit')", 873 skipInspection: true, 874 }, 875 { 876 allegrosql: "select * from information_schema.inspection_result where item in ('dbs.lease', 'scheduler.limit') and item in ('raftstore.threadpool', 'scheduler.limit')", 877 items: set.NewStringSet("scheduler.limit"), 878 }, 879 { 880 allegrosql: `select * from information_schema.inspection_result 881 where item in ('dbs.lease', 'scheduler.limit') 882 and item in ('raftstore.threadpool', 'scheduler.limit') 883 and rule in ('dbs', 'config') 884 and rule in ('slow_query', 'config')`, 885 rules: set.NewStringSet("config"), 886 items: set.NewStringSet("scheduler.limit"), 887 }, 888 { 889 allegrosql: `select * from information_schema.inspection_result 890 where item in ('dbs.lease', 'scheduler.limit') 891 and item in ('raftstore.threadpool', 'scheduler.limit') 892 and item in ('raftstore.threadpool', 'dbs.lease') 893 and item in ('dbs.lease', 'dbs.tenant')`, 894 skipInspection: true, 895 }, 896 } 897 BerolinaSQL := BerolinaSQL.New() 898 for _, ca := range cases { 899 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 900 c.Assert(logicalMemBlock.Extractor, NotNil) 901 902 clusterConfigExtractor := logicalMemBlock.Extractor.(*causetembedded.InspectionResultBlockExtractor) 903 if len(ca.rules) > 0 { 904 c.Assert(clusterConfigExtractor.Memrules, DeepEquals, ca.rules, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 905 } 906 if len(ca.items) > 0 { 907 c.Assert(clusterConfigExtractor.Items, DeepEquals, ca.items, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 908 } 909 c.Assert(clusterConfigExtractor.SkipInspection, Equals, ca.skipInspection, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 910 } 911 } 912 913 func (s *extractorSuite) TestInspectionSummaryBlockExtractor(c *C) { 914 se, err := stochastik.CreateStochastik4Test(s.causetstore) 915 c.Assert(err, IsNil) 916 917 var cases = []struct { 918 allegrosql string 919 rules set.StringSet 920 names set.StringSet 921 quantiles set.Float64Set 922 skipInspection bool 923 }{ 924 { 925 allegrosql: "select * from information_schema.inspection_summary", 926 }, 927 { 928 allegrosql: "select * from information_schema.inspection_summary where rule='dbs'", 929 rules: set.NewStringSet("dbs"), 930 }, 931 { 932 allegrosql: "select * from information_schema.inspection_summary where 'dbs'=rule or rule='config'", 933 rules: set.NewStringSet("dbs", "config"), 934 }, 935 { 936 allegrosql: "select * from information_schema.inspection_summary where 'dbs'=rule or rule='config' or rule='slow_query'", 937 rules: set.NewStringSet("dbs", "config", "slow_query"), 938 }, 939 { 940 allegrosql: "select * from information_schema.inspection_summary where (rule='config' or rule='slow_query') and (metrics_name='metric_name3' or metrics_name='metric_name1')", 941 rules: set.NewStringSet("config", "slow_query"), 942 names: set.NewStringSet("metric_name3", "metric_name1"), 943 }, 944 { 945 allegrosql: "select * from information_schema.inspection_summary where rule in ('dbs', 'slow_query')", 946 rules: set.NewStringSet("dbs", "slow_query"), 947 }, 948 { 949 allegrosql: "select * from information_schema.inspection_summary where rule in ('dbs', 'slow_query') and metrics_name='metric_name1'", 950 rules: set.NewStringSet("dbs", "slow_query"), 951 names: set.NewStringSet("metric_name1"), 952 }, 953 { 954 allegrosql: "select * from information_schema.inspection_summary where rule in ('dbs', 'slow_query') and metrics_name in ('metric_name1', 'metric_name2')", 955 rules: set.NewStringSet("dbs", "slow_query"), 956 names: set.NewStringSet("metric_name1", "metric_name2"), 957 }, 958 { 959 allegrosql: "select * from information_schema.inspection_summary where rule='dbs' and metrics_name in ('metric_name1', 'metric_name2')", 960 rules: set.NewStringSet("dbs"), 961 names: set.NewStringSet("metric_name1", "metric_name2"), 962 }, 963 { 964 allegrosql: "select * from information_schema.inspection_summary where rule='dbs' and metrics_name='metric_NAME3'", 965 rules: set.NewStringSet("dbs"), 966 names: set.NewStringSet("metric_name3"), 967 }, 968 { 969 allegrosql: "select * from information_schema.inspection_summary where rule='dbs' and rule in ('slow_query', 'dbs')", 970 rules: set.NewStringSet("dbs"), 971 }, 972 { 973 allegrosql: "select * from information_schema.inspection_summary where rule in ('dbs', 'config') and rule in ('slow_query', 'config')", 974 rules: set.NewStringSet("config"), 975 }, 976 { 977 allegrosql: `select * from information_schema.inspection_summary 978 where metrics_name in ('metric_name1', 'metric_name4') 979 and metrics_name in ('metric_name5', 'metric_name4') 980 and rule in ('dbs', 'config') 981 and rule in ('slow_query', 'config') 982 and quantile in (0.80, 0.90)`, 983 rules: set.NewStringSet("config"), 984 names: set.NewStringSet("metric_name4"), 985 quantiles: set.NewFloat64Set(0.80, 0.90), 986 }, 987 { 988 allegrosql: `select * from information_schema.inspection_summary 989 where metrics_name in ('metric_name1', 'metric_name4') 990 and metrics_name in ('metric_name5', 'metric_name4') 991 and metrics_name in ('metric_name5', 'metric_name1') 992 and metrics_name in ('metric_name1', 'metric_name3')`, 993 skipInspection: true, 994 }, 995 } 996 BerolinaSQL := BerolinaSQL.New() 997 for _, ca := range cases { 998 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 999 c.Assert(logicalMemBlock.Extractor, NotNil) 1000 1001 clusterConfigExtractor := logicalMemBlock.Extractor.(*causetembedded.InspectionSummaryBlockExtractor) 1002 if len(ca.rules) > 0 { 1003 c.Assert(clusterConfigExtractor.Memrules, DeepEquals, ca.rules, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 1004 } 1005 if len(ca.names) > 0 { 1006 c.Assert(clusterConfigExtractor.MetricNames, DeepEquals, ca.names, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 1007 } 1008 c.Assert(clusterConfigExtractor.SkipInspection, Equals, ca.skipInspection, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 1009 } 1010 } 1011 1012 func (s *extractorSuite) TestInspectionMemruleBlockExtractor(c *C) { 1013 se, err := stochastik.CreateStochastik4Test(s.causetstore) 1014 c.Assert(err, IsNil) 1015 1016 var cases = []struct { 1017 allegrosql string 1018 tps set.StringSet 1019 skip bool 1020 }{ 1021 { 1022 allegrosql: "select * from information_schema.inspection_rules", 1023 }, 1024 { 1025 allegrosql: "select * from information_schema.inspection_rules where type='inspection'", 1026 tps: set.NewStringSet("inspection"), 1027 }, 1028 { 1029 allegrosql: "select * from information_schema.inspection_rules where type='inspection' or type='summary'", 1030 tps: set.NewStringSet("inspection", "summary"), 1031 }, 1032 { 1033 allegrosql: "select * from information_schema.inspection_rules where type='inspection' and type='summary'", 1034 skip: true, 1035 }, 1036 } 1037 BerolinaSQL := BerolinaSQL.New() 1038 for _, ca := range cases { 1039 logicalMemBlock := s.getLogicalMemBlock(c, se, BerolinaSQL, ca.allegrosql) 1040 c.Assert(logicalMemBlock.Extractor, NotNil) 1041 1042 clusterConfigExtractor := logicalMemBlock.Extractor.(*causetembedded.InspectionMemruleBlockExtractor) 1043 if len(ca.tps) > 0 { 1044 c.Assert(clusterConfigExtractor.Types, DeepEquals, ca.tps, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 1045 } 1046 c.Assert(clusterConfigExtractor.SkipRequest, Equals, ca.skip, Commentf("ALLEGROALLEGROSQL: %v", ca.allegrosql)) 1047 } 1048 }