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

Fix errors caught by Dialyzer #141

Merged
merged 2 commits into from
Apr 29, 2024
Merged

Fix errors caught by Dialyzer #141

merged 2 commits into from
Apr 29, 2024

Conversation

Stratus3D
Copy link
Contributor

It doesn't look like Dialyzer is getting run for this project.

Below are the fixes for the errors reported by Dialyzer:

lib/broadway_kafka/brod_client.ex:83:guard_fail
The guard clause:

when _ :: Keyword.t() === nil

can never succeed.

________________________________________________________________________________
lib/broadway_kafka/brod_client.ex:114:guard_fail
The guard clause:

when _ :: pid() === nil

can never succeed.

________________________________________________________________________________
lib/broadway_kafka/kafka_client.ex:54:unknown_type
Unknown type: :brod.group_coordinator/0.
________________________________________________________________________________
done (warnings were emitted)
Halting VM with exit status 2

@@ -80,7 +80,7 @@ defmodule BroadwayKafka.BrodClient do
offset_reset_policy: offset_reset_policy,
begin_offset: begin_offset,
group_config: [{:offset_commit_policy, @offset_commit_policy} | group_config],
fetch_config: Map.new(fetch_config || []),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fetch_config is always going to be a list that passed validation in the validate_fetch_config/1 function, so the || [] will never be used here.

@@ -111,7 +111,7 @@ defmodule BroadwayKafka.BrodClient do
def ack(group_coordinator, generation_id, topic, partition, offset, config) do
:brod_group_coordinator.ack(group_coordinator, generation_id, topic, partition, offset)

if group_coordinator && config.offset_commit_on_ack do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to Dialyzer group_coordinator will always be set. It's a pid so even if the pid dies it won't be a falsey value.

Copy link
Member

Choose a reason for hiding this comment

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

I believe it is getting this information from the callback but the callback is wrong. According to producer.ex the group_coordinator can definitely be nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both :brod_group_coordinator.ack/5 and :brod_group_coordinator.commit_offsets/2 both expect group_coordinator to be a pid, not nil.

I think Dialyzer is complaining about this nil check because :brod_group_coordinator.ack/5 is invoked before it, so Dialyzer assumes that if we get to this line it MUST NOT be nil (and maybe it won't ever be, if :brod_group_coordinator.ack/5 always crashes when nil is passed).

We can solve this by having both calls inside a if group_coordinator do block. For consistency we should also do the same thing for the update_topics/2 function in this module.

It feels a little strange return ok when group_coordinator coordinator is nil however, but this is the only return type allowed by the behaviour:

  @impl true
  def ack(group_coordinator, generation_id, topic, partition, offset, config) do
    if group_coordinator do
      :brod_group_coordinator.ack(group_coordinator, generation_id, topic, partition, offset)

      if config.offset_commit_on_ack do
        :brod_group_coordinator.commit_offsets(group_coordinator, [{{topic, partition}, offset}])
      end
    end

    :ok
  end

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think that would be better. Although, if this is the case, it seems group_coordinator cannot be nil in this particular branch indeed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR updated. I have made the changes I described above.

@@ -51,7 +52,7 @@ defmodule BroadwayKafka.KafkaClient do
) ::
offset :: integer | no_return()

@callback update_topics(:brod.group_coordinator(), [:brod.topic()]) :: :ok
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't see this type defined in brod so I defined one in this module.

@callback ack(
group_coordinator :: pid,
group_coordinator :: brod_group_coordinator,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactor for consistency.

@josevalim josevalim merged commit 32199fe into dashbitco:main Apr 29, 2024
4 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@Stratus3D
Copy link
Contributor Author

Thanks @josevalim !

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.

2 participants