-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-15859 Make RemoteListOffsets call an async operation (#16602)
This is the part-2 of the KIP-1075 To find the offset for a given timestamp, ListOffsets API is used by the client. When the topic is enabled with remote storage, then we have to fetch the remote indexes such as offset-index and time-index to serve the query. Also, the ListOffsets request can contain the query for multiple topics/partitions. The time taken to read the indexes from remote storage is non-deterministic and the query is handled by the request-handler threads. If there are multiple LIST_OFFSETS queries and most of the request-handler threads are busy in reading the data from remote storage, then the other high-priority requests such as FETCH and PRODUCE might starve and be queued. This can lead to higher latency in producing/consuming messages. In this patch, we have introduced a delayed operation for remote list-offsets call. If the timestamp need to be searched in the remote-storage, then the request-handler threads will pass-on the request to the remote-log-reader threads. And, the request gets handled in asynchronous fashion. Covered the patch with unit and integration tests. Reviewers: Satish Duggana <[email protected]>, Luke Chen <[email protected]>, Chia-Ping Tsai <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,271 additions
and
202 deletions.
There are no files selected for viewing
This file contains 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
79 changes: 79 additions & 0 deletions
79
core/src/main/java/kafka/log/remote/RemoteLogOffsetReader.java
This file contains 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,79 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package kafka.log.remote; | ||
|
||
import org.apache.kafka.common.TopicPartition; | ||
import org.apache.kafka.common.record.FileRecords; | ||
import org.apache.kafka.storage.internals.epoch.LeaderEpochFileCache; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import scala.Option; | ||
import scala.compat.java8.OptionConverters; | ||
import scala.util.Either; | ||
import scala.util.Left; | ||
import scala.util.Right; | ||
|
||
public class RemoteLogOffsetReader implements Callable<Void> { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteLogOffsetReader.class); | ||
private final RemoteLogManager rlm; | ||
private final TopicPartition tp; | ||
private final long timestamp; | ||
private final long startingOffset; | ||
private final LeaderEpochFileCache leaderEpochCache; | ||
private final Supplier<Option<FileRecords.TimestampAndOffset>> searchInLocalLog; | ||
private final Consumer<Either<Exception, Option<FileRecords.TimestampAndOffset>>> callback; | ||
|
||
public RemoteLogOffsetReader(RemoteLogManager rlm, | ||
TopicPartition tp, | ||
long timestamp, | ||
long startingOffset, | ||
LeaderEpochFileCache leaderEpochCache, | ||
Supplier<Option<FileRecords.TimestampAndOffset>> searchInLocalLog, | ||
Consumer<Either<Exception, Option<FileRecords.TimestampAndOffset>>> callback) { | ||
this.rlm = rlm; | ||
this.tp = tp; | ||
this.timestamp = timestamp; | ||
this.startingOffset = startingOffset; | ||
this.leaderEpochCache = leaderEpochCache; | ||
this.searchInLocalLog = searchInLocalLog; | ||
this.callback = callback; | ||
} | ||
|
||
@Override | ||
public Void call() throws Exception { | ||
Either<Exception, Option<FileRecords.TimestampAndOffset>> result; | ||
try { | ||
// If it is not found in remote storage, then search in the local storage starting with local log start offset. | ||
Option<FileRecords.TimestampAndOffset> timestampAndOffsetOpt = | ||
OptionConverters.toScala(rlm.findOffsetByTimestamp(tp, timestamp, startingOffset, leaderEpochCache)) | ||
.orElse(searchInLocalLog::get); | ||
result = Right.apply(timestampAndOffsetOpt); | ||
} catch (Exception e) { | ||
// NOTE: All the exceptions from the secondary storage are catched instead of only the KafkaException. | ||
LOGGER.error("Error occurred while reading the remote log offset for {}", tp, e); | ||
result = Left.apply(e); | ||
} | ||
callback.accept(result); | ||
return null; | ||
} | ||
} |
This file contains 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 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 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 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,38 @@ | ||
/** | ||
* 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. | ||
*/ | ||
package kafka.log | ||
|
||
import org.apache.kafka.common.errors.ApiException | ||
import org.apache.kafka.common.record.FileRecords.TimestampAndOffset | ||
|
||
import java.util.concurrent.{CompletableFuture, Future} | ||
|
||
case class OffsetResultHolder(timestampAndOffsetOpt: Option[TimestampAndOffset], | ||
futureHolderOpt: Option[AsyncOffsetReadFutureHolder[Either[Exception, Option[TimestampAndOffset]]]] = None) { | ||
|
||
var maybeOffsetsError: Option[ApiException] = None | ||
var lastFetchableOffset: Option[Long] = None | ||
} | ||
|
||
/** | ||
* A remote log offset read task future holder. It contains two futures: | ||
* 1. JobFuture - Use this future to cancel the running job. | ||
* 2. TaskFuture - Use this future to get the result of the job/computation. | ||
*/ | ||
case class AsyncOffsetReadFutureHolder[T](jobFuture: Future[Void], taskFuture: CompletableFuture[T]) { | ||
|
||
} |
This file contains 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.