Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 10 additions & 56 deletions addons/pulsar/scripts-ut-spec/bookies_member_leave_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,27 @@ Describe "Pulsar Bookies Member Leave Bash Script Tests"
}
BeforeAll "init"

Describe "format_bookie()"
It "formats bookie with force and deleteCookie"
bin/bookkeeper() {
if [ "$1" = "shell" ] && [ "$2" = "bookieformat" ] && [ "$3" = "-nonInteractive" ] && [ "$4" = "-force" ] && [ "$5" = "-deleteCookie" ]; then
echo "Bookie formatted with force and deleteCookie"
return 0
fi
return 1
}

When call format_bookie "true" "true"
The output should include "Formatting Bookie..."
The output should include "Bookie formatted with force and deleteCookie"
The status should be success
End

It "formats bookie without force and deleteCookie"
bin/bookkeeper() {
if [ "$1" = "shell" ] && [ "$2" = "bookieformat" ] && [ "$3" = "-nonInteractive" ]; then
echo "Bookie formatted"
return 0
fi
return 1
}

When call format_bookie "false" "false"
The output should include "Formatting Bookie..."
The output should include "Bookie formatted"
The status should be success
End
End

Describe "should_format_bookie()"
It "returns true when pod index is greater than or equal to replicas"
When call should_format_bookie "pod-3" "2"
The status should be success
End

It "returns false when pod index is less than replicas"
When call should_format_bookie "pod-1" "2"
The status should be failure
End
End
bin/bookkeeper() {
return 0
}

Describe "bookies_member_leave()"
setup() {
export CURRENT_POD_NAME="pod-3"
export BOOKKEEPER_COMP_REPLICAS="2"
}

It "formats bookie when condition is met"
setup

format_bookie() {
echo "format_bookie called with $1 $2"
}
export CURRENT_POD_NAME="pod-3"
export KB_LEAVE_MEMBER_POD_NAME="pod-3"

When call bookies_member_leave
The output should include "format_bookie called with true true"
The output should include "Formatting Bookie..."
The output should include "Bookie formatted"
End

It "skips formatting when condition is not met"
export CURRENT_POD_NAME="pod-1"
export BOOKKEEPER_COMP_REPLICAS="2"
export KB_LEAVE_MEMBER_POD_NAME="pod-2"

When call bookies_member_leave
The output should include "Skipping Bookie formatting"
The output should include "Member to leave is not current pod, skipping Bookie formatting"
The status should be failure
End
End
End
41 changes: 8 additions & 33 deletions addons/pulsar/scripts/bookies-member-leave.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
#!/bin/bash

format_bookie() {
local force=$1
local delete_cookie=$2

echo "Formatting Bookie..."
if [[ $force == "true" && $delete_cookie == "true" ]]; then
bin/bookkeeper shell bookieformat -nonInteractive -force -deleteCookie || true
else
bin/bookkeeper shell bookieformat -nonInteractive || true
fi
echo "Bookie formatted"
}

# TODO: this logic should be refactored rather than judging by pod name index
should_format_bookie() {
local current_pod_name=$1
local current_component_replicas=$2

local idx=${current_pod_name##*-}
if [[ $idx -ge $current_component_replicas && $current_component_replicas -ne 0 ]]; then
return 0
else
bookies_member_leave() {
if [[ "$KB_LEAVE_MEMBER_POD_NAME" != "$CURRENT_POD_NAME" ]]; then
echo "Member to leave is not current pod, skipping Bookie formatting"
return 1
fi
}

bookies_member_leave() {
# shellcheck disable=SC2153
local current_pod_name=${CURRENT_POD_NAME}
local current_component_replicas=${BOOKKEEPER_COMP_REPLICAS}

if should_format_bookie "$current_pod_name" "$current_component_replicas"; then
format_bookie "true" "true"
else
echo "Skipping Bookie formatting"
fi
# TODO: consider using decommissionbookie? But decommissionbookie needs bookie to stop first, kb doesn't support a "postMemberLeave" hook.
echo "Formatting Bookie..."
export BOOKIE_CONF=/opt/pulsar/conf/bookkeeper.conf
bin/bookkeeper shell bookieformat -nonInteractive -force -deleteCookie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个命令能指定 leave member ip,可以的话就比较简单了吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bookieformat 只能对 pod 所在的节点生效

echo "Bookie formatted"
}

# This is magic for shellspec ut framework.
Expand Down