default.go 3.8 KB

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