Class: Kafkat::Command::Reassign

Inherits:
Base
  • Object
show all
Defined in:
lib/kafkat/command/reassign.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#admin, #initialize, #kafka_logs, register_as, usage, usages, #zookeeper

Methods included from Logging

#print_err

Methods included from Kafkat::CommandIO

#prompt_and_execute_assignments

Methods included from Formatting

#justify, #print_assignment, #print_assignment_header, #print_broker, #print_broker_header, #print_partition, #print_partition_header, #print_topic, #print_topic_header, #print_topic_name

Constructor Details

This class inherits a constructor from Kafkat::Command::Base

Instance Method Details

#add_partition_to_all_lowest(tp, nb_partitions_to_add) ⇒ Object

Add a topic partition to lowest unbalanced nodes you can specify the number of times the partition must be added to fit its minimal replica count



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kafkat/command/reassign.rb', line 138

def add_partition_to_all_lowest( tp, nb_partitions_to_add )

  nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
  nodes_lowest = nodes_sizes_replicas.sort_by{|k,v| v}.map { |a| a[0] }

  nodes_lowest.each do |node|
    break if nb_partitions_to_add <= 0

    unless @nodes_lists_replicas[node].has_key?(tp)
      add_partition_to_node( tp, node )
      nb_partitions_to_add -= 1
    end
  end
end

#add_partition_to_node(tp, node) ⇒ Object

Add a topic partition to a specific node



128
129
130
131
132
133
# File 'lib/kafkat/command/reassign.rb', line 128

def add_partition_to_node( tp, node )

  @nodes_lists_replicas[node][tp] = ''
  @partitions_lists[tp]['replicas'] ||= {}
  @partitions_lists[tp]['replicas'][node] = ''
end

#migrate_excess_replicas_from_node(node, nb_partitions_to_remove) ⇒ Object

Migrate excess replicas from node to lowest unbalanced nodes It chooses absent replicas from lowest unbalanced nodes, to make sure we fill the lowest BUG: There is very probably an issue here in case the lowest node has less than ‘nb_partitions_to_remove` absent partitions from source node



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/kafkat/command/reassign.rb', line 202

def migrate_excess_replicas_from_node( node, nb_partitions_to_remove )

  # This method is not efficient, but that is not something you run often
  nb_partitions_to_remove.times do |i|

    # Find out the lowest balanced nodes
    nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
    node_lowest = nodes_sizes_replicas.sort_by{|k,v| v}[0][0]

    # Find out which partitions the lowest balanced node does not have and which can be migrated from this node
    partition_to_migrate = (@nodes_lists_replicas[node].keys - @nodes_lists_replicas[node_lowest].keys)[0]

    remove_partition_from_node( partition_to_migrate, node )
    add_partition_to_node( partition_to_migrate, node_lowest )

  end

  # # Find out the lowest balanced nodes
  # nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
  # nodes_lowest = nodes_sizes_replicas.sort_by{|k,v| v}.map { |a| a[0] }

  # nodes_lowest.each do |node_lowest|
  #   break if nb_partitions_to_remove <= 0

  #   # Find out which partitions the lowest balanced node does not have and which can be migrated from this node
  #   partitions_to_migrate = @nodes_lists_replicas[node].keys - @nodes_lists_replicas[node_lowest].keys

  #   partitions_to_migrate.each do |tp|
  #     break if nb_partitions_to_remove <= 0

  #     remove_partition_from_node( tp, node )
  #     add_partition_to_node( tp, node_lowest )

  #     nb_partitions_to_remove -= 1
  #   end
  # end
end

#remove_excess_replicas_from_node(node, nb_partitions_to_remove) ⇒ Object

Remove excess replicas from node If nb_partitions_to_remove is nil, then remove all excess replicas First remove partitions which are not in ISR and already replicated enough times on other nodes of the cluster Then regardless of ISR



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/kafkat/command/reassign.rb', line 167

def remove_excess_replicas_from_node( node, nb_partitions_to_remove )

  # First remove partitions which are not in ISR and already replicated enough times on other nodes of the cluster

  replicas_not_isr = @nodes_lists_replicas[node].keys - @nodes_lists_isr[node].keys

  replicas_not_isr_over_replicated = replicas_not_isr.keep_if { |tp| @partitions_lists[tp]['replicas'].keys.size > @replica_count }

  replicas_not_isr_over_replicated.each do |tp|
    break if (!nb_partitions_to_remove.nil? && nb_partitions_to_remove <= 0)

    remove_partition_from_node( tp, node )

    nb_partitions_to_remove -= 1 unless nb_partitions_to_remove.nil?
  end


  # Then remove partitions which regardless of ISR and already replicated enough times on other nodes of the cluster

  replicas_over_replicated = @nodes_lists_replicas[node].keys.keep_if { |tp| @partitions_lists[tp]['replicas'].keys.size > @replica_count }

  replicas_over_replicated.each do |tp|
    break if (!nb_partitions_to_remove.nil? && nb_partitions_to_remove <= 0)

    remove_partition_from_node( tp, node )

    nb_partitions_to_remove -= 1 unless nb_partitions_to_remove.nil?
  end

end

#remove_partition_from_node(tp, node) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/kafkat/command/reassign.rb', line 154

def remove_partition_from_node( tp, node )

    @nodes_lists_replicas[node].delete tp
    @nodes_lists_isr[node].delete tp
    @partitions_lists[tp]['replicas'].delete(node)
    @partitions_lists[tp]['isr'].delete(node)
end

#runObject



32
33
34
35
36
37
38
39
40
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
89
90
91
92
93
94
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/kafkat/command/reassign.rb', line 32

def run
  topic_names = ARGV.shift unless ARGV[0] && ARGV[0].start_with?('--')

  all_brokers = zookeeper.get_brokers

  topics = nil
  if topic_names
     topics_list = topic_names.split(',')
     topics = zookeeper.get_topics(topics_list)
  end
  topics ||= zookeeper.get_topics

  opts = Trollop.options do
    opt :brokers, "replica set (broker IDs)", type: :string
    opt :replicas, "number of replicas (count)", type: :integer
  end

  broker_ids = opts[:brokers] && opts[:brokers].split(',').map(&:to_i)
  replica_count = opts[:replicas]

  broker_ids ||= zookeeper.get_brokers.values.map(&:id)

  all_brokers_id = all_brokers.values.map(&:id)
  broker_ids.each do |id|
    if !all_brokers_id.include?(id)
      print "ERROR: Broker #{id} is not currently active.\n"
      exit 1
    end
  end

  # *** This logic is duplicated from Kakfa 0.8.1.1 ***

  assignments = []
  broker_count = broker_ids.size

  # Helper structures
  @replica_count = replica_count
  @nodes_lists_replicas = {}
  @nodes_lists_isr = {}
  @partitions_lists={}
  @partitions_lists_old={}

  topics.each do |_, t|
    # This is how Kafka's AdminUtils determines these values.
    partition_count = t.partitions.size
    topic_replica_count = replica_count || t.partitions[0].replicas.size

    if topic_replica_count > broker_count
      print "ERROR: Replication factor (#{topic_replica_count}) is larger than brokers (#{broker_count}).\n"
      exit 1
    end

    # start_index = Random.rand(broker_count)
    # replica_shift = Random.rand(broker_count)

    # t.partitions.each do |p|
    #   replica_shift += 1 if p.id > 0 && p.id % broker_count == 0
    #   first_replica_index = (p.id + start_index) % broker_count

    #   replicas = [broker_ids[first_replica_index]]

    #   (0...topic_replica_count-1).each do |i|
    #     shift = 1 + (replica_shift + i) % (broker_count - 1)
    #     index = (first_replica_index + shift) % broker_count
    #     replicas << broker_ids[index]
    #   end

    #   replicas.reverse!
    #   assignments << Assignment.new(t.name, p.id, replicas)
    # end

    t.partitions.each do |p|
      @partitions_lists[p.topic_name + ' ' + p.id.to_s] ||= {'name'=>p.topic_name, 'id'=>p.id, 'replicas'=>{},'isr'=>{}}
      @partitions_lists_old[p.topic_name + ' ' + p.id.to_s] ||= {'name'=>p.topic_name, 'id'=>p.id, 'replicas'=>{},'isr'=>{}}

      p.replicas.each do |node|
        @partitions_lists[p.topic_name + ' ' + p.id.to_s]['replicas'][node] = ''
        @partitions_lists_old[p.topic_name + ' ' + p.id.to_s]['replicas'][node] = ''
        @nodes_lists_replicas[node] ||= {}
        @nodes_lists_replicas[node][p.topic_name + ' ' + p.id.to_s] = ''
      end

      p.isr.each do |node|
        @partitions_lists[p.topic_name + ' ' + p.id.to_s]['isr'][node] = ''
        @partitions_lists_old[p.topic_name + ' ' + p.id.to_s]['isr'][node] = ''

        # nodes_sizes_isr[node] ||= 0
        # nodes_sizes_isr[node] += 1
        @nodes_lists_isr[node] ||= {}
        @nodes_lists_isr[node][p.topic_name + ' ' + p.id.to_s] = ''
      end
    end
  end


  # Add a topic partition to a specific node
  def add_partition_to_node( tp, node )

    @nodes_lists_replicas[node][tp] = ''
    @partitions_lists[tp]['replicas'] ||= {}
    @partitions_lists[tp]['replicas'][node] = ''
  end


  # Add a topic partition to lowest unbalanced nodes
  # you can specify the number of times the partition must be added to fit its minimal replica count
  def add_partition_to_all_lowest( tp, nb_partitions_to_add )

    nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
    nodes_lowest = nodes_sizes_replicas.sort_by{|k,v| v}.map { |a| a[0] }

    nodes_lowest.each do |node|
      break if nb_partitions_to_add <= 0

      unless @nodes_lists_replicas[node].has_key?(tp)
        add_partition_to_node( tp, node )
        nb_partitions_to_add -= 1
      end
    end
  end


  def remove_partition_from_node( tp, node )

      @nodes_lists_replicas[node].delete tp
      @nodes_lists_isr[node].delete tp
      @partitions_lists[tp]['replicas'].delete(node)
      @partitions_lists[tp]['isr'].delete(node)
  end


  # Remove excess replicas from node
  # If nb_partitions_to_remove is nil, then remove all excess replicas
  # First remove partitions which are not in ISR and already replicated enough times on other nodes of the cluster
  # Then regardless of ISR
  def remove_excess_replicas_from_node( node, nb_partitions_to_remove )

    # First remove partitions which are not in ISR and already replicated enough times on other nodes of the cluster

    replicas_not_isr = @nodes_lists_replicas[node].keys - @nodes_lists_isr[node].keys

    replicas_not_isr_over_replicated = replicas_not_isr.keep_if { |tp| @partitions_lists[tp]['replicas'].keys.size > @replica_count }

    replicas_not_isr_over_replicated.each do |tp|
      break if (!nb_partitions_to_remove.nil? && nb_partitions_to_remove <= 0)

      remove_partition_from_node( tp, node )

      nb_partitions_to_remove -= 1 unless nb_partitions_to_remove.nil?
    end


    # Then remove partitions which regardless of ISR and already replicated enough times on other nodes of the cluster

    replicas_over_replicated = @nodes_lists_replicas[node].keys.keep_if { |tp| @partitions_lists[tp]['replicas'].keys.size > @replica_count }

    replicas_over_replicated.each do |tp|
      break if (!nb_partitions_to_remove.nil? && nb_partitions_to_remove <= 0)

      remove_partition_from_node( tp, node )

      nb_partitions_to_remove -= 1 unless nb_partitions_to_remove.nil?
    end

  end


  # Migrate excess replicas from node to lowest unbalanced nodes
  # It chooses absent replicas from lowest unbalanced nodes, to make sure we fill the lowest
  # BUG: There is very probably an issue here in case the lowest node has less than `nb_partitions_to_remove` absent partitions from source node
  def migrate_excess_replicas_from_node( node, nb_partitions_to_remove )

    # This method is not efficient, but that is not something you run often
    nb_partitions_to_remove.times do |i|

      # Find out the lowest balanced nodes
      nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
      node_lowest = nodes_sizes_replicas.sort_by{|k,v| v}[0][0]

      # Find out which partitions the lowest balanced node does not have and which can be migrated from this node
      partition_to_migrate = (@nodes_lists_replicas[node].keys - @nodes_lists_replicas[node_lowest].keys)[0]

      remove_partition_from_node( partition_to_migrate, node )
      add_partition_to_node( partition_to_migrate, node_lowest )

    end

    # # Find out the lowest balanced nodes
    # nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
    # nodes_lowest = nodes_sizes_replicas.sort_by{|k,v| v}.map { |a| a[0] }

    # nodes_lowest.each do |node_lowest|
    #   break if nb_partitions_to_remove <= 0

    #   # Find out which partitions the lowest balanced node does not have and which can be migrated from this node
    #   partitions_to_migrate = @nodes_lists_replicas[node].keys - @nodes_lists_replicas[node_lowest].keys

    #   partitions_to_migrate.each do |tp|
    #     break if nb_partitions_to_remove <= 0

    #     remove_partition_from_node( tp, node )
    #     add_partition_to_node( tp, node_lowest )

    #     nb_partitions_to_remove -= 1
    #   end
    # end
  end


  ###
  #   At this point, the helper structures represent the current repartition of partitions
  #   We will modify the helper structures to balance the partitions accross brokers whith the minimum number of partitions moves
  ###

  # Add under replicated partitions to lowest unbalanced nodes
  partitions_under_replicated = @partitions_lists.keys.keep_if { |tp| @partitions_lists[tp]['replicas'].size < @replica_count }
  partitions_under_replicated.each do |tp|
    how_many = @replica_count - @partitions_lists[tp]['replicas'].size
    add_x_to_all_lowest( tp, how_many )
  end


  # Begin leaning and draining nodes with the most partitions towards the lowest unbalanced nodes
  2.times do |i|

    # List all nodes from biggest to lowest balanced
    nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
    nodes_biggest = nodes_sizes_replicas.sort_by{|k,v| v}.reverse.map { |a| a[0] }

    # helper count of partition replicas to balance
    nodes_left_to_balance = nodes_biggest.size
    nb_partitions_left_to_balance = @replica_count * @partitions_lists.keys.size

    # Remove excess replicas on all nodes from biggest to lowest unbalanced nodes, until balance is achieved
    nodes_biggest.each do |node|

      # Figure out the numbers of partitions each node should have to be balanced
      # I am wary about float operations, for example to do a 5.00000001.ceil()
      # nb_partitions_should_have = ( nb_partitions_left_to_balance / nodes_left_to_balance.to_f ).ceil
      nb_partitions_should_have = nb_partitions_left_to_balance / nodes_left_to_balance
      nb_partitions_should_have += 1 if (nb_partitions_left_to_balance % nodes_left_to_balance) > 0


      # nb_partitions_to_remove = @nodes_lists_replicas[node].keys.size - nb_partitions_should_have.ceil
      nb_partitions_to_remove = @nodes_lists_replicas[node].keys.size - nb_partitions_should_have
      remove_excess_replicas_from_node( node, nb_partitions_to_remove) if nb_partitions_to_remove > 0

      # nb_partitions_to_remove = @nodes_lists_replicas[node].keys.size - nb_partitions_should_have.ceil
      nb_partitions_to_remove = @nodes_lists_replicas[node].keys.size - nb_partitions_should_have
      migrate_excess_replicas_from_node( node, nb_partitions_to_remove) if nb_partitions_to_remove > 0


      nodes_left_to_balance -= 1
      nb_partitions_left_to_balance = nb_partitions_left_to_balance - nb_partitions_should_have

    end
  end

  # Remove excess replicas on all nodes from lowest unbalanced nodes to biggest nodes. This is to enforce replica count, and if the whole algorithm is fine, it should do nothing
  nodes_sizes_replicas = @nodes_lists_replicas.hmap { |k,v| { k => v.size } }
  nodes_biggest = nodes_sizes_replicas.sort_by{|k,v| v}.reverse.map { |a| a[0] }
  nodes_biggest.each do |node|
    remove_excess_replicas_from_node( node, nil )
  end


  require 'pp'
  puts "\nCurrent partition assignment:"
  pp @partitions_lists_old.hmap { |k,v| { k => { 'replicas'=>v['replicas'], 'isr'=>v['isr']} } }
  puts "\nOptimized partition assignment with minimal partition migrations:"
  pp @partitions_lists.hmap { |k,v| { k => { 'replicas'=>v['replicas'], 'isr'=>v['isr']} } }
  puts ''


  @partitions_lists.each_key do |tp|
    assignments << Assignment.new( @partitions_lists[tp]['name'],
                                   @partitions_lists[tp]['id'],
                                   @partitions_lists[tp]['replicas'].keys,
                                 )
  end


  # ****************

  prompt_and_execute_assignments(assignments)
end