xsql_processor_test.go 75 KB

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