Class: Kafkat::Command::ResignForce

Inherits:
Base
  • Object
show all
Defined in:
lib/kafkat/command/resign-rewrite.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

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/kafkat/command/resign-rewrite.rb', line 12

def run
  broker_id = ARGV[0] && ARGV.shift.to_i
  if broker_id.nil?
    puts "You must specify a broker ID."
    exit 1
  end

  opts = Trollop.options do
    opt :force, "force"
  end

  print "This operation rewrites leaderships in ZK to exclude broker '#{broker_id}'.\n"
  print "WARNING: This is a last resort. Try the 'shutdown' command first!\n\n".red

  return unless agree("Proceed (y/n)?")

  brokers = zookeeper.get_brokers
  topics = zookeeper.get_topics
  force = opts[:force]

  ops = {}
  topics.each do |_, t|
    t.partitions.each do |p|
      next if p.leader != broker_id

      alternates = p.isr.reject { |i| i == broker_id }
      new_leader_id = alternates.sample

      if !new_leader_id && !force
        print "Partition #{t.name}-#{p.id} has no other ISRs!\n"
        exit 1
      end

      new_leader_id ||= -1
      ops[p] = new_leader_id
    end
  end

  print "\n"
  print "Summary of the new assignments:\n\n"

  print "Partition\tLeader\n"
  ops.each do |p, lid|
    print justify("#{p.topic_name}-#{p.id}")
    print justify(lid.to_s)
    print "\n"
  end

  begin
    print "\nStarting.\n"
    ops.each do |p, lid|
      retryable(tries: 3, on: Interface::Zookeeper::WriteConflictError) do
        zookeeper.write_leader(p, lid)
      end
    end
  rescue Interface::Zookeeper::WriteConflictError => e
    print "Failed to update leaderships in ZK. Try re-running.\n\n"
    exit 1
  end

  print "Done.\n"
end