-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from bbalser/ack_hack
Custom Ack hack - when configured elsa will ack messages directly and not go through the :brod_group_coordinator
- Loading branch information
Showing
6 changed files
with
388 additions
and
15 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
defmodule Elsa.Group.DirectAcknowledger do | ||
use GenServer | ||
require Logger | ||
|
||
@timeout 5_000 | ||
|
||
def ack(server, member_id, topic, partition, generation_id, offset) do | ||
GenServer.call(server, {:ack, member_id, topic, partition, generation_id, offset}) | ||
end | ||
|
||
def start_link(opts) do | ||
name = Keyword.fetch!(opts, :name) | ||
GenServer.start_link(__MODULE__, opts, name: name) | ||
end | ||
|
||
def init(opts) do | ||
state = %{ | ||
client: Keyword.fetch!(opts, :client), | ||
group: Keyword.fetch!(opts, :group) | ||
} | ||
|
||
{:ok, state, {:continue, :connect}} | ||
end | ||
|
||
def handle_continue(:connect, state) do | ||
with {:ok, {endpoint, conn_config}} <- :brod_client.get_group_coordinator(state.client, state.group), | ||
{:ok, connection} <- :kpro.connect(endpoint, conn_config) do | ||
Logger.debug(fn -> "#{__MODULE__}: Coordinator available for group #{state.group} on client #{state.client}" end) | ||
{:noreply, Map.put(state, :connection, connection)} | ||
else | ||
{:error, reason} when is_list(reason) -> | ||
case Keyword.get(reason, :error_code) do | ||
:coordinator_not_available -> | ||
Process.sleep(1_000) | ||
{:noreply, state, {:continue, :connect}} | ||
|
||
_ -> | ||
{:stop, reason, state} | ||
end | ||
|
||
{:error, reason} -> | ||
{:stop, reason, state} | ||
end | ||
end | ||
|
||
def handle_call({:ack, member_id, topic, partition, generation_id, offset}, _from, state) do | ||
request = make_request_body(state, member_id, topic, partition, generation_id, offset) | ||
|
||
with {:ok, response} <- :brod_utils.request_sync(state.connection, request, @timeout), | ||
:ok <- parse_response(response) do | ||
{:reply, :ok, state} | ||
else | ||
{:error, reason} -> {:stop, reason, state} | ||
end | ||
end | ||
|
||
defp make_request_body(state, member_id, topic, partition, generation_id, offset) do | ||
partitions = [ | ||
%{ | ||
partition: partition, | ||
offset: offset + 1, | ||
metadata: "+1/#{:io_lib.format("~p/~p", [node(), self()])}" | ||
} | ||
] | ||
|
||
topics = [ | ||
%{ | ||
topic: topic, | ||
partitions: partitions | ||
} | ||
] | ||
|
||
request_body = %{ | ||
group_id: state.group, | ||
generation_id: generation_id, | ||
member_id: member_id, | ||
retention_time: -1, | ||
topics: topics | ||
} | ||
|
||
:brod_kafka_request.offset_commit(state.connection, request_body) | ||
end | ||
|
||
defp parse_response(response) do | ||
case parse_offset_commit_response(response) do | ||
[] -> :ok | ||
errors -> {:error, errors} | ||
end | ||
end | ||
|
||
defp parse_offset_commit_response(response) do | ||
response.responses | ||
|> Enum.map(&parse_partition_responses/1) | ||
|> List.flatten() | ||
end | ||
|
||
defp parse_partition_responses(%{topic: topic, partition_responses: responses}) do | ||
responses | ||
|> Enum.filter(fn %{error_code: code} -> code != :no_error end) | ||
|> Enum.map(fn %{error_code: code, partition: partition} -> %{topic: topic, error: code, partition: partition} end) | ||
end | ||
end |
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,55 @@ | ||
defmodule Elsa.Group.DirectAcknowledgerTest do | ||
use ExUnit.Case | ||
use Divo | ||
import AsyncAssertion | ||
|
||
defmodule MessageHandler do | ||
use Elsa.Consumer.MessageHandler | ||
|
||
def handle_messages(_messages) do | ||
:ack | ||
end | ||
end | ||
|
||
@endpoints Application.get_env(:elsa, :brokers) | ||
@group "group-1a" | ||
@topic "topic-1a" | ||
|
||
test "direct acknowledger ack over privately managed connection" do | ||
:ok = Elsa.create_topic(@endpoints, @topic) | ||
|
||
{:ok, _elsa_sup_pid} = | ||
Elsa.Group.Supervisor.start_link( | ||
name: :test_direct_acker, | ||
endpoints: @endpoints, | ||
group: @group, | ||
topics: [@topic], | ||
handler: MessageHandler, | ||
direct_ack: true, | ||
config: [ | ||
begin_offset: :earliest | ||
] | ||
) | ||
|
||
Elsa.produce(@endpoints, @topic, {"key1", "value1"}, partition: 0) | ||
|
||
Process.sleep(8_000) | ||
|
||
assert_async(fn -> | ||
assert 1 == get_committed_offsets(:test_direct_acker, @group, @topic, 0) | ||
end) | ||
end | ||
|
||
defp get_committed_offsets(client, group, topic, partition) do | ||
{:ok, responses} = :brod.fetch_committed_offsets(client, group) | ||
|
||
case Enum.find(responses, fn %{topic: t} -> topic == t end) do | ||
nil -> | ||
:undefined | ||
|
||
topic -> | ||
partition = Enum.find(topic.partition_responses, fn %{partition: p} -> partition == p end) | ||
partition.offset | ||
end | ||
end | ||
end |
Oops, something went wrong.