default.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package context
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/lf-edge/ekuiper/internal/conf"
  6. "github.com/lf-edge/ekuiper/pkg/api"
  7. "github.com/lf-edge/ekuiper/pkg/cast"
  8. "github.com/sirupsen/logrus"
  9. "sync"
  10. "time"
  11. )
  12. const LoggerKey = "$$logger"
  13. type DefaultContext struct {
  14. ruleId string
  15. opId string
  16. instanceId int
  17. ctx context.Context
  18. err error
  19. //Only initialized after withMeta set
  20. store api.Store
  21. state *sync.Map
  22. snapshot map[string]interface{}
  23. }
  24. func Background() *DefaultContext {
  25. c := &DefaultContext{
  26. ctx: context.Background(),
  27. }
  28. return c
  29. }
  30. func WithValue(parent *DefaultContext, key, val interface{}) *DefaultContext {
  31. parent.ctx = context.WithValue(parent.ctx, key, val)
  32. return parent
  33. }
  34. //Implement context interface
  35. func (c *DefaultContext) Deadline() (deadline time.Time, ok bool) {
  36. return c.ctx.Deadline()
  37. }
  38. func (c *DefaultContext) Done() <-chan struct{} {
  39. return c.ctx.Done()
  40. }
  41. func (c *DefaultContext) Err() error {
  42. if c.err != nil {
  43. return c.err
  44. }
  45. return c.ctx.Err()
  46. }
  47. func (c *DefaultContext) Value(key interface{}) interface{} {
  48. return c.ctx.Value(key)
  49. }
  50. // Stream metas
  51. func (c *DefaultContext) GetContext() context.Context {
  52. return c.ctx
  53. }
  54. func (c *DefaultContext) GetLogger() api.Logger {
  55. l, ok := c.ctx.Value(LoggerKey).(*logrus.Entry)
  56. if l != nil && ok {
  57. return l
  58. }
  59. return conf.Log.WithField("caller", "default")
  60. }
  61. func (c *DefaultContext) GetRuleId() string {
  62. return c.ruleId
  63. }
  64. func (c *DefaultContext) GetOpId() string {
  65. return c.opId
  66. }
  67. func (c *DefaultContext) GetInstanceId() int {
  68. return c.instanceId
  69. }
  70. func (c *DefaultContext) GetRootPath() string {
  71. loc, _ := conf.GetLoc("")
  72. return loc
  73. }
  74. func (c *DefaultContext) SetError(err error) {
  75. c.err = err
  76. }
  77. func (c *DefaultContext) WithMeta(ruleId string, opId string, store api.Store) api.StreamContext {
  78. s, err := store.GetOpState(opId)
  79. if err != nil {
  80. c.GetLogger().Warnf("Initialize context store error for %s: %s", opId, err)
  81. }
  82. return &DefaultContext{
  83. ruleId: ruleId,
  84. opId: opId,
  85. instanceId: 0,
  86. ctx: c.ctx,
  87. store: store,
  88. state: s,
  89. }
  90. }
  91. func (c *DefaultContext) WithInstance(instanceId int) api.StreamContext {
  92. return &DefaultContext{
  93. instanceId: instanceId,
  94. ruleId: c.ruleId,
  95. opId: c.opId,
  96. ctx: c.ctx,
  97. state: c.state,
  98. }
  99. }
  100. func (c *DefaultContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  101. ctx, cancel := context.WithCancel(c.ctx)
  102. return &DefaultContext{
  103. ruleId: c.ruleId,
  104. opId: c.opId,
  105. instanceId: c.instanceId,
  106. ctx: ctx,
  107. state: c.state,
  108. }, cancel
  109. }
  110. func (c *DefaultContext) IncrCounter(key string, amount int) error {
  111. if v, ok := c.state.Load(key); ok {
  112. if vi, err := cast.ToInt(v, cast.STRICT); err != nil {
  113. return fmt.Errorf("state[%s] must be an int", key)
  114. } else {
  115. c.state.Store(key, vi+amount)
  116. }
  117. } else {
  118. c.state.Store(key, amount)
  119. }
  120. return nil
  121. }
  122. func (c *DefaultContext) GetCounter(key string) (int, error) {
  123. if v, ok := c.state.Load(key); ok {
  124. if vi, err := cast.ToInt(v, cast.STRICT); err != nil {
  125. return 0, fmt.Errorf("state[%s] is not a number, but %v", key, v)
  126. } else {
  127. return vi, nil
  128. }
  129. } else {
  130. c.state.Store(key, 0)
  131. return 0, nil
  132. }
  133. }
  134. func (c *DefaultContext) PutState(key string, value interface{}) error {
  135. c.state.Store(key, value)
  136. return nil
  137. }
  138. func (c *DefaultContext) GetState(key string) (interface{}, error) {
  139. if v, ok := c.state.Load(key); ok {
  140. return v, nil
  141. } else {
  142. return nil, nil
  143. }
  144. }
  145. func (c *DefaultContext) DeleteState(key string) error {
  146. c.state.Delete(key)
  147. return nil
  148. }
  149. func (c *DefaultContext) Snapshot() error {
  150. c.snapshot = cast.SyncMapToMap(c.state)
  151. return nil
  152. }
  153. func (c *DefaultContext) SaveState(checkpointId int64) error {
  154. err := c.store.SaveState(checkpointId, c.opId, c.snapshot)
  155. if err != nil {
  156. return err
  157. }
  158. c.snapshot = nil
  159. return nil
  160. }