Class: Ktl::Topic

Inherits:
Command show all
Defined in:
lib/ktl/topic.rb

Instance Method Summary collapse

Instance Method Details

#add_partitions(*names) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ktl/topic.rb', line 74

def add_partitions(*names)
  with_zk_client do |zk_client|
    names.each do |name|
      opts = options.merge(alter: nil, topic: name)
      topic_options = Kafka::Admin.to_topic_options(opts)
      logger.warn %(if "#{name}" uses keyed messages, the partition logic or ordering of the messages will be affected)
      silence_scala do
        Kafka::Admin::TopicCommand.alter_topic(zk_client.raw_client, topic_options)
      end
      logger.info %(increased partitions to #{options.partitions} for "#{name}")
    end
  end
end

#alter(regexp) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ktl/topic.rb', line 106

def alter(regexp)
  with_zk_client do |zk_client|
    opts = {zookeeper: options.zookeeper, topic: regexp}
    opts[:config] = options.add.dup unless options.add.empty?
    opts[:delete_config] = options.remove.dup unless options.remove.empty?
    if opts[:config] || opts[:delete_config]
      topic_options = Kafka::Admin.to_topic_options(opts)
      silence_scala do
        Kafka::Admin::TopicCommand.alter_topic(zk_client.raw_client, topic_options)
      end
      logger.info %(updated configuration for topics matching "#{regexp}")
    else
      raise ArgumentError, 'missing --add or --remove option'
    end
  end
end

#create(*names) ⇒ Object



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
# File 'lib/ktl/topic.rb', line 40

def create(*names)
  with_zk_client do |zk_client|
    names.each do |name|
      opts = options.merge(create: nil, topic: name)
      if options.rack_aware_allocation || options.rendezvous_allocation
        plan_factory = if options.rack_aware_allocation
          RackAwareShufflePlan
        else
          RendezvousShufflePlan
        end

        plan = plan_factory.new(zk_client, replication_factor: options.replication_factor.to_i)
        zk_utils = Kafka::Utils::ZkUtils.new(nil, nil, false)
        opts.delete(:rack_aware_allocation)
        opts.delete(:rendezvous_allocation)
        plan = plan.generate_for_new_topic(name, options.partitions.to_i)
        opts[:replica_assignment] = plan.map {|broker_list| broker_list.join(':')}.join(',')
      end
      topic_options = Kafka::Admin.to_topic_options(opts)
      silence_scala do
        Kafka::Admin::TopicCommand.create_topic(zk_client.raw_client, topic_options)
      end
      message = %(created topic "#{name}" with #{opts[:partitions]} partition(s))
      message << %(, and replication factor #{opts[:replication_factor]})
      message << %(, with replica assignment: #{opts[:replica_assignment]}) if opts[:replica_assignment]
      message << %(, with config: #{opts[:config]}) unless opts[:config].empty?
      logger.info(message)
    end
  end
end

#delete(regexp) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ktl/topic.rb', line 90

def delete(regexp)
  with_zk_client do |zk_client|
    topics = zk_client.all_topics
    topics = topics.filter { |t| !!t.match(regexp) }
    logger.info %(about to mark #{topics.size} topics for deletion)
    topics.foreach do |topic|
      Kafka::Utils.delete_topic(zk_client.raw_client, topic)
      logger.debug %(successfully marked "#{topic}" for deletion)
    end
  end
end

#describe(regexp = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ktl/topic.rb', line 19

def describe(regexp=nil)
  with_zk_client do |zk_client|
    opts = {describe: nil}
    opts[:topic] = regexp if regexp
    opts[:topics_with_overrides] = nil if options.with_overrides?
    opts[:unavailable_partitions] = nil if options.unavailable?
    opts[:under_replicated_partitions] = nil if options.under_replicated?
    topic_options = Kafka::Admin.to_topic_options(opts)
    Kafka::Admin::TopicCommand.describe_topic(zk_client.raw_client, topic_options)
  end
end

#listObject



7
8
9
10
11
12
# File 'lib/ktl/topic.rb', line 7

def list
  with_zk_client do |zk_client|
    topic_options = Kafka::Admin.to_topic_options(options.merge(list: nil))
    Kafka::Admin::TopicCommand.list_topics(zk_client.raw_client, topic_options)
  end
end