default.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2021 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. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. "github.com/lf-edge/ekuiper/internal/topo/connection"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/cast"
  22. "github.com/sirupsen/logrus"
  23. "strings"
  24. "sync"
  25. "time"
  26. )
  27. const LoggerKey = "$$logger"
  28. type DefaultContext struct {
  29. ruleId string
  30. opId string
  31. instanceId int
  32. ctx context.Context
  33. err error
  34. //Only initialized after withMeta set
  35. store api.Store
  36. state *sync.Map
  37. snapshot map[string]interface{}
  38. // cache
  39. jsonEvalReg sync.Map
  40. }
  41. func Background() *DefaultContext {
  42. c := &DefaultContext{
  43. ctx: context.Background(),
  44. }
  45. return c
  46. }
  47. func WithValue(parent *DefaultContext, key, val interface{}) *DefaultContext {
  48. parent.ctx = context.WithValue(parent.ctx, key, val)
  49. return parent
  50. }
  51. //Implement context interface
  52. func (c *DefaultContext) Deadline() (deadline time.Time, ok bool) {
  53. return c.ctx.Deadline()
  54. }
  55. func (c *DefaultContext) Done() <-chan struct{} {
  56. return c.ctx.Done()
  57. }
  58. func (c *DefaultContext) Err() error {
  59. if c.err != nil {
  60. return c.err
  61. }
  62. return c.ctx.Err()
  63. }
  64. func (c *DefaultContext) Value(key interface{}) interface{} {
  65. return c.ctx.Value(key)
  66. }
  67. // Stream metas
  68. func (c *DefaultContext) GetContext() context.Context {
  69. return c.ctx
  70. }
  71. func (c *DefaultContext) GetLogger() api.Logger {
  72. l, ok := c.ctx.Value(LoggerKey).(*logrus.Entry)
  73. if l != nil && ok {
  74. return l
  75. }
  76. return conf.Log.WithField("caller", "default")
  77. }
  78. func (c *DefaultContext) GetRuleId() string {
  79. return c.ruleId
  80. }
  81. func (c *DefaultContext) GetOpId() string {
  82. return c.opId
  83. }
  84. func (c *DefaultContext) GetInstanceId() int {
  85. return c.instanceId
  86. }
  87. func (c *DefaultContext) GetRootPath() string {
  88. loc, _ := conf.GetLoc("")
  89. return loc
  90. }
  91. func (c *DefaultContext) SetError(err error) {
  92. c.err = err
  93. }
  94. func (c *DefaultContext) ParseDynamicProp(prop string, data interface{}) (interface{}, error) {
  95. // If not a json path, just return itself
  96. if !strings.HasPrefix(prop, "$") {
  97. return prop, nil
  98. }
  99. var (
  100. je conf.JsonPathEval
  101. err error
  102. )
  103. if raw, ok := c.jsonEvalReg.Load(prop); ok {
  104. je = raw.(conf.JsonPathEval)
  105. } else {
  106. je, err = conf.GetJsonPathEval(prop)
  107. if err != nil {
  108. return nil, err
  109. }
  110. c.jsonEvalReg.Store(prop, je)
  111. }
  112. return je.Eval(data)
  113. }
  114. func (c *DefaultContext) WithMeta(ruleId string, opId string, store api.Store) api.StreamContext {
  115. s, err := store.GetOpState(opId)
  116. if err != nil {
  117. c.GetLogger().Warnf("Initialize context store error for %s: %s", opId, err)
  118. }
  119. return &DefaultContext{
  120. ruleId: ruleId,
  121. opId: opId,
  122. instanceId: 0,
  123. ctx: c.ctx,
  124. store: store,
  125. state: s,
  126. jsonEvalReg: sync.Map{},
  127. }
  128. }
  129. func (c *DefaultContext) WithInstance(instanceId int) api.StreamContext {
  130. return &DefaultContext{
  131. instanceId: instanceId,
  132. ruleId: c.ruleId,
  133. opId: c.opId,
  134. ctx: c.ctx,
  135. state: c.state,
  136. }
  137. }
  138. func (c *DefaultContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  139. ctx, cancel := context.WithCancel(c.ctx)
  140. return &DefaultContext{
  141. ruleId: c.ruleId,
  142. opId: c.opId,
  143. instanceId: c.instanceId,
  144. ctx: ctx,
  145. state: c.state,
  146. }, cancel
  147. }
  148. func (c *DefaultContext) IncrCounter(key string, amount int) error {
  149. if v, ok := c.state.Load(key); ok {
  150. if vi, err := cast.ToInt(v, cast.STRICT); err != nil {
  151. return fmt.Errorf("state[%s] must be an int", key)
  152. } else {
  153. c.state.Store(key, vi+amount)
  154. }
  155. } else {
  156. c.state.Store(key, amount)
  157. }
  158. return nil
  159. }
  160. func (c *DefaultContext) GetCounter(key string) (int, error) {
  161. if v, ok := c.state.Load(key); ok {
  162. if vi, err := cast.ToInt(v, cast.STRICT); err != nil {
  163. return 0, fmt.Errorf("state[%s] is not a number, but %v", key, v)
  164. } else {
  165. return vi, nil
  166. }
  167. } else {
  168. c.state.Store(key, 0)
  169. return 0, nil
  170. }
  171. }
  172. func (c *DefaultContext) PutState(key string, value interface{}) error {
  173. c.state.Store(key, value)
  174. return nil
  175. }
  176. func (c *DefaultContext) GetState(key string) (interface{}, error) {
  177. if v, ok := c.state.Load(key); ok {
  178. return v, nil
  179. } else {
  180. return nil, nil
  181. }
  182. }
  183. func (c *DefaultContext) DeleteState(key string) error {
  184. c.state.Delete(key)
  185. return nil
  186. }
  187. func (c *DefaultContext) Snapshot() error {
  188. c.snapshot = cast.SyncMapToMap(c.state)
  189. return nil
  190. }
  191. func (c *DefaultContext) SaveState(checkpointId int64) error {
  192. err := c.store.SaveState(checkpointId, c.opId, c.snapshot)
  193. if err != nil {
  194. return err
  195. }
  196. c.snapshot = nil
  197. return nil
  198. }
  199. func (c *DefaultContext) GetConnection(connectSelector string) (interface{}, error) {
  200. return connection.GetConnection(connectSelector)
  201. }
  202. func (c *DefaultContext) ReleaseConnection(connectSelector string) {
  203. connection.ReleaseConnection(connectSelector)
  204. }