pipeline.go 6.7 KB
Newer Older
W
wangzelin.wzl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
// Copyright (c) 2021 OceanBase
// obagent is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//
// http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package engine

import (
	"context"
	"sync"
	"time"

	"github.com/pkg/errors"
	prom "github.com/prometheus/client_golang/prometheus"
	log "github.com/sirupsen/logrus"

	"github.com/oceanbase/obagent/config"
	"github.com/oceanbase/obagent/metric"
	"github.com/oceanbase/obagent/stat"
)

//PipelineInstance pipeline working instance
type PipelineInstance struct {
	pipeline *pipeline
	name     string
	config   *config.PipelineConfig
	clock    *clock
}

type clock struct {
	context context.Context
	cancel  context.CancelFunc
	ticker  *time.Ticker
}

//Init initialize the pipeline instance.
//Including the initialization operation of the input processor output exporter.
//If err is nil, indicates successful initialization.
func (p *PipelineInstance) Init() error {
	err := p.pipeline.init()
	if err != nil {
		return errors.Wrap(err, "pipeline init")
	}
	return nil
}

//Start pipeline instance start working, divided into push and pull modes.
//Push mode, start the clock to push.
//Pull mode, register route with route manager and add pipeline.
func (p *PipelineInstance) Start() {
	switch p.config.ScheduleStrategy {
	case config.Trigger:
		p.registerRoute()
		GetRouteManager().addPipelineFromPipelineGroup(p.config.ExposeUrl, p)
	case config.Periodic:
		p.startClock(p.config.Period)
	}
}

//Stop pipeline instance stop working.
//Push mode, stop the clock to push.
//Pull mode, delete pipeline to the config manager.
func (p *PipelineInstance) Stop() {
	switch p.config.ScheduleStrategy {
	case config.Trigger:
		GetRouteManager().delPipelineFromPipelineGroup(p.config.ExposeUrl, p)
	case config.Periodic:
		p.clock.stopClock()
	}
}

func (p *PipelineInstance) registerRoute() {
	GetRouteManager().registerHTTPRoute(p.config.ExposeUrl)
}

func (p *PipelineInstance) parallelCompute() []metric.Metric {
	var waitGroup sync.WaitGroup
	var metricTotal []metric.Metric
	var metricMutex sync.Mutex
	for _, inputInstance := range p.pipeline.InputInstances {
		waitGroup.Add(1)
		go collectAndProcess(&waitGroup, &metricTotal, &metricMutex, inputInstance, p.pipeline.ProcessorInstances)
	}
	waitGroup.Wait()
	return metricTotal
}

func (p *PipelineInstance) startClock(duration time.Duration) {
	ctx, cancel := context.WithCancel(context.Background())
	ticker := time.NewTicker(duration)
	c := &clock{
		context: ctx,
		cancel:  cancel,
		ticker:  ticker,
	}
	p.clock = c
	go func() {
		for {
			select {
			case <-c.ticker.C:
				p.pipelinePush()
			case <-c.context.Done():
				log.Info("pipeline clock is stop")
				return
			}
		}
	}()
}

func (c *clock) stopClock() {
	c.ticker.Stop()
	c.cancel()
}

//pipelinePush perform a pipeline push.
// ┌────────┐       ┌────────────┐       ┌────────────┐       ┌────────────┐
// │ Input1 │------>│ Processor1 │------>│ Processor2 │------>│ Processor3 │---┐
// └────────┘       └────────────┘       └────────────┘       └────────────┘   │
// ┌────────┐       ┌────────────┐       ┌────────────┐       ┌────────────┐   │    ┌────────┐
// │ Input2 │------>│ Processor1 │------>│ Processor2 │------>│ Processor3 │---┼--->│ Output │
// └────────┘       └────────────┘       └────────────┘       └────────────┘   │    └────────┘
// ┌────────┐       ┌────────────┐       ┌────────────┐       ┌────────────┐   │
// │ Input3 │------>│ Processor1 │------>│ Processor2 │------>│ Processor3 │---┘
// └────────┘       └────────────┘       └────────────┘       └────────────┘
func (p *PipelineInstance) pipelinePush() {
	tStart := time.Now()
	metrics := p.parallelCompute()
	if metrics == nil || len(metrics) == 0 {
		log.Warnf("push pipeline parallel compute result metrics is nil")
		return
	}
	err := p.pipeline.OutputInstance.Write(metrics)
	if err != nil {
		log.WithError(err).Error("pipeline output plugin write metric failed")
		return
	}
	elapsedTimeSeconds := time.Now().Sub(tStart).Seconds()
	stat.MonAgentPipelineExecuteTotal.With(prom.Labels{"name": p.name, "status": "Successful"}).Inc()
	stat.MonAgentPipelineExecuteSecondsTotal.With(prom.Labels{"name": p.name, "status": "Successful"}).Add(elapsedTimeSeconds)
}

func collectAndProcess(waitGroup *sync.WaitGroup, metricTotal *[]metric.Metric, metricMutex *sync.Mutex, inputInstance *InputInstance, processorInstance []*ProcessorInstance) {
	defer waitGroup.Done()

	metrics, err := inputInstance.Collect()
	if err != nil {
		log.WithError(err).Error("input plugin collect failed")
		return
	}
	for _, processorInstance := range processorInstance {
		metrics, err = processorInstance.Process(metrics...)
		if err != nil {
			log.WithError(err).Error("process plugin process failed")
			continue
		}
	}

	metricMutex.Lock()
	*metricTotal = append(*metricTotal, metrics...)
	metricMutex.Unlock()
}

type pipeline struct {
	InputInstances     []*InputInstance
	ProcessorInstances []*ProcessorInstance
	OutputInstance     *OutputInstance
	ExporterInstance   *ExporterInstance
}

func (p *pipeline) init() error {

	for _, inputInstance := range p.InputInstances {
		err := inputInstance.init(inputInstance.Config)
		if err != nil {
			return errors.Wrap(err, "input init")
		}
	}

	for _, processorInstance := range p.ProcessorInstances {
		err := processorInstance.init(processorInstance.Config)
		if err != nil {
			return errors.Wrap(err, "processor init")
		}
	}

	if p.OutputInstance != nil {
		err := p.OutputInstance.init(p.OutputInstance.Config)
		if err != nil {
			return errors.Wrap(err, "output init")
		}
	}

	if p.ExporterInstance != nil {
		err := p.ExporterInstance.init(p.ExporterInstance.Config)
		if err != nil {
			return errors.Wrap(err, "exporter init")
		}
	}

	return nil
}