clolov commented on code in PR #18100:
URL: https://github.com/apache/kafka/pull/18100#discussion_r1877904591
##########
core/src/main/java/kafka/log/remote/RemoteLogOffsetReader.java:
##########
@@ -61,17 +60,18 @@ public RemoteLogOffsetReader(RemoteLogManager rlm,
@Override
public Void call() throws Exception {
- Either<Exception, Option<FileRecords.TimestampAndOffset>> result;
+ OffsetResultHolder.FileRecordsOrError 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);
+ result = new
OffsetResultHolder.FileRecordsOrError(Optional.empty(),
+ timestampAndOffsetOpt.isDefined() ?
Optional.of(timestampAndOffsetOpt.get()) : Optional.empty());
Review Comment:
Can't `timestampAndOffsetOpt.isDefined() ?
Optional.of(timestampAndOffsetOpt.get()) : Optional.empty()` be simplified to
just `timestampAndOffsetOpt`?
##########
core/src/main/scala/kafka/cluster/Partition.scala:
##########
@@ -1627,21 +1628,21 @@ class Partition(val topicPartition: TopicPartition,
def getOffsetByTimestamp: OffsetResultHolder = {
logManager.getLog(topicPartition)
.map(log => log.fetchOffsetByTimestamp(timestamp, remoteLogManager))
- .getOrElse(OffsetResultHolder(timestampAndOffsetOpt = None))
+ .getOrElse(new OffsetResultHolder(Optional.empty()))
}
// If we're in the lagging HW state after a leader election, throw
OffsetNotAvailable for "latest" offset
// or for a timestamp lookup that is beyond the last fetchable offset.
timestamp match {
case ListOffsetsRequest.LATEST_TIMESTAMP =>
maybeOffsetsError.map(e => throw e)
- .getOrElse(OffsetResultHolder(Some(new
TimestampAndOffset(RecordBatch.NO_TIMESTAMP, lastFetchableOffset,
Optional.of(leaderEpoch)))))
+ .getOrElse(new OffsetResultHolder(Optional.of(new
TimestampAndOffset(RecordBatch.NO_TIMESTAMP, lastFetchableOffset,
Optional.of(leaderEpoch)))))
case ListOffsetsRequest.EARLIEST_TIMESTAMP |
ListOffsetsRequest.EARLIEST_LOCAL_TIMESTAMP =>
getOffsetByTimestamp
case _ =>
val offsetResultHolder = getOffsetByTimestamp
- offsetResultHolder.maybeOffsetsError = maybeOffsetsError
- offsetResultHolder.lastFetchableOffset = Some(lastFetchableOffset)
+ offsetResultHolder.maybeOffsetsError(if (maybeOffsetsError.isDefined)
Optional.of(maybeOffsetsError.get) else Optional.empty())
Review Comment:
Same question
##########
storage/src/main/java/org/apache/kafka/storage/log/OffsetResultHolder.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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 org.apache.kafka.storage.log;
+
+import org.apache.kafka.common.errors.ApiException;
+import org.apache.kafka.common.record.FileRecords.TimestampAndOffset;
+
+import java.util.Objects;
+import java.util.Optional;
+
+public class OffsetResultHolder {
+
+ private Optional<TimestampAndOffset> timestampAndOffsetOpt;
+ private Optional<AsyncOffsetReadFutureHolder<FileRecordsOrError>>
futureHolderOpt;
+ private Optional<ApiException> maybeOffsetsError = Optional.empty();
+ private Optional<Long> lastFetchableOffset = Optional.empty();
+
+ public OffsetResultHolder() {
+ this(Optional.empty(), Optional.empty());
+ }
+
+ public OffsetResultHolder(
+ Optional<TimestampAndOffset> timestampAndOffsetOpt,
+ Optional<AsyncOffsetReadFutureHolder<FileRecordsOrError>>
futureHolderOpt
+ ) {
+ this.timestampAndOffsetOpt = timestampAndOffsetOpt;
+ this.futureHolderOpt = futureHolderOpt;
+ }
+
+ public OffsetResultHolder(Optional<TimestampAndOffset>
timestampAndOffsetOpt) {
Review Comment:
Is there a downside to defining a few more constructors which take the
concrete type and create an Optional internally? I believe this will save you a
lot of `Optional.of(new Something()` elsewhere, no?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]