Skip to content

Commit

Permalink
use cluster data for default replication (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld authored Sep 6, 2023
1 parent 95b7b01 commit 06747c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- [Improvement] Introduce cookie based sessions management for future usage.
- [Improvement] Introduce config validation.
- [Improvement] Provide flash messages support.
- [Improvement] Use replication factor of two by default (if not overridden) for Web UI topics when there is more than one broker.
- [Fix] Return 402 status instead of 500 on Pro features that are not available in OSS.
- [Fix] Fix a case where errors would not be visible without Rails due to the `String#first` usage.
- [Fix] Fix a case where live-poll would be disabled but would still update data.
Expand Down
33 changes: 27 additions & 6 deletions lib/karafka/web/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ class Cli < Thor
method_option(
:replication_factor,
desc: 'Replication factor for created topics',
default: 1,
default: false,
check_default_type: false,
type: :numeric
)
# Installs Karafka Web. Creates all needed topics, populates the data and adds the needed
# code to `karafka.rb`.
def install
Karafka::Web::Installer.new.install(replication_factor: options[:replication_factor])
Karafka::Web::Installer.new.install(
replication_factor: compute_replication_factor(options[:replication_factor])
)
end

desc 'migrate', 'Creates necessary topics if not present and populates state data'
method_option(
:replication_factor,
desc: 'Replication factor for created topics',
default: 1,
default: false,
check_default_type: false,
type: :numeric
)
# Creates new topics (if any) and populates missing data.
Expand All @@ -37,27 +41,44 @@ def install
# 2. When upgrading Web-UI in-between versions that would require extra topics and/or extra
# states populated.
def migrate
Karafka::Web::Installer.new.migrate(replication_factor: options[:replication_factor])
Karafka::Web::Installer.new.migrate(
replication_factor: compute_replication_factor(options[:replication_factor])
)
end

desc 'reset', 'Resets the Web UI by removing all the Web topics and creating them again'
method_option(
:replication_factor,
desc: 'Replication factor for created topics',
default: 1,
default: false,
check_default_type: false,
type: :numeric
)
# Resets Karafka Web. Removes the topics, creates them again and populates the initial state
# again. This is useful in case the Web-UI metrics or anything else got corrupted.
def reset
Karafka::Web::Installer.new.reset(replication_factor: options[:replication_factor])
Karafka::Web::Installer.new.reset(
replication_factor: compute_replication_factor(options[:replication_factor])
)
end

desc 'uninstall', 'Removes all the Web UI topics and the enabled code'
# Uninstalls Karafka Web
def uninstall
Karafka::Web::Installer.new.uninstall
end

private

# Takes the CLI user provided replication factor but if not present, uses the brokers count
# to decide. For non-dev clusters (with one broker) we usually want to have replication of
# two, just to have some redundancy.
# @param cli_replication_factor [Integer, false] user requested replication factor or false
# if we are supposed to compute the factor automatically
# @return [Integer] replication factor for Karafka Web UI topics
def compute_replication_factor(cli_replication_factor)
cli_replication_factor || Ui::Models::ClusterInfo.fetch.brokers.size > 1 ? 2 : 1
end
end
end
end

0 comments on commit 06747c7

Please sign in to comment.