Skip to content

Commit

Permalink
refactor: Update lease ID extraction to use JSON output and hex conve…
Browse files Browse the repository at this point in the history
…rsion

This commit implements improvements to etcd lease management:
- Use JSON output for lease creation and health checks
- Properly handle decimal to hex lease ID conversion
- Add robust error handling and debugging output
- Maintain hex format for lease operations
  • Loading branch information
pcfreak30 committed Dec 31, 2024
1 parent d5f6182 commit 90a9c23
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ init_etcd() {
etcd_args="$etcd_args --user=$ETCD_USERNAME:$ETCD_PASSWORD"
fi

# Keep retrying until health check succeeds, but discard output
retry_command etcdctl ${etcd_args} endpoint health >/dev/null
# Keep retrying until health check succeeds
local health_output
health_output=$(retry_command etcdctl ${etcd_args} endpoint health -w json)
if ! echo "$health_output" | jq -e '.[0].health' >/dev/null; then
echo "Failed to verify etcd health: $health_output"
return 1
fi

# Return clean etcd args
echo "$etcd_args"
Expand Down Expand Up @@ -79,12 +84,22 @@ register_node() {
exit 1
fi

# Create lease with retry
# Create lease with retry and get JSON output
local lease_output
lease_output=$(retry_command etcdctl ${etcd_args} lease grant 60)
LEASE_ID=$(echo "$lease_output" | grep -oE 'ID: [0-9a-fA-F]+' | cut -d' ' -f2)
lease_output=$(retry_command etcdctl ${etcd_args} lease --hex grant 60 -w json)

# Extract decimal lease ID and convert to hex
local lease_id_dec
lease_id_dec=$(echo "$lease_output" | jq -r .ID)
if [ -z "$lease_id_dec" ] || [ "$lease_id_dec" = "null" ]; then
echo "Failed to obtain valid lease ID from output: $lease_output"
exit 1
fi

# Convert decimal to hex
LEASE_ID=$(printf "%x\n" "$lease_id_dec")
if [ -z "$LEASE_ID" ]; then
echo "Failed to obtain valid lease ID"
echo "Failed to convert lease ID to hex: $lease_id_dec"
exit 1
fi

Expand Down Expand Up @@ -118,7 +133,7 @@ update_heartbeat() {
fi

while true; do
# Keep lease alive with retry
# Keep lease alive with retry (using hex lease ID)
retry_command etcdctl ${etcd_args} lease keep-alive $lease_id >/dev/null 2>&1 &

# Update timestamp
Expand Down

0 comments on commit 90a9c23

Please sign in to comment.