xsql_processor_test.go 75 KB

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