Skip to content

Commit 1acda8d

Browse files
muirCopilot
andauthored
[fix] wait for broadcast consumers to be "stable" before saying consume is ready (#110)
In multi-broker kafka clusters, it can take a little while for consumers to be stable. This is only important for the startup of broadcast consumers -- other things may be waiting for the consumers to be ready before generating events. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c704de4 commit 1acda8d

8 files changed

Lines changed: 201 additions & 93 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: "Start Kafka (3 brokers)"
2+
description: "Starts a 3-broker Kafka cluster (ZooKeeper mode) for GitHub Actions tests"
3+
4+
outputs:
5+
brokers:
6+
description: "Space-separated broker list for clients on the runner"
7+
value: ${{ steps.out.outputs.brokers }}
8+
9+
runs:
10+
using: "composite"
11+
steps:
12+
- name: Start ZooKeeper + Kafka(3)
13+
shell: bash
14+
run: |
15+
set -euo pipefail
16+
17+
NET="gha-kafka"
18+
ZK="gha-zk"
19+
K1="gha-kafka1"
20+
K2="gha-kafka2"
21+
K3="gha-kafka3"
22+
23+
# If a previous run left anything around, clean it up.
24+
docker rm -f "$K1" "$K2" "$K3" "$ZK" >/dev/null 2>&1 || true
25+
docker network rm "$NET" >/dev/null 2>&1 || true
26+
27+
docker network create "$NET"
28+
29+
docker run -d --name "$ZK" --network "$NET" \
30+
-e ALLOW_ANONYMOUS_LOGIN=yes \
31+
bitnamilegacy/zookeeper:3.9
32+
33+
# Common env for all brokers:
34+
# - INTERNAL listener: used broker<->broker (reachable via service/container name on the docker network)
35+
# - EXTERNAL listener: mapped to localhost:<port> for the test process running on the runner VM
36+
common_env=(
37+
-e ALLOW_PLAINTEXT_LISTENER=yes
38+
-e KAFKA_CFG_ZOOKEEPER_CONNECT="$ZK:2181"
39+
-e KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP="INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT"
40+
-e KAFKA_CFG_INTER_BROKER_LISTENER_NAME="INTERNAL"
41+
-e KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR=3
42+
-e KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=3
43+
-e KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR=2
44+
-e KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS=0
45+
)
46+
47+
docker run -d --name "$K1" --network "$NET" \
48+
-p 19092:19092 \
49+
-e KAFKA_BROKER_ID=1 \
50+
-e KAFKA_CFG_LISTENERS="INTERNAL://:9092,EXTERNAL://:19092" \
51+
-e KAFKA_CFG_ADVERTISED_LISTENERS="INTERNAL://$K1:9092,EXTERNAL://localhost:19092" \
52+
"${common_env[@]}" \
53+
bitnamilegacy/kafka:3.6.2
54+
55+
docker run -d --name "$K2" --network "$NET" \
56+
-p 29092:29092 \
57+
-e KAFKA_BROKER_ID=2 \
58+
-e KAFKA_CFG_LISTENERS="INTERNAL://:9092,EXTERNAL://:29092" \
59+
-e KAFKA_CFG_ADVERTISED_LISTENERS="INTERNAL://$K2:9092,EXTERNAL://localhost:29092" \
60+
"${common_env[@]}" \
61+
bitnamilegacy/kafka:3.6.2
62+
63+
docker run -d --name "$K3" --network "$NET" \
64+
-p 39092:39092 \
65+
-e KAFKA_BROKER_ID=3 \
66+
-e KAFKA_CFG_LISTENERS="INTERNAL://:9092,EXTERNAL://:39092" \
67+
-e KAFKA_CFG_ADVERTISED_LISTENERS="INTERNAL://$K3:9092,EXTERNAL://localhost:39092" \
68+
"${common_env[@]}" \
69+
bitnamilegacy/kafka:3.6.2
70+
71+
echo "Waiting for Kafka to become ready..."
72+
for i in {1..90}; do
73+
if docker exec "$K1" bash -lc "\
74+
if ! command -v /opt/bitnami/kafka/bin/kafka-topics.sh >/dev/null 2>&1; then
75+
echo 'kafka-topics.sh not found in /opt/bitnami/kafka/bin' >&2
76+
ls -la /opt/bitnami/kafka/bin >&2 || true
77+
exit 127
78+
fi
79+
echo 'client.listener.name=INTERNAL' > /tmp/kafka-client.properties
80+
/opt/bitnami/kafka/bin/kafka-topics.sh \
81+
--bootstrap-server $K1:9092 \
82+
--command-config /tmp/kafka-client.properties \
83+
--list \
84+
>/dev/null 2>&1
85+
"; then
86+
echo "Kafka ready."
87+
exit 0
88+
fi
89+
if (( i % 10 == 0 )); then
90+
echo "Kafka not ready yet (attempt $i)."
91+
docker ps -a --filter "name=$K1" --filter "name=$K2" --filter "name=$K3" --filter "name=$ZK" || true
92+
docker logs --tail 50 "$K1" || true
93+
docker logs --tail 50 "$K2" || true
94+
docker logs --tail 50 "$K3" || true
95+
fi
96+
sleep 2
97+
done
98+
99+
echo "Kafka did not become ready in time."
100+
docker ps -a || true
101+
docker inspect "$ZK" "$K1" "$K2" "$K3" || true
102+
docker logs "$ZK" || true
103+
docker logs "$K1" || true
104+
docker logs "$K2" || true
105+
docker logs "$K3" || true
106+
exit 1
107+
108+
- name: Emit brokers output
109+
id: out
110+
shell: bash
111+
run: |
112+
echo "brokers=localhost:19092 localhost:29092 localhost:39092" >> "$GITHUB_OUTPUT"

.github/workflows/nodb.yml

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,6 @@ jobs:
1313
Test-no-database-integration:
1414
runs-on: ubuntu-latest
1515

16-
services:
17-
kafka:
18-
image: bitnamilegacy/kafka:3.6.2
19-
env:
20-
KAFKA_BROKER_ID: 1
21-
KAFKA_CFG_NODE_ID: 1
22-
KAFKA_CFG_PROCESS_ROLES: controller,broker
23-
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
24-
KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
25-
KAFKA_CFG_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
26-
KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
27-
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
28-
KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: false
29-
KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
30-
KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
31-
KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR: 1
32-
KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
33-
KAFKA_CFG_INTER_BROKER_LISTENER_NAME: PLAINTEXT
34-
ports:
35-
- 9092:9092
36-
- 9093:9093
37-
3816
steps:
3917
- name: Harden Runner
4018
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76
@@ -53,6 +31,10 @@ jobs:
5331
- name: Check out repository code
5432
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
5533

34+
- name: Start Kafka (3 brokers)
35+
id: kafka
36+
uses: ./.github/actions/kafka-3-brokers
37+
5638
- name: Get Go cache paths
5739
id: go-cache-paths
5840
run: |
@@ -85,7 +67,7 @@ jobs:
8567
- name: Test and Coverage
8668
env:
8769
EVENTS_POSTGRES_TEST_DSN: "postgres://postgres:postgres@localhost?sslmode=disable"
88-
EVENTS_KAFKA_BROKERS: "localhost:9092"
70+
EVENTS_KAFKA_BROKERS: ${{ steps.kafka.outputs.brokers }}
8971
# NTEST_BUFFERING: "false"
9072
run: |
9173
export EVENTS_DEBUG_NOTIFY="true"

.github/workflows/pg.yml

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@ jobs:
2626
ports:
2727
- 5432:5432
2828

29-
kafka:
30-
image: bitnamilegacy/kafka:3.6.2
31-
env:
32-
KAFKA_BROKER_ID: 1
33-
KAFKA_CFG_NODE_ID: 1
34-
KAFKA_CFG_PROCESS_ROLES: controller,broker
35-
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
36-
KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
37-
KAFKA_CFG_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
38-
KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
39-
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
40-
KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: false
41-
KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
42-
KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
43-
KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR: 1
44-
KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
45-
KAFKA_CFG_INTER_BROKER_LISTENER_NAME: PLAINTEXT
46-
ports:
47-
- 9092:9092
48-
- 9093:9093
49-
5029
steps:
5130
- name: Harden Runner
5231
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76
@@ -94,10 +73,14 @@ jobs:
9473
- name: Build
9574
run: go build -v ./...
9675

76+
- name: Start Kafka (3 brokers)
77+
id: kafka
78+
uses: ./.github/actions/kafka-3-brokers
79+
9780
- name: Test and Coverage
9881
env:
9982
EVENTS_POSTGRES_TEST_DSN: "postgres://postgres:postgres@localhost?sslmode=disable"
100-
EVENTS_KAFKA_BROKERS: "localhost:9092"
83+
EVENTS_KAFKA_BROKERS: ${{ steps.kafka.outputs.brokers }}
10184
# NTEST_BUFFERING: "false"
10285
run: |
10386
export EVENTS_DEBUG_NOTIFY="true"

.github/workflows/s2.yml

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@ jobs:
2626
ROOT_PASSWORD: test
2727
SINGLESTORE_LICENSE: ${{ secrets.SINGLESTORE_LICENSE }}
2828

29-
kafka:
30-
image: bitnamilegacy/kafka:3.6.2
31-
env:
32-
KAFKA_BROKER_ID: 1
33-
KAFKA_CFG_NODE_ID: 1
34-
KAFKA_CFG_PROCESS_ROLES: controller,broker
35-
KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
36-
KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
37-
KAFKA_CFG_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
38-
KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
39-
KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
40-
KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: false
41-
KAFKA_CFG_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
42-
KAFKA_CFG_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
43-
KAFKA_CFG_TRANSACTION_STATE_LOG_MIN_ISR: 1
44-
KAFKA_CFG_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
45-
KAFKA_CFG_INTER_BROKER_LISTENER_NAME: PLAINTEXT
46-
ports:
47-
- 9092:9092
48-
- 9093:9093
49-
5029
steps:
5130
- name: Harden Runner
5231
uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76
@@ -95,10 +74,14 @@ jobs:
9574
- name: Build
9675
run: go build -v ./...
9776

77+
- name: Start Kafka (3 brokers)
78+
id: kafka
79+
uses: ./.github/actions/kafka-3-brokers
80+
9881
- name: Test and Coverage
9982
env:
10083
EVENTS_S2TEST_DSN: "root:test@tcp(127.0.0.1:3306)/eventstest?tls=false&parseTime=true"
101-
EVENTS_KAFKA_BROKERS: "localhost:9092"
84+
EVENTS_KAFKA_BROKERS: ${{ steps.kafka.outputs.brokers }}
10285
# NTEST_BUFFERING: "false"
10386
run: |
10487
export EVENTS_DEBUG_NOTIFY="true"

0 commit comments

Comments
 (0)