xsql_processor_test.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. package processors
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "github.com/emqx/kuiper/xsql"
  7. "github.com/emqx/kuiper/xstream"
  8. "github.com/emqx/kuiper/xstream/api"
  9. "github.com/emqx/kuiper/xstream/nodes"
  10. "github.com/emqx/kuiper/xstream/test"
  11. "path"
  12. "reflect"
  13. "strings"
  14. "testing"
  15. "time"
  16. )
  17. var DbDir = getDbDir()
  18. func getDbDir() string {
  19. dbDir, err := common.GetAndCreateDataLoc("test")
  20. if err != nil {
  21. log.Panic(err)
  22. }
  23. log.Infof("db location is %s", dbDir)
  24. return dbDir
  25. }
  26. func TestStreamCreateProcessor(t *testing.T) {
  27. var tests = []struct {
  28. s string
  29. r []string
  30. err string
  31. }{
  32. {
  33. s: `SHOW STREAMS;`,
  34. r: []string{"No stream definitions are found."},
  35. },
  36. {
  37. s: `EXPLAIN STREAM topic1;`,
  38. err: "Stream topic1 is not found.",
  39. },
  40. {
  41. s: `CREATE STREAM topic1 (
  42. USERID BIGINT,
  43. FIRST_NAME STRING,
  44. LAST_NAME STRING,
  45. NICKNAMES ARRAY(STRING),
  46. Gender BOOLEAN,
  47. ADDRESS STRUCT(STREET_NAME STRING, NUMBER BIGINT),
  48. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  49. r: []string{"Stream topic1 is created."},
  50. },
  51. {
  52. s: `CREATE STREAM topic1 (
  53. USERID BIGINT,
  54. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  55. err: "Create stream fails: Item topic1 already exists.",
  56. },
  57. {
  58. s: `EXPLAIN STREAM topic1;`,
  59. r: []string{"TO BE SUPPORTED"},
  60. },
  61. {
  62. s: `DESCRIBE STREAM topic1;`,
  63. r: []string{"Fields\n--------------------------------------------------------------------------------\nUSERID\tbigint\nFIRST_NAME\tstring\nLAST_NAME\tstring\nNICKNAMES\t" +
  64. "array(string)\nGender\tboolean\nADDRESS\tstruct(STREET_NAME string, NUMBER bigint)\n\n" +
  65. "DATASOURCE: users\nFORMAT: AVRO\nKEY: USERID\n"},
  66. },
  67. {
  68. s: `SHOW STREAMS;`,
  69. r: []string{"topic1"},
  70. },
  71. {
  72. s: `DROP STREAM topic1;`,
  73. r: []string{"Stream topic1 is dropped."},
  74. },
  75. {
  76. s: `DESCRIBE STREAM topic1;`,
  77. err: "Stream topic1 is not found.",
  78. },
  79. {
  80. s: `DROP STREAM topic1;`,
  81. err: "Drop stream fails: topic1 is not found.",
  82. },
  83. }
  84. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  85. streamDB := path.Join(getDbDir(), "streamTest")
  86. for i, tt := range tests {
  87. results, err := NewStreamProcessor(tt.s, streamDB).Exec()
  88. if !reflect.DeepEqual(tt.err, errstring(err)) {
  89. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  90. } else if tt.err == "" {
  91. if !reflect.DeepEqual(tt.r, results) {
  92. t.Errorf("%d. %q\n\nstmt mismatch:\nexp=%s\ngot=%#v\n\n", i, tt.s, tt.r, results)
  93. }
  94. }
  95. }
  96. }
  97. func createStreams(t *testing.T) {
  98. demo := `CREATE STREAM demo (
  99. color STRING,
  100. size BIGINT,
  101. ts BIGINT
  102. ) WITH (DATASOURCE="demo", FORMAT="json", KEY="ts");`
  103. _, err := NewStreamProcessor(demo, path.Join(DbDir, "stream")).Exec()
  104. if err != nil {
  105. t.Log(err)
  106. }
  107. demo1 := `CREATE STREAM demo1 (
  108. temp FLOAT,
  109. hum BIGINT,
  110. ts BIGINT
  111. ) WITH (DATASOURCE="demo1", FORMAT="json", KEY="ts");`
  112. _, err = NewStreamProcessor(demo1, path.Join(DbDir, "stream")).Exec()
  113. if err != nil {
  114. t.Log(err)
  115. }
  116. sessionDemo := `CREATE STREAM sessionDemo (
  117. temp FLOAT,
  118. hum BIGINT,
  119. ts BIGINT
  120. ) WITH (DATASOURCE="sessionDemo", FORMAT="json", KEY="ts");`
  121. _, err = NewStreamProcessor(sessionDemo, path.Join(DbDir, "stream")).Exec()
  122. if err != nil {
  123. t.Log(err)
  124. }
  125. }
  126. func dropStreams(t *testing.T) {
  127. demo := `DROP STREAM demo`
  128. _, err := NewStreamProcessor(demo, path.Join(DbDir, "stream")).Exec()
  129. if err != nil {
  130. t.Log(err)
  131. }
  132. demo1 := `DROP STREAM demo1`
  133. _, err = NewStreamProcessor(demo1, path.Join(DbDir, "stream")).Exec()
  134. if err != nil {
  135. t.Log(err)
  136. }
  137. sessionDemo := `DROP STREAM sessionDemo`
  138. _, err = NewStreamProcessor(sessionDemo, path.Join(DbDir, "stream")).Exec()
  139. if err != nil {
  140. t.Log(err)
  141. }
  142. }
  143. func getMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  144. var data []*xsql.Tuple
  145. switch name {
  146. case "demo":
  147. data = []*xsql.Tuple{
  148. {
  149. Emitter: name,
  150. Message: map[string]interface{}{
  151. "color": "red",
  152. "size": 3,
  153. "ts": 1541152486013,
  154. },
  155. Timestamp: 1541152486013,
  156. },
  157. {
  158. Emitter: name,
  159. Message: map[string]interface{}{
  160. "color": "blue",
  161. "size": 6,
  162. "ts": 1541152486822,
  163. },
  164. Timestamp: 1541152486822,
  165. },
  166. {
  167. Emitter: name,
  168. Message: map[string]interface{}{
  169. "color": "blue",
  170. "size": 2,
  171. "ts": 1541152487632,
  172. },
  173. Timestamp: 1541152487632,
  174. },
  175. {
  176. Emitter: name,
  177. Message: map[string]interface{}{
  178. "color": "yellow",
  179. "size": 4,
  180. "ts": 1541152488442,
  181. },
  182. Timestamp: 1541152488442,
  183. },
  184. {
  185. Emitter: name,
  186. Message: map[string]interface{}{
  187. "color": "red",
  188. "size": 1,
  189. "ts": 1541152489252,
  190. },
  191. Timestamp: 1541152489252,
  192. },
  193. }
  194. case "demo1":
  195. data = []*xsql.Tuple{
  196. {
  197. Emitter: name,
  198. Message: map[string]interface{}{
  199. "temp": 25.5,
  200. "hum": 65,
  201. "ts": 1541152486013,
  202. },
  203. Timestamp: 1541152486013,
  204. },
  205. {
  206. Emitter: name,
  207. Message: map[string]interface{}{
  208. "temp": 27.5,
  209. "hum": 59,
  210. "ts": 1541152486823,
  211. },
  212. Timestamp: 1541152486823,
  213. },
  214. {
  215. Emitter: name,
  216. Message: map[string]interface{}{
  217. "temp": 28.1,
  218. "hum": 75,
  219. "ts": 1541152487632,
  220. },
  221. Timestamp: 1541152487632,
  222. },
  223. {
  224. Emitter: name,
  225. Message: map[string]interface{}{
  226. "temp": 27.4,
  227. "hum": 80,
  228. "ts": 1541152488442,
  229. },
  230. Timestamp: 1541152488442,
  231. },
  232. {
  233. Emitter: name,
  234. Message: map[string]interface{}{
  235. "temp": 25.5,
  236. "hum": 62,
  237. "ts": 1541152489252,
  238. },
  239. Timestamp: 1541152489252,
  240. },
  241. }
  242. case "sessionDemo":
  243. data = []*xsql.Tuple{
  244. {
  245. Emitter: name,
  246. Message: map[string]interface{}{
  247. "temp": 25.5,
  248. "hum": 65,
  249. "ts": 1541152486013,
  250. },
  251. Timestamp: 1541152486013,
  252. },
  253. {
  254. Emitter: name,
  255. Message: map[string]interface{}{
  256. "temp": 27.5,
  257. "hum": 59,
  258. "ts": 1541152486823,
  259. },
  260. Timestamp: 1541152486823,
  261. },
  262. {
  263. Emitter: name,
  264. Message: map[string]interface{}{
  265. "temp": 28.1,
  266. "hum": 75,
  267. "ts": 1541152487932,
  268. },
  269. Timestamp: 1541152487932,
  270. },
  271. {
  272. Emitter: name,
  273. Message: map[string]interface{}{
  274. "temp": 27.4,
  275. "hum": 80,
  276. "ts": 1541152488442,
  277. },
  278. Timestamp: 1541152488442,
  279. },
  280. {
  281. Emitter: name,
  282. Message: map[string]interface{}{
  283. "temp": 25.5,
  284. "hum": 62,
  285. "ts": 1541152489252,
  286. },
  287. Timestamp: 1541152489252,
  288. },
  289. {
  290. Emitter: name,
  291. Message: map[string]interface{}{
  292. "temp": 26.2,
  293. "hum": 63,
  294. "ts": 1541152490062,
  295. },
  296. Timestamp: 1541152490062,
  297. },
  298. {
  299. Emitter: name,
  300. Message: map[string]interface{}{
  301. "temp": 26.8,
  302. "hum": 71,
  303. "ts": 1541152490872,
  304. },
  305. Timestamp: 1541152490872,
  306. },
  307. {
  308. Emitter: name,
  309. Message: map[string]interface{}{
  310. "temp": 28.9,
  311. "hum": 85,
  312. "ts": 1541152491682,
  313. },
  314. Timestamp: 1541152491682,
  315. },
  316. {
  317. Emitter: name,
  318. Message: map[string]interface{}{
  319. "temp": 29.1,
  320. "hum": 92,
  321. "ts": 1541152492492,
  322. },
  323. Timestamp: 1541152492492,
  324. },
  325. {
  326. Emitter: name,
  327. Message: map[string]interface{}{
  328. "temp": 32.2,
  329. "hum": 99,
  330. "ts": 1541152493202,
  331. },
  332. Timestamp: 1541152493202,
  333. },
  334. {
  335. Emitter: name,
  336. Message: map[string]interface{}{
  337. "temp": 30.9,
  338. "hum": 87,
  339. "ts": 1541152494112,
  340. },
  341. Timestamp: 1541152494112,
  342. },
  343. }
  344. }
  345. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  346. "DATASOURCE": name,
  347. })
  348. }
  349. func TestSingleSQL(t *testing.T) {
  350. var tests = []struct {
  351. name string
  352. sql string
  353. r [][]map[string]interface{}
  354. s string
  355. m map[string]interface{}
  356. }{
  357. {
  358. name: `rule1`,
  359. sql: `SELECT * FROM demo`,
  360. r: [][]map[string]interface{}{
  361. {{
  362. "color": "red",
  363. "size": float64(3),
  364. "ts": float64(1541152486013),
  365. }},
  366. {{
  367. "color": "blue",
  368. "size": float64(6),
  369. "ts": float64(1541152486822),
  370. }},
  371. {{
  372. "color": "blue",
  373. "size": float64(2),
  374. "ts": float64(1541152487632),
  375. }},
  376. {{
  377. "color": "yellow",
  378. "size": float64(4),
  379. "ts": float64(1541152488442),
  380. }},
  381. {{
  382. "color": "red",
  383. "size": float64(1),
  384. "ts": float64(1541152489252),
  385. }},
  386. },
  387. m: map[string]interface{}{
  388. "op_preprocessor_demo_0_exceptions_total": int64(0),
  389. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  390. "op_preprocessor_demo_0_records_in_total": int64(5),
  391. "op_preprocessor_demo_0_records_out_total": int64(5),
  392. "op_project_0_exceptions_total": int64(0),
  393. "op_project_0_process_latency_ms": int64(0),
  394. "op_project_0_records_in_total": int64(5),
  395. "op_project_0_records_out_total": int64(5),
  396. "sink_mockSink_0_exceptions_total": int64(0),
  397. "sink_mockSink_0_records_in_total": int64(5),
  398. "sink_mockSink_0_records_out_total": int64(5),
  399. "source_demo_0_exceptions_total": int64(0),
  400. "source_demo_0_records_in_total": int64(5),
  401. "source_demo_0_records_out_total": int64(5),
  402. },
  403. s: "sink_mockSink_0_records_out_total",
  404. }, {
  405. name: `rule2`,
  406. sql: `SELECT color, ts FROM demo where size > 3`,
  407. r: [][]map[string]interface{}{
  408. {{
  409. "color": "blue",
  410. "ts": float64(1541152486822),
  411. }},
  412. {{
  413. "color": "yellow",
  414. "ts": float64(1541152488442),
  415. }},
  416. },
  417. s: "op_filter_0_records_in_total",
  418. m: map[string]interface{}{
  419. "op_preprocessor_demo_0_exceptions_total": int64(0),
  420. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  421. "op_preprocessor_demo_0_records_in_total": int64(5),
  422. "op_preprocessor_demo_0_records_out_total": int64(5),
  423. "op_project_0_exceptions_total": int64(0),
  424. "op_project_0_process_latency_ms": int64(0),
  425. "op_project_0_records_in_total": int64(2),
  426. "op_project_0_records_out_total": int64(2),
  427. "sink_mockSink_0_exceptions_total": int64(0),
  428. "sink_mockSink_0_records_in_total": int64(2),
  429. "sink_mockSink_0_records_out_total": int64(2),
  430. "source_demo_0_exceptions_total": int64(0),
  431. "source_demo_0_records_in_total": int64(5),
  432. "source_demo_0_records_out_total": int64(5),
  433. "op_filter_0_exceptions_total": int64(0),
  434. "op_filter_0_process_latency_ms": int64(0),
  435. "op_filter_0_records_in_total": int64(5),
  436. "op_filter_0_records_out_total": int64(2),
  437. },
  438. }, {
  439. name: `rule3`,
  440. sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  441. r: [][]map[string]interface{}{
  442. {{
  443. "Int8": float64(6),
  444. "ts": float64(1541152486822),
  445. }},
  446. {{
  447. "Int8": float64(4),
  448. "ts": float64(1541152488442),
  449. }},
  450. },
  451. s: "op_filter_0_records_in_total",
  452. m: map[string]interface{}{
  453. "op_preprocessor_demo_0_exceptions_total": int64(0),
  454. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  455. "op_preprocessor_demo_0_records_in_total": int64(5),
  456. "op_preprocessor_demo_0_records_out_total": int64(5),
  457. "op_project_0_exceptions_total": int64(0),
  458. "op_project_0_process_latency_ms": int64(0),
  459. "op_project_0_records_in_total": int64(2),
  460. "op_project_0_records_out_total": int64(2),
  461. "sink_mockSink_0_exceptions_total": int64(0),
  462. "sink_mockSink_0_records_in_total": int64(2),
  463. "sink_mockSink_0_records_out_total": int64(2),
  464. "source_demo_0_exceptions_total": int64(0),
  465. "source_demo_0_records_in_total": int64(5),
  466. "source_demo_0_records_out_total": int64(5),
  467. "op_filter_0_exceptions_total": int64(0),
  468. "op_filter_0_process_latency_ms": int64(0),
  469. "op_filter_0_records_in_total": int64(5),
  470. "op_filter_0_records_out_total": int64(2),
  471. },
  472. },
  473. }
  474. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  475. createStreams(t)
  476. defer dropStreams(t)
  477. //defer close(done)
  478. for i, tt := range tests {
  479. test.ResetClock(1541152486000)
  480. p := NewRuleProcessor(DbDir)
  481. parser := xsql.NewParser(strings.NewReader(tt.sql))
  482. var (
  483. sources []*nodes.SourceNode
  484. syncs []chan int
  485. )
  486. if stmt, err := xsql.Language.Parse(parser); err != nil {
  487. t.Errorf("parse sql %s error: %s", tt.sql, err)
  488. } else {
  489. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  490. t.Errorf("sql %s is not a select statement", tt.sql)
  491. } else {
  492. streams := xsql.GetStreams(selectStmt)
  493. for _, stream := range streams {
  494. next := make(chan int)
  495. syncs = append(syncs, next)
  496. source := getMockSource(stream, next, 5)
  497. sources = append(sources, source)
  498. }
  499. }
  500. }
  501. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  502. "bufferLength": float64(100),
  503. }}, sources)
  504. if err != nil {
  505. t.Error(err)
  506. }
  507. mockSink := test.NewMockSink()
  508. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  509. tp.AddSink(inputs, sink)
  510. errCh := tp.Open()
  511. func() {
  512. for i := 0; i < 5; i++ {
  513. syncs[i%len(syncs)] <- i
  514. select {
  515. case err = <-errCh:
  516. t.Log(err)
  517. tp.Cancel()
  518. return
  519. default:
  520. }
  521. }
  522. for retry := 100; retry > 0; retry-- {
  523. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  524. break
  525. }
  526. time.Sleep(time.Duration(retry) * time.Millisecond)
  527. }
  528. }()
  529. results := mockSink.GetResults()
  530. var maps [][]map[string]interface{}
  531. for _, v := range results {
  532. var mapRes []map[string]interface{}
  533. err := json.Unmarshal(v, &mapRes)
  534. if err != nil {
  535. t.Errorf("Failed to parse the input into map")
  536. continue
  537. }
  538. maps = append(maps, mapRes)
  539. }
  540. if !reflect.DeepEqual(tt.r, maps) {
  541. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  542. continue
  543. }
  544. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  545. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  546. }
  547. tp.Cancel()
  548. }
  549. }
  550. func TestWindow(t *testing.T) {
  551. var tests = []struct {
  552. name string
  553. sql string
  554. size int
  555. r [][]map[string]interface{}
  556. m map[string]interface{}
  557. }{
  558. {
  559. name: `rule1`,
  560. sql: `SELECT * FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  561. size: 5,
  562. r: [][]map[string]interface{}{
  563. {{
  564. "color": "red",
  565. "size": float64(3),
  566. "ts": float64(1541152486013),
  567. }, {
  568. "color": "blue",
  569. "size": float64(6),
  570. "ts": float64(1541152486822),
  571. }},
  572. {{
  573. "color": "red",
  574. "size": float64(3),
  575. "ts": float64(1541152486013),
  576. }, {
  577. "color": "blue",
  578. "size": float64(6),
  579. "ts": float64(1541152486822),
  580. }, {
  581. "color": "blue",
  582. "size": float64(2),
  583. "ts": float64(1541152487632),
  584. }},
  585. {{
  586. "color": "blue",
  587. "size": float64(2),
  588. "ts": float64(1541152487632),
  589. }, {
  590. "color": "yellow",
  591. "size": float64(4),
  592. "ts": float64(1541152488442),
  593. }},
  594. },
  595. m: map[string]interface{}{
  596. "op_preprocessor_demo_0_exceptions_total": int64(0),
  597. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  598. "op_preprocessor_demo_0_records_in_total": int64(5),
  599. "op_preprocessor_demo_0_records_out_total": int64(5),
  600. "op_project_0_exceptions_total": int64(0),
  601. "op_project_0_process_latency_ms": int64(0),
  602. "op_project_0_records_in_total": int64(3),
  603. "op_project_0_records_out_total": int64(3),
  604. "sink_mockSink_0_exceptions_total": int64(0),
  605. "sink_mockSink_0_records_in_total": int64(3),
  606. "sink_mockSink_0_records_out_total": int64(3),
  607. "source_demo_0_exceptions_total": int64(0),
  608. "source_demo_0_records_in_total": int64(5),
  609. "source_demo_0_records_out_total": int64(5),
  610. "op_window_0_exceptions_total": int64(0),
  611. "op_window_0_process_latency_ms": int64(0),
  612. "op_window_0_records_in_total": int64(5),
  613. "op_window_0_records_out_total": int64(3),
  614. },
  615. }, {
  616. name: `rule2`,
  617. sql: `SELECT color, ts FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  618. size: 5,
  619. r: [][]map[string]interface{}{
  620. {{
  621. "color": "red",
  622. "ts": float64(1541152486013),
  623. }, {
  624. "color": "blue",
  625. "ts": float64(1541152486822),
  626. }},
  627. {{
  628. "color": "yellow",
  629. "ts": float64(1541152488442),
  630. }},
  631. },
  632. m: map[string]interface{}{
  633. "op_preprocessor_demo_0_exceptions_total": int64(0),
  634. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  635. "op_preprocessor_demo_0_records_in_total": int64(5),
  636. "op_preprocessor_demo_0_records_out_total": int64(5),
  637. "op_project_0_exceptions_total": int64(0),
  638. "op_project_0_process_latency_ms": int64(0),
  639. "op_project_0_records_in_total": int64(2),
  640. "op_project_0_records_out_total": int64(2),
  641. "sink_mockSink_0_exceptions_total": int64(0),
  642. "sink_mockSink_0_records_in_total": int64(2),
  643. "sink_mockSink_0_records_out_total": int64(2),
  644. "source_demo_0_exceptions_total": int64(0),
  645. "source_demo_0_records_in_total": int64(5),
  646. "source_demo_0_records_out_total": int64(5),
  647. "op_window_0_exceptions_total": int64(0),
  648. "op_window_0_process_latency_ms": int64(0),
  649. "op_window_0_records_in_total": int64(5),
  650. "op_window_0_records_out_total": int64(3),
  651. "op_filter_0_exceptions_total": int64(0),
  652. "op_filter_0_process_latency_ms": int64(0),
  653. "op_filter_0_records_in_total": int64(3),
  654. "op_filter_0_records_out_total": int64(2),
  655. },
  656. }, {
  657. name: `rule3`,
  658. sql: `SELECT color, temp, ts FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  659. size: 5,
  660. r: [][]map[string]interface{}{
  661. {{
  662. "color": "red",
  663. "temp": 25.5,
  664. "ts": float64(1541152486013),
  665. }}, {{
  666. "color": "red",
  667. "temp": 25.5,
  668. "ts": float64(1541152486013),
  669. }}, {{
  670. "color": "red",
  671. "temp": 25.5,
  672. "ts": float64(1541152486013),
  673. }}, {{
  674. "color": "blue",
  675. "temp": 28.1,
  676. "ts": float64(1541152487632),
  677. }}, {{
  678. "color": "blue",
  679. "temp": 28.1,
  680. "ts": float64(1541152487632),
  681. }}, {{
  682. "color": "blue",
  683. "temp": 28.1,
  684. "ts": float64(1541152487632),
  685. }, {
  686. "color": "yellow",
  687. "temp": 27.4,
  688. "ts": float64(1541152488442),
  689. }}, {{
  690. "color": "yellow",
  691. "temp": 27.4,
  692. "ts": float64(1541152488442),
  693. }}, {{
  694. "color": "yellow",
  695. "temp": 27.4,
  696. "ts": float64(1541152488442),
  697. }, {
  698. "color": "red",
  699. "temp": 25.5,
  700. "ts": float64(1541152489252),
  701. }},
  702. },
  703. m: map[string]interface{}{
  704. "op_preprocessor_demo_0_exceptions_total": int64(0),
  705. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  706. "op_preprocessor_demo_0_records_in_total": int64(5),
  707. "op_preprocessor_demo_0_records_out_total": int64(5),
  708. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  709. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  710. "op_preprocessor_demo1_0_records_in_total": int64(5),
  711. "op_preprocessor_demo1_0_records_out_total": int64(5),
  712. "op_project_0_exceptions_total": int64(0),
  713. "op_project_0_process_latency_ms": int64(0),
  714. "op_project_0_records_in_total": int64(8),
  715. "op_project_0_records_out_total": int64(8),
  716. "sink_mockSink_0_exceptions_total": int64(0),
  717. "sink_mockSink_0_records_in_total": int64(8),
  718. "sink_mockSink_0_records_out_total": int64(8),
  719. "source_demo_0_exceptions_total": int64(0),
  720. "source_demo_0_records_in_total": int64(5),
  721. "source_demo_0_records_out_total": int64(5),
  722. "source_demo1_0_exceptions_total": int64(0),
  723. "source_demo1_0_records_in_total": int64(5),
  724. "source_demo1_0_records_out_total": int64(5),
  725. "op_window_0_exceptions_total": int64(0),
  726. "op_window_0_process_latency_ms": int64(0),
  727. "op_window_0_records_in_total": int64(10),
  728. "op_window_0_records_out_total": int64(10),
  729. "op_join_0_exceptions_total": int64(0),
  730. "op_join_0_process_latency_ms": int64(0),
  731. "op_join_0_records_in_total": int64(10),
  732. "op_join_0_records_out_total": int64(8),
  733. },
  734. }, {
  735. name: `rule4`,
  736. sql: `SELECT color FROM demo GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  737. size: 5,
  738. r: [][]map[string]interface{}{
  739. {{
  740. "color": "red",
  741. }}, {{
  742. "color": "blue",
  743. }, {
  744. "color": "red",
  745. }}, {{
  746. "color": "blue",
  747. }, {
  748. "color": "red",
  749. }}, {{
  750. "color": "blue",
  751. }, {
  752. "color": "yellow",
  753. }}, {{
  754. "color": "blue",
  755. }, {
  756. "color": "red",
  757. }, {
  758. "color": "yellow",
  759. }},
  760. },
  761. m: map[string]interface{}{
  762. "op_preprocessor_demo_0_exceptions_total": int64(0),
  763. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  764. "op_preprocessor_demo_0_records_in_total": int64(5),
  765. "op_preprocessor_demo_0_records_out_total": int64(5),
  766. "op_project_0_exceptions_total": int64(0),
  767. "op_project_0_process_latency_ms": int64(0),
  768. "op_project_0_records_in_total": int64(5),
  769. "op_project_0_records_out_total": int64(5),
  770. "sink_mockSink_0_exceptions_total": int64(0),
  771. "sink_mockSink_0_records_in_total": int64(5),
  772. "sink_mockSink_0_records_out_total": int64(5),
  773. "source_demo_0_exceptions_total": int64(0),
  774. "source_demo_0_records_in_total": int64(5),
  775. "source_demo_0_records_out_total": int64(5),
  776. "op_window_0_exceptions_total": int64(0),
  777. "op_window_0_process_latency_ms": int64(0),
  778. "op_window_0_records_in_total": int64(5),
  779. "op_window_0_records_out_total": int64(5),
  780. "op_aggregate_0_exceptions_total": int64(0),
  781. "op_aggregate_0_process_latency_ms": int64(0),
  782. "op_aggregate_0_records_in_total": int64(5),
  783. "op_aggregate_0_records_out_total": int64(5),
  784. "op_order_0_exceptions_total": int64(0),
  785. "op_order_0_process_latency_ms": int64(0),
  786. "op_order_0_records_in_total": int64(5),
  787. "op_order_0_records_out_total": int64(5),
  788. },
  789. }, {
  790. name: `rule5`,
  791. sql: `SELECT temp FROM sessionDemo GROUP BY SessionWindow(ss, 2, 1) `,
  792. size: 11,
  793. r: [][]map[string]interface{}{
  794. {{
  795. "temp": 25.5,
  796. }, {
  797. "temp": 27.5,
  798. }}, {{
  799. "temp": 28.1,
  800. }, {
  801. "temp": 27.4,
  802. }, {
  803. "temp": 25.5,
  804. }}, {{
  805. "temp": 26.2,
  806. }, {
  807. "temp": 26.8,
  808. }, {
  809. "temp": 28.9,
  810. }, {
  811. "temp": 29.1,
  812. }, {
  813. "temp": 32.2,
  814. }},
  815. },
  816. m: map[string]interface{}{
  817. "op_preprocessor_sessionDemo_0_exceptions_total": int64(0),
  818. "op_preprocessor_sessionDemo_0_process_latency_ms": int64(0),
  819. "op_preprocessor_sessionDemo_0_records_in_total": int64(11),
  820. "op_preprocessor_sessionDemo_0_records_out_total": int64(11),
  821. "op_project_0_exceptions_total": int64(0),
  822. "op_project_0_process_latency_ms": int64(0),
  823. "op_project_0_records_in_total": int64(3),
  824. "op_project_0_records_out_total": int64(3),
  825. "sink_mockSink_0_exceptions_total": int64(0),
  826. "sink_mockSink_0_records_in_total": int64(3),
  827. "sink_mockSink_0_records_out_total": int64(3),
  828. "source_sessionDemo_0_exceptions_total": int64(0),
  829. "source_sessionDemo_0_records_in_total": int64(11),
  830. "source_sessionDemo_0_records_out_total": int64(11),
  831. "op_window_0_exceptions_total": int64(0),
  832. "op_window_0_process_latency_ms": int64(0),
  833. "op_window_0_records_in_total": int64(11),
  834. "op_window_0_records_out_total": int64(3),
  835. },
  836. }, {
  837. name: `rule6`,
  838. sql: `SELECT max(temp) as m, count(color) as c FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  839. size: 5,
  840. r: [][]map[string]interface{}{
  841. {{
  842. "m": 25.5,
  843. "c": float64(1),
  844. }}, {{
  845. "m": 25.5,
  846. "c": float64(1),
  847. }}, {{
  848. "m": 25.5,
  849. "c": float64(1),
  850. }}, {{
  851. "m": 28.1,
  852. "c": float64(1),
  853. }}, {{
  854. "m": 28.1,
  855. "c": float64(1),
  856. }}, {{
  857. "m": 28.1,
  858. "c": float64(2),
  859. }}, {{
  860. "m": 27.4,
  861. "c": float64(1),
  862. }}, {{
  863. "m": 27.4,
  864. "c": float64(2),
  865. }},
  866. },
  867. m: map[string]interface{}{
  868. "op_preprocessor_demo_0_exceptions_total": int64(0),
  869. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  870. "op_preprocessor_demo_0_records_in_total": int64(5),
  871. "op_preprocessor_demo_0_records_out_total": int64(5),
  872. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  873. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  874. "op_preprocessor_demo1_0_records_in_total": int64(5),
  875. "op_preprocessor_demo1_0_records_out_total": int64(5),
  876. "op_project_0_exceptions_total": int64(0),
  877. "op_project_0_process_latency_ms": int64(0),
  878. "op_project_0_records_in_total": int64(8),
  879. "op_project_0_records_out_total": int64(8),
  880. "sink_mockSink_0_exceptions_total": int64(0),
  881. "sink_mockSink_0_records_in_total": int64(8),
  882. "sink_mockSink_0_records_out_total": int64(8),
  883. "source_demo_0_exceptions_total": int64(0),
  884. "source_demo_0_records_in_total": int64(5),
  885. "source_demo_0_records_out_total": int64(5),
  886. "source_demo1_0_exceptions_total": int64(0),
  887. "source_demo1_0_records_in_total": int64(5),
  888. "source_demo1_0_records_out_total": int64(5),
  889. "op_window_0_exceptions_total": int64(0),
  890. "op_window_0_process_latency_ms": int64(0),
  891. "op_window_0_records_in_total": int64(10),
  892. "op_window_0_records_out_total": int64(10),
  893. "op_join_0_exceptions_total": int64(0),
  894. "op_join_0_process_latency_ms": int64(0),
  895. "op_join_0_records_in_total": int64(10),
  896. "op_join_0_records_out_total": int64(8),
  897. },
  898. },
  899. }
  900. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  901. createStreams(t)
  902. defer dropStreams(t)
  903. for i, tt := range tests {
  904. test.ResetClock(1541152486000)
  905. p := NewRuleProcessor(DbDir)
  906. parser := xsql.NewParser(strings.NewReader(tt.sql))
  907. var (
  908. sources []*nodes.SourceNode
  909. syncs []chan int
  910. )
  911. if stmt, err := xsql.Language.Parse(parser); err != nil {
  912. t.Errorf("parse sql %s error: %s", tt.sql, err)
  913. } else {
  914. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  915. t.Errorf("sql %s is not a select statement", tt.sql)
  916. } else {
  917. streams := xsql.GetStreams(selectStmt)
  918. for _, stream := range streams {
  919. next := make(chan int)
  920. syncs = append(syncs, next)
  921. source := getMockSource(stream, next, tt.size)
  922. sources = append(sources, source)
  923. }
  924. }
  925. }
  926. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  927. if err != nil {
  928. t.Error(err)
  929. }
  930. mockSink := test.NewMockSink()
  931. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  932. tp.AddSink(inputs, sink)
  933. errCh := tp.Open()
  934. func() {
  935. for i := 0; i < tt.size*len(syncs); i++ {
  936. syncs[i%len(syncs)] <- i
  937. for {
  938. time.Sleep(1)
  939. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  940. break
  941. }
  942. }
  943. select {
  944. case err = <-errCh:
  945. t.Log(err)
  946. tp.Cancel()
  947. return
  948. default:
  949. }
  950. }
  951. retry := 100
  952. for ; retry > 0; retry-- {
  953. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  954. break
  955. }
  956. t.Logf("wait to try another %d times", retry)
  957. time.Sleep(time.Duration(retry) * time.Millisecond)
  958. }
  959. if retry == 0 {
  960. err := compareMetrics(tp, tt.m, tt.sql)
  961. t.Errorf("could not get correct metrics: %v", err)
  962. }
  963. }()
  964. results := mockSink.GetResults()
  965. var maps [][]map[string]interface{}
  966. for _, v := range results {
  967. var mapRes []map[string]interface{}
  968. err := json.Unmarshal(v, &mapRes)
  969. if err != nil {
  970. t.Errorf("Failed to parse the input into map")
  971. continue
  972. }
  973. maps = append(maps, mapRes)
  974. }
  975. if !reflect.DeepEqual(tt.r, maps) {
  976. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  977. }
  978. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  979. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  980. }
  981. tp.Cancel()
  982. }
  983. }
  984. func createEventStreams(t *testing.T) {
  985. demo := `CREATE STREAM demoE (
  986. color STRING,
  987. size BIGINT,
  988. ts BIGINT
  989. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  990. _, err := NewStreamProcessor(demo, path.Join(DbDir, "stream")).Exec()
  991. if err != nil {
  992. t.Log(err)
  993. }
  994. demo1 := `CREATE STREAM demo1E (
  995. temp FLOAT,
  996. hum BIGINT,
  997. ts BIGINT
  998. ) WITH (DATASOURCE="demo1E", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  999. _, err = NewStreamProcessor(demo1, path.Join(DbDir, "stream")).Exec()
  1000. if err != nil {
  1001. t.Log(err)
  1002. }
  1003. sessionDemo := `CREATE STREAM sessionDemoE (
  1004. temp FLOAT,
  1005. hum BIGINT,
  1006. ts BIGINT
  1007. ) WITH (DATASOURCE="sessionDemoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1008. _, err = NewStreamProcessor(sessionDemo, path.Join(DbDir, "stream")).Exec()
  1009. if err != nil {
  1010. t.Log(err)
  1011. }
  1012. }
  1013. func dropEventStreams(t *testing.T) {
  1014. demo := `DROP STREAM demoE`
  1015. _, err := NewStreamProcessor(demo, path.Join(DbDir, "stream")).Exec()
  1016. if err != nil {
  1017. t.Log(err)
  1018. }
  1019. demo1 := `DROP STREAM demo1E`
  1020. _, err = NewStreamProcessor(demo1, path.Join(DbDir, "stream")).Exec()
  1021. if err != nil {
  1022. t.Log(err)
  1023. }
  1024. sessionDemo := `DROP STREAM sessionDemoE`
  1025. _, err = NewStreamProcessor(sessionDemo, path.Join(DbDir, "stream")).Exec()
  1026. if err != nil {
  1027. t.Log(err)
  1028. }
  1029. }
  1030. func getEventMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  1031. var data []*xsql.Tuple
  1032. switch name {
  1033. case "demoE":
  1034. data = []*xsql.Tuple{
  1035. {
  1036. Emitter: name,
  1037. Message: map[string]interface{}{
  1038. "color": "red",
  1039. "size": 3,
  1040. "ts": 1541152486013,
  1041. },
  1042. Timestamp: 1541152486013,
  1043. },
  1044. {
  1045. Emitter: name,
  1046. Message: map[string]interface{}{
  1047. "color": "blue",
  1048. "size": 2,
  1049. "ts": 1541152487632,
  1050. },
  1051. Timestamp: 1541152487632,
  1052. },
  1053. {
  1054. Emitter: name,
  1055. Message: map[string]interface{}{
  1056. "color": "red",
  1057. "size": 1,
  1058. "ts": 1541152489252,
  1059. },
  1060. Timestamp: 1541152489252,
  1061. },
  1062. { //dropped item
  1063. Emitter: name,
  1064. Message: map[string]interface{}{
  1065. "color": "blue",
  1066. "size": 6,
  1067. "ts": 1541152486822,
  1068. },
  1069. Timestamp: 1541152486822,
  1070. },
  1071. {
  1072. Emitter: name,
  1073. Message: map[string]interface{}{
  1074. "color": "yellow",
  1075. "size": 4,
  1076. "ts": 1541152488442,
  1077. },
  1078. Timestamp: 1541152488442,
  1079. },
  1080. { //To lift the watermark and issue all windows
  1081. Emitter: name,
  1082. Message: map[string]interface{}{
  1083. "color": "yellow",
  1084. "size": 4,
  1085. "ts": 1541152492342,
  1086. },
  1087. Timestamp: 1541152488442,
  1088. },
  1089. }
  1090. case "demo1E":
  1091. data = []*xsql.Tuple{
  1092. {
  1093. Emitter: name,
  1094. Message: map[string]interface{}{
  1095. "temp": 27.5,
  1096. "hum": 59,
  1097. "ts": 1541152486823,
  1098. },
  1099. Timestamp: 1541152486823,
  1100. },
  1101. {
  1102. Emitter: name,
  1103. Message: map[string]interface{}{
  1104. "temp": 25.5,
  1105. "hum": 65,
  1106. "ts": 1541152486013,
  1107. },
  1108. Timestamp: 1541152486013,
  1109. },
  1110. {
  1111. Emitter: name,
  1112. Message: map[string]interface{}{
  1113. "temp": 27.4,
  1114. "hum": 80,
  1115. "ts": 1541152488442,
  1116. },
  1117. Timestamp: 1541152488442,
  1118. },
  1119. {
  1120. Emitter: name,
  1121. Message: map[string]interface{}{
  1122. "temp": 28.1,
  1123. "hum": 75,
  1124. "ts": 1541152487632,
  1125. },
  1126. Timestamp: 1541152487632,
  1127. },
  1128. {
  1129. Emitter: name,
  1130. Message: map[string]interface{}{
  1131. "temp": 25.5,
  1132. "hum": 62,
  1133. "ts": 1541152489252,
  1134. },
  1135. Timestamp: 1541152489252,
  1136. },
  1137. {
  1138. Emitter: name,
  1139. Message: map[string]interface{}{
  1140. "temp": 25.5,
  1141. "hum": 62,
  1142. "ts": 1541152499252,
  1143. },
  1144. Timestamp: 1541152499252,
  1145. },
  1146. }
  1147. case "sessionDemoE":
  1148. data = []*xsql.Tuple{
  1149. {
  1150. Emitter: name,
  1151. Message: map[string]interface{}{
  1152. "temp": 25.5,
  1153. "hum": 65,
  1154. "ts": 1541152486013,
  1155. },
  1156. Timestamp: 1541152486013,
  1157. },
  1158. {
  1159. Emitter: name,
  1160. Message: map[string]interface{}{
  1161. "temp": 28.1,
  1162. "hum": 75,
  1163. "ts": 1541152487932,
  1164. },
  1165. Timestamp: 1541152487932,
  1166. },
  1167. {
  1168. Emitter: name,
  1169. Message: map[string]interface{}{
  1170. "temp": 27.5,
  1171. "hum": 59,
  1172. "ts": 1541152486823,
  1173. },
  1174. Timestamp: 1541152486823,
  1175. },
  1176. {
  1177. Emitter: name,
  1178. Message: map[string]interface{}{
  1179. "temp": 25.5,
  1180. "hum": 62,
  1181. "ts": 1541152489252,
  1182. },
  1183. Timestamp: 1541152489252,
  1184. },
  1185. {
  1186. Emitter: name,
  1187. Message: map[string]interface{}{
  1188. "temp": 27.4,
  1189. "hum": 80,
  1190. "ts": 1541152488442,
  1191. },
  1192. Timestamp: 1541152488442,
  1193. },
  1194. {
  1195. Emitter: name,
  1196. Message: map[string]interface{}{
  1197. "temp": 26.2,
  1198. "hum": 63,
  1199. "ts": 1541152490062,
  1200. },
  1201. Timestamp: 1541152490062,
  1202. },
  1203. {
  1204. Emitter: name,
  1205. Message: map[string]interface{}{
  1206. "temp": 28.9,
  1207. "hum": 85,
  1208. "ts": 1541152491682,
  1209. },
  1210. Timestamp: 1541152491682,
  1211. },
  1212. {
  1213. Emitter: name,
  1214. Message: map[string]interface{}{
  1215. "temp": 26.8,
  1216. "hum": 71,
  1217. "ts": 1541152490872,
  1218. },
  1219. Timestamp: 1541152490872,
  1220. },
  1221. {
  1222. Emitter: name,
  1223. Message: map[string]interface{}{
  1224. "temp": 29.1,
  1225. "hum": 92,
  1226. "ts": 1541152492492,
  1227. },
  1228. Timestamp: 1541152492492,
  1229. },
  1230. {
  1231. Emitter: name,
  1232. Message: map[string]interface{}{
  1233. "temp": 30.9,
  1234. "hum": 87,
  1235. "ts": 1541152494112,
  1236. },
  1237. Timestamp: 1541152494112,
  1238. },
  1239. {
  1240. Emitter: name,
  1241. Message: map[string]interface{}{
  1242. "temp": 32.2,
  1243. "hum": 99,
  1244. "ts": 1541152493202,
  1245. },
  1246. Timestamp: 1541152493202,
  1247. },
  1248. {
  1249. Emitter: name,
  1250. Message: map[string]interface{}{
  1251. "temp": 32.2,
  1252. "hum": 99,
  1253. "ts": 1541152499202,
  1254. },
  1255. Timestamp: 1541152499202,
  1256. },
  1257. }
  1258. }
  1259. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, true), map[string]string{
  1260. "DATASOURCE": name,
  1261. })
  1262. }
  1263. func TestEventWindow(t *testing.T) {
  1264. var tests = []struct {
  1265. name string
  1266. sql string
  1267. size int
  1268. r [][]map[string]interface{}
  1269. m map[string]interface{}
  1270. }{
  1271. {
  1272. name: `rule1`,
  1273. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1274. size: 6,
  1275. r: [][]map[string]interface{}{
  1276. {{
  1277. "color": "red",
  1278. "size": float64(3),
  1279. "ts": float64(1541152486013),
  1280. }},
  1281. {{
  1282. "color": "red",
  1283. "size": float64(3),
  1284. "ts": float64(1541152486013),
  1285. }, {
  1286. "color": "blue",
  1287. "size": float64(2),
  1288. "ts": float64(1541152487632),
  1289. }},
  1290. {{
  1291. "color": "blue",
  1292. "size": float64(2),
  1293. "ts": float64(1541152487632),
  1294. }, {
  1295. "color": "yellow",
  1296. "size": float64(4),
  1297. "ts": float64(1541152488442),
  1298. }}, {{
  1299. "color": "yellow",
  1300. "size": float64(4),
  1301. "ts": float64(1541152488442),
  1302. }, {
  1303. "color": "red",
  1304. "size": float64(1),
  1305. "ts": float64(1541152489252),
  1306. }}, {{
  1307. "color": "red",
  1308. "size": float64(1),
  1309. "ts": float64(1541152489252),
  1310. }},
  1311. },
  1312. m: map[string]interface{}{
  1313. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1314. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1315. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1316. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1317. "op_project_0_exceptions_total": int64(0),
  1318. "op_project_0_process_latency_ms": int64(0),
  1319. "op_project_0_records_in_total": int64(5),
  1320. "op_project_0_records_out_total": int64(5),
  1321. "sink_mockSink_0_exceptions_total": int64(0),
  1322. "sink_mockSink_0_records_in_total": int64(5),
  1323. "sink_mockSink_0_records_out_total": int64(5),
  1324. "source_demoE_0_exceptions_total": int64(0),
  1325. "source_demoE_0_records_in_total": int64(6),
  1326. "source_demoE_0_records_out_total": int64(6),
  1327. "op_window_0_exceptions_total": int64(0),
  1328. "op_window_0_process_latency_ms": int64(0),
  1329. "op_window_0_records_in_total": int64(6),
  1330. "op_window_0_records_out_total": int64(5),
  1331. },
  1332. }, {
  1333. name: `rule2`,
  1334. sql: `SELECT color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1335. size: 6,
  1336. r: [][]map[string]interface{}{
  1337. {{
  1338. "color": "red",
  1339. "ts": float64(1541152486013),
  1340. }},
  1341. {{
  1342. "color": "yellow",
  1343. "ts": float64(1541152488442),
  1344. }},
  1345. },
  1346. m: map[string]interface{}{
  1347. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1348. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1349. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1350. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1351. "op_project_0_exceptions_total": int64(0),
  1352. "op_project_0_process_latency_ms": int64(0),
  1353. "op_project_0_records_in_total": int64(2),
  1354. "op_project_0_records_out_total": int64(2),
  1355. "sink_mockSink_0_exceptions_total": int64(0),
  1356. "sink_mockSink_0_records_in_total": int64(2),
  1357. "sink_mockSink_0_records_out_total": int64(2),
  1358. "source_demoE_0_exceptions_total": int64(0),
  1359. "source_demoE_0_records_in_total": int64(6),
  1360. "source_demoE_0_records_out_total": int64(6),
  1361. "op_window_0_exceptions_total": int64(0),
  1362. "op_window_0_process_latency_ms": int64(0),
  1363. "op_window_0_records_in_total": int64(6),
  1364. "op_window_0_records_out_total": int64(4),
  1365. "op_filter_0_exceptions_total": int64(0),
  1366. "op_filter_0_process_latency_ms": int64(0),
  1367. "op_filter_0_records_in_total": int64(4),
  1368. "op_filter_0_records_out_total": int64(2),
  1369. },
  1370. }, {
  1371. name: `rule3`,
  1372. sql: `SELECT color, temp, ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  1373. size: 6,
  1374. r: [][]map[string]interface{}{
  1375. {{
  1376. "color": "red",
  1377. "temp": 25.5,
  1378. "ts": float64(1541152486013),
  1379. }}, {{
  1380. "color": "red",
  1381. "temp": 25.5,
  1382. "ts": float64(1541152486013),
  1383. }}, {{
  1384. "color": "blue",
  1385. "temp": 28.1,
  1386. "ts": float64(1541152487632),
  1387. }}, {{
  1388. "color": "blue",
  1389. "temp": 28.1,
  1390. "ts": float64(1541152487632),
  1391. }, {
  1392. "color": "yellow",
  1393. "temp": 27.4,
  1394. "ts": float64(1541152488442),
  1395. }}, {{
  1396. "color": "yellow",
  1397. "temp": 27.4,
  1398. "ts": float64(1541152488442),
  1399. }, {
  1400. "color": "red",
  1401. "temp": 25.5,
  1402. "ts": float64(1541152489252),
  1403. }},
  1404. },
  1405. m: map[string]interface{}{
  1406. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1407. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1408. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1409. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1410. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  1411. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  1412. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  1413. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  1414. "op_project_0_exceptions_total": int64(0),
  1415. "op_project_0_process_latency_ms": int64(0),
  1416. "op_project_0_records_in_total": int64(5),
  1417. "op_project_0_records_out_total": int64(5),
  1418. "sink_mockSink_0_exceptions_total": int64(0),
  1419. "sink_mockSink_0_records_in_total": int64(5),
  1420. "sink_mockSink_0_records_out_total": int64(5),
  1421. "source_demoE_0_exceptions_total": int64(0),
  1422. "source_demoE_0_records_in_total": int64(6),
  1423. "source_demoE_0_records_out_total": int64(6),
  1424. "source_demo1E_0_exceptions_total": int64(0),
  1425. "source_demo1E_0_records_in_total": int64(6),
  1426. "source_demo1E_0_records_out_total": int64(6),
  1427. "op_window_0_exceptions_total": int64(0),
  1428. "op_window_0_process_latency_ms": int64(0),
  1429. "op_window_0_records_in_total": int64(12),
  1430. "op_window_0_records_out_total": int64(5),
  1431. "op_join_0_exceptions_total": int64(0),
  1432. "op_join_0_process_latency_ms": int64(0),
  1433. "op_join_0_records_in_total": int64(5),
  1434. "op_join_0_records_out_total": int64(5),
  1435. },
  1436. }, {
  1437. name: `rule4`,
  1438. sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  1439. size: 6,
  1440. r: [][]map[string]interface{}{
  1441. {{
  1442. "color": "red",
  1443. }}, {{
  1444. "color": "blue",
  1445. }, {
  1446. "color": "red",
  1447. }}, {{
  1448. "color": "blue",
  1449. }, {
  1450. "color": "yellow",
  1451. }}, {{
  1452. "color": "blue",
  1453. }, {
  1454. "color": "red",
  1455. }, {
  1456. "color": "yellow",
  1457. }},
  1458. },
  1459. m: map[string]interface{}{
  1460. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1461. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1462. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1463. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1464. "op_project_0_exceptions_total": int64(0),
  1465. "op_project_0_process_latency_ms": int64(0),
  1466. "op_project_0_records_in_total": int64(4),
  1467. "op_project_0_records_out_total": int64(4),
  1468. "sink_mockSink_0_exceptions_total": int64(0),
  1469. "sink_mockSink_0_records_in_total": int64(4),
  1470. "sink_mockSink_0_records_out_total": int64(4),
  1471. "source_demoE_0_exceptions_total": int64(0),
  1472. "source_demoE_0_records_in_total": int64(6),
  1473. "source_demoE_0_records_out_total": int64(6),
  1474. "op_window_0_exceptions_total": int64(0),
  1475. "op_window_0_process_latency_ms": int64(0),
  1476. "op_window_0_records_in_total": int64(6),
  1477. "op_window_0_records_out_total": int64(4),
  1478. "op_aggregate_0_exceptions_total": int64(0),
  1479. "op_aggregate_0_process_latency_ms": int64(0),
  1480. "op_aggregate_0_records_in_total": int64(4),
  1481. "op_aggregate_0_records_out_total": int64(4),
  1482. "op_order_0_exceptions_total": int64(0),
  1483. "op_order_0_process_latency_ms": int64(0),
  1484. "op_order_0_records_in_total": int64(4),
  1485. "op_order_0_records_out_total": int64(4),
  1486. },
  1487. }, {
  1488. name: `rule5`,
  1489. sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  1490. size: 12,
  1491. r: [][]map[string]interface{}{
  1492. {{
  1493. "temp": 25.5,
  1494. }}, {{
  1495. "temp": 28.1,
  1496. }, {
  1497. "temp": 27.4,
  1498. }, {
  1499. "temp": 25.5,
  1500. }}, {{
  1501. "temp": 26.2,
  1502. }, {
  1503. "temp": 26.8,
  1504. }, {
  1505. "temp": 28.9,
  1506. }, {
  1507. "temp": 29.1,
  1508. }, {
  1509. "temp": 32.2,
  1510. }}, {{
  1511. "temp": 30.9,
  1512. }},
  1513. },
  1514. m: map[string]interface{}{
  1515. "op_preprocessor_sessionDemoE_0_exceptions_total": int64(0),
  1516. "op_preprocessor_sessionDemoE_0_process_latency_ms": int64(0),
  1517. "op_preprocessor_sessionDemoE_0_records_in_total": int64(12),
  1518. "op_preprocessor_sessionDemoE_0_records_out_total": int64(12),
  1519. "op_project_0_exceptions_total": int64(0),
  1520. "op_project_0_process_latency_ms": int64(0),
  1521. "op_project_0_records_in_total": int64(4),
  1522. "op_project_0_records_out_total": int64(4),
  1523. "sink_mockSink_0_exceptions_total": int64(0),
  1524. "sink_mockSink_0_records_in_total": int64(4),
  1525. "sink_mockSink_0_records_out_total": int64(4),
  1526. "source_sessionDemoE_0_exceptions_total": int64(0),
  1527. "source_sessionDemoE_0_records_in_total": int64(12),
  1528. "source_sessionDemoE_0_records_out_total": int64(12),
  1529. "op_window_0_exceptions_total": int64(0),
  1530. "op_window_0_process_latency_ms": int64(0),
  1531. "op_window_0_records_in_total": int64(12),
  1532. "op_window_0_records_out_total": int64(4),
  1533. },
  1534. }, {
  1535. name: `rule6`,
  1536. sql: `SELECT max(temp) as m, count(color) as c FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  1537. size: 6,
  1538. r: [][]map[string]interface{}{
  1539. {{
  1540. "m": 25.5,
  1541. "c": float64(1),
  1542. }}, {{
  1543. "m": 25.5,
  1544. "c": float64(1),
  1545. }}, {{
  1546. "m": 28.1,
  1547. "c": float64(1),
  1548. }}, {{
  1549. "m": 28.1,
  1550. "c": float64(2),
  1551. }}, {{
  1552. "m": 27.4,
  1553. "c": float64(2),
  1554. }},
  1555. },
  1556. m: map[string]interface{}{
  1557. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1558. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1559. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1560. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1561. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  1562. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  1563. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  1564. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  1565. "op_project_0_exceptions_total": int64(0),
  1566. "op_project_0_process_latency_ms": int64(0),
  1567. "op_project_0_records_in_total": int64(5),
  1568. "op_project_0_records_out_total": int64(5),
  1569. "sink_mockSink_0_exceptions_total": int64(0),
  1570. "sink_mockSink_0_records_in_total": int64(5),
  1571. "sink_mockSink_0_records_out_total": int64(5),
  1572. "source_demoE_0_exceptions_total": int64(0),
  1573. "source_demoE_0_records_in_total": int64(6),
  1574. "source_demoE_0_records_out_total": int64(6),
  1575. "source_demo1E_0_exceptions_total": int64(0),
  1576. "source_demo1E_0_records_in_total": int64(6),
  1577. "source_demo1E_0_records_out_total": int64(6),
  1578. "op_window_0_exceptions_total": int64(0),
  1579. "op_window_0_records_in_total": int64(12),
  1580. "op_window_0_records_out_total": int64(5),
  1581. "op_join_0_exceptions_total": int64(0),
  1582. "op_join_0_process_latency_ms": int64(0),
  1583. "op_join_0_records_in_total": int64(5),
  1584. "op_join_0_records_out_total": int64(5),
  1585. },
  1586. },
  1587. }
  1588. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1589. createEventStreams(t)
  1590. defer dropEventStreams(t)
  1591. for i, tt := range tests {
  1592. test.ResetClock(1541152486000)
  1593. p := NewRuleProcessor(DbDir)
  1594. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1595. var (
  1596. sources []*nodes.SourceNode
  1597. syncs []chan int
  1598. )
  1599. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1600. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1601. } else {
  1602. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1603. t.Errorf("sql %s is not a select statement", tt.sql)
  1604. } else {
  1605. streams := xsql.GetStreams(selectStmt)
  1606. for _, stream := range streams {
  1607. next := make(chan int)
  1608. syncs = append(syncs, next)
  1609. source := getEventMockSource(stream, next, tt.size)
  1610. sources = append(sources, source)
  1611. }
  1612. }
  1613. }
  1614. tp, inputs, err := p.createTopoWithSources(&api.Rule{
  1615. Id: tt.name, Sql: tt.sql,
  1616. Options: map[string]interface{}{
  1617. "isEventTime": true,
  1618. "lateTolerance": float64(1000),
  1619. },
  1620. }, sources)
  1621. if err != nil {
  1622. t.Error(err)
  1623. }
  1624. mockSink := test.NewMockSink()
  1625. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  1626. tp.AddSink(inputs, sink)
  1627. errCh := tp.Open()
  1628. func() {
  1629. for i := 0; i < tt.size*len(syncs); i++ {
  1630. syncs[i%len(syncs)] <- i
  1631. for {
  1632. time.Sleep(1)
  1633. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1634. break
  1635. }
  1636. }
  1637. select {
  1638. case err = <-errCh:
  1639. t.Log(err)
  1640. tp.Cancel()
  1641. return
  1642. default:
  1643. }
  1644. }
  1645. mockClock := test.GetMockClock()
  1646. mockClock.Add(1000 * time.Millisecond)
  1647. retry := 100
  1648. for ; retry > 0; retry-- {
  1649. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1650. break
  1651. }
  1652. t.Logf("wait to try another %d times", retry)
  1653. time.Sleep(time.Duration(retry) * time.Millisecond)
  1654. }
  1655. if retry == 0 {
  1656. err := compareMetrics(tp, tt.m, tt.sql)
  1657. t.Errorf("could not get correct metrics: %v", err)
  1658. }
  1659. }()
  1660. results := mockSink.GetResults()
  1661. var maps [][]map[string]interface{}
  1662. for _, v := range results {
  1663. var mapRes []map[string]interface{}
  1664. err := json.Unmarshal(v, &mapRes)
  1665. if err != nil {
  1666. t.Errorf("Failed to parse the input into map")
  1667. continue
  1668. }
  1669. maps = append(maps, mapRes)
  1670. }
  1671. if !reflect.DeepEqual(tt.r, maps) {
  1672. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1673. }
  1674. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1675. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1676. }
  1677. tp.Cancel()
  1678. }
  1679. }
  1680. func getMetric(tp *xstream.TopologyNew, name string) int {
  1681. keys, values := tp.GetMetrics()
  1682. for index, key := range keys {
  1683. if key == name {
  1684. return int(values[index].(int64))
  1685. }
  1686. }
  1687. fmt.Println("can't find " + name)
  1688. return 0
  1689. }
  1690. func compareMetrics(tp *xstream.TopologyNew, m map[string]interface{}, sql string) (err error) {
  1691. keys, values := tp.GetMetrics()
  1692. //for i, k := range keys{
  1693. // log.Printf("%s:%v", k, values[i])
  1694. //}
  1695. for k, v := range m {
  1696. var (
  1697. index int
  1698. key string
  1699. matched bool
  1700. )
  1701. for index, key = range keys {
  1702. if k == key {
  1703. if strings.HasSuffix(k, "process_latency_ms") {
  1704. if values[index].(int64) >= v.(int64) {
  1705. matched = true
  1706. continue
  1707. } else {
  1708. break
  1709. }
  1710. }
  1711. if values[index] == v {
  1712. matched = true
  1713. }
  1714. break
  1715. }
  1716. }
  1717. if matched {
  1718. continue
  1719. }
  1720. //do not find
  1721. if index < len(values) {
  1722. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%t)\n\ngot=%#v(%t)\n\n", k, v, v, values[index], values[index])
  1723. } else {
  1724. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  1725. }
  1726. }
  1727. return nil
  1728. }
  1729. func errstring(err error) string {
  1730. if err != nil {
  1731. return err.Error()
  1732. }
  1733. return ""
  1734. }