Class: Kafka::ConsumerGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/kafka/consumer_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster:, logger:, group_id:, session_timeout:, retention_time:) ⇒ ConsumerGroup

Returns a new instance of ConsumerGroup.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kafka/consumer_group.rb', line 8

def initialize(cluster:, logger:, group_id:, session_timeout:, retention_time:)
  @cluster = cluster
  @logger = logger
  @group_id = group_id
  @session_timeout = session_timeout
  @member_id = ""
  @generation_id = nil
  @members = {}
  @topics = Set.new
  @assigned_partitions = {}
  @assignment_strategy = RoundRobinAssignmentStrategy.new(cluster: @cluster)
  @retention_time = retention_time
end

Instance Attribute Details

#assigned_partitionsObject (readonly)

Returns the value of attribute assigned_partitions.



6
7
8
# File 'lib/kafka/consumer_group.rb', line 6

def assigned_partitions
  @assigned_partitions
end

#generation_idObject (readonly)

Returns the value of attribute generation_id.



6
7
8
# File 'lib/kafka/consumer_group.rb', line 6

def generation_id
  @generation_id
end

Instance Method Details

#commit_offsets(offsets) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kafka/consumer_group.rb', line 66

def commit_offsets(offsets)
  response = coordinator.commit_offsets(
    group_id: @group_id,
    member_id: @member_id,
    generation_id: @generation_id,
    offsets: offsets,
    retention_time: @retention_time
  )

  response.topics.each do |topic, partitions|
    partitions.each do |partition, error_code|
      Protocol.handle_error(error_code)
    end
  end
rescue Kafka::Error => e
  @logger.error "Error committing offsets: #{e}"
  raise OffsetCommitError, e
end

#fetch_offsetsObject



59
60
61
62
63
64
# File 'lib/kafka/consumer_group.rb', line 59

def fetch_offsets
  coordinator.fetch_offsets(
    group_id: @group_id,
    topics: @assigned_partitions,
  )
end

#heartbeatObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kafka/consumer_group.rb', line 85

def heartbeat
  @logger.info "Sending heartbeat..."

  response = coordinator.heartbeat(
    group_id: @group_id,
    generation_id: @generation_id,
    member_id: @member_id,
  )

  Protocol.handle_error(response.error_code)
rescue ConnectionError, UnknownMemberId, RebalanceInProgress, IllegalGeneration => e
  @logger.error "Error sending heartbeat: #{e}"
  raise HeartbeatError, e
rescue NotCoordinatorForGroup
  @logger.error "Failed to find coordinator for group `#{@group_id}`; retrying..."
  sleep 1
  @coordinator = nil
  retry
end

#joinObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kafka/consumer_group.rb', line 35

def join
  if @topics.empty?
    raise Kafka::Error, "Cannot join group without at least one topic subscription"
  end

  join_group
  synchronize
rescue NotCoordinatorForGroup
  @logger.error "Failed to find coordinator for group `#{@group_id}`; retrying..."
  sleep 1
  @coordinator = nil
  retry
rescue ConnectionError
  @logger.error "Connection error while trying to join group `#{@group_id}`; retrying..."
  @coordinator = nil
  retry
end

#leaveObject



53
54
55
56
57
# File 'lib/kafka/consumer_group.rb', line 53

def leave
  @logger.info "Leaving group `#{@group_id}`"
  coordinator.leave_group(group_id: @group_id, member_id: @member_id)
rescue ConnectionError
end

#member?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/kafka/consumer_group.rb', line 31

def member?
  !@generation_id.nil?
end

#subscribe(topic) ⇒ Object



22
23
24
25
# File 'lib/kafka/consumer_group.rb', line 22

def subscribe(topic)
  @topics.add(topic)
  @cluster.add_target_topics([topic])
end

#subscribed_partitionsObject



27
28
29
# File 'lib/kafka/consumer_group.rb', line 27

def subscribed_partitions
  @assigned_partitions.select { |topic, _| @topics.include?(topic) }
end