mock_topo.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  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. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package topotest
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. "time"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. "github.com/lf-edge/ekuiper/internal/processor"
  24. "github.com/lf-edge/ekuiper/internal/testx"
  25. "github.com/lf-edge/ekuiper/internal/topo"
  26. "github.com/lf-edge/ekuiper/internal/topo/node"
  27. "github.com/lf-edge/ekuiper/internal/topo/planner"
  28. "github.com/lf-edge/ekuiper/internal/topo/topotest/mockclock"
  29. "github.com/lf-edge/ekuiper/internal/topo/topotest/mocknode"
  30. "github.com/lf-edge/ekuiper/internal/xsql"
  31. "github.com/lf-edge/ekuiper/pkg/api"
  32. "github.com/lf-edge/ekuiper/pkg/ast"
  33. "github.com/lf-edge/ekuiper/pkg/cast"
  34. )
  35. func init() {
  36. testx.InitEnv()
  37. }
  38. const POSTLEAP = 1000 // Time change after all data sends out
  39. type RuleTest struct {
  40. Name string
  41. Sql string
  42. R interface{} // The result
  43. M map[string]interface{} // final metrics
  44. T *api.PrintableTopo // printable topo, an optional field
  45. W int // wait time for each data sending, in milli
  46. }
  47. func CompareMetrics(tp *topo.Topo, m map[string]interface{}) (err error) {
  48. keys, values := tp.GetMetrics()
  49. for k, v := range m {
  50. var (
  51. index int
  52. key string
  53. matched bool
  54. )
  55. for index, key = range keys {
  56. if k == key {
  57. if strings.HasSuffix(k, "process_latency_us") {
  58. if values[index].(int64) >= v.(int64) {
  59. matched = true
  60. continue
  61. }
  62. break
  63. }
  64. if values[index] == v {
  65. matched = true
  66. }
  67. break
  68. }
  69. }
  70. if matched {
  71. continue
  72. }
  73. if conf.Config.Basic.Debug == true {
  74. for i, k := range keys {
  75. conf.Log.Printf("%s:%v", k, values[i])
  76. }
  77. }
  78. // do not find
  79. if index < len(values) {
  80. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%T)\n\ngot=%#v(%T)\n\n", k, v, v, values[index], values[index])
  81. } else {
  82. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  83. }
  84. }
  85. return nil
  86. }
  87. func CommonResultFunc(result [][]byte) interface{} {
  88. var maps [][]map[string]interface{}
  89. for _, v := range result {
  90. var mapRes []map[string]interface{}
  91. err := json.Unmarshal(v, &mapRes)
  92. if err != nil {
  93. panic(fmt.Sprintf("Failed to parse the input %v into map", string(v)))
  94. }
  95. maps = append(maps, mapRes)
  96. }
  97. return maps
  98. }
  99. func DoRuleTest(t *testing.T, tests []RuleTest, j int, opt *api.RuleOption, wait int) {
  100. doRuleTestBySinkProps(t, tests, j, opt, wait, nil, CommonResultFunc)
  101. }
  102. func doRuleTestBySinkProps(t *testing.T, tests []RuleTest, j int, opt *api.RuleOption, w int, sinkProps map[string]interface{}, resultFunc func(result [][]byte) interface{}) {
  103. fmt.Printf("The test bucket for option %d size is %d.\n\n", j, len(tests))
  104. for i, tt := range tests {
  105. datas, dataLength, tp, mockSink, errCh := createStream(t, tt, j, opt, sinkProps)
  106. if tp == nil {
  107. t.Errorf("topo is not created successfully")
  108. break
  109. }
  110. wait := tt.W
  111. if wait == 0 {
  112. if w > 0 {
  113. wait = w
  114. } else {
  115. wait = 5
  116. }
  117. }
  118. switch opt.Qos {
  119. case api.ExactlyOnce:
  120. wait *= 10
  121. case api.AtLeastOnce:
  122. wait *= 3
  123. }
  124. var retry int
  125. if opt.Qos > api.AtMostOnce {
  126. for retry = 3; retry > 0; retry-- {
  127. if tp.GetCoordinator() == nil || !tp.GetCoordinator().IsActivated() {
  128. conf.Log.Debugf("waiting for coordinator ready %d\n", retry)
  129. time.Sleep(10 * time.Millisecond)
  130. } else {
  131. break
  132. }
  133. }
  134. if retry < 0 {
  135. t.Error("coordinator timeout")
  136. t.FailNow()
  137. }
  138. }
  139. if err := sendData(t, dataLength, tt.M, datas, errCh, tp, POSTLEAP, wait); err != nil {
  140. t.Errorf("send data error %s", err)
  141. break
  142. }
  143. compareResult(t, mockSink, resultFunc, tt, i, tp)
  144. }
  145. }
  146. func compareResult(t *testing.T, mockSink *mocknode.MockSink, resultFunc func(result [][]byte) interface{}, tt RuleTest, i int, tp *topo.Topo) {
  147. // Check results
  148. results := mockSink.GetResults()
  149. maps := resultFunc(results)
  150. if !reflect.DeepEqual(tt.R, maps) {
  151. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.Sql, tt.R, maps)
  152. }
  153. if err := CompareMetrics(tp, tt.M); err != nil {
  154. t.Errorf("%d. %q\n\nmetrics mismatch:\n\n%s\n\n", i, tt.Sql, err)
  155. }
  156. if tt.T != nil {
  157. topo := tp.GetTopo()
  158. if !reflect.DeepEqual(tt.T, topo) {
  159. t.Errorf("%d. %q\n\ntopo mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.Sql, tt.T, topo)
  160. }
  161. }
  162. tp.Cancel()
  163. }
  164. func sendData(t *testing.T, dataLength int, metrics map[string]interface{}, datas [][]*xsql.Tuple, errCh <-chan error, tp *topo.Topo, postleap int, wait int) error {
  165. // Send data and move time
  166. mockClock := mockclock.GetMockClock()
  167. // Set the current time
  168. mockClock.Add(0)
  169. // TODO assume multiple data source send the data in order and has the same length
  170. for i := 0; i < dataLength; i++ {
  171. // wait for table to load
  172. time.Sleep(100 * time.Millisecond)
  173. for _, d := range datas {
  174. time.Sleep(time.Duration(wait) * time.Millisecond)
  175. // Make sure time is going forward only
  176. // gradually add up time to ensure checkpoint is triggered before the data send
  177. for n := conf.GetNowInMilli() + 100; d[i].Timestamp+100 > n; n += 100 {
  178. if d[i].Timestamp < n {
  179. n = d[i].Timestamp
  180. }
  181. mockClock.Set(cast.TimeFromUnixMilli(n))
  182. conf.Log.Debugf("Clock set to %d", conf.GetNowInMilli())
  183. time.Sleep(1 * time.Millisecond)
  184. }
  185. select {
  186. case err := <-errCh:
  187. t.Log(err)
  188. tp.Cancel()
  189. return err
  190. default:
  191. }
  192. }
  193. }
  194. mockClock.Add(time.Duration(postleap) * time.Millisecond)
  195. conf.Log.Debugf("Clock add to %d", conf.GetNowInMilli())
  196. // Check if stream done. Poll for metrics,
  197. time.Sleep(10 * time.Millisecond)
  198. var retry int
  199. for retry = 4; retry > 0; retry-- {
  200. var err error
  201. if err = CompareMetrics(tp, metrics); err == nil {
  202. break
  203. }
  204. conf.Log.Errorf("check metrics error at %d: %s", retry, err)
  205. time.Sleep(1000 * time.Millisecond)
  206. }
  207. if retry == 0 {
  208. t.Error("send data timeout")
  209. } else if retry < 2 {
  210. conf.Log.Debugf("try %d for metric comparison\n", 2-retry)
  211. }
  212. return nil
  213. }
  214. func createStream(t *testing.T, tt RuleTest, j int, opt *api.RuleOption, sinkProps map[string]interface{}) ([][]*xsql.Tuple, int, *topo.Topo, *mocknode.MockSink, <-chan error) {
  215. mockclock.ResetClock(1541152486000)
  216. // Create stream
  217. var (
  218. sources []*node.SourceNode
  219. datas [][]*xsql.Tuple
  220. dataLength int
  221. )
  222. parser := xsql.NewParser(strings.NewReader(tt.Sql))
  223. if stmt, err := xsql.Language.Parse(parser); err != nil {
  224. t.Errorf("parse sql %s error: %s", tt.Sql, err)
  225. } else {
  226. if selectStmt, ok := stmt.(*ast.SelectStatement); !ok {
  227. t.Errorf("sql %s is not a select statement", tt.Sql)
  228. } else {
  229. streams := xsql.GetStreams(selectStmt)
  230. for _, stream := range streams {
  231. data, ok := mocknode.TestData[stream]
  232. if !ok {
  233. continue
  234. }
  235. dataLength = len(data)
  236. datas = append(datas, data)
  237. }
  238. }
  239. }
  240. mockSink := mocknode.NewMockSink()
  241. sink := node.NewSinkNodeWithSink("mockSink", mockSink, sinkProps)
  242. tp, err := planner.PlanSQLWithSourcesAndSinks(&api.Rule{Id: fmt.Sprintf("%s_%d", tt.Name, j), Sql: tt.Sql, Options: opt}, sources, []*node.SinkNode{sink})
  243. if err != nil {
  244. t.Error(err)
  245. return nil, 0, nil, nil, nil
  246. }
  247. errCh := tp.Open()
  248. return datas, dataLength, tp, mockSink, errCh
  249. }
  250. // Create or drop streams
  251. func HandleStream(createOrDrop bool, names []string, t *testing.T) {
  252. p := processor.NewStreamProcessor()
  253. for _, name := range names {
  254. var sql string
  255. if createOrDrop {
  256. switch name {
  257. case "demoArr":
  258. sql = `CREATE STREAM demoArr () WITH (DATASOURCE="demoArr", TYPE="mock", FORMAT="json", KEY="ts");`
  259. case "demo":
  260. sql = `CREATE STREAM demo (
  261. color STRING,
  262. size BIGINT,
  263. ts BIGINT
  264. ) WITH (DATASOURCE="demo", TYPE="mock", FORMAT="json", KEY="ts");`
  265. case "demoError":
  266. sql = `CREATE STREAM demoError (
  267. color STRING,
  268. size BIGINT,
  269. ts BIGINT
  270. ) WITH (DATASOURCE="demoError", TYPE="mock", FORMAT="json", KEY="ts",STRICT_VALIDATION="true");`
  271. case "demo1":
  272. sql = `CREATE STREAM demo1 (
  273. temp FLOAT,
  274. hum BIGINT,` +
  275. "`from`" + ` STRING,
  276. ts BIGINT
  277. ) WITH (DATASOURCE="demo1", TYPE="mock", FORMAT="json", KEY="ts");`
  278. case "demoTable":
  279. sql = `CREATE TABLE demoTable (
  280. device STRING,
  281. ts BIGINT
  282. ) WITH (DATASOURCE="demoTable", TYPE="mock", RETAIN_SIZE="3");`
  283. case "sessionDemo":
  284. sql = `CREATE STREAM sessionDemo (
  285. temp FLOAT,
  286. hum BIGINT,
  287. ts BIGINT
  288. ) WITH (DATASOURCE="sessionDemo", TYPE="mock", FORMAT="json", KEY="ts");`
  289. case "demoE":
  290. sql = `CREATE STREAM demoE (
  291. color STRING,
  292. size BIGINT,
  293. ts BIGINT
  294. ) WITH (DATASOURCE="demoE", TYPE="mock", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  295. case "demo1E":
  296. sql = `CREATE STREAM demo1E (
  297. temp FLOAT,
  298. hum BIGINT,
  299. ts BIGINT
  300. ) WITH (DATASOURCE="demo1E", TYPE="mock", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  301. case "sessionDemoE":
  302. sql = `CREATE STREAM sessionDemoE (
  303. temp FLOAT,
  304. hum BIGINT,
  305. ts BIGINT
  306. ) WITH (DATASOURCE="sessionDemoE", TYPE="mock", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  307. case "demoErr":
  308. sql = `CREATE STREAM demoErr (
  309. color STRING,
  310. size BIGINT,
  311. ts BIGINT
  312. ) WITH (DATASOURCE="demoErr", TYPE="mock", FORMAT="json", KEY="ts", TIMESTAMP="ts",STRICT_VALIDATION="true");`
  313. case "ldemo":
  314. sql = `CREATE STREAM ldemo (
  315. ) WITH (DATASOURCE="ldemo", TYPE="mock", FORMAT="json");`
  316. case "ldemo1":
  317. sql = `CREATE STREAM ldemo1 (
  318. ) WITH (DATASOURCE="ldemo1", TYPE="mock", FORMAT="json");`
  319. case "lsessionDemo":
  320. sql = `CREATE STREAM lsessionDemo (
  321. ) WITH (DATASOURCE="lsessionDemo", TYPE="mock", FORMAT="json");`
  322. case "ext":
  323. sql = "CREATE STREAM ext (count bigint) WITH (DATASOURCE=\"ext\", FORMAT=\"JSON\", TYPE=\"random\", CONF_KEY=\"ext\",STRICT_VALIDATION=\"true\")"
  324. case "ext2":
  325. sql = "CREATE STREAM ext2 (count bigint) WITH (DATASOURCE=\"ext2\", FORMAT=\"JSON\", TYPE=\"random\", CONF_KEY=\"dedup\")"
  326. case "extpy":
  327. sql = "CREATE STREAM extpy (name string, value bigint) WITH (FORMAT=\"JSON\", TYPE=\"pyjson\", CONF_KEY=\"ext\")"
  328. case "text":
  329. sql = "CREATE STREAM text (slogan string, brand string) WITH (DATASOURCE=\"text\", TYPE=\"mock\", FORMAT=\"JSON\")"
  330. case "binDemo":
  331. sql = "CREATE STREAM binDemo () WITH (DATASOURCE=\"binDemo\", TYPE=\"mock\", FORMAT=\"BINARY\")"
  332. case "table1":
  333. sql = `CREATE TABLE table1 (
  334. name STRING,
  335. size BIGINT,
  336. id BIGINT
  337. ) WITH (DATASOURCE="lookup.json", FORMAT="json", CONF_KEY="test");`
  338. case "helloStr":
  339. sql = `CREATE STREAM helloStr (name string) WITH (DATASOURCE="helloStr", TYPE="mock", FORMAT="JSON")`
  340. case "commands":
  341. sql = `CREATE STREAM commands (cmd string, base64_img string, encoded_json string) WITH (DATASOURCE="commands", FORMAT="JSON", TYPE="mock")`
  342. case "fakeBin":
  343. sql = "CREATE STREAM fakeBin () WITH (DATASOURCE=\"fakeBin\", TYPE=\"mock\", FORMAT=\"BINARY\")"
  344. case "shelves":
  345. sql = `CREATE STREAM shelves (
  346. name string,
  347. size BIGINT,
  348. shelf STRUCT(theme STRING,id BIGINT, subfield STRING)
  349. ) WITH (DATASOURCE="shelves", TYPE="mock", FORMAT="json");`
  350. case "mes":
  351. sql = `CREATE STREAM mes (message_id string, text string) WITH (DATASOURCE="mes", TYPE="mock", FORMAT="JSON")`
  352. default:
  353. t.Errorf("create stream %s fail", name)
  354. }
  355. } else {
  356. if strings.Index(name, "table") == 0 {
  357. sql = `DROP TABLE ` + name
  358. } else {
  359. sql = `DROP STREAM ` + name
  360. }
  361. }
  362. _, err := p.ExecStmt(sql)
  363. if err != nil {
  364. t.Log(err)
  365. }
  366. }
  367. }
  368. type RuleCheckpointTest struct {
  369. RuleTest
  370. PauseSize int // Stop stream after sending pauseSize source to test checkpoint resume
  371. Cc int // checkpoint count when paused
  372. PauseMetric map[string]interface{} // The metric to check when paused
  373. }
  374. func DoCheckpointRuleTest(t *testing.T, tests []RuleCheckpointTest, j int, opt *api.RuleOption) {
  375. fmt.Printf("The test bucket for option %d size is %d.\n\n", j, len(tests))
  376. for i, tt := range tests {
  377. datas, dataLength, tp, mockSink, errCh := createStream(t, tt.RuleTest, j, opt, nil)
  378. if tp == nil {
  379. t.Errorf("topo is not created successfully")
  380. break
  381. }
  382. var retry int
  383. for retry = 10; retry > 0; retry-- {
  384. if tp.GetCoordinator() == nil || !tp.GetCoordinator().IsActivated() {
  385. conf.Log.Debugf("waiting for coordinator ready %d\n", retry)
  386. time.Sleep(10 * time.Millisecond)
  387. } else {
  388. break
  389. }
  390. }
  391. if retry == 0 {
  392. t.Error("coordinator timeout")
  393. t.FailNow()
  394. }
  395. conf.Log.Debugf("Start sending first phase data done at %d", conf.GetNowInMilli())
  396. if err := sendData(t, tt.PauseSize, tt.PauseMetric, datas, errCh, tp, 100, 100); err != nil {
  397. t.Errorf("first phase send data error %s", err)
  398. break
  399. }
  400. conf.Log.Debugf("Send first phase data done at %d", conf.GetNowInMilli())
  401. // compare checkpoint count
  402. time.Sleep(10 * time.Millisecond)
  403. for retry = 3; retry > 0; retry-- {
  404. actual := tp.GetCoordinator().GetCompleteCount()
  405. if tt.Cc == actual {
  406. break
  407. }
  408. conf.Log.Debugf("check checkpointCount error at %d: %d\n", retry, actual)
  409. time.Sleep(200 * time.Millisecond)
  410. }
  411. cc := tp.GetCoordinator().GetCompleteCount()
  412. tp.Cancel()
  413. if retry == 0 {
  414. t.Errorf("%d-%d. checkpoint count\n\nresult mismatch:\n\nexp=%#v\n\ngot=%d\n\n", i, j, tt.Cc, cc)
  415. return
  416. } else if retry < 3 {
  417. conf.Log.Debugf("try %d for checkpoint count\n", 4-retry)
  418. }
  419. tp.Cancel()
  420. time.Sleep(10 * time.Millisecond)
  421. // resume stream
  422. conf.Log.Debugf("Resume stream at %d", conf.GetNowInMilli())
  423. errCh = tp.Open()
  424. conf.Log.Debugf("After open stream at %d", conf.GetNowInMilli())
  425. if err := sendData(t, dataLength, tt.M, datas, errCh, tp, POSTLEAP, 10); err != nil {
  426. t.Errorf("second phase send data error %s", err)
  427. break
  428. }
  429. compareResult(t, mockSink, CommonResultFunc, tt.RuleTest, i, tp)
  430. }
  431. }
  432. func CreateRule(name, sql string) (*api.Rule, error) {
  433. p := processor.NewRuleProcessor()
  434. p.ExecDrop(name)
  435. return p.ExecCreateWithValidation(name, sql)
  436. }