pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[fix][broker] Fix replicator getting stuck under rate limiter throttling and honor readBatchSize/maxReadSizeBytes on the default read path

Open void-ptr974 opened this issue 5 months ago • 1 comments

Motivation

PersistentReplicator.readMoreEntries() created an InFlightTask before checking the replicator dispatch rate limiter.

When replicator dispatch throttling was enabled and the rate limiter had no message or byte permits, the method scheduled a retry and returned without issuing cursor.asyncReadEntriesOrWait(...). The newly-created task stayed in entries == null, so it looked like a pending cursor read even though no read request existed.

On the next retry, hasPendingRead() returned true and the replicator stopped scheduling further reads. This could leave geo-replication stuck with backlog when replicator dispatch throttling is enabled.

In addition, readBatchSize/maxReadSizeBytes logic wasn't applied on the default read path when the rate limiter isn't configured.

Modifications

Compute producer and rate-limiter permits before creating the in-flight read task.

Create the InFlightTask only after confirming that a real cursor read will be issued. This preserves the invariant that an entries == null in-flight task corresponds to an actual pending cursor read.

Store the computed byte read limit on the InFlightTask, so the task carries both message and byte read limits without an extra wrapper object.

Added a unit test covering the no-permit rate-limiter path for both message and byte throttling, verifying that no pending in-flight read task is created.

Honor readBatchSize/maxReadSizeBytes on the default read path. This was previously ignored as a result of some earlier changes.

Verifying this change

This change added tests and can be verified as follows:

  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.persistent.PersistentReplicatorInflightTaskTest.testRateLimiterWithoutPermitsDoesNotCreateInFlightTask
  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.persistent.PersistentReplicatorInflightTaskTest.testCreateOrRecycleInFlightTaskIntoQueue
  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.persistent.PersistentReplicatorInflightTaskTest.testMaybeCreateInFlightReadTask
  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.ReplicatorRateLimiterTest.testReplicatorRateLimiterMessageReceivedAllMessages
  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.ReplicatorRateLimiterTest.testReplicatorRateLimiterByBytes

Does this pull request potentially affect one of the following parts:

  • [ ] Dependencies (add or upgrade a dependency)
  • [ ] The public API
  • [ ] The schema
  • [ ] The default values of configurations
  • [ ] The threading model
  • [ ] The binary protocol
  • [ ] The REST endpoints
  • [ ] The admin CLI options
  • [ ] The metrics
  • [ ] Anything that affects deployment

void-ptr974 avatar Jun 12 '26 01:06 void-ptr974

Hi @poorbarcode, this one adjusts when PersistentReplicator creates in-flight read tasks under dispatch rate limiting. Could you help review the pending-read / permit invariant when you have time? Thanks!

void-ptr974 avatar Jun 14 '26 13:06 void-ptr974

Thanks @poorbarcode and @lhotari. I pushed the remaining follow-ups.

  • Renamed InFlightTask.bytesToRead to maxBytesToRead. This should make it clearer that the field only carries the max byte limit for cursor.asyncReadEntriesOrWait(...); it is not involved in InFlightTask.isDone(), which still only depends on the read result entries and completed entries.
  • Converted testRateLimiterWithoutPermitsDoesNotCreateInFlightTask to use a TestNG @DataProvider, so the message-permit and byte-permit cases run as separate cases.
  • Added an integration test in OneWayReplicatorTest to cover the real replication path when the replicator dispatch rate limiter temporarily has no permits, and verify replication continues afterwards.

I also ran the focused broker tests locally multiple times, including the new integration test.

void-ptr974 avatar Jun 25 '26 15:06 void-ptr974

Thanks @poorbarcode and @lhotari. I pushed another follow-up to make the read-limit handling clearer.

What changed:

  • Changed the new integration test to use topic-level replicator dispatch rate policy, so the test does not affect other topics in the namespace.
  • Removed maxBytesToRead from InFlightTask.
  • Split the decision into two parts:
    • ReadLimits decides the next read size, including both message and byte limits.
    • InFlightTask only tracks the in-flight read/publish lifecycle.
  • readMoreEntries() now keeps the byte limit as a local value and passes it directly to cursor.asyncReadEntriesOrWait(...).
  • Updated the unit tests to validate read-limit decisions directly, including both message and byte limits.

The main idea is to avoid mixing a read request parameter into InFlightTask. maxBytesToRead only belongs to the next cursor read, while InFlightTask should only represent the pending/completed entries lifecycle. This should make the intent clearer and avoid an extra wrapper object/allocation on the read scheduling path.

void-ptr974 avatar Jun 25 '26 16:06 void-ptr974

Pushed a small cleanup follow-up in 8c4e573.

What changed:

  • Removed the @VisibleForTesting maybeGetReadLimitsForNextRead() wrapper and made ReadLimits private again.
  • Removed the tests that directly exercised that internal helper, so the read-limit decision and inFlightTasks locking protocol are no longer exposed only for tests.
  • Updated the schema-rewind comment to describe the readMoreEntries() scheduling path instead of referencing a removed helper.

The regression coverage remains in OneWayReplicatorTest.testReplicatorContinuesAfterRateLimiterHasNoPermits, which exercises the real replication path with replicator dispatch rate limiting and verifies replication continues afterwards. I also reran the focused broker tests, compile, and checkstyle locally.

void-ptr974 avatar Jun 26 '26 02:06 void-ptr974

This PR makes another bug occur more frequently. The bug is fixed by #26106. Explanation in https://github.com/apache/pulsar/pull/26106#issuecomment-4848761650

lhotari avatar Jun 30 '26 23:06 lhotari