2023-02-13 18:37:53 +00:00
|
|
|
package stream
|
2021-05-08 14:55:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/consts"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/handshake"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
2023-01-07 02:59:39 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
2021-05-08 14:55:44 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2023-02-13 18:37:53 +00:00
|
|
|
// Parse parses data sent from vminsert to bc and calls callback for parsed rows.
|
2021-10-08 10:52:56 +00:00
|
|
|
// Optional function isReadOnly must return true if the storage cannot accept new data.
|
2023-01-07 02:16:25 +00:00
|
|
|
// In this case the data read from bc isn't accepted and the readonly status is sent back bc.
|
2021-05-08 14:55:44 +00:00
|
|
|
//
|
|
|
|
// The callback can be called concurrently multiple times for streamed data from req.
|
|
|
|
//
|
|
|
|
// callback shouldn't hold block after returning.
|
2023-02-13 18:37:53 +00:00
|
|
|
func Parse(bc *handshake.BufferedConn, callback func(rows []storage.MetricRow) error, isReadOnly func() bool) error {
|
2023-01-07 02:59:39 +00:00
|
|
|
wcr := writeconcurrencylimiter.GetReader(bc)
|
|
|
|
defer writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
r := io.Reader(wcr)
|
|
|
|
|
2021-05-08 14:55:44 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var (
|
|
|
|
callbackErrLock sync.Mutex
|
|
|
|
callbackErr error
|
|
|
|
)
|
|
|
|
for {
|
2023-01-07 02:59:39 +00:00
|
|
|
reqBuf, err := readBlock(nil, r, bc, isReadOnly)
|
2022-10-17 21:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
wg.Wait()
|
|
|
|
if err == io.EOF {
|
|
|
|
// Remote end gracefully closed the connection.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blocksRead.Inc()
|
2022-10-17 21:06:56 +00:00
|
|
|
uw := getUnmarshalWork()
|
2022-10-17 21:24:02 +00:00
|
|
|
uw.reqBuf = reqBuf
|
2021-05-08 14:55:44 +00:00
|
|
|
uw.callback = func(rows []storage.MetricRow) {
|
|
|
|
if err := callback(rows); err != nil {
|
|
|
|
processErrors.Inc()
|
|
|
|
callbackErrLock.Lock()
|
|
|
|
if callbackErr == nil {
|
|
|
|
callbackErr = fmt.Errorf("error when processing native block: %w", err)
|
|
|
|
}
|
|
|
|
callbackErrLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uw.wg = &wg
|
|
|
|
wg.Add(1)
|
|
|
|
common.ScheduleUnmarshalWork(uw)
|
2023-01-07 02:59:39 +00:00
|
|
|
wcr.DecConcurrency()
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// readBlock reads the next data block from vminsert-initiated bc, appends it to dst and returns the result.
|
2023-01-07 02:59:39 +00:00
|
|
|
func readBlock(dst []byte, r io.Reader, bc *handshake.BufferedConn, isReadOnly func() bool) ([]byte, error) {
|
2021-10-08 10:52:56 +00:00
|
|
|
sizeBuf := auxBufPool.Get()
|
|
|
|
defer auxBufPool.Put(sizeBuf)
|
2022-02-01 15:48:25 +00:00
|
|
|
sizeBuf.B = bytesutil.ResizeNoCopyMayOverallocate(sizeBuf.B, 8)
|
2023-01-07 02:59:39 +00:00
|
|
|
if _, err := io.ReadFull(r, sizeBuf.B); err != nil {
|
2021-05-08 14:55:44 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
readErrors.Inc()
|
|
|
|
err = fmt.Errorf("cannot read packet size: %w", err)
|
|
|
|
}
|
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
packetSize := encoding.UnmarshalUint64(sizeBuf.B)
|
2022-04-05 12:35:08 +00:00
|
|
|
if packetSize > consts.MaxInsertPacketSizeForVMStorage {
|
2021-05-08 14:55:44 +00:00
|
|
|
parseErrors.Inc()
|
2022-04-05 12:35:08 +00:00
|
|
|
return dst, fmt.Errorf("too big packet size: %d; shouldn't exceed %d", packetSize, consts.MaxInsertPacketSizeForVMStorage)
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
|
|
|
dstLen := len(dst)
|
2022-02-01 15:48:25 +00:00
|
|
|
dst = bytesutil.ResizeWithCopyMayOverallocate(dst, dstLen+int(packetSize))
|
2023-01-07 02:59:39 +00:00
|
|
|
if n, err := io.ReadFull(r, dst[dstLen:]); err != nil {
|
2021-05-08 14:55:44 +00:00
|
|
|
readErrors.Inc()
|
|
|
|
return dst, fmt.Errorf("cannot read packet with size %d bytes: %w; read only %d bytes", packetSize, err, n)
|
|
|
|
}
|
2021-10-08 10:52:56 +00:00
|
|
|
if isReadOnly != nil && isReadOnly() {
|
|
|
|
// The vmstorage is in readonly mode, so drop the read block of data
|
|
|
|
// and send `read only` status to vminsert.
|
|
|
|
dst = dst[:dstLen]
|
|
|
|
if err := sendAck(bc, 2); err != nil {
|
|
|
|
writeErrors.Inc()
|
|
|
|
return dst, fmt.Errorf("cannot send readonly status to vminsert: %w", err)
|
|
|
|
}
|
|
|
|
return dst, nil
|
|
|
|
}
|
2021-05-08 14:55:44 +00:00
|
|
|
// Send `ack` to vminsert that the packet has been received.
|
2021-10-08 10:52:56 +00:00
|
|
|
if err := sendAck(bc, 1); err != nil {
|
|
|
|
writeErrors.Inc()
|
|
|
|
return dst, fmt.Errorf("cannot send `ack` to vminsert: %w", err)
|
|
|
|
}
|
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendAck(bc *handshake.BufferedConn, status byte) error {
|
2021-05-08 14:55:44 +00:00
|
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
|
|
if err := bc.SetWriteDeadline(deadline); err != nil {
|
2021-10-08 10:52:56 +00:00
|
|
|
return fmt.Errorf("cannot set write deadline: %w", err)
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
2021-10-08 10:52:56 +00:00
|
|
|
b := auxBufPool.Get()
|
|
|
|
defer auxBufPool.Put(b)
|
2021-10-08 12:37:57 +00:00
|
|
|
b.B = append(b.B[:0], status)
|
|
|
|
if _, err := bc.Write(b.B); err != nil {
|
2021-10-08 10:52:56 +00:00
|
|
|
return err
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
2023-09-01 07:34:16 +00:00
|
|
|
return bc.Flush()
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:52:56 +00:00
|
|
|
var auxBufPool bytesutil.ByteBufferPool
|
2021-05-08 14:55:44 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
readErrors = metrics.NewCounter(`vm_protoparser_read_errors_total{type="clusternative"}`)
|
|
|
|
writeErrors = metrics.NewCounter(`vm_protoparser_write_errors_total{type="clusternative"}`)
|
|
|
|
rowsRead = metrics.NewCounter(`vm_protoparser_rows_read_total{type="clusternative"}`)
|
|
|
|
blocksRead = metrics.NewCounter(`vm_protoparser_blocks_read_total{type="clusternative"}`)
|
|
|
|
|
|
|
|
parseErrors = metrics.NewCounter(`vm_protoparser_parse_errors_total{type="clusternative"}`)
|
|
|
|
processErrors = metrics.NewCounter(`vm_protoparser_process_errors_total{type="clusternative"}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
type unmarshalWork struct {
|
2021-07-02 10:32:56 +00:00
|
|
|
wg *sync.WaitGroup
|
|
|
|
callback func(rows []storage.MetricRow)
|
|
|
|
reqBuf []byte
|
|
|
|
mrs []storage.MetricRow
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 21:06:56 +00:00
|
|
|
func (uw *unmarshalWork) reset() {
|
|
|
|
uw.wg = nil
|
|
|
|
uw.callback = nil
|
|
|
|
// Zero reqBuf, since it may occupy big amounts of memory (consts.MaxInsertPacketSizeForVMStorage).
|
|
|
|
uw.reqBuf = nil
|
|
|
|
uw.mrs = uw.mrs[:0]
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:55:44 +00:00
|
|
|
// Unmarshal implements common.UnmarshalWork
|
|
|
|
func (uw *unmarshalWork) Unmarshal() {
|
2021-06-23 12:45:05 +00:00
|
|
|
reqBuf := uw.reqBuf
|
|
|
|
for len(reqBuf) > 0 {
|
2021-05-08 14:55:44 +00:00
|
|
|
// Limit the number of rows passed to callback in order to reduce memory usage
|
|
|
|
// when processing big packets of rows.
|
2021-06-23 12:45:05 +00:00
|
|
|
mrs, tail, err := storage.UnmarshalMetricRows(uw.mrs[:0], reqBuf, maxRowsPerCallback)
|
|
|
|
uw.mrs = mrs
|
|
|
|
if err != nil {
|
|
|
|
parseErrors.Inc()
|
|
|
|
logger.Errorf("cannot unmarshal MetricRow from clusternative block with size %d (remaining %d bytes): %s", len(reqBuf), len(tail), err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rowsRead.Add(len(mrs))
|
|
|
|
uw.callback(mrs)
|
|
|
|
reqBuf = tail
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
2021-06-23 12:45:05 +00:00
|
|
|
wg := uw.wg
|
|
|
|
wg.Done()
|
2022-10-17 21:06:56 +00:00
|
|
|
putUnmarshalWork(uw)
|
2021-05-08 14:55:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const maxRowsPerCallback = 10000
|
2022-10-17 21:06:56 +00:00
|
|
|
|
|
|
|
func getUnmarshalWork() *unmarshalWork {
|
|
|
|
v := unmarshalWorkPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &unmarshalWork{}
|
|
|
|
}
|
|
|
|
return v.(*unmarshalWork)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putUnmarshalWork(uw *unmarshalWork) {
|
|
|
|
uw.reset()
|
|
|
|
unmarshalWorkPool.Put(uw)
|
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalWorkPool sync.Pool
|