Repository navigation
fix(inkless:consolidation): route DeleteRecords to the real leader and report cross-tier low watermark [KC-332] #710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
viktorsomogyi
merged 2 commits into
main
from
svv/ts-unification-delete-records-routing
Jul 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
core/src/main/scala/kafka/server/DisklessDeleteRecordsForwarder.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| // Copyright (c) 2024 Aiven, Helsinki, Finland. https://aiven.io/ | ||
|
|
||
| package kafka.server | ||
|
|
||
| import kafka.server.metadata.InklessMetadataView | ||
| import kafka.utils.Logging | ||
| import org.apache.kafka.clients.{ClientResponse, NetworkClient, RequestCompletionHandler} | ||
| import org.apache.kafka.common.message.DeleteRecordsRequestData | ||
| import org.apache.kafka.common.message.DeleteRecordsRequestData.{DeleteRecordsPartition, DeleteRecordsTopic} | ||
| import org.apache.kafka.common.message.DeleteRecordsResponseData.DeleteRecordsPartitionResult | ||
| import org.apache.kafka.common.protocol.Errors | ||
| import org.apache.kafka.common.requests.{DeleteRecordsRequest, DeleteRecordsResponse} | ||
| import org.apache.kafka.common.utils.Time | ||
| import org.apache.kafka.common.{Node, TopicPartition} | ||
| import org.apache.kafka.metadata.{MetadataCache, PartitionRegistration} | ||
| import org.apache.kafka.server.util.{InterBrokerSendThread, RequestAndCompletionHandler} | ||
|
|
||
| import java.util | ||
| import java.util.concurrent.{CompletableFuture, ConcurrentLinkedQueue} | ||
| import scala.collection.mutable | ||
| import scala.jdk.CollectionConverters._ | ||
|
|
||
| /** | ||
| * Forwards the leader-only leg of a `DeleteRecords` request to a diskless partition's real KRaft | ||
| * leader over the inter-broker listener. | ||
| * | ||
| * For a diskless topic that owns a classic/consolidated prefix (switched, switch-pending or | ||
| * consolidating), `ReplicaManager.deleteRecords` splits the request into a broker-agnostic diskless | ||
| * (control-plane) leg and a local-log leg. The local-log leg only succeeds on the real KRaft leader | ||
| * (`Partition.deleteRecordsOnLeader` requires `leaderLogIfLocal`). Because | ||
| * `InklessTopicMetadataTransformer` advertises a hash/AZ-selected replica as the client-facing | ||
| * leader for locality-aware read routing, the AdminClient generally delivers `DeleteRecords` to a | ||
| * follower, which rejects it with `NOT_LEADER_OR_FOLLOWER`. | ||
| * | ||
| * `KafkaApis` uses this forwarder to send the affected partitions to their real leader (resolved | ||
| * from the raw KRaft metadata image, independent of the transformer). The receiving leader runs its | ||
| * own `ReplicaManager.deleteRecords` and serves both legs authoritatively, so read AZ-routing is | ||
| * left untouched. | ||
| * | ||
| * Modeled on `AddPartitionsToTxnManager` / `TransactionMarkerChannelManager`. | ||
| */ | ||
| class DisklessDeleteRecordsForwarder( | ||
| config: KafkaConfig, | ||
| networkClient: NetworkClient, | ||
| metadataCache: MetadataCache, | ||
| metadataView: InklessMetadataView, | ||
| time: Time | ||
| ) extends InterBrokerSendThread( | ||
| "DisklessDeleteRecordsForwarder-" + config.brokerId, | ||
| networkClient, | ||
| config.requestTimeoutMs, | ||
| time | ||
| ) with Logging { | ||
|
|
||
| this.logIdent = s"[DisklessDeleteRecordsForwarder broker=${config.brokerId}] " | ||
|
|
||
| private val interBrokerListenerName = config.interBrokerListenerName | ||
|
|
||
| private case class PendingForward( | ||
| leader: Node, | ||
| offsets: Map[TopicPartition, Long], | ||
| timeoutMs: Int, | ||
| future: CompletableFuture[Map[TopicPartition, DeleteRecordsPartitionResult]] | ||
| ) | ||
|
|
||
| private val queue = new ConcurrentLinkedQueue[PendingForward]() | ||
|
|
||
| /** | ||
| * Split `offsets` into the partitions this broker handles locally (via | ||
| * `ReplicaManager.deleteRecords`) and the partitions whose local-log leg must run on another | ||
| * broker's real KRaft leader, grouped by that leader. | ||
| * | ||
| * A partition is forwarded iff it is a diskless topic that owns a local-log leg | ||
| * (switched / switch-pending / consolidating) and its real leader is a different, reachable | ||
| * broker. Pure-diskless partitions (control-plane only) and partitions this broker already leads | ||
| * stay local, preserving existing behaviour. | ||
| */ | ||
| def routeByLeader( | ||
| offsets: Map[TopicPartition, Long] | ||
| ): (Map[TopicPartition, Long], Map[Node, Map[TopicPartition, Long]]) = { | ||
| val local = mutable.Map.empty[TopicPartition, Long] | ||
| val forward = mutable.Map.empty[Node, mutable.Map[TopicPartition, Long]] | ||
| offsets.foreach { case (topicPartition, offset) => | ||
| leaderToForwardTo(topicPartition) match { | ||
| case Some(leader) => forward.getOrElseUpdate(leader, mutable.Map.empty) += (topicPartition -> offset) | ||
| case None => local += (topicPartition -> offset) | ||
| } | ||
| } | ||
| (local.toMap, forward.map { case (node, partitionOffsets) => node -> partitionOffsets.toMap }.toMap) | ||
| } | ||
|
|
||
| private def leaderToForwardTo(topicPartition: TopicPartition): Option[Node] = { | ||
| if (!metadataView.isDisklessTopic(topicPartition.topic) || !hasLocalLeg(topicPartition)) { | ||
| None | ||
| } else { | ||
| val leader = metadataCache.getPartitionLeaderEndpoint( | ||
| topicPartition.topic, topicPartition.partition, interBrokerListenerName) | ||
| if (leader.isPresent && !leader.get.isEmpty && leader.get.id != config.brokerId) Some(leader.get) | ||
| else None | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Whether the partition has a classic/consolidated prefix that lives in a local `UnifiedLog`, | ||
| * i.e. a leader-only local-log leg of `DeleteRecords` that is not broker-agnostic. Derived purely | ||
| * from KRaft metadata so it can be evaluated on any broker (a born consolidating partition may not | ||
| * yet have a local log on the leader; the leader resolves that when it runs the split). | ||
| */ | ||
| private def hasLocalLeg(topicPartition: TopicPartition): Boolean = { | ||
| val start = metadataView.getClassicToDisklessStartOffset(topicPartition) | ||
| start >= 0 || | ||
| start == PartitionRegistration.CLASSIC_TO_DISKLESS_SWITCH_PENDING || | ||
| (start == PartitionRegistration.NO_CLASSIC_TO_DISKLESS_START_OFFSET && | ||
| metadataView.isConsolidatingDisklessTopic(topicPartition.topic)) | ||
| } | ||
|
|
||
| /** | ||
| * Forward a `DeleteRecords` sub-request for `offsets` to `leader` and complete the returned future | ||
| * with the per-partition results. On a routing/network failure the future completes with a | ||
| * retriable `NOT_LEADER_OR_FOLLOWER` per partition so the AdminClient re-reads metadata and | ||
| * retries once leadership settles. | ||
| */ | ||
| def forward( | ||
| leader: Node, | ||
| offsets: Map[TopicPartition, Long], | ||
| timeoutMs: Int | ||
| ): CompletableFuture[Map[TopicPartition, DeleteRecordsPartitionResult]] = { | ||
| val future = new CompletableFuture[Map[TopicPartition, DeleteRecordsPartitionResult]]() | ||
| if (leader == null || leader.isEmpty) { | ||
| future.complete(errorResults(offsets.keys, Errors.NOT_LEADER_OR_FOLLOWER)) | ||
| } else { | ||
| queue.add(PendingForward(leader, offsets, timeoutMs, future)) | ||
| wakeup() | ||
| } | ||
| future | ||
| } | ||
|
|
||
| private def errorResults( | ||
| partitions: Iterable[TopicPartition], | ||
| error: Errors | ||
| ): Map[TopicPartition, DeleteRecordsPartitionResult] = { | ||
| partitions.map { tp => | ||
| tp -> new DeleteRecordsPartitionResult() | ||
| .setPartitionIndex(tp.partition) | ||
| .setLowWatermark(DeleteRecordsResponse.INVALID_LOW_WATERMARK) | ||
| .setErrorCode(error.code) | ||
| }.toMap | ||
| } | ||
|
|
||
| override def generateRequests(): util.Collection[RequestAndCompletionHandler] = { | ||
| val requests = new util.ArrayList[RequestAndCompletionHandler]() | ||
| val now = time.milliseconds() | ||
| var pending = queue.poll() | ||
| while (pending != null) { | ||
| requests.add(buildRequest(pending, now)) | ||
| pending = queue.poll() | ||
| } | ||
| requests | ||
| } | ||
|
|
||
| private def buildRequest(pending: PendingForward, now: Long): RequestAndCompletionHandler = { | ||
| val topics = new util.LinkedHashMap[String, DeleteRecordsTopic]() | ||
| pending.offsets.foreach { case (tp, offset) => | ||
| val topic = topics.computeIfAbsent(tp.topic, name => new DeleteRecordsTopic().setName(name)) | ||
| topic.partitions().add(new DeleteRecordsPartition() | ||
| .setPartitionIndex(tp.partition) | ||
| .setOffset(offset)) | ||
| } | ||
| val data = new DeleteRecordsRequestData() | ||
| .setTopics(new util.ArrayList[DeleteRecordsTopic](topics.values())) | ||
| .setTimeoutMs(pending.timeoutMs) | ||
| new RequestAndCompletionHandler( | ||
| now, | ||
| pending.leader, | ||
| new DeleteRecordsRequest.Builder(data), | ||
| completionHandler(pending) | ||
| ) | ||
| } | ||
|
|
||
| private def completionHandler(pending: PendingForward): RequestCompletionHandler = new RequestCompletionHandler { | ||
| override def onComplete(response: ClientResponse): Unit = { | ||
| try { | ||
| if (response.authenticationException != null) { | ||
| error(s"DeleteRecords forwarding to ${pending.leader} failed with an authentication error", response.authenticationException) | ||
| pending.future.complete(errorResults(pending.offsets.keys, Errors.forException(response.authenticationException))) | ||
| } else if (response.versionMismatch != null) { | ||
| error(s"DeleteRecords forwarding to ${pending.leader} failed with a version mismatch", response.versionMismatch) | ||
| pending.future.complete(errorResults(pending.offsets.keys, Errors.UNKNOWN_SERVER_ERROR)) | ||
| } else if (response.wasDisconnected || response.wasTimedOut) { | ||
| warn(s"DeleteRecords forwarding to ${pending.leader} failed with a network error " + | ||
| s"(disconnected=${response.wasDisconnected}, timedOut=${response.wasTimedOut}); the client will retry") | ||
| pending.future.complete(errorResults(pending.offsets.keys, Errors.NOT_LEADER_OR_FOLLOWER)) | ||
| } else { | ||
| pending.future.complete(parseResponse(pending, response.responseBody.asInstanceOf[DeleteRecordsResponse])) | ||
| } | ||
| } catch { | ||
| case e: Throwable => | ||
| error(s"Failed to handle the forwarded DeleteRecords response from ${pending.leader}", e) | ||
| pending.future.complete(errorResults(pending.offsets.keys, Errors.UNKNOWN_SERVER_ERROR)) | ||
| } finally { | ||
| wakeup() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def parseResponse( | ||
| pending: PendingForward, | ||
| response: DeleteRecordsResponse | ||
| ): Map[TopicPartition, DeleteRecordsPartitionResult] = { | ||
| val results = mutable.Map.empty[TopicPartition, DeleteRecordsPartitionResult] | ||
| for (topicResult <- response.data.topics.asScala; partitionResult <- topicResult.partitions.asScala) { | ||
| val tp = new TopicPartition(topicResult.name, partitionResult.partitionIndex) | ||
| results += tp -> new DeleteRecordsPartitionResult() | ||
| .setPartitionIndex(partitionResult.partitionIndex) | ||
| .setLowWatermark(partitionResult.lowWatermark) | ||
| .setErrorCode(partitionResult.errorCode) | ||
| } | ||
| // Any partition the leader did not answer for is reported as retriable so the client retries. | ||
| pending.offsets.keys.filterNot(results.contains).foreach { tp => | ||
| results += tp -> new DeleteRecordsPartitionResult() | ||
| .setPartitionIndex(tp.partition) | ||
| .setLowWatermark(DeleteRecordsResponse.INVALID_LOW_WATERMARK) | ||
| .setErrorCode(Errors.NOT_LEADER_OR_FOLLOWER.code) | ||
| } | ||
| results.toMap | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't preserve the principal when we forward these requests. This may result in
TOPIC_AUTHORIZATION_FAILEDwhen the request is performed from the broker. AFAIU Kafka has the envelop mechanism for preserving the original principalThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we use this class in KafkaApis.handleDeleteRecordsRequest, which does an authorization, so it should be gated. Regarding the
TOPIC_AUTHORIZATION_FAILEDerror I'm not sure how can we get this if we forward with the brokers own ID? Should we assume that broker principals may not have permission to delete records?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed this 🤔 I mean, it's possible to configure brokers to have only
CLUSTER_ACTION, for example, which won't allowDeleteRecords.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can address this as a followup, though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's probably a bigger change, we can keep this PR to the minimal.