lookup_node.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 node
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/internal/topo/lookup"
  19. "github.com/lf-edge/ekuiper/internal/topo/lookup/cache"
  20. nodeConf "github.com/lf-edge/ekuiper/internal/topo/node/conf"
  21. "github.com/lf-edge/ekuiper/internal/topo/node/metric"
  22. "github.com/lf-edge/ekuiper/internal/xsql"
  23. "github.com/lf-edge/ekuiper/pkg/api"
  24. "github.com/lf-edge/ekuiper/pkg/ast"
  25. "github.com/lf-edge/ekuiper/pkg/cast"
  26. "github.com/lf-edge/ekuiper/pkg/infra"
  27. )
  28. type LookupConf struct {
  29. Cache bool `json:"cache"`
  30. CacheTTL int `json:"cacheTtl"`
  31. CacheMissingKey bool `json:"cacheMissingKey"`
  32. }
  33. // LookupNode will look up the data from the external source when receiving an event
  34. type LookupNode struct {
  35. *defaultSinkNode
  36. statManager metric.StatManager
  37. sourceType string
  38. joinType ast.JoinType
  39. vals []ast.Expr
  40. srcOptions *ast.Options
  41. conf *LookupConf
  42. fields []string
  43. keys []string
  44. }
  45. func NewLookupNode(name string, fields []string, keys []string, joinType ast.JoinType, vals []ast.Expr, srcOptions *ast.Options, options *api.RuleOption) (*LookupNode, error) {
  46. t := srcOptions.TYPE
  47. if t == "" {
  48. return nil, fmt.Errorf("source type is not specified")
  49. }
  50. props := nodeConf.GetSourceConf(t, srcOptions)
  51. lookupConf := &LookupConf{}
  52. if lc, ok := props["lookup"].(map[string]interface{}); ok {
  53. err := cast.MapToStruct(lc, lookupConf)
  54. if err != nil {
  55. return nil, err
  56. }
  57. }
  58. n := &LookupNode{
  59. fields: fields,
  60. keys: keys,
  61. srcOptions: srcOptions,
  62. conf: lookupConf,
  63. sourceType: t,
  64. joinType: joinType,
  65. vals: vals,
  66. }
  67. n.defaultSinkNode = &defaultSinkNode{
  68. input: make(chan interface{}, options.BufferLength),
  69. defaultNode: &defaultNode{
  70. outputs: make(map[string]chan<- interface{}),
  71. name: name,
  72. sendError: options.SendError,
  73. },
  74. }
  75. return n, nil
  76. }
  77. func (n *LookupNode) Exec(ctx api.StreamContext, errCh chan<- error) {
  78. n.ctx = ctx
  79. log := ctx.GetLogger()
  80. log.Debugf("LookupNode %s is started", n.name)
  81. if len(n.outputs) <= 0 {
  82. infra.DrainError(ctx, fmt.Errorf("no output channel found"), errCh)
  83. return
  84. }
  85. stats, err := metric.NewStatManager(ctx, "op")
  86. if err != nil {
  87. infra.DrainError(ctx, fmt.Errorf("no output channel found"), errCh)
  88. return
  89. }
  90. n.statManager = stats
  91. n.statManagers = []metric.StatManager{stats}
  92. go func() {
  93. err := infra.SafeRun(func() error {
  94. ns, err := lookup.Attach(n.name)
  95. if err != nil {
  96. return err
  97. }
  98. defer lookup.Detach(n.name)
  99. fv, _ := xsql.NewFunctionValuersForOp(ctx)
  100. var c *cache.Cache
  101. if n.conf.Cache {
  102. c = cache.NewCache(n.conf.CacheTTL, n.conf.CacheMissingKey)
  103. defer c.Close()
  104. }
  105. // Start the lookup source loop
  106. for {
  107. log.Debugf("LookupNode %s is looping", n.name)
  108. select {
  109. // process incoming item from both streams(transformed) and tables
  110. case item, opened := <-n.input:
  111. processed := false
  112. if item, processed = n.preprocess(item); processed {
  113. break
  114. }
  115. n.statManager.IncTotalRecordsIn()
  116. n.statManager.ProcessTimeStart()
  117. if !opened {
  118. n.statManager.IncTotalExceptions("input channel closed")
  119. break
  120. }
  121. switch d := item.(type) {
  122. case error:
  123. _ = n.Broadcast(d)
  124. n.statManager.IncTotalExceptions(d.Error())
  125. case *xsql.WatermarkTuple:
  126. _ = n.Broadcast(d)
  127. case xsql.TupleRow:
  128. log.Debugf("Lookup Node receive tuple input %s", d)
  129. n.statManager.ProcessTimeStart()
  130. sets := &xsql.JoinTuples{Content: make([]*xsql.JoinTuple, 0)}
  131. err := n.lookup(ctx, d, fv, ns, sets, c)
  132. if err != nil {
  133. _ = n.Broadcast(err)
  134. n.statManager.IncTotalExceptions(err.Error())
  135. } else {
  136. _ = n.Broadcast(sets)
  137. n.statManager.IncTotalRecordsOut()
  138. }
  139. n.statManager.ProcessTimeEnd()
  140. n.statManager.SetBufferLength(int64(len(n.input)))
  141. case *xsql.WindowTuples:
  142. log.Debugf("Lookup Node receive window input %s", d)
  143. n.statManager.ProcessTimeStart()
  144. sets := &xsql.JoinTuples{Content: make([]*xsql.JoinTuple, 0)}
  145. err := d.Range(func(i int, r xsql.ReadonlyRow) (bool, error) {
  146. tr, ok := r.(xsql.TupleRow)
  147. if !ok {
  148. return false, fmt.Errorf("Invalid window element, must be a tuple row but got %v", r)
  149. }
  150. err := n.lookup(ctx, tr, fv, ns, sets, c)
  151. if err != nil {
  152. return false, err
  153. }
  154. return true, nil
  155. })
  156. if err != nil {
  157. _ = n.Broadcast(err)
  158. n.statManager.IncTotalExceptions(err.Error())
  159. } else {
  160. _ = n.Broadcast(sets)
  161. n.statManager.IncTotalRecordsOut()
  162. }
  163. n.statManager.ProcessTimeEnd()
  164. n.statManager.SetBufferLength(int64(len(n.input)))
  165. default:
  166. e := fmt.Errorf("run lookup node error: invalid input type but got %[1]T(%[1]v)", d)
  167. _ = n.Broadcast(e)
  168. n.statManager.IncTotalExceptions(e.Error())
  169. }
  170. case <-ctx.Done():
  171. log.Infoln("Cancelling lookup node....")
  172. return nil
  173. }
  174. }
  175. })
  176. if err != nil {
  177. infra.DrainError(ctx, err, errCh)
  178. }
  179. }()
  180. }
  181. // lookup will lookup the cache firstly, if expires, read the external source
  182. func (n *LookupNode) lookup(ctx api.StreamContext, d xsql.TupleRow, fv *xsql.FunctionValuer, ns api.LookupSource, tuples *xsql.JoinTuples, c *cache.Cache) error {
  183. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(d, fv)}
  184. cvs := make([]interface{}, len(n.vals))
  185. hasNil := false
  186. for i, val := range n.vals {
  187. cvs[i] = ve.Eval(val)
  188. if cvs[i] == nil {
  189. hasNil = true
  190. }
  191. }
  192. var (
  193. r []api.SourceTuple
  194. e error
  195. ok bool
  196. )
  197. if !hasNil { // if any of the value is nil, the lookup will always return empty result
  198. if c != nil {
  199. k := fmt.Sprintf("%v", cvs)
  200. r, ok = c.Get(k)
  201. if !ok {
  202. r, e = ns.Lookup(ctx, n.fields, n.keys, cvs)
  203. if e != nil {
  204. return e
  205. }
  206. c.Set(k, r)
  207. }
  208. } else {
  209. r, e = ns.Lookup(ctx, n.fields, n.keys, cvs)
  210. }
  211. }
  212. if e != nil {
  213. return e
  214. } else {
  215. if len(r) == 0 {
  216. if n.joinType == ast.LEFT_JOIN {
  217. merged := &xsql.JoinTuple{}
  218. merged.AddTuple(d)
  219. tuples.Content = append(tuples.Content, merged)
  220. } else {
  221. ctx.GetLogger().Debugf("Lookup Node %s no result found for tuple %s", n.name, d)
  222. return nil
  223. }
  224. }
  225. for _, v := range r {
  226. merged := &xsql.JoinTuple{}
  227. merged.AddTuple(d)
  228. t := &xsql.Tuple{
  229. Emitter: n.name,
  230. Message: v.Message(),
  231. Metadata: v.Meta(),
  232. Timestamp: conf.GetNowInMilli(),
  233. }
  234. merged.AddTuple(t)
  235. tuples.Content = append(tuples.Content, merged)
  236. }
  237. return nil
  238. }
  239. }
  240. func (n *LookupNode) merge(ctx api.StreamContext, d xsql.TupleRow, r []map[string]interface{}) {
  241. n.statManager.ProcessTimeStart()
  242. sets := &xsql.JoinTuples{Content: make([]*xsql.JoinTuple, 0)}
  243. if len(r) == 0 {
  244. if n.joinType == ast.LEFT_JOIN {
  245. merged := &xsql.JoinTuple{}
  246. merged.AddTuple(d)
  247. sets.Content = append(sets.Content, merged)
  248. } else {
  249. ctx.GetLogger().Debugf("Lookup Node %s no result found for tuple %s", n.name, d)
  250. return
  251. }
  252. }
  253. for _, v := range r {
  254. merged := &xsql.JoinTuple{}
  255. merged.AddTuple(d)
  256. t := &xsql.Tuple{
  257. Emitter: n.name,
  258. Message: v,
  259. Timestamp: conf.GetNowInMilli(),
  260. }
  261. merged.AddTuple(t)
  262. sets.Content = append(sets.Content, merged)
  263. }
  264. _ = n.Broadcast(sets)
  265. n.statManager.ProcessTimeEnd()
  266. n.statManager.IncTotalRecordsOut()
  267. n.statManager.SetBufferLength(int64(len(n.input)))
  268. }