default.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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) SetError(err error) {
  70. c.err = err
  71. }
  72. func (c *DefaultContext) WithMeta(ruleId string, opId string, store api.Store) api.StreamContext {
  73. s, err := store.GetOpState(opId)
  74. if err != nil {
  75. c.GetLogger().Warnf("Initialize context store error for %s: %s", opId, err)
  76. }
  77. return &DefaultContext{
  78. ruleId: ruleId,
  79. opId: opId,
  80. instanceId: 0,
  81. ctx: c.ctx,
  82. store: store,
  83. state: s,
  84. }
  85. }
  86. func (c *DefaultContext) WithInstance(instanceId int) api.StreamContext {
  87. return &DefaultContext{
  88. instanceId: instanceId,
  89. ruleId: c.ruleId,
  90. opId: c.opId,
  91. ctx: c.ctx,
  92. state: c.state,
  93. }
  94. }
  95. func (c *DefaultContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  96. ctx, cancel := context.WithCancel(c.ctx)
  97. return &DefaultContext{
  98. ruleId: c.ruleId,
  99. opId: c.opId,
  100. instanceId: c.instanceId,
  101. ctx: ctx,
  102. state: c.state,
  103. }, cancel
  104. }
  105. func (c *DefaultContext) IncrCounter(key string, amount int) error {
  106. if v, ok := c.state.Load(key); ok {
  107. if vi, err := common.ToInt(v); err != nil {
  108. return fmt.Errorf("state[%s] must be an int", key)
  109. } else {
  110. c.state.Store(key, vi+amount)
  111. }
  112. } else {
  113. c.state.Store(key, amount)
  114. }
  115. return nil
  116. }
  117. func (c *DefaultContext) GetCounter(key string) (int, error) {
  118. if v, ok := c.state.Load(key); ok {
  119. if vi, err := common.ToInt(v); err != nil {
  120. return 0, fmt.Errorf("state[%s] is not a number, but %v", key, v)
  121. } else {
  122. return vi, nil
  123. }
  124. } else {
  125. c.state.Store(key, 0)
  126. return 0, nil
  127. }
  128. }
  129. func (c *DefaultContext) PutState(key string, value interface{}) error {
  130. c.state.Store(key, value)
  131. return nil
  132. }
  133. func (c *DefaultContext) GetState(key string) (interface{}, error) {
  134. if v, ok := c.state.Load(key); ok {
  135. return v, nil
  136. } else {
  137. return nil, nil
  138. }
  139. }
  140. func (c *DefaultContext) DeleteState(key string) error {
  141. c.state.Delete(key)
  142. return nil
  143. }
  144. func (c *DefaultContext) Snapshot() error {
  145. c.snapshot = common.SyncMapToMap(c.state)
  146. return nil
  147. }
  148. func (c *DefaultContext) SaveState(checkpointId int64) error {
  149. err := c.store.SaveState(checkpointId, c.opId, c.snapshot)
  150. if err != nil {
  151. return err
  152. }
  153. c.snapshot = nil
  154. return nil
  155. }