-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into 536-new-match-fields
- Loading branch information
Showing
30 changed files
with
589 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
pipelines: | ||
split_join: | ||
settings: | ||
event_timeout: 1h | ||
capacity: 128 | ||
input: | ||
type: file | ||
offsets_op: reset | ||
maintenance_interval: 1m | ||
actions: | ||
- type: debug | ||
message: input event sample | ||
- type: split | ||
field: data | ||
- type: join | ||
field: message | ||
start: '/^start/' | ||
continue: '/^continue/' | ||
- type: debug | ||
message: output event sample | ||
output: | ||
type: kafka |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package split_join | ||
|
||
import ( | ||
"github.com/Shopify/sarama" | ||
) | ||
|
||
type handlerFunc func(message *sarama.ConsumerMessage) | ||
|
||
func (h handlerFunc) Setup(_ sarama.ConsumerGroupSession) error { return nil } | ||
|
||
func (h handlerFunc) Cleanup(_ sarama.ConsumerGroupSession) error { return nil } | ||
|
||
func (h handlerFunc) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error { | ||
for msg := range claim.Messages() { | ||
h(msg) | ||
session.MarkMessage(msg, "") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package split_join | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Shopify/sarama" | ||
"github.com/ozontech/file.d/cfg" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
brokerHost = "localhost:9092" | ||
group = "file_d_test_split_join_client" | ||
|
||
arrayLen = 4 | ||
sample = `{ "data": [ { "first": "1" }, { "message": "start " }, { "message": "continue" }, { "second": "2" }, { "third": "3" } ] }` | ||
|
||
messages = 10 | ||
) | ||
|
||
type Config struct { | ||
inputDir string | ||
consumer sarama.ConsumerGroup | ||
topic string | ||
} | ||
|
||
func (c *Config) Configure(t *testing.T, conf *cfg.Config, pipelineName string) { | ||
r := require.New(t) | ||
|
||
c.inputDir = t.TempDir() | ||
offsetsDir := t.TempDir() | ||
c.topic = fmt.Sprintf("file_d_test_split_join_%d", time.Now().UnixNano()) | ||
t.Logf("generated topic: %s", c.topic) | ||
|
||
input := conf.Pipelines[pipelineName].Raw.Get("input") | ||
input.Set("watching_dir", c.inputDir) | ||
input.Set("filename_pattern", "input.log") | ||
input.Set("offsets_file", filepath.Join(offsetsDir, "offsets.yaml")) | ||
|
||
output := conf.Pipelines[pipelineName].Raw.Get("output") | ||
output.Set("brokers", []string{brokerHost}) | ||
output.Set("default_topic", c.topic) | ||
|
||
addrs := []string{brokerHost} | ||
config := sarama.NewConfig() | ||
config.Consumer.Offsets.Initial = sarama.OffsetOldest | ||
|
||
admin, err := sarama.NewClusterAdmin(addrs, config) | ||
r.NoError(err) | ||
r.NoError(admin.CreateTopic(c.topic, &sarama.TopicDetail{ | ||
NumPartitions: 1, | ||
ReplicationFactor: 1, | ||
}, false)) | ||
|
||
c.consumer, err = sarama.NewConsumerGroup(addrs, group, config) | ||
r.NoError(err) | ||
} | ||
|
||
func (c *Config) Send(t *testing.T) { | ||
file, err := os.Create(path.Join(c.inputDir, "input.log")) | ||
require.NoError(t, err) | ||
defer func(file *os.File) { | ||
_ = file.Close() | ||
}(file) | ||
|
||
for i := 0; i < messages; i++ { | ||
_, err = file.WriteString(sample + "\n") | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
func (c *Config) Validate(t *testing.T) { | ||
r := require.New(t) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
expectedEventsCount := messages * arrayLen | ||
|
||
strBuilder := strings.Builder{} | ||
gotEvents := 0 | ||
done := make(chan struct{}) | ||
|
||
go func() { | ||
r.NoError(c.consumer.Consume(ctx, []string{c.topic}, handlerFunc(func(msg *sarama.ConsumerMessage) { | ||
strBuilder.Write(msg.Value) | ||
strBuilder.WriteString("\n") | ||
gotEvents++ | ||
if gotEvents == expectedEventsCount { | ||
close(done) | ||
} | ||
}))) | ||
}() | ||
|
||
select { | ||
case <-done: | ||
case <-ctx.Done(): | ||
r.Failf("test timed out", "got: %v, expected: %v, consumed: %s", gotEvents, expectedEventsCount, strBuilder.String()) | ||
} | ||
|
||
got := strBuilder.String() | ||
|
||
expected := strings.Repeat(`{"first":"1"} | ||
{"message":"start continue"} | ||
{"second":"2"} | ||
{"third":"3"} | ||
`, | ||
messages) | ||
|
||
r.Equal(len(expected), len(got)) | ||
r.Equal(expected, got) | ||
r.Equal(expectedEventsCount, gotEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.