|
| 1 | +## This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +## License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +## file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +## |
| 5 | +## Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | + |
| 7 | +defmodule RabbitMQ.CLI.Queues.Commands.GrowToCountCommand do |
| 8 | + alias RabbitMQ.CLI.Core.{DocGuide, Validators} |
| 9 | + import RabbitMQ.CLI.Core.DataCoercion |
| 10 | + |
| 11 | + @behaviour RabbitMQ.CLI.CommandBehaviour |
| 12 | + |
| 13 | + defp default_opts, |
| 14 | + do: %{vhost_pattern: ".*", queue_pattern: ".*", membership: "promotable", errors_only: false} |
| 15 | + |
| 16 | + def switches(), |
| 17 | + do: [ |
| 18 | + vhost_pattern: :string, |
| 19 | + queue_pattern: :string, |
| 20 | + membership: :string, |
| 21 | + errors_only: :boolean |
| 22 | + ] |
| 23 | + |
| 24 | + def merge_defaults(args, opts) do |
| 25 | + {args, Map.merge(default_opts(), opts)} |
| 26 | + end |
| 27 | + |
| 28 | + def validate(args, _) when length(args) < 2 do |
| 29 | + {:validation_failure, :not_enough_args} |
| 30 | + end |
| 31 | + |
| 32 | + def validate(args, _) when length(args) > 2 do |
| 33 | + {:validation_failure, :too_many_args} |
| 34 | + end |
| 35 | + |
| 36 | + def validate([_, s], _) |
| 37 | + when not (s == "all" or |
| 38 | + s == "even") do |
| 39 | + {:validation_failure, "strategy '#{s}' is not recognised."} |
| 40 | + end |
| 41 | + |
| 42 | + def validate([n, _], _) |
| 43 | + when (is_integer(n) and n <= 0) do |
| 44 | + {:validation_failure, "node count '#{n}' must be greater than 0."} |
| 45 | + end |
| 46 | + |
| 47 | + def validate(_, %{membership: m}) |
| 48 | + when not (m == "promotable" or |
| 49 | + m == "non_voter" or |
| 50 | + m == "voter") do |
| 51 | + {:validation_failure, "voter status '#{m}' is not recognised."} |
| 52 | + end |
| 53 | + |
| 54 | + def validate(_, _) do |
| 55 | + :ok |
| 56 | + end |
| 57 | + |
| 58 | + def validate_execution_environment(args, opts) do |
| 59 | + Validators.chain( |
| 60 | + [ |
| 61 | + &Validators.rabbit_is_running/2 |
| 62 | + ], |
| 63 | + [args, opts] |
| 64 | + ) |
| 65 | + end |
| 66 | + |
| 67 | + def run([node_count, strategy], %{ |
| 68 | + node: node_name, |
| 69 | + vhost_pattern: vhost_pat, |
| 70 | + queue_pattern: queue_pat, |
| 71 | + membership: membership, |
| 72 | + errors_only: errors_only |
| 73 | + }) when is_integer(node_count) do |
| 74 | + |
| 75 | + args = [node_count, vhost_pat, queue_pat, to_atom(strategy)] |
| 76 | + |
| 77 | + args = |
| 78 | + case to_atom(membership) do |
| 79 | + :promotable -> args |
| 80 | + other -> args ++ [other] |
| 81 | + end |
| 82 | + |
| 83 | + case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :grow, args) do |
| 84 | + {:error, _} = error -> |
| 85 | + error |
| 86 | + |
| 87 | + {:badrpc, _} = error -> |
| 88 | + error |
| 89 | + |
| 90 | + results when errors_only -> |
| 91 | + for {{:resource, vhost, _kind, name}, {:error, _, _} = res} <- results, |
| 92 | + do: [ |
| 93 | + {:vhost, vhost}, |
| 94 | + {:name, name}, |
| 95 | + {:size, format_size(res)}, |
| 96 | + {:result, format_result(res)} |
| 97 | + ] |
| 98 | + |
| 99 | + results -> |
| 100 | + for {{:resource, vhost, _kind, name}, res} <- results, |
| 101 | + do: [ |
| 102 | + {:vhost, vhost}, |
| 103 | + {:name, name}, |
| 104 | + {:size, format_size(res)}, |
| 105 | + {:result, format_result(res)} |
| 106 | + ] |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + use RabbitMQ.CLI.DefaultOutput |
| 111 | + |
| 112 | + def formatter(), do: RabbitMQ.CLI.Formatters.Table |
| 113 | + |
| 114 | + def usage, |
| 115 | + do: |
| 116 | + "grow_to_count <node_count> <all | even> [--vhost-pattern <pattern>] [--queue-pattern <pattern>] [--membership <promotable|voter>]" |
| 117 | + |
| 118 | + def usage_additional do |
| 119 | + [ |
| 120 | + ["<node_count>", "number of nodes to place replicas on"], |
| 121 | + [ |
| 122 | + "<all | even>", |
| 123 | + "add a member for all matching queues or just those whose membership count is an even number" |
| 124 | + ], |
| 125 | + ["--queue-pattern <pattern>", "regular expression to match queue names"], |
| 126 | + ["--vhost-pattern <pattern>", "regular expression to match virtual host names"], |
| 127 | + ["--membership <promotable|voter>", "add a promotable non-voter (default) or full voter"], |
| 128 | + ["--errors-only", "only list queues which reported an error"] |
| 129 | + ] |
| 130 | + end |
| 131 | + |
| 132 | + def usage_doc_guides() do |
| 133 | + [ |
| 134 | + DocGuide.quorum_queues() |
| 135 | + ] |
| 136 | + end |
| 137 | + |
| 138 | + def help_section, do: :cluster_management |
| 139 | + |
| 140 | + def description, |
| 141 | + do: |
| 142 | + "Grows quorum queue clusters by adding member replicas on the specified number of nodes for all matching queues" |
| 143 | + |
| 144 | + def banner([node_count, strategy], _) do |
| 145 | + "Growing #{strategy} quorum queues on #{node_count} nodes..." |
| 146 | + end |
| 147 | + |
| 148 | + # |
| 149 | + # Implementation |
| 150 | + # |
| 151 | + |
| 152 | + defp format_size({:ok, size}) do |
| 153 | + size |
| 154 | + end |
| 155 | + |
| 156 | + defp format_size({:error, _size, :timeout}) do |
| 157 | + # the actual size is uncertain here |
| 158 | + "?" |
| 159 | + end |
| 160 | + |
| 161 | + defp format_size({:error, size, _}) do |
| 162 | + size |
| 163 | + end |
| 164 | + |
| 165 | + defp format_result({:ok, _size}) do |
| 166 | + "ok" |
| 167 | + end |
| 168 | + |
| 169 | + defp format_result({:error, _size, :timeout}) do |
| 170 | + "error: the operation timed out and may not have been completed" |
| 171 | + end |
| 172 | + |
| 173 | + defp format_result({:error, _size, err}) do |
| 174 | + to_string(:io_lib.format("error: ~W", [err, 10])) |
| 175 | + end |
| 176 | +end |
0 commit comments