-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Fix Kafka output panic at startup #41824
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b987165
Make Kafka output satisfy NetworkClient interface.
cmacknz e6ffaaf
Make Redis output satisfy network client.
cmacknz f6b2f99
Add initial regression integration test.
cmacknz d9aa8c4
Add an integration test to ensure connectivity.
cmacknz 3d29537
Fix build error in old integration test.
cmacknz d92fc5c
Fix redis lint error.
cmacknz 30fb117
Fix typo in comment.
cmacknz 6a2ffbe
Fix another typo.
cmacknz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,89 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
//go:build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Shopify/sarama" | ||
) | ||
|
||
var ( | ||
// https://github.com/elastic/sarama/blob/c7eabfcee7e5bcd7d0071f0ece4d6bec8c33928a/config_test.go#L14-L17 | ||
// The version of MockBroker used when this test was written only supports the lowest protocol version by default. | ||
// Version incompatibilities will result in message decoding errors between the mock and the beat. | ||
kafkaVersion = sarama.MinVersion | ||
Comment on lines
+31
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discovering the need for this cost me 3 hours 🙃 |
||
kafkaTopic = "test_topic" | ||
kafkaCfg = ` | ||
mockbeat: | ||
logging: | ||
level: debug | ||
selectors: | ||
- publisher_pipeline_output | ||
- kafka | ||
queue.mem: | ||
events: 4096 | ||
flush.timeout: 0s | ||
output.kafka: | ||
topic: %s | ||
version: %s | ||
hosts: | ||
- %s | ||
backoff: | ||
init: 0.1s | ||
max: 0.2s | ||
` | ||
) | ||
|
||
// TestKafkaOutputCanConnectAndPublish ensures the beat Kafka output can successfully produce messages to Kafka. | ||
// Regression test for https://github.com/elastic/beats/issues/41823 where the Kafka output would | ||
// panic on the first Publish because it's Connect method was no longer called. | ||
func TestKafkaOutputCanConnectAndPublish(t *testing.T) { | ||
// Create a Mock Kafka broker that will listen on localhost on a random unallocated port. | ||
// The reference configuration was taken from https://github.com/elastic/sarama/blob/c7eabfcee7e5bcd7d0071f0ece4d6bec8c33928a/async_producer_test.go#L141. | ||
leader := sarama.NewMockBroker(t, 1) | ||
defer leader.Close() | ||
|
||
// The mock broker must respond to a single metadata request. | ||
metadataResponse := new(sarama.MetadataResponse) | ||
metadataResponse.AddBroker(leader.Addr(), leader.BrokerID()) | ||
metadataResponse.AddTopicPartition(kafkaTopic, 0, leader.BrokerID(), nil, nil, nil, sarama.ErrNoError) | ||
leader.Returns(metadataResponse) | ||
|
||
// The mock broker must return a single produce response. If no produce request is received, the test will fail. | ||
// This guarantees that mockbeat successfully produced a message to Kafka and connectivity is established. | ||
prodSuccess := new(sarama.ProduceResponse) | ||
prodSuccess.AddTopicPartition(kafkaTopic, 0, sarama.ErrNoError) | ||
leader.Returns(prodSuccess) | ||
|
||
// Start mockbeat with the appropriate configuration. | ||
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test") | ||
mockbeat.WriteConfigFile(fmt.Sprintf(kafkaCfg, kafkaTopic, kafkaVersion, leader.Addr())) | ||
mockbeat.Start() | ||
|
||
// Wait for mockbeat to log that it successfully published a batch to Kafka. | ||
// This ensures that mockbeat received the expected produce response configured above. | ||
mockbeat.WaitForLogs( | ||
`finished kafka batch`, | ||
10*time.Second, | ||
"did not find finished batch log") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you revert this change and run the integration test, it will fail with the panic in the original issue.