-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
691dc96
commit aba784d
Showing
19 changed files
with
236 additions
and
27 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,5 @@ | ||
# Producing | ||
|
||
## Message De-duplication | ||
|
||
## Filter Value |
Empty file.
Empty file.
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,9 @@ | ||
# Overview | ||
|
||
## Streams | ||
|
||
## Single Active Consumer | ||
|
||
### Upgrade | ||
|
||
## Super Streams |
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
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,64 @@ | ||
defmodule RabbitMQStream.SuperConsumer.Manager do | ||
defmodule PartitionConsumer do | ||
use RabbitMQStream.Consumer, | ||
initial_offset: :last | ||
|
||
def handle_update(_, true) do | ||
{:ok, :last} | ||
end | ||
end | ||
|
||
alias RabbitMQStream.SuperConsumer | ||
|
||
use GenServer | ||
|
||
def start_link(opts) do | ||
GenServer.start_link(__MODULE__, opts) | ||
end | ||
|
||
@impl true | ||
def init(opts \\ []) do | ||
state = struct(SuperConsumer, opts) | ||
|
||
{:ok, state, {:continue, :start}} | ||
end | ||
|
||
@impl true | ||
def handle_continue(:start, %SuperConsumer{} = state) do | ||
# If stream exists, fetch its paritions information | ||
{:ok, data} = | ||
state.partitions | ||
|> Enum.map(&"#{state.super_stream}-#{&1}") | ||
|> state.connection.query_metadata() | ||
|
||
# We create all the missing streams | ||
for %{code: :stream_does_not_exist, name: name} <- data.streams do | ||
:ok = state.connection.create_stream(name) | ||
end | ||
|
||
dbg(state.partitions) | ||
|
||
for partition <- state.partitions do | ||
dbg(partition) | ||
# We want to start each child, but don't really care about its state | ||
{:ok, _pid} = | ||
DynamicSupervisor.start_child( | ||
state.dynamic_supervisor, | ||
{ | ||
PartitionConsumer, | ||
Keyword.merge(state.consumer_opts, | ||
name: partition, | ||
connection: state.connection, | ||
stream_name: "#{state.super_stream}-#{partition}", | ||
properties: [ | ||
single_active_consumer: true, | ||
super_stream: state.super_stream | ||
] | ||
) | ||
} | ||
) | ||
end | ||
|
||
{:noreply, state} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule RabbitMQStream.SuperConsumer do | ||
defmacro __using__(opts) do | ||
quote do | ||
@opts unquote(opts) | ||
@behaviour RabbitMQStream.SuperConsumer | ||
|
||
use Supervisor | ||
|
||
def start_link(opts) do | ||
Supervisor.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
@impl true | ||
def init(opts) do | ||
children = [ | ||
{DynamicSupervisor, strategy: :one_for_one, name: __MODULE__.DynamicSupervisor}, | ||
{RabbitMQStream.SuperConsumer.Manager, | ||
opts ++ [name: __MODULE__.Manager, dynamic_supervisor: __MODULE__.DynamicSupervisor]} | ||
] | ||
|
||
# We use `one_for_all` because if the DynamicSupervisor shuts down for some reason, we must be able to | ||
# re-build all the children from the Manager | ||
Supervisor.init(children, strategy: :one_for_all) | ||
end | ||
end | ||
end | ||
|
||
@optional_callbacks handle_chunk: 1, handle_chunk: 2 | ||
@callback handle_chunk(chunk :: RabbitMQStream.OsirisChunk.t()) :: term() | ||
@callback handle_chunk(chunk :: RabbitMQStream.OsirisChunk.t(), state :: RabbitMQStream.Consumer.t()) :: term() | ||
|
||
defstruct [ | ||
:super_stream, | ||
:partitions, | ||
:connection, | ||
:consumer_opts, | ||
:dynamic_supervisor | ||
] | ||
|
||
@type t :: %__MODULE__{ | ||
super_stream: String.t(), | ||
partitions: [String.t()], | ||
connection: module(), | ||
dynamic_supervisor: module(), | ||
consumer_opts: [RabbitMQStream.Consumer.consumer_option()] | nil | ||
} | ||
|
||
@type super_consumer_option :: | ||
{:super_stream, String.t()} | ||
| {:partitions, [String.t()]} | ||
| {:connection, module()} | ||
| {:consumer_opts, [RabbitMQStream.Consumer.consumer_option()]} | ||
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
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
Oops, something went wrong.