xsql_processor_test.go 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834
  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": 3,
  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 "ldemo1":
  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 "lsessionDemo":
  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. name: `rule7`,
  1400. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1401. size: 5,
  1402. r: [][]map[string]interface{}{
  1403. {{
  1404. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  1405. }},
  1406. {{
  1407. "color": "blue",
  1408. "size": float64(6),
  1409. "ts": float64(1541152486822),
  1410. }},
  1411. {{
  1412. "color": "blue",
  1413. "size": float64(6),
  1414. "ts": float64(1541152486822),
  1415. }, {
  1416. "color": "blue",
  1417. "size": float64(2),
  1418. "ts": float64(1541152487632),
  1419. }},
  1420. {{
  1421. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  1422. }},
  1423. {{
  1424. "color": "blue",
  1425. "size": float64(2),
  1426. "ts": float64(1541152487632),
  1427. }},
  1428. {{
  1429. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  1430. }},
  1431. },
  1432. m: map[string]interface{}{
  1433. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  1434. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1435. "op_preprocessor_demoE_0_records_in_total": int64(5),
  1436. "op_preprocessor_demoE_0_records_out_total": int64(2),
  1437. "op_project_0_exceptions_total": int64(3),
  1438. "op_project_0_process_latency_ms": int64(0),
  1439. "op_project_0_records_in_total": int64(6),
  1440. "op_project_0_records_out_total": int64(3),
  1441. "sink_mockSink_0_exceptions_total": int64(0),
  1442. "sink_mockSink_0_records_in_total": int64(6),
  1443. "sink_mockSink_0_records_out_total": int64(6),
  1444. "source_demoE_0_exceptions_total": int64(0),
  1445. "source_demoE_0_records_in_total": int64(5),
  1446. "source_demoE_0_records_out_total": int64(5),
  1447. "op_window_0_exceptions_total": int64(3),
  1448. "op_window_0_process_latency_ms": int64(0),
  1449. "op_window_0_records_in_total": int64(5),
  1450. "op_window_0_records_out_total": int64(3),
  1451. },
  1452. },
  1453. }
  1454. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1455. createStreams(t)
  1456. defer dropStreams(t)
  1457. for i, tt := range tests {
  1458. test.ResetClock(1541152486000)
  1459. p := NewRuleProcessor(DbDir)
  1460. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1461. var (
  1462. sources []*nodes.SourceNode
  1463. syncs []chan int
  1464. )
  1465. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1466. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1467. } else {
  1468. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1469. t.Errorf("sql %s is not a select statement", tt.sql)
  1470. } else {
  1471. streams := xsql.GetStreams(selectStmt)
  1472. for _, stream := range streams {
  1473. next := make(chan int)
  1474. syncs = append(syncs, next)
  1475. source := getMockSource(stream, next, tt.size)
  1476. sources = append(sources, source)
  1477. }
  1478. }
  1479. }
  1480. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  1481. if err != nil {
  1482. t.Error(err)
  1483. }
  1484. mockSink := test.NewMockSink()
  1485. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  1486. tp.AddSink(inputs, sink)
  1487. errCh := tp.Open()
  1488. func() {
  1489. for i := 0; i < tt.size*len(syncs); i++ {
  1490. syncs[i%len(syncs)] <- i
  1491. for {
  1492. time.Sleep(1)
  1493. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1494. break
  1495. }
  1496. }
  1497. select {
  1498. case err = <-errCh:
  1499. t.Log(err)
  1500. tp.Cancel()
  1501. return
  1502. default:
  1503. }
  1504. }
  1505. retry := 100
  1506. for ; retry > 0; retry-- {
  1507. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1508. break
  1509. }
  1510. t.Logf("wait to try another %d times", retry)
  1511. time.Sleep(time.Duration(retry) * time.Millisecond)
  1512. }
  1513. if retry == 0 {
  1514. err := compareMetrics(tp, tt.m, tt.sql)
  1515. t.Errorf("could not get correct metrics: %v", err)
  1516. }
  1517. }()
  1518. results := mockSink.GetResults()
  1519. var maps [][]map[string]interface{}
  1520. for _, v := range results {
  1521. var mapRes []map[string]interface{}
  1522. err := json.Unmarshal(v, &mapRes)
  1523. if err != nil {
  1524. t.Errorf("Failed to parse the input into map")
  1525. continue
  1526. }
  1527. maps = append(maps, mapRes)
  1528. }
  1529. if !reflect.DeepEqual(tt.r, maps) {
  1530. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1531. }
  1532. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1533. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1534. }
  1535. tp.Cancel()
  1536. }
  1537. }
  1538. func TestWindowError(t *testing.T) {
  1539. var tests = []struct {
  1540. name string
  1541. sql string
  1542. size int
  1543. r [][]map[string]interface{}
  1544. m map[string]interface{}
  1545. }{
  1546. {
  1547. name: `rule1`,
  1548. sql: `SELECT size * 3 FROM ldemo GROUP BY TUMBLINGWINDOW(ss, 2)`,
  1549. size: 5,
  1550. r: [][]map[string]interface{}{
  1551. {{
  1552. "error": "run Select error: invalid operation string(string) * int64(3)",
  1553. }},
  1554. },
  1555. m: map[string]interface{}{
  1556. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1557. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1558. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1559. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1560. "op_project_0_exceptions_total": int64(1),
  1561. "op_project_0_process_latency_ms": int64(0),
  1562. "op_project_0_records_in_total": int64(1),
  1563. "op_project_0_records_out_total": int64(0),
  1564. "sink_mockSink_0_exceptions_total": int64(0),
  1565. "sink_mockSink_0_records_in_total": int64(1),
  1566. "sink_mockSink_0_records_out_total": int64(1),
  1567. "source_ldemo_0_exceptions_total": int64(0),
  1568. "source_ldemo_0_records_in_total": int64(5),
  1569. "source_ldemo_0_records_out_total": int64(5),
  1570. "op_window_0_exceptions_total": int64(0),
  1571. "op_window_0_process_latency_ms": int64(0),
  1572. "op_window_0_records_in_total": int64(5),
  1573. "op_window_0_records_out_total": int64(1),
  1574. },
  1575. }, {
  1576. name: `rule2`,
  1577. sql: `SELECT color, ts FROM ldemo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1578. size: 5,
  1579. r: [][]map[string]interface{}{
  1580. {{
  1581. "error": "run Where error: invalid operation string(string) > int64(2)",
  1582. }}, {{
  1583. "ts": float64(1541152487632),
  1584. }},
  1585. },
  1586. m: map[string]interface{}{
  1587. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1588. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1589. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1590. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1591. "op_project_0_exceptions_total": int64(1),
  1592. "op_project_0_process_latency_ms": int64(0),
  1593. "op_project_0_records_in_total": int64(2),
  1594. "op_project_0_records_out_total": int64(1),
  1595. "sink_mockSink_0_exceptions_total": int64(0),
  1596. "sink_mockSink_0_records_in_total": int64(2),
  1597. "sink_mockSink_0_records_out_total": int64(2),
  1598. "source_ldemo_0_exceptions_total": int64(0),
  1599. "source_ldemo_0_records_in_total": int64(5),
  1600. "source_ldemo_0_records_out_total": int64(5),
  1601. "op_window_0_exceptions_total": int64(0),
  1602. "op_window_0_process_latency_ms": int64(0),
  1603. "op_window_0_records_in_total": int64(5),
  1604. "op_window_0_records_out_total": int64(3),
  1605. "op_filter_0_exceptions_total": int64(1),
  1606. "op_filter_0_process_latency_ms": int64(0),
  1607. "op_filter_0_records_in_total": int64(3),
  1608. "op_filter_0_records_out_total": int64(1),
  1609. },
  1610. }, {
  1611. name: `rule3`,
  1612. sql: `SELECT color, temp, ts FROM ldemo INNER JOIN ldemo1 ON ldemo.ts = ldemo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1613. size: 5,
  1614. r: [][]map[string]interface{}{
  1615. {{
  1616. "color": "red",
  1617. "temp": 25.5,
  1618. "ts": float64(1541152486013),
  1619. }}, {{
  1620. "color": "red",
  1621. "temp": 25.5,
  1622. "ts": float64(1541152486013),
  1623. }}, {{
  1624. "color": "red",
  1625. "temp": 25.5,
  1626. "ts": float64(1541152486013),
  1627. }}, {{
  1628. "temp": 28.1,
  1629. "ts": float64(1541152487632),
  1630. }}, {{
  1631. "temp": 28.1,
  1632. "ts": float64(1541152487632),
  1633. }}, {{
  1634. "error": "run Join error: invalid operation int64(1541152487632) = string(1541152488442)",
  1635. }}, {{
  1636. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1637. }}, {{
  1638. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  1639. }},
  1640. },
  1641. m: map[string]interface{}{
  1642. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1643. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1644. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1645. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1646. "op_preprocessor_ldemo1_0_exceptions_total": int64(0),
  1647. "op_preprocessor_ldemo1_0_process_latency_ms": int64(0),
  1648. "op_preprocessor_ldemo1_0_records_in_total": int64(5),
  1649. "op_preprocessor_ldemo1_0_records_out_total": int64(5),
  1650. "op_project_0_exceptions_total": int64(3),
  1651. "op_project_0_process_latency_ms": int64(0),
  1652. "op_project_0_records_in_total": int64(8),
  1653. "op_project_0_records_out_total": int64(5),
  1654. "sink_mockSink_0_exceptions_total": int64(0),
  1655. "sink_mockSink_0_records_in_total": int64(8),
  1656. "sink_mockSink_0_records_out_total": int64(8),
  1657. "source_ldemo_0_exceptions_total": int64(0),
  1658. "source_ldemo_0_records_in_total": int64(5),
  1659. "source_ldemo_0_records_out_total": int64(5),
  1660. "source_ldemo1_0_exceptions_total": int64(0),
  1661. "source_ldemo1_0_records_in_total": int64(5),
  1662. "source_ldemo1_0_records_out_total": int64(5),
  1663. "op_window_0_exceptions_total": int64(0),
  1664. "op_window_0_process_latency_ms": int64(0),
  1665. "op_window_0_records_in_total": int64(10),
  1666. "op_window_0_records_out_total": int64(10),
  1667. "op_join_0_exceptions_total": int64(3),
  1668. "op_join_0_process_latency_ms": int64(0),
  1669. "op_join_0_records_in_total": int64(10),
  1670. "op_join_0_records_out_total": int64(5),
  1671. },
  1672. //}, {
  1673. // name: `rule4`,
  1674. // sql: `SELECT color FROM ldemo GROUP BY SlidingWindow(ss, 2), color having color > 5`,
  1675. // size: 5,
  1676. // r: [][]map[string]interface{}{
  1677. // {{
  1678. // "color": "red",
  1679. // }}, {{
  1680. // "color": "blue",
  1681. // }, {
  1682. // "color": "red",
  1683. // }}, {{
  1684. // "color": "blue",
  1685. // }, {
  1686. // "color": "red",
  1687. // }}, {{
  1688. // "color": "blue",
  1689. // }, {
  1690. // "color": "yellow",
  1691. // }}, {{
  1692. // "color": "blue",
  1693. // }, {
  1694. // "color": "red",
  1695. // }, {
  1696. // "color": "yellow",
  1697. // }},
  1698. // },
  1699. // m: map[string]interface{}{
  1700. // "op_preprocessor_demo_0_exceptions_total": int64(0),
  1701. // "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1702. // "op_preprocessor_demo_0_records_in_total": int64(5),
  1703. // "op_preprocessor_demo_0_records_out_total": int64(5),
  1704. //
  1705. // "op_project_0_exceptions_total": int64(0),
  1706. // "op_project_0_process_latency_ms": int64(0),
  1707. // "op_project_0_records_in_total": int64(5),
  1708. // "op_project_0_records_out_total": int64(5),
  1709. //
  1710. // "sink_mockSink_0_exceptions_total": int64(0),
  1711. // "sink_mockSink_0_records_in_total": int64(5),
  1712. // "sink_mockSink_0_records_out_total": int64(5),
  1713. //
  1714. // "source_demo_0_exceptions_total": int64(0),
  1715. // "source_demo_0_records_in_total": int64(5),
  1716. // "source_demo_0_records_out_total": int64(5),
  1717. //
  1718. // "op_window_0_exceptions_total": int64(0),
  1719. // "op_window_0_process_latency_ms": int64(0),
  1720. // "op_window_0_records_in_total": int64(5),
  1721. // "op_window_0_records_out_total": int64(5),
  1722. //
  1723. // "op_aggregate_0_exceptions_total": int64(0),
  1724. // "op_aggregate_0_process_latency_ms": int64(0),
  1725. // "op_aggregate_0_records_in_total": int64(5),
  1726. // "op_aggregate_0_records_out_total": int64(5),
  1727. //
  1728. // "op_order_0_exceptions_total": int64(0),
  1729. // "op_order_0_process_latency_ms": int64(0),
  1730. // "op_order_0_records_in_total": int64(5),
  1731. // "op_order_0_records_out_total": int64(5),
  1732. // },
  1733. },
  1734. }
  1735. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1736. createSchemalessStreams(t)
  1737. defer dropSchemalessStreams(t)
  1738. common.InitConf()
  1739. for i, tt := range tests {
  1740. test.ResetClock(1541152486000)
  1741. p := NewRuleProcessor(DbDir)
  1742. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1743. var (
  1744. sources []*nodes.SourceNode
  1745. syncs []chan int
  1746. )
  1747. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1748. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1749. } else {
  1750. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1751. t.Errorf("sql %s is not a select statement", tt.sql)
  1752. } else {
  1753. streams := xsql.GetStreams(selectStmt)
  1754. for _, stream := range streams {
  1755. next := make(chan int)
  1756. syncs = append(syncs, next)
  1757. source := getMockSourceL(stream, next, tt.size)
  1758. sources = append(sources, source)
  1759. }
  1760. }
  1761. }
  1762. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  1763. if err != nil {
  1764. t.Error(err)
  1765. }
  1766. mockSink := test.NewMockSink()
  1767. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  1768. tp.AddSink(inputs, sink)
  1769. errCh := tp.Open()
  1770. func() {
  1771. for i := 0; i < tt.size*len(syncs); i++ {
  1772. syncs[i%len(syncs)] <- i
  1773. for {
  1774. time.Sleep(1)
  1775. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1776. break
  1777. }
  1778. }
  1779. select {
  1780. case err = <-errCh:
  1781. t.Log(err)
  1782. tp.Cancel()
  1783. return
  1784. default:
  1785. }
  1786. }
  1787. retry := 100
  1788. for ; retry > 0; retry-- {
  1789. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1790. break
  1791. }
  1792. t.Logf("wait to try another %d times", retry)
  1793. time.Sleep(time.Duration(retry) * time.Millisecond)
  1794. }
  1795. if retry == 0 {
  1796. err := compareMetrics(tp, tt.m, tt.sql)
  1797. t.Errorf("could not get correct metrics: %v", err)
  1798. }
  1799. }()
  1800. results := mockSink.GetResults()
  1801. var maps [][]map[string]interface{}
  1802. for _, v := range results {
  1803. var mapRes []map[string]interface{}
  1804. err := json.Unmarshal(v, &mapRes)
  1805. if err != nil {
  1806. t.Errorf("Failed to parse the input into map")
  1807. continue
  1808. }
  1809. maps = append(maps, mapRes)
  1810. }
  1811. if !reflect.DeepEqual(tt.r, maps) {
  1812. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1813. }
  1814. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1815. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1816. }
  1817. tp.Cancel()
  1818. }
  1819. }
  1820. func createEventStreams(t *testing.T) {
  1821. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  1822. demo := `CREATE STREAM demoE (
  1823. color STRING,
  1824. size BIGINT,
  1825. ts BIGINT
  1826. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1827. _, err := p.ExecStmt(demo)
  1828. if err != nil {
  1829. t.Log(err)
  1830. }
  1831. demo1 := `CREATE STREAM demo1E (
  1832. temp FLOAT,
  1833. hum BIGINT,
  1834. ts BIGINT
  1835. ) WITH (DATASOURCE="demo1E", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1836. _, err = p.ExecStmt(demo1)
  1837. if err != nil {
  1838. t.Log(err)
  1839. }
  1840. sessionDemo := `CREATE STREAM sessionDemoE (
  1841. temp FLOAT,
  1842. hum BIGINT,
  1843. ts BIGINT
  1844. ) WITH (DATASOURCE="sessionDemoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1845. _, err = p.ExecStmt(sessionDemo)
  1846. if err != nil {
  1847. t.Log(err)
  1848. }
  1849. demoErr := `CREATE STREAM demoErr (
  1850. color STRING,
  1851. size BIGINT,
  1852. ts BIGINT
  1853. ) WITH (DATASOURCE="demoErr", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  1854. _, err = p.ExecStmt(demoErr)
  1855. if err != nil {
  1856. t.Log(err)
  1857. }
  1858. }
  1859. func dropEventStreams(t *testing.T) {
  1860. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  1861. demo := `DROP STREAM demoE`
  1862. _, err := p.ExecStmt(demo)
  1863. if err != nil {
  1864. t.Log(err)
  1865. }
  1866. demo1 := `DROP STREAM demo1E`
  1867. _, err = p.ExecStmt(demo1)
  1868. if err != nil {
  1869. t.Log(err)
  1870. }
  1871. sessionDemo := `DROP STREAM sessionDemoE`
  1872. _, err = p.ExecStmt(sessionDemo)
  1873. if err != nil {
  1874. t.Log(err)
  1875. }
  1876. demoErr := `DROP STREAM demoErr`
  1877. _, err = p.ExecStmt(demoErr)
  1878. if err != nil {
  1879. t.Log(err)
  1880. }
  1881. }
  1882. func getEventMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  1883. var data []*xsql.Tuple
  1884. switch name {
  1885. case "demoE":
  1886. data = []*xsql.Tuple{
  1887. {
  1888. Emitter: name,
  1889. Message: map[string]interface{}{
  1890. "color": "red",
  1891. "size": 3,
  1892. "ts": 1541152486013,
  1893. },
  1894. Timestamp: 1541152486013,
  1895. },
  1896. {
  1897. Emitter: name,
  1898. Message: map[string]interface{}{
  1899. "color": "blue",
  1900. "size": 2,
  1901. "ts": 1541152487632,
  1902. },
  1903. Timestamp: 1541152487632,
  1904. },
  1905. {
  1906. Emitter: name,
  1907. Message: map[string]interface{}{
  1908. "color": "red",
  1909. "size": 1,
  1910. "ts": 1541152489252,
  1911. },
  1912. Timestamp: 1541152489252,
  1913. },
  1914. { //dropped item
  1915. Emitter: name,
  1916. Message: map[string]interface{}{
  1917. "color": "blue",
  1918. "size": 6,
  1919. "ts": 1541152486822,
  1920. },
  1921. Timestamp: 1541152486822,
  1922. },
  1923. {
  1924. Emitter: name,
  1925. Message: map[string]interface{}{
  1926. "color": "yellow",
  1927. "size": 4,
  1928. "ts": 1541152488442,
  1929. },
  1930. Timestamp: 1541152488442,
  1931. },
  1932. { //To lift the watermark and issue all windows
  1933. Emitter: name,
  1934. Message: map[string]interface{}{
  1935. "color": "yellow",
  1936. "size": 4,
  1937. "ts": 1541152492342,
  1938. },
  1939. Timestamp: 1541152488442,
  1940. },
  1941. }
  1942. case "demo1E":
  1943. data = []*xsql.Tuple{
  1944. {
  1945. Emitter: name,
  1946. Message: map[string]interface{}{
  1947. "temp": 27.5,
  1948. "hum": 59,
  1949. "ts": 1541152486823,
  1950. },
  1951. Timestamp: 1541152486823,
  1952. },
  1953. {
  1954. Emitter: name,
  1955. Message: map[string]interface{}{
  1956. "temp": 25.5,
  1957. "hum": 65,
  1958. "ts": 1541152486013,
  1959. },
  1960. Timestamp: 1541152486013,
  1961. },
  1962. {
  1963. Emitter: name,
  1964. Message: map[string]interface{}{
  1965. "temp": 27.4,
  1966. "hum": 80,
  1967. "ts": 1541152488442,
  1968. },
  1969. Timestamp: 1541152488442,
  1970. },
  1971. {
  1972. Emitter: name,
  1973. Message: map[string]interface{}{
  1974. "temp": 28.1,
  1975. "hum": 75,
  1976. "ts": 1541152487632,
  1977. },
  1978. Timestamp: 1541152487632,
  1979. },
  1980. {
  1981. Emitter: name,
  1982. Message: map[string]interface{}{
  1983. "temp": 25.5,
  1984. "hum": 62,
  1985. "ts": 1541152489252,
  1986. },
  1987. Timestamp: 1541152489252,
  1988. },
  1989. {
  1990. Emitter: name,
  1991. Message: map[string]interface{}{
  1992. "temp": 25.5,
  1993. "hum": 62,
  1994. "ts": 1541152499252,
  1995. },
  1996. Timestamp: 1541152499252,
  1997. },
  1998. }
  1999. case "sessionDemoE":
  2000. data = []*xsql.Tuple{
  2001. {
  2002. Emitter: name,
  2003. Message: map[string]interface{}{
  2004. "temp": 25.5,
  2005. "hum": 65,
  2006. "ts": 1541152486013,
  2007. },
  2008. Timestamp: 1541152486013,
  2009. },
  2010. {
  2011. Emitter: name,
  2012. Message: map[string]interface{}{
  2013. "temp": 28.1,
  2014. "hum": 75,
  2015. "ts": 1541152487932,
  2016. },
  2017. Timestamp: 1541152487932,
  2018. },
  2019. {
  2020. Emitter: name,
  2021. Message: map[string]interface{}{
  2022. "temp": 27.5,
  2023. "hum": 59,
  2024. "ts": 1541152486823,
  2025. },
  2026. Timestamp: 1541152486823,
  2027. },
  2028. {
  2029. Emitter: name,
  2030. Message: map[string]interface{}{
  2031. "temp": 25.5,
  2032. "hum": 62,
  2033. "ts": 1541152489252,
  2034. },
  2035. Timestamp: 1541152489252,
  2036. },
  2037. {
  2038. Emitter: name,
  2039. Message: map[string]interface{}{
  2040. "temp": 27.4,
  2041. "hum": 80,
  2042. "ts": 1541152488442,
  2043. },
  2044. Timestamp: 1541152488442,
  2045. },
  2046. {
  2047. Emitter: name,
  2048. Message: map[string]interface{}{
  2049. "temp": 26.2,
  2050. "hum": 63,
  2051. "ts": 1541152490062,
  2052. },
  2053. Timestamp: 1541152490062,
  2054. },
  2055. {
  2056. Emitter: name,
  2057. Message: map[string]interface{}{
  2058. "temp": 28.9,
  2059. "hum": 85,
  2060. "ts": 1541152491682,
  2061. },
  2062. Timestamp: 1541152491682,
  2063. },
  2064. {
  2065. Emitter: name,
  2066. Message: map[string]interface{}{
  2067. "temp": 26.8,
  2068. "hum": 71,
  2069. "ts": 1541152490872,
  2070. },
  2071. Timestamp: 1541152490872,
  2072. },
  2073. {
  2074. Emitter: name,
  2075. Message: map[string]interface{}{
  2076. "temp": 29.1,
  2077. "hum": 92,
  2078. "ts": 1541152492492,
  2079. },
  2080. Timestamp: 1541152492492,
  2081. },
  2082. {
  2083. Emitter: name,
  2084. Message: map[string]interface{}{
  2085. "temp": 30.9,
  2086. "hum": 87,
  2087. "ts": 1541152494112,
  2088. },
  2089. Timestamp: 1541152494112,
  2090. },
  2091. {
  2092. Emitter: name,
  2093. Message: map[string]interface{}{
  2094. "temp": 32.2,
  2095. "hum": 99,
  2096. "ts": 1541152493202,
  2097. },
  2098. Timestamp: 1541152493202,
  2099. },
  2100. {
  2101. Emitter: name,
  2102. Message: map[string]interface{}{
  2103. "temp": 32.2,
  2104. "hum": 99,
  2105. "ts": 1541152499202,
  2106. },
  2107. Timestamp: 1541152499202,
  2108. },
  2109. }
  2110. case "demoErr":
  2111. data = []*xsql.Tuple{
  2112. {
  2113. Emitter: name,
  2114. Message: map[string]interface{}{
  2115. "color": "red",
  2116. "size": 3,
  2117. "ts": 1541152486013,
  2118. },
  2119. Timestamp: 1541152486013,
  2120. },
  2121. {
  2122. Emitter: name,
  2123. Message: map[string]interface{}{
  2124. "color": 2,
  2125. "size": "blue",
  2126. "ts": 1541152487632,
  2127. },
  2128. Timestamp: 1541152487632,
  2129. },
  2130. {
  2131. Emitter: name,
  2132. Message: map[string]interface{}{
  2133. "color": "red",
  2134. "size": 1,
  2135. "ts": 1541152489252,
  2136. },
  2137. Timestamp: 1541152489252,
  2138. },
  2139. { //dropped item
  2140. Emitter: name,
  2141. Message: map[string]interface{}{
  2142. "color": "blue",
  2143. "size": 6,
  2144. "ts": 1541152486822,
  2145. },
  2146. Timestamp: 1541152486822,
  2147. },
  2148. {
  2149. Emitter: name,
  2150. Message: map[string]interface{}{
  2151. "color": "yellow",
  2152. "size": 4,
  2153. "ts": 1541152488442,
  2154. },
  2155. Timestamp: 1541152488442,
  2156. },
  2157. { //To lift the watermark and issue all windows
  2158. Emitter: name,
  2159. Message: map[string]interface{}{
  2160. "color": "yellow",
  2161. "size": 4,
  2162. "ts": 1541152492342,
  2163. },
  2164. Timestamp: 1541152488442,
  2165. },
  2166. }
  2167. }
  2168. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, true), map[string]string{
  2169. "DATASOURCE": name,
  2170. })
  2171. }
  2172. func TestEventWindow(t *testing.T) {
  2173. var tests = []struct {
  2174. name string
  2175. sql string
  2176. size int
  2177. r [][]map[string]interface{}
  2178. m map[string]interface{}
  2179. }{
  2180. {
  2181. name: `rule1`,
  2182. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2183. size: 6,
  2184. r: [][]map[string]interface{}{
  2185. {{
  2186. "color": "red",
  2187. "size": float64(3),
  2188. "ts": float64(1541152486013),
  2189. }},
  2190. {{
  2191. "color": "red",
  2192. "size": float64(3),
  2193. "ts": float64(1541152486013),
  2194. }, {
  2195. "color": "blue",
  2196. "size": float64(2),
  2197. "ts": float64(1541152487632),
  2198. }},
  2199. {{
  2200. "color": "blue",
  2201. "size": float64(2),
  2202. "ts": float64(1541152487632),
  2203. }, {
  2204. "color": "yellow",
  2205. "size": float64(4),
  2206. "ts": float64(1541152488442),
  2207. }}, {{
  2208. "color": "yellow",
  2209. "size": float64(4),
  2210. "ts": float64(1541152488442),
  2211. }, {
  2212. "color": "red",
  2213. "size": float64(1),
  2214. "ts": float64(1541152489252),
  2215. }}, {{
  2216. "color": "red",
  2217. "size": float64(1),
  2218. "ts": float64(1541152489252),
  2219. }},
  2220. },
  2221. m: map[string]interface{}{
  2222. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2223. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2224. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2225. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2226. "op_project_0_exceptions_total": int64(0),
  2227. "op_project_0_process_latency_ms": int64(0),
  2228. "op_project_0_records_in_total": int64(5),
  2229. "op_project_0_records_out_total": int64(5),
  2230. "sink_mockSink_0_exceptions_total": int64(0),
  2231. "sink_mockSink_0_records_in_total": int64(5),
  2232. "sink_mockSink_0_records_out_total": int64(5),
  2233. "source_demoE_0_exceptions_total": int64(0),
  2234. "source_demoE_0_records_in_total": int64(6),
  2235. "source_demoE_0_records_out_total": int64(6),
  2236. "op_window_0_exceptions_total": int64(0),
  2237. "op_window_0_process_latency_ms": int64(0),
  2238. "op_window_0_records_in_total": int64(6),
  2239. "op_window_0_records_out_total": int64(5),
  2240. },
  2241. }, {
  2242. name: `rule2`,
  2243. sql: `SELECT color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  2244. size: 6,
  2245. r: [][]map[string]interface{}{
  2246. {{
  2247. "color": "red",
  2248. "ts": float64(1541152486013),
  2249. }},
  2250. {{
  2251. "color": "yellow",
  2252. "ts": float64(1541152488442),
  2253. }},
  2254. },
  2255. m: map[string]interface{}{
  2256. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2257. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2258. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2259. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2260. "op_project_0_exceptions_total": int64(0),
  2261. "op_project_0_process_latency_ms": int64(0),
  2262. "op_project_0_records_in_total": int64(2),
  2263. "op_project_0_records_out_total": int64(2),
  2264. "sink_mockSink_0_exceptions_total": int64(0),
  2265. "sink_mockSink_0_records_in_total": int64(2),
  2266. "sink_mockSink_0_records_out_total": int64(2),
  2267. "source_demoE_0_exceptions_total": int64(0),
  2268. "source_demoE_0_records_in_total": int64(6),
  2269. "source_demoE_0_records_out_total": int64(6),
  2270. "op_window_0_exceptions_total": int64(0),
  2271. "op_window_0_process_latency_ms": int64(0),
  2272. "op_window_0_records_in_total": int64(6),
  2273. "op_window_0_records_out_total": int64(4),
  2274. "op_filter_0_exceptions_total": int64(0),
  2275. "op_filter_0_process_latency_ms": int64(0),
  2276. "op_filter_0_records_in_total": int64(4),
  2277. "op_filter_0_records_out_total": int64(2),
  2278. },
  2279. }, {
  2280. name: `rule3`,
  2281. sql: `SELECT color, temp, ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  2282. size: 6,
  2283. r: [][]map[string]interface{}{
  2284. {{
  2285. "color": "red",
  2286. "temp": 25.5,
  2287. "ts": float64(1541152486013),
  2288. }}, {{
  2289. "color": "red",
  2290. "temp": 25.5,
  2291. "ts": float64(1541152486013),
  2292. }}, {{
  2293. "color": "blue",
  2294. "temp": 28.1,
  2295. "ts": float64(1541152487632),
  2296. }}, {{
  2297. "color": "blue",
  2298. "temp": 28.1,
  2299. "ts": float64(1541152487632),
  2300. }, {
  2301. "color": "yellow",
  2302. "temp": 27.4,
  2303. "ts": float64(1541152488442),
  2304. }}, {{
  2305. "color": "yellow",
  2306. "temp": 27.4,
  2307. "ts": float64(1541152488442),
  2308. }, {
  2309. "color": "red",
  2310. "temp": 25.5,
  2311. "ts": float64(1541152489252),
  2312. }},
  2313. },
  2314. m: map[string]interface{}{
  2315. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2316. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2317. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2318. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2319. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2320. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2321. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2322. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2323. "op_project_0_exceptions_total": int64(0),
  2324. "op_project_0_process_latency_ms": int64(0),
  2325. "op_project_0_records_in_total": int64(5),
  2326. "op_project_0_records_out_total": int64(5),
  2327. "sink_mockSink_0_exceptions_total": int64(0),
  2328. "sink_mockSink_0_records_in_total": int64(5),
  2329. "sink_mockSink_0_records_out_total": int64(5),
  2330. "source_demoE_0_exceptions_total": int64(0),
  2331. "source_demoE_0_records_in_total": int64(6),
  2332. "source_demoE_0_records_out_total": int64(6),
  2333. "source_demo1E_0_exceptions_total": int64(0),
  2334. "source_demo1E_0_records_in_total": int64(6),
  2335. "source_demo1E_0_records_out_total": int64(6),
  2336. "op_window_0_exceptions_total": int64(0),
  2337. "op_window_0_process_latency_ms": int64(0),
  2338. "op_window_0_records_in_total": int64(12),
  2339. "op_window_0_records_out_total": int64(5),
  2340. "op_join_0_exceptions_total": int64(0),
  2341. "op_join_0_process_latency_ms": int64(0),
  2342. "op_join_0_records_in_total": int64(5),
  2343. "op_join_0_records_out_total": int64(5),
  2344. },
  2345. }, {
  2346. name: `rule4`,
  2347. sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  2348. size: 6,
  2349. r: [][]map[string]interface{}{
  2350. {{
  2351. "color": "red",
  2352. }}, {{
  2353. "color": "blue",
  2354. }, {
  2355. "color": "red",
  2356. }}, {{
  2357. "color": "blue",
  2358. }, {
  2359. "color": "yellow",
  2360. }}, {{
  2361. "color": "blue",
  2362. }, {
  2363. "color": "red",
  2364. }, {
  2365. "color": "yellow",
  2366. }},
  2367. },
  2368. m: map[string]interface{}{
  2369. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2370. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2371. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2372. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2373. "op_project_0_exceptions_total": int64(0),
  2374. "op_project_0_process_latency_ms": int64(0),
  2375. "op_project_0_records_in_total": int64(4),
  2376. "op_project_0_records_out_total": int64(4),
  2377. "sink_mockSink_0_exceptions_total": int64(0),
  2378. "sink_mockSink_0_records_in_total": int64(4),
  2379. "sink_mockSink_0_records_out_total": int64(4),
  2380. "source_demoE_0_exceptions_total": int64(0),
  2381. "source_demoE_0_records_in_total": int64(6),
  2382. "source_demoE_0_records_out_total": int64(6),
  2383. "op_window_0_exceptions_total": int64(0),
  2384. "op_window_0_process_latency_ms": int64(0),
  2385. "op_window_0_records_in_total": int64(6),
  2386. "op_window_0_records_out_total": int64(4),
  2387. "op_aggregate_0_exceptions_total": int64(0),
  2388. "op_aggregate_0_process_latency_ms": int64(0),
  2389. "op_aggregate_0_records_in_total": int64(4),
  2390. "op_aggregate_0_records_out_total": int64(4),
  2391. "op_order_0_exceptions_total": int64(0),
  2392. "op_order_0_process_latency_ms": int64(0),
  2393. "op_order_0_records_in_total": int64(4),
  2394. "op_order_0_records_out_total": int64(4),
  2395. },
  2396. }, {
  2397. name: `rule5`,
  2398. sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  2399. size: 12,
  2400. r: [][]map[string]interface{}{
  2401. {{
  2402. "temp": 25.5,
  2403. }}, {{
  2404. "temp": 28.1,
  2405. }, {
  2406. "temp": 27.4,
  2407. }, {
  2408. "temp": 25.5,
  2409. }}, {{
  2410. "temp": 26.2,
  2411. }, {
  2412. "temp": 26.8,
  2413. }, {
  2414. "temp": 28.9,
  2415. }, {
  2416. "temp": 29.1,
  2417. }, {
  2418. "temp": 32.2,
  2419. }}, {{
  2420. "temp": 30.9,
  2421. }},
  2422. },
  2423. m: map[string]interface{}{
  2424. "op_preprocessor_sessionDemoE_0_exceptions_total": int64(0),
  2425. "op_preprocessor_sessionDemoE_0_process_latency_ms": int64(0),
  2426. "op_preprocessor_sessionDemoE_0_records_in_total": int64(12),
  2427. "op_preprocessor_sessionDemoE_0_records_out_total": int64(12),
  2428. "op_project_0_exceptions_total": int64(0),
  2429. "op_project_0_process_latency_ms": int64(0),
  2430. "op_project_0_records_in_total": int64(4),
  2431. "op_project_0_records_out_total": int64(4),
  2432. "sink_mockSink_0_exceptions_total": int64(0),
  2433. "sink_mockSink_0_records_in_total": int64(4),
  2434. "sink_mockSink_0_records_out_total": int64(4),
  2435. "source_sessionDemoE_0_exceptions_total": int64(0),
  2436. "source_sessionDemoE_0_records_in_total": int64(12),
  2437. "source_sessionDemoE_0_records_out_total": int64(12),
  2438. "op_window_0_exceptions_total": int64(0),
  2439. "op_window_0_process_latency_ms": int64(0),
  2440. "op_window_0_records_in_total": int64(12),
  2441. "op_window_0_records_out_total": int64(4),
  2442. },
  2443. }, {
  2444. name: `rule6`,
  2445. 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)`,
  2446. size: 6,
  2447. r: [][]map[string]interface{}{
  2448. {{
  2449. "m": 25.5,
  2450. "c": float64(1),
  2451. }}, {{
  2452. "m": 25.5,
  2453. "c": float64(1),
  2454. }}, {{
  2455. "m": 28.1,
  2456. "c": float64(1),
  2457. }}, {{
  2458. "m": 28.1,
  2459. "c": float64(2),
  2460. }}, {{
  2461. "m": 27.4,
  2462. "c": float64(2),
  2463. }},
  2464. },
  2465. m: map[string]interface{}{
  2466. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2467. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2468. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2469. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2470. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2471. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2472. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2473. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2474. "op_project_0_exceptions_total": int64(0),
  2475. "op_project_0_process_latency_ms": int64(0),
  2476. "op_project_0_records_in_total": int64(5),
  2477. "op_project_0_records_out_total": int64(5),
  2478. "sink_mockSink_0_exceptions_total": int64(0),
  2479. "sink_mockSink_0_records_in_total": int64(5),
  2480. "sink_mockSink_0_records_out_total": int64(5),
  2481. "source_demoE_0_exceptions_total": int64(0),
  2482. "source_demoE_0_records_in_total": int64(6),
  2483. "source_demoE_0_records_out_total": int64(6),
  2484. "source_demo1E_0_exceptions_total": int64(0),
  2485. "source_demo1E_0_records_in_total": int64(6),
  2486. "source_demo1E_0_records_out_total": int64(6),
  2487. "op_window_0_exceptions_total": int64(0),
  2488. "op_window_0_records_in_total": int64(12),
  2489. "op_window_0_records_out_total": int64(5),
  2490. "op_join_0_exceptions_total": int64(0),
  2491. "op_join_0_process_latency_ms": int64(0),
  2492. "op_join_0_records_in_total": int64(5),
  2493. "op_join_0_records_out_total": int64(5),
  2494. },
  2495. }, {
  2496. name: `rule7`,
  2497. sql: `SELECT * FROM demoErr GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2498. size: 6,
  2499. r: [][]map[string]interface{}{
  2500. {{
  2501. "error": "error in preprocessor: invalid data type for color, expect string but found int(2)",
  2502. }},
  2503. {{
  2504. "color": "red",
  2505. "size": float64(3),
  2506. "ts": float64(1541152486013),
  2507. }},
  2508. {{
  2509. "color": "red",
  2510. "size": float64(3),
  2511. "ts": float64(1541152486013),
  2512. }},
  2513. {{
  2514. "color": "yellow",
  2515. "size": float64(4),
  2516. "ts": float64(1541152488442),
  2517. }}, {{
  2518. "color": "yellow",
  2519. "size": float64(4),
  2520. "ts": float64(1541152488442),
  2521. }, {
  2522. "color": "red",
  2523. "size": float64(1),
  2524. "ts": float64(1541152489252),
  2525. }}, {{
  2526. "color": "red",
  2527. "size": float64(1),
  2528. "ts": float64(1541152489252),
  2529. }},
  2530. },
  2531. m: map[string]interface{}{
  2532. "op_preprocessor_demoErr_0_exceptions_total": int64(1),
  2533. "op_preprocessor_demoErr_0_process_latency_ms": int64(0),
  2534. "op_preprocessor_demoErr_0_records_in_total": int64(6),
  2535. "op_preprocessor_demoErr_0_records_out_total": int64(5),
  2536. "op_project_0_exceptions_total": int64(1),
  2537. "op_project_0_process_latency_ms": int64(0),
  2538. "op_project_0_records_in_total": int64(6),
  2539. "op_project_0_records_out_total": int64(5),
  2540. "sink_mockSink_0_exceptions_total": int64(0),
  2541. "sink_mockSink_0_records_in_total": int64(6),
  2542. "sink_mockSink_0_records_out_total": int64(6),
  2543. "source_demoErr_0_exceptions_total": int64(0),
  2544. "source_demoErr_0_records_in_total": int64(6),
  2545. "source_demoErr_0_records_out_total": int64(6),
  2546. "op_window_0_exceptions_total": int64(1),
  2547. "op_window_0_process_latency_ms": int64(0),
  2548. "op_window_0_records_in_total": int64(6),
  2549. "op_window_0_records_out_total": int64(5),
  2550. },
  2551. },
  2552. }
  2553. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2554. createEventStreams(t)
  2555. defer dropEventStreams(t)
  2556. common.InitConf()
  2557. for i, tt := range tests {
  2558. test.ResetClock(1541152486000)
  2559. p := NewRuleProcessor(DbDir)
  2560. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2561. var (
  2562. sources []*nodes.SourceNode
  2563. syncs []chan int
  2564. )
  2565. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2566. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2567. } else {
  2568. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2569. t.Errorf("sql %s is not a select statement", tt.sql)
  2570. } else {
  2571. streams := xsql.GetStreams(selectStmt)
  2572. for _, stream := range streams {
  2573. next := make(chan int)
  2574. syncs = append(syncs, next)
  2575. source := getEventMockSource(stream, next, tt.size)
  2576. sources = append(sources, source)
  2577. }
  2578. }
  2579. }
  2580. tp, inputs, err := p.createTopoWithSources(&api.Rule{
  2581. Id: tt.name, Sql: tt.sql,
  2582. Options: map[string]interface{}{
  2583. "isEventTime": true,
  2584. "lateTolerance": float64(1000),
  2585. },
  2586. }, sources)
  2587. if err != nil {
  2588. t.Error(err)
  2589. }
  2590. mockSink := test.NewMockSink()
  2591. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink)
  2592. tp.AddSink(inputs, sink)
  2593. errCh := tp.Open()
  2594. func() {
  2595. for i := 0; i < tt.size*len(syncs); i++ {
  2596. syncs[i%len(syncs)] <- i
  2597. for {
  2598. time.Sleep(1)
  2599. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  2600. break
  2601. }
  2602. }
  2603. select {
  2604. case err = <-errCh:
  2605. t.Log(err)
  2606. tp.Cancel()
  2607. return
  2608. default:
  2609. }
  2610. }
  2611. mockClock := test.GetMockClock()
  2612. mockClock.Add(1000 * time.Millisecond)
  2613. retry := 100
  2614. for ; retry > 0; retry-- {
  2615. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2616. break
  2617. }
  2618. t.Logf("wait to try another %d times", retry)
  2619. time.Sleep(time.Duration(retry) * time.Millisecond)
  2620. }
  2621. if retry == 0 {
  2622. err := compareMetrics(tp, tt.m, tt.sql)
  2623. t.Errorf("could not get correct metrics: %v", err)
  2624. }
  2625. }()
  2626. results := mockSink.GetResults()
  2627. var maps [][]map[string]interface{}
  2628. for _, v := range results {
  2629. var mapRes []map[string]interface{}
  2630. err := json.Unmarshal(v, &mapRes)
  2631. if err != nil {
  2632. t.Errorf("Failed to parse the input into map")
  2633. continue
  2634. }
  2635. maps = append(maps, mapRes)
  2636. }
  2637. if !reflect.DeepEqual(tt.r, maps) {
  2638. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  2639. }
  2640. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  2641. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  2642. }
  2643. tp.Cancel()
  2644. }
  2645. }
  2646. func getMetric(tp *xstream.TopologyNew, name string) int {
  2647. keys, values := tp.GetMetrics()
  2648. for index, key := range keys {
  2649. if key == name {
  2650. return int(values[index].(int64))
  2651. }
  2652. }
  2653. fmt.Println("can't find " + name)
  2654. return 0
  2655. }
  2656. func compareMetrics(tp *xstream.TopologyNew, m map[string]interface{}, sql string) (err error) {
  2657. keys, values := tp.GetMetrics()
  2658. //for i, k := range keys{
  2659. // log.Printf("%s:%v", k, values[i])
  2660. //}
  2661. for k, v := range m {
  2662. var (
  2663. index int
  2664. key string
  2665. matched bool
  2666. )
  2667. for index, key = range keys {
  2668. if k == key {
  2669. if strings.HasSuffix(k, "process_latency_ms") {
  2670. if values[index].(int64) >= v.(int64) {
  2671. matched = true
  2672. continue
  2673. } else {
  2674. break
  2675. }
  2676. }
  2677. if values[index] == v {
  2678. matched = true
  2679. }
  2680. break
  2681. }
  2682. }
  2683. if matched {
  2684. continue
  2685. }
  2686. //do not find
  2687. if index < len(values) {
  2688. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%t)\n\ngot=%#v(%t)\n\n", k, v, v, values[index], values[index])
  2689. } else {
  2690. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  2691. }
  2692. }
  2693. return nil
  2694. }
  2695. func errstring(err error) string {
  2696. if err != nil {
  2697. return err.Error()
  2698. }
  2699. return ""
  2700. }