Skip to content

Commit 975df44

Browse files
committed
backups: Handle transiently incorrect partition count
When using the backups library to restore topics on KRaft, we are observing the partition count check failing. Subsequent manual checks of the partition count shows the correct count. This commit adds a fix to make the restoration process anticipate that partition count metadata will be initially incorrect but eventually converge on the correct value.
1 parent cdb588b commit 975df44

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/karapace/backup/api.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from __future__ import annotations
99

10+
import time
11+
1012
from .backends.reader import BaseBackupReader, BaseItemsBackupReader, ProducerSend, RestoreTopic, RestoreTopicLegacy
1113
from .backends.v3.constants import V3_MARKER
1214
from .backends.v3.schema import ChecksumAlgorithm
@@ -271,8 +273,28 @@ def _producer(config: Config, topic: str) -> Iterator[KafkaProducer]:
271273
:raises PartitionCountError: if the topic does not have exactly one partition.
272274
:raises Exception: if client creation fails, concrete exception types are unknown, see Kafka implementation.
273275
"""
276+
exception: PartitionCountError | None
277+
274278
with kafka_producer_from_config(config) as producer:
275-
__check_partition_count(topic, producer.partitions_for)
279+
for attempt in range(5):
280+
try:
281+
__check_partition_count(topic, producer.partitions_for)
282+
except PartitionCountError as exc:
283+
exception = exc
284+
LOG.info(
285+
"Partition count for topic %r is not yet as expected, will wait and retry: %r",
286+
topic,
287+
exception,
288+
)
289+
time.sleep(10)
290+
else:
291+
exception = None
292+
break
293+
294+
if exception:
295+
LOG.warning("Partition count still incorrect after exhausting all retry attempts.")
296+
raise exception
297+
276298
yield producer
277299

278300

0 commit comments

Comments
 (0)