xsql_processor.go 17 KB

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