@@ -212,7 +212,7 @@ class LogStash::Inputs::Kafka < LogStash::Inputs::Base
212
212
# `key`: A ByteBuffer containing the message key
213
213
# `timestamp`: The timestamp of this message
214
214
config :decorate_events , :validate => :boolean , :default => false
215
-
215
+ config :manual_commit_interval_ms , :validate => :string
216
216
217
217
public
218
218
def register
@@ -221,6 +221,7 @@ def register
221
221
222
222
public
223
223
def run ( logstash_queue )
224
+ @manual_commit_interval_ms = manual_commit_interval_ms . to_i
224
225
@runner_consumers = consumer_threads . times . map { |i | create_consumer ( "#{ client_id } -#{ i } " ) }
225
226
@runner_threads = @runner_consumers . map { |consumer | thread_runner ( logstash_queue , consumer ) }
226
227
@runner_threads . each { |t | t . join }
@@ -247,6 +248,7 @@ def thread_runner(logstash_queue, consumer)
247
248
else
248
249
consumer . subscribe ( topics ) ;
249
250
end
251
+ last_commit_time = timestamp_ms
250
252
codec_instance = @codec . clone
251
253
while !stop?
252
254
records = consumer . poll ( poll_timeout_ms )
@@ -266,8 +268,9 @@ def thread_runner(logstash_queue, consumer)
266
268
end
267
269
end
268
270
# Manual offset commit
269
- if @enable_auto_commit == "false"
271
+ if has_to_commit? ( last_commit_time )
270
272
consumer . commitSync
273
+ last_commit_time = timestamp_ms
271
274
end
272
275
end
273
276
rescue org . apache . kafka . common . errors . WakeupException => e
@@ -354,4 +357,16 @@ def set_sasl_config(props)
354
357
355
358
props . put ( "sasl.kerberos.service.name" , sasl_kerberos_service_name ) unless sasl_kerberos_service_name . nil?
356
359
end
360
+
361
+ def timestamp_ms
362
+ ( Time . now . to_f * 1000 ) . to_i
363
+ end
364
+
365
+ def has_to_commit? ( last_commit_time )
366
+ # If auto_commit is enable we just leave the commit to the client library on poll and close actions
367
+ return false if @enable_auto_commit == "false"
368
+
369
+ # If auto_commit is disable, we need to commit, we will do it depending on the manual_commit_interval option
370
+ @manual_commit_interval_ms <= 0 || ( last_commit_time + @manual_commit_interval_ms ) < timestamp_ms
371
+ end
357
372
end #class LogStash::Inputs::Kafka
0 commit comments