2023-06-20 07:55:12 +02:00
|
|
|
package logsql
|
|
|
|
|
|
|
|
import (
|
2024-05-12 16:33:29 +02:00
|
|
|
"context"
|
2023-06-20 07:55:12 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2023-06-21 10:10:45 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2023-06-20 07:55:12 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
2024-02-18 21:58:47 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httputils"
|
2023-06-20 07:55:12 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
|
|
|
|
)
|
|
|
|
|
2023-06-21 10:10:45 +02:00
|
|
|
var (
|
|
|
|
maxSortBufferSize = flagutil.NewBytes("select.maxSortBufferSize", 1024*1024, "Query results from /select/logsql/query are automatically sorted by _time "+
|
2023-07-19 10:10:51 +02:00
|
|
|
"if their summary size doesn't exceed this value; otherwise, query results are streamed in the response without sorting; "+
|
|
|
|
"too big value for this flag may result in high memory usage since the sorting is performed in memory")
|
2023-06-21 10:10:45 +02:00
|
|
|
)
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
// ProcessQueryRequest handles /select/logsql/query request.
|
|
|
|
func ProcessQueryRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
2023-06-20 07:55:12 +02:00
|
|
|
// Extract tenantID
|
|
|
|
tenantID, err := logstorage.GetTenantIDFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
2024-02-18 22:01:34 +01:00
|
|
|
limit, err := httputils.GetInt(r, "limit")
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
2023-06-20 07:55:12 +02:00
|
|
|
|
|
|
|
qStr := r.FormValue("query")
|
|
|
|
q, err := logstorage.ParseQuery(qStr)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot parse query [%s]: %s", qStr, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/stream+json; charset=utf-8")
|
2024-02-18 22:01:34 +01:00
|
|
|
|
2023-06-21 10:10:45 +02:00
|
|
|
sw := getSortWriter()
|
2024-02-18 22:01:34 +01:00
|
|
|
sw.Init(w, maxSortBufferSize.IntN(), limit)
|
2023-06-20 07:55:12 +02:00
|
|
|
tenantIDs := []logstorage.TenantID{tenantID}
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
ctxWithCancel, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
writeBlock := func(_ uint, timestamps []int64, columns []logstorage.BlockColumn) {
|
2023-06-20 07:55:12 +02:00
|
|
|
if len(columns) == 0 {
|
2024-02-18 22:01:34 +01:00
|
|
|
return
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
2024-02-18 22:01:34 +01:00
|
|
|
|
2023-06-20 07:55:12 +02:00
|
|
|
bb := blockResultPool.Get()
|
2024-05-12 16:33:29 +02:00
|
|
|
for i := range timestamps {
|
|
|
|
WriteJSONRow(bb, columns, i)
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
2024-02-18 21:58:47 +01:00
|
|
|
|
2024-02-18 22:01:34 +01:00
|
|
|
if !sw.TryWrite(bb.B) {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
blockResultPool.Put(bb)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = vlstorage.RunQuery(ctxWithCancel, tenantIDs, q, writeBlock)
|
|
|
|
|
2023-06-21 10:10:45 +02:00
|
|
|
sw.FinalFlush()
|
|
|
|
putSortWriter(sw)
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "cannot execute query [%s]: %s", qStr, err)
|
|
|
|
}
|
|
|
|
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var blockResultPool bytesutil.ByteBufferPool
|