Skip to content

Instantly share code, notes, and snippets.

@iv-m
Created January 19, 2017 15:31
Show Gist options
  • Save iv-m/9504a57e19bcd8e7d3f959bb2c473fb1 to your computer and use it in GitHub Desktop.
Save iv-m/9504a57e19bcd8e7d3f959bb2c473fb1 to your computer and use it in GitHub Desktop.
Scripts that calls kafka-reassign-partitions and kafka-preferred-replica-election tools
#!/bin/bash
#
# Spreads the topic among the active brokers.
# Does not change the replication factor.
#
# Usage: $0 <ZOOKEEPER> <TOPIC>
set -eu
KAFKA_HOME="/crypt/home/iv/opt/kafka_2.11-0.10.1.0"
ZOOKEEPER="$1"
TOPIC="$2"
WORKDIR=''
trap '[ -z "$WORKDIR" ] || rm -rf "$WORKDIR"' HUP PIPE INT QUIT TERM EXIT
WORKDIR=$(mktemp --directory --tmpdir ssl_for_k8s.XXXXXXXX)
cd "$WORKDIR"
TOPICS_JSON='topics.json'
JSON_OUT='reassign.json'
echo -n "{\"topics\":[{\"topic\": \"$TOPIC\"}], \"version\":1}" > "$TOPICS_JSON"
BROKERS="$($KAFKA_HOME/bin/zookeeper-shell.sh "$ZOOKEEPER" ls /brokers/ids | tail -n 1 | sed 's/[^,[:digit:]]//g')"
echo "Brokers: $BROKERS"
echo "Gathering the reassignment plan..."
"$KAFKA_HOME/bin/kafka-reassign-partitions.sh" \
--zookeeper "$ZOOKEEPER" \
--broker-list "$BROKERS" \
--topics-to-move-json-file "$TOPICS_JSON" \
--generate \
| tail -n 1 > "$JSON_OUT"
echo "Starting the reassignment..."
"$KAFKA_HOME/bin/kafka-reassign-partitions.sh" \
--zookeeper "$ZOOKEEPER" \
--reassignment-json-file "$JSON_OUT" \
--execute
while ! "$KAFKA_HOME/bin/kafka-reassign-partitions.sh" \
--zookeeper "$ZOOKEEPER" \
--reassignment-json-file "$JSON_OUT" \
--verify > /dev/null; do
echo 'Operation in progress...'
sleep 5s
done
echo 'Reassignment complete.'
"$KAFKA_HOME/bin/kafka-preferred-replica-election.sh" \
--zookeeper "$ZOOKEEPER" \
--path-to-json-file "$JSON_OUT"
echo 'Done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment