2022-10-11 07:55:22 +00:00
|
|
|
package logmower
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-11-09 12:19:56 +00:00
|
|
|
"log"
|
2022-10-11 07:55:22 +00:00
|
|
|
"os"
|
2022-11-05 23:45:19 +00:00
|
|
|
"time"
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-09 14:00:44 +00:00
|
|
|
ms "git.k-space.ee/k-space/logmower-shipper/pkg/mongo_struct"
|
2022-10-11 07:55:22 +00:00
|
|
|
"github.com/jtagcat/util"
|
|
|
|
prom "github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
mongoOpt "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-06 16:41:10 +00:00
|
|
|
promFileInitialSeekSkipped = promauto.NewGaugeVec(prom.GaugeOpts{
|
|
|
|
Namespace: PrometheusPrefix,
|
|
|
|
// Subsystem: "file",
|
|
|
|
Name: "skipped_bytes",
|
|
|
|
Help: "Bytes skipped in file after discovering",
|
|
|
|
}, []string{"filename"})
|
|
|
|
promFileCatchupDone = promauto.NewGaugeVec(prom.GaugeOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-10-11 07:55:22 +00:00
|
|
|
Subsystem: "file",
|
|
|
|
Name: "catchupped",
|
2022-11-06 16:41:10 +00:00
|
|
|
Help: "(0 or) 1 if initial backlog has been sent; (total <= watcher_file_count)",
|
2022-10-11 07:55:22 +00:00
|
|
|
}, []string{"filename"}) // TODO: rm filename?
|
|
|
|
promFileErr = promauto.NewCounterVec(prom.CounterOpts{
|
2022-11-06 15:02:49 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
2022-10-11 07:55:22 +00:00
|
|
|
Subsystem: "file",
|
|
|
|
Name: "errors_count",
|
2022-11-06 16:41:10 +00:00
|
|
|
Help: "Errors while reading file",
|
|
|
|
}, []string{"filename"})
|
|
|
|
promFileLineSize = promauto.NewHistogramVec(prom.HistogramOpts{
|
|
|
|
Namespace: PrometheusPrefix,
|
|
|
|
// Subsystem: "file",
|
|
|
|
Name: "line_size_bytes",
|
|
|
|
Help: "Log line size in bytes",
|
|
|
|
Buckets: []float64{80, 160, 320, 640, 1280},
|
|
|
|
}, []string{"filename"})
|
2022-10-11 07:55:22 +00:00
|
|
|
)
|
|
|
|
|
2022-11-05 23:45:19 +00:00
|
|
|
const SendQueueLimit = 1024
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
type file struct {
|
|
|
|
ms.File
|
|
|
|
metricsName string // filepath.Base()
|
|
|
|
}
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
// TODO: caller could call duplicate shipFile of same name on file replace: sends might not work properly
|
|
|
|
func (f file) Process(ctx context.Context, db *mongo.Collection, recordLimitBytes int) {
|
|
|
|
lineChan := make(chan RawLine)
|
2022-11-06 20:02:29 +00:00
|
|
|
defer close(lineChan)
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
dbQueue := make(chan ms.Record, SendQueueLimit)
|
|
|
|
go RawLines(lineChan).Process(recordLimitBytes, dbQueue)
|
2022-11-05 23:45:19 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
waitGo := util.GoWg(func() {
|
|
|
|
queueT(dbQueue).sender(db, f.metricsName)
|
|
|
|
})
|
|
|
|
defer waitGo()
|
2022-11-05 23:45:19 +00:00
|
|
|
|
2022-11-06 16:41:10 +00:00
|
|
|
// TODO: better way to kill or wait for sendQueue before retrying (or duplicates?)
|
2022-11-06 23:23:20 +00:00
|
|
|
_ = wait.ManagedExponentialBackoffWithContext(ctx, defaultBackoff(), func() (done bool, _ error) {
|
2022-11-09 12:19:56 +00:00
|
|
|
err := f.trySubmit(ctx, db, lineChan)
|
2022-11-05 23:45:19 +00:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
2022-10-11 07:55:22 +00:00
|
|
|
}
|
2022-11-05 23:45:19 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
promFileErr.WithLabelValues(f.metricsName).Add(1)
|
|
|
|
log.Printf("processing file %q: %e", f.metricsName, err)
|
|
|
|
|
|
|
|
// nil: loop and keep retrying indefinitely
|
|
|
|
return false, nil
|
2022-10-11 07:55:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
// use submitter(), don't use directly
|
|
|
|
func (f file) trySubmit(ctx context.Context, db *mongo.Collection, sendQueue chan<- RawLine) error {
|
2022-11-06 01:43:18 +00:00
|
|
|
// TODO: better way for respecting ?killing sender for retry
|
2022-11-05 23:45:19 +00:00
|
|
|
for {
|
|
|
|
if len(sendQueue) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2022-10-11 07:55:22 +00:00
|
|
|
// get files with offset
|
2022-11-09 12:19:56 +00:00
|
|
|
offsetResult, _ := mongoWithErr(db.FindOne(mongoTimeoutCtx(ctx),
|
|
|
|
bson.D{{Key: ms.RecordKeyHostId, Value: f.Host.Id}, {Key: ms.RecordKeyFilePath, Value: f.Path}},
|
|
|
|
&mongoOpt.FindOneOptions{Sort: bson.D{{Key: ms.RecordKeyOffset, Value: -1}}}, // sort descending (get largest)
|
2022-10-11 07:55:22 +00:00
|
|
|
))
|
2022-11-06 01:43:18 +00:00
|
|
|
|
2022-11-06 21:15:23 +00:00
|
|
|
offsetResultBytes, err := offsetResult.DecodeBytes()
|
2022-10-11 07:55:22 +00:00
|
|
|
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
|
2022-11-06 16:41:10 +00:00
|
|
|
return fmt.Errorf("retrieving offset from database: %w", err)
|
2022-10-11 07:55:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
dbOffset := ms.RecordOffsetFromBson(&offsetResultBytes)
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
fi, err := os.Stat(f.Path)
|
2022-10-11 07:55:22 +00:00
|
|
|
if err != nil {
|
2022-11-06 02:04:32 +00:00
|
|
|
return fmt.Errorf("getting original file size: %w", err)
|
2022-10-11 07:55:22 +00:00
|
|
|
}
|
|
|
|
startSize := fi.Size()
|
|
|
|
|
2022-11-06 12:41:09 +00:00
|
|
|
sctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
promFileInitialSeekSkipped.WithLabelValues(f.metricsName).Set(float64(dbOffset))
|
2022-11-06 16:41:10 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
lineChan, errChan, err := util.TailFile(sctx, f.Path, dbOffset, io.SeekStart)
|
2022-10-11 07:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tailing file: %w", err)
|
|
|
|
}
|
2022-11-05 23:45:19 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
var catchUpped bool
|
|
|
|
promFileCatchupDone.WithLabelValues(f.metricsName).Set(0)
|
2022-11-06 14:11:23 +00:00
|
|
|
|
2022-10-11 07:55:22 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
return fmt.Errorf("tailing file: %w", err)
|
2022-11-05 23:45:19 +00:00
|
|
|
|
2022-11-06 13:46:07 +00:00
|
|
|
case line, ok := <-lineChan:
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
promFileLineSize.WithLabelValues(f.metricsName).Observe(float64(len(line.Bytes)))
|
2022-11-06 16:41:10 +00:00
|
|
|
|
2022-11-06 14:09:28 +00:00
|
|
|
if !catchUpped {
|
2022-11-06 14:35:49 +00:00
|
|
|
catchUpped = line.EndOffset >= startSize
|
2022-11-06 14:09:28 +00:00
|
|
|
|
|
|
|
if catchUpped {
|
2022-11-09 12:19:56 +00:00
|
|
|
promFileCatchupDone.WithLabelValues(f.metricsName).Set(1)
|
2022-10-11 07:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:02:29 +00:00
|
|
|
if len(line.Bytes) == 0 {
|
2022-11-06 13:57:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
sendQueue <- RawLine{
|
|
|
|
file: &f,
|
2022-11-06 01:43:18 +00:00
|
|
|
|
2022-11-09 12:19:56 +00:00
|
|
|
Offset: line.EndOffset,
|
|
|
|
B: line.Bytes,
|
2022-10-11 07:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 23:45:19 +00:00
|
|
|
func mongoWithErr[t interface{ Err() error }](mongoWrap t) (t, error) {
|
2022-10-11 07:55:22 +00:00
|
|
|
return mongoWrap, mongoWrap.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// func JitterUntilCancelWithContext(pctx context.Context, f func(context.Context, context.CancelFunc), period time.Duration, jitterFactor float64, sliding bool) {
|
|
|
|
// ctx, cancel := context.WithCancel(pctx)
|
|
|
|
// wait.JitterUntil(func() { f(ctx, cancel) }, period, jitterFactor, sliding, ctx.Done())
|
|
|
|
// }
|