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

perf(transform): Improve AggregateToArray Throughput #150

Merged
merged 2 commits into from
Mar 21, 2024
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
3 changes: 1 addition & 2 deletions build/config/substation.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,7 @@
condition=$.cnd.meta.negate({ inspector: $.cnd.fmt.json() }),
transform=$.tf.util.drop(),
),
$.tf.agg.to.arr(),
$.tf.arr.join({ separator: '\n' }),
$.tf.agg.to.string({ separator: '\n' }),
$.tf.str.append({ suffix: '\n' }),
],
},
Expand Down
15 changes: 3 additions & 12 deletions transform/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package transform
import (
"bytes"
"fmt"
"slices"

iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
"github.com/brexhq/substation/message"
)

type aggregateArrayConfig struct {
Expand All @@ -18,17 +18,8 @@ func (c *aggregateArrayConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}

func aggToArray(data [][]byte) ([]byte, error) {
msg := message.New()

for _, d := range data {
if err := msg.SetValue("array.-1", d); err != nil {
return nil, err
}
}

b := msg.GetValue("array")
return b.Bytes(), nil
func aggToArray(data [][]byte) []byte {
return slices.Concat([]byte("["), bytes.Join(data, []byte(",")), []byte("]"))
}

type aggregateStrConfig struct {
Expand Down
19 changes: 6 additions & 13 deletions transform/aggregate_to_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,19 @@ func (tf *aggregateToArray) Transform(ctx context.Context, msg *message.Message)
tf.mu.Lock()
defer tf.mu.Unlock()

//nolint: nestif // ignore nesting complexity
if msg.IsControl() {
var output []*message.Message

for _, items := range tf.agg.GetAll() {
agg, err := aggToArray(items.Get())
if err != nil {
return nil, fmt.Errorf("transform: aggregate_to_array: %v", err)
}
array := aggToArray(items.Get())

outMsg := message.New()
if tf.hasObjTrg {
if err := outMsg.SetValue(tf.conf.Object.TargetKey, agg); err != nil {
if err := outMsg.SetValue(tf.conf.Object.TargetKey, array); err != nil {
return nil, fmt.Errorf("transform: aggregate_to_array: %v", err)
}
} else {
outMsg.SetData(agg)
outMsg.SetData(array)
}

output = append(output, outMsg)
Expand All @@ -80,18 +76,15 @@ func (tf *aggregateToArray) Transform(ctx context.Context, msg *message.Message)
return nil, nil
}

agg, err := aggToArray(tf.agg.Get(key))
if err != nil {
return nil, fmt.Errorf("transform: aggregate_to_array: %v", err)
}
array := aggToArray(tf.agg.Get(key))

outMsg := message.New()
if tf.hasObjTrg {
if err := outMsg.SetValue(tf.conf.Object.TargetKey, agg); err != nil {
if err := outMsg.SetValue(tf.conf.Object.TargetKey, array); err != nil {
return nil, fmt.Errorf("transform: aggregate_to_array: %v", err)
}
} else {
outMsg.SetData(agg)
outMsg.SetData(array)
}

// If data cannot be added after reset, then the batch is misconfgured.
Expand Down
Loading