From 8d2b3f7aa5be5bee89a8e1d2cb006fd28caf4289 Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Sat, 29 Jan 2022 13:39:21 +0100 Subject: [PATCH] Return early if push payload does not contain data (#5229) Optimize for: * payload does not contain any streams * a stream does not contain any entries * none of the streams contain any entries Signed-off-by: Christian Haudum --- pkg/distributor/distributor.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/distributor/distributor.go b/pkg/distributor/distributor.go index 8030f3dee5..2b65da90bf 100644 --- a/pkg/distributor/distributor.go +++ b/pkg/distributor/distributor.go @@ -222,18 +222,28 @@ func (d *Distributor) Push(ctx context.Context, req *logproto.PushRequest) (*log return nil, err } + // Return early if request does not contain any streams + if len(req.Streams) == 0 { + return &logproto.PushResponse{}, nil + } + // First we flatten out the request into a list of samples. // We use the heuristic of 1 sample per TS to size the array. // We also work out the hash value at the same time. streams := make([]streamTracker, 0, len(req.Streams)) keys := make([]uint32, 0, len(req.Streams)) - var validationErr error validatedSamplesSize := 0 validatedSamplesCount := 0 + var validationErr error validationContext := d.validator.getValidationContextForTime(time.Now(), userID) for _, stream := range req.Streams { + // Return early if stream does not contain any entries + if len(stream.Entries) == 0 { + continue + } + // Truncate first so subsequent steps have consistent line lengths d.truncateLines(validationContext, &stream) @@ -262,16 +272,11 @@ func (d *Distributor) Push(ctx context.Context, req *logproto.PushRequest) (*log } stream.Entries = stream.Entries[:n] - if len(stream.Entries) == 0 { - continue - } - keys = append(keys, util.TokenFor(userID, stream.Labels)) - streams = append(streams, streamTracker{ - stream: stream, - }) + streams = append(streams, streamTracker{stream: stream}) } + // Return early if none of the streams contained entries if len(streams) == 0 { return &logproto.PushResponse{}, validationErr }