2023-06-20 05:55:12 +00:00
|
|
|
package elasticsearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
2023-08-11 13:52:57 +00:00
|
|
|
"flag"
|
2023-06-20 05:55:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-09-29 09:55:38 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert/insertutils"
|
2023-06-20 05:55:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bufferedwriter"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
2023-06-22 02:39:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
2023-06-20 05:55:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"
|
|
|
|
)
|
|
|
|
|
2023-08-11 13:52:57 +00:00
|
|
|
var (
|
|
|
|
elasticsearchVersion = flag.String("elasticsearch.version", "8.9.0", "Elasticsearch version to report to client")
|
|
|
|
)
|
|
|
|
|
2023-07-14 17:52:11 +00:00
|
|
|
// RequestHandler processes Elasticsearch insert requests
|
2023-06-20 05:55:12 +00:00
|
|
|
func RequestHandler(path string, w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
// This header is needed for Logstash
|
|
|
|
w.Header().Set("X-Elastic-Product", "Elasticsearch")
|
|
|
|
|
|
|
|
if strings.HasPrefix(path, "/_ilm/policy") {
|
2023-07-14 17:52:11 +00:00
|
|
|
// Return fake response for Elasticsearch ilm request.
|
2023-06-20 05:55:12 +00:00
|
|
|
fmt.Fprintf(w, `{}`)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(path, "/_index_template") {
|
2023-07-14 17:52:11 +00:00
|
|
|
// Return fake response for Elasticsearch index template request.
|
2023-06-20 05:55:12 +00:00
|
|
|
fmt.Fprintf(w, `{}`)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(path, "/_ingest") {
|
2023-07-14 17:52:11 +00:00
|
|
|
// Return fake response for Elasticsearch ingest pipeline request.
|
2023-06-20 05:55:12 +00:00
|
|
|
// See: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/put-pipeline-api.html
|
|
|
|
fmt.Fprintf(w, `{}`)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(path, "/_nodes") {
|
2023-07-14 17:52:11 +00:00
|
|
|
// Return fake response for Elasticsearch nodes discovery request.
|
2023-06-20 05:55:12 +00:00
|
|
|
// See: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/cluster.html
|
|
|
|
fmt.Fprintf(w, `{}`)
|
|
|
|
return true
|
|
|
|
}
|
2024-08-06 13:43:33 +00:00
|
|
|
if strings.HasPrefix(path, "/logstash") || strings.HasPrefix(path, "/_logstash") {
|
|
|
|
// Return fake response for Logstash APIs requests.
|
|
|
|
// See: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/logstash-apis.html
|
|
|
|
fmt.Fprintf(w, `{}`)
|
|
|
|
return true
|
|
|
|
}
|
2023-06-20 05:55:12 +00:00
|
|
|
switch path {
|
|
|
|
case "/":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2023-07-14 17:52:11 +00:00
|
|
|
// Return fake response for Elasticsearch ping request.
|
|
|
|
// See the latest available version for Elasticsearch at https://github.com/elastic/elasticsearch/releases
|
2023-06-20 05:55:12 +00:00
|
|
|
fmt.Fprintf(w, `{
|
|
|
|
"version": {
|
2023-08-11 13:52:57 +00:00
|
|
|
"number": %q
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2023-08-11 13:52:57 +00:00
|
|
|
}`, *elasticsearchVersion)
|
2023-06-20 05:55:12 +00:00
|
|
|
case http.MethodHead:
|
|
|
|
// Return empty response for Logstash ping request.
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
case "/_license":
|
2023-07-14 17:52:11 +00:00
|
|
|
// Return fake response for Elasticsearch license request.
|
2023-06-20 05:55:12 +00:00
|
|
|
fmt.Fprintf(w, `{
|
|
|
|
"license": {
|
|
|
|
"uid": "cbff45e7-c553-41f7-ae4f-9205eabd80xx",
|
|
|
|
"type": "oss",
|
|
|
|
"status": "active",
|
|
|
|
"expiry_date_in_millis" : 4000000000000
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
return true
|
|
|
|
case "/_bulk":
|
|
|
|
startTime := time.Now()
|
|
|
|
bulkRequestsTotal.Inc()
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
cp, err := insertutils.GetCommonParams(r)
|
2023-06-20 05:55:12 +00:00
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return true
|
|
|
|
}
|
2023-10-02 14:26:02 +00:00
|
|
|
if err := vlstorage.CanWriteData(); err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return true
|
|
|
|
}
|
2024-06-17 20:28:15 +00:00
|
|
|
lmp := cp.NewLogMessageProcessor()
|
2023-06-20 05:55:12 +00:00
|
|
|
isGzip := r.Header.Get("Content-Encoding") == "gzip"
|
2024-06-17 20:28:15 +00:00
|
|
|
n, err := readBulkRequest(r.Body, isGzip, cp.TimeField, cp.MsgField, lmp)
|
|
|
|
lmp.MustClose()
|
2023-06-20 05:55:12 +00:00
|
|
|
if err != nil {
|
2023-11-17 14:58:52 +00:00
|
|
|
logger.Warnf("cannot decode log message #%d in /_bulk request: %s, stream fields: %s", n, err, cp.StreamFields)
|
2023-06-20 05:55:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
tookMs := time.Since(startTime).Milliseconds()
|
|
|
|
bw := bufferedwriter.Get(w)
|
|
|
|
defer bufferedwriter.Put(bw)
|
|
|
|
WriteBulkResponse(bw, n, tookMs)
|
|
|
|
_ = bw.Flush()
|
2023-09-18 21:58:32 +00:00
|
|
|
|
|
|
|
// update bulkRequestDuration only for successfully parsed requests
|
|
|
|
// There is no need in updating bulkRequestDuration for request errors,
|
|
|
|
// since their timings are usually much smaller than the timing for successful request parsing.
|
|
|
|
bulkRequestDuration.UpdateDuration(startTime)
|
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 03:02:46 +00:00
|
|
|
var (
|
2023-09-16 13:10:29 +00:00
|
|
|
bulkRequestsTotal = metrics.NewCounter(`vl_http_requests_total{path="/insert/elasticsearch/_bulk"}`)
|
2023-09-18 21:58:32 +00:00
|
|
|
rowsIngestedTotal = metrics.NewCounter(`vl_rows_ingested_total{type="elasticsearch_bulk"}`)
|
|
|
|
bulkRequestDuration = metrics.NewHistogram(`vl_http_request_duration_seconds{path="/insert/elasticsearch/_bulk"}`)
|
2023-06-21 03:02:46 +00:00
|
|
|
)
|
2023-06-20 05:55:12 +00:00
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
func readBulkRequest(r io.Reader, isGzip bool, timeField, msgField string, lmp insertutils.LogMessageProcessor) (int, error) {
|
2023-06-20 05:55:12 +00:00
|
|
|
// See https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
|
|
|
|
|
|
|
|
if isGzip {
|
2023-06-22 02:39:22 +00:00
|
|
|
zr, err := common.GetGzipReader(r)
|
2023-06-20 05:55:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot read gzipped _bulk request: %w", err)
|
|
|
|
}
|
2023-06-22 02:39:22 +00:00
|
|
|
defer common.PutGzipReader(zr)
|
2023-06-20 05:55:12 +00:00
|
|
|
r = zr
|
|
|
|
}
|
|
|
|
|
|
|
|
wcr := writeconcurrencylimiter.GetReader(r)
|
|
|
|
defer writeconcurrencylimiter.PutReader(wcr)
|
|
|
|
|
|
|
|
lb := lineBufferPool.Get()
|
|
|
|
defer lineBufferPool.Put(lb)
|
|
|
|
|
2023-06-22 02:39:22 +00:00
|
|
|
lb.B = bytesutil.ResizeNoCopyNoOverallocate(lb.B, insertutils.MaxLineSizeBytes.IntN())
|
2023-06-20 05:55:12 +00:00
|
|
|
sc := bufio.NewScanner(wcr)
|
|
|
|
sc.Buffer(lb.B, len(lb.B))
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
nCheckpoint := 0
|
|
|
|
for {
|
2024-06-17 20:28:15 +00:00
|
|
|
ok, err := readBulkLine(sc, timeField, msgField, lmp)
|
2023-06-20 05:55:12 +00:00
|
|
|
wcr.DecConcurrency()
|
|
|
|
if err != nil || !ok {
|
|
|
|
rowsIngestedTotal.Add(n - nCheckpoint)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
if batchSize := n - nCheckpoint; n >= 1000 {
|
|
|
|
rowsIngestedTotal.Add(batchSize)
|
|
|
|
nCheckpoint = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var lineBufferPool bytesutil.ByteBufferPool
|
|
|
|
|
2024-06-17 20:28:15 +00:00
|
|
|
func readBulkLine(sc *bufio.Scanner, timeField, msgField string, lmp insertutils.LogMessageProcessor) (bool, error) {
|
2023-06-21 04:23:36 +00:00
|
|
|
var line []byte
|
|
|
|
|
2023-06-21 04:16:11 +00:00
|
|
|
// Read the command, must be "create" or "index"
|
2023-06-21 04:23:36 +00:00
|
|
|
for len(line) == 0 {
|
|
|
|
if !sc.Scan() {
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
if errors.Is(err, bufio.ErrTooLong) {
|
|
|
|
return false, fmt.Errorf(`cannot read "create" or "index" command, since its size exceeds -insert.maxLineSizeBytes=%d`,
|
2023-06-22 02:39:22 +00:00
|
|
|
insertutils.MaxLineSizeBytes.IntN())
|
2023-06-21 04:23:36 +00:00
|
|
|
}
|
|
|
|
return false, err
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2023-06-21 04:23:36 +00:00
|
|
|
return false, nil
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2023-06-21 04:23:36 +00:00
|
|
|
line = sc.Bytes()
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2023-06-21 04:16:11 +00:00
|
|
|
lineStr := bytesutil.ToUnsafeString(line)
|
|
|
|
if !strings.Contains(lineStr, `"create"`) && !strings.Contains(lineStr, `"index"`) {
|
|
|
|
return false, fmt.Errorf(`unexpected command %q; expecting "create" or "index"`, line)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode log message
|
|
|
|
if !sc.Scan() {
|
|
|
|
if err := sc.Err(); err != nil {
|
|
|
|
if errors.Is(err, bufio.ErrTooLong) {
|
2023-06-22 02:39:22 +00:00
|
|
|
return false, fmt.Errorf("cannot read log message, since its size exceeds -insert.maxLineSizeBytes=%d", insertutils.MaxLineSizeBytes.IntN())
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf(`missing log message after the "create" or "index" command`)
|
|
|
|
}
|
|
|
|
line = sc.Bytes()
|
2024-05-20 02:08:30 +00:00
|
|
|
p := logstorage.GetJSONParser()
|
2024-05-22 19:01:20 +00:00
|
|
|
if err := p.ParseLogMessage(line); err != nil {
|
2023-06-21 04:16:11 +00:00
|
|
|
return false, fmt.Errorf("cannot parse json-encoded log entry: %w", err)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 23:21:47 +00:00
|
|
|
ts, err := extractTimestampFromFields(timeField, p.Fields)
|
2023-06-20 05:55:12 +00:00
|
|
|
if err != nil {
|
2023-06-21 04:16:11 +00:00
|
|
|
return false, fmt.Errorf("cannot parse timestamp: %w", err)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
if ts == 0 {
|
|
|
|
ts = time.Now().UnixNano()
|
|
|
|
}
|
2024-06-17 10:13:18 +00:00
|
|
|
logstorage.RenameField(p.Fields, msgField, "_msg")
|
2024-06-17 20:28:15 +00:00
|
|
|
lmp.AddRow(ts, p.Fields)
|
2024-05-20 02:08:30 +00:00
|
|
|
logstorage.PutJSONParser(p)
|
2023-09-29 09:55:38 +00:00
|
|
|
|
2023-06-20 05:55:12 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractTimestampFromFields(timeField string, fields []logstorage.Field) (int64, error) {
|
|
|
|
for i := range fields {
|
|
|
|
f := &fields[i]
|
|
|
|
if f.Name != timeField {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
timestamp, err := parseElasticsearchTimestamp(f.Value)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
f.Value = ""
|
|
|
|
return timestamp, nil
|
|
|
|
}
|
2023-07-20 23:21:47 +00:00
|
|
|
return 0, nil
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseElasticsearchTimestamp(s string) (int64, error) {
|
2023-07-20 23:21:47 +00:00
|
|
|
if s == "0" || s == "" {
|
|
|
|
// Special case - zero or empty timestamp must be substituted
|
|
|
|
// with the current time by the caller.
|
|
|
|
return 0, nil
|
|
|
|
}
|
2023-06-20 05:55:12 +00:00
|
|
|
if len(s) < len("YYYY-MM-DD") || s[len("YYYY")] != '-' {
|
|
|
|
// Try parsing timestamp in milliseconds
|
|
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot parse timestamp in milliseconds from %q: %w", s, err)
|
|
|
|
}
|
|
|
|
if n > int64(math.MaxInt64)/1e6 {
|
|
|
|
return 0, fmt.Errorf("too big timestamp in milliseconds: %d; mustn't exceed %d", n, int64(math.MaxInt64)/1e6)
|
|
|
|
}
|
|
|
|
if n < int64(math.MinInt64)/1e6 {
|
|
|
|
return 0, fmt.Errorf("too small timestamp in milliseconds: %d; must be bigger than %d", n, int64(math.MinInt64)/1e6)
|
|
|
|
}
|
|
|
|
n *= 1e6
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
if len(s) == len("YYYY-MM-DD") {
|
|
|
|
t, err := time.Parse("2006-01-02", s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("cannot parse date %q: %w", s, err)
|
|
|
|
}
|
|
|
|
return t.UnixNano(), nil
|
|
|
|
}
|
2024-06-17 10:13:18 +00:00
|
|
|
nsecs, ok := logstorage.TryParseTimestampRFC3339Nano(s)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("cannot parse timestamp %q", s)
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|
2024-06-17 10:13:18 +00:00
|
|
|
return nsecs, nil
|
2023-06-20 05:55:12 +00:00
|
|
|
}
|