Class: Karafka::Pro::Processing::Coordinators::VirtualOffsetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb

Overview

Note:

We still use the regular coordinator “real” offset management as we want to have them as separated as possible because the real seek offset management is also used for pausing, filtering and others and should not be impacted by the virtual one

Note:

This manager is not thread-safe by itself. It should operate from coordinator locked locations.

Manager that keeps track of our offsets with the virtualization layer that are local to given partition assignment. It allows for easier offset management for virtual virtual partition cases as it provides us ability to mark as consumed and move the real offset behind as expected.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic, partition, offset_metadata_strategy) ⇒ VirtualOffsetManager

Note:

We need topic and partition because we use a seek message (virtual) for real offset management. We could keep real message reference but this can be memory consuming and not worth it.

Returns a new instance of VirtualOffsetManager.

Parameters:

  • topic (String)
  • partition (Integer)
  • offset_metadata_strategy (Symbol)

    what metadata should we select. That is, should we use the most recent or one picked from the offset that is going to be committed



58
59
60
61
62
63
64
65
66
67
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 58

def initialize(topic, partition, )
  @topic = topic
  @partition = partition
  @groups = []
  @marked = {}
  @offsets_metadata = {}
  @real_offset = -1
  @offset_metadata_strategy = 
  @current_offset_metadata = nil
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



47
48
49
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 47

def groups
  @groups
end

Instance Method Details

#clearObject

Clears the manager for a next collective operation



70
71
72
73
74
75
76
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 70

def clear
  @groups.clear
  @offsets_metadata.clear
  @current_offset_metadata = nil
  @marked.clear
  @real_offset = -1
end

#mark(message, offset_metadata) ⇒ Object

Marks given message as marked (virtually consumed). We mark given message offset and other earlier offsets from the same group as done and we can refresh our real offset representation based on that as it might have changed to a newer real offset.

Parameters:

  • message (Karafka::Messages::Message)

    message coming from VP we want to mark

  • offset_metadata (String, nil)

    offset metadata. ‘nil` if none



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 95

def mark(message, )
  offset = message.offset

  # Store metadata when we materialize the most stable offset
  @offsets_metadata[offset] = 
  @current_offset_metadata = 

  group = nil
  position = nil

  @groups.each do |reg_group|
    pos = reg_group.index(offset)

    if pos
      group = reg_group
      position = pos
      break
    end
  end

  # This case can happen when someone uses MoM and wants to mark message from a previous
  # batch as consumed. We can add it, since the real offset refresh will point to it
  unless group
    group = [offset]
    position = 0
    @groups << group
  end

  # Mark all previous messages from the same group also as virtually consumed
  group[0..position].each do |markable_offset|
    # Set previous messages metadata offset as the offset of higher one for overwrites
    # unless a different metadata were set explicitely
    @offsets_metadata[markable_offset] ||= 
    @marked[markable_offset] = true
  end

  # Recompute the real offset representation
  materialize_real_offset
end

#mark_until(message, offset_metadata) ⇒ Object

Mark all from all groups including the ‘message`. Useful when operating in a collapsed state for marking

Parameters:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 139

def mark_until(message, )
  mark(message, )

  @groups.each do |group|
    group.each do |offset|
      next if offset > message.offset

      @offsets_metadata[offset] = 
      @marked[offset] = true
    end
  end

  materialize_real_offset
end

#markableArray<Messages::Seek, String>

Returns markable message for real offset marking and its associated metadata.

Returns:

  • (Array<Messages::Seek, String>)

    markable message for real offset marking and its associated metadata

Raises:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 167

def markable
  raise Errors::InvalidRealOffsetUsageError unless markable?

   = case @offset_metadata_strategy
  when :exact
    @offsets_metadata.fetch(@real_offset)
  when :current
    @current_offset_metadata
  else
    raise Errors::UnsupportedCaseError, @offset_metadata_strategy
  end

  [
    Messages::Seek.new(
      @topic,
      @partition,
      @real_offset
    ),
    
  ]
end

#markable?Boolean

Is there a real offset we can mark as consumed

Returns:

  • (Boolean)


161
162
163
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 161

def markable?
  !@real_offset.negative?
end

#markedArray<Integer>

Returns Offsets of messages already marked as consumed virtually.

Returns:

  • (Array<Integer>)

    Offsets of messages already marked as consumed virtually



155
156
157
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 155

def marked
  @marked.select { |_, status| status }.map { |offset, _| offset }.sort
end

#register(offsets_group) ⇒ Object

Registers an offset group coming from one virtual consumer. In order to move the real underlying offset accordingly, we need to make sure to track the virtual consumers offsets groups independently and only materialize the end result.

Parameters:

  • offsets_group (Array<Integer>)

    offsets from one virtual consumer



83
84
85
86
87
# File 'lib/karafka/pro/processing/coordinators/virtual_offset_manager.rb', line 83

def register(offsets_group)
  @groups << offsets_group

  offsets_group.each { |offset| @marked[offset] = false }
end