Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce to a single code path through sendBatches regardless of limit #6216

Merged
merged 4 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/ingester/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,13 @@ func (i *Ingester) Query(req *logproto.QueryRequest, queryServer logproto.Querie

defer errUtil.LogErrorWithContext(ctx, "closing iterator", it.Close)

return sendBatches(ctx, it, queryServer, req.Limit)
// sendBatches uses -1 to specify no limit.
batchLimit := int32(req.Limit)
if batchLimit == 0 {
batchLimit = -1
}

return sendBatches(ctx, it, queryServer, batchLimit)
}

// QuerySample the ingesters for series from logs matching a set of matchers.
Expand Down
37 changes: 11 additions & 26 deletions pkg/ingester/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,38 +695,23 @@ type QuerierQueryServer interface {
Send(res *logproto.QueryResponse) error
}

func sendBatches(ctx context.Context, i iter.EntryIterator, queryServer QuerierQueryServer, limit uint32) error {
func sendBatches(ctx context.Context, i iter.EntryIterator, queryServer QuerierQueryServer, limit int32) error {
stats := stats.FromContext(ctx)
if limit == 0 {
// send all batches.
for !isDone(ctx) {
batch, size, err := iter.ReadBatch(i, queryBatchSize)
if err != nil {
return err
}
if len(batch.Streams) == 0 {
return nil
}
stats.AddIngesterBatch(int64(size))
batch.Stats = stats.Ingester()

if err := queryServer.Send(batch); err != nil {
return err
}

stats.Reset()

}
return nil
}
// send until the limit is reached.
sent := uint32(0)
for sent < limit && !isDone(queryServer.Context()) {
batch, batchSize, err := iter.ReadBatch(i, math.MinUint32(queryBatchSize, limit-sent))
for limit != 0 && !isDone(ctx) {
fetchSize := uint32(queryBatchSize)
if limit > 0 {
fetchSize = math.MinUint32(queryBatchSize, uint32(limit))
}
batch, batchSize, err := iter.ReadBatch(i, fetchSize)
if err != nil {
return err
}
sent += batchSize

if limit > 0 {
limit -= int32(batchSize)
}

if len(batch.Streams) == 0 {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/ingester/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func Test_Iterator(t *testing.T) {
return nil
},
),
uint32(2)),
int32(2)),
)
require.Equal(t, 2, len(res.Streams))
// each entry translated into a unique stream
Expand Down