Class: Kafka::FetchOperation

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(cluster:, logger:, min_bytes: 1, max_wait_time: 5) ⇒ FetchOperation

Returns a new instance of FetchOperation.


21
22
23
24
25
26
27
# File 'lib/kafka/fetch_operation.rb', line 21

def initialize(cluster:, logger:, min_bytes: 1, max_wait_time: 5)
  @cluster = cluster
  @logger = logger
  @min_bytes = min_bytes
  @max_wait_time = max_wait_time
  @topics = {}
end

Instance Method Details

#executeObject


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
89
90
91
92
93
94
95
96
97
# File 'lib/kafka/fetch_operation.rb', line 43

def execute
  @cluster.add_target_topics(@topics.keys)
  @cluster.

  topics_by_broker = {}

  @topics.each do |topic, partitions|
    partitions.each do |partition, options|
      broker = @cluster.get_leader(topic, partition)

      topics_by_broker[broker] ||= {}
      topics_by_broker[broker][topic] ||= {}
      topics_by_broker[broker][topic][partition] = options
    end
  end

  topics_by_broker.flat_map {|broker, topics|
    resolve_offsets(broker, topics)

    options = {
      max_wait_time: @max_wait_time * 1000, # Kafka expects ms, not secs
      min_bytes: @min_bytes,
      topics: topics,
    }

    response = broker.fetch_messages(**options)

    response.topics.flat_map {|fetched_topic|
      fetched_topic.partitions.map {|fetched_partition|
        Protocol.handle_error(fetched_partition.error_code)

        messages = fetched_partition.messages.map {|message|
          FetchedMessage.new(
            value: message.value,
            key: message.key,
            topic: fetched_topic.name,
            partition: fetched_partition.partition,
            offset: message.offset,
          )
        }

        FetchedBatch.new(
          topic: fetched_topic.name,
          partition: fetched_partition.partition,
          highwater_mark_offset: fetched_partition.highwater_mark_offset,
          messages: messages,
        )
      }
    }
  }
rescue Kafka::LeaderNotAvailable, Kafka::NotLeaderForPartition
  @cluster.mark_as_stale!

  raise
end

#fetch_from_partition(topic, partition, offset: :latest, max_bytes: 1048576) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kafka/fetch_operation.rb', line 29

def fetch_from_partition(topic, partition, offset: :latest, max_bytes: 1048576)
  if offset == :earliest
    offset = -2
  elsif offset == :latest
    offset = -1
  end

  @topics[topic] ||= {}
  @topics[topic][partition] = {
    fetch_offset: offset,
    max_bytes: max_bytes,
  }
end