2024-05-22 21:01:20 +02:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-05 03:18:12 +02:00
|
|
|
"math"
|
2024-05-22 21:01:20 +02:00
|
|
|
"unsafe"
|
2024-05-28 19:29:41 +02:00
|
|
|
|
2024-07-05 01:17:03 +02:00
|
|
|
"github.com/valyala/quicktemplate"
|
|
|
|
|
2024-05-28 19:29:41 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2024-05-22 21:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// pipeFormat processes '| format ...' pipe.
|
|
|
|
//
|
|
|
|
// See https://docs.victoriametrics.com/victorialogs/logsql/#format-pipe
|
|
|
|
type pipeFormat struct {
|
|
|
|
formatStr string
|
|
|
|
steps []patternStep
|
|
|
|
|
|
|
|
resultField string
|
|
|
|
|
2024-05-25 00:30:58 +02:00
|
|
|
keepOriginalFields bool
|
|
|
|
skipEmptyResults bool
|
|
|
|
|
2024-05-22 21:01:20 +02:00
|
|
|
// iff is an optional filter for skipping the format func
|
|
|
|
iff *ifFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pf *pipeFormat) String() string {
|
|
|
|
s := "format"
|
|
|
|
if pf.iff != nil {
|
|
|
|
s += " " + pf.iff.String()
|
|
|
|
}
|
|
|
|
s += " " + quoteTokenIfNeeded(pf.formatStr)
|
|
|
|
if !isMsgFieldName(pf.resultField) {
|
|
|
|
s += " as " + quoteTokenIfNeeded(pf.resultField)
|
|
|
|
}
|
2024-05-25 00:30:58 +02:00
|
|
|
if pf.keepOriginalFields {
|
|
|
|
s += " keep_original_fields"
|
|
|
|
}
|
|
|
|
if pf.skipEmptyResults {
|
|
|
|
s += " skip_empty_results"
|
|
|
|
}
|
2024-05-22 21:01:20 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:18:42 +02:00
|
|
|
func (pf *pipeFormat) canLiveTail() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-22 21:01:20 +02:00
|
|
|
func (pf *pipeFormat) updateNeededFields(neededFields, unneededFields fieldsSet) {
|
2024-05-30 16:19:23 +02:00
|
|
|
if neededFields.isEmpty() {
|
|
|
|
if pf.iff != nil {
|
|
|
|
neededFields.addFields(pf.iff.neededFields)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-22 21:01:20 +02:00
|
|
|
if neededFields.contains("*") {
|
|
|
|
if !unneededFields.contains(pf.resultField) {
|
2024-05-25 00:30:58 +02:00
|
|
|
if !pf.keepOriginalFields && !pf.skipEmptyResults {
|
|
|
|
unneededFields.add(pf.resultField)
|
|
|
|
}
|
2024-05-22 21:01:20 +02:00
|
|
|
if pf.iff != nil {
|
|
|
|
unneededFields.removeFields(pf.iff.neededFields)
|
|
|
|
}
|
|
|
|
for _, step := range pf.steps {
|
|
|
|
if step.field != "" {
|
|
|
|
unneededFields.remove(step.field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if neededFields.contains(pf.resultField) {
|
2024-05-25 00:30:58 +02:00
|
|
|
if !pf.keepOriginalFields && !pf.skipEmptyResults {
|
|
|
|
neededFields.remove(pf.resultField)
|
|
|
|
}
|
2024-05-22 21:01:20 +02:00
|
|
|
if pf.iff != nil {
|
|
|
|
neededFields.addFields(pf.iff.neededFields)
|
|
|
|
}
|
|
|
|
for _, step := range pf.steps {
|
|
|
|
if step.field != "" {
|
|
|
|
neededFields.add(step.field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-25 21:36:16 +02:00
|
|
|
func (pf *pipeFormat) optimize() {
|
|
|
|
pf.iff.optimizeFilterIn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pf *pipeFormat) hasFilterInWithQuery() bool {
|
|
|
|
return pf.iff.hasFilterInWithQuery()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pf *pipeFormat) initFilterInValues(cache map[string][]string, getFieldValuesFunc getFieldValuesFunc) (pipe, error) {
|
|
|
|
iffNew, err := pf.iff.initFilterInValues(cache, getFieldValuesFunc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pfNew := *pf
|
|
|
|
pfNew.iff = iffNew
|
|
|
|
return &pfNew, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pf *pipeFormat) newPipeProcessor(workersCount int, _ <-chan struct{}, _ func(), ppNext pipeProcessor) pipeProcessor {
|
2024-05-22 21:01:20 +02:00
|
|
|
return &pipeFormatProcessor{
|
|
|
|
pf: pf,
|
2024-05-25 21:36:16 +02:00
|
|
|
ppNext: ppNext,
|
2024-05-22 21:01:20 +02:00
|
|
|
|
|
|
|
shards: make([]pipeFormatProcessorShard, workersCount),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeFormatProcessor struct {
|
|
|
|
pf *pipeFormat
|
2024-05-25 21:36:16 +02:00
|
|
|
ppNext pipeProcessor
|
2024-05-22 21:01:20 +02:00
|
|
|
|
|
|
|
shards []pipeFormatProcessorShard
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeFormatProcessorShard struct {
|
|
|
|
pipeFormatProcessorShardNopad
|
|
|
|
|
|
|
|
// The padding prevents false sharing on widespread platforms with 128 mod (cache line size) = 0 .
|
|
|
|
_ [128 - unsafe.Sizeof(pipeFormatProcessorShardNopad{})%128]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeFormatProcessorShardNopad struct {
|
|
|
|
bm bitmap
|
|
|
|
|
2024-05-25 21:36:16 +02:00
|
|
|
a arena
|
|
|
|
rc resultColumn
|
2024-05-22 21:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pfp *pipeFormatProcessor) writeBlock(workerID uint, br *blockResult) {
|
2024-09-25 16:16:53 +02:00
|
|
|
if br.rowsLen == 0 {
|
2024-05-22 21:01:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shard := &pfp.shards[workerID]
|
2024-05-25 21:36:16 +02:00
|
|
|
pf := pfp.pf
|
2024-05-22 21:01:20 +02:00
|
|
|
|
|
|
|
bm := &shard.bm
|
2024-09-25 16:16:53 +02:00
|
|
|
bm.init(br.rowsLen)
|
2024-05-22 21:01:20 +02:00
|
|
|
bm.setBits()
|
2024-05-25 21:36:16 +02:00
|
|
|
if iff := pf.iff; iff != nil {
|
2024-05-22 21:01:20 +02:00
|
|
|
iff.f.applyToBlockResult(br, bm)
|
|
|
|
if bm.isZero() {
|
2024-05-25 21:36:16 +02:00
|
|
|
pfp.ppNext.writeBlock(workerID, br)
|
2024-05-22 21:01:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-25 21:36:16 +02:00
|
|
|
shard.rc.name = pf.resultField
|
|
|
|
|
|
|
|
resultColumn := br.getColumnByName(pf.resultField)
|
2024-09-25 16:16:53 +02:00
|
|
|
for rowIdx := 0; rowIdx < br.rowsLen; rowIdx++ {
|
2024-05-25 21:36:16 +02:00
|
|
|
v := ""
|
2024-05-22 21:01:20 +02:00
|
|
|
if bm.isSetBit(rowIdx) {
|
2024-05-25 21:36:16 +02:00
|
|
|
v = shard.formatRow(pf, br, rowIdx)
|
|
|
|
if v == "" && pf.skipEmptyResults || pf.keepOriginalFields {
|
|
|
|
if vOrig := resultColumn.getValueAtRow(br, rowIdx); vOrig != "" {
|
|
|
|
v = vOrig
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 21:01:20 +02:00
|
|
|
} else {
|
2024-05-25 21:36:16 +02:00
|
|
|
v = resultColumn.getValueAtRow(br, rowIdx)
|
2024-05-22 21:01:20 +02:00
|
|
|
}
|
2024-05-25 21:36:16 +02:00
|
|
|
shard.rc.addValue(v)
|
2024-05-22 21:01:20 +02:00
|
|
|
}
|
|
|
|
|
2024-05-25 21:36:16 +02:00
|
|
|
br.addResultColumn(&shard.rc)
|
|
|
|
pfp.ppNext.writeBlock(workerID, br)
|
|
|
|
|
|
|
|
shard.a.reset()
|
|
|
|
shard.rc.reset()
|
2024-05-22 21:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pfp *pipeFormatProcessor) flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-25 21:36:16 +02:00
|
|
|
func (shard *pipeFormatProcessorShard) formatRow(pf *pipeFormat, br *blockResult, rowIdx int) string {
|
2024-05-28 19:29:41 +02:00
|
|
|
b := shard.a.b
|
|
|
|
bLen := len(b)
|
2024-05-22 21:01:20 +02:00
|
|
|
for _, step := range pf.steps {
|
|
|
|
b = append(b, step.prefix...)
|
|
|
|
if step.field != "" {
|
|
|
|
c := br.getColumnByName(step.field)
|
|
|
|
v := c.getValueAtRow(br, rowIdx)
|
2024-06-05 03:18:12 +02:00
|
|
|
switch step.fieldOpt {
|
|
|
|
case "q":
|
2024-07-05 01:17:03 +02:00
|
|
|
b = quicktemplate.AppendJSONString(b, v, true)
|
2024-06-05 03:18:12 +02:00
|
|
|
case "time":
|
|
|
|
nsecs, ok := tryParseInt64(v)
|
|
|
|
if !ok {
|
|
|
|
b = append(b, v...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b = marshalTimestampRFC3339NanoString(b, nsecs)
|
|
|
|
case "duration":
|
|
|
|
nsecs, ok := tryParseInt64(v)
|
|
|
|
if !ok {
|
|
|
|
b = append(b, v...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b = marshalDurationString(b, nsecs)
|
|
|
|
case "ipv4":
|
|
|
|
ipNum, ok := tryParseUint64(v)
|
|
|
|
if !ok || ipNum > math.MaxUint32 {
|
|
|
|
b = append(b, v...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b = marshalIPv4String(b, uint32(ipNum))
|
|
|
|
default:
|
2024-05-22 21:01:20 +02:00
|
|
|
b = append(b, v...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 19:29:41 +02:00
|
|
|
shard.a.b = b
|
2024-05-22 21:01:20 +02:00
|
|
|
|
2024-05-28 19:29:41 +02:00
|
|
|
return bytesutil.ToUnsafeString(b[bLen:])
|
2024-05-22 21:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parsePipeFormat(lex *lexer) (*pipeFormat, error) {
|
|
|
|
if !lex.isKeyword("format") {
|
|
|
|
return nil, fmt.Errorf("unexpected token: %q; want %q", lex.token, "format")
|
|
|
|
}
|
|
|
|
lex.nextToken()
|
|
|
|
|
|
|
|
// parse optional if (...)
|
|
|
|
var iff *ifFilter
|
|
|
|
if lex.isKeyword("if") {
|
|
|
|
f, err := parseIfFilter(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
iff = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse format
|
|
|
|
formatStr, err := getCompoundToken(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot read 'format': %w", err)
|
|
|
|
}
|
|
|
|
steps, err := parsePatternSteps(formatStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse 'pattern' %q: %w", formatStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse optional 'as ...` part
|
|
|
|
resultField := "_msg"
|
|
|
|
if lex.isKeyword("as") {
|
|
|
|
lex.nextToken()
|
|
|
|
field, err := parseFieldName(lex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse result field after 'format %q as': %w", formatStr, err)
|
|
|
|
}
|
|
|
|
resultField = field
|
|
|
|
}
|
|
|
|
|
2024-05-25 00:30:58 +02:00
|
|
|
keepOriginalFields := false
|
|
|
|
skipEmptyResults := false
|
|
|
|
switch {
|
|
|
|
case lex.isKeyword("keep_original_fields"):
|
|
|
|
lex.nextToken()
|
|
|
|
keepOriginalFields = true
|
|
|
|
case lex.isKeyword("skip_empty_results"):
|
|
|
|
lex.nextToken()
|
|
|
|
skipEmptyResults = true
|
|
|
|
}
|
|
|
|
|
2024-05-22 21:01:20 +02:00
|
|
|
pf := &pipeFormat{
|
2024-05-25 00:30:58 +02:00
|
|
|
formatStr: formatStr,
|
|
|
|
steps: steps,
|
|
|
|
resultField: resultField,
|
|
|
|
keepOriginalFields: keepOriginalFields,
|
|
|
|
skipEmptyResults: skipEmptyResults,
|
|
|
|
iff: iff,
|
2024-05-22 21:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pf, nil
|
|
|
|
}
|