xsql_processor_test.go 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392
  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. common.InitConf()
  20. dbDir, err := common.GetAndCreateDataLoc("test")
  21. if err != nil {
  22. log.Panic(err)
  23. }
  24. log.Infof("db location is %s", dbDir)
  25. return dbDir
  26. }
  27. func TestStreamCreateProcessor(t *testing.T) {
  28. var tests = []struct {
  29. s string
  30. r []string
  31. err string
  32. }{
  33. {
  34. s: `SHOW STREAMS;`,
  35. r: []string{"No stream definitions are found."},
  36. },
  37. {
  38. s: `EXPLAIN STREAM topic1;`,
  39. err: "Stream topic1 is not found.",
  40. },
  41. {
  42. s: `CREATE STREAM topic1 (
  43. USERID BIGINT,
  44. FIRST_NAME STRING,
  45. LAST_NAME STRING,
  46. NICKNAMES ARRAY(STRING),
  47. Gender BOOLEAN,
  48. ADDRESS STRUCT(STREET_NAME STRING, NUMBER BIGINT),
  49. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  50. r: []string{"Stream topic1 is created."},
  51. },
  52. {
  53. s: `CREATE STREAM ` + "`stream`" + ` (
  54. USERID BIGINT,
  55. FIRST_NAME STRING,
  56. LAST_NAME STRING,
  57. NICKNAMES ARRAY(STRING),
  58. Gender BOOLEAN,
  59. ` + "`地址`" + ` STRUCT(STREET_NAME STRING, NUMBER BIGINT),
  60. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  61. r: []string{"Stream stream is created."},
  62. },
  63. {
  64. s: `CREATE STREAM topic1 (
  65. USERID BIGINT,
  66. ) WITH (DATASOURCE="users", FORMAT="AVRO", KEY="USERID");`,
  67. err: "Create stream fails: Item topic1 already exists.",
  68. },
  69. {
  70. s: `EXPLAIN STREAM topic1;`,
  71. r: []string{"TO BE SUPPORTED"},
  72. },
  73. {
  74. s: `DESCRIBE STREAM topic1;`,
  75. r: []string{"Fields\n--------------------------------------------------------------------------------\nUSERID\tbigint\nFIRST_NAME\tstring\nLAST_NAME\tstring\nNICKNAMES\t" +
  76. "array(string)\nGender\tboolean\nADDRESS\tstruct(STREET_NAME string, NUMBER bigint)\n\n" +
  77. "DATASOURCE: users\nFORMAT: AVRO\nKEY: USERID\n"},
  78. },
  79. {
  80. s: `DROP STREAM topic1;`,
  81. r: []string{"Stream topic1 is dropped."},
  82. },
  83. {
  84. s: `SHOW STREAMS;`,
  85. r: []string{"stream"},
  86. },
  87. {
  88. s: `DESCRIBE STREAM topic1;`,
  89. err: "Stream topic1 is not found.",
  90. },
  91. {
  92. s: `DROP STREAM topic1;`,
  93. err: "Drop stream fails: topic1 is not found.",
  94. },
  95. {
  96. s: "DROP STREAM `stream`;",
  97. r: []string{"Stream stream is dropped."},
  98. },
  99. }
  100. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  101. streamDB := path.Join(getDbDir(), "streamTest")
  102. for i, tt := range tests {
  103. results, err := NewStreamProcessor(streamDB).ExecStmt(tt.s)
  104. if !reflect.DeepEqual(tt.err, errstring(err)) {
  105. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
  106. } else if tt.err == "" {
  107. if !reflect.DeepEqual(tt.r, results) {
  108. t.Errorf("%d. %q\n\nstmt mismatch:\nexp=%s\ngot=%#v\n\n", i, tt.s, tt.r, results)
  109. }
  110. }
  111. }
  112. }
  113. func createStreams(t *testing.T) {
  114. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  115. demo := `CREATE STREAM demo (
  116. color STRING,
  117. size BIGINT,
  118. ts BIGINT
  119. ) WITH (DATASOURCE="demo", FORMAT="json", KEY="ts");`
  120. _, err := p.ExecStmt(demo)
  121. if err != nil {
  122. t.Log(err)
  123. }
  124. demoE := `CREATE STREAM demoE (
  125. color STRING,
  126. size BIGINT,
  127. ts BIGINT
  128. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts");`
  129. _, err = p.ExecStmt(demoE)
  130. if err != nil {
  131. t.Log(err)
  132. }
  133. demo1 := `CREATE STREAM demo1 (
  134. temp FLOAT,
  135. hum BIGINT,` +
  136. "`from`" + ` STRING,
  137. ts BIGINT
  138. ) WITH (DATASOURCE="demo1", FORMAT="json", KEY="ts");`
  139. _, err = p.ExecStmt(demo1)
  140. if err != nil {
  141. t.Log(err)
  142. }
  143. sessionDemo := `CREATE STREAM sessionDemo (
  144. temp FLOAT,
  145. hum BIGINT,
  146. ts BIGINT
  147. ) WITH (DATASOURCE="sessionDemo", FORMAT="json", KEY="ts");`
  148. _, err = p.ExecStmt(sessionDemo)
  149. if err != nil {
  150. t.Log(err)
  151. }
  152. }
  153. func dropStreams(t *testing.T) {
  154. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  155. demo := `DROP STREAM demo`
  156. _, err := p.ExecStmt(demo)
  157. if err != nil {
  158. t.Log(err)
  159. }
  160. demoE := `DROP STREAM demoE`
  161. _, err = p.ExecStmt(demoE)
  162. if err != nil {
  163. t.Log(err)
  164. }
  165. demo1 := `DROP STREAM demo1`
  166. _, err = p.ExecStmt(demo1)
  167. if err != nil {
  168. t.Log(err)
  169. }
  170. sessionDemo := `DROP STREAM sessionDemo`
  171. _, err = p.ExecStmt(sessionDemo)
  172. if err != nil {
  173. t.Log(err)
  174. }
  175. }
  176. func createSchemalessStreams(t *testing.T) {
  177. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  178. demo := `CREATE STREAM ldemo (
  179. ) WITH (DATASOURCE="ldemo", FORMAT="json");`
  180. _, err := p.ExecStmt(demo)
  181. if err != nil {
  182. t.Log(err)
  183. }
  184. demo1 := `CREATE STREAM ldemo1 (
  185. ) WITH (DATASOURCE="ldemo1", FORMAT="json");`
  186. _, err = p.ExecStmt(demo1)
  187. if err != nil {
  188. t.Log(err)
  189. }
  190. sessionDemo := `CREATE STREAM lsessionDemo (
  191. ) WITH (DATASOURCE="lsessionDemo", FORMAT="json");`
  192. _, err = p.ExecStmt(sessionDemo)
  193. if err != nil {
  194. t.Log(err)
  195. }
  196. }
  197. func dropSchemalessStreams(t *testing.T) {
  198. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  199. demo := `DROP STREAM ldemo`
  200. _, err := p.ExecStmt(demo)
  201. if err != nil {
  202. t.Log(err)
  203. }
  204. demo1 := `DROP STREAM ldemo1`
  205. _, err = p.ExecStmt(demo1)
  206. if err != nil {
  207. t.Log(err)
  208. }
  209. sessionDemo := `DROP STREAM lsessionDemo`
  210. _, err = p.ExecStmt(sessionDemo)
  211. if err != nil {
  212. t.Log(err)
  213. }
  214. }
  215. func getMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  216. var data []*xsql.Tuple
  217. switch name {
  218. case "demo":
  219. data = []*xsql.Tuple{
  220. {
  221. Emitter: name,
  222. Message: map[string]interface{}{
  223. "color": "red",
  224. "size": 3,
  225. "ts": 1541152486013,
  226. },
  227. Timestamp: 1541152486013,
  228. },
  229. {
  230. Emitter: name,
  231. Message: map[string]interface{}{
  232. "color": "blue",
  233. "size": 6,
  234. "ts": 1541152486822,
  235. },
  236. Timestamp: 1541152486822,
  237. },
  238. {
  239. Emitter: name,
  240. Message: map[string]interface{}{
  241. "color": "blue",
  242. "size": 2,
  243. "ts": 1541152487632,
  244. },
  245. Timestamp: 1541152487632,
  246. },
  247. {
  248. Emitter: name,
  249. Message: map[string]interface{}{
  250. "color": "yellow",
  251. "size": 4,
  252. "ts": 1541152488442,
  253. },
  254. Timestamp: 1541152488442,
  255. },
  256. {
  257. Emitter: name,
  258. Message: map[string]interface{}{
  259. "color": "red",
  260. "size": 1,
  261. "ts": 1541152489252,
  262. },
  263. Timestamp: 1541152489252,
  264. },
  265. }
  266. case "demoE":
  267. data = []*xsql.Tuple{
  268. {
  269. Emitter: name,
  270. Message: map[string]interface{}{
  271. "color": 3,
  272. "size": "red",
  273. "ts": 1541152486013,
  274. },
  275. Timestamp: 1541152486013,
  276. },
  277. {
  278. Emitter: name,
  279. Message: map[string]interface{}{
  280. "color": "blue",
  281. "size": 6,
  282. "ts": "1541152486822",
  283. },
  284. Timestamp: 1541152486822,
  285. },
  286. {
  287. Emitter: name,
  288. Message: map[string]interface{}{
  289. "color": "blue",
  290. "size": 2,
  291. "ts": 1541152487632,
  292. },
  293. Timestamp: 1541152487632,
  294. },
  295. {
  296. Emitter: name,
  297. Message: map[string]interface{}{
  298. "color": 7,
  299. "size": 4,
  300. "ts": 1541152488442,
  301. },
  302. Timestamp: 1541152488442,
  303. },
  304. {
  305. Emitter: name,
  306. Message: map[string]interface{}{
  307. "color": "red",
  308. "size": "blue",
  309. "ts": 1541152489252,
  310. },
  311. Timestamp: 1541152489252,
  312. },
  313. }
  314. case "demo1":
  315. data = []*xsql.Tuple{
  316. {
  317. Emitter: name,
  318. Message: map[string]interface{}{
  319. "temp": 25.5,
  320. "hum": 65,
  321. "from": "device1",
  322. "ts": 1541152486013,
  323. },
  324. Timestamp: 1541152486013,
  325. },
  326. {
  327. Emitter: name,
  328. Message: map[string]interface{}{
  329. "temp": 27.5,
  330. "hum": 59,
  331. "from": "device2",
  332. "ts": 1541152486823,
  333. },
  334. Timestamp: 1541152486823,
  335. },
  336. {
  337. Emitter: name,
  338. Message: map[string]interface{}{
  339. "temp": 28.1,
  340. "hum": 75,
  341. "from": "device3",
  342. "ts": 1541152487632,
  343. },
  344. Timestamp: 1541152487632,
  345. },
  346. {
  347. Emitter: name,
  348. Message: map[string]interface{}{
  349. "temp": 27.4,
  350. "hum": 80,
  351. "from": "device1",
  352. "ts": 1541152488442,
  353. },
  354. Timestamp: 1541152488442,
  355. },
  356. {
  357. Emitter: name,
  358. Message: map[string]interface{}{
  359. "temp": 25.5,
  360. "hum": 62,
  361. "from": "device3",
  362. "ts": 1541152489252,
  363. },
  364. Timestamp: 1541152489252,
  365. },
  366. }
  367. case "sessionDemo":
  368. data = []*xsql.Tuple{
  369. {
  370. Emitter: name,
  371. Message: map[string]interface{}{
  372. "temp": 25.5,
  373. "hum": 65,
  374. "ts": 1541152486013,
  375. },
  376. Timestamp: 1541152486013,
  377. },
  378. {
  379. Emitter: name,
  380. Message: map[string]interface{}{
  381. "temp": 27.5,
  382. "hum": 59,
  383. "ts": 1541152486823,
  384. },
  385. Timestamp: 1541152486823,
  386. },
  387. {
  388. Emitter: name,
  389. Message: map[string]interface{}{
  390. "temp": 28.1,
  391. "hum": 75,
  392. "ts": 1541152487932,
  393. },
  394. Timestamp: 1541152487932,
  395. },
  396. {
  397. Emitter: name,
  398. Message: map[string]interface{}{
  399. "temp": 27.4,
  400. "hum": 80,
  401. "ts": 1541152488442,
  402. },
  403. Timestamp: 1541152488442,
  404. },
  405. {
  406. Emitter: name,
  407. Message: map[string]interface{}{
  408. "temp": 25.5,
  409. "hum": 62,
  410. "ts": 1541152489252,
  411. },
  412. Timestamp: 1541152489252,
  413. },
  414. {
  415. Emitter: name,
  416. Message: map[string]interface{}{
  417. "temp": 26.2,
  418. "hum": 63,
  419. "ts": 1541152490062,
  420. },
  421. Timestamp: 1541152490062,
  422. },
  423. {
  424. Emitter: name,
  425. Message: map[string]interface{}{
  426. "temp": 26.8,
  427. "hum": 71,
  428. "ts": 1541152490872,
  429. },
  430. Timestamp: 1541152490872,
  431. },
  432. {
  433. Emitter: name,
  434. Message: map[string]interface{}{
  435. "temp": 28.9,
  436. "hum": 85,
  437. "ts": 1541152491682,
  438. },
  439. Timestamp: 1541152491682,
  440. },
  441. {
  442. Emitter: name,
  443. Message: map[string]interface{}{
  444. "temp": 29.1,
  445. "hum": 92,
  446. "ts": 1541152492492,
  447. },
  448. Timestamp: 1541152492492,
  449. },
  450. {
  451. Emitter: name,
  452. Message: map[string]interface{}{
  453. "temp": 32.2,
  454. "hum": 99,
  455. "ts": 1541152493202,
  456. },
  457. Timestamp: 1541152493202,
  458. },
  459. {
  460. Emitter: name,
  461. Message: map[string]interface{}{
  462. "temp": 30.9,
  463. "hum": 87,
  464. "ts": 1541152494112,
  465. },
  466. Timestamp: 1541152494112,
  467. },
  468. }
  469. }
  470. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  471. "DATASOURCE": name,
  472. })
  473. }
  474. func TestSingleSQL(t *testing.T) {
  475. var tests = []struct {
  476. name string
  477. sql string
  478. r [][]map[string]interface{}
  479. s string
  480. m map[string]interface{}
  481. }{
  482. {
  483. name: `rule1`,
  484. sql: `SELECT * FROM demo`,
  485. r: [][]map[string]interface{}{
  486. {{
  487. "color": "red",
  488. "size": float64(3),
  489. "ts": float64(1541152486013),
  490. }},
  491. {{
  492. "color": "blue",
  493. "size": float64(6),
  494. "ts": float64(1541152486822),
  495. }},
  496. {{
  497. "color": "blue",
  498. "size": float64(2),
  499. "ts": float64(1541152487632),
  500. }},
  501. {{
  502. "color": "yellow",
  503. "size": float64(4),
  504. "ts": float64(1541152488442),
  505. }},
  506. {{
  507. "color": "red",
  508. "size": float64(1),
  509. "ts": float64(1541152489252),
  510. }},
  511. },
  512. m: map[string]interface{}{
  513. "op_preprocessor_demo_0_exceptions_total": int64(0),
  514. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  515. "op_preprocessor_demo_0_records_in_total": int64(5),
  516. "op_preprocessor_demo_0_records_out_total": int64(5),
  517. "op_project_0_exceptions_total": int64(0),
  518. "op_project_0_process_latency_ms": int64(0),
  519. "op_project_0_records_in_total": int64(5),
  520. "op_project_0_records_out_total": int64(5),
  521. "sink_mockSink_0_exceptions_total": int64(0),
  522. "sink_mockSink_0_records_in_total": int64(5),
  523. "sink_mockSink_0_records_out_total": int64(5),
  524. "source_demo_0_exceptions_total": int64(0),
  525. "source_demo_0_records_in_total": int64(5),
  526. "source_demo_0_records_out_total": int64(5),
  527. },
  528. s: "sink_mockSink_0_records_out_total",
  529. }, {
  530. name: `rule2`,
  531. sql: `SELECT color, ts FROM demo where size > 3`,
  532. r: [][]map[string]interface{}{
  533. {{
  534. "color": "blue",
  535. "ts": float64(1541152486822),
  536. }},
  537. {{
  538. "color": "yellow",
  539. "ts": float64(1541152488442),
  540. }},
  541. },
  542. s: "op_filter_0_records_in_total",
  543. m: map[string]interface{}{
  544. "op_preprocessor_demo_0_exceptions_total": int64(0),
  545. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  546. "op_preprocessor_demo_0_records_in_total": int64(5),
  547. "op_preprocessor_demo_0_records_out_total": int64(5),
  548. "op_project_0_exceptions_total": int64(0),
  549. "op_project_0_process_latency_ms": int64(0),
  550. "op_project_0_records_in_total": int64(2),
  551. "op_project_0_records_out_total": int64(2),
  552. "sink_mockSink_0_exceptions_total": int64(0),
  553. "sink_mockSink_0_records_in_total": int64(2),
  554. "sink_mockSink_0_records_out_total": int64(2),
  555. "source_demo_0_exceptions_total": int64(0),
  556. "source_demo_0_records_in_total": int64(5),
  557. "source_demo_0_records_out_total": int64(5),
  558. "op_filter_0_exceptions_total": int64(0),
  559. "op_filter_0_process_latency_ms": int64(0),
  560. "op_filter_0_records_in_total": int64(5),
  561. "op_filter_0_records_out_total": int64(2),
  562. },
  563. }, {
  564. name: `rule3`,
  565. sql: `SELECT size as Int8, ts FROM demo where size > 3`,
  566. r: [][]map[string]interface{}{
  567. {{
  568. "Int8": float64(6),
  569. "ts": float64(1541152486822),
  570. }},
  571. {{
  572. "Int8": float64(4),
  573. "ts": float64(1541152488442),
  574. }},
  575. },
  576. s: "op_filter_0_records_in_total",
  577. m: map[string]interface{}{
  578. "op_preprocessor_demo_0_exceptions_total": int64(0),
  579. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  580. "op_preprocessor_demo_0_records_in_total": int64(5),
  581. "op_preprocessor_demo_0_records_out_total": int64(5),
  582. "op_project_0_exceptions_total": int64(0),
  583. "op_project_0_process_latency_ms": int64(0),
  584. "op_project_0_records_in_total": int64(2),
  585. "op_project_0_records_out_total": int64(2),
  586. "sink_mockSink_0_exceptions_total": int64(0),
  587. "sink_mockSink_0_records_in_total": int64(2),
  588. "sink_mockSink_0_records_out_total": int64(2),
  589. "source_demo_0_exceptions_total": int64(0),
  590. "source_demo_0_records_in_total": int64(5),
  591. "source_demo_0_records_out_total": int64(5),
  592. "op_filter_0_exceptions_total": int64(0),
  593. "op_filter_0_process_latency_ms": int64(0),
  594. "op_filter_0_records_in_total": int64(5),
  595. "op_filter_0_records_out_total": int64(2),
  596. },
  597. }, {
  598. name: `rule4`,
  599. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  600. r: [][]map[string]interface{}{
  601. {{
  602. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  603. }},
  604. {{
  605. "Int8": float64(6),
  606. "ts": float64(1541152486822),
  607. }},
  608. {{
  609. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  610. }},
  611. {{
  612. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  613. }},
  614. },
  615. s: "op_filter_0_records_in_total",
  616. m: map[string]interface{}{
  617. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  618. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  619. "op_preprocessor_demoE_0_records_in_total": int64(5),
  620. "op_preprocessor_demoE_0_records_out_total": int64(2),
  621. "op_project_0_exceptions_total": int64(3),
  622. "op_project_0_process_latency_ms": int64(0),
  623. "op_project_0_records_in_total": int64(4),
  624. "op_project_0_records_out_total": int64(1),
  625. "sink_mockSink_0_exceptions_total": int64(0),
  626. "sink_mockSink_0_records_in_total": int64(4),
  627. "sink_mockSink_0_records_out_total": int64(4),
  628. "source_demoE_0_exceptions_total": int64(0),
  629. "source_demoE_0_records_in_total": int64(5),
  630. "source_demoE_0_records_out_total": int64(5),
  631. "op_filter_0_exceptions_total": int64(3),
  632. "op_filter_0_process_latency_ms": int64(0),
  633. "op_filter_0_records_in_total": int64(5),
  634. "op_filter_0_records_out_total": int64(1),
  635. },
  636. }, {
  637. name: `rule4`,
  638. sql: `SELECT size as Int8, ts FROM demoE where size > 3`,
  639. r: [][]map[string]interface{}{
  640. {{
  641. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  642. }},
  643. {{
  644. "Int8": float64(6),
  645. "ts": float64(1541152486822),
  646. }},
  647. {{
  648. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  649. }},
  650. {{
  651. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  652. }},
  653. },
  654. s: "op_filter_0_records_in_total",
  655. m: map[string]interface{}{
  656. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  657. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  658. "op_preprocessor_demoE_0_records_in_total": int64(5),
  659. "op_preprocessor_demoE_0_records_out_total": int64(2),
  660. "op_project_0_exceptions_total": int64(3),
  661. "op_project_0_process_latency_ms": int64(0),
  662. "op_project_0_records_in_total": int64(4),
  663. "op_project_0_records_out_total": int64(1),
  664. "sink_mockSink_0_exceptions_total": int64(0),
  665. "sink_mockSink_0_records_in_total": int64(4),
  666. "sink_mockSink_0_records_out_total": int64(4),
  667. "source_demoE_0_exceptions_total": int64(0),
  668. "source_demoE_0_records_in_total": int64(5),
  669. "source_demoE_0_records_out_total": int64(5),
  670. "op_filter_0_exceptions_total": int64(3),
  671. "op_filter_0_process_latency_ms": int64(0),
  672. "op_filter_0_records_in_total": int64(5),
  673. "op_filter_0_records_out_total": int64(1),
  674. },
  675. }, {
  676. name: `rule5`,
  677. sql: `SELECT meta(topic) as m, ts FROM demo`,
  678. r: [][]map[string]interface{}{
  679. {{
  680. "m": "mock",
  681. "ts": float64(1541152486013),
  682. }},
  683. {{
  684. "m": "mock",
  685. "ts": float64(1541152486822),
  686. }},
  687. {{
  688. "m": "mock",
  689. "ts": float64(1541152487632),
  690. }},
  691. {{
  692. "m": "mock",
  693. "ts": float64(1541152488442),
  694. }},
  695. {{
  696. "m": "mock",
  697. "ts": float64(1541152489252),
  698. }},
  699. },
  700. m: map[string]interface{}{
  701. "op_preprocessor_demo_0_exceptions_total": int64(0),
  702. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  703. "op_preprocessor_demo_0_records_in_total": int64(5),
  704. "op_preprocessor_demo_0_records_out_total": int64(5),
  705. "op_project_0_exceptions_total": int64(0),
  706. "op_project_0_process_latency_ms": int64(0),
  707. "op_project_0_records_in_total": int64(5),
  708. "op_project_0_records_out_total": int64(5),
  709. "sink_mockSink_0_exceptions_total": int64(0),
  710. "sink_mockSink_0_records_in_total": int64(5),
  711. "sink_mockSink_0_records_out_total": int64(5),
  712. "source_demo_0_exceptions_total": int64(0),
  713. "source_demo_0_records_in_total": int64(5),
  714. "source_demo_0_records_out_total": int64(5),
  715. },
  716. s: "sink_mockSink_0_records_out_total",
  717. }, {
  718. name: `rule6`,
  719. sql: `SELECT color, ts FROM demo where size > 3 and meta(topic)="mock"`,
  720. r: [][]map[string]interface{}{
  721. {{
  722. "color": "blue",
  723. "ts": float64(1541152486822),
  724. }},
  725. {{
  726. "color": "yellow",
  727. "ts": float64(1541152488442),
  728. }},
  729. },
  730. s: "op_filter_0_records_in_total",
  731. m: map[string]interface{}{
  732. "op_preprocessor_demo_0_exceptions_total": int64(0),
  733. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  734. "op_preprocessor_demo_0_records_in_total": int64(5),
  735. "op_preprocessor_demo_0_records_out_total": int64(5),
  736. "op_project_0_exceptions_total": int64(0),
  737. "op_project_0_process_latency_ms": int64(0),
  738. "op_project_0_records_in_total": int64(2),
  739. "op_project_0_records_out_total": int64(2),
  740. "sink_mockSink_0_exceptions_total": int64(0),
  741. "sink_mockSink_0_records_in_total": int64(2),
  742. "sink_mockSink_0_records_out_total": int64(2),
  743. "source_demo_0_exceptions_total": int64(0),
  744. "source_demo_0_records_in_total": int64(5),
  745. "source_demo_0_records_out_total": int64(5),
  746. "op_filter_0_exceptions_total": int64(0),
  747. "op_filter_0_process_latency_ms": int64(0),
  748. "op_filter_0_records_in_total": int64(5),
  749. "op_filter_0_records_out_total": int64(2),
  750. },
  751. }, {
  752. name: `rule1`,
  753. sql: "SELECT `from` FROM demo1",
  754. r: [][]map[string]interface{}{
  755. {{
  756. "from": "device1",
  757. }},
  758. {{
  759. "from": "device2",
  760. }},
  761. {{
  762. "from": "device3",
  763. }},
  764. {{
  765. "from": "device1",
  766. }},
  767. {{
  768. "from": "device3",
  769. }},
  770. },
  771. m: map[string]interface{}{
  772. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  773. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  774. "op_preprocessor_demo1_0_records_in_total": int64(5),
  775. "op_preprocessor_demo1_0_records_out_total": int64(5),
  776. "op_project_0_exceptions_total": int64(0),
  777. "op_project_0_process_latency_ms": int64(0),
  778. "op_project_0_records_in_total": int64(5),
  779. "op_project_0_records_out_total": int64(5),
  780. "sink_mockSink_0_exceptions_total": int64(0),
  781. "sink_mockSink_0_records_in_total": int64(5),
  782. "sink_mockSink_0_records_out_total": int64(5),
  783. "source_demo1_0_exceptions_total": int64(0),
  784. "source_demo1_0_records_in_total": int64(5),
  785. "source_demo1_0_records_out_total": int64(5),
  786. },
  787. s: "sink_mockSink_0_records_out_total",
  788. }, {
  789. name: `rule1`,
  790. sql: "SELECT * FROM demo1 where `from`=\"device1\"",
  791. r: [][]map[string]interface{}{
  792. {{
  793. "temp": float64(25.5),
  794. "hum": float64(65),
  795. "from": "device1",
  796. "ts": float64(1541152486013),
  797. }},
  798. {{
  799. "temp": float64(27.4),
  800. "hum": float64(80),
  801. "from": "device1",
  802. "ts": float64(1541152488442),
  803. }},
  804. },
  805. m: map[string]interface{}{
  806. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  807. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  808. "op_preprocessor_demo1_0_records_in_total": int64(5),
  809. "op_preprocessor_demo1_0_records_out_total": int64(5),
  810. "op_project_0_exceptions_total": int64(0),
  811. "op_project_0_process_latency_ms": int64(0),
  812. "op_project_0_records_in_total": int64(2),
  813. "op_project_0_records_out_total": int64(2),
  814. "op_filter_0_exceptions_total": int64(0),
  815. "op_filter_0_process_latency_ms": int64(0),
  816. "op_filter_0_records_in_total": int64(5),
  817. "op_filter_0_records_out_total": int64(2),
  818. "sink_mockSink_0_exceptions_total": int64(0),
  819. "sink_mockSink_0_records_in_total": int64(2),
  820. "sink_mockSink_0_records_out_total": int64(2),
  821. "source_demo1_0_exceptions_total": int64(0),
  822. "source_demo1_0_records_in_total": int64(5),
  823. "source_demo1_0_records_out_total": int64(5),
  824. },
  825. s: "sink_mockSink_0_records_out_total",
  826. },
  827. }
  828. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  829. createStreams(t)
  830. defer dropStreams(t)
  831. //defer close(done)
  832. for i, tt := range tests {
  833. test.ResetClock(1541152486000)
  834. p := NewRuleProcessor(DbDir)
  835. parser := xsql.NewParser(strings.NewReader(tt.sql))
  836. var (
  837. sources []*nodes.SourceNode
  838. syncs []chan int
  839. )
  840. if stmt, err := xsql.Language.Parse(parser); err != nil {
  841. t.Errorf("parse sql %s error: %s", tt.sql, err)
  842. } else {
  843. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  844. t.Errorf("sql %s is not a select statement", tt.sql)
  845. } else {
  846. streams := xsql.GetStreams(selectStmt)
  847. for _, stream := range streams {
  848. next := make(chan int)
  849. syncs = append(syncs, next)
  850. source := getMockSource(stream, next, 5)
  851. sources = append(sources, source)
  852. }
  853. }
  854. }
  855. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  856. "bufferLength": float64(100),
  857. }}, sources)
  858. if err != nil {
  859. t.Error(err)
  860. }
  861. mockSink := test.NewMockSink()
  862. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  863. tp.AddSink(inputs, sink)
  864. errCh := tp.Open()
  865. func() {
  866. for i := 0; i < 5; i++ {
  867. syncs[i%len(syncs)] <- i
  868. select {
  869. case err = <-errCh:
  870. t.Log(err)
  871. tp.Cancel()
  872. return
  873. default:
  874. }
  875. }
  876. for retry := 100; retry > 0; retry-- {
  877. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  878. break
  879. }
  880. time.Sleep(time.Duration(retry) * time.Millisecond)
  881. }
  882. }()
  883. results := mockSink.GetResults()
  884. var maps [][]map[string]interface{}
  885. for _, v := range results {
  886. var mapRes []map[string]interface{}
  887. err := json.Unmarshal(v, &mapRes)
  888. if err != nil {
  889. t.Errorf("Failed to parse the input into map")
  890. continue
  891. }
  892. maps = append(maps, mapRes)
  893. }
  894. if !reflect.DeepEqual(tt.r, maps) {
  895. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  896. continue
  897. }
  898. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  899. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  900. }
  901. tp.Cancel()
  902. }
  903. }
  904. func TestSingleSQLTemplate(t *testing.T) {
  905. var tests = []struct {
  906. name string
  907. sql string
  908. r []map[string]interface{}
  909. s string
  910. m map[string]interface{}
  911. }{
  912. {
  913. name: `rule1`,
  914. sql: `SELECT * FROM demo`,
  915. r: []map[string]interface{}{
  916. {
  917. "c": "red",
  918. "wrapper": "w1",
  919. },
  920. {
  921. "c": "blue",
  922. "wrapper": "w1",
  923. },
  924. {
  925. "c": "blue",
  926. "wrapper": "w1",
  927. },
  928. {
  929. "c": "yellow",
  930. "wrapper": "w1",
  931. },
  932. {
  933. "c": "red",
  934. "wrapper": "w1",
  935. },
  936. },
  937. m: map[string]interface{}{
  938. "op_preprocessor_demo_0_exceptions_total": int64(0),
  939. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  940. "op_preprocessor_demo_0_records_in_total": int64(5),
  941. "op_preprocessor_demo_0_records_out_total": int64(5),
  942. "op_project_0_exceptions_total": int64(0),
  943. "op_project_0_process_latency_ms": int64(0),
  944. "op_project_0_records_in_total": int64(5),
  945. "op_project_0_records_out_total": int64(5),
  946. "sink_mockSink_0_exceptions_total": int64(0),
  947. "sink_mockSink_0_records_in_total": int64(5),
  948. "sink_mockSink_0_records_out_total": int64(5),
  949. "source_demo_0_exceptions_total": int64(0),
  950. "source_demo_0_records_in_total": int64(5),
  951. "source_demo_0_records_out_total": int64(5),
  952. },
  953. s: "sink_mockSink_0_records_out_total",
  954. },
  955. }
  956. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  957. createStreams(t)
  958. defer dropStreams(t)
  959. //defer close(done)
  960. for i, tt := range tests {
  961. test.ResetClock(1541152486000)
  962. p := NewRuleProcessor(DbDir)
  963. parser := xsql.NewParser(strings.NewReader(tt.sql))
  964. var (
  965. sources []*nodes.SourceNode
  966. syncs []chan int
  967. )
  968. if stmt, err := xsql.Language.Parse(parser); err != nil {
  969. t.Errorf("parse sql %s error: %s", tt.sql, err)
  970. } else {
  971. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  972. t.Errorf("sql %s is not a select statement", tt.sql)
  973. } else {
  974. streams := xsql.GetStreams(selectStmt)
  975. for _, stream := range streams {
  976. next := make(chan int)
  977. syncs = append(syncs, next)
  978. source := getMockSource(stream, next, 5)
  979. sources = append(sources, source)
  980. }
  981. }
  982. }
  983. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  984. "bufferLength": float64(100),
  985. }}, sources)
  986. if err != nil {
  987. t.Error(err)
  988. }
  989. mockSink := test.NewMockSink()
  990. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, map[string]interface{}{
  991. "dataTemplate": `{"wrapper":"w1", "c":"{{.color}}"}`,
  992. "sendSingle": true,
  993. })
  994. tp.AddSink(inputs, sink)
  995. errCh := tp.Open()
  996. func() {
  997. for i := 0; i < 5; i++ {
  998. syncs[i%len(syncs)] <- i
  999. select {
  1000. case err = <-errCh:
  1001. t.Log(err)
  1002. tp.Cancel()
  1003. return
  1004. default:
  1005. }
  1006. }
  1007. for retry := 100; retry > 0; retry-- {
  1008. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1009. break
  1010. }
  1011. time.Sleep(time.Duration(retry) * time.Millisecond)
  1012. }
  1013. }()
  1014. results := mockSink.GetResults()
  1015. var maps []map[string]interface{}
  1016. for _, v := range results {
  1017. var mapRes map[string]interface{}
  1018. err := json.Unmarshal(v, &mapRes)
  1019. if err != nil {
  1020. t.Errorf("Failed to parse the input into map")
  1021. continue
  1022. }
  1023. maps = append(maps, mapRes)
  1024. }
  1025. if !reflect.DeepEqual(tt.r, maps) {
  1026. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1027. continue
  1028. }
  1029. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1030. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1031. }
  1032. tp.Cancel()
  1033. }
  1034. }
  1035. func TestNoneSingleSQLTemplate(t *testing.T) {
  1036. var tests = []struct {
  1037. name string
  1038. sql string
  1039. r [][]byte
  1040. s string
  1041. m map[string]interface{}
  1042. }{
  1043. {
  1044. name: `rule1`,
  1045. sql: `SELECT * FROM demo`,
  1046. r: [][]byte{
  1047. []byte("<div>results</div><ul><li>red - 3</li></ul>"),
  1048. []byte("<div>results</div><ul><li>blue - 6</li></ul>"),
  1049. []byte("<div>results</div><ul><li>blue - 2</li></ul>"),
  1050. []byte("<div>results</div><ul><li>yellow - 4</li></ul>"),
  1051. []byte("<div>results</div><ul><li>red - 1</li></ul>"),
  1052. },
  1053. m: map[string]interface{}{
  1054. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1055. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1056. "op_preprocessor_demo_0_records_in_total": int64(5),
  1057. "op_preprocessor_demo_0_records_out_total": int64(5),
  1058. "op_project_0_exceptions_total": int64(0),
  1059. "op_project_0_process_latency_ms": int64(0),
  1060. "op_project_0_records_in_total": int64(5),
  1061. "op_project_0_records_out_total": int64(5),
  1062. "sink_mockSink_0_exceptions_total": int64(0),
  1063. "sink_mockSink_0_records_in_total": int64(5),
  1064. "sink_mockSink_0_records_out_total": int64(5),
  1065. "source_demo_0_exceptions_total": int64(0),
  1066. "source_demo_0_records_in_total": int64(5),
  1067. "source_demo_0_records_out_total": int64(5),
  1068. },
  1069. s: "sink_mockSink_0_records_out_total",
  1070. },
  1071. }
  1072. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1073. createStreams(t)
  1074. defer dropStreams(t)
  1075. //defer close(done)
  1076. for i, tt := range tests {
  1077. test.ResetClock(1541152486000)
  1078. p := NewRuleProcessor(DbDir)
  1079. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1080. var (
  1081. sources []*nodes.SourceNode
  1082. syncs []chan int
  1083. )
  1084. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1085. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1086. } else {
  1087. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1088. t.Errorf("sql %s is not a select statement", tt.sql)
  1089. } else {
  1090. streams := xsql.GetStreams(selectStmt)
  1091. for _, stream := range streams {
  1092. next := make(chan int)
  1093. syncs = append(syncs, next)
  1094. source := getMockSource(stream, next, 5)
  1095. sources = append(sources, source)
  1096. }
  1097. }
  1098. }
  1099. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1100. "bufferLength": float64(100),
  1101. }}, sources)
  1102. if err != nil {
  1103. t.Error(err)
  1104. }
  1105. mockSink := test.NewMockSink()
  1106. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, map[string]interface{}{
  1107. "dataTemplate": `<div>results</div><ul>{{range .}}<li>{{.color}} - {{.size}}</li>{{end}}</ul>`,
  1108. })
  1109. tp.AddSink(inputs, sink)
  1110. errCh := tp.Open()
  1111. func() {
  1112. for i := 0; i < 5; i++ {
  1113. syncs[i%len(syncs)] <- i
  1114. select {
  1115. case err = <-errCh:
  1116. t.Log(err)
  1117. tp.Cancel()
  1118. return
  1119. default:
  1120. }
  1121. }
  1122. for retry := 100; retry > 0; retry-- {
  1123. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1124. break
  1125. }
  1126. time.Sleep(time.Duration(retry) * time.Millisecond)
  1127. }
  1128. }()
  1129. results := mockSink.GetResults()
  1130. if !reflect.DeepEqual(tt.r, results) {
  1131. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, results)
  1132. continue
  1133. }
  1134. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1135. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1136. }
  1137. tp.Cancel()
  1138. }
  1139. }
  1140. func getMockSourceL(name string, done <-chan int, size int) *nodes.SourceNode {
  1141. var data []*xsql.Tuple
  1142. switch name {
  1143. case "ldemo":
  1144. data = []*xsql.Tuple{
  1145. {
  1146. Emitter: name,
  1147. Message: map[string]interface{}{
  1148. "color": "red",
  1149. "size": 3,
  1150. "ts": 1541152486013,
  1151. },
  1152. Timestamp: 1541152486013,
  1153. },
  1154. {
  1155. Emitter: name,
  1156. Message: map[string]interface{}{
  1157. "color": "blue",
  1158. "size": "string",
  1159. "ts": 1541152486822,
  1160. },
  1161. Timestamp: 1541152486822,
  1162. },
  1163. {
  1164. Emitter: name,
  1165. Message: map[string]interface{}{
  1166. "size": 3,
  1167. "ts": 1541152487632,
  1168. },
  1169. Timestamp: 1541152487632,
  1170. },
  1171. {
  1172. Emitter: name,
  1173. Message: map[string]interface{}{
  1174. "color": 49,
  1175. "size": 2,
  1176. "ts": 1541152488442,
  1177. },
  1178. Timestamp: 1541152488442,
  1179. },
  1180. {
  1181. Emitter: name,
  1182. Message: map[string]interface{}{
  1183. "color": "red",
  1184. "ts": 1541152489252,
  1185. },
  1186. Timestamp: 1541152489252,
  1187. },
  1188. }
  1189. case "ldemo1":
  1190. data = []*xsql.Tuple{
  1191. {
  1192. Emitter: name,
  1193. Message: map[string]interface{}{
  1194. "temp": 25.5,
  1195. "hum": 65,
  1196. "ts": 1541152486013,
  1197. },
  1198. Timestamp: 1541152486013,
  1199. },
  1200. {
  1201. Emitter: name,
  1202. Message: map[string]interface{}{
  1203. "temp": 27.5,
  1204. "hum": 59,
  1205. "ts": 1541152486823,
  1206. },
  1207. Timestamp: 1541152486823,
  1208. },
  1209. {
  1210. Emitter: name,
  1211. Message: map[string]interface{}{
  1212. "temp": 28.1,
  1213. "hum": 75,
  1214. "ts": 1541152487632,
  1215. },
  1216. Timestamp: 1541152487632,
  1217. },
  1218. {
  1219. Emitter: name,
  1220. Message: map[string]interface{}{
  1221. "temp": 27.4,
  1222. "hum": 80,
  1223. "ts": "1541152488442",
  1224. },
  1225. Timestamp: 1541152488442,
  1226. },
  1227. {
  1228. Emitter: name,
  1229. Message: map[string]interface{}{
  1230. "temp": 25.5,
  1231. "hum": 62,
  1232. "ts": 1541152489252,
  1233. },
  1234. Timestamp: 1541152489252,
  1235. },
  1236. }
  1237. case "lsessionDemo":
  1238. data = []*xsql.Tuple{
  1239. {
  1240. Emitter: name,
  1241. Message: map[string]interface{}{
  1242. "temp": 25.5,
  1243. "hum": 65,
  1244. "ts": 1541152486013,
  1245. },
  1246. Timestamp: 1541152486013,
  1247. },
  1248. {
  1249. Emitter: name,
  1250. Message: map[string]interface{}{
  1251. "temp": 27.5,
  1252. "hum": 59,
  1253. "ts": 1541152486823,
  1254. },
  1255. Timestamp: 1541152486823,
  1256. },
  1257. {
  1258. Emitter: name,
  1259. Message: map[string]interface{}{
  1260. "temp": 28.1,
  1261. "hum": 75,
  1262. "ts": 1541152487932,
  1263. },
  1264. Timestamp: 1541152487932,
  1265. },
  1266. {
  1267. Emitter: name,
  1268. Message: map[string]interface{}{
  1269. "temp": 27.4,
  1270. "hum": 80,
  1271. "ts": 1541152488442,
  1272. },
  1273. Timestamp: 1541152488442,
  1274. },
  1275. {
  1276. Emitter: name,
  1277. Message: map[string]interface{}{
  1278. "temp": 25.5,
  1279. "hum": 62,
  1280. "ts": 1541152489252,
  1281. },
  1282. Timestamp: 1541152489252,
  1283. },
  1284. {
  1285. Emitter: name,
  1286. Message: map[string]interface{}{
  1287. "temp": 26.2,
  1288. "hum": 63,
  1289. "ts": 1541152490062,
  1290. },
  1291. Timestamp: 1541152490062,
  1292. },
  1293. {
  1294. Emitter: name,
  1295. Message: map[string]interface{}{
  1296. "temp": 26.8,
  1297. "hum": 71,
  1298. "ts": 1541152490872,
  1299. },
  1300. Timestamp: 1541152490872,
  1301. },
  1302. {
  1303. Emitter: name,
  1304. Message: map[string]interface{}{
  1305. "temp": 28.9,
  1306. "hum": 85,
  1307. "ts": 1541152491682,
  1308. },
  1309. Timestamp: 1541152491682,
  1310. },
  1311. {
  1312. Emitter: name,
  1313. Message: map[string]interface{}{
  1314. "temp": 29.1,
  1315. "hum": 92,
  1316. "ts": 1541152492492,
  1317. },
  1318. Timestamp: 1541152492492,
  1319. },
  1320. {
  1321. Emitter: name,
  1322. Message: map[string]interface{}{
  1323. "temp": 2.2,
  1324. "hum": 99,
  1325. "ts": 1541152493202,
  1326. },
  1327. Timestamp: 1541152493202,
  1328. },
  1329. {
  1330. Emitter: name,
  1331. Message: map[string]interface{}{
  1332. "temp": 30.9,
  1333. "hum": 87,
  1334. "ts": 1541152494112,
  1335. },
  1336. Timestamp: 1541152494112,
  1337. },
  1338. }
  1339. }
  1340. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, false), map[string]string{
  1341. "DATASOURCE": name,
  1342. })
  1343. }
  1344. func TestSingleSQLError(t *testing.T) {
  1345. var tests = []struct {
  1346. name string
  1347. sql string
  1348. r [][]map[string]interface{}
  1349. s string
  1350. m map[string]interface{}
  1351. }{
  1352. {
  1353. name: `rule1`,
  1354. sql: `SELECT color, ts FROM ldemo where size >= 3`,
  1355. r: [][]map[string]interface{}{
  1356. {{
  1357. "color": "red",
  1358. "ts": float64(1541152486013),
  1359. }},
  1360. {{
  1361. "error": "run Where error: invalid operation string(string) >= int64(3)",
  1362. }},
  1363. {{
  1364. "ts": float64(1541152487632),
  1365. }},
  1366. },
  1367. s: "op_filter_0_records_in_total",
  1368. m: map[string]interface{}{
  1369. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1370. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1371. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1372. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1373. "op_project_0_exceptions_total": int64(1),
  1374. "op_project_0_process_latency_ms": int64(0),
  1375. "op_project_0_records_in_total": int64(3),
  1376. "op_project_0_records_out_total": int64(2),
  1377. "sink_mockSink_0_exceptions_total": int64(0),
  1378. "sink_mockSink_0_records_in_total": int64(3),
  1379. "sink_mockSink_0_records_out_total": int64(3),
  1380. "source_ldemo_0_exceptions_total": int64(0),
  1381. "source_ldemo_0_records_in_total": int64(5),
  1382. "source_ldemo_0_records_out_total": int64(5),
  1383. "op_filter_0_exceptions_total": int64(1),
  1384. "op_filter_0_process_latency_ms": int64(0),
  1385. "op_filter_0_records_in_total": int64(5),
  1386. "op_filter_0_records_out_total": int64(2),
  1387. },
  1388. }, {
  1389. name: `rule2`,
  1390. sql: `SELECT size * 5 FROM ldemo`,
  1391. r: [][]map[string]interface{}{
  1392. {{
  1393. "rengine_field_0": float64(15),
  1394. }},
  1395. {{
  1396. "error": "run Select error: invalid operation string(string) * int64(5)",
  1397. }},
  1398. {{
  1399. "rengine_field_0": float64(15),
  1400. }},
  1401. {{
  1402. "rengine_field_0": float64(10),
  1403. }},
  1404. {{}},
  1405. },
  1406. s: "op_filter_0_records_in_total",
  1407. m: map[string]interface{}{
  1408. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  1409. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  1410. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  1411. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  1412. "op_project_0_exceptions_total": int64(1),
  1413. "op_project_0_process_latency_ms": int64(0),
  1414. "op_project_0_records_in_total": int64(5),
  1415. "op_project_0_records_out_total": int64(4),
  1416. "sink_mockSink_0_exceptions_total": int64(0),
  1417. "sink_mockSink_0_records_in_total": int64(5),
  1418. "sink_mockSink_0_records_out_total": int64(5),
  1419. "source_ldemo_0_exceptions_total": int64(0),
  1420. "source_ldemo_0_records_in_total": int64(5),
  1421. "source_ldemo_0_records_out_total": int64(5),
  1422. },
  1423. },
  1424. }
  1425. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1426. createSchemalessStreams(t)
  1427. defer dropSchemalessStreams(t)
  1428. //defer close(done)
  1429. for i, tt := range tests {
  1430. test.ResetClock(1541152486000)
  1431. p := NewRuleProcessor(DbDir)
  1432. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1433. var (
  1434. sources []*nodes.SourceNode
  1435. syncs []chan int
  1436. )
  1437. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1438. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1439. } else {
  1440. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1441. t.Errorf("sql %s is not a select statement", tt.sql)
  1442. } else {
  1443. streams := xsql.GetStreams(selectStmt)
  1444. for _, stream := range streams {
  1445. next := make(chan int)
  1446. syncs = append(syncs, next)
  1447. source := getMockSourceL(stream, next, 5)
  1448. sources = append(sources, source)
  1449. }
  1450. }
  1451. }
  1452. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql, Options: map[string]interface{}{
  1453. "bufferLength": float64(100),
  1454. }}, sources)
  1455. if err != nil {
  1456. t.Error(err)
  1457. }
  1458. mockSink := test.NewMockSink()
  1459. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  1460. tp.AddSink(inputs, sink)
  1461. errCh := tp.Open()
  1462. func() {
  1463. for i := 0; i < 5; i++ {
  1464. syncs[i%len(syncs)] <- i
  1465. select {
  1466. case err = <-errCh:
  1467. t.Log(err)
  1468. tp.Cancel()
  1469. return
  1470. default:
  1471. }
  1472. }
  1473. for retry := 100; retry > 0; retry-- {
  1474. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  1475. break
  1476. }
  1477. time.Sleep(time.Duration(retry) * time.Millisecond)
  1478. }
  1479. }()
  1480. results := mockSink.GetResults()
  1481. var maps [][]map[string]interface{}
  1482. for _, v := range results {
  1483. var mapRes []map[string]interface{}
  1484. err := json.Unmarshal(v, &mapRes)
  1485. if err != nil {
  1486. t.Errorf("Failed to parse the input into map")
  1487. continue
  1488. }
  1489. maps = append(maps, mapRes)
  1490. }
  1491. if !reflect.DeepEqual(tt.r, maps) {
  1492. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  1493. continue
  1494. }
  1495. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  1496. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  1497. }
  1498. tp.Cancel()
  1499. }
  1500. }
  1501. func TestWindow(t *testing.T) {
  1502. var tests = []struct {
  1503. name string
  1504. sql string
  1505. size int
  1506. r [][]map[string]interface{}
  1507. m map[string]interface{}
  1508. }{
  1509. {
  1510. name: `rule1`,
  1511. sql: `SELECT * FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1512. size: 5,
  1513. r: [][]map[string]interface{}{
  1514. {{
  1515. "color": "red",
  1516. "size": float64(3),
  1517. "ts": float64(1541152486013),
  1518. }, {
  1519. "color": "blue",
  1520. "size": float64(6),
  1521. "ts": float64(1541152486822),
  1522. }},
  1523. {{
  1524. "color": "red",
  1525. "size": float64(3),
  1526. "ts": float64(1541152486013),
  1527. }, {
  1528. "color": "blue",
  1529. "size": float64(6),
  1530. "ts": float64(1541152486822),
  1531. }, {
  1532. "color": "blue",
  1533. "size": float64(2),
  1534. "ts": float64(1541152487632),
  1535. }},
  1536. {{
  1537. "color": "blue",
  1538. "size": float64(2),
  1539. "ts": float64(1541152487632),
  1540. }, {
  1541. "color": "yellow",
  1542. "size": float64(4),
  1543. "ts": float64(1541152488442),
  1544. }},
  1545. },
  1546. m: map[string]interface{}{
  1547. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1548. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1549. "op_preprocessor_demo_0_records_in_total": int64(5),
  1550. "op_preprocessor_demo_0_records_out_total": int64(5),
  1551. "op_project_0_exceptions_total": int64(0),
  1552. "op_project_0_process_latency_ms": int64(0),
  1553. "op_project_0_records_in_total": int64(3),
  1554. "op_project_0_records_out_total": int64(3),
  1555. "sink_mockSink_0_exceptions_total": int64(0),
  1556. "sink_mockSink_0_records_in_total": int64(3),
  1557. "sink_mockSink_0_records_out_total": int64(3),
  1558. "source_demo_0_exceptions_total": int64(0),
  1559. "source_demo_0_records_in_total": int64(5),
  1560. "source_demo_0_records_out_total": int64(5),
  1561. "op_window_0_exceptions_total": int64(0),
  1562. "op_window_0_process_latency_ms": int64(0),
  1563. "op_window_0_records_in_total": int64(5),
  1564. "op_window_0_records_out_total": int64(3),
  1565. },
  1566. }, {
  1567. name: `rule2`,
  1568. sql: `SELECT color, ts FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  1569. size: 5,
  1570. r: [][]map[string]interface{}{
  1571. {{
  1572. "color": "red",
  1573. "ts": float64(1541152486013),
  1574. }, {
  1575. "color": "blue",
  1576. "ts": float64(1541152486822),
  1577. }},
  1578. {{
  1579. "color": "yellow",
  1580. "ts": float64(1541152488442),
  1581. }},
  1582. },
  1583. m: map[string]interface{}{
  1584. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1585. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1586. "op_preprocessor_demo_0_records_in_total": int64(5),
  1587. "op_preprocessor_demo_0_records_out_total": int64(5),
  1588. "op_project_0_exceptions_total": int64(0),
  1589. "op_project_0_process_latency_ms": int64(0),
  1590. "op_project_0_records_in_total": int64(2),
  1591. "op_project_0_records_out_total": int64(2),
  1592. "sink_mockSink_0_exceptions_total": int64(0),
  1593. "sink_mockSink_0_records_in_total": int64(2),
  1594. "sink_mockSink_0_records_out_total": int64(2),
  1595. "source_demo_0_exceptions_total": int64(0),
  1596. "source_demo_0_records_in_total": int64(5),
  1597. "source_demo_0_records_out_total": int64(5),
  1598. "op_window_0_exceptions_total": int64(0),
  1599. "op_window_0_process_latency_ms": int64(0),
  1600. "op_window_0_records_in_total": int64(5),
  1601. "op_window_0_records_out_total": int64(3),
  1602. "op_filter_0_exceptions_total": int64(0),
  1603. "op_filter_0_process_latency_ms": int64(0),
  1604. "op_filter_0_records_in_total": int64(3),
  1605. "op_filter_0_records_out_total": int64(2),
  1606. },
  1607. }, {
  1608. name: `rule3`,
  1609. sql: `SELECT color, temp, ts FROM demo INNER JOIN demo1 ON demo.ts = demo1.ts GROUP BY SlidingWindow(ss, 1)`,
  1610. size: 5,
  1611. r: [][]map[string]interface{}{
  1612. {{
  1613. "color": "red",
  1614. "temp": 25.5,
  1615. "ts": float64(1541152486013),
  1616. }}, {{
  1617. "color": "red",
  1618. "temp": 25.5,
  1619. "ts": float64(1541152486013),
  1620. }}, {{
  1621. "color": "red",
  1622. "temp": 25.5,
  1623. "ts": float64(1541152486013),
  1624. }}, {{
  1625. "color": "blue",
  1626. "temp": 28.1,
  1627. "ts": float64(1541152487632),
  1628. }}, {{
  1629. "color": "blue",
  1630. "temp": 28.1,
  1631. "ts": float64(1541152487632),
  1632. }}, {{
  1633. "color": "blue",
  1634. "temp": 28.1,
  1635. "ts": float64(1541152487632),
  1636. }, {
  1637. "color": "yellow",
  1638. "temp": 27.4,
  1639. "ts": float64(1541152488442),
  1640. }}, {{
  1641. "color": "yellow",
  1642. "temp": 27.4,
  1643. "ts": float64(1541152488442),
  1644. }}, {{
  1645. "color": "yellow",
  1646. "temp": 27.4,
  1647. "ts": float64(1541152488442),
  1648. }, {
  1649. "color": "red",
  1650. "temp": 25.5,
  1651. "ts": float64(1541152489252),
  1652. }},
  1653. },
  1654. m: map[string]interface{}{
  1655. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1656. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1657. "op_preprocessor_demo_0_records_in_total": int64(5),
  1658. "op_preprocessor_demo_0_records_out_total": int64(5),
  1659. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1660. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1661. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1662. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1663. "op_project_0_exceptions_total": int64(0),
  1664. "op_project_0_process_latency_ms": int64(0),
  1665. "op_project_0_records_in_total": int64(8),
  1666. "op_project_0_records_out_total": int64(8),
  1667. "sink_mockSink_0_exceptions_total": int64(0),
  1668. "sink_mockSink_0_records_in_total": int64(8),
  1669. "sink_mockSink_0_records_out_total": int64(8),
  1670. "source_demo_0_exceptions_total": int64(0),
  1671. "source_demo_0_records_in_total": int64(5),
  1672. "source_demo_0_records_out_total": int64(5),
  1673. "source_demo1_0_exceptions_total": int64(0),
  1674. "source_demo1_0_records_in_total": int64(5),
  1675. "source_demo1_0_records_out_total": int64(5),
  1676. "op_window_0_exceptions_total": int64(0),
  1677. "op_window_0_process_latency_ms": int64(0),
  1678. "op_window_0_records_in_total": int64(10),
  1679. "op_window_0_records_out_total": int64(10),
  1680. "op_join_0_exceptions_total": int64(0),
  1681. "op_join_0_process_latency_ms": int64(0),
  1682. "op_join_0_records_in_total": int64(10),
  1683. "op_join_0_records_out_total": int64(8),
  1684. },
  1685. }, {
  1686. name: `rule4`,
  1687. sql: `SELECT color FROM demo GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  1688. size: 5,
  1689. r: [][]map[string]interface{}{
  1690. {{
  1691. "color": "red",
  1692. }}, {{
  1693. "color": "blue",
  1694. }, {
  1695. "color": "red",
  1696. }}, {{
  1697. "color": "blue",
  1698. }, {
  1699. "color": "red",
  1700. }}, {{
  1701. "color": "blue",
  1702. }, {
  1703. "color": "yellow",
  1704. }}, {{
  1705. "color": "blue",
  1706. }, {
  1707. "color": "red",
  1708. }, {
  1709. "color": "yellow",
  1710. }},
  1711. },
  1712. m: map[string]interface{}{
  1713. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1714. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1715. "op_preprocessor_demo_0_records_in_total": int64(5),
  1716. "op_preprocessor_demo_0_records_out_total": int64(5),
  1717. "op_project_0_exceptions_total": int64(0),
  1718. "op_project_0_process_latency_ms": int64(0),
  1719. "op_project_0_records_in_total": int64(5),
  1720. "op_project_0_records_out_total": int64(5),
  1721. "sink_mockSink_0_exceptions_total": int64(0),
  1722. "sink_mockSink_0_records_in_total": int64(5),
  1723. "sink_mockSink_0_records_out_total": int64(5),
  1724. "source_demo_0_exceptions_total": int64(0),
  1725. "source_demo_0_records_in_total": int64(5),
  1726. "source_demo_0_records_out_total": int64(5),
  1727. "op_window_0_exceptions_total": int64(0),
  1728. "op_window_0_process_latency_ms": int64(0),
  1729. "op_window_0_records_in_total": int64(5),
  1730. "op_window_0_records_out_total": int64(5),
  1731. "op_aggregate_0_exceptions_total": int64(0),
  1732. "op_aggregate_0_process_latency_ms": int64(0),
  1733. "op_aggregate_0_records_in_total": int64(5),
  1734. "op_aggregate_0_records_out_total": int64(5),
  1735. "op_order_0_exceptions_total": int64(0),
  1736. "op_order_0_process_latency_ms": int64(0),
  1737. "op_order_0_records_in_total": int64(5),
  1738. "op_order_0_records_out_total": int64(5),
  1739. },
  1740. }, {
  1741. name: `rule5`,
  1742. sql: `SELECT temp FROM sessionDemo GROUP BY SessionWindow(ss, 2, 1) `,
  1743. size: 11,
  1744. r: [][]map[string]interface{}{
  1745. {{
  1746. "temp": 25.5,
  1747. }, {
  1748. "temp": 27.5,
  1749. }}, {{
  1750. "temp": 28.1,
  1751. }, {
  1752. "temp": 27.4,
  1753. }, {
  1754. "temp": 25.5,
  1755. }}, {{
  1756. "temp": 26.2,
  1757. }, {
  1758. "temp": 26.8,
  1759. }, {
  1760. "temp": 28.9,
  1761. }, {
  1762. "temp": 29.1,
  1763. }, {
  1764. "temp": 32.2,
  1765. }},
  1766. },
  1767. m: map[string]interface{}{
  1768. "op_preprocessor_sessionDemo_0_exceptions_total": int64(0),
  1769. "op_preprocessor_sessionDemo_0_process_latency_ms": int64(0),
  1770. "op_preprocessor_sessionDemo_0_records_in_total": int64(11),
  1771. "op_preprocessor_sessionDemo_0_records_out_total": int64(11),
  1772. "op_project_0_exceptions_total": int64(0),
  1773. "op_project_0_process_latency_ms": int64(0),
  1774. "op_project_0_records_in_total": int64(3),
  1775. "op_project_0_records_out_total": int64(3),
  1776. "sink_mockSink_0_exceptions_total": int64(0),
  1777. "sink_mockSink_0_records_in_total": int64(3),
  1778. "sink_mockSink_0_records_out_total": int64(3),
  1779. "source_sessionDemo_0_exceptions_total": int64(0),
  1780. "source_sessionDemo_0_records_in_total": int64(11),
  1781. "source_sessionDemo_0_records_out_total": int64(11),
  1782. "op_window_0_exceptions_total": int64(0),
  1783. "op_window_0_process_latency_ms": int64(0),
  1784. "op_window_0_records_in_total": int64(11),
  1785. "op_window_0_records_out_total": int64(3),
  1786. },
  1787. }, {
  1788. name: `rule6`,
  1789. 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)`,
  1790. size: 5,
  1791. r: [][]map[string]interface{}{
  1792. {{
  1793. "m": 25.5,
  1794. "c": float64(1),
  1795. }}, {{
  1796. "m": 25.5,
  1797. "c": float64(1),
  1798. }}, {{
  1799. "m": 25.5,
  1800. "c": float64(1),
  1801. }}, {{
  1802. "m": 28.1,
  1803. "c": float64(1),
  1804. }}, {{
  1805. "m": 28.1,
  1806. "c": float64(1),
  1807. }}, {{
  1808. "m": 28.1,
  1809. "c": float64(2),
  1810. }}, {{
  1811. "m": 27.4,
  1812. "c": float64(1),
  1813. }}, {{
  1814. "m": 27.4,
  1815. "c": float64(2),
  1816. }},
  1817. },
  1818. m: map[string]interface{}{
  1819. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1820. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1821. "op_preprocessor_demo_0_records_in_total": int64(5),
  1822. "op_preprocessor_demo_0_records_out_total": int64(5),
  1823. "op_preprocessor_demo1_0_exceptions_total": int64(0),
  1824. "op_preprocessor_demo1_0_process_latency_ms": int64(0),
  1825. "op_preprocessor_demo1_0_records_in_total": int64(5),
  1826. "op_preprocessor_demo1_0_records_out_total": int64(5),
  1827. "op_project_0_exceptions_total": int64(0),
  1828. "op_project_0_process_latency_ms": int64(0),
  1829. "op_project_0_records_in_total": int64(8),
  1830. "op_project_0_records_out_total": int64(8),
  1831. "sink_mockSink_0_exceptions_total": int64(0),
  1832. "sink_mockSink_0_records_in_total": int64(8),
  1833. "sink_mockSink_0_records_out_total": int64(8),
  1834. "source_demo_0_exceptions_total": int64(0),
  1835. "source_demo_0_records_in_total": int64(5),
  1836. "source_demo_0_records_out_total": int64(5),
  1837. "source_demo1_0_exceptions_total": int64(0),
  1838. "source_demo1_0_records_in_total": int64(5),
  1839. "source_demo1_0_records_out_total": int64(5),
  1840. "op_window_0_exceptions_total": int64(0),
  1841. "op_window_0_process_latency_ms": int64(0),
  1842. "op_window_0_records_in_total": int64(10),
  1843. "op_window_0_records_out_total": int64(10),
  1844. "op_join_0_exceptions_total": int64(0),
  1845. "op_join_0_process_latency_ms": int64(0),
  1846. "op_join_0_records_in_total": int64(10),
  1847. "op_join_0_records_out_total": int64(8),
  1848. },
  1849. }, {
  1850. name: `rule7`,
  1851. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  1852. size: 5,
  1853. r: [][]map[string]interface{}{
  1854. {{
  1855. "error": "error in preprocessor: invalid data type for color, expect string but found int(3)",
  1856. }},
  1857. {{
  1858. "color": "blue",
  1859. "size": float64(6),
  1860. "ts": float64(1541152486822),
  1861. }},
  1862. {{
  1863. "color": "blue",
  1864. "size": float64(6),
  1865. "ts": float64(1541152486822),
  1866. }, {
  1867. "color": "blue",
  1868. "size": float64(2),
  1869. "ts": float64(1541152487632),
  1870. }},
  1871. {{
  1872. "error": "error in preprocessor: invalid data type for color, expect string but found int(7)",
  1873. }},
  1874. {{
  1875. "color": "blue",
  1876. "size": float64(2),
  1877. "ts": float64(1541152487632),
  1878. }},
  1879. {{
  1880. "error": "error in preprocessor: invalid data type for size, expect bigint but found string(blue)",
  1881. }},
  1882. },
  1883. m: map[string]interface{}{
  1884. "op_preprocessor_demoE_0_exceptions_total": int64(3),
  1885. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  1886. "op_preprocessor_demoE_0_records_in_total": int64(5),
  1887. "op_preprocessor_demoE_0_records_out_total": int64(2),
  1888. "op_project_0_exceptions_total": int64(3),
  1889. "op_project_0_process_latency_ms": int64(0),
  1890. "op_project_0_records_in_total": int64(6),
  1891. "op_project_0_records_out_total": int64(3),
  1892. "sink_mockSink_0_exceptions_total": int64(0),
  1893. "sink_mockSink_0_records_in_total": int64(6),
  1894. "sink_mockSink_0_records_out_total": int64(6),
  1895. "source_demoE_0_exceptions_total": int64(0),
  1896. "source_demoE_0_records_in_total": int64(5),
  1897. "source_demoE_0_records_out_total": int64(5),
  1898. "op_window_0_exceptions_total": int64(3),
  1899. "op_window_0_process_latency_ms": int64(0),
  1900. "op_window_0_records_in_total": int64(5),
  1901. "op_window_0_records_out_total": int64(3),
  1902. },
  1903. }, {
  1904. name: `rule8`,
  1905. sql: `SELECT color, ts, count(*) as c FROM demo where size > 2 GROUP BY tumblingwindow(ss, 1) having c > 1`,
  1906. size: 5,
  1907. r: [][]map[string]interface{}{
  1908. {{
  1909. "color": "red",
  1910. "ts": float64(1541152486013),
  1911. "c": float64(2),
  1912. }},
  1913. },
  1914. m: map[string]interface{}{
  1915. "op_preprocessor_demo_0_exceptions_total": int64(0),
  1916. "op_preprocessor_demo_0_process_latency_ms": int64(0),
  1917. "op_preprocessor_demo_0_records_in_total": int64(5),
  1918. "op_preprocessor_demo_0_records_out_total": int64(5),
  1919. "op_project_0_exceptions_total": int64(0),
  1920. "op_project_0_process_latency_ms": int64(0),
  1921. "op_project_0_records_in_total": int64(1),
  1922. "op_project_0_records_out_total": int64(1),
  1923. "sink_mockSink_0_exceptions_total": int64(0),
  1924. "sink_mockSink_0_records_in_total": int64(1),
  1925. "sink_mockSink_0_records_out_total": int64(1),
  1926. "source_demo_0_exceptions_total": int64(0),
  1927. "source_demo_0_records_in_total": int64(5),
  1928. "source_demo_0_records_out_total": int64(5),
  1929. "op_window_0_exceptions_total": int64(0),
  1930. "op_window_0_process_latency_ms": int64(0),
  1931. "op_window_0_records_in_total": int64(5),
  1932. "op_window_0_records_out_total": int64(3),
  1933. "op_filter_0_exceptions_total": int64(0),
  1934. "op_filter_0_process_latency_ms": int64(0),
  1935. "op_filter_0_records_in_total": int64(3),
  1936. "op_filter_0_records_out_total": int64(2),
  1937. "op_aggregate_0_exceptions_total": int64(0),
  1938. "op_aggregate_0_process_latency_ms": int64(0),
  1939. "op_aggregate_0_records_in_total": int64(2),
  1940. "op_aggregate_0_records_out_total": int64(2),
  1941. "op_having_0_exceptions_total": int64(0),
  1942. "op_having_0_process_latency_ms": int64(0),
  1943. "op_having_0_records_in_total": int64(2),
  1944. "op_having_0_records_out_total": int64(1),
  1945. },
  1946. },
  1947. }
  1948. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  1949. createStreams(t)
  1950. defer dropStreams(t)
  1951. for i, tt := range tests {
  1952. test.ResetClock(1541152486000)
  1953. p := NewRuleProcessor(DbDir)
  1954. parser := xsql.NewParser(strings.NewReader(tt.sql))
  1955. var (
  1956. sources []*nodes.SourceNode
  1957. syncs []chan int
  1958. )
  1959. if stmt, err := xsql.Language.Parse(parser); err != nil {
  1960. t.Errorf("parse sql %s error: %s", tt.sql, err)
  1961. } else {
  1962. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  1963. t.Errorf("sql %s is not a select statement", tt.sql)
  1964. } else {
  1965. streams := xsql.GetStreams(selectStmt)
  1966. for _, stream := range streams {
  1967. next := make(chan int)
  1968. syncs = append(syncs, next)
  1969. source := getMockSource(stream, next, tt.size)
  1970. sources = append(sources, source)
  1971. }
  1972. }
  1973. }
  1974. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  1975. if err != nil {
  1976. t.Error(err)
  1977. }
  1978. mockSink := test.NewMockSink()
  1979. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  1980. tp.AddSink(inputs, sink)
  1981. errCh := tp.Open()
  1982. func() {
  1983. for i := 0; i < tt.size*len(syncs); i++ {
  1984. syncs[i%len(syncs)] <- i
  1985. for {
  1986. time.Sleep(1)
  1987. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  1988. break
  1989. }
  1990. }
  1991. select {
  1992. case err = <-errCh:
  1993. t.Log(err)
  1994. tp.Cancel()
  1995. return
  1996. default:
  1997. }
  1998. }
  1999. retry := 100
  2000. for ; retry > 0; retry-- {
  2001. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2002. break
  2003. }
  2004. t.Logf("wait to try another %d times", retry)
  2005. time.Sleep(time.Duration(retry) * time.Millisecond)
  2006. }
  2007. if retry == 0 {
  2008. err := compareMetrics(tp, tt.m, tt.sql)
  2009. t.Errorf("could not get correct metrics: %v", err)
  2010. }
  2011. }()
  2012. results := mockSink.GetResults()
  2013. var maps [][]map[string]interface{}
  2014. for _, v := range results {
  2015. var mapRes []map[string]interface{}
  2016. err := json.Unmarshal(v, &mapRes)
  2017. if err != nil {
  2018. t.Errorf("Failed to parse the input into map")
  2019. continue
  2020. }
  2021. maps = append(maps, mapRes)
  2022. }
  2023. if !reflect.DeepEqual(tt.r, maps) {
  2024. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  2025. }
  2026. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  2027. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  2028. }
  2029. tp.Cancel()
  2030. }
  2031. }
  2032. func TestWindowError(t *testing.T) {
  2033. var tests = []struct {
  2034. name string
  2035. sql string
  2036. size int
  2037. r [][]map[string]interface{}
  2038. m map[string]interface{}
  2039. }{
  2040. {
  2041. name: `rule1`,
  2042. sql: `SELECT size * 3 FROM ldemo GROUP BY TUMBLINGWINDOW(ss, 2)`,
  2043. size: 5,
  2044. r: [][]map[string]interface{}{
  2045. {{
  2046. "error": "run Select error: invalid operation string(string) * int64(3)",
  2047. }},
  2048. },
  2049. m: map[string]interface{}{
  2050. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2051. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2052. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2053. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2054. "op_project_0_exceptions_total": int64(1),
  2055. "op_project_0_process_latency_ms": int64(0),
  2056. "op_project_0_records_in_total": int64(1),
  2057. "op_project_0_records_out_total": int64(0),
  2058. "sink_mockSink_0_exceptions_total": int64(0),
  2059. "sink_mockSink_0_records_in_total": int64(1),
  2060. "sink_mockSink_0_records_out_total": int64(1),
  2061. "source_ldemo_0_exceptions_total": int64(0),
  2062. "source_ldemo_0_records_in_total": int64(5),
  2063. "source_ldemo_0_records_out_total": int64(5),
  2064. "op_window_0_exceptions_total": int64(0),
  2065. "op_window_0_process_latency_ms": int64(0),
  2066. "op_window_0_records_in_total": int64(5),
  2067. "op_window_0_records_out_total": int64(1),
  2068. },
  2069. }, {
  2070. name: `rule2`,
  2071. sql: `SELECT color, ts FROM ldemo where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  2072. size: 5,
  2073. r: [][]map[string]interface{}{
  2074. {{
  2075. "error": "run Where error: invalid operation string(string) > int64(2)",
  2076. }}, {{
  2077. "ts": float64(1541152487632),
  2078. }},
  2079. },
  2080. m: map[string]interface{}{
  2081. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2082. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2083. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2084. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2085. "op_project_0_exceptions_total": int64(1),
  2086. "op_project_0_process_latency_ms": int64(0),
  2087. "op_project_0_records_in_total": int64(2),
  2088. "op_project_0_records_out_total": int64(1),
  2089. "sink_mockSink_0_exceptions_total": int64(0),
  2090. "sink_mockSink_0_records_in_total": int64(2),
  2091. "sink_mockSink_0_records_out_total": int64(2),
  2092. "source_ldemo_0_exceptions_total": int64(0),
  2093. "source_ldemo_0_records_in_total": int64(5),
  2094. "source_ldemo_0_records_out_total": int64(5),
  2095. "op_window_0_exceptions_total": int64(0),
  2096. "op_window_0_process_latency_ms": int64(0),
  2097. "op_window_0_records_in_total": int64(5),
  2098. "op_window_0_records_out_total": int64(3),
  2099. "op_filter_0_exceptions_total": int64(1),
  2100. "op_filter_0_process_latency_ms": int64(0),
  2101. "op_filter_0_records_in_total": int64(3),
  2102. "op_filter_0_records_out_total": int64(1),
  2103. },
  2104. }, {
  2105. name: `rule3`,
  2106. sql: `SELECT color, temp, ts FROM ldemo INNER JOIN ldemo1 ON ldemo.ts = ldemo1.ts GROUP BY SlidingWindow(ss, 1)`,
  2107. size: 5,
  2108. r: [][]map[string]interface{}{
  2109. {{
  2110. "color": "red",
  2111. "temp": 25.5,
  2112. "ts": float64(1541152486013),
  2113. }}, {{
  2114. "color": "red",
  2115. "temp": 25.5,
  2116. "ts": float64(1541152486013),
  2117. }}, {{
  2118. "color": "red",
  2119. "temp": 25.5,
  2120. "ts": float64(1541152486013),
  2121. }}, {{
  2122. "temp": 28.1,
  2123. "ts": float64(1541152487632),
  2124. }}, {{
  2125. "temp": 28.1,
  2126. "ts": float64(1541152487632),
  2127. }}, {{
  2128. "error": "run Join error: invalid operation int64(1541152487632) = string(1541152488442)",
  2129. }}, {{
  2130. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  2131. }}, {{
  2132. "error": "run Join error: invalid operation int64(1541152488442) = string(1541152488442)",
  2133. }},
  2134. },
  2135. m: map[string]interface{}{
  2136. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2137. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2138. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2139. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2140. "op_preprocessor_ldemo1_0_exceptions_total": int64(0),
  2141. "op_preprocessor_ldemo1_0_process_latency_ms": int64(0),
  2142. "op_preprocessor_ldemo1_0_records_in_total": int64(5),
  2143. "op_preprocessor_ldemo1_0_records_out_total": int64(5),
  2144. "op_project_0_exceptions_total": int64(3),
  2145. "op_project_0_process_latency_ms": int64(0),
  2146. "op_project_0_records_in_total": int64(8),
  2147. "op_project_0_records_out_total": int64(5),
  2148. "sink_mockSink_0_exceptions_total": int64(0),
  2149. "sink_mockSink_0_records_in_total": int64(8),
  2150. "sink_mockSink_0_records_out_total": int64(8),
  2151. "source_ldemo_0_exceptions_total": int64(0),
  2152. "source_ldemo_0_records_in_total": int64(5),
  2153. "source_ldemo_0_records_out_total": int64(5),
  2154. "source_ldemo1_0_exceptions_total": int64(0),
  2155. "source_ldemo1_0_records_in_total": int64(5),
  2156. "source_ldemo1_0_records_out_total": int64(5),
  2157. "op_window_0_exceptions_total": int64(0),
  2158. "op_window_0_process_latency_ms": int64(0),
  2159. "op_window_0_records_in_total": int64(10),
  2160. "op_window_0_records_out_total": int64(10),
  2161. "op_join_0_exceptions_total": int64(3),
  2162. "op_join_0_process_latency_ms": int64(0),
  2163. "op_join_0_records_in_total": int64(10),
  2164. "op_join_0_records_out_total": int64(5),
  2165. },
  2166. }, {
  2167. name: `rule4`,
  2168. sql: `SELECT color FROM ldemo GROUP BY SlidingWindow(ss, 2), color having size >= 2 order by color`,
  2169. size: 5,
  2170. r: [][]map[string]interface{}{
  2171. {{
  2172. "color": "red",
  2173. }}, {{
  2174. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2175. }}, {{
  2176. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2177. }}, {{
  2178. "error": "run Having error: invalid operation string(string) >= int64(2)",
  2179. }}, {{
  2180. "color": float64(49),
  2181. }, {}},
  2182. },
  2183. m: map[string]interface{}{
  2184. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2185. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2186. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2187. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2188. "op_project_0_exceptions_total": int64(3),
  2189. "op_project_0_process_latency_ms": int64(0),
  2190. "op_project_0_records_in_total": int64(5),
  2191. "op_project_0_records_out_total": int64(2),
  2192. "sink_mockSink_0_exceptions_total": int64(0),
  2193. "sink_mockSink_0_records_in_total": int64(5),
  2194. "sink_mockSink_0_records_out_total": int64(5),
  2195. "source_ldemo_0_exceptions_total": int64(0),
  2196. "source_ldemo_0_records_in_total": int64(5),
  2197. "source_ldemo_0_records_out_total": int64(5),
  2198. "op_window_0_exceptions_total": int64(0),
  2199. "op_window_0_process_latency_ms": int64(0),
  2200. "op_window_0_records_in_total": int64(5),
  2201. "op_window_0_records_out_total": int64(5),
  2202. "op_aggregate_0_exceptions_total": int64(0),
  2203. "op_aggregate_0_process_latency_ms": int64(0),
  2204. "op_aggregate_0_records_in_total": int64(5),
  2205. "op_aggregate_0_records_out_total": int64(5),
  2206. "op_having_0_exceptions_total": int64(3),
  2207. "op_having_0_process_latency_ms": int64(0),
  2208. "op_having_0_records_in_total": int64(5),
  2209. "op_having_0_records_out_total": int64(2),
  2210. },
  2211. }, {
  2212. name: `rule5`,
  2213. sql: `SELECT color, size FROM ldemo GROUP BY tumblingwindow(ss, 1) ORDER BY size`,
  2214. size: 5,
  2215. r: [][]map[string]interface{}{
  2216. {{
  2217. "error": "run Order By error: incompatible types for comparison: int and string",
  2218. }}, {{
  2219. "size": float64(3),
  2220. }}, {{
  2221. "color": float64(49),
  2222. "size": float64(2),
  2223. }},
  2224. },
  2225. m: map[string]interface{}{
  2226. "op_preprocessor_ldemo_0_exceptions_total": int64(0),
  2227. "op_preprocessor_ldemo_0_process_latency_ms": int64(0),
  2228. "op_preprocessor_ldemo_0_records_in_total": int64(5),
  2229. "op_preprocessor_ldemo_0_records_out_total": int64(5),
  2230. "op_project_0_exceptions_total": int64(1),
  2231. "op_project_0_process_latency_ms": int64(0),
  2232. "op_project_0_records_in_total": int64(3),
  2233. "op_project_0_records_out_total": int64(2),
  2234. "sink_mockSink_0_exceptions_total": int64(0),
  2235. "sink_mockSink_0_records_in_total": int64(3),
  2236. "sink_mockSink_0_records_out_total": int64(3),
  2237. "source_ldemo_0_exceptions_total": int64(0),
  2238. "source_ldemo_0_records_in_total": int64(5),
  2239. "source_ldemo_0_records_out_total": int64(5),
  2240. "op_window_0_exceptions_total": int64(0),
  2241. "op_window_0_process_latency_ms": int64(0),
  2242. "op_window_0_records_in_total": int64(5),
  2243. "op_window_0_records_out_total": int64(3),
  2244. "op_order_0_exceptions_total": int64(1),
  2245. "op_order_0_process_latency_ms": int64(0),
  2246. "op_order_0_records_in_total": int64(3),
  2247. "op_order_0_records_out_total": int64(2),
  2248. },
  2249. },
  2250. }
  2251. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  2252. createSchemalessStreams(t)
  2253. defer dropSchemalessStreams(t)
  2254. for i, tt := range tests {
  2255. test.ResetClock(1541152486000)
  2256. p := NewRuleProcessor(DbDir)
  2257. parser := xsql.NewParser(strings.NewReader(tt.sql))
  2258. var (
  2259. sources []*nodes.SourceNode
  2260. syncs []chan int
  2261. )
  2262. if stmt, err := xsql.Language.Parse(parser); err != nil {
  2263. t.Errorf("parse sql %s error: %s", tt.sql, err)
  2264. } else {
  2265. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  2266. t.Errorf("sql %s is not a select statement", tt.sql)
  2267. } else {
  2268. streams := xsql.GetStreams(selectStmt)
  2269. for _, stream := range streams {
  2270. next := make(chan int)
  2271. syncs = append(syncs, next)
  2272. source := getMockSourceL(stream, next, tt.size)
  2273. sources = append(sources, source)
  2274. }
  2275. }
  2276. }
  2277. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: tt.name, Sql: tt.sql}, sources)
  2278. if err != nil {
  2279. t.Error(err)
  2280. }
  2281. mockSink := test.NewMockSink()
  2282. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  2283. tp.AddSink(inputs, sink)
  2284. errCh := tp.Open()
  2285. func() {
  2286. for i := 0; i < tt.size*len(syncs); i++ {
  2287. syncs[i%len(syncs)] <- i
  2288. for {
  2289. time.Sleep(1)
  2290. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  2291. break
  2292. }
  2293. }
  2294. select {
  2295. case err = <-errCh:
  2296. t.Log(err)
  2297. tp.Cancel()
  2298. return
  2299. default:
  2300. }
  2301. }
  2302. retry := 100
  2303. for ; retry > 0; retry-- {
  2304. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  2305. break
  2306. }
  2307. t.Logf("wait to try another %d times", retry)
  2308. time.Sleep(time.Duration(retry) * time.Millisecond)
  2309. }
  2310. if retry == 0 {
  2311. err := compareMetrics(tp, tt.m, tt.sql)
  2312. t.Errorf("could not get correct metrics: %v", err)
  2313. }
  2314. }()
  2315. results := mockSink.GetResults()
  2316. var maps [][]map[string]interface{}
  2317. for _, v := range results {
  2318. var mapRes []map[string]interface{}
  2319. err := json.Unmarshal(v, &mapRes)
  2320. if err != nil {
  2321. t.Errorf("Failed to parse the input into map")
  2322. continue
  2323. }
  2324. maps = append(maps, mapRes)
  2325. }
  2326. if !reflect.DeepEqual(tt.r, maps) {
  2327. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  2328. }
  2329. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  2330. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  2331. }
  2332. tp.Cancel()
  2333. }
  2334. }
  2335. func createEventStreams(t *testing.T) {
  2336. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  2337. demo := `CREATE STREAM demoE (
  2338. color STRING,
  2339. size BIGINT,
  2340. ts BIGINT
  2341. ) WITH (DATASOURCE="demoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2342. _, err := p.ExecStmt(demo)
  2343. if err != nil {
  2344. t.Log(err)
  2345. }
  2346. demo1 := `CREATE STREAM demo1E (
  2347. temp FLOAT,
  2348. hum BIGINT,
  2349. ts BIGINT
  2350. ) WITH (DATASOURCE="demo1E", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2351. _, err = p.ExecStmt(demo1)
  2352. if err != nil {
  2353. t.Log(err)
  2354. }
  2355. sessionDemo := `CREATE STREAM sessionDemoE (
  2356. temp FLOAT,
  2357. hum BIGINT,
  2358. ts BIGINT
  2359. ) WITH (DATASOURCE="sessionDemoE", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2360. _, err = p.ExecStmt(sessionDemo)
  2361. if err != nil {
  2362. t.Log(err)
  2363. }
  2364. demoErr := `CREATE STREAM demoErr (
  2365. color STRING,
  2366. size BIGINT,
  2367. ts BIGINT
  2368. ) WITH (DATASOURCE="demoErr", FORMAT="json", KEY="ts", TIMESTAMP="ts");`
  2369. _, err = p.ExecStmt(demoErr)
  2370. if err != nil {
  2371. t.Log(err)
  2372. }
  2373. }
  2374. func dropEventStreams(t *testing.T) {
  2375. p := NewStreamProcessor(path.Join(DbDir, "stream"))
  2376. demo := `DROP STREAM demoE`
  2377. _, err := p.ExecStmt(demo)
  2378. if err != nil {
  2379. t.Log(err)
  2380. }
  2381. demo1 := `DROP STREAM demo1E`
  2382. _, err = p.ExecStmt(demo1)
  2383. if err != nil {
  2384. t.Log(err)
  2385. }
  2386. sessionDemo := `DROP STREAM sessionDemoE`
  2387. _, err = p.ExecStmt(sessionDemo)
  2388. if err != nil {
  2389. t.Log(err)
  2390. }
  2391. demoErr := `DROP STREAM demoErr`
  2392. _, err = p.ExecStmt(demoErr)
  2393. if err != nil {
  2394. t.Log(err)
  2395. }
  2396. }
  2397. func getEventMockSource(name string, done <-chan int, size int) *nodes.SourceNode {
  2398. var data []*xsql.Tuple
  2399. switch name {
  2400. case "demoE":
  2401. data = []*xsql.Tuple{
  2402. {
  2403. Emitter: name,
  2404. Message: map[string]interface{}{
  2405. "color": "red",
  2406. "size": 3,
  2407. "ts": 1541152486013,
  2408. },
  2409. Timestamp: 1541152486013,
  2410. },
  2411. {
  2412. Emitter: name,
  2413. Message: map[string]interface{}{
  2414. "color": "blue",
  2415. "size": 2,
  2416. "ts": 1541152487632,
  2417. },
  2418. Timestamp: 1541152487632,
  2419. },
  2420. {
  2421. Emitter: name,
  2422. Message: map[string]interface{}{
  2423. "color": "red",
  2424. "size": 1,
  2425. "ts": 1541152489252,
  2426. },
  2427. Timestamp: 1541152489252,
  2428. },
  2429. { //dropped item
  2430. Emitter: name,
  2431. Message: map[string]interface{}{
  2432. "color": "blue",
  2433. "size": 6,
  2434. "ts": 1541152486822,
  2435. },
  2436. Timestamp: 1541152486822,
  2437. },
  2438. {
  2439. Emitter: name,
  2440. Message: map[string]interface{}{
  2441. "color": "yellow",
  2442. "size": 4,
  2443. "ts": 1541152488442,
  2444. },
  2445. Timestamp: 1541152488442,
  2446. },
  2447. { //To lift the watermark and issue all windows
  2448. Emitter: name,
  2449. Message: map[string]interface{}{
  2450. "color": "yellow",
  2451. "size": 4,
  2452. "ts": 1541152492342,
  2453. },
  2454. Timestamp: 1541152488442,
  2455. },
  2456. }
  2457. case "demo1E":
  2458. data = []*xsql.Tuple{
  2459. {
  2460. Emitter: name,
  2461. Message: map[string]interface{}{
  2462. "temp": 27.5,
  2463. "hum": 59,
  2464. "ts": 1541152486823,
  2465. },
  2466. Timestamp: 1541152486823,
  2467. },
  2468. {
  2469. Emitter: name,
  2470. Message: map[string]interface{}{
  2471. "temp": 25.5,
  2472. "hum": 65,
  2473. "ts": 1541152486013,
  2474. },
  2475. Timestamp: 1541152486013,
  2476. },
  2477. {
  2478. Emitter: name,
  2479. Message: map[string]interface{}{
  2480. "temp": 27.4,
  2481. "hum": 80,
  2482. "ts": 1541152488442,
  2483. },
  2484. Timestamp: 1541152488442,
  2485. },
  2486. {
  2487. Emitter: name,
  2488. Message: map[string]interface{}{
  2489. "temp": 28.1,
  2490. "hum": 75,
  2491. "ts": 1541152487632,
  2492. },
  2493. Timestamp: 1541152487632,
  2494. },
  2495. {
  2496. Emitter: name,
  2497. Message: map[string]interface{}{
  2498. "temp": 25.5,
  2499. "hum": 62,
  2500. "ts": 1541152489252,
  2501. },
  2502. Timestamp: 1541152489252,
  2503. },
  2504. {
  2505. Emitter: name,
  2506. Message: map[string]interface{}{
  2507. "temp": 25.5,
  2508. "hum": 62,
  2509. "ts": 1541152499252,
  2510. },
  2511. Timestamp: 1541152499252,
  2512. },
  2513. }
  2514. case "sessionDemoE":
  2515. data = []*xsql.Tuple{
  2516. {
  2517. Emitter: name,
  2518. Message: map[string]interface{}{
  2519. "temp": 25.5,
  2520. "hum": 65,
  2521. "ts": 1541152486013,
  2522. },
  2523. Timestamp: 1541152486013,
  2524. },
  2525. {
  2526. Emitter: name,
  2527. Message: map[string]interface{}{
  2528. "temp": 28.1,
  2529. "hum": 75,
  2530. "ts": 1541152487932,
  2531. },
  2532. Timestamp: 1541152487932,
  2533. },
  2534. {
  2535. Emitter: name,
  2536. Message: map[string]interface{}{
  2537. "temp": 27.5,
  2538. "hum": 59,
  2539. "ts": 1541152486823,
  2540. },
  2541. Timestamp: 1541152486823,
  2542. },
  2543. {
  2544. Emitter: name,
  2545. Message: map[string]interface{}{
  2546. "temp": 25.5,
  2547. "hum": 62,
  2548. "ts": 1541152489252,
  2549. },
  2550. Timestamp: 1541152489252,
  2551. },
  2552. {
  2553. Emitter: name,
  2554. Message: map[string]interface{}{
  2555. "temp": 27.4,
  2556. "hum": 80,
  2557. "ts": 1541152488442,
  2558. },
  2559. Timestamp: 1541152488442,
  2560. },
  2561. {
  2562. Emitter: name,
  2563. Message: map[string]interface{}{
  2564. "temp": 26.2,
  2565. "hum": 63,
  2566. "ts": 1541152490062,
  2567. },
  2568. Timestamp: 1541152490062,
  2569. },
  2570. {
  2571. Emitter: name,
  2572. Message: map[string]interface{}{
  2573. "temp": 28.9,
  2574. "hum": 85,
  2575. "ts": 1541152491682,
  2576. },
  2577. Timestamp: 1541152491682,
  2578. },
  2579. {
  2580. Emitter: name,
  2581. Message: map[string]interface{}{
  2582. "temp": 26.8,
  2583. "hum": 71,
  2584. "ts": 1541152490872,
  2585. },
  2586. Timestamp: 1541152490872,
  2587. },
  2588. {
  2589. Emitter: name,
  2590. Message: map[string]interface{}{
  2591. "temp": 29.1,
  2592. "hum": 92,
  2593. "ts": 1541152492492,
  2594. },
  2595. Timestamp: 1541152492492,
  2596. },
  2597. {
  2598. Emitter: name,
  2599. Message: map[string]interface{}{
  2600. "temp": 30.9,
  2601. "hum": 87,
  2602. "ts": 1541152494112,
  2603. },
  2604. Timestamp: 1541152494112,
  2605. },
  2606. {
  2607. Emitter: name,
  2608. Message: map[string]interface{}{
  2609. "temp": 32.2,
  2610. "hum": 99,
  2611. "ts": 1541152493202,
  2612. },
  2613. Timestamp: 1541152493202,
  2614. },
  2615. {
  2616. Emitter: name,
  2617. Message: map[string]interface{}{
  2618. "temp": 32.2,
  2619. "hum": 99,
  2620. "ts": 1541152499202,
  2621. },
  2622. Timestamp: 1541152499202,
  2623. },
  2624. }
  2625. case "demoErr":
  2626. data = []*xsql.Tuple{
  2627. {
  2628. Emitter: name,
  2629. Message: map[string]interface{}{
  2630. "color": "red",
  2631. "size": 3,
  2632. "ts": 1541152486013,
  2633. },
  2634. Timestamp: 1541152486013,
  2635. },
  2636. {
  2637. Emitter: name,
  2638. Message: map[string]interface{}{
  2639. "color": 2,
  2640. "size": "blue",
  2641. "ts": 1541152487632,
  2642. },
  2643. Timestamp: 1541152487632,
  2644. },
  2645. {
  2646. Emitter: name,
  2647. Message: map[string]interface{}{
  2648. "color": "red",
  2649. "size": 1,
  2650. "ts": 1541152489252,
  2651. },
  2652. Timestamp: 1541152489252,
  2653. },
  2654. { //dropped item
  2655. Emitter: name,
  2656. Message: map[string]interface{}{
  2657. "color": "blue",
  2658. "size": 6,
  2659. "ts": 1541152486822,
  2660. },
  2661. Timestamp: 1541152486822,
  2662. },
  2663. {
  2664. Emitter: name,
  2665. Message: map[string]interface{}{
  2666. "color": "yellow",
  2667. "size": 4,
  2668. "ts": 1541152488442,
  2669. },
  2670. Timestamp: 1541152488442,
  2671. },
  2672. { //To lift the watermark and issue all windows
  2673. Emitter: name,
  2674. Message: map[string]interface{}{
  2675. "color": "yellow",
  2676. "size": 4,
  2677. "ts": 1541152492342,
  2678. },
  2679. Timestamp: 1541152488442,
  2680. },
  2681. }
  2682. }
  2683. return nodes.NewSourceNodeWithSource(name, test.NewMockSource(data[:size], done, true), map[string]string{
  2684. "DATASOURCE": name,
  2685. })
  2686. }
  2687. func TestEventWindow(t *testing.T) {
  2688. var tests = []struct {
  2689. name string
  2690. sql string
  2691. size int
  2692. r [][]map[string]interface{}
  2693. m map[string]interface{}
  2694. }{
  2695. {
  2696. name: `rule1`,
  2697. sql: `SELECT * FROM demoE GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  2698. size: 6,
  2699. r: [][]map[string]interface{}{
  2700. {{
  2701. "color": "red",
  2702. "size": float64(3),
  2703. "ts": float64(1541152486013),
  2704. }},
  2705. {{
  2706. "color": "red",
  2707. "size": float64(3),
  2708. "ts": float64(1541152486013),
  2709. }, {
  2710. "color": "blue",
  2711. "size": float64(2),
  2712. "ts": float64(1541152487632),
  2713. }},
  2714. {{
  2715. "color": "blue",
  2716. "size": float64(2),
  2717. "ts": float64(1541152487632),
  2718. }, {
  2719. "color": "yellow",
  2720. "size": float64(4),
  2721. "ts": float64(1541152488442),
  2722. }}, {{
  2723. "color": "yellow",
  2724. "size": float64(4),
  2725. "ts": float64(1541152488442),
  2726. }, {
  2727. "color": "red",
  2728. "size": float64(1),
  2729. "ts": float64(1541152489252),
  2730. }}, {{
  2731. "color": "red",
  2732. "size": float64(1),
  2733. "ts": float64(1541152489252),
  2734. }},
  2735. },
  2736. m: map[string]interface{}{
  2737. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2738. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2739. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2740. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2741. "op_project_0_exceptions_total": int64(0),
  2742. "op_project_0_process_latency_ms": int64(0),
  2743. "op_project_0_records_in_total": int64(5),
  2744. "op_project_0_records_out_total": int64(5),
  2745. "sink_mockSink_0_exceptions_total": int64(0),
  2746. "sink_mockSink_0_records_in_total": int64(5),
  2747. "sink_mockSink_0_records_out_total": int64(5),
  2748. "source_demoE_0_exceptions_total": int64(0),
  2749. "source_demoE_0_records_in_total": int64(6),
  2750. "source_demoE_0_records_out_total": int64(6),
  2751. "op_window_0_exceptions_total": int64(0),
  2752. "op_window_0_process_latency_ms": int64(0),
  2753. "op_window_0_records_in_total": int64(6),
  2754. "op_window_0_records_out_total": int64(5),
  2755. },
  2756. }, {
  2757. name: `rule2`,
  2758. sql: `SELECT color, ts FROM demoE where size > 2 GROUP BY tumblingwindow(ss, 1)`,
  2759. size: 6,
  2760. r: [][]map[string]interface{}{
  2761. {{
  2762. "color": "red",
  2763. "ts": float64(1541152486013),
  2764. }},
  2765. {{
  2766. "color": "yellow",
  2767. "ts": float64(1541152488442),
  2768. }},
  2769. },
  2770. m: map[string]interface{}{
  2771. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2772. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2773. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2774. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2775. "op_project_0_exceptions_total": int64(0),
  2776. "op_project_0_process_latency_ms": int64(0),
  2777. "op_project_0_records_in_total": int64(2),
  2778. "op_project_0_records_out_total": int64(2),
  2779. "sink_mockSink_0_exceptions_total": int64(0),
  2780. "sink_mockSink_0_records_in_total": int64(2),
  2781. "sink_mockSink_0_records_out_total": int64(2),
  2782. "source_demoE_0_exceptions_total": int64(0),
  2783. "source_demoE_0_records_in_total": int64(6),
  2784. "source_demoE_0_records_out_total": int64(6),
  2785. "op_window_0_exceptions_total": int64(0),
  2786. "op_window_0_process_latency_ms": int64(0),
  2787. "op_window_0_records_in_total": int64(6),
  2788. "op_window_0_records_out_total": int64(4),
  2789. "op_filter_0_exceptions_total": int64(0),
  2790. "op_filter_0_process_latency_ms": int64(0),
  2791. "op_filter_0_records_in_total": int64(4),
  2792. "op_filter_0_records_out_total": int64(2),
  2793. },
  2794. }, {
  2795. name: `rule3`,
  2796. sql: `SELECT color, temp, ts FROM demoE INNER JOIN demo1E ON demoE.ts = demo1E.ts GROUP BY SlidingWindow(ss, 1)`,
  2797. size: 6,
  2798. r: [][]map[string]interface{}{
  2799. {{
  2800. "color": "red",
  2801. "temp": 25.5,
  2802. "ts": float64(1541152486013),
  2803. }}, {{
  2804. "color": "red",
  2805. "temp": 25.5,
  2806. "ts": float64(1541152486013),
  2807. }}, {{
  2808. "color": "blue",
  2809. "temp": 28.1,
  2810. "ts": float64(1541152487632),
  2811. }}, {{
  2812. "color": "blue",
  2813. "temp": 28.1,
  2814. "ts": float64(1541152487632),
  2815. }, {
  2816. "color": "yellow",
  2817. "temp": 27.4,
  2818. "ts": float64(1541152488442),
  2819. }}, {{
  2820. "color": "yellow",
  2821. "temp": 27.4,
  2822. "ts": float64(1541152488442),
  2823. }, {
  2824. "color": "red",
  2825. "temp": 25.5,
  2826. "ts": float64(1541152489252),
  2827. }},
  2828. },
  2829. m: map[string]interface{}{
  2830. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2831. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2832. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2833. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2834. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2835. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2836. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2837. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2838. "op_project_0_exceptions_total": int64(0),
  2839. "op_project_0_process_latency_ms": int64(0),
  2840. "op_project_0_records_in_total": int64(5),
  2841. "op_project_0_records_out_total": int64(5),
  2842. "sink_mockSink_0_exceptions_total": int64(0),
  2843. "sink_mockSink_0_records_in_total": int64(5),
  2844. "sink_mockSink_0_records_out_total": int64(5),
  2845. "source_demoE_0_exceptions_total": int64(0),
  2846. "source_demoE_0_records_in_total": int64(6),
  2847. "source_demoE_0_records_out_total": int64(6),
  2848. "source_demo1E_0_exceptions_total": int64(0),
  2849. "source_demo1E_0_records_in_total": int64(6),
  2850. "source_demo1E_0_records_out_total": int64(6),
  2851. "op_window_0_exceptions_total": int64(0),
  2852. "op_window_0_process_latency_ms": int64(0),
  2853. "op_window_0_records_in_total": int64(12),
  2854. "op_window_0_records_out_total": int64(5),
  2855. "op_join_0_exceptions_total": int64(0),
  2856. "op_join_0_process_latency_ms": int64(0),
  2857. "op_join_0_records_in_total": int64(5),
  2858. "op_join_0_records_out_total": int64(5),
  2859. },
  2860. }, {
  2861. name: `rule4`,
  2862. sql: `SELECT color FROM demoE GROUP BY SlidingWindow(ss, 2), color ORDER BY color`,
  2863. size: 6,
  2864. r: [][]map[string]interface{}{
  2865. {{
  2866. "color": "red",
  2867. }}, {{
  2868. "color": "blue",
  2869. }, {
  2870. "color": "red",
  2871. }}, {{
  2872. "color": "blue",
  2873. }, {
  2874. "color": "yellow",
  2875. }}, {{
  2876. "color": "blue",
  2877. }, {
  2878. "color": "red",
  2879. }, {
  2880. "color": "yellow",
  2881. }},
  2882. },
  2883. m: map[string]interface{}{
  2884. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2885. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2886. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2887. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2888. "op_project_0_exceptions_total": int64(0),
  2889. "op_project_0_process_latency_ms": int64(0),
  2890. "op_project_0_records_in_total": int64(4),
  2891. "op_project_0_records_out_total": int64(4),
  2892. "sink_mockSink_0_exceptions_total": int64(0),
  2893. "sink_mockSink_0_records_in_total": int64(4),
  2894. "sink_mockSink_0_records_out_total": int64(4),
  2895. "source_demoE_0_exceptions_total": int64(0),
  2896. "source_demoE_0_records_in_total": int64(6),
  2897. "source_demoE_0_records_out_total": int64(6),
  2898. "op_window_0_exceptions_total": int64(0),
  2899. "op_window_0_process_latency_ms": int64(0),
  2900. "op_window_0_records_in_total": int64(6),
  2901. "op_window_0_records_out_total": int64(4),
  2902. "op_aggregate_0_exceptions_total": int64(0),
  2903. "op_aggregate_0_process_latency_ms": int64(0),
  2904. "op_aggregate_0_records_in_total": int64(4),
  2905. "op_aggregate_0_records_out_total": int64(4),
  2906. "op_order_0_exceptions_total": int64(0),
  2907. "op_order_0_process_latency_ms": int64(0),
  2908. "op_order_0_records_in_total": int64(4),
  2909. "op_order_0_records_out_total": int64(4),
  2910. },
  2911. }, {
  2912. name: `rule5`,
  2913. sql: `SELECT temp FROM sessionDemoE GROUP BY SessionWindow(ss, 2, 1) `,
  2914. size: 12,
  2915. r: [][]map[string]interface{}{
  2916. {{
  2917. "temp": 25.5,
  2918. }}, {{
  2919. "temp": 28.1,
  2920. }, {
  2921. "temp": 27.4,
  2922. }, {
  2923. "temp": 25.5,
  2924. }}, {{
  2925. "temp": 26.2,
  2926. }, {
  2927. "temp": 26.8,
  2928. }, {
  2929. "temp": 28.9,
  2930. }, {
  2931. "temp": 29.1,
  2932. }, {
  2933. "temp": 32.2,
  2934. }}, {{
  2935. "temp": 30.9,
  2936. }},
  2937. },
  2938. m: map[string]interface{}{
  2939. "op_preprocessor_sessionDemoE_0_exceptions_total": int64(0),
  2940. "op_preprocessor_sessionDemoE_0_process_latency_ms": int64(0),
  2941. "op_preprocessor_sessionDemoE_0_records_in_total": int64(12),
  2942. "op_preprocessor_sessionDemoE_0_records_out_total": int64(12),
  2943. "op_project_0_exceptions_total": int64(0),
  2944. "op_project_0_process_latency_ms": int64(0),
  2945. "op_project_0_records_in_total": int64(4),
  2946. "op_project_0_records_out_total": int64(4),
  2947. "sink_mockSink_0_exceptions_total": int64(0),
  2948. "sink_mockSink_0_records_in_total": int64(4),
  2949. "sink_mockSink_0_records_out_total": int64(4),
  2950. "source_sessionDemoE_0_exceptions_total": int64(0),
  2951. "source_sessionDemoE_0_records_in_total": int64(12),
  2952. "source_sessionDemoE_0_records_out_total": int64(12),
  2953. "op_window_0_exceptions_total": int64(0),
  2954. "op_window_0_process_latency_ms": int64(0),
  2955. "op_window_0_records_in_total": int64(12),
  2956. "op_window_0_records_out_total": int64(4),
  2957. },
  2958. }, {
  2959. name: `rule6`,
  2960. 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)`,
  2961. size: 6,
  2962. r: [][]map[string]interface{}{
  2963. {{
  2964. "m": 25.5,
  2965. "c": float64(1),
  2966. }}, {{
  2967. "m": 25.5,
  2968. "c": float64(1),
  2969. }}, {{
  2970. "m": 28.1,
  2971. "c": float64(1),
  2972. }}, {{
  2973. "m": 28.1,
  2974. "c": float64(2),
  2975. }}, {{
  2976. "m": 27.4,
  2977. "c": float64(2),
  2978. }},
  2979. },
  2980. m: map[string]interface{}{
  2981. "op_preprocessor_demoE_0_exceptions_total": int64(0),
  2982. "op_preprocessor_demoE_0_process_latency_ms": int64(0),
  2983. "op_preprocessor_demoE_0_records_in_total": int64(6),
  2984. "op_preprocessor_demoE_0_records_out_total": int64(6),
  2985. "op_preprocessor_demo1E_0_exceptions_total": int64(0),
  2986. "op_preprocessor_demo1E_0_process_latency_ms": int64(0),
  2987. "op_preprocessor_demo1E_0_records_in_total": int64(6),
  2988. "op_preprocessor_demo1E_0_records_out_total": int64(6),
  2989. "op_project_0_exceptions_total": int64(0),
  2990. "op_project_0_process_latency_ms": int64(0),
  2991. "op_project_0_records_in_total": int64(5),
  2992. "op_project_0_records_out_total": int64(5),
  2993. "sink_mockSink_0_exceptions_total": int64(0),
  2994. "sink_mockSink_0_records_in_total": int64(5),
  2995. "sink_mockSink_0_records_out_total": int64(5),
  2996. "source_demoE_0_exceptions_total": int64(0),
  2997. "source_demoE_0_records_in_total": int64(6),
  2998. "source_demoE_0_records_out_total": int64(6),
  2999. "source_demo1E_0_exceptions_total": int64(0),
  3000. "source_demo1E_0_records_in_total": int64(6),
  3001. "source_demo1E_0_records_out_total": int64(6),
  3002. "op_window_0_exceptions_total": int64(0),
  3003. "op_window_0_records_in_total": int64(12),
  3004. "op_window_0_records_out_total": int64(5),
  3005. "op_join_0_exceptions_total": int64(0),
  3006. "op_join_0_process_latency_ms": int64(0),
  3007. "op_join_0_records_in_total": int64(5),
  3008. "op_join_0_records_out_total": int64(5),
  3009. },
  3010. }, {
  3011. name: `rule7`,
  3012. sql: `SELECT * FROM demoErr GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  3013. size: 6,
  3014. r: [][]map[string]interface{}{
  3015. {{
  3016. "error": "error in preprocessor: invalid data type for color, expect string but found int(2)",
  3017. }},
  3018. {{
  3019. "color": "red",
  3020. "size": float64(3),
  3021. "ts": float64(1541152486013),
  3022. }},
  3023. {{
  3024. "color": "red",
  3025. "size": float64(3),
  3026. "ts": float64(1541152486013),
  3027. }},
  3028. {{
  3029. "color": "yellow",
  3030. "size": float64(4),
  3031. "ts": float64(1541152488442),
  3032. }}, {{
  3033. "color": "yellow",
  3034. "size": float64(4),
  3035. "ts": float64(1541152488442),
  3036. }, {
  3037. "color": "red",
  3038. "size": float64(1),
  3039. "ts": float64(1541152489252),
  3040. }}, {{
  3041. "color": "red",
  3042. "size": float64(1),
  3043. "ts": float64(1541152489252),
  3044. }},
  3045. },
  3046. m: map[string]interface{}{
  3047. "op_preprocessor_demoErr_0_exceptions_total": int64(1),
  3048. "op_preprocessor_demoErr_0_process_latency_ms": int64(0),
  3049. "op_preprocessor_demoErr_0_records_in_total": int64(6),
  3050. "op_preprocessor_demoErr_0_records_out_total": int64(5),
  3051. "op_project_0_exceptions_total": int64(1),
  3052. "op_project_0_process_latency_ms": int64(0),
  3053. "op_project_0_records_in_total": int64(6),
  3054. "op_project_0_records_out_total": int64(5),
  3055. "sink_mockSink_0_exceptions_total": int64(0),
  3056. "sink_mockSink_0_records_in_total": int64(6),
  3057. "sink_mockSink_0_records_out_total": int64(6),
  3058. "source_demoErr_0_exceptions_total": int64(0),
  3059. "source_demoErr_0_records_in_total": int64(6),
  3060. "source_demoErr_0_records_out_total": int64(6),
  3061. "op_window_0_exceptions_total": int64(1),
  3062. "op_window_0_process_latency_ms": int64(0),
  3063. "op_window_0_records_in_total": int64(6),
  3064. "op_window_0_records_out_total": int64(5),
  3065. },
  3066. },
  3067. }
  3068. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  3069. createEventStreams(t)
  3070. defer dropEventStreams(t)
  3071. for i, tt := range tests {
  3072. test.ResetClock(1541152486000)
  3073. p := NewRuleProcessor(DbDir)
  3074. parser := xsql.NewParser(strings.NewReader(tt.sql))
  3075. var (
  3076. sources []*nodes.SourceNode
  3077. syncs []chan int
  3078. )
  3079. if stmt, err := xsql.Language.Parse(parser); err != nil {
  3080. t.Errorf("parse sql %s error: %s", tt.sql, err)
  3081. } else {
  3082. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  3083. t.Errorf("sql %s is not a select statement", tt.sql)
  3084. } else {
  3085. streams := xsql.GetStreams(selectStmt)
  3086. for _, stream := range streams {
  3087. next := make(chan int)
  3088. syncs = append(syncs, next)
  3089. source := getEventMockSource(stream, next, tt.size)
  3090. sources = append(sources, source)
  3091. }
  3092. }
  3093. }
  3094. tp, inputs, err := p.createTopoWithSources(&api.Rule{
  3095. Id: tt.name, Sql: tt.sql,
  3096. Options: map[string]interface{}{
  3097. "isEventTime": true,
  3098. "lateTolerance": float64(1000),
  3099. },
  3100. }, sources)
  3101. if err != nil {
  3102. t.Error(err)
  3103. }
  3104. mockSink := test.NewMockSink()
  3105. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  3106. tp.AddSink(inputs, sink)
  3107. errCh := tp.Open()
  3108. func() {
  3109. for i := 0; i < tt.size*len(syncs); i++ {
  3110. syncs[i%len(syncs)] <- i
  3111. for {
  3112. time.Sleep(1)
  3113. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  3114. break
  3115. }
  3116. }
  3117. select {
  3118. case err = <-errCh:
  3119. t.Log(err)
  3120. tp.Cancel()
  3121. return
  3122. default:
  3123. }
  3124. }
  3125. mockClock := test.GetMockClock()
  3126. mockClock.Add(1000 * time.Millisecond)
  3127. retry := 100
  3128. for ; retry > 0; retry-- {
  3129. if err := compareMetrics(tp, tt.m, tt.sql); err == nil {
  3130. break
  3131. }
  3132. t.Logf("wait to try another %d times", retry)
  3133. time.Sleep(time.Duration(retry) * time.Millisecond)
  3134. }
  3135. if retry == 0 {
  3136. err := compareMetrics(tp, tt.m, tt.sql)
  3137. t.Errorf("could not get correct metrics: %v", err)
  3138. }
  3139. }()
  3140. results := mockSink.GetResults()
  3141. var maps [][]map[string]interface{}
  3142. for _, v := range results {
  3143. var mapRes []map[string]interface{}
  3144. err := json.Unmarshal(v, &mapRes)
  3145. if err != nil {
  3146. t.Errorf("Failed to parse the input into map")
  3147. continue
  3148. }
  3149. maps = append(maps, mapRes)
  3150. }
  3151. if !reflect.DeepEqual(tt.r, maps) {
  3152. t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  3153. }
  3154. if err := compareMetrics(tp, tt.m, tt.sql); err != nil {
  3155. t.Errorf("%d. %q\n\n%v", i, tt.sql, err)
  3156. }
  3157. tp.Cancel()
  3158. }
  3159. }
  3160. func getMetric(tp *xstream.TopologyNew, name string) int {
  3161. keys, values := tp.GetMetrics()
  3162. for index, key := range keys {
  3163. if key == name {
  3164. return int(values[index].(int64))
  3165. }
  3166. }
  3167. fmt.Println("can't find " + name)
  3168. return 0
  3169. }
  3170. func compareMetrics(tp *xstream.TopologyNew, m map[string]interface{}, sql string) (err error) {
  3171. keys, values := tp.GetMetrics()
  3172. //for i, k := range keys {
  3173. // log.Printf("%s:%v", k, values[i])
  3174. //}
  3175. for k, v := range m {
  3176. var (
  3177. index int
  3178. key string
  3179. matched bool
  3180. )
  3181. for index, key = range keys {
  3182. if k == key {
  3183. if strings.HasSuffix(k, "process_latency_ms") {
  3184. if values[index].(int64) >= v.(int64) {
  3185. matched = true
  3186. continue
  3187. } else {
  3188. break
  3189. }
  3190. }
  3191. if values[index] == v {
  3192. matched = true
  3193. }
  3194. break
  3195. }
  3196. }
  3197. if matched {
  3198. continue
  3199. }
  3200. //do not find
  3201. if index < len(values) {
  3202. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v(%t)\n\ngot=%#v(%t)\n\n", k, v, v, values[index], values[index])
  3203. } else {
  3204. return fmt.Errorf("metrics mismatch for %s:\n\nexp=%#v\n\ngot=nil\n\n", k, v)
  3205. }
  3206. }
  3207. return nil
  3208. }
  3209. func errstring(err error) string {
  3210. if err != nil {
  3211. return err.Error()
  3212. }
  3213. return ""
  3214. }