Class: WaterDrop::Producer
- Inherits:
-
Object
- Object
- WaterDrop::Producer
- Extended by:
- Forwardable
- Defined in:
- lib/waterdrop/producer.rb,
lib/waterdrop/producer/sync.rb,
lib/waterdrop/producer/async.rb,
lib/waterdrop/producer/buffer.rb,
lib/waterdrop/producer/status.rb,
lib/waterdrop/producer/builder.rb
Overview
Main WaterDrop messages producer
Defined Under Namespace
Modules: Async, Buffer, Sync Classes: Builder, Status
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Dry-configurable config object.
-
#id ⇒ String
readonly
Uuid of the current producer.
-
#messages ⇒ Concurrent::Array
readonly
Internal messages buffer.
-
#monitor ⇒ Object
readonly
Monitor we want to use.
-
#status ⇒ Status
readonly
Producer status object.
Instance Method Summary collapse
-
#client ⇒ Rdkafka::Producer
Raw rdkafka producer.
-
#close ⇒ Object
Flushes the buffers in a sync way and closes the producer.
-
#ensure_active! ⇒ Object
Ensures that we don’t run any operations when the producer is not configured or when it was already closed.
-
#initialize(&block) ⇒ Producer
constructor
Creates a not-yet-configured instance of the producer.
-
#setup(&block) ⇒ Object
Sets up the whole configuration and initializes all that is needed.
-
#validate_message!(message) ⇒ Object
Ensures that the message we want to send out to Kafka is actually valid and that it can be sent there.
-
#wait(handler) ⇒ Object
Waits on a given handler.
Methods included from Buffer
#buffer, #buffer_many, #flush_async, #flush_sync
Methods included from Async
#produce_async, #produce_many_async
Methods included from Sync
#produce_many_sync, #produce_sync
Constructor Details
#initialize(&block) ⇒ Producer
Creates a not-yet-configured instance of the producer
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/waterdrop/producer.rb', line 36 def initialize(&block) @buffer_mutex = Mutex.new @connecting_mutex = Mutex.new @closing_mutex = Mutex.new @status = Status.new @messages = Concurrent::Array.new return unless block setup(&block) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns dry-configurable config object.
31 32 33 |
# File 'lib/waterdrop/producer.rb', line 31 def config @config end |
#id ⇒ String (readonly)
Returns uuid of the current producer.
23 24 25 |
# File 'lib/waterdrop/producer.rb', line 23 def id @id end |
#messages ⇒ Concurrent::Array (readonly)
Returns internal messages buffer.
27 28 29 |
# File 'lib/waterdrop/producer.rb', line 27 def @messages end |
#monitor ⇒ Object (readonly)
Returns monitor we want to use.
29 30 31 |
# File 'lib/waterdrop/producer.rb', line 29 def monitor @monitor end |
#status ⇒ Status (readonly)
Returns producer status object.
25 26 27 |
# File 'lib/waterdrop/producer.rb', line 25 def status @status end |
Instance Method Details
#client ⇒ Rdkafka::Producer
Client is lazy initialized, keeping in mind also the fact of a potential fork that can happen any time.
It is not recommended to fork a producer that is already in use so in case of bootstrapping a cluster, it’s much better to fork configured but not used producers
Returns raw rdkafka producer.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/waterdrop/producer.rb', line 70 def client return @client if @client && @pid == Process.pid # Don't allow to obtain a client reference for a producer that was not configured raise Errors::ProducerNotConfiguredError, id if @status.initial? @connecting_mutex.synchronize do return @client if @client && @pid == Process.pid # We should raise an error when trying to use a producer from a fork, that is already # connected to Kafka. We allow forking producers only before they are used raise Errors::ProducerUsedInParentProcess, Process.pid if @status.connected? # We undefine all the finalizers, in case it was a fork, so the finalizers from the parent # process don't leak ObjectSpace.undefine_finalizer(id) # Finalizer tracking is needed for handling shutdowns gracefully. # I don't expect everyone to remember about closing all the producers all the time, thus # this approach is better. Although it is still worth keeping in mind, that this will # block GC from removing a no longer used producer unless closed properly but at least # won't crash the VM upon closing the process ObjectSpace.define_finalizer(id, proc { close }) @pid = Process.pid @client = Builder.new.call(self, @config) # Register statistics runner for this particular type of callbacks ::Karafka::Core::Instrumentation.statistics_callbacks.add( @id, Instrumentation::Callbacks::Statistics.new(@id, @client.name, @config.monitor) ) # Register error tracking callback ::Karafka::Core::Instrumentation.error_callbacks.add( @id, Instrumentation::Callbacks::Error.new(@id, @client.name, @config.monitor) ) @status.connected! end @client end |
#close ⇒ Object
Flushes the buffers in a sync way and closes the producer
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/waterdrop/producer.rb', line 115 def close @closing_mutex.synchronize do return unless @status.active? @monitor.instrument( 'producer.closed', producer_id: id ) do @status.closing! # No need for auto-gc if everything got closed by us # This should be used only in case a producer was not closed properly and forgotten ObjectSpace.undefine_finalizer(id) # We save this thread id because we need to bypass the activity verification on the # producer for final flush of buffers. @closing_thread_id = Thread.current.object_id # Flush has its own buffer mutex but even if it is blocked, flushing can still happen # as we close the client after the flushing (even if blocked by the mutex) flush(true) # We should not close the client in several threads the same time # It is safe to run it several times but not exactly the same moment # We also mark it as closed only if it was connected, if not, it would trigger a new # connection that anyhow would be immediately closed client.close(@config.max_wait_timeout) if @client # Remove callbacks runners that were registered ::Karafka::Core::Instrumentation.statistics_callbacks.delete(@id) ::Karafka::Core::Instrumentation.error_callbacks.delete(@id) @status.closed! end end end |
#ensure_active! ⇒ Object
Ensures that we don’t run any operations when the producer is not configured or when it was already closed
154 155 156 157 158 159 160 161 162 |
# File 'lib/waterdrop/producer.rb', line 154 def ensure_active! return if @status.active? raise Errors::ProducerNotConfiguredError, id if @status.initial? raise Errors::ProducerClosedError, id if @status.closing? || @status.closed? # This should never happen raise Errors::StatusInvalidError, [id, @status.to_s] end |
#setup(&block) ⇒ Object
Sets up the whole configuration and initializes all that is needed
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/waterdrop/producer.rb', line 51 def setup(&block) raise Errors::ProducerAlreadyConfiguredError, id unless @status.initial? @config = Config .new .setup(&block) .config @id = @config.id @monitor = @config.monitor @contract = Contracts::Message.new(max_payload_size: @config.max_payload_size) @status.configured! end |
#validate_message!(message) ⇒ Object
Ensures that the message we want to send out to Kafka is actually valid and that it can be sent there
168 169 170 |
# File 'lib/waterdrop/producer.rb', line 168 def () @contract.validate!(, Errors::MessageInvalidError) end |
#wait(handler) ⇒ Object
Waits on a given handler
175 176 177 178 179 180 |
# File 'lib/waterdrop/producer.rb', line 175 def wait(handler) handler.wait( max_wait_timeout: @config.max_wait_timeout, wait_timeout: @config.wait_timeout ) end |