xsql_processor_test.go 92 KB

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