Class: Karafka::Cli::Topics::Plan

Inherits:
Base
  • Object
show all
Defined in:
lib/karafka/cli/topics/plan.rb

Overview

Plans the migration process and prints what changes are going to be applied if migration would to be executed

Instance Method Summary collapse

Methods included from Helpers::Colorize

#green, #grey, #red, #yellow

Instance Method Details

#callBoolean

Figures out scope of changes that need to happen

Returns:

  • (Boolean)

    true if running migrate would change anything, false otherwise



11
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
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
# File 'lib/karafka/cli/topics/plan.rb', line 11

def call
  # If no changes at all, just print and stop
  if topics_to_create.empty? && topics_to_repartition.empty? && topics_to_alter.empty?
    puts "Karafka will #{yellow('not')} perform any actions. No changes needed."

    return false
  end

  changes = false

  unless topics_to_create.empty?
    changes = true
    puts 'Following topics will be created:'
    puts

    topics_to_create.each do |topic|
      puts "  #{green('+')} #{topic.name}:"
      puts "    #{green('+')} partitions: \"#{topic.declaratives.partitions}\""

      topic.declaratives.details.each do |name, value|
        puts "    #{green('+')} #{name}: \"#{value}\""
      end

      puts
    end
  end

  unless topics_to_repartition.empty?
    upscale = {}
    downscale = {}

    topics_to_repartition.each do |topic, partitions|
      from = partitions
      to = topic.declaratives.partitions

      if from < to
        upscale[topic] = partitions
      else
        downscale[topic] = partitions
      end
    end

    unless upscale.empty?
      changes = true
      puts 'Following topics will be repartitioned:'
      puts

      upscale.each do |topic, partitions|
        from = partitions
        to = topic.declaratives.partitions
        y = yellow('~')
        puts "  #{y} #{topic.name}:"
        puts "    #{y} partitions: \"#{red(from)}\" #{grey('=>')} \"#{green(to)}\""
        puts
      end
    end

    unless downscale.empty?
      puts(
        'Following topics repartitioning will be ignored as downscaling is not supported:'
      )
      puts

      downscale.each do |topic, partitions|
        from = partitions
        to = topic.declaratives.partitions

        puts "  #{grey('~')} #{topic.name}:"
        puts "    #{grey('~')} partitions: \"#{grey(from)}\" #{grey('=>')} \"#{grey(to)}\""
        puts
      end
    end
  end

  unless topics_to_alter.empty?
    changes = true
    puts 'Following topics will have configuration changes:'
    puts

    topics_to_alter.each do |topic, configs|
      puts "  #{yellow('~')} #{topic.name}:"

      configs.each do |name, changes|
        from = changes.fetch(:from)
        to = changes.fetch(:to)
        action = changes.fetch(:action)
        type = action == :change ? yellow('~') : green('+')
        puts "    #{type} #{name}: \"#{red(from)}\" #{grey('=>')} \"#{green(to)}\""
      end

      puts
    end
  end

  changes
end