Class: Kafka::FetchOperation
- Inherits:
-
Object
- Object
- Kafka::FetchOperation
- Defined in:
- lib/kafka/fetch_operation.rb
Overview
Fetches messages from one or more partitions.
operation = Kafka::FetchOperation.new(
cluster: cluster,
logger: logger,
min_bytes: 1,
max_wait_time: 10,
)
# These calls will schedule fetches from the specified topics/partitions.
operation.fetch_from_partition("greetings", 42, offset: :latest, max_bytes: 100000)
operation.fetch_from_partition("goodbyes", 13, offset: :latest, max_bytes: 100000)
operation.execute
Instance Method Summary collapse
- #execute ⇒ Object
- #fetch_from_partition(topic, partition, offset:, max_bytes:) ⇒ Object
-
#initialize(cluster:, logger:, min_bytes:, max_wait_time:) ⇒ FetchOperation
constructor
A new instance of FetchOperation.
Constructor Details
#initialize(cluster:, logger:, min_bytes:, max_wait_time:) ⇒ FetchOperation
Returns a new instance of FetchOperation.
19 20 21 22 23 24 25 |
# File 'lib/kafka/fetch_operation.rb', line 19 def initialize(cluster:, logger:, min_bytes:, max_wait_time:) @cluster = cluster @logger = logger @min_bytes = min_bytes @max_wait_time = max_wait_time @topics = {} end |
Instance Method Details
#execute ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/kafka/fetch_operation.rb', line 41 def execute @cluster.add_target_topics(@topics.keys) @cluster. topics_by_broker = {} @topics.each do |topic, partitions| partitions.each do |partition, | broker = @cluster.get_leader(topic, partition) topics_by_broker[broker] ||= {} topics_by_broker[broker][topic] ||= {} topics_by_broker[broker][topic][partition] = end end topics_by_broker.flat_map {|broker, topics| resolve_offsets(broker, topics) = { max_wait_time: @max_wait_time * 1000, # Kafka expects ms, not secs min_bytes: @min_bytes, topics: topics, } response = broker.(**) response.topics.flat_map {|fetched_topic| fetched_topic.partitions.flat_map {|fetched_partition| Protocol.handle_error(fetched_partition.error_code) fetched_partition..map {|| FetchedMessage.new( value: .value, key: .key, topic: fetched_topic.name, partition: fetched_partition.partition, offset: .offset, ) } } } } rescue Kafka::LeaderNotAvailable, Kafka::NotLeaderForPartition @cluster.mark_as_stale! raise end |
#fetch_from_partition(topic, partition, offset:, max_bytes:) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kafka/fetch_operation.rb', line 27 def fetch_from_partition(topic, partition, offset:, max_bytes:) if offset == :earliest offset = -2 elsif offset == :latest offset = -1 end @topics[topic] ||= {} @topics[topic][partition] = { fetch_offset: offset, max_bytes: max_bytes, } end |