-
Notifications
You must be signed in to change notification settings - Fork 3.9k
rabbitmq-queues: a command to display a member with highest (commit, log, snapshot) index #14237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ayanda-D
wants to merge
3
commits into
rabbitmq:main
Choose a base branch
from
Ayanda-D:pick-highest-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
β2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/member_with_highest_index.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
## This Source Code Form is subject to the terms of the Mozilla Public | ||
## License, v. 2.0. If a copy of the MPL was not distributed with this | ||
## file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
## | ||
## Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term βBroadcomβ refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
defmodule RabbitMQ.CLI.Queues.Commands.MemberWithHighestIndexCommand do | ||
alias RabbitMQ.CLI.Core.DocGuide | ||
import RabbitMQ.CLI.Core.DataCoercion | ||
|
||
@behaviour RabbitMQ.CLI.CommandBehaviour | ||
|
||
use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument | ||
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning | ||
|
||
def switches(), do: [index: :string, timeout: :integer] | ||
def aliases(), do: [i: :index, t: :timeout] | ||
|
||
def merge_defaults(args, opts) do | ||
{args, Map.merge(%{vhost: "/", index: "log"}, opts)} | ||
end | ||
|
||
def run([name] = _args, %{vhost: vhost, index: index, node: node_name}) do | ||
case :rabbit_misc.rpc_call(node_name, :rabbit_quorum_queue, :get_member_with_highest_index, [ | ||
vhost, | ||
name, | ||
to_atom(String.downcase(index)) | ||
]) do | ||
{:error, :classic_queue_not_supported} -> | ||
index = format_index(String.downcase(index)) | ||
{:error, "Cannot get #{index} index from a classic queue"} | ||
|
||
{:error, :not_found} -> | ||
{:error, {:not_found, :queue, vhost, name}} | ||
|
||
other -> | ||
other | ||
end | ||
end | ||
|
||
use RabbitMQ.CLI.DefaultOutput | ||
|
||
def formatter(), do: RabbitMQ.CLI.Formatters.PrettyTable | ||
|
||
def usage, do: "member_with_highest_index <queue> [--vhost <vhost>] [--index <commit|commit_index|log|log_index|snapshot|snapshot_index>]" | ||
|
||
def usage_additional do | ||
[ | ||
["<queue>", "quorum queue name"], | ||
["--index <commit|commit_index|log|log_index|snapshot|snapshot_index>", "name of the index to use to lookup highest member"] | ||
] | ||
end | ||
|
||
def usage_doc_guides() do | ||
[ | ||
DocGuide.quorum_queues() | ||
] | ||
end | ||
|
||
def help_section, do: :replication | ||
|
||
def description, do: "Look up first member of a quorum queue with the highest commit, log or snapshot index." | ||
|
||
def banner([name], %{node: node, index: index, vhost: vhost}) do | ||
index = format_index(String.downcase(index)) | ||
"Member with highest #{index} index for queue #{name} in vhost #{vhost} on node #{node}..." | ||
end | ||
|
||
defp format_index("log_index"), do: "log" | ||
defp format_index("commit_index"), do: "commit" | ||
defp format_index("snapshot_index"), do: "snapshot" | ||
defp format_index(index_name), do: index_name | ||
end |
55 changes: 55 additions & 0 deletions
55
deps/rabbitmq_cli/test/queues/member_with_highest_index_test.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
## This Source Code Form is subject to the terms of the Mozilla Public | ||
## License, v. 2.0. If a copy of the MPL was not distributed with this | ||
## file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
## | ||
## Copyright (c) 2007-2025 Broadcom. All Rights Reserved. The term βBroadcomβ refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
defmodule RabbitMQ.CLI.Queues.Commands.MemberWithHighestIndexCommandTest do | ||
use ExUnit.Case, async: false | ||
import TestHelper | ||
|
||
@command RabbitMQ.CLI.Queues.Commands.MemberWithHighestIndexCommand | ||
|
||
setup_all do | ||
RabbitMQ.CLI.Core.Distribution.start() | ||
|
||
:ok | ||
end | ||
|
||
setup context do | ||
{:ok, | ||
opts: %{ | ||
node: get_rabbit_hostname(), | ||
timeout: context[:test_timeout] || 30000 | ||
}} | ||
end | ||
|
||
test "validate: when no arguments are provided, returns a failure" do | ||
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} | ||
end | ||
|
||
test "validate: when two or more arguments are provided, returns a failure" do | ||
assert @command.validate(["quorum-queue-a", "one-extra-arg"], %{}) == | ||
{:validation_failure, :too_many_args} | ||
|
||
assert @command.validate( | ||
["quorum-queue-a", "extra-arg", "another-extra-arg"], | ||
%{} | ||
) == {:validation_failure, :too_many_args} | ||
end | ||
|
||
test "validate: treats one positional arguments and default switches as a success" do | ||
assert @command.validate(["quorum-queue-a"], %{}) == :ok | ||
end | ||
|
||
@tag test_timeout: 3000 | ||
test "run: targeting an unreachable node throws a badrpc" do | ||
assert match?( | ||
{:badrpc, _}, | ||
@command.run( | ||
["quorum-queue-a"], | ||
%{node: :jake@thedog, vhost: "/", index: "log", timeout: 200} | ||
) | ||
) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just FYI, the
-100
here is just an arbitrary number to initialise the accumulator, to ensure its less than the initial index value of-1
, e.g. initial snapshot index =-1
: https://github.com/rabbitmq/ra/blob/v2.16.11/src/ra_snapshot.erl#L195