-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: SIGNAL-7060 filter out large messages during termination instead of sending to Kafka #95
Merged
seungjinstord
merged 9 commits into
main
from
SIGNAL-7060-precheck-kafee-message-sizes-before-sending
Sep 19, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad705d2
UPDATE: to filter out large messages during termination flow, to just…
seungjinstord 74f0582
ADD: test to confirm this edge case
seungjinstord afd284e
ADD: test to check for case of a lot of smaller messages
seungjinstord 74ec06f
UPDATE: just times two for the small message count to make a big poin…
seungjinstord a56a651
REMOVE: unused variable
seungjinstord 36c6415
UPDATE: move the message triaging out of terminate_send because it's …
seungjinstord a7bc084
UPDATE: to use :queue.fold to directly create a new queue instead of …
seungjinstord 2b96d34
UPDATE: test grammar fix
seungjinstord 3fb13cd
Update test/kafee/producer/async_worker_test.exs
seungjinstord 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,6 +351,76 @@ defmodule Kafee.Producer.AsyncWorkerTest do | |
assert logs =~ "exception was raised trying to send the remaining messages to Kafka" | ||
assert 11 = logs |> String.split("Unsent Kafka message") |> length() | ||
end | ||
|
||
@tag capture_log: true | ||
test "any leftover messages that are large during shutdown gets logged and will not publish", %{ | ||
Comment on lines
+355
to
+356
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. Nice work on the tests 👍 I hope it wasn't too hard to write an accurate representation here. 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. It was fine, other examples were really helpful :) |
||
pid: pid, | ||
topic: topic, | ||
state: state | ||
} do | ||
[small_message_1, small_message_2] = BrodApi.generate_producer_message_list(topic, 2) | ||
message_fixture = File.read!("test/support/example/large_message.json") | ||
large_message_fixture = String.duplicate(message_fixture, 10) | ||
|
||
# This message will skip being sent to Kafka, and only be logged | ||
large_message_1 = | ||
topic | ||
|> BrodApi.generate_producer_message() | ||
|> Map.put(:value, large_message_fixture) | ||
|> Map.put(:key, "large_msg_1") | ||
|
||
remaining_messages = [small_message_1, large_message_1, small_message_2] | ||
state = %{state | queue: :queue.from_list(remaining_messages), send_timeout: :infinity} | ||
|
||
log = | ||
capture_log(fn -> | ||
assert :ok = AsyncWorker.terminate(:normal, state) | ||
Process.sleep(@wait_timeout) | ||
end) | ||
|
||
remaining_brod_messages = BrodApi.to_kafka_message([small_message_1, small_message_2]) | ||
assert_called_once(:brod.produce(_client_id, ^topic, 0, _key, ^remaining_brod_messages)) | ||
|
||
async_worker_state = pid |> Patch.Listener.target() |> :sys.get_state() | ||
assert 0 == :queue.len(async_worker_state.queue) | ||
|
||
assert log =~ "Message in queue is too large, will not push to Kafka" | ||
assert log =~ "send 2 messages to Kafka before terminate" | ||
refute log =~ "exception was raised trying to send the remaining messages to Kafka" | ||
refute log =~ "Unsent Kafka message" | ||
end | ||
|
||
@tag capture_log: true | ||
test "should handle leftover messages which are each small sized but have a total size exceeding max_request_bytes", | ||
%{ | ||
pid: pid, | ||
topic: topic, | ||
state: %{max_request_bytes: max_request_bytes} = state | ||
} do | ||
[small_message] = BrodApi.generate_producer_message_list(topic, 1) | ||
small_message_unit_size = kafka_message_size_bytes(small_message) | ||
|
||
small_message_total = Kernel.ceil(max_request_bytes / small_message_unit_size) * 2 | ||
remaining_messages = BrodApi.generate_producer_message_list(topic, small_message_total) | ||
|
||
state = %{state | queue: :queue.from_list(remaining_messages), send_timeout: :infinity} | ||
|
||
log = | ||
capture_log(fn -> | ||
assert :ok = AsyncWorker.terminate(:normal, state) | ||
Process.sleep(@wait_timeout) | ||
end) | ||
|
||
# just assert if called; lower level :brod code might split up the messages into more then one call | ||
assert_called(:brod.produce(_client_id, ^topic, 0, _key, _messages)) | ||
|
||
async_worker_state = pid |> Patch.Listener.target() |> :sys.get_state() | ||
assert 0 == :queue.len(async_worker_state.queue) | ||
|
||
assert log =~ "send #{small_message_total} messages to Kafka before terminate" | ||
refute log =~ "exception was raised trying to send the remaining messages to Kafka" | ||
refute log =~ "Unsent Kafka message" | ||
end | ||
end | ||
|
||
describe "build_message_batch/1" do | ||
|
@@ -425,4 +495,10 @@ defmodule Kafee.Producer.AsyncWorkerTest do | |
} | ||
end | ||
end | ||
|
||
defp kafka_message_size_bytes(message) do | ||
message | ||
|> Map.take([:key, :value, :headers]) | ||
|> :erlang.external_size() | ||
end | ||
end |
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.
Small refactoring as this logic is used in two functions