rule.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package processor
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/internal/pkg/store"
  21. "github.com/lf-edge/ekuiper/internal/xsql"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. "github.com/lf-edge/ekuiper/pkg/errorx"
  24. "github.com/lf-edge/ekuiper/pkg/kv"
  25. "path"
  26. )
  27. type RuleProcessor struct {
  28. db kv.KeyValue
  29. }
  30. func NewRuleProcessor() *RuleProcessor {
  31. err, db := store.GetKV("rule")
  32. if err != nil {
  33. panic(fmt.Sprintf("Can not initalize store for the rule processor at path 'rule': %v", err))
  34. }
  35. processor := &RuleProcessor{
  36. db: db,
  37. }
  38. return processor
  39. }
  40. func (p *RuleProcessor) ExecCreate(name, ruleJson string) (*api.Rule, error) {
  41. rule, err := p.getRuleByJson(name, ruleJson)
  42. if err != nil {
  43. return nil, err
  44. }
  45. err = p.db.Setnx(rule.Id, ruleJson)
  46. if err != nil {
  47. return nil, err
  48. } else {
  49. log.Infof("Rule %s is created.", rule.Id)
  50. }
  51. return rule, nil
  52. }
  53. func (p *RuleProcessor) ExecUpdate(name, ruleJson string) (*api.Rule, error) {
  54. rule, err := p.getRuleByJson(name, ruleJson)
  55. if err != nil {
  56. return nil, err
  57. }
  58. err = p.db.Set(rule.Id, ruleJson)
  59. if err != nil {
  60. return nil, err
  61. } else {
  62. log.Infof("Rule %s is update.", rule.Id)
  63. }
  64. return rule, nil
  65. }
  66. func (p *RuleProcessor) ExecReplaceRuleState(name string, triggered bool) (err error) {
  67. rule, err := p.GetRuleByName(name)
  68. if err != nil {
  69. return err
  70. }
  71. rule.Triggered = triggered
  72. ruleJson, err := json.Marshal(rule)
  73. if err != nil {
  74. return fmt.Errorf("Marshal rule %s error : %s.", name, err)
  75. }
  76. err = p.db.Set(name, string(ruleJson))
  77. if err != nil {
  78. return err
  79. } else {
  80. log.Infof("Rule %s is replaced.", name)
  81. }
  82. return err
  83. }
  84. func (p *RuleProcessor) GetRuleJson(name string) (string, error) {
  85. var s1 string
  86. f, _ := p.db.Get(name, &s1)
  87. if !f {
  88. return "", errorx.NewWithCode(errorx.NOT_FOUND, fmt.Sprintf("Rule %s is not found.", name))
  89. }
  90. return s1, nil
  91. }
  92. func (p *RuleProcessor) GetRuleByName(name string) (*api.Rule, error) {
  93. var s1 string
  94. f, _ := p.db.Get(name, &s1)
  95. if !f {
  96. return nil, errorx.NewWithCode(errorx.NOT_FOUND, fmt.Sprintf("Rule %s is not found.", name))
  97. }
  98. return p.getRuleByJson(name, s1)
  99. }
  100. func (p *RuleProcessor) getDefaultRule(name, sql string) *api.Rule {
  101. return &api.Rule{
  102. Id: name,
  103. Sql: sql,
  104. Options: &api.RuleOption{
  105. IsEventTime: false,
  106. LateTol: 1000,
  107. Concurrency: 1,
  108. BufferLength: 1024,
  109. SendMetaToSink: false,
  110. SendError: true,
  111. Qos: api.AtMostOnce,
  112. CheckpointInterval: 300000,
  113. },
  114. }
  115. }
  116. func (p *RuleProcessor) getRuleByJson(id, ruleJson string) (*api.Rule, error) {
  117. opt := conf.Config.Rule
  118. //set default rule options
  119. rule := &api.Rule{
  120. Options: &opt,
  121. }
  122. if err := json.Unmarshal([]byte(ruleJson), &rule); err != nil {
  123. return nil, fmt.Errorf("Parse rule %s error : %s.", ruleJson, err)
  124. }
  125. //validation
  126. if rule.Id == "" && id == "" {
  127. return nil, fmt.Errorf("Missing rule id.")
  128. }
  129. if id != "" && rule.Id != "" && id != rule.Id {
  130. return nil, fmt.Errorf("Name is not consistent with rule id.")
  131. }
  132. if rule.Id == "" {
  133. rule.Id = id
  134. }
  135. if rule.Sql != "" {
  136. if rule.Graph != nil {
  137. return nil, fmt.Errorf("Rule %s has both sql and graph.", rule.Id)
  138. }
  139. if _, err := xsql.GetStatementFromSql(rule.Sql); err != nil {
  140. return nil, err
  141. }
  142. if rule.Actions == nil || len(rule.Actions) == 0 {
  143. return nil, fmt.Errorf("Missing rule actions.")
  144. }
  145. } else {
  146. if rule.Graph == nil {
  147. return nil, fmt.Errorf("Rule %s has neither sql nor graph.", rule.Id)
  148. }
  149. }
  150. if rule.Options == nil {
  151. rule.Options = &opt
  152. }
  153. //Setnx default options
  154. if rule.Options.CheckpointInterval < 0 {
  155. return nil, fmt.Errorf("rule option checkpointInterval %d is invalid, require a positive integer", rule.Options.CheckpointInterval)
  156. }
  157. if rule.Options.Concurrency < 0 {
  158. return nil, fmt.Errorf("rule option concurrency %d is invalid, require a positive integer", rule.Options.Concurrency)
  159. }
  160. if rule.Options.BufferLength < 0 {
  161. return nil, fmt.Errorf("rule option bufferLength %d is invalid, require a positive integer", rule.Options.BufferLength)
  162. }
  163. if rule.Options.LateTol < 0 {
  164. return nil, fmt.Errorf("rule option lateTolerance %d is invalid, require a positive integer", rule.Options.LateTol)
  165. }
  166. return rule, nil
  167. }
  168. func (p *RuleProcessor) ExecDesc(name string) (string, error) {
  169. var s1 string
  170. f, _ := p.db.Get(name, &s1)
  171. if !f {
  172. return "", fmt.Errorf("Rule %s is not found.", name)
  173. }
  174. dst := &bytes.Buffer{}
  175. if err := json.Indent(dst, []byte(s1), "", " "); err != nil {
  176. return "", err
  177. }
  178. return fmt.Sprintln(dst.String()), nil
  179. }
  180. func (p *RuleProcessor) GetAllRules() ([]string, error) {
  181. return p.db.Keys()
  182. }
  183. func (p *RuleProcessor) ExecDrop(name string) (string, error) {
  184. result := fmt.Sprintf("Rule %s is dropped.", name)
  185. var ruleJson string
  186. if ok, _ := p.db.Get(name, &ruleJson); ok {
  187. if err := cleanSinkCache(name); err != nil {
  188. result = fmt.Sprintf("%s. Clean sink cache faile: %s.", result, err)
  189. }
  190. if err := cleanCheckpoint(name); err != nil {
  191. result = fmt.Sprintf("%s. Clean checkpoint cache faile: %s.", result, err)
  192. }
  193. }
  194. err := p.db.Delete(name)
  195. if err != nil {
  196. return "", err
  197. } else {
  198. return result, nil
  199. }
  200. }
  201. func cleanCheckpoint(name string) error {
  202. err := store.DropTS(name)
  203. if err != nil {
  204. return err
  205. }
  206. return nil
  207. }
  208. func cleanSinkCache(name string) error {
  209. err := store.DropKV(path.Join("sink", name))
  210. if err != nil {
  211. return err
  212. }
  213. return nil
  214. }