xsql_processor_test.go 92 KB

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