xsql_processor_test.go 47 KB

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