Skip to content

Stop re-parsing topic URIs when a validated TopicUri already exists - #151

Open
vahidhashemian wants to merge 2 commits into
pinterest:4.0from
vahidhashemian:4.0
Open

vahidhashemian wants to merge 2 commits into
pinterest:4.0from
vahidhashemian:4.0

Conversation

@vahidhashemian

Copy link
Copy Markdown
Contributor

Summary

The PSC client was parsing a topic URI every time it built a TopicUriPartition on the producer send path and the consumer record path, then immediately discarding that parse and attaching a TopicUri it already had. TopicUriPartition now has a constructor that keeps the caller's URI string and an already validated TopicUri without running the URI and topic-RN regular expressions again. Call sites that already hold a validated TopicUri use that constructor, or the existing TopicUriPartition(TopicUri, int) constructor when the string is already the canonical form from getTopicUriAsString().

This is limited to the psc client. The Flink connector is unchanged. The public TopicUriPartition(String) and TopicUriPartition(String, int) constructors still parse, so callers that only have a URI string behave as before. PscProducer.validateTopicUri and PscConsumer.validateTopicUri are still the cached entry points for a new string.

Producer

PscProducer.send still validates the message URI through the cache. The partition it attaches now keeps the exact string the caller passed, including a URI with no protocol, and the validated backend TopicUri instance. A later send of the same string reuses that instance. PscKafkaProducer uses the same approach when it builds the KafkaMessageId after an ack, both in the completion callback and in Future.get(), and when getPartitions returns one partition per Kafka partition. Those message ids still carry the canonical URI string, the registered KafkaTopicUri, and the partition Kafka assigned.

Consumer

Each Kafka record and each Memq record now builds its partition from the TopicUri already registered for that backend topic. The resulting message id still has the same URI string, partition, offset, timestamp, serialized sizes, and, for Memq, the end-of-batch flag. The same construction is used for the partition set on a poll, Kafka rebalance callbacks, Kafka and Memq assignment(), Kafka and Memq getPartitions(), Kafka commitSync(), and the Kafka and Memq commit callbacks, including Kafka auto-resolution both enabled and disabled.

PscKafkaConsumer.committed(Collection) no longer builds a partition from TopicPartition.topic(). That value is the Kafka topic name, so parsing it failed and the returned message id had a null topic URI. It now returns the TopicUriPartition the caller passed in, which is what startOffsets and endOffsets already do. assign() still returns the partitions the caller supplied when the consumer is not subscribed.

Kafka commitSync() previously returned a message id whose topic URI was a freshly parsed BaseTopicUri and was not replaced with the registered KafkaTopicUri. It now carries that registered KafkaTopicUri. Equality, hash code, and ordering against a partition finalized with the same URI still match, because those are based on the URI components rather than the concrete class.

Tests

The new tests compare every changed construction with the previous pattern of parsing the string and then finalizing the partition. They cover plaintext and secure Kafka and Memq URIs, the unset partition and partitions 0, 1, and Integer.MAX_VALUE, equality, hash code, ordering, map and set lookup, message ids, and Java serialization. A non-canonical caller string stays distinct from the canonical partition and is preserved. The string constructors still parse a valid URI and still leave the topic URI unset when the string is invalid. Producer send, Kafka and Memq record conversion, Kafka assignment, rebalance, getPartitions, committed, and both commit-callback modes, and Memq assignment and commit, are all checked against that previous finalized result.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant