lookup_node.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. go func() {
  92. err := infra.SafeRun(func() error {
  93. ns, err := lookup.Attach(n.name)
  94. if err != nil {
  95. return err
  96. }
  97. defer lookup.Detach(n.name)
  98. fv, _ := xsql.NewFunctionValuersForOp(ctx)
  99. var c *cache.Cache
  100. if n.conf.Cache {
  101. c = cache.NewCache(n.conf.CacheTTL, n.conf.CacheMissingKey)
  102. defer c.Close()
  103. }
  104. // Start the lookup source loop
  105. for {
  106. log.Debugf("LookupNode %s is looping", n.name)
  107. select {
  108. // process incoming item from both streams(transformed) and tables
  109. case item, opened := <-n.input:
  110. processed := false
  111. if item, processed = n.preprocess(item); processed {
  112. break
  113. }
  114. n.statManager.IncTotalRecordsIn()
  115. n.statManager.ProcessTimeStart()
  116. if !opened {
  117. n.statManager.IncTotalExceptions("input channel closed")
  118. break
  119. }
  120. switch d := item.(type) {
  121. case error:
  122. _ = n.Broadcast(d)
  123. n.statManager.IncTotalExceptions(d.Error())
  124. case *xsql.WatermarkTuple:
  125. _ = n.Broadcast(d)
  126. case xsql.TupleRow:
  127. log.Debugf("Lookup Node receive tuple input %s", d)
  128. n.statManager.ProcessTimeStart()
  129. sets := &xsql.JoinTuples{Content: make([]*xsql.JoinTuple, 0)}
  130. err := n.lookup(ctx, d, fv, ns, sets, c)
  131. if err != nil {
  132. _ = n.Broadcast(err)
  133. n.statManager.IncTotalExceptions(err.Error())
  134. } else {
  135. _ = n.Broadcast(sets)
  136. n.statManager.IncTotalRecordsOut()
  137. }
  138. n.statManager.ProcessTimeEnd()
  139. n.statManager.SetBufferLength(int64(len(n.input)))
  140. case *xsql.WindowTuples:
  141. log.Debugf("Lookup Node receive window input %s", d)
  142. n.statManager.ProcessTimeStart()
  143. sets := &xsql.JoinTuples{Content: make([]*xsql.JoinTuple, 0)}
  144. err := d.Range(func(i int, r xsql.ReadonlyRow) (bool, error) {
  145. tr, ok := r.(xsql.TupleRow)
  146. if !ok {
  147. return false, fmt.Errorf("Invalid window element, must be a tuple row but got %v", r)
  148. }
  149. err := n.lookup(ctx, tr, fv, ns, sets, c)
  150. if err != nil {
  151. return false, err
  152. }
  153. return true, nil
  154. })
  155. if err != nil {
  156. _ = n.Broadcast(err)
  157. n.statManager.IncTotalExceptions(err.Error())
  158. } else {
  159. _ = n.Broadcast(sets)
  160. n.statManager.IncTotalRecordsOut()
  161. }
  162. n.statManager.ProcessTimeEnd()
  163. n.statManager.SetBufferLength(int64(len(n.input)))
  164. default:
  165. e := fmt.Errorf("run lookup node error: invalid input type but got %[1]T(%[1]v)", d)
  166. _ = n.Broadcast(e)
  167. n.statManager.IncTotalExceptions(e.Error())
  168. }
  169. case <-ctx.Done():
  170. log.Infoln("Cancelling lookup node....")
  171. return nil
  172. }
  173. }
  174. })
  175. if err != nil {
  176. infra.DrainError(ctx, err, errCh)
  177. }
  178. }()
  179. }
  180. // lookup will lookup the cache firstly, if expires, read the external source
  181. func (n *LookupNode) lookup(ctx api.StreamContext, d xsql.TupleRow, fv *xsql.FunctionValuer, ns api.LookupSource, tuples *xsql.JoinTuples, c *cache.Cache) error {
  182. ve := &xsql.ValuerEval{Valuer: xsql.MultiValuer(d, fv)}
  183. cvs := make([]interface{}, len(n.vals))
  184. hasNil := false
  185. for i, val := range n.vals {
  186. cvs[i] = ve.Eval(val)
  187. if cvs[i] == nil {
  188. hasNil = true
  189. }
  190. }
  191. var (
  192. r []api.SourceTuple
  193. e error
  194. ok bool
  195. )
  196. if !hasNil { // if any of the value is nil, the lookup will always return empty result
  197. if c != nil {
  198. k := fmt.Sprintf("%v", cvs)
  199. r, ok = c.Get(k)
  200. if !ok {
  201. r, e = ns.Lookup(ctx, n.fields, n.keys, cvs)
  202. if e != nil {
  203. return e
  204. }
  205. c.Set(k, r)
  206. }
  207. } else {
  208. r, e = ns.Lookup(ctx, n.fields, n.keys, cvs)
  209. }
  210. }
  211. if e != nil {
  212. return e
  213. } else {
  214. if len(r) == 0 {
  215. if n.joinType == ast.LEFT_JOIN {
  216. merged := &xsql.JoinTuple{}
  217. merged.AddTuple(d)
  218. tuples.Content = append(tuples.Content, merged)
  219. } else {
  220. ctx.GetLogger().Debugf("Lookup Node %s no result found for tuple %s", n.name, d)
  221. return nil
  222. }
  223. }
  224. for _, v := range r {
  225. merged := &xsql.JoinTuple{}
  226. merged.AddTuple(d)
  227. t := &xsql.Tuple{
  228. Emitter: n.name,
  229. Message: v.Message(),
  230. Metadata: v.Meta(),
  231. Timestamp: conf.GetNowInMilli(),
  232. }
  233. merged.AddTuple(t)
  234. tuples.Content = append(tuples.Content, merged)
  235. }
  236. return nil
  237. }
  238. }
  239. func (n *LookupNode) GetMetrics() [][]interface{} {
  240. if n.statManager != nil {
  241. return [][]interface{}{
  242. n.statManager.GetMetrics(),
  243. }
  244. } else {
  245. return nil
  246. }
  247. }
  248. func (n *LookupNode) merge(ctx api.StreamContext, d xsql.TupleRow, r []map[string]interface{}) {
  249. n.statManager.ProcessTimeStart()
  250. sets := &xsql.JoinTuples{Content: make([]*xsql.JoinTuple, 0)}
  251. if len(r) == 0 {
  252. if n.joinType == ast.LEFT_JOIN {
  253. merged := &xsql.JoinTuple{}
  254. merged.AddTuple(d)
  255. sets.Content = append(sets.Content, merged)
  256. } else {
  257. ctx.GetLogger().Debugf("Lookup Node %s no result found for tuple %s", n.name, d)
  258. return
  259. }
  260. }
  261. for _, v := range r {
  262. merged := &xsql.JoinTuple{}
  263. merged.AddTuple(d)
  264. t := &xsql.Tuple{
  265. Emitter: n.name,
  266. Message: v,
  267. Timestamp: conf.GetNowInMilli(),
  268. }
  269. merged.AddTuple(t)
  270. sets.Content = append(sets.Content, merged)
  271. }
  272. _ = n.Broadcast(sets)
  273. n.statManager.ProcessTimeEnd()
  274. n.statManager.IncTotalRecordsOut()
  275. n.statManager.SetBufferLength(int64(len(n.input)))
  276. }