@@ -5,7 +5,15 @@ defmodule KafkaBatcher.Accumulator do
55 See details how it works in KafkaBatcher.Accumulator.State module
66 """
77
8- alias KafkaBatcher . { Accumulator.State , MessageObject , TempStorage }
8+ alias KafkaBatcher . {
9+ Accumulator ,
10+ Accumulator.State ,
11+ MessageObject ,
12+ PipelineUnit ,
13+ Producers ,
14+ TempStorage
15+ }
16+
917 alias KafkaBatcher.Behaviours.Collector , as: CollectorBehaviour
1018
1119 @ error_notifier Application . compile_env ( :kafka_batcher , :error_notifier , KafkaBatcher.DefaultErrorNotifier )
@@ -14,25 +22,34 @@ defmodule KafkaBatcher.Accumulator do
1422 use GenServer
1523 require Logger
1624
17- def start_link ( args ) do
18- GenServer . start_link ( __MODULE__ , args , name: reg_name ( args ) )
25+ @ spec start_link ( PipelineUnit . t ( ) ) :: GenServer . on_start ( )
26+ def start_link ( % PipelineUnit { } = pipeline_unit ) do
27+ GenServer . start_link (
28+ __MODULE__ ,
29+ pipeline_unit ,
30+ name: reg_name ( pipeline_unit )
31+ )
1932 end
2033
2134 @ doc "Returns a specification to start this module under a supervisor"
22- def child_spec ( args ) do
23- { accumulator_mod , args } = Keyword . pop ( args , :accumulator_mod , __MODULE__ )
24-
35+ @ spec child_spec ( PipelineUnit . t ( ) ) :: Supervisor . child_spec ( )
36+ def child_spec ( % PipelineUnit { } = pipeline_unit ) do
2537 % {
26- id: reg_name ( args ) ,
27- start: { accumulator_mod , :start_link , [ args ] }
38+ id: reg_name ( pipeline_unit ) ,
39+ start: {
40+ PipelineUnit . get_accumulator_mod ( pipeline_unit ) ,
41+ :start_link ,
42+ [ pipeline_unit ]
43+ }
2844 }
2945 end
3046
3147 @ doc """
3248 Finds appropriate Accumulator process by topic & partition and dispatches `event` to it
3349 """
34- def add_event ( % MessageObject { } = event , topic_name , partition \\ nil ) do
35- GenServer . call ( reg_name ( topic_name: topic_name , partition: partition ) , { :add_event , event } )
50+ @ spec add_event ( MessageObject . t ( ) , PipelineUnit . t ( ) ) :: :ok | { :error , term ( ) }
51+ def add_event ( % MessageObject { } = event , % PipelineUnit { } = pipeline_unit ) do
52+ GenServer . call ( reg_name ( pipeline_unit ) , { :add_event , event } )
3653 catch
3754 _ , _reason ->
3855 Logger . warning ( "KafkaBatcher: Couldn't get through to accumulator" )
@@ -43,15 +60,17 @@ defmodule KafkaBatcher.Accumulator do
4360 ## Callbacks
4461 ##
4562 @ impl GenServer
46- def init ( args ) do
63+ def init ( % PipelineUnit { } = pipeline_unit ) do
4764 Process . flag ( :trap_exit , true )
48- state = build_state ( args )
65+
66+ topic_name = PipelineUnit . get_topic_name ( pipeline_unit )
67+ partition = PipelineUnit . get_partition ( pipeline_unit )
4968
5069 Logger . debug ( """
51- KafkaBatcher: Accumulator process started: topic #{ state . topic_name } partition #{ state . partition } pid #{ inspect ( self ( ) ) }
70+ KafkaBatcher: Accumulator process started: topic #{ topic_name } partition #{ partition } pid #{ inspect ( self ( ) ) }
5271 """ )
5372
54- { :ok , state }
73+ { :ok , % State { pipeline_unit: pipeline_unit } }
5574 end
5675
5776 @ impl GenServer
@@ -78,7 +97,7 @@ defmodule KafkaBatcher.Accumulator do
7897 { :noreply , new_state }
7998
8099 { :error , _reason , new_state } ->
81- state . collector . set_lock ( )
100+ PipelineUnit . get_collector ( state . pipeline_unit ) . set_lock ( )
82101 { :noreply , new_state }
83102 end
84103 end
@@ -92,7 +111,7 @@ defmodule KafkaBatcher.Accumulator do
92111 def handle_info ( term , state ) do
93112 Logger . warning ( """
94113 KafkaBatcher: Unknown message #{ inspect ( term ) } to #{ __MODULE__ } .handle_info/2.
95- Current state: #{ inspect ( drop_sensitive ( state ) ) }
114+ Current state: #{ inspect ( state ) }
96115 """ )
97116
98117 { :noreply , state }
@@ -104,12 +123,14 @@ defmodule KafkaBatcher.Accumulator do
104123 end
105124
106125 @ impl GenServer
107- def format_status ( _reason , [ pdict , state ] ) do
108- [ pdict , drop_sensitive ( state ) ]
109- end
110-
111- defp drop_sensitive ( % State { config: config } = state ) do
112- % State { state | config: Keyword . drop ( config , [ :sasl ] ) }
126+ def format_status ( _reason , [ pdict , % State { } = state ] ) do
127+ [
128+ pdict ,
129+ % State {
130+ state
131+ | pipeline_unit: PipelineUnit . drop_sensitive ( state . pipeline_unit )
132+ }
133+ ]
113134 end
114135
115136 defp cleanup ( % { pending_messages: [ ] , messages_to_produce: [ ] } ) do
@@ -122,7 +143,11 @@ defmodule KafkaBatcher.Accumulator do
122143 end
123144
124145 defp set_cleanup_timer_if_not_exists ( % State { cleanup_timer_ref: nil } = state ) do
125- ref = :erlang . start_timer ( state . max_wait_time , self ( ) , :cleanup )
146+ % PipelineUnit {
147+ accumulator_config: % Accumulator.Config { max_wait_time: max_wait_time }
148+ } = state . pipeline_unit
149+
150+ ref = :erlang . start_timer ( max_wait_time , self ( ) , :cleanup )
126151 % State { state | cleanup_timer_ref: ref }
127152 end
128153
@@ -161,37 +186,26 @@ defmodule KafkaBatcher.Accumulator do
161186
162187 @ spec produce_list ( messages :: [ CollectorBehaviour . event ( ) ] , state :: State . t ( ) ) :: :ok | { :error , any ( ) }
163188 defp produce_list ( messages , state ) when is_list ( messages ) do
164- @ producer . produce_list ( messages , state . topic_name , state . partition , state . config )
189+ @ producer . produce_list ( state . config , messages , state . topic_name , state . partition )
165190 catch
166191 _ , reason ->
167192 { :error , reason }
168193 end
169194
170- defp build_state ( args ) do
171- config = Keyword . fetch! ( args , :config )
172-
173- % State {
174- topic_name: Keyword . fetch! ( args , :topic_name ) ,
175- partition: Keyword . get ( args , :partition ) ,
176- config: config ,
177- batch_flusher: Keyword . fetch! ( config , :batch_flusher ) ,
178- batch_size: Keyword . fetch! ( config , :batch_size ) ,
179- max_wait_time: Keyword . fetch! ( config , :max_wait_time ) ,
180- min_delay: Keyword . fetch! ( config , :min_delay ) ,
181- max_batch_bytesize: Keyword . fetch! ( config , :max_batch_bytesize ) ,
182- collector: Keyword . fetch! ( args , :collector )
183- }
184- end
195+ defp reg_name ( % PipelineUnit { } = pipeline_unit ) do
196+ % PipelineUnit {
197+ producer_config: % Producers.Config { client_name: client_name }
198+ } = pipeline_unit
185199
186- defp reg_name ( args ) do
187- topic_name = Keyword . fetch! ( args , :topic_name )
200+ topic_name = PipelineUnit . get_topic_name ( pipeline_unit )
201+ partition = PipelineUnit . get_partition ( pipeline_unit )
188202
189- case Keyword . get ( args , : partition) do
203+ case partition do
190204 nil ->
191- :"#{ __MODULE__ } .#{ topic_name } "
205+ :"#{ __MODULE__ } .#{ client_name } . #{ topic_name } "
192206
193207 partition ->
194- :"#{ __MODULE__ } .#{ topic_name } .#{ partition } "
208+ :"#{ __MODULE__ } .#{ client_name } . #{ topic_name } .#{ partition } "
195209 end
196210 end
197211end
0 commit comments