123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // Copyright 2021-2022 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package node
- import (
- "fmt"
- "github.com/lf-edge/ekuiper/internal/topo/node/metric"
- "github.com/lf-edge/ekuiper/internal/xsql"
- "github.com/lf-edge/ekuiper/pkg/api"
- "github.com/lf-edge/ekuiper/pkg/infra"
- "sync"
- )
- // UnOperation interface represents unary operations (i.e. Map, Filter, etc)
- type UnOperation interface {
- Apply(ctx api.StreamContext, data interface{}, fv *xsql.FunctionValuer, afv *xsql.AggregateFunctionValuer) interface{}
- }
- // UnFunc implements UnOperation as type func (context.Context, interface{})
- type UnFunc func(api.StreamContext, interface{}) interface{}
- // Apply implements UnOperation.Apply method
- func (f UnFunc) Apply(ctx api.StreamContext, data interface{}) interface{} {
- return f(ctx, data)
- }
- type UnaryOperator struct {
- *defaultSinkNode
- op UnOperation
- mutex sync.RWMutex
- cancelled bool
- }
- // NewUnary creates *UnaryOperator value
- func New(name string, options *api.RuleOption) *UnaryOperator {
- return &UnaryOperator{
- defaultSinkNode: &defaultSinkNode{
- input: make(chan interface{}, options.BufferLength),
- defaultNode: &defaultNode{
- name: name,
- outputs: make(map[string]chan<- interface{}),
- concurrency: 1,
- sendError: options.SendError,
- },
- },
- }
- }
- // SetOperation sets the executor operation
- func (o *UnaryOperator) SetOperation(op UnOperation) {
- o.op = op
- }
- // Exec is the entry point for the executor
- func (o *UnaryOperator) Exec(ctx api.StreamContext, errCh chan<- error) {
- o.ctx = ctx
- log := ctx.GetLogger()
- log.Debugf("Unary operator %s is started", o.name)
- if len(o.outputs) <= 0 {
- infra.DrainError(ctx, fmt.Errorf("no output channel found"), errCh)
- return
- }
- // validate p
- if o.concurrency < 1 {
- o.concurrency = 1
- }
- //reset status
- o.statManagers = nil
- for i := 0; i < o.concurrency; i++ { // workers
- instance := i
- go func() {
- err := infra.SafeRun(func() error {
- o.doOp(ctx.WithInstance(instance), errCh)
- return nil
- })
- if err != nil {
- infra.DrainError(ctx, err, errCh)
- }
- }()
- }
- }
- func (o *UnaryOperator) doOp(ctx api.StreamContext, errCh chan<- error) {
- logger := ctx.GetLogger()
- if o.op == nil {
- logger.Infoln("Unary operator missing operation")
- return
- }
- exeCtx, cancel := ctx.WithCancel()
- defer func() {
- logger.Infof("unary operator %s instance %d done, cancelling future items", o.name, ctx.GetInstanceId())
- cancel()
- }()
- stats, err := metric.NewStatManager(ctx, "op")
- if err != nil {
- infra.DrainError(ctx, err, errCh)
- return
- }
- o.mutex.Lock()
- o.statManagers = append(o.statManagers, stats)
- o.mutex.Unlock()
- fv, afv := xsql.NewFunctionValuersForOp(exeCtx)
- for {
- select {
- // process incoming item
- case item := <-o.input:
- processed := false
- if item, processed = o.preprocess(item); processed {
- break
- }
- stats.IncTotalRecordsIn()
- stats.ProcessTimeStart()
- result := o.op.Apply(exeCtx, item, fv, afv)
- switch val := result.(type) {
- case nil:
- continue
- case error:
- logger.Errorf("Operation %s error: %s", ctx.GetOpId(), val)
- o.Broadcast(val)
- stats.IncTotalExceptions(val.Error())
- continue
- default:
- stats.ProcessTimeEnd()
- o.Broadcast(val)
- stats.IncTotalRecordsOut()
- stats.SetBufferLength(int64(len(o.input)))
- }
- // is cancelling
- case <-ctx.Done():
- logger.Infof("unary operator %s instance %d cancelling....", o.name, ctx.GetInstanceId())
- o.mutex.Lock()
- cancel()
- o.cancelled = true
- o.mutex.Unlock()
- return
- }
- }
- }
|