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

chore: optimize single-record blocking write #297

Merged
merged 1 commit into from
Jan 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions api/writeAPIBlocking.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ import (
// }
type WriteAPIBlocking interface {
// WriteRecord writes line protocol record(s) into bucket.
// WriteRecord writes without implicit batching. Batch is created from given number of records
// WriteRecord writes without implicit batching. Batch is created from given number of records.
// Individual arguments can also be batches (multiple records separated by newline).
// Non-blocking alternative is available in the WriteAPI interface
WriteRecord(ctx context.Context, line ...string) error
// WritePoint data point into bucket.
Expand Down Expand Up @@ -80,18 +81,25 @@ func (w *writeAPIBlocking) write(ctx context.Context, line string) error {
}

func (w *writeAPIBlocking) WriteRecord(ctx context.Context, line ...string) error {
if len(line) > 0 {
var sb strings.Builder
for _, line := range line {
b := []byte(line)
b = append(b, 0xa)
if _, err := sb.Write(b); err != nil {
return err
}
if len(line) == 0 {
return nil
}
if len(line) == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty lines are ignored by InfluxDB, adding extra new line is not required, it could simply return w.write(ctx, line[0])

https://github.com/influxdata/influxdb/blob/11c00813f106062829759a97673c7c945d5859ca/models/points.go#L369

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing implementation assumes there are no terminating newlines on the passed records; it's necessary to add one when missing to keep the existing behavior. Also, not adding a newline causes the tests to fail, and keeps this consistent with what's produced by line-protocol, see influxdata/line-protocol#52.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: That existing behavior is verified by the tests, which is why it isn't the way you suggested....

ln := line[0]
if ln[len(ln)-1] != '\n' {
ln += "\n"
}
return w.write(ctx, sb.String())
return w.write(ctx, ln)
}
return nil
var sb strings.Builder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a nit: this could be also optimized to avoid useless allocations of temporary buffers, it could Grow in advance to len(line)+sum_of_line_lengths

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strings.Join(lines, "\n") would probably be best, except for the need to add a terminal newline. Lifting []bytes out of the existing loop and expanding it at need based on line length could also work. I didn't want to make that large a change, but if it's important enough I'll take a stab at it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes strings.Join is already an optimized code and it would be the best option, though it would probably break the tests. The original goal herein (doc + simple optimization for 1 line) is already the necessary improvement.

The level and depth of optimizations is up to you ... it was just a nit from me. @vlastahajek is the approver herein.

for _, line := range line {
b := []byte(line)
b = append(b, 0xa)
if _, err := sb.Write(b); err != nil {
return err
}
}
return w.write(ctx, sb.String())
}

func (w *writeAPIBlocking) WritePoint(ctx context.Context, point ...*write.Point) error {
Expand Down
15 changes: 15 additions & 0 deletions api/writeAPIBlocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"net"
"net/http"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -57,6 +58,20 @@ func TestWriteRecord(t *testing.T) {
require.Equal(t, "invalid: data", err.Error())
}

func TestWriteRecordBatch(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
lines := test.GenRecords(10)
batch := strings.Join(lines, "\n")
err := writeAPI.WriteRecord(context.Background(), batch)
require.Nil(t, err)
require.Len(t, service.Lines(), 10)
for i, l := range lines {
assert.Equal(t, l, service.Lines()[i])
}
service.Close()
}

func TestWriteParallel(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
Expand Down