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

Kafka output drop invalid messages #2737

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ https://github.com/elastic/beats/compare/v5.0.0-rc1...master[Check the HEAD diff

*Affecting all Beats*

- Fix kafka output re-trying batches with too large events. {issue}2735[2735]

*Metricbeat*

*Packetbeat*
Expand Down
47 changes: 34 additions & 13 deletions libbeat/outputs/kafka/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type client struct {
}

type msgRef struct {
count int32
batch []outputs.Data
cb func([]outputs.Data, error)
count int32
total int
failed []outputs.Data
cb func([]outputs.Data, error)

err error
}
Expand Down Expand Up @@ -104,9 +105,10 @@ func (c *client) AsyncPublishEvents(
debugf("publish events")

ref := &msgRef{
count: int32(len(data)),
batch: data,
cb: cb,
count: int32(len(data)),
total: len(data),
failed: nil,
cb: cb,
}

ch := c.producer.Input()
Expand Down Expand Up @@ -136,7 +138,7 @@ func (c *client) getEventMessage(data *outputs.Data) (*message, error) {
return msg, nil
}

msg.event = event
msg.data = *data

topic, err := c.topic.Select(event)
if err != nil {
Expand Down Expand Up @@ -188,16 +190,28 @@ func (c *client) errorWorker(ch <-chan *sarama.ProducerError) {

for errMsg := range ch {
msg := errMsg.Msg.Metadata.(*message)
msg.ref.fail(errMsg.Err)
msg.ref.fail(msg, errMsg.Err)
}
}

func (r *msgRef) done() {
r.dec()
}

func (r *msgRef) fail(err error) {
r.err = err
func (r *msgRef) fail(msg *message, err error) {
switch err {
case sarama.ErrInvalidMessage:
logp.Err("Kafka (topic=%v): dropping invalid message", msg.topic)

case sarama.ErrMessageSizeTooLarge, sarama.ErrInvalidMessageSize:
logp.Err("Kafka (topic=%v): dropping too large message of size %v.",
msg.topic,
len(msg.key)+len(msg.value))

default:
r.failed = append(r.failed, msg.data)
r.err = err
}
r.dec()
}

Expand All @@ -211,11 +225,18 @@ func (r *msgRef) dec() {

err := r.err
if err != nil {
eventsNotAcked.Add(int64(len(r.batch)))
failed := len(r.failed)
success := r.total - failed

eventsNotAcked.Add(int64(failed))
if success > 0 {
ackedEvents.Add(int64(success))
}

debugf("Kafka publish failed with: %v", err)
r.cb(r.batch, err)
r.cb(r.failed, err)
} else {
ackedEvents.Add(int64(len(r.batch)))
ackedEvents.Add(int64(r.total))
r.cb(nil, nil)
}
}
3 changes: 1 addition & 2 deletions libbeat/outputs/kafka/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

"github.com/Shopify/sarama"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs"
)

Expand All @@ -20,7 +19,7 @@ type message struct {
hash uint32
partition int32

event common.MapStr
data outputs.Data
}

var kafkaMessageKey interface{} = int(0)
Expand Down
2 changes: 1 addition & 1 deletion libbeat/outputs/kafka/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func makeFieldsHashPartitioner(fields []string, dropFail bool) partitioner {

var err error
for _, field := range fields {
err = hashFieldValue(hasher, msg.event, field)
err = hashFieldValue(hasher, msg.data.Event, field)
if err != nil {
break
}
Expand Down
5 changes: 3 additions & 2 deletions libbeat/outputs/kafka/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/Shopify/sarama"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/outputs"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -219,7 +220,7 @@ func partTestSimple(N int, makeKey bool) partTestScenario {
}

msg := &message{partition: -1}
msg.event = event
msg.data = outputs.Data{event, nil}
msg.topic = "test"
if makeKey {
msg.key = randASCIIBytes(10)
Expand Down Expand Up @@ -272,7 +273,7 @@ func partTestHashInvariant(N int) partTestScenario {
}

msg := &message{partition: -1}
msg.event = event
msg.data = outputs.Data{event, nil}
msg.topic = "test"
msg.key = randASCIIBytes(10)
msg.value = jsonEvent
Expand Down