Skip to content

Conversation

jeqo
Copy link
Contributor

@jeqo jeqo commented Jun 24, 2025

When refreshing metadata, new requests to next broker may be processed before the last request on the previous broker, causing OutOfOrderSequence.
These errors are retried and data finally lands in order. However to avoid this error, this proposal adds artificial latency to the metadata response when it includes inkless topics. This way it gives enough time for the previous broker to complete processing requests, before the new one takes on.

I don't see any major reason why adding latency to this call would be detrimental. Metadata refresh happens in between requests, and it should happen once every 1-5minutes, depending on the metadata.max.age.ms

@jeqo jeqo requested a review from Copilot June 24, 2025 08:39
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Inkless topic metadata update logic to avoid OutOfOrderSequence errors by introducing an artificial latency delay when processing metadata for inkless topics. Key changes include updating the InklessTopicMetadataTransformer constructor to require a Time instance, updating tests accordingly, and modifying the KafkaApis reference to include the new parameter.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
storage/inkless/src/test/java/io/aiven/inkless/metadata/InklessTopicMetadataTransformerTest.java Updated transformer instantiation and added null tests for the new Time parameter
storage/inkless/src/main/java/io/aiven/inkless/metadata/InklessTopicMetadataTransformer.java Introduced a Time dependency and added a sleep call to add latency for inkless topics
core/src/main/scala/kafka/server/KafkaApis.scala Updated transformer instantiation to pass the Time instance

When refreshing metadata, new requests to next broker may be processed
_before_ the last request on the previous broker, causing
OutOfOrderSequence.
These errors are retried and data finally lands in order.
However to avoid this error, this proposal adds artificial latency to
the metadata response when it includes inkless topics.
This way it gives enough time for the previous broker to complete
processing requests, before the new one takes on.
@jeqo jeqo force-pushed the jeqo/add-latency-metadata-update branch from 4749007 to 9432574 Compare June 24, 2025 08:48
@jeqo jeqo marked this pull request as ready for review June 24, 2025 09:04
@jeqo jeqo requested a review from ivanyu June 24, 2025 09:04
Comment on lines +74 to +76
// Introduce artificial latency to avoid a race condition between the metadata update and the producer
// causing OutOfOrderSequenceException.
time.sleep(500);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's a good idea for several reasons:

  1. I don't see how waiting for fixed delay of 500 ms may solve the problem... As produce and metadata updates happen asynchronously, they may interleave in arbitrary order with or without the sleep.
  2. Blocking the handler thread isn't great.

I think the problem is theoretically possible on classic Kafka, too, it's juts unlikely because of more leadership stability + the NOT_LEADER_OR_FOLLOWER error. How bad would be it if we just do nothing and rely on existing recovery mechanism?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As produce and metadata updates happen asynchronously, they may interleave in arbitrary order with or without the sleep.

From what I found, metadata updates are not async (e.g. https://github.com/apache/kafka/blob/3c1f965c60789dcc8ee14ebabcbb4e16ebffc5ee/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java#L642) hence 500ms (choose arbitrarily but aiming to cope with a rotation on the broker side, default 250ms) would give room to avoid the race condition; though I'd agree any other implementation could potentially handle this update asynchronously and this solution may not be enough.

I think the problem is theoretically possible on classic Kafka, too

I don't think it is with partition leaders. The producer state is cached on the leader and if leadership changes, it starts accepting requests after updating the state.

How bad would be it if we just do nothing and rely on existing recovery mechanism?

OutOfOrderSequence errors are retried: https://github.com/apache/kafka/blob/6f783f85362071f82da3dcef706c7e6b89b86c2a/clients/src/main/java/org/apache/kafka/clients/producer/internals/Sender.java#L829-L834
We could just document that this error is expected and retried, and users could safely ignore it on their logs.
Another side effect we have observed is that retries lead to latency spikes (as the retry is handled within producer machinery so from the request time it just takes longer) -- though again this could be documented.

@jeqo jeqo marked this pull request as draft June 26, 2025 15:00
Copy link

This PR is being marked as stale since it has not had any activity in 90 days. If you
would like to keep this PR alive, please leave a comment asking for a review. If the PR has
merge conflicts, update it with the latest from the base branch.

If you are having difficulty finding a reviewer, please reach out on the [mailing list](https://kafka.apache.org/contact).

If this PR is no longer valid or desired, please feel free to close it. If no activity occurs in the next 30 days, it will be automatically closed.

@github-actions github-actions bot added the stale label Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants