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

feat: nil value allowed in message / topic optional #83

Closed
wants to merge 2 commits into from

Conversation

gemantzu
Copy link
Contributor

Checklist

Problem

  • We have stumbled upon cases where a nil value is causing Kafee to break.
  • We cannot use Kafee at the moment, as we have some generic producers, while the topic is supposed to be always required when using... use.

Details

@gemantzu gemantzu requested a review from a team as a code owner January 31, 2024 13:29
@gemantzu gemantzu force-pushed the fixes-non-binary-value branch from fa63199 to 0ef9ddd Compare January 31, 2024 13:30
@gemantzu
Copy link
Contributor Author

I am going to fix the test. I would also like to pick your ears on making the partition_fun non required as well, since it has a default.

@@ -252,7 +252,7 @@ defmodule Kafee.Producer.Message do

def validate!(%Message{} = message) do
for {key, value} <- message.headers do
if not (is_binary(key) and is_binary(value)) do
if not (is_binary(key) and allowed_value?(value)) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not immediately apparent why this is desirable behavior. Rather than patching Kafee to allow nil header values, can you help us better understand why we're sending those values in the first place and are unable to fix the originating code instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not always able to have an actual value on a key. We stumbled upon this a few times on development, therefore the patch. We probably need to think this even more with additional changes, ie maybe we need to tolerate atom keys in general, or we can call to_string in the value (with or without inspect) for more complex values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we eject nil values on the service side?

test "raises on non string header value", %{topic: topic} do
test "does not raise on non string header value", %{topic: topic} do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're flipping this test, it makes me think this is more of a breaking change than a chore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's true. I am going to change the title.

@gemantzu gemantzu changed the title chore: nil value allowed in message / topic optional feat: nil value allowed in message / topic optional Jan 31, 2024
@@ -372,4 +372,8 @@ defmodule Kafee.Producer.Message do
#{inspected_message}
"""
end

defp allowed_value?(nil), do: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil values are technically invalid by the low level kafka protocol library used by brod and most other Erlang libraries:

https://github.com/kafka4beam/kafka_protocol/blob/5d55105d224374b39d10357e0b1e56d7b3a72326/src/kpro.erl#L164

We will need to test this a ton to ensure it actually works. I have a hunch that brod to_string encodes nil which just results in "".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't suggest allowing nil values since I can't see how they would be meaningful in metadata. We could however, delete any keys with nil values instead of raising an exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to look at the code @gemantzu is referring that's having trouble with this. This feels like something that should be handled service side 🤔

@@ -70,7 +70,7 @@ defmodule Kafee.Producer do
doc: """
The Kafka topic to send messages to.
""",
required: true,
required: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fine for most things, though I'd probably add some additional code to error on the async adapter as it will cause a ton of processes to spawn for every new topic sent to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, we should at some point explore the possibility to make this indeed the default, as it would enforce us to have one producer (and even better, one consumer) per topic instead of the multi use ones we have at the moment, but if we want to adapt now version 3.0, this is making it impossible unless we implement those changes.

@zorbash
Copy link
Contributor

zorbash commented Feb 1, 2024

Slightly unrelated to this PR but to avoid running into message header validation exceptions, we can either make the typespecs of the publish function in WarehouseEvents.KafeeProducer (and the Parcel service one which is almost identical) more strict %{String.t => String.t} instead of the current map() or we can automatically stringify keys.

@gemantzu
Copy link
Contributor Author

gemantzu commented Feb 6, 2024

Ok. Based on the discussions, I am going to remove the PR from here and push a PR on the two services for the typespecs so that dialyzer can catch it.

@gemantzu gemantzu closed this Feb 6, 2024
@gemantzu gemantzu reopened this Feb 6, 2024
@gemantzu gemantzu closed this Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants