Skip to content
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

Allow topics with cleanup policy to be created automatically #279

Merged
merged 2 commits into from
Mar 12, 2018
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
2 changes: 1 addition & 1 deletion create-topics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ IFS=','; for topicToCreate in $KAFKA_CREATE_TOPICS; do
IFS=':' read -r -a topicConfig <<< "$topicToCreate"
config=
if [ -n "${topicConfig[3]}" ]; then
config="--config cleanup.policy=${topicConfig[3]}"
config="--config=cleanup.policy=${topicConfig[3]}"
fi
JMX_PORT='' "$KAFKA_HOME/bin/kafka-topics.sh" --create --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --replication-factor "${topicConfig[2]}" --partitions "${topicConfig[1]}" --topic "${topicConfig[0]}" "$config" --if-not-exists &
done
Expand Down
34 changes: 25 additions & 9 deletions test/003-create-topics.kafka.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@
testCreateTopics() {
NOW=$(date +%s)

DEFAULT="default-$NOW"
KAFKA_CREATE_TOPICS="$DEFAULT:1:1" create-topics.sh
# TOPICS array contains the topic name to create / validate
# CLEANUP array contains the expected cleanup policy configuration for the topic
TOPICS[0]="default-$NOW"
CLEANUP[0]=""

DEFAULT_EXISTS=$(/opt/kafka/bin/kafka-topics.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --list --topic "$DEFAULT")
DEFAULT_POLICY=$(/opt/kafka/bin/kafka-configs.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --entity-type topics --entity-name "$DEFAULT" --describe | awk -F'cleanup.policy=' '{print $2}')
DEFAULT_RESULT="$DEFAULT_EXISTS:$DEFAULT_POLICY"
TOPICS[1]="compact-$NOW"
CLEANUP[1]="compact"

if [[ "$DEFAULT_RESULT" != "$DEFAULT:" ]]; then
echo "topic not configured correctly: $DEFAULT_RESULT"
return 1
fi
KAFKA_CREATE_TOPICS="${TOPICS[0]}:1:1,${TOPICS[1]}:2:1:compact" create-topics.sh

# Loop through each array, validate that topic exists, and correct cleanup policy is set
for i in "${!TOPICS[@]}"; do
TOPIC=${TOPICS[i]}

echo "Validating topic '$TOPIC'"

EXISTS=$(/opt/kafka/bin/kafka-topics.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --list --topic "$TOPIC")
POLICY=$(/opt/kafka/bin/kafka-configs.sh --zookeeper "$KAFKA_ZOOKEEPER_CONNECT" --entity-type topics --entity-name "$TOPIC" --describe | awk -F'cleanup.policy=' '{print $2}')

RESULT="$EXISTS:$POLICY"
EXPECTED="$TOPIC:${CLEANUP[i]}"

if [[ "$RESULT" != "$EXPECTED" ]]; then
echo "$TOPIC topic not configured correctly: '$RESULT'"
return 1
fi
done

return 0
}
Expand Down