VictoriaMetrics/app/vminsert/graphite/request_handler.go

170 lines
4.3 KiB
Go
Raw Normal View History

2019-05-22 21:16:55 +00:00
package graphite
import (
"fmt"
"io"
"net"
"runtime"
"sync"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/concurrencylimiter"
2019-05-22 21:23:23 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/netstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics"
2019-05-22 21:16:55 +00:00
"github.com/VictoriaMetrics/metrics"
"github.com/valyala/fastjson/fastfloat"
2019-05-22 21:16:55 +00:00
)
var rowsInserted = tenantmetrics.NewCounterMap(`vm_rows_inserted_total{type="graphite"}`)
2019-05-22 21:16:55 +00:00
// insertHandler processes remote write for graphite plaintext protocol.
//
// See https://graphite.readthedocs.io/en/latest/feeding-carbon.html#the-plaintext-protocol
2019-05-22 21:23:23 +00:00
func insertHandler(at *auth.Token, r io.Reader) error {
2019-05-22 21:16:55 +00:00
return concurrencylimiter.Do(func() error {
2019-05-22 21:23:23 +00:00
return insertHandlerInternal(at, r)
2019-05-22 21:16:55 +00:00
})
}
2019-05-22 21:23:23 +00:00
func insertHandlerInternal(at *auth.Token, r io.Reader) error {
2019-05-22 21:16:55 +00:00
ctx := getPushCtx()
defer putPushCtx(ctx)
for ctx.Read(r) {
2019-05-22 21:23:23 +00:00
if err := ctx.InsertRows(at); err != nil {
2019-05-22 21:16:55 +00:00
return err
}
}
return ctx.Error()
}
2019-05-22 21:23:23 +00:00
func (ctx *pushCtx) InsertRows(at *auth.Token) error {
2019-05-22 21:16:55 +00:00
rows := ctx.Rows.Rows
ic := &ctx.Common
2019-05-22 21:23:23 +00:00
ic.Reset()
atCopy := *at
2019-05-22 21:16:55 +00:00
for i := range rows {
r := &rows[i]
ic.Labels = ic.Labels[:0]
ic.AddLabel("", r.Metric)
for j := range r.Tags {
tag := &r.Tags[j]
if atCopy.AccountID == 0 {
// Multi-tenancy support via custom tags.
// Do not allow overriding AccountID and ProjectID from atCopy for security reasons.
if tag.Key == "VictoriaMetrics_AccountID" {
atCopy.AccountID = uint32(fastfloat.ParseUint64BestEffort(tag.Value))
}
if atCopy.ProjectID == 0 && tag.Key == "VictoriaMetrics_ProjectID" {
atCopy.ProjectID = uint32(fastfloat.ParseUint64BestEffort(tag.Value))
}
}
2019-05-22 21:16:55 +00:00
ic.AddLabel(tag.Key, tag.Value)
}
if err := ic.WriteDataPoint(&atCopy, ic.Labels, r.Timestamp, r.Value); err != nil {
2019-05-22 21:23:23 +00:00
return err
}
2019-05-22 21:16:55 +00:00
}
// Assume that all the rows for a single connection belong to the same (AccountID, ProjectID).
rowsInserted.Get(&atCopy).Add(len(rows))
2019-05-22 21:16:55 +00:00
return ic.FlushBufs()
}
const flushTimeout = 3 * time.Second
func (ctx *pushCtx) Read(r io.Reader) bool {
graphiteReadCalls.Inc()
if ctx.err != nil {
return false
}
if c, ok := r.(net.Conn); ok {
if err := c.SetReadDeadline(time.Now().Add(flushTimeout)); err != nil {
graphiteReadErrors.Inc()
ctx.err = fmt.Errorf("cannot set read deadline: %s", err)
return false
}
}
ctx.reqBuf, ctx.tailBuf, ctx.err = common.ReadLinesBlock(r, ctx.reqBuf, ctx.tailBuf)
if ctx.err != nil {
if ne, ok := ctx.err.(net.Error); ok && ne.Timeout() {
2019-05-22 21:16:55 +00:00
// Flush the read data on timeout and try reading again.
ctx.err = nil
2019-05-22 21:16:55 +00:00
} else {
if ctx.err != io.EOF {
graphiteReadErrors.Inc()
ctx.err = fmt.Errorf("cannot read graphite plaintext protocol data: %s", ctx.err)
}
2019-05-22 21:16:55 +00:00
return false
}
}
if err := ctx.Rows.Unmarshal(bytesutil.ToUnsafeString(ctx.reqBuf)); err != nil {
2019-05-22 21:16:55 +00:00
graphiteUnmarshalErrors.Inc()
ctx.err = fmt.Errorf("cannot unmarshal graphite plaintext protocol data with size %d: %s", len(ctx.reqBuf), err)
2019-05-22 21:16:55 +00:00
return false
}
// Convert timestamps from seconds to milliseconds
for i := range ctx.Rows.Rows {
ctx.Rows.Rows[i].Timestamp *= 1e3
}
return true
}
type pushCtx struct {
Rows Rows
2019-05-22 21:23:23 +00:00
Common netstorage.InsertCtx
2019-05-22 21:16:55 +00:00
reqBuf []byte
2019-05-22 21:16:55 +00:00
tailBuf []byte
err error
}
func (ctx *pushCtx) Error() error {
if ctx.err == io.EOF {
return nil
}
return ctx.err
}
func (ctx *pushCtx) reset() {
ctx.Rows.Reset()
2019-05-22 21:23:23 +00:00
ctx.Common.Reset()
ctx.reqBuf = ctx.reqBuf[:0]
2019-05-22 21:16:55 +00:00
ctx.tailBuf = ctx.tailBuf[:0]
ctx.err = nil
}
var (
graphiteReadCalls = metrics.NewCounter(`vm_read_calls_total{name="graphite"}`)
graphiteReadErrors = metrics.NewCounter(`vm_read_errors_total{name="graphite"}`)
graphiteUnmarshalErrors = metrics.NewCounter(`vm_unmarshal_errors_total{name="graphite"}`)
)
func getPushCtx() *pushCtx {
select {
case ctx := <-pushCtxPoolCh:
return ctx
default:
if v := pushCtxPool.Get(); v != nil {
return v.(*pushCtx)
}
return &pushCtx{}
}
}
func putPushCtx(ctx *pushCtx) {
ctx.reset()
select {
case pushCtxPoolCh <- ctx:
default:
pushCtxPool.Put(ctx)
}
}
var pushCtxPool sync.Pool
var pushCtxPoolCh = make(chan *pushCtx, runtime.GOMAXPROCS(-1))