xsql_processor.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. "github.com/emqx/kuiper/xstream/operators"
  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(stmt *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, fmt.Errorf("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. return p.DropStream(stmt.Name)
  147. }
  148. func (p *StreamProcessor) DropStream(name string) (string, error) {
  149. err := p.db.Open()
  150. if err != nil {
  151. return "", fmt.Errorf("Drop stream fails, error when opening db: %v.", err)
  152. }
  153. defer p.db.Close()
  154. err = p.db.Delete(name)
  155. if err != nil {
  156. return "", fmt.Errorf("Drop stream fails: %v.", err)
  157. } else {
  158. return fmt.Sprintf("Stream %s is dropped.", name), nil
  159. }
  160. }
  161. func GetStream(m *common.SimpleKVStore, name string) (stmt *xsql.StreamStmt, err error) {
  162. s, f := m.Get(name)
  163. if !f {
  164. return nil, fmt.Errorf("Cannot find key %s. ", name)
  165. }
  166. s1, _ := s.(string)
  167. parser := xsql.NewParser(strings.NewReader(s1))
  168. stream, err := xsql.Language.Parse(parser)
  169. stmt, ok := stream.(*xsql.StreamStmt)
  170. if !ok {
  171. err = fmt.Errorf("Error resolving the stream %s, the data in db may be corrupted.", name)
  172. }
  173. return
  174. }
  175. type RuleProcessor struct {
  176. db common.KeyValue
  177. rootDbDir string
  178. }
  179. func NewRuleProcessor(d string) *RuleProcessor {
  180. processor := &RuleProcessor{
  181. db: common.GetSimpleKVStore(path.Join(d, "rule")),
  182. rootDbDir: d,
  183. }
  184. return processor
  185. }
  186. func (p *RuleProcessor) ExecCreate(name, ruleJson string) (*api.Rule, error) {
  187. rule, err := p.getRuleByJson(name, ruleJson)
  188. if err != nil {
  189. return nil, err
  190. }
  191. err = p.db.Open()
  192. if err != nil {
  193. return nil, err
  194. }
  195. defer p.db.Close()
  196. err = p.db.Set(rule.Id, ruleJson)
  197. if err != nil {
  198. return nil, err
  199. } else {
  200. log.Infof("Rule %s is created.", rule.Id)
  201. }
  202. return rule, nil
  203. }
  204. func (p *RuleProcessor) GetRuleByName(name string) (*api.Rule, error) {
  205. err := p.db.Open()
  206. if err != nil {
  207. return nil, err
  208. }
  209. defer p.db.Close()
  210. s, f := p.db.Get(string(name))
  211. if !f {
  212. return nil, fmt.Errorf("Rule %s is not found.", name)
  213. }
  214. s1, _ := s.(string)
  215. return p.getRuleByJson(name, s1)
  216. }
  217. func (p *RuleProcessor) getRuleByJson(name, ruleJson string) (*api.Rule, error) {
  218. var rule api.Rule
  219. if err := json.Unmarshal([]byte(ruleJson), &rule); err != nil {
  220. return nil, fmt.Errorf("Parse rule %s error : %s.", ruleJson, err)
  221. }
  222. //validation
  223. if rule.Id == "" && name == "" {
  224. return nil, fmt.Errorf("Missing rule id.")
  225. }
  226. if name != "" && rule.Id != "" && name != rule.Id {
  227. return nil, fmt.Errorf("Name is not consistent with rule id.")
  228. }
  229. if rule.Id == "" {
  230. rule.Id = name
  231. }
  232. if rule.Sql == "" {
  233. return nil, fmt.Errorf("Missing rule SQL.")
  234. }
  235. if rule.Actions == nil || len(rule.Actions) == 0 {
  236. return nil, fmt.Errorf("Missing rule actions.")
  237. }
  238. return &rule, nil
  239. }
  240. func (p *RuleProcessor) ExecInitRule(rule *api.Rule) (*xstream.TopologyNew, error) {
  241. if tp, inputs, err := p.createTopo(rule); err != nil {
  242. return nil, err
  243. } else {
  244. for _, m := range rule.Actions {
  245. for name, action := range m {
  246. props, ok := action.(map[string]interface{})
  247. if !ok {
  248. return nil, fmt.Errorf("expect map[string]interface{} type for the action properties, but found %v", action)
  249. }
  250. tp.AddSink(inputs, nodes.NewSinkNode("sink_"+name, name, props))
  251. }
  252. }
  253. return tp, nil
  254. }
  255. }
  256. func (p *RuleProcessor) ExecQuery(ruleid, sql string) (*xstream.TopologyNew, error) {
  257. if tp, inputs, err := p.createTopo(&api.Rule{Id: ruleid, Sql: sql}); err != nil {
  258. return nil, err
  259. } else {
  260. tp.AddSink(inputs, nodes.NewSinkNode("sink_memory_log", "logToMemory", nil))
  261. go func() {
  262. select {
  263. case err := <-tp.Open():
  264. log.Infof("closing query for error: %v", err)
  265. tp.GetContext().SetError(err)
  266. tp.Cancel()
  267. }
  268. }()
  269. return tp, nil
  270. }
  271. }
  272. func (p *RuleProcessor) ExecDesc(name string) (string, error) {
  273. err := p.db.Open()
  274. if err != nil {
  275. return "", err
  276. }
  277. defer p.db.Close()
  278. s, f := p.db.Get(name)
  279. if !f {
  280. return "", fmt.Errorf("Rule %s is not found.", name)
  281. }
  282. s1, _ := s.(string)
  283. dst := &bytes.Buffer{}
  284. if err := json.Indent(dst, []byte(s1), "", " "); err != nil {
  285. return "", err
  286. }
  287. return fmt.Sprintln(dst.String()), nil
  288. }
  289. func (p *RuleProcessor) ExecShow() (string, error) {
  290. keys, err := p.GetAllRules()
  291. if err != nil {
  292. return "", err
  293. }
  294. if len(keys) == 0 {
  295. keys = append(keys, "No rule definitions are found.")
  296. }
  297. var result string
  298. for _, c := range keys {
  299. result = result + fmt.Sprintln(c)
  300. }
  301. return result, nil
  302. }
  303. func (p *RuleProcessor) GetAllRules() ([]string, error) {
  304. err := p.db.Open()
  305. if err != nil {
  306. return nil, err
  307. }
  308. defer p.db.Close()
  309. return p.db.Keys()
  310. }
  311. func (p *RuleProcessor) ExecDrop(name string) (string, error) {
  312. err := p.db.Open()
  313. if err != nil {
  314. return "", err
  315. }
  316. defer p.db.Close()
  317. err = p.db.Delete(string(name))
  318. if err != nil {
  319. return "", err
  320. } else {
  321. return fmt.Sprintf("Rule %s is dropped.", name), nil
  322. }
  323. }
  324. func (p *RuleProcessor) createTopo(rule *api.Rule) (*xstream.TopologyNew, []api.Emitter, error) {
  325. return p.createTopoWithSources(rule, nil)
  326. }
  327. //For test to mock source
  328. func (p *RuleProcessor) createTopoWithSources(rule *api.Rule, sources []*nodes.SourceNode) (*xstream.TopologyNew, []api.Emitter, error) {
  329. name := rule.Id
  330. sql := rule.Sql
  331. var (
  332. isEventTime bool
  333. lateTol int64
  334. concurrency = 1
  335. bufferLength = 1024
  336. )
  337. if iet, ok := rule.Options["isEventTime"]; ok {
  338. isEventTime, ok = iet.(bool)
  339. if !ok {
  340. return nil, nil, fmt.Errorf("Invalid rule option isEventTime %v, bool type is required.", iet)
  341. }
  342. }
  343. if isEventTime {
  344. if l, ok := rule.Options["lateTolerance"]; ok {
  345. if fl, ok := l.(float64); ok {
  346. lateTol = int64(fl)
  347. } else {
  348. return nil, nil, fmt.Errorf("Invalid rule option lateTolerance %v, int type is required.", l)
  349. }
  350. }
  351. }
  352. if l, ok := rule.Options["concurrency"]; ok {
  353. if fl, ok := l.(float64); ok {
  354. concurrency = int(fl)
  355. } else {
  356. return nil, nil, fmt.Errorf("Invalid rule option concurrency %v, int type is required.", l)
  357. }
  358. }
  359. if l, ok := rule.Options["bufferLength"]; ok {
  360. if fl, ok := l.(float64); ok {
  361. bufferLength = int(fl)
  362. } else {
  363. return nil, nil, fmt.Errorf("Invalid rule option bufferLength %v, int type is required.", l)
  364. }
  365. }
  366. log.Infof("Init rule with options {isEventTime: %v, lateTolerance: %d, concurrency: %d, bufferLength: %d", isEventTime, lateTol, concurrency, bufferLength)
  367. shouldCreateSource := sources == nil
  368. parser := xsql.NewParser(strings.NewReader(sql))
  369. if stmt, err := xsql.Language.Parse(parser); err != nil {
  370. return nil, nil, fmt.Errorf("Parse SQL %s error: %s.", sql, err)
  371. } else {
  372. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  373. return nil, nil, fmt.Errorf("SQL %s is not a select statement.", sql)
  374. } else {
  375. tp := xstream.NewWithName(name)
  376. var inputs []api.Emitter
  377. streamsFromStmt := xsql.GetStreams(selectStmt)
  378. if !shouldCreateSource && len(streamsFromStmt) != len(sources) {
  379. return nil, nil, fmt.Errorf("Invalid parameter sources or streams, the length cannot match the statement, expect %d sources.", len(streamsFromStmt))
  380. }
  381. store := common.GetSimpleKVStore(path.Join(p.rootDbDir, "stream"))
  382. err := store.Open()
  383. if err != nil {
  384. return nil, nil, err
  385. }
  386. defer store.Close()
  387. for i, s := range streamsFromStmt {
  388. streamStmt, err := GetStream(store, s)
  389. if err != nil {
  390. return nil, nil, fmt.Errorf("fail to get stream %s, please check if stream is created", s)
  391. }
  392. pp, err := plans.NewPreprocessor(streamStmt, selectStmt.Fields, isEventTime)
  393. if err != nil {
  394. return nil, nil, err
  395. }
  396. if shouldCreateSource {
  397. node := nodes.NewSourceNode(s, streamStmt.Options)
  398. tp.AddSrc(node)
  399. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s, bufferLength)
  400. preprocessorOp.SetConcurrency(concurrency)
  401. tp.AddOperator([]api.Emitter{node}, preprocessorOp)
  402. inputs = append(inputs, preprocessorOp)
  403. } else {
  404. tp.AddSrc(sources[i])
  405. preprocessorOp := xstream.Transform(pp, "preprocessor_"+s, bufferLength)
  406. preprocessorOp.SetConcurrency(concurrency)
  407. tp.AddOperator([]api.Emitter{sources[i]}, preprocessorOp)
  408. inputs = append(inputs, preprocessorOp)
  409. }
  410. }
  411. dimensions := selectStmt.Dimensions
  412. var w *xsql.Window
  413. if dimensions != nil {
  414. w = dimensions.GetWindow()
  415. if w != nil {
  416. wop, err := operators.NewWindowOp("window", w, isEventTime, lateTol, streamsFromStmt, bufferLength)
  417. if err != nil {
  418. return nil, nil, err
  419. }
  420. tp.AddOperator(inputs, wop)
  421. inputs = []api.Emitter{wop}
  422. }
  423. }
  424. if w != nil && selectStmt.Joins != nil {
  425. joinOp := xstream.Transform(&plans.JoinPlan{Joins: selectStmt.Joins, From: selectStmt.Sources[0].(*xsql.Table)}, "join", bufferLength)
  426. joinOp.SetConcurrency(concurrency)
  427. tp.AddOperator(inputs, joinOp)
  428. inputs = []api.Emitter{joinOp}
  429. }
  430. if selectStmt.Condition != nil {
  431. filterOp := xstream.Transform(&plans.FilterPlan{Condition: selectStmt.Condition}, "filter", bufferLength)
  432. filterOp.SetConcurrency(concurrency)
  433. tp.AddOperator(inputs, filterOp)
  434. inputs = []api.Emitter{filterOp}
  435. }
  436. var ds xsql.Dimensions
  437. if dimensions != nil {
  438. ds = dimensions.GetGroups()
  439. if ds != nil && len(ds) > 0 {
  440. aggregateOp := xstream.Transform(&plans.AggregatePlan{Dimensions: ds}, "aggregate", bufferLength)
  441. aggregateOp.SetConcurrency(concurrency)
  442. tp.AddOperator(inputs, aggregateOp)
  443. inputs = []api.Emitter{aggregateOp}
  444. }
  445. }
  446. if selectStmt.Having != nil {
  447. havingOp := xstream.Transform(&plans.HavingPlan{selectStmt.Having}, "having", bufferLength)
  448. havingOp.SetConcurrency(concurrency)
  449. tp.AddOperator(inputs, havingOp)
  450. inputs = []api.Emitter{havingOp}
  451. }
  452. if selectStmt.SortFields != nil {
  453. orderOp := xstream.Transform(&plans.OrderPlan{SortFields: selectStmt.SortFields}, "order", bufferLength)
  454. orderOp.SetConcurrency(concurrency)
  455. tp.AddOperator(inputs, orderOp)
  456. inputs = []api.Emitter{orderOp}
  457. }
  458. if selectStmt.Fields != nil {
  459. projectOp := xstream.Transform(&plans.ProjectPlan{Fields: selectStmt.Fields, IsAggregate: xsql.IsAggStatement(selectStmt)}, "project", bufferLength)
  460. projectOp.SetConcurrency(concurrency)
  461. tp.AddOperator(inputs, projectOp)
  462. inputs = []api.Emitter{projectOp}
  463. }
  464. return tp, inputs, nil
  465. }
  466. }
  467. }