2020-02-23 11:35:47 +00:00
|
|
|
package opentsdbhttp
|
|
|
|
|
|
|
|
import (
|
2020-09-27 23:06:27 +00:00
|
|
|
"bufio"
|
2020-02-23 11:35:47 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-05-14 19:01:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2020-08-16 14:05:52 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2020-04-10 09:43:51 +00:00
|
|
|
var (
|
2020-08-16 14:05:52 +00:00
|
|
|
maxInsertRequestSize = flagutil.NewBytes("opentsdbhttp.maxInsertRequestSize", 32*1024*1024, "The maximum size of OpenTSDB HTTP put request")
|
2020-04-10 09:43:51 +00:00
|
|
|
trimTimestamp = flag.Duration("opentsdbhttpTrimTimestamp", time.Millisecond, "Trim timestamps for OpenTSDB HTTP data to this duration. "+
|
|
|
|
"Minimum practical duration is 1ms. Higher duration (i.e. 1s) may be used for reducing disk space usage for timestamp data")
|
|
|
|
)
|
2020-02-23 11:35:47 +00:00
|
|
|
|
|
|
|
// ParseStream parses OpenTSDB http lines from req and calls callback for the parsed rows.
|
|
|
|
//
|
2020-11-01 21:12:13 +00:00
|
|
|
// The callback can be called concurrently multiple times for streamed data from req.
|
2020-02-23 11:35:47 +00:00
|
|
|
//
|
|
|
|
// callback shouldn't hold rows after returning.
|
|
|
|
func ParseStream(req *http.Request, callback func(rows []Row) error) error {
|
|
|
|
readCalls.Inc()
|
|
|
|
r := req.Body
|
|
|
|
if req.Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
zr, err := common.GetGzipReader(r)
|
|
|
|
if err != nil {
|
|
|
|
readErrors.Inc()
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot read gzipped http protocol data: %w", err)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
defer common.PutGzipReader(zr)
|
|
|
|
r = zr
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:06:27 +00:00
|
|
|
ctx := getStreamContext(r)
|
2020-02-23 11:35:47 +00:00
|
|
|
defer putStreamContext(ctx)
|
|
|
|
|
|
|
|
// Read the request in ctx.reqBuf
|
2020-09-27 23:06:27 +00:00
|
|
|
lr := io.LimitReader(ctx.br, int64(maxInsertRequestSize.N)+1)
|
2020-02-23 11:35:47 +00:00
|
|
|
reqLen, err := ctx.reqBuf.ReadFrom(lr)
|
|
|
|
if err != nil {
|
|
|
|
readErrors.Inc()
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot read HTTP OpenTSDB request: %w", err)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2020-08-16 14:05:52 +00:00
|
|
|
if reqLen > int64(maxInsertRequestSize.N) {
|
2020-02-23 11:35:47 +00:00
|
|
|
readErrors.Inc()
|
2020-08-16 14:05:52 +00:00
|
|
|
return fmt.Errorf("too big HTTP OpenTSDB request; mustn't exceed `-opentsdbhttp.maxInsertRequestSize=%d` bytes", maxInsertRequestSize.N)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:03:54 +00:00
|
|
|
// Process the request synchronously, since there is no sense in processing a single request asynchronously.
|
|
|
|
// Sync code is easier to read and understand.
|
|
|
|
p := getJSONParser()
|
|
|
|
defer putJSONParser(p)
|
|
|
|
v, err := p.ParseBytes(ctx.reqBuf.B)
|
|
|
|
if err != nil {
|
2020-11-13 11:22:00 +00:00
|
|
|
unmarshalErrors.Inc()
|
2020-11-13 11:03:54 +00:00
|
|
|
return fmt.Errorf("cannot parse HTTP OpenTSDB json: %w", err)
|
|
|
|
}
|
|
|
|
rs := getRows()
|
|
|
|
defer putRows(rs)
|
|
|
|
rs.Unmarshal(v)
|
|
|
|
rows := rs.Rows
|
|
|
|
rowsRead.Add(len(rows))
|
|
|
|
|
|
|
|
// Fill in missing timestamps
|
|
|
|
currentTimestamp := int64(fasttime.UnixTimestamp())
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
|
|
|
if r.Timestamp == 0 {
|
|
|
|
r.Timestamp = currentTimestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert timestamps in seconds to milliseconds if needed.
|
|
|
|
// See http://opentsdb.net/docs/javadoc/net/opentsdb/core/Const.html#SECOND_MASK
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
|
|
|
if r.Timestamp&secondMask == 0 {
|
|
|
|
r.Timestamp *= 1e3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim timestamps if required.
|
|
|
|
if tsTrim := trimTimestamp.Milliseconds(); tsTrim > 1 {
|
|
|
|
for i := range rows {
|
|
|
|
row := &rows[i]
|
|
|
|
row.Timestamp -= row.Timestamp % tsTrim
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := callback(rows); err != nil {
|
|
|
|
return fmt.Errorf("error when processing imported data: %w", err)
|
|
|
|
}
|
2020-09-28 01:11:55 +00:00
|
|
|
return nil
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const secondMask int64 = 0x7FFFFFFF00000000
|
|
|
|
|
|
|
|
type streamContext struct {
|
2020-09-27 23:06:27 +00:00
|
|
|
br *bufio.Reader
|
2020-02-23 11:35:47 +00:00
|
|
|
reqBuf bytesutil.ByteBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *streamContext) reset() {
|
2020-09-27 23:06:27 +00:00
|
|
|
ctx.br.Reset(nil)
|
2020-02-23 11:35:47 +00:00
|
|
|
ctx.reqBuf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-02-28 18:19:35 +00:00
|
|
|
readCalls = metrics.NewCounter(`vm_protoparser_read_calls_total{type="opentsdbhttp"}`)
|
|
|
|
readErrors = metrics.NewCounter(`vm_protoparser_read_errors_total{type="opentsdbhttp"}`)
|
|
|
|
rowsRead = metrics.NewCounter(`vm_protoparser_rows_read_total{type="opentsdbhttp"}`)
|
|
|
|
unmarshalErrors = metrics.NewCounter(`vm_protoparser_unmarshal_errors_total{type="opentsdbhttp"}`)
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
2020-09-27 23:06:27 +00:00
|
|
|
func getStreamContext(r io.Reader) *streamContext {
|
2020-02-23 11:35:47 +00:00
|
|
|
select {
|
|
|
|
case ctx := <-streamContextPoolCh:
|
2020-09-27 23:06:27 +00:00
|
|
|
ctx.br.Reset(r)
|
2020-02-23 11:35:47 +00:00
|
|
|
return ctx
|
|
|
|
default:
|
|
|
|
if v := streamContextPool.Get(); v != nil {
|
2020-09-27 23:06:27 +00:00
|
|
|
ctx := v.(*streamContext)
|
|
|
|
ctx.br.Reset(r)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
return &streamContext{
|
|
|
|
br: bufio.NewReaderSize(r, 64*1024),
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putStreamContext(ctx *streamContext) {
|
|
|
|
ctx.reset()
|
|
|
|
select {
|
|
|
|
case streamContextPoolCh <- ctx:
|
|
|
|
default:
|
|
|
|
streamContextPool.Put(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var streamContextPool sync.Pool
|
|
|
|
var streamContextPoolCh = make(chan *streamContext, runtime.GOMAXPROCS(-1))
|
2020-09-28 01:11:55 +00:00
|
|
|
|
2020-11-13 11:03:54 +00:00
|
|
|
func getRows() *Rows {
|
|
|
|
v := rowsPool.Get()
|
2020-09-28 01:11:55 +00:00
|
|
|
if v == nil {
|
2020-11-13 11:03:54 +00:00
|
|
|
return &Rows{}
|
2020-09-28 01:11:55 +00:00
|
|
|
}
|
2020-11-13 11:03:54 +00:00
|
|
|
return v.(*Rows)
|
2020-09-28 01:11:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:03:54 +00:00
|
|
|
func putRows(rs *Rows) {
|
|
|
|
rs.Reset()
|
|
|
|
rowsPool.Put(rs)
|
2020-09-28 01:11:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:03:54 +00:00
|
|
|
var rowsPool sync.Pool
|