default.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2022-2023 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 context
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "regexp"
  20. "sync"
  21. "text/template"
  22. "time"
  23. "github.com/sirupsen/logrus"
  24. "github.com/lf-edge/ekuiper/internal/conf"
  25. "github.com/lf-edge/ekuiper/internal/topo/transform"
  26. "github.com/lf-edge/ekuiper/pkg/api"
  27. "github.com/lf-edge/ekuiper/pkg/cast"
  28. )
  29. const (
  30. LoggerKey = "$$logger"
  31. RuleStartKey = "$$ruleStart"
  32. )
  33. type DefaultContext struct {
  34. ruleId string
  35. opId string
  36. instanceId int
  37. ctx context.Context
  38. err error
  39. // Only initialized after withMeta set
  40. store api.Store
  41. state *sync.Map
  42. snapshot map[string]interface{}
  43. // cache
  44. tpReg sync.Map
  45. jpReg sync.Map
  46. }
  47. func Background() *DefaultContext {
  48. c := &DefaultContext{
  49. ctx: context.Background(),
  50. }
  51. return c
  52. }
  53. func WithValue(parent *DefaultContext, key, val interface{}) *DefaultContext {
  54. parent.ctx = context.WithValue(parent.ctx, key, val)
  55. return parent
  56. }
  57. // Deadline Implement context interface
  58. func (c *DefaultContext) Deadline() (deadline time.Time, ok bool) {
  59. return c.ctx.Deadline()
  60. }
  61. func (c *DefaultContext) Done() <-chan struct{} {
  62. return c.ctx.Done()
  63. }
  64. func (c *DefaultContext) Err() error {
  65. if c.err != nil {
  66. return c.err
  67. }
  68. return c.ctx.Err()
  69. }
  70. func (c *DefaultContext) Value(key interface{}) interface{} {
  71. return c.ctx.Value(key)
  72. }
  73. func (c *DefaultContext) GetContext() context.Context {
  74. return c.ctx
  75. }
  76. func (c *DefaultContext) GetLogger() api.Logger {
  77. l, ok := c.ctx.Value(LoggerKey).(*logrus.Entry)
  78. if l != nil && ok {
  79. return l
  80. }
  81. return conf.Log.WithField("caller", "default")
  82. }
  83. func (c *DefaultContext) GetRuleId() string {
  84. return c.ruleId
  85. }
  86. func (c *DefaultContext) GetOpId() string {
  87. return c.opId
  88. }
  89. func (c *DefaultContext) GetInstanceId() int {
  90. return c.instanceId
  91. }
  92. func (c *DefaultContext) GetRootPath() string {
  93. loc, _ := conf.GetLoc("")
  94. return loc
  95. }
  96. func (c *DefaultContext) SetError(err error) {
  97. c.err = err
  98. }
  99. // ParseTemplate parse template string against data
  100. // The templates are built only once and cached in the context by its raw string as the key
  101. // If the prop string is not a template, a nil template is cached to indicate it has been parsed, and it will return the original string
  102. func (c *DefaultContext) ParseTemplate(prop string, data interface{}) (string, error) {
  103. var (
  104. tp *template.Template
  105. err error
  106. )
  107. if raw, ok := c.tpReg.Load(prop); ok {
  108. if raw != nil {
  109. tp = raw.(*template.Template)
  110. } else {
  111. return prop, nil
  112. }
  113. } else { // not parsed before
  114. re := regexp.MustCompile(`{{(.*?)}}`)
  115. // check if it is a template
  116. if re.Match([]byte(prop)) {
  117. tp, err = transform.GenTp(prop)
  118. if err != nil {
  119. return fmt.Sprintf("%v", data), err
  120. }
  121. c.tpReg.Store(prop, tp)
  122. } else {
  123. c.tpReg.Store(prop, nil)
  124. return prop, nil
  125. }
  126. }
  127. var output bytes.Buffer
  128. err = tp.Execute(&output, data)
  129. if err != nil {
  130. return fmt.Sprintf("%v", data), err
  131. }
  132. return output.String(), nil
  133. }
  134. func (c *DefaultContext) ParseJsonPath(prop string, data interface{}) (interface{}, error) {
  135. var (
  136. je conf.JsonPathEval
  137. err error
  138. )
  139. if raw, ok := c.jpReg.Load(prop); ok {
  140. je = raw.(conf.JsonPathEval)
  141. } else {
  142. je, err = conf.GetJsonPathEval(prop)
  143. if err != nil {
  144. return nil, err
  145. }
  146. c.jpReg.Store(prop, je)
  147. }
  148. return je.Eval(data)
  149. }
  150. func (c *DefaultContext) WithMeta(ruleId string, opId string, store api.Store) api.StreamContext {
  151. s, err := store.GetOpState(opId)
  152. if err != nil {
  153. c.GetLogger().Warnf("Initialize context store error for %s: %s", opId, err)
  154. }
  155. return &DefaultContext{
  156. ruleId: ruleId,
  157. opId: opId,
  158. instanceId: 0,
  159. ctx: c.ctx,
  160. store: store,
  161. state: s,
  162. tpReg: sync.Map{},
  163. jpReg: sync.Map{},
  164. }
  165. }
  166. func (c *DefaultContext) WithInstance(instanceId int) api.StreamContext {
  167. return &DefaultContext{
  168. instanceId: instanceId,
  169. ruleId: c.ruleId,
  170. opId: c.opId,
  171. ctx: c.ctx,
  172. state: c.state,
  173. }
  174. }
  175. func (c *DefaultContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  176. ctx, cancel := context.WithCancel(c.ctx)
  177. return &DefaultContext{
  178. ruleId: c.ruleId,
  179. opId: c.opId,
  180. instanceId: c.instanceId,
  181. ctx: ctx,
  182. state: c.state,
  183. }, cancel
  184. }
  185. func (c *DefaultContext) IncrCounter(key string, amount int) error {
  186. if v, ok := c.state.Load(key); ok {
  187. if vi, err := cast.ToInt(v, cast.STRICT); err != nil {
  188. return fmt.Errorf("state[%s] must be an int", key)
  189. } else {
  190. c.state.Store(key, vi+amount)
  191. }
  192. } else {
  193. c.state.Store(key, amount)
  194. }
  195. return nil
  196. }
  197. func (c *DefaultContext) GetCounter(key string) (int, error) {
  198. if v, ok := c.state.Load(key); ok {
  199. if vi, err := cast.ToInt(v, cast.STRICT); err != nil {
  200. return 0, fmt.Errorf("state[%s] is not a number, but %v", key, v)
  201. } else {
  202. return vi, nil
  203. }
  204. } else {
  205. c.state.Store(key, 0)
  206. return 0, nil
  207. }
  208. }
  209. func (c *DefaultContext) PutState(key string, value interface{}) error {
  210. c.state.Store(key, value)
  211. return nil
  212. }
  213. func (c *DefaultContext) GetState(key string) (interface{}, error) {
  214. if v, ok := c.state.Load(key); ok {
  215. return v, nil
  216. } else {
  217. return nil, nil
  218. }
  219. }
  220. func (c *DefaultContext) DeleteState(key string) error {
  221. c.state.Delete(key)
  222. return nil
  223. }
  224. func (c *DefaultContext) Snapshot() error {
  225. c.snapshot = cast.SyncMapToMap(c.state)
  226. return nil
  227. }
  228. func (c *DefaultContext) SaveState(checkpointId int64) error {
  229. err := c.store.SaveState(checkpointId, c.opId, c.snapshot)
  230. if err != nil {
  231. return err
  232. }
  233. c.snapshot = nil
  234. return nil
  235. }