VictoriaMetrics/lib/logstorage/pipe_extract.go

219 lines
4.7 KiB
Go
Raw Normal View History

2024-05-19 02:24:32 +00:00
package logstorage
import (
"fmt"
2024-05-19 10:56:08 +00:00
"unsafe"
2024-05-19 02:24:32 +00:00
)
2024-05-19 13:11:17 +00:00
// pipeExtract processes '| extract from <field> <pattern>' pipe.
2024-05-19 10:56:08 +00:00
//
// See https://docs.victoriametrics.com/victorialogs/logsql/#extract-pipe
type pipeExtract struct {
2024-05-19 11:47:30 +00:00
fromField string
2024-05-20 10:43:46 +00:00
steps []patternStep
2024-05-19 10:56:08 +00:00
2024-05-19 13:11:17 +00:00
pattern string
2024-05-20 14:09:07 +00:00
// iff is an optional filter for skipping the extract func
iff filter
iffNeededFields []string
2024-05-19 10:56:08 +00:00
}
func (pe *pipeExtract) String() string {
2024-05-19 11:47:30 +00:00
s := "extract"
if !isMsgFieldName(pe.fromField) {
s += " from " + quoteTokenIfNeeded(pe.fromField)
}
2024-05-19 13:11:17 +00:00
s += " " + quoteTokenIfNeeded(pe.pattern)
2024-05-20 14:09:07 +00:00
if pe.iff != nil {
s += " if (" + pe.iff.String() + ")"
}
2024-05-19 11:47:30 +00:00
return s
2024-05-19 10:56:08 +00:00
}
func (pe *pipeExtract) updateNeededFields(neededFields, unneededFields fieldsSet) {
2024-05-19 13:11:17 +00:00
if neededFields.contains("*") {
2024-05-19 19:25:52 +00:00
unneededFieldsOrig := unneededFields.clone()
2024-05-19 13:11:17 +00:00
needFromField := false
for _, step := range pe.steps {
if step.field != "" {
2024-05-19 19:25:52 +00:00
if !unneededFieldsOrig.contains(step.field) {
2024-05-19 13:11:17 +00:00
needFromField = true
}
2024-05-19 19:25:52 +00:00
unneededFields.add(step.field)
2024-05-19 13:11:17 +00:00
}
}
if needFromField {
2024-05-20 14:09:07 +00:00
unneededFields.removeFields(pe.iffNeededFields)
2024-05-19 13:11:17 +00:00
unneededFields.remove(pe.fromField)
} else {
unneededFields.add(pe.fromField)
}
} else {
2024-05-20 14:09:07 +00:00
neededFieldsOrig := neededFields.clone()
2024-05-19 13:11:17 +00:00
needFromField := false
for _, step := range pe.steps {
2024-05-20 14:09:07 +00:00
if step.field != "" && neededFieldsOrig.contains(step.field) {
2024-05-19 19:25:52 +00:00
needFromField = true
neededFields.remove(step.field)
2024-05-19 13:11:17 +00:00
}
}
if needFromField {
2024-05-20 14:09:07 +00:00
neededFields.addFields(pe.iffNeededFields)
2024-05-19 13:11:17 +00:00
neededFields.add(pe.fromField)
2024-05-19 10:56:08 +00:00
}
}
}
2024-05-19 19:27:53 +00:00
func (pe *pipeExtract) newPipeProcessor(workersCount int, _ <-chan struct{}, _ func(), ppBase pipeProcessor) pipeProcessor {
2024-05-19 10:56:08 +00:00
shards := make([]pipeExtractProcessorShard, workersCount)
for i := range shards {
2024-05-20 10:43:46 +00:00
ef := newPattern(pe.steps)
2024-05-19 10:56:08 +00:00
shards[i] = pipeExtractProcessorShard{
pipeExtractProcessorShardNopad: pipeExtractProcessorShardNopad{
2024-05-20 14:09:07 +00:00
ef: ef,
2024-05-19 10:56:08 +00:00
},
}
}
pep := &pipeExtractProcessor{
pe: pe,
ppBase: ppBase,
shards: shards,
}
return pep
}
type pipeExtractProcessor struct {
pe *pipeExtract
ppBase pipeProcessor
shards []pipeExtractProcessorShard
}
type pipeExtractProcessorShard struct {
pipeExtractProcessorShardNopad
// The padding prevents false sharing on widespread platforms with 128 mod (cache line size) = 0 .
_ [128 - unsafe.Sizeof(pipeExtractProcessorShardNopad{})%128]byte
}
type pipeExtractProcessorShardNopad struct {
2024-05-20 10:43:46 +00:00
ef *pattern
2024-05-19 11:23:27 +00:00
2024-05-20 14:09:07 +00:00
bm bitmap
uctx fieldsUnpackerContext
wctx pipeUnpackWriteContext
2024-05-19 10:56:08 +00:00
}
func (pep *pipeExtractProcessor) writeBlock(workerID uint, br *blockResult) {
if len(br.timestamps) == 0 {
return
}
shard := &pep.shards[workerID]
2024-05-20 14:09:07 +00:00
shard.wctx.init(br, pep.ppBase)
2024-05-19 10:56:08 +00:00
ef := shard.ef
2024-05-20 14:09:07 +00:00
bm := &shard.bm
bm.init(len(br.timestamps))
bm.setBits()
if iff := pep.pe.iff; iff != nil {
iff.applyToBlockResult(br, bm)
if bm.isZero() {
// Fast path - nothing to extract.
pep.ppBase.writeBlock(workerID, br)
return
}
}
2024-05-19 12:22:09 +00:00
c := br.getColumnByName(pep.pe.fromField)
if c.isConst {
v := c.valuesEncoded[0]
2024-05-19 10:56:08 +00:00
ef.apply(v)
2024-05-20 14:09:07 +00:00
for i := range br.timestamps {
shard.uctx.resetFields()
if bm.isSetBit(i) {
for _, f := range ef.fields {
shard.uctx.addField(f.name, *f.value, "")
}
2024-05-19 12:22:09 +00:00
}
2024-05-20 14:09:07 +00:00
shard.wctx.writeRow(i, shard.uctx.fields)
2024-05-19 12:22:09 +00:00
}
} else {
values := c.getValues(br)
2024-05-20 14:09:07 +00:00
vPrevApplied := ""
2024-05-19 12:22:09 +00:00
for i, v := range values {
2024-05-20 14:09:07 +00:00
shard.uctx.resetFields()
if bm.isSetBit(i) {
if vPrevApplied != v {
ef.apply(v)
vPrevApplied = v
}
for _, f := range ef.fields {
shard.uctx.addField(f.name, *f.value, "")
}
2024-05-19 12:22:09 +00:00
}
2024-05-20 14:09:07 +00:00
shard.wctx.writeRow(i, shard.uctx.fields)
2024-05-19 11:23:27 +00:00
}
}
2024-05-19 12:22:09 +00:00
2024-05-20 14:09:07 +00:00
shard.wctx.flush()
shard.uctx.reset()
2024-05-19 10:56:08 +00:00
}
func (pep *pipeExtractProcessor) flush() error {
return nil
}
2024-05-19 11:47:30 +00:00
func parsePipeExtract(lex *lexer) (*pipeExtract, error) {
if !lex.isKeyword("extract") {
return nil, fmt.Errorf("unexpected token: %q; want %q", lex.token, "extract")
}
lex.nextToken()
fromField := "_msg"
if lex.isKeyword("from") {
lex.nextToken()
f, err := parseFieldName(lex)
if err != nil {
return nil, fmt.Errorf("cannot parse 'from' field name: %w", err)
}
fromField = f
}
2024-05-20 14:09:07 +00:00
// parse pattern
2024-05-19 13:11:17 +00:00
pattern, err := getCompoundToken(lex)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-19 13:11:17 +00:00
return nil, fmt.Errorf("cannot read 'pattern': %w", err)
2024-05-19 11:47:30 +00:00
}
2024-05-20 10:43:46 +00:00
steps, err := parsePatternSteps(pattern)
2024-05-19 11:47:30 +00:00
if err != nil {
2024-05-19 13:11:17 +00:00
return nil, fmt.Errorf("cannot parse 'pattern' %q: %w", pattern, err)
2024-05-19 11:47:30 +00:00
}
pe := &pipeExtract{
fromField: fromField,
steps: steps,
2024-05-19 13:11:17 +00:00
pattern: pattern,
2024-05-19 11:47:30 +00:00
}
2024-05-20 14:09:07 +00:00
// parse optional if (...)
if lex.isKeyword("if") {
iff, err := parseIfFilter(lex)
if err != nil {
2024-05-20 14:41:08 +00:00
return nil, err
2024-05-20 14:09:07 +00:00
}
pe.iff = iff
neededFields := newFieldsSet()
iff.updateNeededFields(neededFields)
pe.iffNeededFields = neededFields.getAll()
}
2024-05-19 11:47:30 +00:00
return pe, nil
}