xsql_processor.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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(stmt *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. var rule api.Rule
  245. if err := json.Unmarshal([]byte(ruleJson), &rule); err != nil {
  246. return nil, fmt.Errorf("Parse rule %s error : %s.", ruleJson, err)
  247. }
  248. //validation
  249. if rule.Id == "" && name == "" {
  250. return nil, fmt.Errorf("Missing rule id.")
  251. }
  252. if name != "" && rule.Id != "" && name != rule.Id {
  253. return nil, fmt.Errorf("Name is not consistent with rule id.")
  254. }
  255. if rule.Id == "" {
  256. rule.Id = name
  257. }
  258. if rule.Sql == "" {
  259. return nil, fmt.Errorf("Missing rule SQL.")
  260. }
  261. if rule.Actions == nil || len(rule.Actions) == 0 {
  262. return nil, fmt.Errorf("Missing rule actions.")
  263. }
  264. return &rule, nil
  265. }
  266. func (p *RuleProcessor) ExecInitRule(rule *api.Rule) (*xstream.TopologyNew, error) {
  267. if tp, inputs, err := p.createTopo(rule); err != nil {
  268. return nil, err
  269. } else {
  270. for i, m := range rule.Actions {
  271. for name, action := range m {
  272. props, ok := action.(map[string]interface{})
  273. if !ok {
  274. return nil, fmt.Errorf("expect map[string]interface{} type for the action properties, but found %v", action)
  275. }
  276. tp.AddSink(inputs, nodes.NewSinkNode(fmt.Sprintf("%s_%d", name, i), name, props))
  277. }
  278. }
  279. return tp, nil
  280. }
  281. }
  282. func (p *RuleProcessor) ExecQuery(ruleid, sql string) (*xstream.TopologyNew, error) {
  283. if tp, inputs, err := p.createTopo(&api.Rule{Id: ruleid, Sql: sql}); err != nil {
  284. return nil, err
  285. } else {
  286. tp.AddSink(inputs, nodes.NewSinkNode("sink_memory_log", "logToMemory", nil))
  287. go func() {
  288. select {
  289. case err := <-tp.Open():
  290. log.Infof("closing query for error: %v", err)
  291. tp.GetContext().SetError(err)
  292. tp.Cancel()
  293. }
  294. }()
  295. return tp, nil
  296. }
  297. }
  298. func (p *RuleProcessor) ExecDesc(name string) (string, error) {
  299. err := p.db.Open()
  300. if err != nil {
  301. return "", err
  302. }
  303. defer p.db.Close()
  304. s, f := p.db.Get(name)
  305. if !f {
  306. return "", fmt.Errorf("Rule %s is not found.", name)
  307. }
  308. s1, _ := s.(string)
  309. dst := &bytes.Buffer{}
  310. if err := json.Indent(dst, []byte(s1), "", " "); err != nil {
  311. return "", err
  312. }
  313. return fmt.Sprintln(dst.String()), nil
  314. }
  315. func (p *RuleProcessor) GetAllRules() ([]string, error) {
  316. err := p.db.Open()
  317. if err != nil {
  318. return nil, err
  319. }
  320. defer p.db.Close()
  321. return p.db.Keys()
  322. }
  323. func (p *RuleProcessor) ExecDrop(name string) (string, error) {
  324. err := p.db.Open()
  325. if err != nil {
  326. return "", err
  327. }
  328. defer p.db.Close()
  329. err = p.db.Delete(name)
  330. if err != nil {
  331. return "", err
  332. } else {
  333. return fmt.Sprintf("Rule %s is dropped.", name), nil
  334. }
  335. }
  336. func (p *RuleProcessor) createTopo(rule *api.Rule) (*xstream.TopologyNew, []api.Emitter, error) {
  337. return p.createTopoWithSources(rule, nil)
  338. }
  339. //For test to mock source
  340. func (p *RuleProcessor) createTopoWithSources(rule *api.Rule, sources []*nodes.SourceNode) (*xstream.TopologyNew, []api.Emitter, error) {
  341. name := rule.Id
  342. sql := rule.Sql
  343. var (
  344. isEventTime bool
  345. lateTol int64
  346. concurrency = 1
  347. bufferLength = 1024
  348. sendMataToSink = false
  349. )
  350. if iet, ok := rule.Options["isEventTime"]; ok {
  351. isEventTime, ok = iet.(bool)
  352. if !ok {
  353. return nil, nil, fmt.Errorf("Invalid rule option isEventTime %v, bool type is required.", iet)
  354. }
  355. }
  356. if isEventTime {
  357. if l, ok := rule.Options["lateTolerance"]; ok {
  358. if fl, ok := l.(float64); ok {
  359. lateTol = int64(fl)
  360. } else {
  361. return nil, nil, fmt.Errorf("Invalid rule option lateTolerance %v, int type is required.", l)
  362. }
  363. }
  364. }
  365. if l, ok := rule.Options["concurrency"]; ok {
  366. if fl, ok := l.(float64); ok {
  367. concurrency = int(fl)
  368. } else {
  369. return nil, nil, fmt.Errorf("Invalid rule option concurrency %v, int type is required.", l)
  370. }
  371. }
  372. if l, ok := rule.Options["bufferLength"]; ok {
  373. if fl, ok := l.(float64); ok {
  374. bufferLength = int(fl)
  375. } else {
  376. return nil, nil, fmt.Errorf("Invalid rule option bufferLength %v, int type is required.", l)
  377. }
  378. }
  379. if l, ok := rule.Options["sendMetaToSink"]; ok {
  380. if fl, ok := l.(bool); ok {
  381. sendMataToSink = fl
  382. } else {
  383. return nil, nil, fmt.Errorf("Invalid rule option sendMetaToSink %v, bool type is required.", l)
  384. }
  385. }
  386. log.Infof("Init rule with options {isEventTime: %v, lateTolerance: %d, concurrency: %d, bufferLength: %d", isEventTime, lateTol, concurrency, bufferLength)
  387. shouldCreateSource := sources == nil
  388. parser := xsql.NewParser(strings.NewReader(sql))
  389. if stmt, err := xsql.Language.Parse(parser); err != nil {
  390. return nil, nil, fmt.Errorf("Parse SQL %s error: %s.", sql, err)
  391. } else {
  392. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  393. return nil, nil, fmt.Errorf("SQL %s is not a select statement.", sql)
  394. } else {
  395. tp := xstream.NewWithName(name)
  396. var inputs []api.Emitter
  397. streamsFromStmt := xsql.GetStreams(selectStmt)
  398. dimensions := selectStmt.Dimensions
  399. if !shouldCreateSource && len(streamsFromStmt) != len(sources) {
  400. return nil, nil, fmt.Errorf("Invalid parameter sources or streams, the length cannot match the statement, expect %d sources.", len(streamsFromStmt))
  401. }
  402. if sendMataToSink && (len(streamsFromStmt) > 1 || dimensions != nil) {
  403. return nil, nil, fmt.Errorf("Invalid option sendMetaToSink, it can not be applied to window")
  404. }
  405. store := common.GetSimpleKVStore(path.Join(p.rootDbDir, "stream"))
  406. err := store.Open()
  407. if err != nil {
  408. return nil, nil, err
  409. }
  410. defer store.Close()
  411. var alias, aggregateAlias xsql.Fields
  412. for _, f := range selectStmt.Fields {
  413. if f.AName != "" {
  414. if !xsql.HasAggFuncs(f.Expr) {
  415. alias = append(alias, f)
  416. } else {
  417. aggregateAlias = append(aggregateAlias, f)
  418. }
  419. }
  420. }
  421. for i, s := range streamsFromStmt {
  422. streamStmt, err := GetStream(store, s)
  423. if err != nil {
  424. return nil, nil, fmt.Errorf("fail to get stream %s, please check if stream is created", s)
  425. }
  426. pp, err := plans.NewPreprocessor(streamStmt, alias, isEventTime)
  427. if err != nil {
  428. return nil, nil, err
  429. }
  430. if shouldCreateSource {
  431. node := nodes.NewSourceNode(s, streamStmt.Options)
  432. tp.AddSrc(node)
  433. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s, bufferLength)
  434. preprocessorOp.SetConcurrency(concurrency)
  435. tp.AddOperator([]api.Emitter{node}, preprocessorOp)
  436. inputs = append(inputs, preprocessorOp)
  437. } else {
  438. tp.AddSrc(sources[i])
  439. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s, bufferLength)
  440. preprocessorOp.SetConcurrency(concurrency)
  441. tp.AddOperator([]api.Emitter{sources[i]}, preprocessorOp)
  442. inputs = append(inputs, preprocessorOp)
  443. }
  444. }
  445. var w *xsql.Window
  446. if dimensions != nil {
  447. w = dimensions.GetWindow()
  448. if w != nil {
  449. wop, err := nodes.NewWindowOp("window", w, isEventTime, lateTol, streamsFromStmt, bufferLength)
  450. if err != nil {
  451. return nil, nil, err
  452. }
  453. tp.AddOperator(inputs, wop)
  454. inputs = []api.Emitter{wop}
  455. }
  456. }
  457. if w != nil && selectStmt.Joins != nil {
  458. joinOp := xstream.Transform(&plans.JoinPlan{Joins: selectStmt.Joins, From: selectStmt.Sources[0].(*xsql.Table)}, "join", bufferLength)
  459. joinOp.SetConcurrency(concurrency)
  460. tp.AddOperator(inputs, joinOp)
  461. inputs = []api.Emitter{joinOp}
  462. }
  463. if selectStmt.Condition != nil {
  464. filterOp := xstream.Transform(&plans.FilterPlan{Condition: selectStmt.Condition}, "filter", bufferLength)
  465. filterOp.SetConcurrency(concurrency)
  466. tp.AddOperator(inputs, filterOp)
  467. inputs = []api.Emitter{filterOp}
  468. }
  469. var ds xsql.Dimensions
  470. if dimensions != nil || len(aggregateAlias) > 0 {
  471. ds = dimensions.GetGroups()
  472. if (ds != nil && len(ds) > 0) || len(aggregateAlias) > 0 {
  473. aggregateOp := xstream.Transform(&plans.AggregatePlan{Dimensions: ds, Alias: aggregateAlias}, "aggregate", bufferLength)
  474. aggregateOp.SetConcurrency(concurrency)
  475. tp.AddOperator(inputs, aggregateOp)
  476. inputs = []api.Emitter{aggregateOp}
  477. }
  478. }
  479. if selectStmt.Having != nil {
  480. havingOp := xstream.Transform(&plans.HavingPlan{selectStmt.Having}, "having", bufferLength)
  481. havingOp.SetConcurrency(concurrency)
  482. tp.AddOperator(inputs, havingOp)
  483. inputs = []api.Emitter{havingOp}
  484. }
  485. if selectStmt.SortFields != nil {
  486. orderOp := xstream.Transform(&plans.OrderPlan{SortFields: selectStmt.SortFields}, "order", bufferLength)
  487. orderOp.SetConcurrency(concurrency)
  488. tp.AddOperator(inputs, orderOp)
  489. inputs = []api.Emitter{orderOp}
  490. }
  491. if selectStmt.Fields != nil {
  492. projectOp := xstream.Transform(&plans.ProjectPlan{Fields: selectStmt.Fields, IsAggregate: xsql.IsAggStatement(selectStmt), SendMeta: sendMataToSink}, "project", bufferLength)
  493. projectOp.SetConcurrency(concurrency)
  494. tp.AddOperator(inputs, projectOp)
  495. inputs = []api.Emitter{projectOp}
  496. }
  497. return tp, inputs, nil
  498. }
  499. }
  500. }