stream.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2021-2023 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 api
  15. import (
  16. "context"
  17. "sync"
  18. "time"
  19. )
  20. type SourceTuple interface {
  21. Message() map[string]interface{}
  22. Meta() map[string]interface{}
  23. Timestamp() time.Time
  24. }
  25. type DefaultSourceTuple struct {
  26. Mess map[string]interface{} `json:"message"`
  27. M map[string]interface{} `json:"meta"`
  28. Time time.Time `json:"timestamp"`
  29. }
  30. func NewDefaultSourceTuple(message map[string]interface{}, meta map[string]interface{}) *DefaultSourceTuple {
  31. return &DefaultSourceTuple{
  32. Mess: message,
  33. M: meta,
  34. Time: time.Now(),
  35. }
  36. }
  37. func NewDefaultSourceTupleWithTime(message map[string]interface{}, meta map[string]interface{}, timestamp time.Time) *DefaultSourceTuple {
  38. return &DefaultSourceTuple{
  39. Mess: message,
  40. M: meta,
  41. Time: timestamp,
  42. }
  43. }
  44. func (t *DefaultSourceTuple) Message() map[string]interface{} {
  45. return t.Mess
  46. }
  47. func (t *DefaultSourceTuple) Meta() map[string]interface{} {
  48. return t.M
  49. }
  50. func (t *DefaultSourceTuple) Timestamp() time.Time {
  51. return t.Time
  52. }
  53. type Logger interface {
  54. Debug(args ...interface{})
  55. Info(args ...interface{})
  56. Warn(args ...interface{})
  57. Error(args ...interface{})
  58. Debugln(args ...interface{})
  59. Infoln(args ...interface{})
  60. Warnln(args ...interface{})
  61. Errorln(args ...interface{})
  62. Debugf(format string, args ...interface{})
  63. Infof(format string, args ...interface{})
  64. Warnf(format string, args ...interface{})
  65. Errorf(format string, args ...interface{})
  66. }
  67. type Store interface {
  68. SaveState(checkpointId int64, opId string, state map[string]interface{}) error
  69. // SaveCheckpoint saves the whole checkpoint state into storage
  70. SaveCheckpoint(checkpointId int64) error
  71. GetOpState(opId string) (*sync.Map, error)
  72. Clean() error
  73. }
  74. type Closable interface {
  75. Close(ctx StreamContext) error
  76. }
  77. type Source interface {
  78. // Open Should be sync function for normal case. The container will run it in go func
  79. Open(ctx StreamContext, consumer chan<- SourceTuple, errCh chan<- error)
  80. // Configure Called during initialization. Configure the source with the data source(e.g. topic for mqtt) and the properties
  81. // read from the yaml
  82. Configure(datasource string, props map[string]interface{}) error
  83. Closable
  84. }
  85. type LookupSource interface {
  86. // Open creates the connection to the external data source
  87. Open(ctx StreamContext) error
  88. // Configure Called during initialization. Configure the source with the data source(e.g. topic for mqtt) and the properties
  89. // read from the yaml
  90. Configure(datasource string, props map[string]interface{}) error
  91. // Lookup receive lookup values to construct the query and return query results
  92. Lookup(ctx StreamContext, fields []string, keys []string, values []interface{}) ([]SourceTuple, error)
  93. Closable
  94. }
  95. type Sink interface {
  96. // Open Should be sync function for normal case. The container will run it in go func
  97. Open(ctx StreamContext) error
  98. // Configure Called during initialization. Configure the sink with the properties from rule action definition
  99. Configure(props map[string]interface{}) error
  100. // Collect Called when each row of data has transferred to this sink
  101. Collect(ctx StreamContext, data interface{}) error
  102. Closable
  103. }
  104. type Emitter interface {
  105. AddOutput(chan<- interface{}, string) error
  106. }
  107. type Collector interface {
  108. GetInput() (chan<- interface{}, string)
  109. }
  110. type TopNode interface {
  111. GetName() string
  112. }
  113. type Rewindable interface {
  114. GetOffset() (interface{}, error)
  115. Rewind(offset interface{}) error
  116. }
  117. type RuleOption struct {
  118. IsEventTime bool `json:"isEventTime" yaml:"isEventTime"`
  119. LateTol int64 `json:"lateTolerance" yaml:"lateTolerance"`
  120. Concurrency int `json:"concurrency" yaml:"concurrency"`
  121. BufferLength int `json:"bufferLength" yaml:"bufferLength"`
  122. SendMetaToSink bool `json:"sendMetaToSink" yaml:"sendMetaToSink"`
  123. SendError bool `json:"sendError" yaml:"sendError"`
  124. Qos Qos `json:"qos" yaml:"qos"`
  125. CheckpointInterval int `json:"checkpointInterval" yaml:"checkpointInterval"`
  126. Restart *RestartStrategy `json:"restartStrategy" yaml:"restartStrategy"`
  127. Cron string `json:"cron" yaml:"cron"`
  128. Duration string `json:"duration" yaml:"duration"`
  129. CronDatetimeRange []DatetimeRange `json:"cronDatetimeRange" yaml:"cronDatetimeRange"`
  130. }
  131. type DatetimeRange struct {
  132. Begin string `json:"begin" yaml:"begin"`
  133. End string `json:"end" yaml:"end"`
  134. }
  135. type RestartStrategy struct {
  136. Attempts int `json:"attempts" yaml:"attempts"`
  137. Delay int `json:"delay" yaml:"delay"`
  138. Multiplier float64 `json:"multiplier" yaml:"multiplier"`
  139. MaxDelay int `json:"maxDelay" yaml:"maxDelay"`
  140. JitterFactor float64 `json:"jitter" yaml:"jitter"`
  141. }
  142. type PrintableTopo struct {
  143. Sources []string `json:"sources"`
  144. Edges map[string][]interface{} `json:"edges"`
  145. }
  146. type GraphNode struct {
  147. Type string `json:"type"`
  148. NodeType string `json:"nodeType"`
  149. Props map[string]interface{} `json:"props"`
  150. // UI is a placeholder for ui properties
  151. UI map[string]interface{} `json:"ui"`
  152. }
  153. // SourceMeta is the meta data of a source node. It describes what existed stream/table to refer to.
  154. // It is part of the Props in the GraphNode and it is optional
  155. type SourceMeta struct {
  156. SourceName string `json:"sourceName"` // the name of the stream or table
  157. SourceType string `json:"sourceType"` // stream or table
  158. }
  159. type RuleGraph struct {
  160. Nodes map[string]*GraphNode `json:"nodes"`
  161. Topo *PrintableTopo `json:"topo"`
  162. }
  163. // Rule the definition of the business logic
  164. // Sql and Graph are mutually exclusive, at least one of them should be set
  165. type Rule struct {
  166. Triggered bool `json:"triggered"`
  167. Id string `json:"id,omitempty"`
  168. Name string `json:"name,omitempty"` // The display name of a rule
  169. Sql string `json:"sql,omitempty"`
  170. Graph *RuleGraph `json:"graph,omitempty"`
  171. Actions []map[string]interface{} `json:"actions,omitempty"`
  172. Options *RuleOption `json:"options,omitempty"`
  173. }
  174. func (r *Rule) IsScheduleRule() bool {
  175. if r.Options == nil {
  176. return false
  177. }
  178. return len(r.Options.Cron) > 0 && len(r.Options.Duration) > 0
  179. }
  180. type StreamContext interface {
  181. context.Context
  182. GetLogger() Logger
  183. GetRuleId() string
  184. GetOpId() string
  185. GetInstanceId() int
  186. GetRootPath() string
  187. WithMeta(ruleId string, opId string, store Store) StreamContext
  188. WithInstance(instanceId int) StreamContext
  189. WithCancel() (StreamContext, context.CancelFunc)
  190. SetError(e error)
  191. // IncrCounter State handling
  192. IncrCounter(key string, amount int) error
  193. GetCounter(key string) (int, error)
  194. PutState(key string, value interface{}) error
  195. GetState(key string) (interface{}, error)
  196. DeleteState(key string) error
  197. // ParseTemplate parse the template string with the given data
  198. ParseTemplate(template string, data interface{}) (string, error)
  199. // ParseJsonPath parse the jsonPath string with the given data
  200. ParseJsonPath(jsonPath string, data interface{}) (interface{}, error)
  201. // TransformOutput Transform output according to the properties including dataTemplate, sendSingle, fields
  202. // TransformOutput first transform data through the dataTemplate property,and then select data based on the fields property
  203. // It is recommended that you do not configure both the dataTemplate property and the fields property.
  204. // The second parameter is whether the data is transformed or just return as its json format.
  205. TransformOutput(data interface{}) ([]byte, bool, error)
  206. // Decode is set in the source according to the format.
  207. // It decodes byte array into map or map slice.
  208. Decode(data []byte) (map[string]interface{}, error)
  209. DecodeIntoList(data []byte) ([]map[string]interface{}, error)
  210. }
  211. type Operator interface {
  212. Emitter
  213. Collector
  214. Exec(StreamContext, chan<- error)
  215. GetName() string
  216. GetMetrics() [][]interface{}
  217. }
  218. type FunctionContext interface {
  219. StreamContext
  220. GetFuncId() int
  221. }
  222. type Function interface {
  223. // Validate The argument is a list of xsql.Expr
  224. Validate(args []interface{}) error
  225. // Exec Execute the function, return the result and if execution is successful.
  226. // If execution fails, return the error and false.
  227. Exec(args []interface{}, ctx FunctionContext) (interface{}, bool)
  228. // IsAggregate If this function is an aggregate function. Each parameter of an aggregate function will be a slice
  229. IsAggregate() bool
  230. }
  231. const (
  232. AtMostOnce Qos = iota
  233. AtLeastOnce
  234. ExactlyOnce
  235. )
  236. type Qos int
  237. type MessageClient interface {
  238. Subscribe(c StreamContext, subChan []TopicChannel, messageErrors chan error, params map[string]interface{}) error
  239. Publish(c StreamContext, topic string, message []byte, params map[string]interface{}) error
  240. }
  241. // TopicChannel is the data structure for subscriber
  242. type TopicChannel struct {
  243. // Topic for subscriber to filter on if any
  244. Topic string
  245. // Messages is the returned message channel for the subscriber
  246. Messages chan<- interface{}
  247. }