|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package kafka.server.share; |
| 18 | + |
| 19 | +import kafka.cluster.Partition; |
| 20 | +import kafka.server.ReplicaManager; |
| 21 | + |
| 22 | +import org.apache.kafka.common.IsolationLevel; |
| 23 | +import org.apache.kafka.common.TopicIdPartition; |
| 24 | +import org.apache.kafka.common.errors.NotLeaderOrFollowerException; |
| 25 | +import org.apache.kafka.common.errors.OffsetNotAvailableException; |
| 26 | +import org.apache.kafka.common.record.internal.FileRecords; |
| 27 | +import org.apache.kafka.common.requests.ListOffsetsRequest; |
| 28 | +import org.apache.kafka.server.partition.PartitionListener; |
| 29 | +import org.apache.kafka.server.share.PartitionMetadataProvider; |
| 30 | +import org.apache.kafka.server.storage.log.FetchIsolation; |
| 31 | +import org.apache.kafka.storage.internals.log.LogOffsetMetadata; |
| 32 | +import org.apache.kafka.storage.internals.log.LogOffsetSnapshot; |
| 33 | + |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +import scala.Some; |
| 40 | + |
| 41 | +/** |
| 42 | + * Implementation of {@link PartitionMetadataProvider} backed by {@link ReplicaManager}. |
| 43 | + */ |
| 44 | +public class ReplicaManagerPartitionMetadataProvider implements PartitionMetadataProvider { |
| 45 | + |
| 46 | + private static final Logger log = LoggerFactory.getLogger(ReplicaManagerPartitionMetadataProvider.class); |
| 47 | + |
| 48 | + private final ReplicaManager replicaManager; |
| 49 | + |
| 50 | + public ReplicaManagerPartitionMetadataProvider(ReplicaManager replicaManager) { |
| 51 | + this.replicaManager = replicaManager; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public long offsetForEarliestTimestamp(TopicIdPartition topicIdPartition, int leaderEpoch) { |
| 56 | + Optional<FileRecords.TimestampAndOffset> timestampAndOffset = replicaManager.fetchOffsetForTimestamp( |
| 57 | + topicIdPartition.topicPartition(), ListOffsetsRequest.EARLIEST_TIMESTAMP, scala.Option.empty(), |
| 58 | + Optional.of(leaderEpoch), true).timestampAndOffsetOpt(); |
| 59 | + if (timestampAndOffset.isEmpty()) { |
| 60 | + throw new OffsetNotAvailableException("Offset for earliest timestamp not found for topic partition: " + topicIdPartition); |
| 61 | + } |
| 62 | + return timestampAndOffset.get().offset; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public long offsetForLatestTimestamp(TopicIdPartition topicIdPartition, int leaderEpoch) { |
| 67 | + Optional<FileRecords.TimestampAndOffset> timestampAndOffset = replicaManager.fetchOffsetForTimestamp( |
| 68 | + topicIdPartition.topicPartition(), ListOffsetsRequest.LATEST_TIMESTAMP, new Some<>(IsolationLevel.READ_UNCOMMITTED), |
| 69 | + Optional.of(leaderEpoch), true).timestampAndOffsetOpt(); |
| 70 | + if (timestampAndOffset.isEmpty()) { |
| 71 | + throw new OffsetNotAvailableException("Offset for latest timestamp not found for topic partition: " + topicIdPartition); |
| 72 | + } |
| 73 | + return timestampAndOffset.get().offset; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public long offsetForTimestamp(TopicIdPartition topicIdPartition, long timestamp, int leaderEpoch) { |
| 78 | + Optional<FileRecords.TimestampAndOffset> timestampAndOffset = replicaManager.fetchOffsetForTimestamp( |
| 79 | + topicIdPartition.topicPartition(), timestamp, new Some<>(IsolationLevel.READ_UNCOMMITTED), |
| 80 | + Optional.of(leaderEpoch), true).timestampAndOffsetOpt(); |
| 81 | + if (timestampAndOffset.isEmpty()) { |
| 82 | + throw new OffsetNotAvailableException("Offset for timestamp " + timestamp + " not found for topic partition: " + topicIdPartition); |
| 83 | + } |
| 84 | + return timestampAndOffset.get().offset; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Optional<LogOffsetMetadata> endOffsetMetadata(TopicIdPartition topicIdPartition, FetchIsolation isolation) { |
| 89 | + Partition partition = leaderPartition(topicIdPartition); |
| 90 | + LogOffsetSnapshot offsetSnapshot = partition.fetchOffsetSnapshot(Optional.empty(), true); |
| 91 | + if (isolation == FetchIsolation.LOG_END) |
| 92 | + return Optional.of(offsetSnapshot.logEndOffset()); |
| 93 | + else if (isolation == FetchIsolation.HIGH_WATERMARK) |
| 94 | + return Optional.of(offsetSnapshot.highWatermark()); |
| 95 | + else |
| 96 | + return Optional.of(offsetSnapshot.lastStableOffset()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int leaderEpoch(TopicIdPartition topicIdPartition) { |
| 101 | + return leaderPartition(topicIdPartition).getLeaderEpoch(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean addPartitionListener(TopicIdPartition topicIdPartition, PartitionListener listener) { |
| 106 | + return replicaManager.maybeAddListener(topicIdPartition.topicPartition(), listener); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void removePartitionListener(TopicIdPartition topicIdPartition, PartitionListener listener) { |
| 111 | + replicaManager.removeListener(topicIdPartition.topicPartition(), listener); |
| 112 | + } |
| 113 | + |
| 114 | + private Partition leaderPartition(TopicIdPartition topicIdPartition) { |
| 115 | + Partition partition = replicaManager.getPartitionOrException(topicIdPartition.topicPartition()); |
| 116 | + if (!partition.isLeader()) { |
| 117 | + log.debug("The broker is not the leader for topic partition: {}", topicIdPartition.topicPartition()); |
| 118 | + throw new NotLeaderOrFollowerException(); |
| 119 | + } |
| 120 | + return partition; |
| 121 | + } |
| 122 | +} |
0 commit comments