mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
This change split the inlet component into a simpler inlet and a new outlet component. The new inlet component receive flows and put them in Kafka, unparsed. The outlet component takes them from Kafka and resume the processing from here (flow parsing, enrichment) and puts them in ClickHouse. The main goal is to ensure the inlet does a minimal work to not be late when processing packets (and restart faster). It also brings some simplification as the number of knobs to tune everything is reduced: for inlet, we only need to tune the queue size for UDP, the number of workers and a few Kafka parameters; for outlet, we need to tune a few Kafka parameters, the number of workers and a few ClickHouse parameters. The outlet component features a simple Kafka input component. The core component becomes just a callback function. There is also a new ClickHouse component to push data to ClickHouse using the low-level ch-go library with batch inserts. This processing has an impact on the internal representation of a FlowMessage. Previously, it was tailored to dynamically build the protobuf message to be put in Kafka. Now, it builds the batch request to be sent to ClickHouse. This makes the FlowMessage structure hides the content of the next batch request and therefore, it should be reused. This also changes the way we decode flows as they don't output FlowMessage anymore, they reuse one that is provided to each worker. The ClickHouse tables are slightly updated. Instead of using Kafka engine, the Null engine is used instead. Fix #1122
107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
// SPDX-FileCopyrightText: 2022 Free Mobile
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
// Package core plumbs all the other components together.
|
|
package core
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gopkg.in/tomb.v2"
|
|
|
|
"akvorado/common/daemon"
|
|
"akvorado/common/helpers/cache"
|
|
"akvorado/common/httpserver"
|
|
"akvorado/common/reporter"
|
|
"akvorado/common/schema"
|
|
"akvorado/outlet/clickhouse"
|
|
"akvorado/outlet/flow"
|
|
"akvorado/outlet/kafka"
|
|
"akvorado/outlet/metadata"
|
|
"akvorado/outlet/routing"
|
|
)
|
|
|
|
// Component represents the HTTP compomenent.
|
|
type Component struct {
|
|
r *reporter.Reporter
|
|
d *Dependencies
|
|
t tomb.Tomb
|
|
config Configuration
|
|
|
|
metrics metrics
|
|
|
|
httpFlowClients uint32 // for dumping flows
|
|
httpFlowChannel chan []byte
|
|
httpFlowFlushDelay time.Duration
|
|
|
|
classifierExporterCache *cache.Cache[exporterInfo, exporterClassification]
|
|
classifierInterfaceCache *cache.Cache[exporterAndInterfaceInfo, interfaceClassification]
|
|
classifierErrLogger reporter.Logger
|
|
}
|
|
|
|
// Dependencies define the dependencies of the HTTP component.
|
|
type Dependencies struct {
|
|
Daemon daemon.Component
|
|
Flow *flow.Component
|
|
Metadata *metadata.Component
|
|
Routing *routing.Component
|
|
Kafka kafka.Component
|
|
ClickHouse clickhouse.Component
|
|
HTTP *httpserver.Component
|
|
Schema *schema.Component
|
|
}
|
|
|
|
// New creates a new core component.
|
|
func New(r *reporter.Reporter, configuration Configuration, dependencies Dependencies) (*Component, error) {
|
|
c := Component{
|
|
r: r,
|
|
d: &dependencies,
|
|
config: configuration,
|
|
|
|
httpFlowClients: 0,
|
|
httpFlowChannel: make(chan []byte, 10),
|
|
httpFlowFlushDelay: time.Second,
|
|
|
|
classifierExporterCache: cache.New[exporterInfo, exporterClassification](),
|
|
classifierInterfaceCache: cache.New[exporterAndInterfaceInfo, interfaceClassification](),
|
|
classifierErrLogger: r.Sample(reporter.BurstSampler(10*time.Second, 3)),
|
|
}
|
|
c.d.Daemon.Track(&c.t, "outlet/core")
|
|
c.initMetrics()
|
|
return &c, nil
|
|
}
|
|
|
|
// Start starts the core component.
|
|
func (c *Component) Start() error {
|
|
c.r.Info().Msg("starting core component")
|
|
c.d.Kafka.StartWorkers(c.newWorker)
|
|
|
|
// Classifier cache expiration
|
|
c.t.Go(func() error {
|
|
for {
|
|
select {
|
|
case <-c.t.Dying():
|
|
return nil
|
|
case <-time.After(c.config.ClassifierCacheDuration):
|
|
before := time.Now().Add(-c.config.ClassifierCacheDuration)
|
|
c.classifierExporterCache.DeleteLastAccessedBefore(before)
|
|
c.classifierInterfaceCache.DeleteLastAccessedBefore(before)
|
|
}
|
|
}
|
|
})
|
|
|
|
c.d.HTTP.GinRouter.GET("/api/v0/outlet/flows", c.FlowsHTTPHandler)
|
|
return nil
|
|
}
|
|
|
|
// Stop stops the core component.
|
|
func (c *Component) Stop() error {
|
|
defer func() {
|
|
close(c.httpFlowChannel)
|
|
c.r.Info().Msg("core component stopped")
|
|
}()
|
|
c.r.Info().Msg("stopping core component")
|
|
c.t.Kill(nil)
|
|
return c.t.Wait()
|
|
}
|