2021-01-31 23:10:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
2022-05-02 07:06:34 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/barpool"
|
2021-01-31 23:10:16 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/influx"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/vm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type influxProcessor struct {
|
2022-05-06 15:06:54 +00:00
|
|
|
ic *influx.Client
|
|
|
|
im *vm.Importer
|
|
|
|
cc int
|
|
|
|
separator string
|
|
|
|
skipDbLabel bool
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 15:06:54 +00:00
|
|
|
func newInfluxProcessor(ic *influx.Client, im *vm.Importer, cc int, separator string, skipDbLabel bool) *influxProcessor {
|
2021-01-31 23:10:16 +00:00
|
|
|
if cc < 1 {
|
|
|
|
cc = 1
|
|
|
|
}
|
|
|
|
return &influxProcessor{
|
2022-05-06 15:06:54 +00:00
|
|
|
ic: ic,
|
|
|
|
im: im,
|
|
|
|
cc: cc,
|
|
|
|
separator: separator,
|
|
|
|
skipDbLabel: skipDbLabel,
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:12:01 +00:00
|
|
|
func (ip *influxProcessor) run(silent, verbose bool) error {
|
2021-01-31 23:10:16 +00:00
|
|
|
series, err := ip.ic.Explore()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("explore query failed: %s", err)
|
|
|
|
}
|
|
|
|
if len(series) < 1 {
|
|
|
|
return fmt.Errorf("found no timeseries to import")
|
|
|
|
}
|
|
|
|
|
|
|
|
question := fmt.Sprintf("Found %d timeseries to import. Continue?", len(series))
|
|
|
|
if !silent && !prompt(question) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:06:34 +00:00
|
|
|
bar := barpool.AddWithTemplate(fmt.Sprintf(barTpl, "Processing series"), len(series))
|
|
|
|
if err := barpool.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-31 23:10:16 +00:00
|
|
|
seriesCh := make(chan *influx.Series)
|
|
|
|
errCh := make(chan error)
|
|
|
|
ip.im.ResetStats()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(ip.cc)
|
|
|
|
for i := 0; i < ip.cc; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for s := range seriesCh {
|
|
|
|
if err := ip.do(s); err != nil {
|
|
|
|
errCh <- fmt.Errorf("request failed for %q.%q: %s", s.Measurement, s.Field, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bar.Increment()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// any error breaks the import
|
|
|
|
for _, s := range series {
|
|
|
|
select {
|
|
|
|
case infErr := <-errCh:
|
|
|
|
return fmt.Errorf("influx error: %s", infErr)
|
|
|
|
case vmErr := <-ip.im.Errors():
|
2022-01-03 19:12:01 +00:00
|
|
|
return fmt.Errorf("import process failed: %s", wrapErr(vmErr, verbose))
|
2021-01-31 23:10:16 +00:00
|
|
|
case seriesCh <- s:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(seriesCh)
|
|
|
|
wg.Wait()
|
|
|
|
ip.im.Close()
|
2022-05-03 05:03:41 +00:00
|
|
|
close(errCh)
|
2021-01-31 23:10:16 +00:00
|
|
|
// drain import errors channel
|
|
|
|
for vmErr := range ip.im.Errors() {
|
2022-01-03 19:12:01 +00:00
|
|
|
if vmErr.Err != nil {
|
|
|
|
return fmt.Errorf("import process failed: %s", wrapErr(vmErr, verbose))
|
|
|
|
}
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
2022-05-03 05:03:41 +00:00
|
|
|
for err := range errCh {
|
|
|
|
return fmt.Errorf("import process failed: %s", err)
|
|
|
|
}
|
2022-05-02 07:06:34 +00:00
|
|
|
barpool.Stop()
|
2021-01-31 23:10:16 +00:00
|
|
|
log.Println("Import finished!")
|
|
|
|
log.Print(ip.im.Stats())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbLabel = "db"
|
|
|
|
|
|
|
|
func (ip *influxProcessor) do(s *influx.Series) error {
|
|
|
|
cr, err := ip.ic.FetchDataPoints(s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch datapoints: %s", err)
|
|
|
|
}
|
2021-01-31 23:31:25 +00:00
|
|
|
defer func() {
|
|
|
|
_ = cr.Close()
|
|
|
|
}()
|
2021-01-31 23:10:16 +00:00
|
|
|
var name string
|
|
|
|
if s.Measurement != "" {
|
|
|
|
name = fmt.Sprintf("%s%s%s", s.Measurement, ip.separator, s.Field)
|
|
|
|
} else {
|
|
|
|
name = s.Field
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := make([]vm.LabelPair, len(s.LabelPairs))
|
|
|
|
var containsDBLabel bool
|
|
|
|
for i, lp := range s.LabelPairs {
|
|
|
|
if lp.Name == dbLabel {
|
|
|
|
containsDBLabel = true
|
|
|
|
}
|
|
|
|
labels[i] = vm.LabelPair{
|
|
|
|
Name: lp.Name,
|
|
|
|
Value: lp.Value,
|
|
|
|
}
|
|
|
|
}
|
2022-05-06 15:06:54 +00:00
|
|
|
if !containsDBLabel && !ip.skipDbLabel {
|
2021-01-31 23:10:16 +00:00
|
|
|
labels = append(labels, vm.LabelPair{
|
|
|
|
Name: dbLabel,
|
|
|
|
Value: ip.ic.Database(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
time, values, err := cr.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// skip empty results
|
|
|
|
if len(time) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-03 05:03:41 +00:00
|
|
|
ts := vm.TimeSeries{
|
2021-01-31 23:10:16 +00:00
|
|
|
Name: name,
|
|
|
|
LabelPairs: labels,
|
|
|
|
Timestamps: time,
|
|
|
|
Values: values,
|
|
|
|
}
|
2022-05-03 05:03:41 +00:00
|
|
|
if err := ip.im.Input(&ts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
}
|