default.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/emqx/kuiper/xstream/states"
  8. "github.com/sirupsen/logrus"
  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. state states.StateContext
  19. }
  20. func Background() *DefaultContext {
  21. c := &DefaultContext{
  22. ctx: context.Background(),
  23. }
  24. s := states.NewStateContext(states.MEMORY, c.GetLogger())
  25. c.state = s
  26. return c
  27. }
  28. func WithValue(parent *DefaultContext, key, val interface{}) *DefaultContext {
  29. parent.ctx = context.WithValue(parent.ctx, key, val)
  30. return parent
  31. }
  32. //Implement context interface
  33. func (c *DefaultContext) Deadline() (deadline time.Time, ok bool) {
  34. return c.ctx.Deadline()
  35. }
  36. func (c *DefaultContext) Done() <-chan struct{} {
  37. return c.ctx.Done()
  38. }
  39. func (c *DefaultContext) Err() error {
  40. if c.err != nil {
  41. return c.err
  42. }
  43. return c.ctx.Err()
  44. }
  45. func (c *DefaultContext) Value(key interface{}) interface{} {
  46. return c.ctx.Value(key)
  47. }
  48. // Stream metas
  49. func (c *DefaultContext) GetContext() context.Context {
  50. return c.ctx
  51. }
  52. func (c *DefaultContext) GetLogger() api.Logger {
  53. l, ok := c.ctx.Value(LoggerKey).(*logrus.Entry)
  54. if l != nil && ok {
  55. return l
  56. }
  57. return common.Log.WithField("caller", "default")
  58. }
  59. func (c *DefaultContext) GetRuleId() string {
  60. return c.ruleId
  61. }
  62. func (c *DefaultContext) GetOpId() string {
  63. return c.opId
  64. }
  65. func (c *DefaultContext) GetInstanceId() int {
  66. return c.instanceId
  67. }
  68. func (c *DefaultContext) SetError(err error) {
  69. c.err = err
  70. }
  71. func (c *DefaultContext) WithMeta(ruleId string, opId string) api.StreamContext {
  72. return &DefaultContext{
  73. ruleId: ruleId,
  74. opId: opId,
  75. instanceId: 0,
  76. ctx: c.ctx,
  77. state: c.state,
  78. }
  79. }
  80. func (c *DefaultContext) WithInstance(instanceId int) api.StreamContext {
  81. return &DefaultContext{
  82. instanceId: instanceId,
  83. ruleId: c.ruleId,
  84. opId: c.opId,
  85. ctx: c.ctx,
  86. state: c.state,
  87. }
  88. }
  89. func (c *DefaultContext) WithCancel() (api.StreamContext, context.CancelFunc) {
  90. ctx, cancel := context.WithCancel(c.ctx)
  91. return &DefaultContext{
  92. ruleId: c.ruleId,
  93. opId: c.opId,
  94. instanceId: c.instanceId,
  95. ctx: ctx,
  96. state: c.state,
  97. }, cancel
  98. }
  99. func (c *DefaultContext) IncrCounter(key string, amount int) error {
  100. return c.state.IncrCounter(key, amount)
  101. }
  102. func (c *DefaultContext) GetCounter(key string) (int, error) {
  103. return c.state.GetCounter(key)
  104. }
  105. func (c *DefaultContext) PutState(key string, value interface{}) error {
  106. return c.state.PutState(key, value)
  107. }
  108. func (c *DefaultContext) GetState(key string) (interface{}, error) {
  109. return c.state.GetState(key)
  110. }
  111. func (c *DefaultContext) DeleteState(key string) error {
  112. return c.state.DeleteState(key)
  113. }
  114. func (c *DefaultContext) SaveState(checkpointId int64) error {
  115. fmt.Printf("placeholder for context save state for checkpoint %d", checkpointId)
  116. return nil
  117. }