2022-11-09 16:07:28 +00:00
|
|
|
package sender
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-09 18:24:57 +00:00
|
|
|
"fmt"
|
2022-11-09 16:07:28 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.k-space.ee/k-space/logmower-shipper/pkg/globals"
|
|
|
|
m "git.k-space.ee/k-space/logmower-shipper/pkg/mongo"
|
|
|
|
"github.com/jtagcat/util"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2022-11-09 17:56:44 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2022-11-09 16:07:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MaxBatchItems = 10000
|
|
|
|
MaxBatchTime = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type Queue <-chan m.Record
|
|
|
|
|
2022-11-09 17:56:44 +00:00
|
|
|
func (queue Queue) Sender(db *mongo.Collection, metricsFilename string, cancelOnError func()) {
|
2022-11-09 16:07:28 +00:00
|
|
|
batched := make(chan []m.Record)
|
|
|
|
|
|
|
|
// metrics for batcher and queue
|
|
|
|
go func() {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
promShipperQueued.WithLabelValues(metricsFilename).Set(float64(
|
|
|
|
len(queue)))
|
|
|
|
|
|
|
|
timer := time.NewTimer(time.Second)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
util.Batch(MaxBatchItems, MaxBatchTime, queue, batched)
|
|
|
|
// returns when sendQueue is closed
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
promShipperSynced.WithLabelValues(metricsFilename).Set(1)
|
|
|
|
|
|
|
|
batch, ok := <-batched
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
promShipperBatchSizeResult.Observe(float64(len(batch)))
|
|
|
|
|
|
|
|
promShipperSynced.WithLabelValues(metricsFilename).Set(0)
|
|
|
|
|
2022-11-09 18:24:57 +00:00
|
|
|
result, err := insertManyWithSimulate(db, batch)
|
2022-11-09 17:56:44 +00:00
|
|
|
|
|
|
|
var succeedCount int
|
|
|
|
if result != nil {
|
|
|
|
succeedCount = len(result.InsertedIDs)
|
|
|
|
}
|
|
|
|
promShipperDbSent.WithLabelValues(metricsFilename).Add(float64(succeedCount))
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
promShipperDbSendError.WithLabelValues(metricsFilename).Add(1)
|
|
|
|
|
2022-11-09 17:56:44 +00:00
|
|
|
if succeedCount == len(batch) {
|
|
|
|
log.Printf("all insertions in batch were successful, yet failure in database: %e", err)
|
|
|
|
|
|
|
|
cancelOnError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
firstFailed := &batch[succeedCount] // (len-1)+1
|
|
|
|
log.Printf("failure in inserting %q record with offset %d to database: %e",
|
|
|
|
firstFailed.Path, firstFailed.Offset, err)
|
|
|
|
|
|
|
|
cancelOnError()
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 16:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 18:24:57 +00:00
|
|
|
|
|
|
|
func insertManyWithSimulate(db *mongo.Collection, batch []m.Record) (*mongo.InsertManyResult, error) {
|
|
|
|
if !globals.Simulate {
|
|
|
|
var batchBson []interface{} // mongo does not like typing
|
|
|
|
for _, b := range batch {
|
|
|
|
batchBson = append(batchBson, b.ToBson())
|
|
|
|
}
|
|
|
|
|
|
|
|
tru := true
|
|
|
|
return db.InsertMany(globals.MongoTimeout(context.Background()), batchBson, &options.InsertManyOptions{Ordered: &tru})
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("simulating successful database bulk write: %v", batch)
|
|
|
|
|
|
|
|
var res mongo.InsertManyResult
|
|
|
|
for range batch {
|
|
|
|
res.InsertedIDs = append(res.InsertedIDs, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|