Search before reporting
- I searched in the issues and found nothing similar.
Read release policy
- I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.
User environment
- Broker version: 4.0.14 in production. Reproduced on 4.0.13 and 4.0.14 with the official Docker images
apachepulsar/pulsar:4.0.13 and apachepulsar/pulsar:4.0.14 (two standalone clusters with geo-replication).
- Broker Java version: the Java version of the official Docker image.
- Client:
pulsar-admin and pulsar-perf from the same Docker image.
Issue Description
When the system subscription __compaction has replicated status true, the geo-replicator of the topic stops. The replicator stays connected, but msgRateOut goes to 0. The replication backlog then grows without limit.
We saw this in production on Pulsar 4.0.14. One topic built a replication backlog of more than 95 million messages. The script below reproduces it on 4.0.13 and 4.0.14 with two standalone clusters. The broker log below is from 4.0.14, and its line numbers are the same as in our production log.
Pulsar does not stop a client from setting the replicated status on __compaction. Our automation enabled replicated subscriptions on all subscriptions of a topic, including __compaction. Other tools that do the same will hit this bug.
Expected: a replicated subscription update for __compaction (or any update that fails to apply) does not stop geo-replication of the topic.
Actual: the geo-replicator stays connected, but sends nothing (msgRateOut = 0), and the replication backlog grows without limit. A topic unload or a broker restart does not help.
Root cause
__compaction has replicated status true on cluster B. Each compaction moves its mark-delete position past a snapshot. Then B writes a REPLICATED_SUBSCRIPTION_UPDATE marker for __compaction, and the B to A replicator copies it to A.
- The A to B replicator reads this marker.
PersistentReplicator.checkReplicatedSubscriptionMarker sends it to ReplicatedSubscriptionsController.receiveSubscriptionUpdated.
receiveSubscriptionUpdated calls acknowledgeMessage(pos, Cumulative, emptyMap()) on the local __compaction subscription. It does not check whether the subscription is the compactor subscription. It also does not check the replicated status of the subscription on this cluster.
PulsarCompactorSubscription.acknowledgeMessage requires the property CompactedTopicLedger (checkArgument, line 66), so it throws IllegalArgumentException.
GeoPersistentReplicator.replicateEntries catches the exception and logs "Unexpected exception in replication task". The replicator does not release, send or complete the entries after the marker in the same read batch. Their InFlightTask never completes.
getPermitsIfNoPendingRead() returns producerQueueSize - getInflightMessagesCount(). Each failure keeps some permits in use. When the replicator has no permits, readMoreEntries() reads nothing. The replicator stays connected and never recovers.
- The replicator cursor never deletes the marker entry. A topic unload or a broker restart does not help. The new replicator reads the same marker again. During catch-up, the read batches are large, so a few failures use all permits.
Code references (v4.0.14):
Status on master and the release branches
Checked by reading the code. master was not tested.
master / branch-5.0: PulsarCompactorSubscription.acknowledgeMessageAsync still calls checkArgument (ackType, positions.size(), CompactedTopicLedger property) before it returns a future. So the whenComplete error handling in ReplicatedSubscriptionsController.acknowledgeSubscriptionUpdate ([fix][broker] Log replicated subscription update ack failures #26109) does not see the exception. Since [fix][broker] Handle synchronous schema lookup failures in replication #26108, the generic catch in GeoPersistentReplicator.replicateEntries releases the entries and rewinds the cursor, so the permits should no longer leak. But the marker entry is still never deleted. We expect the replicator to read the same marker again, fail and rewind in a loop, so it still cannot move past the marker.
branch-4.0: the generic catch in GeoPersistentReplicator.replicateEntries only logs, as in 4.0.14. We expect 4.0.15 to stall in the same way.
Error messages
ERROR org.apache.pulsar.broker.service.persistent.GeoPersistentReplicator - [persistent://geo/ns/compacted | cluster-a-->cluster-b] Unexpected exception in replication task: null
java.lang.IllegalArgumentException
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:129)
at org.apache.pulsar.broker.service.persistent.PulsarCompactorSubscription.acknowledgeMessage(PulsarCompactorSubscription.java:66)
at org.apache.pulsar.broker.service.persistent.ReplicatedSubscriptionsController.receiveSubscriptionUpdated(ReplicatedSubscriptionsController.java:215)
at org.apache.pulsar.broker.service.persistent.ReplicatedSubscriptionsController.receivedReplicatedSubscriptionMarker(ReplicatedSubscriptionsController.java:123)
at org.apache.pulsar.broker.service.persistent.PersistentTopic.receivedReplicatedSubscriptionMarker(PersistentTopic.java:4867)
at org.apache.pulsar.broker.service.persistent.PersistentReplicator.checkReplicatedSubscriptionMarker(PersistentReplicator.java:849)
at org.apache.pulsar.broker.service.persistent.GeoPersistentReplicator.replicateEntries(GeoPersistentReplicator.java:136)
Reproducing the issue
The script needs Docker. It starts two standalone clusters (cluster-a, cluster-b) with geo-replication and these two topics:
geo/ns/compacted: compacted, with __compaction set to replicated on both clusters.
geo/ns/control: the same traffic, without a replicated __compaction.
Only to make the stall quick, the script sets replicationProducerQueueSize=50 on cluster A. It then unloads the topic on A, which has the same effect as a broker restart. In production, we used the default of 1000, and the stall came after a broker restart.
repro.sh
#!/usr/bin/env bash
# Reproduce: a replicated __compaction subscription stalls the geo-replicator (Pulsar 4.0.13 and 4.0.14).
# Needs Docker. Runs two standalone clusters (cluster-a, cluster-b) with geo-replication.
# Cleanup: ./repro.sh --cleanup
set -euo pipefail
IMAGE=${IMAGE:-apachepulsar/pulsar:4.0.14}
NET=pulsar-geo-repro
TOPIC=persistent://geo/ns/compacted
CONTROL=persistent://geo/ns/control
cleanup() { docker rm -f pulsar-a pulsar-b >/dev/null 2>&1 || true; docker network rm "$NET" >/dev/null 2>&1 || true; }
if [[ "${1:-}" == "--cleanup" ]]; then cleanup; exit 0; fi
cleanup
docker network create "$NET" >/dev/null
start() { # container cluster
docker run -d --name "$1" --hostname "$1" --network "$NET" \
-e clusterName="$2" -e advertisedAddress="$1" \
-e brokerDeleteInactiveTopicsEnabled=false \
-e PULSAR_MEM="-Xms1g -Xmx1g -XX:MaxDirectMemorySize=1g" \
"$IMAGE" bash -c "bin/apply-config-from-env.py conf/standalone.conf && exec bin/pulsar standalone -nfw -nss" >/dev/null
}
admin() { docker exec "$1" bin/pulsar-admin "${@:2}"; }
wait_up() { until docker exec "$1" curl -sf http://localhost:8080/admin/v2/clusters >/dev/null 2>&1; do sleep 2; done; }
compact() { # container topic: run one compaction and wait until the __compaction cursor moves
local before; before=$(md "$1" "$2")
admin "$1" topics compact "$2" >/dev/null
until [[ "$(md "$1" "$2")" != "$before" ]]; do sleep 1; done
}
md() { admin "$1" topics stats-internal "$2" | python3 -c 'import json,sys; c=json.load(sys.stdin)["cursors"]; print(c.get("__compaction",{}).get("markDeletePosition",""))'; }
repl() { # container topic remote
admin "$1" topics stats "$2" | python3 -c '
import json,sys; d=json.load(sys.stdin); r=d["replication"][sys.argv[1]]
print("msgRateIn=%.0f/s replication msgRateOut=%.0f/s replicationBacklog=%d connected=%s" % (d["msgRateIn"], r["msgRateOut"], r["replicationBacklog"], r["connected"]))' "$3"
}
start pulsar-a cluster-a
start pulsar-b cluster-b
wait_up pulsar-a; wait_up pulsar-b
echo "== 0. two standalone clusters are up; set up geo-replication for geo/ns"
for n in a b; do
for o in a b; do
[[ $n == "$o" ]] && continue
admin pulsar-$n clusters create cluster-$o --url http://pulsar-$o:8080 --broker-url pulsar://pulsar-$o:6650
done
admin pulsar-$n tenants create geo --allowed-clusters cluster-a,cluster-b
admin pulsar-$n namespaces create geo/ns --clusters cluster-a,cluster-b
done
# Optional, only to make the stall quick. With the default (1000), the stall needs more failures
# with large read batches (for example, while the replicator catches up on a backlog).
admin pulsar-a brokers update-dynamic-config --config replicationProducerQueueSize --value 50
for t in "$TOPIC" "$CONTROL"; do
admin pulsar-a topics create "$t"
until admin pulsar-b topics list geo/ns 2>/dev/null | grep -q "$t"; do sleep 1; done
done
echo "== 1. continuous traffic on both clusters, on both topics"
for t in "$TOPIC" "$CONTROL"; do
docker exec -d pulsar-a bash -c "bin/pulsar-perf produce -r 3000 -s 200 $t > /dev/null 2>&1"
docker exec -d pulsar-b bash -c "bin/pulsar-perf produce -r 20 -s 200 $t > /dev/null 2>&1"
done
sleep 15
echo "== 2. compact $TOPIC on both clusters (creates __compaction on A and B)"
compact pulsar-a "$TOPIC"; compact pulsar-b "$TOPIC"
echo "== 3. mark __compaction as replicated on both clusters"
admin pulsar-a topics set-replicated-subscription-status "$TOPIC" -s __compaction --enable
admin pulsar-b topics set-replicated-subscription-status "$TOPIC" -s __compaction --enable
sleep 10
repl pulsar-a "$TOPIC" cluster-b
echo "== 4. compact on B a few times; each run makes B send a subscription-update marker for __compaction to A"
for i in 1 2 3 4 5 6; do compact pulsar-b "$TOPIC"; sleep 10; done
repl pulsar-a "$TOPIC" cluster-b
echo "== 5. unload the topic on A (same as a broker restart or a bundle move); the replicator re-reads the old markers"
admin pulsar-a topics unload "$TOPIC"
for i in 1 2 3; do compact pulsar-b "$TOPIC"; sleep 10; done
echo "-- replicator cluster-a --> cluster-b of $TOPIC:"
for i in 1 2 3; do repl pulsar-a "$TOPIC" cluster-b; sleep 15; done
echo "-- control topic (no replicated __compaction), same brokers and same traffic:"
repl pulsar-a "$CONTROL" cluster-b
echo "== 6. broker log of cluster-a"
echo "failures: $(docker logs pulsar-a 2>&1 | grep -c 'Unexpected exception in replication task')"
docker logs pulsar-a 2>&1 | grep -m1 -A8 "Unexpected exception in replication task" | sed -E 's/^[0-9T:,+.-]+ //'
Output on 4.0.14:
== 0. two standalone clusters are up; set up geo-replication for geo/ns
== 1. continuous traffic on both clusters, on both topics
== 2. compact persistent://geo/ns/compacted on both clusters (creates __compaction on A and B)
== 3. mark __compaction as replicated on both clusters
msgRateIn=3020/s replication msgRateOut=777/s replicationBacklog=20 connected=True
== 4. compact on B a few times; each run makes B send a subscription-update marker for __compaction to A
msgRateIn=3025/s replication msgRateOut=942/s replicationBacklog=18 connected=True
== 5. unload the topic on A (same as a broker restart or a bundle move); the replicator re-reads the old markers
-- replicator cluster-a --> cluster-b of persistent://geo/ns/compacted:
msgRateIn=3070/s replication msgRateOut=0/s replicationBacklog=47022 connected=True
msgRateIn=3070/s replication msgRateOut=0/s replicationBacklog=63228 connected=True
msgRateIn=3024/s replication msgRateOut=0/s replicationBacklog=79344 connected=True
-- control topic (no replicated __compaction), same brokers and same traffic:
msgRateIn=3023/s replication msgRateOut=941/s replicationBacklog=6 connected=True
== 6. broker log of cluster-a
failures: 4
[BookKeeperClientWorker-OrderedExecutor-3-0] ERROR org.apache.pulsar.broker.service.persistent.GeoPersistentReplicator - [persistent://geo/ns/compacted | cluster-a-->cluster-b] Unexpected exception in replication task: null
java.lang.IllegalArgumentException
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:129)
at org.apache.pulsar.broker.service.persistent.PulsarCompactorSubscription.acknowledgeMessage(PulsarCompactorSubscription.java:66)
at org.apache.pulsar.broker.service.persistent.ReplicatedSubscriptionsController.receiveSubscriptionUpdated(ReplicatedSubscriptionsController.java:215)
at org.apache.pulsar.broker.service.persistent.ReplicatedSubscriptionsController.receivedReplicatedSubscriptionMarker(ReplicatedSubscriptionsController.java:123)
at org.apache.pulsar.broker.service.persistent.PersistentTopic.receivedReplicatedSubscriptionMarker(PersistentTopic.java:4867)
at org.apache.pulsar.broker.service.persistent.PersistentReplicator.checkReplicatedSubscriptionMarker(PersistentReplicator.java:849)
at org.apache.pulsar.broker.service.persistent.GeoPersistentReplicator.replicateEntries(GeoPersistentReplicator.java:136)
After the unload, the replicator of geo/ns/compacted sends nothing and its backlog grows by about 1000 entries per second. The control topic on the same brokers replicates normally.
Additional information
Discussion: #27030
Workaround (tested on 4.0.13 with the setup of the script)
- Set the replicated status of
__compaction to false on all clusters: pulsar-admin topics set-replicated-subscription-status <topic> -s __compaction --disable.
- This alone is not sufficient. The old update markers stay in the backlog, and
receiveSubscriptionUpdated processes them again after an unload.
- Set
enableReplicatedSubscriptions=false on the brokers of the stalled cluster and restart them. GeoPersistentReplicator then skips the marker check, and the backlog drains. In the test, about 330,000 entries drained in less than one minute.
- Set
enableReplicatedSubscriptions=true again and restart the brokers. Replication stays healthy. While the setting is false, Pulsar does not sync replicated subscriptions for any topic.
Proposed fix
In ReplicatedSubscriptionsController.receiveSubscriptionUpdated, ignore REPLICATED_SUBSCRIPTION_UPDATE markers for the compaction subscription (PersistentTopic.isCompactionSubscription(...)), before both the "acknowledge existing subscription" and the "create missing subscription" paths. __compaction is owned by the local compactor, and its position must only move together with the CompactedTopicLedger property, so a remote update can never be applied to it.
This fix is on the receiving side, so it also clears markers that are already in the backlog of a stuck cluster, and it protects a cluster when the remote cluster runs an older version. The change is small and the same on all branches, so it can be cherry-picked to branch-4.0, branch-4.2 and branch-5.0.
Possible follow-ups, each in its own PR:
- Make
PulsarCompactorSubscription.acknowledgeMessageAsync return a failed future instead of throwing synchronously.
- Reject
setReplicatedSubscriptionStatus (and replicateSubscriptionState=true) for __compaction, so a cluster never sends these markers.
- On
branch-4.0: after an unexpected exception in GeoPersistentReplicator.replicateEntries, release the remaining entries of the batch and complete the InFlightTask (master already does this since [fix][broker] Handle synchronous schema lookup failures in replication #26108).
Are you willing to submit a PR?
- I'm willing to submit a PR!