xsql_processor.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. package processors
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "engine/common"
  6. "engine/xsql"
  7. "engine/xsql/plans"
  8. "engine/xstream"
  9. "engine/xstream/api"
  10. "engine/xstream/extensions"
  11. "engine/xstream/nodes"
  12. "engine/xstream/operators"
  13. "engine/xstream/sinks"
  14. "fmt"
  15. "github.com/dgraph-io/badger"
  16. "github.com/go-yaml/yaml"
  17. "path"
  18. "plugin"
  19. "strings"
  20. "unicode"
  21. )
  22. var log = common.Log
  23. type StreamProcessor struct {
  24. statement string
  25. badgerDir string
  26. }
  27. //@params s : the sql string of create stream statement
  28. //@params d : the directory of the badger DB to save the stream info
  29. func NewStreamProcessor(s, d string) *StreamProcessor {
  30. processor := &StreamProcessor{
  31. statement: s,
  32. badgerDir: d,
  33. }
  34. return processor
  35. }
  36. func (p *StreamProcessor) Exec() (result []string, err error) {
  37. parser := xsql.NewParser(strings.NewReader(p.statement))
  38. stmt, err := xsql.Language.Parse(parser)
  39. if err != nil {
  40. return
  41. }
  42. db, err := common.DbOpen(p.badgerDir)
  43. if err != nil {
  44. return
  45. }
  46. defer common.DbClose(db)
  47. switch s := stmt.(type) {
  48. case *xsql.StreamStmt:
  49. var r string
  50. r, err = p.execCreateStream(s, db)
  51. result = append(result, r)
  52. case *xsql.ShowStreamsStatement:
  53. result, err = p.execShowStream(s, db)
  54. case *xsql.DescribeStreamStatement:
  55. var r string
  56. r, err = p.execDescribeStream(s, db)
  57. result = append(result, r)
  58. case *xsql.ExplainStreamStatement:
  59. var r string
  60. r, err = p.execExplainStream(s, db)
  61. result = append(result, r)
  62. case *xsql.DropStreamStatement:
  63. var r string
  64. r, err = p.execDropStream(s, db)
  65. result = append(result, r)
  66. }
  67. return
  68. }
  69. func (p *StreamProcessor) execCreateStream(stmt *xsql.StreamStmt, db *badger.DB) (string, error) {
  70. err := common.DbSet(db, string(stmt.Name), p.statement)
  71. if err != nil {
  72. return "", err
  73. }else{
  74. return fmt.Sprintf("Stream %s is created.", stmt.Name), nil
  75. }
  76. }
  77. func (p *StreamProcessor) execShowStream(stmt *xsql.ShowStreamsStatement, db *badger.DB) ([]string,error) {
  78. keys, err := common.DbKeys(db)
  79. if len(keys) == 0 {
  80. keys = append(keys, "No stream definitions are found.")
  81. }
  82. return keys, err
  83. }
  84. func (p *StreamProcessor) execDescribeStream(stmt *xsql.DescribeStreamStatement, db *badger.DB) (string,error) {
  85. s, err := common.DbGet(db, string(stmt.Name))
  86. if err != nil {
  87. return "", fmt.Errorf("Stream %s is not found.", stmt.Name)
  88. }
  89. parser := xsql.NewParser(strings.NewReader(s))
  90. stream, err := xsql.Language.Parse(parser)
  91. streamStmt, ok := stream.(*xsql.StreamStmt)
  92. if !ok{
  93. return "", fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", stmt.Name)
  94. }
  95. var buff bytes.Buffer
  96. buff.WriteString("Fields\n--------------------------------------------------------------------------------\n")
  97. for _, f := range streamStmt.StreamFields {
  98. buff.WriteString(f.Name + "\t")
  99. xsql.PrintFieldType(f.FieldType, &buff)
  100. buff.WriteString("\n")
  101. }
  102. buff.WriteString("\n")
  103. common.PrintMap(streamStmt.Options, &buff)
  104. return buff.String(), err
  105. }
  106. func (p *StreamProcessor) execExplainStream(stmt *xsql.ExplainStreamStatement, db *badger.DB) (string,error) {
  107. _, err := common.DbGet(db, string(stmt.Name))
  108. if err != nil{
  109. return "", fmt.Errorf("Stream %s is not found.", stmt.Name)
  110. }
  111. return "TO BE SUPPORTED", nil
  112. }
  113. func (p *StreamProcessor) execDropStream(stmt *xsql.DropStreamStatement, db *badger.DB) (string, error) {
  114. err := common.DbDelete(db, string(stmt.Name))
  115. if err != nil {
  116. return "", err
  117. }else{
  118. return fmt.Sprintf("Stream %s is dropped.", stmt.Name), nil
  119. }
  120. }
  121. func GetStream(db *badger.DB, name string) (stmt *xsql.StreamStmt, err error){
  122. s, err := common.DbGet(db, name)
  123. if err != nil {
  124. return
  125. }
  126. parser := xsql.NewParser(strings.NewReader(s))
  127. stream, err := xsql.Language.Parse(parser)
  128. stmt, ok := stream.(*xsql.StreamStmt)
  129. if !ok{
  130. err = fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", name)
  131. }
  132. return
  133. }
  134. type RuleProcessor struct {
  135. badgerDir string
  136. }
  137. func NewRuleProcessor(d string) *RuleProcessor {
  138. processor := &RuleProcessor{
  139. badgerDir: d,
  140. }
  141. return processor
  142. }
  143. func (p *RuleProcessor) ExecCreate(name, ruleJson string) (*api.Rule, error) {
  144. rule, err := p.getRuleByJson(name, ruleJson)
  145. if err != nil {
  146. return nil, err
  147. }
  148. db, err := common.DbOpen(path.Join(p.badgerDir, "rule"))
  149. if err != nil {
  150. return nil, err
  151. }
  152. err = common.DbSet(db, string(name), ruleJson)
  153. if err != nil {
  154. common.DbClose(db)
  155. return nil, err
  156. }else{
  157. log.Infof("Rule %s is created.", name)
  158. common.DbClose(db)
  159. }
  160. return rule, nil
  161. }
  162. func (p *RuleProcessor) GetRuleByName(name string) (*api.Rule, error) {
  163. db, err := common.DbOpen(path.Join(p.badgerDir, "rule"))
  164. if err != nil {
  165. return nil, err
  166. }
  167. defer common.DbClose(db)
  168. s, err := common.DbGet(db, string(name))
  169. if err != nil {
  170. return nil, fmt.Errorf("Rule %s is not found.", name)
  171. }
  172. return p.getRuleByJson(name, s)
  173. }
  174. func (p *RuleProcessor) getRuleByJson(name, ruleJson string) (*api.Rule, error) {
  175. var rule api.Rule
  176. if err := json.Unmarshal([]byte(ruleJson), &rule); err != nil {
  177. return nil, fmt.Errorf("Parse rule %s error : %s.", ruleJson, err)
  178. }
  179. rule.Id = name
  180. //validation
  181. if name == ""{
  182. return nil, fmt.Errorf("Missing rule id.")
  183. }
  184. if rule.Sql == ""{
  185. return nil, fmt.Errorf("Missing rule SQL.")
  186. }
  187. if rule.Actions == nil || len(rule.Actions) == 0{
  188. return nil, fmt.Errorf("Missing rule actions.")
  189. }
  190. return &rule, nil
  191. }
  192. func (p *RuleProcessor) ExecInitRule(rule *api.Rule) (*xstream.TopologyNew, error) {
  193. if tp, inputs, err := p.createTopo(rule); err != nil {
  194. return nil, err
  195. }else{
  196. for _, m := range rule.Actions {
  197. for name, action := range m {
  198. props, ok := action.(map[string]interface{})
  199. if !ok {
  200. return nil, fmt.Errorf("expect map[string]interface{} type for the action properties, but found %v", action)
  201. }
  202. if s, err := getSink(name, props); err != nil{
  203. return nil, err
  204. }else{
  205. tp.AddSink(inputs, nodes.NewSinkNode("sink_" + name, s))
  206. }
  207. }
  208. }
  209. return tp, nil
  210. }
  211. }
  212. func (p *RuleProcessor) ExecQuery(ruleid, sql string) (*xstream.TopologyNew, error) {
  213. if tp, inputs, err := p.createTopo(&api.Rule{Id: ruleid, Sql: sql}); err != nil {
  214. return nil, err
  215. } else {
  216. tp.AddSink(inputs, nodes.NewSinkNode("sink_memory_log", sinks.NewLogSinkToMemory()))
  217. go func() {
  218. select {
  219. case err := <-tp.Open():
  220. log.Println(err)
  221. tp.Cancel()
  222. }
  223. }()
  224. return tp, nil
  225. }
  226. }
  227. func (p *RuleProcessor) ExecDesc(name string) (string, error) {
  228. db, err := common.DbOpen(path.Join(p.badgerDir, "rule"))
  229. if err != nil {
  230. return "", err
  231. }
  232. defer common.DbClose(db)
  233. s, err := common.DbGet(db, string(name))
  234. if err != nil {
  235. return "", fmt.Errorf("Rule %s is not found.", name)
  236. }
  237. dst := &bytes.Buffer{}
  238. if err := json.Indent(dst, []byte(s), "", " "); err != nil {
  239. return "", err
  240. }
  241. return fmt.Sprintln(dst.String()), nil
  242. }
  243. func (p *RuleProcessor) ExecShow() (string, error) {
  244. keys, err := p.GetAllRules()
  245. if err != nil{
  246. return "", err
  247. }
  248. if len(keys) == 0 {
  249. keys = append(keys, "No rule definitions are found.")
  250. }
  251. var result string
  252. for _, c := range keys{
  253. result = result + fmt.Sprintln(c)
  254. }
  255. return result, nil
  256. }
  257. func (p *RuleProcessor) GetAllRules() ([]string, error) {
  258. db, err := common.DbOpen(path.Join(p.badgerDir, "rule"))
  259. if err != nil {
  260. return nil, err
  261. }
  262. defer common.DbClose(db)
  263. return common.DbKeys(db)
  264. }
  265. func (p *RuleProcessor) ExecDrop(name string) (string, error) {
  266. db, err := common.DbOpen(path.Join(p.badgerDir, "rule"))
  267. if err != nil {
  268. return "", err
  269. }
  270. defer common.DbClose(db)
  271. err = common.DbDelete(db, string(name))
  272. if err != nil {
  273. return "", err
  274. }else{
  275. return fmt.Sprintf("Rule %s is dropped.", name), nil
  276. }
  277. }
  278. func (p *RuleProcessor) createTopo(rule *api.Rule) (*xstream.TopologyNew, []api.Emitter, error) {
  279. return p.createTopoWithSources(rule, nil)
  280. }
  281. //For test to mock source
  282. func (p *RuleProcessor) createTopoWithSources(rule *api.Rule, sources []*nodes.SourceNode) (*xstream.TopologyNew, []api.Emitter, error){
  283. name := rule.Id
  284. sql := rule.Sql
  285. var isEventTime bool
  286. var lateTol int64
  287. if iet, ok := rule.Options["isEventTime"]; ok{
  288. isEventTime, ok = iet.(bool)
  289. if !ok{
  290. return nil, nil, fmt.Errorf("Invalid rule option isEventTime %v, bool type is required.", iet)
  291. }
  292. }
  293. if isEventTime {
  294. if l, ok := rule.Options["lateTolerance"]; ok{
  295. if fl, ok := l.(float64); ok{
  296. lateTol = int64(fl)
  297. }else{
  298. return nil, nil, fmt.Errorf("Invalid rule option lateTolerance %v, int type is required.", l)
  299. }
  300. }
  301. }
  302. shouldCreateSource := sources == nil
  303. parser := xsql.NewParser(strings.NewReader(sql))
  304. if stmt, err := xsql.Language.Parse(parser); err != nil{
  305. return nil, nil, fmt.Errorf("Parse SQL %s error: %s.", sql , err)
  306. }else {
  307. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  308. return nil, nil, fmt.Errorf("SQL %s is not a select statement.", sql)
  309. } else {
  310. tp := xstream.NewWithName(name)
  311. var inputs []api.Emitter
  312. streamsFromStmt := xsql.GetStreams(selectStmt)
  313. if !shouldCreateSource && len(streamsFromStmt) != len(sources){
  314. return nil, nil, fmt.Errorf("Invalid parameter sources or streams, the length cannot match the statement, expect %d sources.", len(streamsFromStmt))
  315. }
  316. db, err := common.DbOpen(path.Join(p.badgerDir, "stream"))
  317. if err != nil {
  318. return nil, nil, err
  319. }
  320. defer common.DbClose(db)
  321. for i, s := range streamsFromStmt {
  322. streamStmt, err := GetStream(db, s)
  323. if err != nil {
  324. return nil, nil, fmt.Errorf("fail to get stream %s, please check if stream is created", s)
  325. }
  326. pp, err := plans.NewPreprocessor(streamStmt, selectStmt.Fields, isEventTime)
  327. if err != nil{
  328. return nil, nil, err
  329. }
  330. if shouldCreateSource{
  331. src, err := getSource(streamStmt)
  332. if err != nil {
  333. return nil, nil, fmt.Errorf("fail to get source: %v", err)
  334. }
  335. node := nodes.NewSourceNode(s, src)
  336. tp.AddSrc(node)
  337. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s)
  338. tp.AddOperator([]api.Emitter{node}, preprocessorOp)
  339. inputs = append(inputs, preprocessorOp)
  340. } else {
  341. tp.AddSrc(sources[i])
  342. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s)
  343. tp.AddOperator([]api.Emitter{sources[i]}, preprocessorOp)
  344. inputs = append(inputs, preprocessorOp)
  345. }
  346. }
  347. dimensions := selectStmt.Dimensions
  348. var w *xsql.Window
  349. if dimensions != nil {
  350. w = dimensions.GetWindow()
  351. if w != nil {
  352. wop, err := operators.NewWindowOp("window", w, isEventTime, lateTol, streamsFromStmt)
  353. if err != nil {
  354. return nil, nil, err
  355. }
  356. tp.AddOperator(inputs, wop)
  357. inputs = []api.Emitter{wop}
  358. }
  359. }
  360. if w != nil && selectStmt.Joins != nil {
  361. joinOp := xstream.Transform(&plans.JoinPlan{Joins: selectStmt.Joins, From: selectStmt.Sources[0].(*xsql.Table)}, "join")
  362. //TODO concurrency setting by command
  363. //joinOp.SetConcurrency(3)
  364. tp.AddOperator(inputs, joinOp)
  365. inputs = []api.Emitter{joinOp}
  366. }
  367. if selectStmt.Condition != nil {
  368. filterOp := xstream.Transform(&plans.FilterPlan{Condition: selectStmt.Condition}, "filter")
  369. //TODO concurrency setting by command
  370. // filterOp.SetConcurrency(3)
  371. tp.AddOperator(inputs, filterOp)
  372. inputs = []api.Emitter{filterOp}
  373. }
  374. var ds xsql.Dimensions
  375. if dimensions != nil {
  376. ds = dimensions.GetGroups()
  377. if ds != nil && len(ds) > 0 {
  378. aggregateOp := xstream.Transform(&plans.AggregatePlan{Dimensions: ds}, "aggregate")
  379. tp.AddOperator(inputs, aggregateOp)
  380. inputs = []api.Emitter{aggregateOp}
  381. }
  382. }
  383. if selectStmt.Having != nil {
  384. havingOp := xstream.Transform(&plans.HavingPlan{selectStmt.Having}, "having")
  385. tp.AddOperator(inputs, havingOp)
  386. inputs = []api.Emitter{havingOp}
  387. }
  388. if selectStmt.SortFields != nil {
  389. orderOp := xstream.Transform(&plans.OrderPlan{SortFields: selectStmt.SortFields}, "order")
  390. tp.AddOperator(inputs, orderOp)
  391. inputs = []api.Emitter{orderOp}
  392. }
  393. if selectStmt.Fields != nil {
  394. projectOp := xstream.Transform(&plans.ProjectPlan{Fields: selectStmt.Fields, IsAggregate: xsql.IsAggStatement(selectStmt)}, "project")
  395. tp.AddOperator(inputs, projectOp)
  396. inputs = []api.Emitter{projectOp}
  397. }
  398. return tp, inputs, nil
  399. }
  400. }
  401. }
  402. func getSource(streamStmt *xsql.StreamStmt) (api.Source, error) {
  403. t, ok := streamStmt.Options["TYPE"]
  404. if !ok{
  405. t = "mqtt"
  406. }
  407. t = ucFirst(t)
  408. var s api.Source
  409. switch t {
  410. case "Mqtt":
  411. s = &extensions.MQTTSource{}
  412. log.Debugf("Source mqtt created")
  413. default:
  414. nf, err := getPlugin(t, "sources")
  415. if err != nil {
  416. return nil, err
  417. }
  418. s, ok = nf.(api.Source)
  419. if !ok {
  420. return nil, fmt.Errorf("exported symbol %s is not type of api.Source", t)
  421. }
  422. }
  423. props := getConf(t, streamStmt.Options["CONF_KEY"])
  424. err := s.Configure(streamStmt.Options["DATASOURCE"], props)
  425. if err != nil{
  426. return nil, err
  427. }
  428. log.Debugf("Source %s created", t)
  429. return s, nil
  430. }
  431. func getConf(t string, confkey string) map[string]interface{} {
  432. conf, err := common.LoadConf("sources/" + t + ".yaml")
  433. props := make(map[string]interface{})
  434. if err == nil {
  435. cfg := make(map[string]map[string]interface{})
  436. if err := yaml.Unmarshal(conf, &cfg); err != nil {
  437. log.Warnf("fail to parse yaml for source %s. Return an empty configuration", t)
  438. } else {
  439. var ok bool
  440. props, ok = cfg["default"]
  441. if !ok {
  442. log.Warnf("default conf is not found", confkey)
  443. }
  444. if c, ok := cfg[confkey]; ok {
  445. for k, v := range c {
  446. props[k] = v
  447. }
  448. }
  449. }
  450. } else {
  451. log.Warnf("config file %s.yaml is not loaded properly. Return an empty configuration", t)
  452. }
  453. log.Debugf("get conf for %s with conf key %s: %v", t, confkey, props)
  454. return props
  455. }
  456. func getPlugin(t string, ptype string) (plugin.Symbol, error) {
  457. mod := "plugins/" + ptype + "/" + t + ".so"
  458. plug, err := plugin.Open(mod)
  459. if err != nil {
  460. return nil, fmt.Errorf("cannot open %s: %v", mod, err)
  461. }
  462. nf, err := plug.Lookup(t)
  463. if err != nil {
  464. return nil, fmt.Errorf("cannot find symbol %s, please check if it is exported", t)
  465. }
  466. return nf, nil
  467. }
  468. func getSink(name string, action map[string]interface{}) (api.Sink, error) {
  469. log.Tracef("trying to get sink %s with action %v", name, action)
  470. var s api.Sink
  471. name = ucFirst(name)
  472. switch name {
  473. case "Log":
  474. s = sinks.NewLogSink()
  475. case "Mqtt":
  476. s = &sinks.MQTTSink{}
  477. default:
  478. nf, err := getPlugin(name, "sinks")
  479. if err != nil {
  480. return nil, err
  481. }
  482. var ok bool
  483. s, ok = nf.(api.Sink)
  484. if !ok {
  485. return nil, fmt.Errorf("exported symbol %s is not type of api.Sink", name)
  486. }
  487. }
  488. err := s.Configure(action)
  489. if err != nil{
  490. return nil, err
  491. }
  492. log.Debugf("Sink %s created", name)
  493. return s, nil
  494. }
  495. func ucFirst(str string) string {
  496. for i, v := range str {
  497. return string(unicode.ToUpper(v)) + str[i+1:]
  498. }
  499. return ""
  500. }