implement context cancellation on db error
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
m "git.k-space.ee/k-space/logmower-shipper/pkg/mongo"
|
||||
"github.com/jtagcat/util"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -18,7 +19,7 @@ const (
|
||||
|
||||
type Queue <-chan m.Record
|
||||
|
||||
func (queue Queue) Sender(db *mongo.Collection, metricsFilename string) {
|
||||
func (queue Queue) Sender(db *mongo.Collection, metricsFilename string, cancelOnError func()) {
|
||||
batched := make(chan []m.Record)
|
||||
|
||||
// metrics for batcher and queue
|
||||
@@ -60,14 +61,31 @@ func (queue Queue) Sender(db *mongo.Collection, metricsFilename string) {
|
||||
batchBson = append(batchBson, b.ToBson())
|
||||
}
|
||||
|
||||
result, err := db.InsertMany(globals.MongoTimeout(context.Background()), batchBson, nil)
|
||||
tru := true
|
||||
result, err := db.InsertMany(globals.MongoTimeout(context.Background()), batchBson, &options.InsertManyOptions{Ordered: &tru})
|
||||
|
||||
var succeedCount int
|
||||
if result != nil {
|
||||
succeedCount = len(result.InsertedIDs)
|
||||
}
|
||||
promShipperDbSent.WithLabelValues(metricsFilename).Add(float64(succeedCount))
|
||||
|
||||
if err != nil {
|
||||
promShipperDbSendError.WithLabelValues(metricsFilename).Add(1)
|
||||
log.Printf("failure in batch submit to database: %e", err) // TODO: add some selective retry here or something, better error handling
|
||||
continue
|
||||
}
|
||||
|
||||
promShipperDbSent.WithLabelValues(metricsFilename).Add(float64(
|
||||
len(result.InsertedIDs)))
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user