xsql_processor.go 15 KB

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