xsql_processor_test.go 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345
  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(streamDB).ExecStmt(tt.s)
  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. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  99. demo := `CREATE STREAM demo (
  100. color STRING,
  101. size BIGINT,
  102. ts BIGINT
  103. ) WITH (DATASOURCE="demo", FORMAT="json", KEY="ts");`
  104. _, err := p.ExecStmt(demo)
  105. if err != nil {
  106. t.Log(err)
  107. }
  108. demoE := `CREATE STREAM demoE (
  109. color STRING,
  110. size BIGINT,
  111. ts BIGINT
  112. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts");`
  113. _, err = p.ExecStmt(demoE)
  114. if err != nil {
  115. t.Log(err)
  116. }
  117. demo1 := `CREATE STREAM demo1 (
  118. temp FLOAT,
  119. hum BIGINT,
  120. ts BIGINT
  121. ) WITH (DATASOURCE="demo1", FORMAT="json", KEY="ts");`
  122. _, err = p.ExecStmt(demo1)
  123. if err != nil {
  124. t.Log(err)
  125. }
  126. sessionDemo := `CREATE STREAM sessionDemo (
  127. temp FLOAT,
  128. hum BIGINT,
  129. ts BIGINT
  130. ) WITH (DATASOURCE="sessionDemo", FORMAT="json", KEY="ts");`
  131. _, err = p.ExecStmt(sessionDemo)
  132. if err != nil {
  133. t.Log(err)
  134. }
  135. }
  136. func dropStreams(t *testing.T) {
  137. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  138. demo := `DROP STREAM demo`
  139. _, err := p.ExecStmt(demo)
  140. if err != nil {
  141. t.Log(err)
  142. }
  143. demoE := `DROP STREAM demoE`
  144. _, err = p.ExecStmt(demoE)
  145. if err != nil {
  146. t.Log(err)
  147. }
  148. demo1 := `DROP STREAM demo1`
  149. _, err = p.ExecStmt(demo1)
  150. if err != nil {
  151. t.Log(err)
  152. }
  153. sessionDemo := `DROP STREAM sessionDemo`
  154. _, err = p.ExecStmt(sessionDemo)
  155. if err != nil {
  156. t.Log(err)
  157. }
  158. }
  159. func createSchemalessStreams(t *testing.T) {
  160. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  161. demo := `CREATE STREAM ldemo (
  162. ) WITH (DATASOURCE="ldemo", FORMAT="json");`
  163. _, err := p.ExecStmt(demo)
  164. if err != nil {
  165. t.Log(err)
  166. }
  167. demo1 := `CREATE STREAM ldemo1 (
  168. ) WITH (DATASOURCE="ldemo1", FORMAT="json");`
  169. _, err = p.ExecStmt(demo1)
  170. if err != nil {
  171. t.Log(err)
  172. }
  173. sessionDemo := `CREATE STREAM lsessionDemo (
  174. ) WITH (DATASOURCE="lsessionDemo", FORMAT="json");`
  175. _, err = p.ExecStmt(sessionDemo)
  176. if err != nil {
  177. t.Log(err)
  178. }
  179. }
  180. func dropSchemalessStreams(t *testing.T) {
  181. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  182. demo := `DROP STREAM ldemo`
  183. _, err := p.ExecStmt(demo)
  184. if err != nil {
  185. t.Log(err)
  186. }
  187. demo1 := `DROP STREAM ldemo1`
  188. _, err = p.ExecStmt(demo1)
  189. if err != nil {
  190. t.Log(err)
  191. }
  192. sessionDemo := `DROP STREAM lsessionDemo`
  193. _, err = p.ExecStmt(sessionDemo)
  194. if err != nil {
  195. t.Log(err)
  196. }
  197. }
  198. func getMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  199. var data []*xsql.Tuple
  200. switch name {
  201. case "demo":
  202. data = []*xsql.Tuple{
  203. {
  204. Emitter: name,
  205. Message: map[string]interface{}{
  206. "color": "red",
  207. "size": 3,
  208. "ts": 1541152486013,
  209. },
  210. Timestamp: 1541152486013,
  211. },
  212. {
  213. Emitter: name,
  214. Message: map[string]interface{}{
  215. "color": "blue",
  216. "size": 6,
  217. "ts": 1541152486822,
  218. },
  219. Timestamp: 1541152486822,
  220. },
  221. {
  222. Emitter: name,
  223. Message: map[string]interface{}{
  224. "color": "blue",
  225. "size": 2,
  226. "ts": 1541152487632,
  227. },
  228. Timestamp: 1541152487632,
  229. },
  230. {
  231. Emitter: name,
  232. Message: map[string]interface{}{
  233. "color": "yellow",
  234. "size": 4,
  235. "ts": 1541152488442,
  236. },
  237. Timestamp: 1541152488442,
  238. },
  239. {
  240. Emitter: name,
  241. Message: map[string]interface{}{
  242. "color": "red",
  243. "size": 1,
  244. "ts": 1541152489252,
  245. },
  246. Timestamp: 1541152489252,
  247. },
  248. }
  249. case "demoE":
  250. data = []*xsql.Tuple{
  251. {
  252. Emitter: name,
  253. Message: map[string]interface{}{
  254. "color": 3,
  255. "size": "red",
  256. "ts": 1541152486013,
  257. },
  258. Timestamp: 1541152486013,
  259. },
  260. {
  261. Emitter: name,
  262. Message: map[string]interface{}{
  263. "color": "blue",
  264. "size": 6,
  265. "ts": "1541152486822",
  266. },
  267. Timestamp: 1541152486822,
  268. },
  269. {
  270. Emitter: name,
  271. Message: map[string]interface{}{
  272. "color": "blue",
  273. "size": 2,
  274. "ts": 1541152487632,
  275. },
  276. Timestamp: 1541152487632,
  277. },
  278. {
  279. Emitter: name,
  280. Message: map[string]interface{}{
  281. "color": 7,
  282. "size": 4,
  283. "ts": 1541152488442,
  284. },
  285. Timestamp: 1541152488442,
  286. },
  287. {
  288. Emitter: name,
  289. Message: map[string]interface{}{
  290. "color": "red",
  291. "size": "blue",
  292. "ts": 1541152489252,
  293. },
  294. Timestamp: 1541152489252,
  295. },
  296. }
  297. case "demo1":
  298. data = []*xsql.Tuple{
  299. {
  300. Emitter: name,
  301. Message: map[string]interface{}{
  302. "temp": 25.5,
  303. "hum": 65,
  304. "ts": 1541152486013,
  305. },
  306. Timestamp: 1541152486013,
  307. },
  308. {
  309. Emitter: name,
  310. Message: map[string]interface{}{
  311. "temp": 27.5,
  312. "hum": 59,
  313. "ts": 1541152486823,
  314. },
  315. Timestamp: 1541152486823,
  316. },
  317. {
  318. Emitter: name,
  319. Message: map[string]interface{}{
  320. "temp": 28.1,
  321. "hum": 75,
  322. "ts": 1541152487632,
  323. },
  324. Timestamp: 1541152487632,
  325. },
  326. {
  327. Emitter: name,
  328. Message: map[string]interface{}{
  329. "temp": 27.4,
  330. "hum": 80,
  331. "ts": 1541152488442,
  332. },
  333. Timestamp: 1541152488442,
  334. },
  335. {
  336. Emitter: name,
  337. Message: map[string]interface{}{
  338. "temp": 25.5,
  339. "hum": 62,
  340. "ts": 1541152489252,
  341. },
  342. Timestamp: 1541152489252,
  343. },
  344. }
  345. case "sessionDemo":
  346. data = []*xsql.Tuple{
  347. {
  348. Emitter: name,
  349. Message: map[string]interface{}{
  350. "temp": 25.5,
  351. "hum": 65,
  352. "ts": 1541152486013,
  353. },
  354. Timestamp: 1541152486013,
  355. },
  356. {
  357. Emitter: name,
  358. Message: map[string]interface{}{
  359. "temp": 27.5,
  360. "hum": 59,
  361. "ts": 1541152486823,
  362. },
  363. Timestamp: 1541152486823,
  364. },
  365. {
  366. Emitter: name,
  367. Message: map[string]interface{}{
  368. "temp": 28.1,
  369. "hum": 75,
  370. "ts": 1541152487932,
  371. },
  372. Timestamp: 1541152487932,
  373. },
  374. {
  375. Emitter: name,
  376. Message: map[string]interface{}{
  377. "temp": 27.4,
  378. "hum": 80,
  379. "ts": 1541152488442,
  380. },
  381. Timestamp: 1541152488442,
  382. },
  383. {
  384. Emitter: name,
  385. Message: map[string]interface{}{
  386. "temp": 25.5,
  387. "hum": 62,
  388. "ts": 1541152489252,
  389. },
  390. Timestamp: 1541152489252,
  391. },
  392. {
  393. Emitter: name,
  394. Message: map[string]interface{}{
  395. "temp": 26.2,
  396. "hum": 63,
  397. "ts": 1541152490062,
  398. },
  399. Timestamp: 1541152490062,
  400. },
  401. {
  402. Emitter: name,
  403. Message: map[string]interface{}{
  404. "temp": 26.8,
  405. "hum": 71,
  406. "ts": 1541152490872,
  407. },
  408. Timestamp: 1541152490872,
  409. },
  410. {
  411. Emitter: name,
  412. Message: map[string]interface{}{
  413. "temp": 28.9,
  414. "hum": 85,
  415. "ts": 1541152491682,
  416. },
  417. Timestamp: 1541152491682,
  418. },
  419. {
  420. Emitter: name,
  421. Message: map[string]interface{}{
  422. "temp": 29.1,
  423. "hum": 92,
  424. "ts": 1541152492492,
  425. },
  426. Timestamp: 1541152492492,
  427. },
  428. {
  429. Emitter: name,
  430. Message: map[string]interface{}{
  431. "temp": 32.2,
  432. "hum": 99,
  433. "ts": 1541152493202,
  434. },
  435. Timestamp: 1541152493202,
  436. },
  437. {
  438. Emitter: name,
  439. Message: map[string]interface{}{
  440. "temp": 30.9,
  441. "hum": 87,
  442. "ts": 1541152494112,
  443. },
  444. Timestamp: 1541152494112,
  445. },
  446. }
  447. }
  448. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  449. "DATASOURCE": name,
  450. })
  451. }
  452. func TestSingleSQL(t *testing.T) {
  453. var tests = []struct {
  454. name string
  455. sql string
  456. r [][]map[string]interface{}
  457. s string
  458. m map[string]interface{}
  459. }{
  460. {
  461. name: `rule1`,
  462. sql: `SELECT * FROM demo`,
  463. r: [][]map[string]interface{}{
  464. {{
  465. "color": "red",
  466. "size": float64(3),
  467. "ts": float64(1541152486013),
  468. }},
  469. {{
  470. "color": "blue",
  471. "size": float64(6),
  472. "ts": float64(1541152486822),
  473. }},
  474. {{
  475. "color": "blue",
  476. "size": float64(2),
  477. "ts": float64(1541152487632),
  478. }},
  479. {{
  480. "color": "yellow",
  481. "size": float64(4),
  482. "ts": float64(1541152488442),
  483. }},
  484. {{
  485. "color": "red",
  486. "size": float64(1),
  487. "ts": float64(1541152489252),
  488. }},
  489. },
  490. m: map[string]interface{}{
  491. "op_preprocessor_demo_0_exceptions_total": int64(0),
  492. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  493. "op_preprocessor_demo_0_records_in_total": int64(5),
  494. "op_preprocessor_demo_0_records_out_total": int64(5),
  495. "op_project_0_exceptions_total": int64(0),
  496. "op_project_0_process_latency_ms": int64(0),
  497. "op_project_0_records_in_total": int64(5),
  498. "op_project_0_records_out_total": int64(5),
  499. "sink_mockSink_0_exceptions_total": int64(0),
  500. "sink_mockSink_0_records_in_total": int64(5),
  501. "sink_mockSink_0_records_out_total": int64(5),
  502. "source_demo_0_exceptions_total": int64(0),
  503. "source_demo_0_records_in_total": int64(5),
  504. "source_demo_0_records_out_total": int64(5),
  505. },
  506. s: "sink_mockSink_0_records_out_total",
  507. }, {
  508. name: `rule2`,
  509. sql: `SELECT color, ts FROM demo where size > 3`,
  510. r: [][]map[string]interface{}{
  511. {{
  512. "color": "blue",
  513. "ts": float64(1541152486822),
  514. }},
  515. {{
  516. "color": "yellow",
  517. "ts": float64(1541152488442),
  518. }},
  519. },
  520. s: "op_filter_0_records_in_total",
  521. m: map[string]interface{}{
  522. "op_preprocessor_demo_0_exceptions_total": int64(0),
  523. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  524. "op_preprocessor_demo_0_records_in_total": int64(5),
  525. "op_preprocessor_demo_0_records_out_total": int64(5),
  526. "op_project_0_exceptions_total": int64(0),
  527. "op_project_0_process_latency_ms": int64(0),
  528. "op_project_0_records_in_total": int64(2),
  529. "op_project_0_records_out_total": int64(2),
  530. "sink_mockSink_0_exceptions_total": int64(0),
  531. "sink_mockSink_0_records_in_total": int64(2),
  532. "sink_mockSink_0_records_out_total": int64(2),
  533. "source_demo_0_exceptions_total": int64(0),
  534. "source_demo_0_records_in_total": int64(5),
  535. "source_demo_0_records_out_total": int64(5),
  536. "op_filter_0_exceptions_total": int64(0),
  537. "op_filter_0_process_latency_ms": int64(0),
  538. "op_filter_0_records_in_total": int64(5),
  539. "op_filter_0_records_out_total": int64(2),
  540. },
  541. }, {
  542. name: `rule3`,
  543. sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  544. r: [][]map[string]interface{}{
  545. {{
  546. "Int8": float64(6),
  547. "ts": float64(1541152486822),
  548. }},
  549. {{
  550. "Int8": float64(4),
  551. "ts": float64(1541152488442),
  552. }},
  553. },
  554. s: "op_filter_0_records_in_total",
  555. m: map[string]interface{}{
  556. "op_preprocessor_demo_0_exceptions_total": int64(0),
  557. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  558. "op_preprocessor_demo_0_records_in_total": int64(5),
  559. "op_preprocessor_demo_0_records_out_total": int64(5),
  560. "op_project_0_exceptions_total": int64(0),
  561. "op_project_0_process_latency_ms": int64(0),
  562. "op_project_0_records_in_total": int64(2),
  563. "op_project_0_records_out_total": int64(2),
  564. "sink_mockSink_0_exceptions_total": int64(0),
  565. "sink_mockSink_0_records_in_total": int64(2),
  566. "sink_mockSink_0_records_out_total": int64(2),
  567. "source_demo_0_exceptions_total": int64(0),
  568. "source_demo_0_records_in_total": int64(5),
  569. "source_demo_0_records_out_total": int64(5),
  570. "op_filter_0_exceptions_total": int64(0),
  571. "op_filter_0_process_latency_ms": int64(0),
  572. "op_filter_0_records_in_total": int64(5),
  573. "op_filter_0_records_out_total": int64(2),
  574. },
  575. }, {
  576. name: `rule4`,
  577. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  578. r: [][]map[string]interface{}{
  579. {{
  580. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  581. }},
  582. {{
  583. "Int8": float64(6),
  584. "ts": float64(1541152486822),
  585. }},
  586. {{
  587. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  588. }},
  589. {{
  590. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  591. }},
  592. },
  593. s: "op_filter_0_records_in_total",
  594. m: map[string]interface{}{
  595. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  596. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  597. "op_preprocessor_demoE_0_records_in_total": int64(5),
  598. "op_preprocessor_demoE_0_records_out_total": int64(2),
  599. "op_project_0_exceptions_total": int64(3),
  600. "op_project_0_process_latency_ms": int64(0),
  601. "op_project_0_records_in_total": int64(4),
  602. "op_project_0_records_out_total": int64(1),
  603. "sink_mockSink_0_exceptions_total": int64(0),
  604. "sink_mockSink_0_records_in_total": int64(4),
  605. "sink_mockSink_0_records_out_total": int64(4),
  606. "source_demoE_0_exceptions_total": int64(0),
  607. "source_demoE_0_records_in_total": int64(5),
  608. "source_demoE_0_records_out_total": int64(5),
  609. "op_filter_0_exceptions_total": int64(3),
  610. "op_filter_0_process_latency_ms": int64(0),
  611. "op_filter_0_records_in_total": int64(5),
  612. "op_filter_0_records_out_total": int64(1),
  613. },
  614. },
  615. }
  616. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  617. createStreams(t)
  618. defer dropStreams(t)
  619. //defer close(done)
  620. for i, tt := range tests {
  621. test.ResetClock(1541152486000)
  622. p := NewRuleProcessor(DbDir)
  623. parser := xsql.NewParser(strings.NewReader(tt.sql))
  624. var (
  625. sources []*nodes.SourceNode
  626. syncs []chan int
  627. )
  628. if stmt, err := xsql.Language.Parse(parser); err != nil {
  629. t.Errorf("parse sql %s error: %s", tt.sql, err)
  630. } else {
  631. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  632. t.Errorf("sql %s is not a select statement", tt.sql)
  633. } else {
  634. streams := xsql.GetStreams(selectStmt)
  635. for _, stream := range streams {
  636. next := make(chan int)
  637. syncs = append(syncs, next)
  638. source := getMockSource(stream, next, 5)
  639. sources = append(sources, source)
  640. }
  641. }
  642. }
  643. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  644. "bufferLength": float64(100),
  645. }}, sources)
  646. if err != nil {
  647. t.Error(err)
  648. }
  649. mockSink := test.NewMockSink()
  650. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  651. tp.AddSink(inputs, sink)
  652. errCh := tp.Open()
  653. func() {
  654. for i := 0; i < 5; i++ {
  655. syncs[i%len(syncs)] <- i
  656. select {
  657. case err = <-errCh:
  658. t.Log(err)
  659. tp.Cancel()
  660. return
  661. default:
  662. }
  663. }
  664. for retry := 100; retry > 0; retry-- {
  665. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  666. break
  667. }
  668. time.Sleep(time.Duration(retry) * time.Millisecond)
  669. }
  670. }()
  671. results := mockSink.GetResults()
  672. var maps [][]map[string]interface{}
  673. for _, v := range results {
  674. var mapRes []map[string]interface{}
  675. err := json.Unmarshal(v, &mapRes)
  676. if err != nil {
  677. t.Errorf("Failed to parse the input into map")
  678. continue
  679. }
  680. maps = append(maps, mapRes)
  681. }
  682. if !reflect.DeepEqual(tt.r, maps) {
  683. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  684. continue
  685. }
  686. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  687. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  688. }
  689. tp.Cancel()
  690. }
  691. }
  692. func getMockSourceL(name string, done <-chan int, size int) *nodes.SourceNode {
  693. var data []*xsql.Tuple
  694. switch name {
  695. case "ldemo":
  696. data = []*xsql.Tuple{
  697. {
  698. Emitter: name,
  699. Message: map[string]interface{}{
  700. "color": "red",
  701. "size": 3,
  702. "ts": 1541152486013,
  703. },
  704. Timestamp: 1541152486013,
  705. },
  706. {
  707. Emitter: name,
  708. Message: map[string]interface{}{
  709. "color": "blue",
  710. "size": "string",
  711. "ts": 1541152486822,
  712. },
  713. Timestamp: 1541152486822,
  714. },
  715. {
  716. Emitter: name,
  717. Message: map[string]interface{}{
  718. "size": 2,
  719. "ts": 1541152487632,
  720. },
  721. Timestamp: 1541152487632,
  722. },
  723. {
  724. Emitter: name,
  725. Message: map[string]interface{}{
  726. "color": 49,
  727. "size": 2,
  728. "ts": 1541152488442,
  729. },
  730. Timestamp: 1541152488442,
  731. },
  732. {
  733. Emitter: name,
  734. Message: map[string]interface{}{
  735. "color": "red",
  736. "ts": 1541152489252,
  737. },
  738. Timestamp: 1541152489252,
  739. },
  740. }
  741. case "demo1":
  742. data = []*xsql.Tuple{
  743. {
  744. Emitter: name,
  745. Message: map[string]interface{}{
  746. "temp": 25.5,
  747. "hum": 65,
  748. "ts": 1541152486013,
  749. },
  750. Timestamp: 1541152486013,
  751. },
  752. {
  753. Emitter: name,
  754. Message: map[string]interface{}{
  755. "temp": 27.5,
  756. "hum": 59,
  757. "ts": 1541152486823,
  758. },
  759. Timestamp: 1541152486823,
  760. },
  761. {
  762. Emitter: name,
  763. Message: map[string]interface{}{
  764. "temp": 28.1,
  765. "hum": 75,
  766. "ts": 1541152487632,
  767. },
  768. Timestamp: 1541152487632,
  769. },
  770. {
  771. Emitter: name,
  772. Message: map[string]interface{}{
  773. "temp": 27.4,
  774. "hum": 80,
  775. "ts": 1541152488442,
  776. },
  777. Timestamp: 1541152488442,
  778. },
  779. {
  780. Emitter: name,
  781. Message: map[string]interface{}{
  782. "temp": 25.5,
  783. "hum": 62,
  784. "ts": 1541152489252,
  785. },
  786. Timestamp: 1541152489252,
  787. },
  788. }
  789. case "sessionDemo":
  790. data = []*xsql.Tuple{
  791. {
  792. Emitter: name,
  793. Message: map[string]interface{}{
  794. "temp": 25.5,
  795. "hum": 65,
  796. "ts": 1541152486013,
  797. },
  798. Timestamp: 1541152486013,
  799. },
  800. {
  801. Emitter: name,
  802. Message: map[string]interface{}{
  803. "temp": 27.5,
  804. "hum": 59,
  805. "ts": 1541152486823,
  806. },
  807. Timestamp: 1541152486823,
  808. },
  809. {
  810. Emitter: name,
  811. Message: map[string]interface{}{
  812. "temp": 28.1,
  813. "hum": 75,
  814. "ts": 1541152487932,
  815. },
  816. Timestamp: 1541152487932,
  817. },
  818. {
  819. Emitter: name,
  820. Message: map[string]interface{}{
  821. "temp": 27.4,
  822. "hum": 80,
  823. "ts": 1541152488442,
  824. },
  825. Timestamp: 1541152488442,
  826. },
  827. {
  828. Emitter: name,
  829. Message: map[string]interface{}{
  830. "temp": 25.5,
  831. "hum": 62,
  832. "ts": 1541152489252,
  833. },
  834. Timestamp: 1541152489252,
  835. },
  836. {
  837. Emitter: name,
  838. Message: map[string]interface{}{
  839. "temp": 26.2,
  840. "hum": 63,
  841. "ts": 1541152490062,
  842. },
  843. Timestamp: 1541152490062,
  844. },
  845. {
  846. Emitter: name,
  847. Message: map[string]interface{}{
  848. "temp": 26.8,
  849. "hum": 71,
  850. "ts": 1541152490872,
  851. },
  852. Timestamp: 1541152490872,
  853. },
  854. {
  855. Emitter: name,
  856. Message: map[string]interface{}{
  857. "temp": 28.9,
  858. "hum": 85,
  859. "ts": 1541152491682,
  860. },
  861. Timestamp: 1541152491682,
  862. },
  863. {
  864. Emitter: name,
  865. Message: map[string]interface{}{
  866. "temp": 29.1,
  867. "hum": 92,
  868. "ts": 1541152492492,
  869. },
  870. Timestamp: 1541152492492,
  871. },
  872. {
  873. Emitter: name,
  874. Message: map[string]interface{}{
  875. "temp": 2.2,
  876. "hum": 99,
  877. "ts": 1541152493202,
  878. },
  879. Timestamp: 1541152493202,
  880. },
  881. {
  882. Emitter: name,
  883. Message: map[string]interface{}{
  884. "temp": 30.9,
  885. "hum": 87,
  886. "ts": 1541152494112,
  887. },
  888. Timestamp: 1541152494112,
  889. },
  890. }
  891. }
  892. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  893. "DATASOURCE": name,
  894. })
  895. }
  896. func TestSingleSQLError(t *testing.T) {
  897. var tests = []struct {
  898. name string
  899. sql string
  900. r [][]map[string]interface{}
  901. s string
  902. m map[string]interface{}
  903. }{
  904. {
  905. name: `rule1`,
  906. sql: `SELECT color, ts FROM ldemo where size >= 3`,
  907. r: [][]map[string]interface{}{
  908. {{
  909. "color": "red",
  910. "ts": float64(1541152486013),
  911. }},
  912. {{
  913. "error": "invalid operation string >= int64",
  914. }},
  915. },
  916. s: "op_filter_0_records_in_total",
  917. m: map[string]interface{}{
  918. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  919. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  920. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  921. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  922. "op_project_0_exceptions_total": int64(1),
  923. "op_project_0_process_latency_ms": int64(0),
  924. "op_project_0_records_in_total": int64(2),
  925. "op_project_0_records_out_total": int64(1),
  926. "sink_mockSink_0_exceptions_total": int64(0),
  927. "sink_mockSink_0_records_in_total": int64(2),
  928. "sink_mockSink_0_records_out_total": int64(2),
  929. "source_ldemo_0_exceptions_total": int64(0),
  930. "source_ldemo_0_records_in_total": int64(5),
  931. "source_ldemo_0_records_out_total": int64(5),
  932. "op_filter_0_exceptions_total": int64(1),
  933. "op_filter_0_process_latency_ms": int64(0),
  934. "op_filter_0_records_in_total": int64(5),
  935. "op_filter_0_records_out_total": int64(1),
  936. },
  937. }, {
  938. name: `rule2`,
  939. sql: `SELECT size * 5 FROM ldemo`,
  940. r: [][]map[string]interface{}{
  941. {{
  942. "rengine_field_0": float64(15),
  943. }},
  944. {{
  945. "error": "invalid operation string * int64",
  946. }},
  947. {{
  948. "rengine_field_0": float64(10),
  949. }},
  950. {{
  951. "rengine_field_0": float64(10),
  952. }},
  953. {{}},
  954. },
  955. s: "op_filter_0_records_in_total",
  956. m: map[string]interface{}{
  957. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  958. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  959. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  960. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  961. "op_project_0_exceptions_total": int64(1),
  962. "op_project_0_process_latency_ms": int64(0),
  963. "op_project_0_records_in_total": int64(5),
  964. "op_project_0_records_out_total": int64(4),
  965. "sink_mockSink_0_exceptions_total": int64(0),
  966. "sink_mockSink_0_records_in_total": int64(5),
  967. "sink_mockSink_0_records_out_total": int64(5),
  968. "source_ldemo_0_exceptions_total": int64(0),
  969. "source_ldemo_0_records_in_total": int64(5),
  970. "source_ldemo_0_records_out_total": int64(5),
  971. },
  972. },
  973. }
  974. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  975. createSchemalessStreams(t)
  976. defer dropSchemalessStreams(t)
  977. //defer close(done)
  978. for i, tt := range tests {
  979. test.ResetClock(1541152486000)
  980. p := NewRuleProcessor(DbDir)
  981. parser := xsql.NewParser(strings.NewReader(tt.sql))
  982. var (
  983. sources []*nodes.SourceNode
  984. syncs []chan int
  985. )
  986. if stmt, err := xsql.Language.Parse(parser); err != nil {
  987. t.Errorf("parse sql %s error: %s", tt.sql, err)
  988. } else {
  989. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  990. t.Errorf("sql %s is not a select statement", tt.sql)
  991. } else {
  992. streams := xsql.GetStreams(selectStmt)
  993. for _, stream := range streams {
  994. next := make(chan int)
  995. syncs = append(syncs, next)
  996. source := getMockSourceL(stream, next, 5)
  997. sources = append(sources, source)
  998. }
  999. }
  1000. }
  1001. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1002. "bufferLength": float64(100),
  1003. }}, sources)
  1004. if err != nil {
  1005. t.Error(err)
  1006. }
  1007. mockSink := test.NewMockSink()
  1008. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  1009. tp.AddSink(inputs, sink)
  1010. errCh := tp.Open()
  1011. func() {
  1012. for i := 0; i < 5; i++ {
  1013. syncs[i%len(syncs)] <- i
  1014. select {
  1015. case err = <-errCh:
  1016. t.Log(err)
  1017. tp.Cancel()
  1018. return
  1019. default:
  1020. }
  1021. }
  1022. for retry := 100; retry > 0; retry-- {
  1023. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1024. break
  1025. }
  1026. time.Sleep(time.Duration(retry) * time.Millisecond)
  1027. }
  1028. }()
  1029. results := mockSink.GetResults()
  1030. var maps [][]map[string]interface{}
  1031. for _, v := range results {
  1032. var mapRes []map[string]interface{}
  1033. err := json.Unmarshal(v, &mapRes)
  1034. if err != nil {
  1035. t.Errorf("Failed to parse the input into map")
  1036. continue
  1037. }
  1038. maps = append(maps, mapRes)
  1039. }
  1040. if !reflect.DeepEqual(tt.r, maps) {
  1041. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1042. continue
  1043. }
  1044. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1045. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1046. }
  1047. tp.Cancel()
  1048. }
  1049. }
  1050. func TestWindow(t *testing.T) {
  1051. var tests = []struct {
  1052. name string
  1053. sql string
  1054. size int
  1055. r [][]map[string]interface{}
  1056. m map[string]interface{}
  1057. }{
  1058. {
  1059. name: `rule1`,
  1060. sql: `SELECT * FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1061. size: 5,
  1062. r: [][]map[string]interface{}{
  1063. {{
  1064. "color": "red",
  1065. "size": float64(3),
  1066. "ts": float64(1541152486013),
  1067. }, {
  1068. "color": "blue",
  1069. "size": float64(6),
  1070. "ts": float64(1541152486822),
  1071. }},
  1072. {{
  1073. "color": "red",
  1074. "size": float64(3),
  1075. "ts": float64(1541152486013),
  1076. }, {
  1077. "color": "blue",
  1078. "size": float64(6),
  1079. "ts": float64(1541152486822),
  1080. }, {
  1081. "color": "blue",
  1082. "size": float64(2),
  1083. "ts": float64(1541152487632),
  1084. }},
  1085. {{
  1086. "color": "blue",
  1087. "size": float64(2),
  1088. "ts": float64(1541152487632),
  1089. }, {
  1090. "color": "yellow",
  1091. "size": float64(4),
  1092. "ts": float64(1541152488442),
  1093. }},
  1094. },
  1095. m: map[string]interface{}{
  1096. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1097. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1098. "op_preprocessor_demo_0_records_in_total": int64(5),
  1099. "op_preprocessor_demo_0_records_out_total": int64(5),
  1100. "op_project_0_exceptions_total": int64(0),
  1101. "op_project_0_process_latency_ms": int64(0),
  1102. "op_project_0_records_in_total": int64(3),
  1103. "op_project_0_records_out_total": int64(3),
  1104. "sink_mockSink_0_exceptions_total": int64(0),
  1105. "sink_mockSink_0_records_in_total": int64(3),
  1106. "sink_mockSink_0_records_out_total": int64(3),
  1107. "source_demo_0_exceptions_total": int64(0),
  1108. "source_demo_0_records_in_total": int64(5),
  1109. "source_demo_0_records_out_total": int64(5),
  1110. "op_window_0_exceptions_total": int64(0),
  1111. "op_window_0_process_latency_ms": int64(0),
  1112. "op_window_0_records_in_total": int64(5),
  1113. "op_window_0_records_out_total": int64(3),
  1114. },
  1115. }, {
  1116. name: `rule2`,
  1117. sql: `SELECT color, ts FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1118. size: 5,
  1119. r: [][]map[string]interface{}{
  1120. {{
  1121. "color": "red",
  1122. "ts": float64(1541152486013),
  1123. }, {
  1124. "color": "blue",
  1125. "ts": float64(1541152486822),
  1126. }},
  1127. {{
  1128. "color": "yellow",
  1129. "ts": float64(1541152488442),
  1130. }},
  1131. },
  1132. m: map[string]interface{}{
  1133. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1134. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1135. "op_preprocessor_demo_0_records_in_total": int64(5),
  1136. "op_preprocessor_demo_0_records_out_total": int64(5),
  1137. "op_project_0_exceptions_total": int64(0),
  1138. "op_project_0_process_latency_ms": int64(0),
  1139. "op_project_0_records_in_total": int64(2),
  1140. "op_project_0_records_out_total": int64(2),
  1141. "sink_mockSink_0_exceptions_total": int64(0),
  1142. "sink_mockSink_0_records_in_total": int64(2),
  1143. "sink_mockSink_0_records_out_total": int64(2),
  1144. "source_demo_0_exceptions_total": int64(0),
  1145. "source_demo_0_records_in_total": int64(5),
  1146. "source_demo_0_records_out_total": int64(5),
  1147. "op_window_0_exceptions_total": int64(0),
  1148. "op_window_0_process_latency_ms": int64(0),
  1149. "op_window_0_records_in_total": int64(5),
  1150. "op_window_0_records_out_total": int64(3),
  1151. "op_filter_0_exceptions_total": int64(0),
  1152. "op_filter_0_process_latency_ms": int64(0),
  1153. "op_filter_0_records_in_total": int64(3),
  1154. "op_filter_0_records_out_total": int64(2),
  1155. },
  1156. }, {
  1157. name: `rule3`,
  1158. sql: `SELECT color, temp, ts FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1159. size: 5,
  1160. r: [][]map[string]interface{}{
  1161. {{
  1162. "color": "red",
  1163. "temp": 25.5,
  1164. "ts": float64(1541152486013),
  1165. }}, {{
  1166. "color": "red",
  1167. "temp": 25.5,
  1168. "ts": float64(1541152486013),
  1169. }}, {{
  1170. "color": "red",
  1171. "temp": 25.5,
  1172. "ts": float64(1541152486013),
  1173. }}, {{
  1174. "color": "blue",
  1175. "temp": 28.1,
  1176. "ts": float64(1541152487632),
  1177. }}, {{
  1178. "color": "blue",
  1179. "temp": 28.1,
  1180. "ts": float64(1541152487632),
  1181. }}, {{
  1182. "color": "blue",
  1183. "temp": 28.1,
  1184. "ts": float64(1541152487632),
  1185. }, {
  1186. "color": "yellow",
  1187. "temp": 27.4,
  1188. "ts": float64(1541152488442),
  1189. }}, {{
  1190. "color": "yellow",
  1191. "temp": 27.4,
  1192. "ts": float64(1541152488442),
  1193. }}, {{
  1194. "color": "yellow",
  1195. "temp": 27.4,
  1196. "ts": float64(1541152488442),
  1197. }, {
  1198. "color": "red",
  1199. "temp": 25.5,
  1200. "ts": float64(1541152489252),
  1201. }},
  1202. },
  1203. m: map[string]interface{}{
  1204. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1205. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1206. "op_preprocessor_demo_0_records_in_total": int64(5),
  1207. "op_preprocessor_demo_0_records_out_total": int64(5),
  1208. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1209. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1210. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1211. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1212. "op_project_0_exceptions_total": int64(0),
  1213. "op_project_0_process_latency_ms": int64(0),
  1214. "op_project_0_records_in_total": int64(8),
  1215. "op_project_0_records_out_total": int64(8),
  1216. "sink_mockSink_0_exceptions_total": int64(0),
  1217. "sink_mockSink_0_records_in_total": int64(8),
  1218. "sink_mockSink_0_records_out_total": int64(8),
  1219. "source_demo_0_exceptions_total": int64(0),
  1220. "source_demo_0_records_in_total": int64(5),
  1221. "source_demo_0_records_out_total": int64(5),
  1222. "source_demo1_0_exceptions_total": int64(0),
  1223. "source_demo1_0_records_in_total": int64(5),
  1224. "source_demo1_0_records_out_total": int64(5),
  1225. "op_window_0_exceptions_total": int64(0),
  1226. "op_window_0_process_latency_ms": int64(0),
  1227. "op_window_0_records_in_total": int64(10),
  1228. "op_window_0_records_out_total": int64(10),
  1229. "op_join_0_exceptions_total": int64(0),
  1230. "op_join_0_process_latency_ms": int64(0),
  1231. "op_join_0_records_in_total": int64(10),
  1232. "op_join_0_records_out_total": int64(8),
  1233. },
  1234. }, {
  1235. name: `rule4`,
  1236. sql: `SELECT color FROM demo GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  1237. size: 5,
  1238. r: [][]map[string]interface{}{
  1239. {{
  1240. "color": "red",
  1241. }}, {{
  1242. "color": "blue",
  1243. }, {
  1244. "color": "red",
  1245. }}, {{
  1246. "color": "blue",
  1247. }, {
  1248. "color": "red",
  1249. }}, {{
  1250. "color": "blue",
  1251. }, {
  1252. "color": "yellow",
  1253. }}, {{
  1254. "color": "blue",
  1255. }, {
  1256. "color": "red",
  1257. }, {
  1258. "color": "yellow",
  1259. }},
  1260. },
  1261. m: map[string]interface{}{
  1262. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1263. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1264. "op_preprocessor_demo_0_records_in_total": int64(5),
  1265. "op_preprocessor_demo_0_records_out_total": int64(5),
  1266. "op_project_0_exceptions_total": int64(0),
  1267. "op_project_0_process_latency_ms": int64(0),
  1268. "op_project_0_records_in_total": int64(5),
  1269. "op_project_0_records_out_total": int64(5),
  1270. "sink_mockSink_0_exceptions_total": int64(0),
  1271. "sink_mockSink_0_records_in_total": int64(5),
  1272. "sink_mockSink_0_records_out_total": int64(5),
  1273. "source_demo_0_exceptions_total": int64(0),
  1274. "source_demo_0_records_in_total": int64(5),
  1275. "source_demo_0_records_out_total": int64(5),
  1276. "op_window_0_exceptions_total": int64(0),
  1277. "op_window_0_process_latency_ms": int64(0),
  1278. "op_window_0_records_in_total": int64(5),
  1279. "op_window_0_records_out_total": int64(5),
  1280. "op_aggregate_0_exceptions_total": int64(0),
  1281. "op_aggregate_0_process_latency_ms": int64(0),
  1282. "op_aggregate_0_records_in_total": int64(5),
  1283. "op_aggregate_0_records_out_total": int64(5),
  1284. "op_order_0_exceptions_total": int64(0),
  1285. "op_order_0_process_latency_ms": int64(0),
  1286. "op_order_0_records_in_total": int64(5),
  1287. "op_order_0_records_out_total": int64(5),
  1288. },
  1289. }, {
  1290. name: `rule5`,
  1291. sql: `SELECT temp FROM sessionDemo GROUP BY SessionWindow(ss, 2, 1) `,
  1292. size: 11,
  1293. r: [][]map[string]interface{}{
  1294. {{
  1295. "temp": 25.5,
  1296. }, {
  1297. "temp": 27.5,
  1298. }}, {{
  1299. "temp": 28.1,
  1300. }, {
  1301. "temp": 27.4,
  1302. }, {
  1303. "temp": 25.5,
  1304. }}, {{
  1305. "temp": 26.2,
  1306. }, {
  1307. "temp": 26.8,
  1308. }, {
  1309. "temp": 28.9,
  1310. }, {
  1311. "temp": 29.1,
  1312. }, {
  1313. "temp": 32.2,
  1314. }},
  1315. },
  1316. m: map[string]interface{}{
  1317. "op_preprocessor_sessionDemo_0_exceptions_total": int64(0),
  1318. "op_preprocessor_sessionDemo_0_process_latency_ms": int64(0),
  1319. "op_preprocessor_sessionDemo_0_records_in_total": int64(11),
  1320. "op_preprocessor_sessionDemo_0_records_out_total": int64(11),
  1321. "op_project_0_exceptions_total": int64(0),
  1322. "op_project_0_process_latency_ms": int64(0),
  1323. "op_project_0_records_in_total": int64(3),
  1324. "op_project_0_records_out_total": int64(3),
  1325. "sink_mockSink_0_exceptions_total": int64(0),
  1326. "sink_mockSink_0_records_in_total": int64(3),
  1327. "sink_mockSink_0_records_out_total": int64(3),
  1328. "source_sessionDemo_0_exceptions_total": int64(0),
  1329. "source_sessionDemo_0_records_in_total": int64(11),
  1330. "source_sessionDemo_0_records_out_total": int64(11),
  1331. "op_window_0_exceptions_total": int64(0),
  1332. "op_window_0_process_latency_ms": int64(0),
  1333. "op_window_0_records_in_total": int64(11),
  1334. "op_window_0_records_out_total": int64(3),
  1335. },
  1336. }, {
  1337. name: `rule6`,
  1338. 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)`,
  1339. size: 5,
  1340. r: [][]map[string]interface{}{
  1341. {{
  1342. "m": 25.5,
  1343. "c": float64(1),
  1344. }}, {{
  1345. "m": 25.5,
  1346. "c": float64(1),
  1347. }}, {{
  1348. "m": 25.5,
  1349. "c": float64(1),
  1350. }}, {{
  1351. "m": 28.1,
  1352. "c": float64(1),
  1353. }}, {{
  1354. "m": 28.1,
  1355. "c": float64(1),
  1356. }}, {{
  1357. "m": 28.1,
  1358. "c": float64(2),
  1359. }}, {{
  1360. "m": 27.4,
  1361. "c": float64(1),
  1362. }}, {{
  1363. "m": 27.4,
  1364. "c": float64(2),
  1365. }},
  1366. },
  1367. m: map[string]interface{}{
  1368. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1369. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1370. "op_preprocessor_demo_0_records_in_total": int64(5),
  1371. "op_preprocessor_demo_0_records_out_total": int64(5),
  1372. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1373. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1374. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1375. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1376. "op_project_0_exceptions_total": int64(0),
  1377. "op_project_0_process_latency_ms": int64(0),
  1378. "op_project_0_records_in_total": int64(8),
  1379. "op_project_0_records_out_total": int64(8),
  1380. "sink_mockSink_0_exceptions_total": int64(0),
  1381. "sink_mockSink_0_records_in_total": int64(8),
  1382. "sink_mockSink_0_records_out_total": int64(8),
  1383. "source_demo_0_exceptions_total": int64(0),
  1384. "source_demo_0_records_in_total": int64(5),
  1385. "source_demo_0_records_out_total": int64(5),
  1386. "source_demo1_0_exceptions_total": int64(0),
  1387. "source_demo1_0_records_in_total": int64(5),
  1388. "source_demo1_0_records_out_total": int64(5),
  1389. "op_window_0_exceptions_total": int64(0),
  1390. "op_window_0_process_latency_ms": int64(0),
  1391. "op_window_0_records_in_total": int64(10),
  1392. "op_window_0_records_out_total": int64(10),
  1393. "op_join_0_exceptions_total": int64(0),
  1394. "op_join_0_process_latency_ms": int64(0),
  1395. "op_join_0_records_in_total": int64(10),
  1396. "op_join_0_records_out_total": int64(8),
  1397. },
  1398. },
  1399. }
  1400. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1401. createStreams(t)
  1402. defer dropStreams(t)
  1403. for i, tt := range tests {
  1404. test.ResetClock(1541152486000)
  1405. p := NewRuleProcessor(DbDir)
  1406. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1407. var (
  1408. sources []*nodes.SourceNode
  1409. syncs []chan int
  1410. )
  1411. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1412. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1413. } else {
  1414. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1415. t.Errorf("sql %s is not a select statement", tt.sql)
  1416. } else {
  1417. streams := xsql.GetStreams(selectStmt)
  1418. for _, stream := range streams {
  1419. next := make(chan int)
  1420. syncs = append(syncs, next)
  1421. source := getMockSource(stream, next, tt.size)
  1422. sources = append(sources, source)
  1423. }
  1424. }
  1425. }
  1426. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  1427. if err != nil {
  1428. t.Error(err)
  1429. }
  1430. mockSink := test.NewMockSink()
  1431. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  1432. tp.AddSink(inputs, sink)
  1433. errCh := tp.Open()
  1434. func() {
  1435. for i := 0; i < tt.size*len(syncs); i++ {
  1436. syncs[i%len(syncs)] <- i
  1437. for {
  1438. time.Sleep(1)
  1439. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1440. break
  1441. }
  1442. }
  1443. select {
  1444. case err = <-errCh:
  1445. t.Log(err)
  1446. tp.Cancel()
  1447. return
  1448. default:
  1449. }
  1450. }
  1451. retry := 100
  1452. for ; retry > 0; retry-- {
  1453. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1454. break
  1455. }
  1456. t.Logf("wait to try another %d times", retry)
  1457. time.Sleep(time.Duration(retry) * time.Millisecond)
  1458. }
  1459. if retry == 0 {
  1460. err := compareMetrics(tp, tt.m, tt.sql)
  1461. t.Errorf("could not get correct metrics: %v", err)
  1462. }
  1463. }()
  1464. results := mockSink.GetResults()
  1465. var maps [][]map[string]interface{}
  1466. for _, v := range results {
  1467. var mapRes []map[string]interface{}
  1468. err := json.Unmarshal(v, &mapRes)
  1469. if err != nil {
  1470. t.Errorf("Failed to parse the input into map")
  1471. continue
  1472. }
  1473. maps = append(maps, mapRes)
  1474. }
  1475. if !reflect.DeepEqual(tt.r, maps) {
  1476. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1477. }
  1478. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1479. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1480. }
  1481. tp.Cancel()
  1482. }
  1483. }
  1484. func createEventStreams(t *testing.T) {
  1485. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  1486. demo := `CREATE STREAM demoE (
  1487. color STRING,
  1488. size BIGINT,
  1489. ts BIGINT
  1490. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1491. _, err := p.ExecStmt(demo)
  1492. if err != nil {
  1493. t.Log(err)
  1494. }
  1495. demo1 := `CREATE STREAM demo1E (
  1496. temp FLOAT,
  1497. hum BIGINT,
  1498. ts BIGINT
  1499. ) WITH (DATASOURCE="demo1E", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1500. _, err = p.ExecStmt(demo1)
  1501. if err != nil {
  1502. t.Log(err)
  1503. }
  1504. sessionDemo := `CREATE STREAM sessionDemoE (
  1505. temp FLOAT,
  1506. hum BIGINT,
  1507. ts BIGINT
  1508. ) WITH (DATASOURCE="sessionDemoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1509. _, err = p.ExecStmt(sessionDemo)
  1510. if err != nil {
  1511. t.Log(err)
  1512. }
  1513. }
  1514. func dropEventStreams(t *testing.T) {
  1515. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  1516. demo := `DROP STREAM demoE`
  1517. _, err := p.ExecStmt(demo)
  1518. if err != nil {
  1519. t.Log(err)
  1520. }
  1521. demo1 := `DROP STREAM demo1E`
  1522. _, err = p.ExecStmt(demo1)
  1523. if err != nil {
  1524. t.Log(err)
  1525. }
  1526. sessionDemo := `DROP STREAM sessionDemoE`
  1527. _, err = p.ExecStmt(sessionDemo)
  1528. if err != nil {
  1529. t.Log(err)
  1530. }
  1531. }
  1532. func getEventMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  1533. var data []*xsql.Tuple
  1534. switch name {
  1535. case "demoE":
  1536. data = []*xsql.Tuple{
  1537. {
  1538. Emitter: name,
  1539. Message: map[string]interface{}{
  1540. "color": "red",
  1541. "size": 3,
  1542. "ts": 1541152486013,
  1543. },
  1544. Timestamp: 1541152486013,
  1545. },
  1546. {
  1547. Emitter: name,
  1548. Message: map[string]interface{}{
  1549. "color": "blue",
  1550. "size": 2,
  1551. "ts": 1541152487632,
  1552. },
  1553. Timestamp: 1541152487632,
  1554. },
  1555. {
  1556. Emitter: name,
  1557. Message: map[string]interface{}{
  1558. "color": "red",
  1559. "size": 1,
  1560. "ts": 1541152489252,
  1561. },
  1562. Timestamp: 1541152489252,
  1563. },
  1564. { //dropped item
  1565. Emitter: name,
  1566. Message: map[string]interface{}{
  1567. "color": "blue",
  1568. "size": 6,
  1569. "ts": 1541152486822,
  1570. },
  1571. Timestamp: 1541152486822,
  1572. },
  1573. {
  1574. Emitter: name,
  1575. Message: map[string]interface{}{
  1576. "color": "yellow",
  1577. "size": 4,
  1578. "ts": 1541152488442,
  1579. },
  1580. Timestamp: 1541152488442,
  1581. },
  1582. { //To lift the watermark and issue all windows
  1583. Emitter: name,
  1584. Message: map[string]interface{}{
  1585. "color": "yellow",
  1586. "size": 4,
  1587. "ts": 1541152492342,
  1588. },
  1589. Timestamp: 1541152488442,
  1590. },
  1591. }
  1592. case "demo1E":
  1593. data = []*xsql.Tuple{
  1594. {
  1595. Emitter: name,
  1596. Message: map[string]interface{}{
  1597. "temp": 27.5,
  1598. "hum": 59,
  1599. "ts": 1541152486823,
  1600. },
  1601. Timestamp: 1541152486823,
  1602. },
  1603. {
  1604. Emitter: name,
  1605. Message: map[string]interface{}{
  1606. "temp": 25.5,
  1607. "hum": 65,
  1608. "ts": 1541152486013,
  1609. },
  1610. Timestamp: 1541152486013,
  1611. },
  1612. {
  1613. Emitter: name,
  1614. Message: map[string]interface{}{
  1615. "temp": 27.4,
  1616. "hum": 80,
  1617. "ts": 1541152488442,
  1618. },
  1619. Timestamp: 1541152488442,
  1620. },
  1621. {
  1622. Emitter: name,
  1623. Message: map[string]interface{}{
  1624. "temp": 28.1,
  1625. "hum": 75,
  1626. "ts": 1541152487632,
  1627. },
  1628. Timestamp: 1541152487632,
  1629. },
  1630. {
  1631. Emitter: name,
  1632. Message: map[string]interface{}{
  1633. "temp": 25.5,
  1634. "hum": 62,
  1635. "ts": 1541152489252,
  1636. },
  1637. Timestamp: 1541152489252,
  1638. },
  1639. {
  1640. Emitter: name,
  1641. Message: map[string]interface{}{
  1642. "temp": 25.5,
  1643. "hum": 62,
  1644. "ts": 1541152499252,
  1645. },
  1646. Timestamp: 1541152499252,
  1647. },
  1648. }
  1649. case "sessionDemoE":
  1650. data = []*xsql.Tuple{
  1651. {
  1652. Emitter: name,
  1653. Message: map[string]interface{}{
  1654. "temp": 25.5,
  1655. "hum": 65,
  1656. "ts": 1541152486013,
  1657. },
  1658. Timestamp: 1541152486013,
  1659. },
  1660. {
  1661. Emitter: name,
  1662. Message: map[string]interface{}{
  1663. "temp": 28.1,
  1664. "hum": 75,
  1665. "ts": 1541152487932,
  1666. },
  1667. Timestamp: 1541152487932,
  1668. },
  1669. {
  1670. Emitter: name,
  1671. Message: map[string]interface{}{
  1672. "temp": 27.5,
  1673. "hum": 59,
  1674. "ts": 1541152486823,
  1675. },
  1676. Timestamp: 1541152486823,
  1677. },
  1678. {
  1679. Emitter: name,
  1680. Message: map[string]interface{}{
  1681. "temp": 25.5,
  1682. "hum": 62,
  1683. "ts": 1541152489252,
  1684. },
  1685. Timestamp: 1541152489252,
  1686. },
  1687. {
  1688. Emitter: name,
  1689. Message: map[string]interface{}{
  1690. "temp": 27.4,
  1691. "hum": 80,
  1692. "ts": 1541152488442,
  1693. },
  1694. Timestamp: 1541152488442,
  1695. },
  1696. {
  1697. Emitter: name,
  1698. Message: map[string]interface{}{
  1699. "temp": 26.2,
  1700. "hum": 63,
  1701. "ts": 1541152490062,
  1702. },
  1703. Timestamp: 1541152490062,
  1704. },
  1705. {
  1706. Emitter: name,
  1707. Message: map[string]interface{}{
  1708. "temp": 28.9,
  1709. "hum": 85,
  1710. "ts": 1541152491682,
  1711. },
  1712. Timestamp: 1541152491682,
  1713. },
  1714. {
  1715. Emitter: name,
  1716. Message: map[string]interface{}{
  1717. "temp": 26.8,
  1718. "hum": 71,
  1719. "ts": 1541152490872,
  1720. },
  1721. Timestamp: 1541152490872,
  1722. },
  1723. {
  1724. Emitter: name,
  1725. Message: map[string]interface{}{
  1726. "temp": 29.1,
  1727. "hum": 92,
  1728. "ts": 1541152492492,
  1729. },
  1730. Timestamp: 1541152492492,
  1731. },
  1732. {
  1733. Emitter: name,
  1734. Message: map[string]interface{}{
  1735. "temp": 30.9,
  1736. "hum": 87,
  1737. "ts": 1541152494112,
  1738. },
  1739. Timestamp: 1541152494112,
  1740. },
  1741. {
  1742. Emitter: name,
  1743. Message: map[string]interface{}{
  1744. "temp": 32.2,
  1745. "hum": 99,
  1746. "ts": 1541152493202,
  1747. },
  1748. Timestamp: 1541152493202,
  1749. },
  1750. {
  1751. Emitter: name,
  1752. Message: map[string]interface{}{
  1753. "temp": 32.2,
  1754. "hum": 99,
  1755. "ts": 1541152499202,
  1756. },
  1757. Timestamp: 1541152499202,
  1758. },
  1759. }
  1760. }
  1761. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, true), map[string]string{
  1762. "DATASOURCE": name,
  1763. })
  1764. }
  1765. func TestEventWindow(t *testing.T) {
  1766. var tests = []struct {
  1767. name string
  1768. sql string
  1769. size int
  1770. r [][]map[string]interface{}
  1771. m map[string]interface{}
  1772. }{
  1773. {
  1774. name: `rule1`,
  1775. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1776. size: 6,
  1777. r: [][]map[string]interface{}{
  1778. {{
  1779. "color": "red",
  1780. "size": float64(3),
  1781. "ts": float64(1541152486013),
  1782. }},
  1783. {{
  1784. "color": "red",
  1785. "size": float64(3),
  1786. "ts": float64(1541152486013),
  1787. }, {
  1788. "color": "blue",
  1789. "size": float64(2),
  1790. "ts": float64(1541152487632),
  1791. }},
  1792. {{
  1793. "color": "blue",
  1794. "size": float64(2),
  1795. "ts": float64(1541152487632),
  1796. }, {
  1797. "color": "yellow",
  1798. "size": float64(4),
  1799. "ts": float64(1541152488442),
  1800. }}, {{
  1801. "color": "yellow",
  1802. "size": float64(4),
  1803. "ts": float64(1541152488442),
  1804. }, {
  1805. "color": "red",
  1806. "size": float64(1),
  1807. "ts": float64(1541152489252),
  1808. }}, {{
  1809. "color": "red",
  1810. "size": float64(1),
  1811. "ts": float64(1541152489252),
  1812. }},
  1813. },
  1814. m: map[string]interface{}{
  1815. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1816. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1817. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1818. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1819. "op_project_0_exceptions_total": int64(0),
  1820. "op_project_0_process_latency_ms": int64(0),
  1821. "op_project_0_records_in_total": int64(5),
  1822. "op_project_0_records_out_total": int64(5),
  1823. "sink_mockSink_0_exceptions_total": int64(0),
  1824. "sink_mockSink_0_records_in_total": int64(5),
  1825. "sink_mockSink_0_records_out_total": int64(5),
  1826. "source_demoE_0_exceptions_total": int64(0),
  1827. "source_demoE_0_records_in_total": int64(6),
  1828. "source_demoE_0_records_out_total": int64(6),
  1829. "op_window_0_exceptions_total": int64(0),
  1830. "op_window_0_process_latency_ms": int64(0),
  1831. "op_window_0_records_in_total": int64(6),
  1832. "op_window_0_records_out_total": int64(5),
  1833. },
  1834. }, {
  1835. name: `rule2`,
  1836. sql: `SELECT color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1837. size: 6,
  1838. r: [][]map[string]interface{}{
  1839. {{
  1840. "color": "red",
  1841. "ts": float64(1541152486013),
  1842. }},
  1843. {{
  1844. "color": "yellow",
  1845. "ts": float64(1541152488442),
  1846. }},
  1847. },
  1848. m: map[string]interface{}{
  1849. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1850. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1851. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1852. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1853. "op_project_0_exceptions_total": int64(0),
  1854. "op_project_0_process_latency_ms": int64(0),
  1855. "op_project_0_records_in_total": int64(2),
  1856. "op_project_0_records_out_total": int64(2),
  1857. "sink_mockSink_0_exceptions_total": int64(0),
  1858. "sink_mockSink_0_records_in_total": int64(2),
  1859. "sink_mockSink_0_records_out_total": int64(2),
  1860. "source_demoE_0_exceptions_total": int64(0),
  1861. "source_demoE_0_records_in_total": int64(6),
  1862. "source_demoE_0_records_out_total": int64(6),
  1863. "op_window_0_exceptions_total": int64(0),
  1864. "op_window_0_process_latency_ms": int64(0),
  1865. "op_window_0_records_in_total": int64(6),
  1866. "op_window_0_records_out_total": int64(4),
  1867. "op_filter_0_exceptions_total": int64(0),
  1868. "op_filter_0_process_latency_ms": int64(0),
  1869. "op_filter_0_records_in_total": int64(4),
  1870. "op_filter_0_records_out_total": int64(2),
  1871. },
  1872. }, {
  1873. name: `rule3`,
  1874. sql: `SELECT color, temp, ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  1875. size: 6,
  1876. r: [][]map[string]interface{}{
  1877. {{
  1878. "color": "red",
  1879. "temp": 25.5,
  1880. "ts": float64(1541152486013),
  1881. }}, {{
  1882. "color": "red",
  1883. "temp": 25.5,
  1884. "ts": float64(1541152486013),
  1885. }}, {{
  1886. "color": "blue",
  1887. "temp": 28.1,
  1888. "ts": float64(1541152487632),
  1889. }}, {{
  1890. "color": "blue",
  1891. "temp": 28.1,
  1892. "ts": float64(1541152487632),
  1893. }, {
  1894. "color": "yellow",
  1895. "temp": 27.4,
  1896. "ts": float64(1541152488442),
  1897. }}, {{
  1898. "color": "yellow",
  1899. "temp": 27.4,
  1900. "ts": float64(1541152488442),
  1901. }, {
  1902. "color": "red",
  1903. "temp": 25.5,
  1904. "ts": float64(1541152489252),
  1905. }},
  1906. },
  1907. m: map[string]interface{}{
  1908. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1909. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1910. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1911. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1912. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  1913. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  1914. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  1915. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  1916. "op_project_0_exceptions_total": int64(0),
  1917. "op_project_0_process_latency_ms": int64(0),
  1918. "op_project_0_records_in_total": int64(5),
  1919. "op_project_0_records_out_total": int64(5),
  1920. "sink_mockSink_0_exceptions_total": int64(0),
  1921. "sink_mockSink_0_records_in_total": int64(5),
  1922. "sink_mockSink_0_records_out_total": int64(5),
  1923. "source_demoE_0_exceptions_total": int64(0),
  1924. "source_demoE_0_records_in_total": int64(6),
  1925. "source_demoE_0_records_out_total": int64(6),
  1926. "source_demo1E_0_exceptions_total": int64(0),
  1927. "source_demo1E_0_records_in_total": int64(6),
  1928. "source_demo1E_0_records_out_total": int64(6),
  1929. "op_window_0_exceptions_total": int64(0),
  1930. "op_window_0_process_latency_ms": int64(0),
  1931. "op_window_0_records_in_total": int64(12),
  1932. "op_window_0_records_out_total": int64(5),
  1933. "op_join_0_exceptions_total": int64(0),
  1934. "op_join_0_process_latency_ms": int64(0),
  1935. "op_join_0_records_in_total": int64(5),
  1936. "op_join_0_records_out_total": int64(5),
  1937. },
  1938. }, {
  1939. name: `rule4`,
  1940. sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  1941. size: 6,
  1942. r: [][]map[string]interface{}{
  1943. {{
  1944. "color": "red",
  1945. }}, {{
  1946. "color": "blue",
  1947. }, {
  1948. "color": "red",
  1949. }}, {{
  1950. "color": "blue",
  1951. }, {
  1952. "color": "yellow",
  1953. }}, {{
  1954. "color": "blue",
  1955. }, {
  1956. "color": "red",
  1957. }, {
  1958. "color": "yellow",
  1959. }},
  1960. },
  1961. m: map[string]interface{}{
  1962. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  1963. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1964. "op_preprocessor_demoE_0_records_in_total": int64(6),
  1965. "op_preprocessor_demoE_0_records_out_total": int64(6),
  1966. "op_project_0_exceptions_total": int64(0),
  1967. "op_project_0_process_latency_ms": int64(0),
  1968. "op_project_0_records_in_total": int64(4),
  1969. "op_project_0_records_out_total": int64(4),
  1970. "sink_mockSink_0_exceptions_total": int64(0),
  1971. "sink_mockSink_0_records_in_total": int64(4),
  1972. "sink_mockSink_0_records_out_total": int64(4),
  1973. "source_demoE_0_exceptions_total": int64(0),
  1974. "source_demoE_0_records_in_total": int64(6),
  1975. "source_demoE_0_records_out_total": int64(6),
  1976. "op_window_0_exceptions_total": int64(0),
  1977. "op_window_0_process_latency_ms": int64(0),
  1978. "op_window_0_records_in_total": int64(6),
  1979. "op_window_0_records_out_total": int64(4),
  1980. "op_aggregate_0_exceptions_total": int64(0),
  1981. "op_aggregate_0_process_latency_ms": int64(0),
  1982. "op_aggregate_0_records_in_total": int64(4),
  1983. "op_aggregate_0_records_out_total": int64(4),
  1984. "op_order_0_exceptions_total": int64(0),
  1985. "op_order_0_process_latency_ms": int64(0),
  1986. "op_order_0_records_in_total": int64(4),
  1987. "op_order_0_records_out_total": int64(4),
  1988. },
  1989. }, {
  1990. name: `rule5`,
  1991. sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  1992. size: 12,
  1993. r: [][]map[string]interface{}{
  1994. {{
  1995. "temp": 25.5,
  1996. }}, {{
  1997. "temp": 28.1,
  1998. }, {
  1999. "temp": 27.4,
  2000. }, {
  2001. "temp": 25.5,
  2002. }}, {{
  2003. "temp": 26.2,
  2004. }, {
  2005. "temp": 26.8,
  2006. }, {
  2007. "temp": 28.9,
  2008. }, {
  2009. "temp": 29.1,
  2010. }, {
  2011. "temp": 32.2,
  2012. }}, {{
  2013. "temp": 30.9,
  2014. }},
  2015. },
  2016. m: map[string]interface{}{
  2017. "op_preprocessor_sessionDemoE_0_exceptions_total": int64(0),
  2018. "op_preprocessor_sessionDemoE_0_process_latency_ms": int64(0),
  2019. "op_preprocessor_sessionDemoE_0_records_in_total": int64(12),
  2020. "op_preprocessor_sessionDemoE_0_records_out_total": int64(12),
  2021. "op_project_0_exceptions_total": int64(0),
  2022. "op_project_0_process_latency_ms": int64(0),
  2023. "op_project_0_records_in_total": int64(4),
  2024. "op_project_0_records_out_total": int64(4),
  2025. "sink_mockSink_0_exceptions_total": int64(0),
  2026. "sink_mockSink_0_records_in_total": int64(4),
  2027. "sink_mockSink_0_records_out_total": int64(4),
  2028. "source_sessionDemoE_0_exceptions_total": int64(0),
  2029. "source_sessionDemoE_0_records_in_total": int64(12),
  2030. "source_sessionDemoE_0_records_out_total": int64(12),
  2031. "op_window_0_exceptions_total": int64(0),
  2032. "op_window_0_process_latency_ms": int64(0),
  2033. "op_window_0_records_in_total": int64(12),
  2034. "op_window_0_records_out_total": int64(4),
  2035. },
  2036. }, {
  2037. name: `rule6`,
  2038. 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)`,
  2039. size: 6,
  2040. r: [][]map[string]interface{}{
  2041. {{
  2042. "m": 25.5,
  2043. "c": float64(1),
  2044. }}, {{
  2045. "m": 25.5,
  2046. "c": float64(1),
  2047. }}, {{
  2048. "m": 28.1,
  2049. "c": float64(1),
  2050. }}, {{
  2051. "m": 28.1,
  2052. "c": float64(2),
  2053. }}, {{
  2054. "m": 27.4,
  2055. "c": float64(2),
  2056. }},
  2057. },
  2058. m: map[string]interface{}{
  2059. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2060. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2061. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2062. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2063. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2064. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2065. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2066. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2067. "op_project_0_exceptions_total": int64(0),
  2068. "op_project_0_process_latency_ms": int64(0),
  2069. "op_project_0_records_in_total": int64(5),
  2070. "op_project_0_records_out_total": int64(5),
  2071. "sink_mockSink_0_exceptions_total": int64(0),
  2072. "sink_mockSink_0_records_in_total": int64(5),
  2073. "sink_mockSink_0_records_out_total": int64(5),
  2074. "source_demoE_0_exceptions_total": int64(0),
  2075. "source_demoE_0_records_in_total": int64(6),
  2076. "source_demoE_0_records_out_total": int64(6),
  2077. "source_demo1E_0_exceptions_total": int64(0),
  2078. "source_demo1E_0_records_in_total": int64(6),
  2079. "source_demo1E_0_records_out_total": int64(6),
  2080. "op_window_0_exceptions_total": int64(0),
  2081. "op_window_0_records_in_total": int64(12),
  2082. "op_window_0_records_out_total": int64(5),
  2083. "op_join_0_exceptions_total": int64(0),
  2084. "op_join_0_process_latency_ms": int64(0),
  2085. "op_join_0_records_in_total": int64(5),
  2086. "op_join_0_records_out_total": int64(5),
  2087. },
  2088. },
  2089. }
  2090. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2091. createEventStreams(t)
  2092. defer dropEventStreams(t)
  2093. for i, tt := range tests {
  2094. test.ResetClock(1541152486000)
  2095. p := NewRuleProcessor(DbDir)
  2096. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2097. var (
  2098. sources []*nodes.SourceNode
  2099. syncs []chan int
  2100. )
  2101. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2102. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2103. } else {
  2104. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2105. t.Errorf("sql %s is not a select statement", tt.sql)
  2106. } else {
  2107. streams := xsql.GetStreams(selectStmt)
  2108. for _, stream := range streams {
  2109. next := make(chan int)
  2110. syncs = append(syncs, next)
  2111. source := getEventMockSource(stream, next, tt.size)
  2112. sources = append(sources, source)
  2113. }
  2114. }
  2115. }
  2116. tp, inputs, err := p.createTopoWithSources(&api.Rule{
  2117. Id: tt.name, Sql: tt.sql,
  2118. Options: map[string]interface{}{
  2119. "isEventTime": true,
  2120. "lateTolerance": float64(1000),
  2121. },
  2122. }, sources)
  2123. if err != nil {
  2124. t.Error(err)
  2125. }
  2126. mockSink := test.NewMockSink()
  2127. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  2128. tp.AddSink(inputs, sink)
  2129. errCh := tp.Open()
  2130. func() {
  2131. for i := 0; i < tt.size*len(syncs); i++ {
  2132. syncs[i%len(syncs)] <- i
  2133. for {
  2134. time.Sleep(1)
  2135. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  2136. break
  2137. }
  2138. }
  2139. select {
  2140. case err = <-errCh:
  2141. t.Log(err)
  2142. tp.Cancel()
  2143. return
  2144. default:
  2145. }
  2146. }
  2147. mockClock := test.GetMockClock()
  2148. mockClock.Add(1000 * time.Millisecond)
  2149. retry := 100
  2150. for ; retry > 0; retry-- {
  2151. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2152. break
  2153. }
  2154. t.Logf("wait to try another %d times", retry)
  2155. time.Sleep(time.Duration(retry) * time.Millisecond)
  2156. }
  2157. if retry == 0 {
  2158. err := compareMetrics(tp, tt.m, tt.sql)
  2159. t.Errorf("could not get correct metrics: %v", err)
  2160. }
  2161. }()
  2162. results := mockSink.GetResults()
  2163. var maps [][]map[string]interface{}
  2164. for _, v := range results {
  2165. var mapRes []map[string]interface{}
  2166. err := json.Unmarshal(v, &mapRes)
  2167. if err != nil {
  2168. t.Errorf("Failed to parse the input into map")
  2169. continue
  2170. }
  2171. maps = append(maps, mapRes)
  2172. }
  2173. if !reflect.DeepEqual(tt.r, maps) {
  2174. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  2175. }
  2176. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  2177. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  2178. }
  2179. tp.Cancel()
  2180. }
  2181. }
  2182. func getMetric(tp *xstream.TopologyNew, name string) int {
  2183. keys, values := tp.GetMetrics()
  2184. for index, key := range keys {
  2185. if key == name {
  2186. return int(values[index].(int64))
  2187. }
  2188. }
  2189. fmt.Println("can't find " + name)
  2190. return 0
  2191. }
  2192. func compareMetrics(tp *xstream.TopologyNew, m map[string]interface{}, sql string) (err error) {
  2193. keys, values := tp.GetMetrics()
  2194. //for i, k := range keys{
  2195. // log.Printf("%s:%v", k, values[i])
  2196. //}
  2197. for k, v := range m {
  2198. var (
  2199. index int
  2200. key string
  2201. matched bool
  2202. )
  2203. for index, key = range keys {
  2204. if k == key {
  2205. if strings.HasSuffix(k, "process_latency_ms") {
  2206. if values[index].(int64) >= v.(int64) {
  2207. matched = true
  2208. continue
  2209. } else {
  2210. break
  2211. }
  2212. }
  2213. if values[index] == v {
  2214. matched = true
  2215. }
  2216. break
  2217. }
  2218. }
  2219. if matched {
  2220. continue
  2221. }
  2222. //do not find
  2223. if index < len(values) {
  2224. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%t)\n\ngot=%#v(%t)\n\n", k, v, v, values[index], values[index])
  2225. } else {
  2226. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  2227. }
  2228. }
  2229. return nil
  2230. }
  2231. func errstring(err error) string {
  2232. if err != nil {
  2233. return err.Error()
  2234. }
  2235. return ""
  2236. }