Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c08b21e
Add guard against returning offsets when HW has not caught up
mumrah Nov 30, 2018
4f1722d
Don't do the HW check when in unclean leader election mode
mumrah Nov 30, 2018
2767f70
Unit tests passing
mumrah Dec 3, 2018
3e75fdf
Make broker and client behavior the same
mumrah Dec 3, 2018
eb068a0
Merge remote-tracking branch 'origin/trunk' into KAFKA-2334
mumrah Dec 3, 2018
3d2f66a
Merge remote-tracking branch 'apache/trunk' into KAFKA-2334
mumrah Dec 3, 2018
70d43d7
Revert "Make broker and client behavior the same"
mumrah Dec 3, 2018
1a0a8c5
Add ASL to new class
mumrah Dec 4, 2018
b76c543
Feedback from PR
mumrah Dec 5, 2018
048b1f7
New protocol version (KAFKA_2_2_IV1)
mumrah Dec 6, 2018
ae075e7
Merge remote-tracking branch 'apache/trunk' into KAFKA-2334
mumrah Dec 6, 2018
8f0e8f4
high-water mark -> high watermark
mumrah Dec 6, 2018
c7336cb
Reverse the logic for the error case (oops)
mumrah Dec 6, 2018
1b42863
Add unit test for offset errors
mumrah Dec 7, 2018
ce40782
Fix scala compile error
mumrah Dec 7, 2018
ea49519
Fix another scala compile error
mumrah Dec 7, 2018
47cf1c7
Fix some comments
mumrah Dec 10, 2018
6a56493
Only do the check for "latest" offset requests
mumrah Dec 10, 2018
31de57b
Throw the new error instead of return None in one case, code reorg
mumrah Dec 11, 2018
ad61063
Style feedback from PR
mumrah Dec 12, 2018
ca36eba
More unit tests, cover an additional error case
mumrah Dec 13, 2018
53dd98b
Clean up some comments
mumrah Dec 13, 2018
9e7a667
Remove LSO reference
mumrah Dec 14, 2018
54d8dd5
Another LSO reference
mumrah Dec 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.common.errors;

/**
* Indicates that the leader is not able to guarantee monotonically increasing offsets
* due to a recent leader election and high-water mark lag
*/
public class OffsetNotAvailableException extends RetriableException {
private static final long serialVersionUID = 1L;

public OffsetNotAvailableException(String message) {
super(message);
}

public OffsetNotAvailableException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.apache.kafka.common.errors.NotEnoughReplicasException;
import org.apache.kafka.common.errors.NotLeaderForPartitionException;
import org.apache.kafka.common.errors.OffsetMetadataTooLarge;
import org.apache.kafka.common.errors.OffsetNotAvailableException;
import org.apache.kafka.common.errors.OffsetOutOfRangeException;
import org.apache.kafka.common.errors.OperationNotAttemptedException;
import org.apache.kafka.common.errors.OutOfOrderSequenceException;
Expand Down Expand Up @@ -290,7 +291,10 @@ public enum Errors {
UNSUPPORTED_COMPRESSION_TYPE(76, "The requesting client does not support the compression type of given partition.",
UnsupportedCompressionTypeException::new),
STALE_BROKER_EPOCH(77, "Broker epoch has changed",
StaleBrokerEpochException::new);
StaleBrokerEpochException::new),
OFFSET_NOT_AVAILABLE(78, "The leader high-water mark has not caught up from a recent leader " +
"election so the offsets cannot be guaranteed to be monotonically increasing",
OffsetNotAvailableException::new);

private static final Logger log = LoggerFactory.getLogger(Errors.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ public class ListOffsetRequest extends AbstractRequest {
ISOLATION_LEVEL,
TOPICS_V4);

// V5 bump to include new possible error code

@cmccabe cmccabe Dec 7, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be specific and say that the new error code is OFFSET_NOT_AVAILABLE

private static final Schema LIST_OFFSET_REQUEST_V5 = LIST_OFFSET_REQUEST_V4;

public static Schema[] schemaVersions() {
return new Schema[] {LIST_OFFSET_REQUEST_V0, LIST_OFFSET_REQUEST_V1, LIST_OFFSET_REQUEST_V2,
LIST_OFFSET_REQUEST_V3, LIST_OFFSET_REQUEST_V4};
LIST_OFFSET_REQUEST_V3, LIST_OFFSET_REQUEST_V4, LIST_OFFSET_REQUEST_V5};
}

private final int replicaId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
* - {@link Errors#UNKNOWN_TOPIC_OR_PARTITION} If the broker does not have metadata for a topic or partition
* - {@link Errors#KAFKA_STORAGE_ERROR} If the log directory for one of the requested partitions is offline
* - {@link Errors#UNKNOWN_SERVER_ERROR} For any unexpected errors
* - {@link Errors#LEADER_NOT_AVAILABLE} The leader's HW has not caught up after recent election (v4 protocol)
* - {@link Errors#OFFSET_NOT_AVAILABLE} The leader's HW has not caught up after recent election (v5+ protocol)
*/
public class ListOffsetResponse extends AbstractResponse {
public static final long UNKNOWN_TIMESTAMP = -1L;
Expand Down Expand Up @@ -125,9 +127,11 @@ public class ListOffsetResponse extends AbstractResponse {
THROTTLE_TIME_MS,
TOPICS_V4);

private static final Schema LIST_OFFSET_RESPONSE_V5 = LIST_OFFSET_RESPONSE_V4;

public static Schema[] schemaVersions() {
return new Schema[] {LIST_OFFSET_RESPONSE_V0, LIST_OFFSET_RESPONSE_V1, LIST_OFFSET_RESPONSE_V2,
LIST_OFFSET_RESPONSE_V3, LIST_OFFSET_RESPONSE_V4};
LIST_OFFSET_RESPONSE_V3, LIST_OFFSET_RESPONSE_V4, LIST_OFFSET_RESPONSE_V5};
}

public static final class PartitionData {
Expand Down
22 changes: 21 additions & 1 deletion core/src/main/scala/kafka/cluster/Partition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,8 @@ class Partition(val topicPartition: TopicPartition,
def fetchOffsetForTimestamp(timestamp: Long,
isolationLevel: Option[IsolationLevel],
currentLeaderEpoch: Optional[Integer],
fetchOnlyFromLeader: Boolean): Option[TimestampAndOffset] = inReadLock(leaderIsrUpdateLock) {
fetchOnlyFromLeader: Boolean,
isFromClient: Boolean): Option[TimestampAndOffset] = inReadLock(leaderIsrUpdateLock) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may be able to drop this argument. When the isolation level is read_uncommitted, then we are limited by the high watermark. For followers, isolation level will be None.

// decide whether to only fetch from leader
val localReplica = localReplicaWithEpochOrException(currentLeaderEpoch, fetchOnlyFromLeader)

Expand All @@ -815,6 +816,25 @@ class Partition(val topicPartition: TopicPartition,
case None => localReplica.logEndOffset.messageOffset
}

// Only actually check the HW if this is a client request and _not_ while unclean leader
// election is enabled
val shouldCheckHW = {
!logManager.currentDefaultConfig.uncleanLeaderElectionEnable &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd want to do this validation even if unclean leader election is enabled. The point that the KIP was making is that high watermark monotonicity can be violated if unclean leader election is enabled. But we'd still want to minimize such violations to cases when an unclean leader election has actually happened.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification, I misunderstood the KIP. I'll take this check out.

isFromClient &&
leaderEpochStartOffsetOpt.isDefined
}

if(shouldCheckHW) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typically we prefer a space after if

// Wait until the HW has caught up with the start offset from this epoch
if(leaderEpochStartOffsetOpt.get > localReplica.highWatermark.messageOffset) {
throw Errors.OFFSET_NOT_AVAILABLE.exception(s"Failed to fetch offsets for " +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this exception apply for all cases or only when the offset for latest timestamp is requested?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. Presumably it is possible for a timestamp query to find an offset between the log end offset and the high watermark as well. I guess the point is that we cannot trust the high watermark upper bound until we can ensure that it cannot have gone backwards. Maybe the only offset you can safely query without this validation is the earliest?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think refusing to return any offset until the HW has caught up is a reasonable solution.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm seems like this behavior was changed then in 6a56493?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! After talking through the various cases with @hachikuji, we decided to only enforce the check on latest timestamp offset requests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Would be good to document the change in the KIP, given the following is now out-dated:

The KIP-207 behavior applies to all ListOffsetsRequests, whether they are for the latest offset, the earliest offset, or a time-based offset

s"partition $topicPartition with leader epoch ${currentLeaderEpoch.get} as this partition's " +
s"high-water mark (${localReplica.highWatermark.messageOffset}) is lagging behind its " +
s"LEO (${leaderEpochStartOffsetOpt.get}).")
}
}


if (timestamp == ListOffsetRequest.LATEST_TIMESTAMP) {
Some(new TimestampAndOffset(RecordBatch.NO_TIMESTAMP, lastFetchableOffset, Optional.of(leaderEpoch)))
} else {
Expand Down
35 changes: 25 additions & 10 deletions core/src/main/scala/kafka/server/KafkaApis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ class KafkaApis(val requestChannel: RequestChannel,
val correlationId = request.header.correlationId
val clientId = request.header.clientId
val offsetRequest = request.body[ListOffsetRequest]
val isV5Schema = request.header.apiVersion() >= 5
Comment thread
mumrah marked this conversation as resolved.
Outdated

val (authorizedRequestInfo, unauthorizedRequestInfo) = offsetRequest.partitionTimestamps.asScala.partition {
case (topicPartition, _) => authorize(request.session, Describe, Resource(Topic, topicPartition.topic, LITERAL))
Expand All @@ -827,9 +828,19 @@ class KafkaApis(val requestChannel: RequestChannel,
ListOffsetResponse.UNKNOWN_OFFSET,
Optional.empty()))
} else {

def buildErrorResponse(e: Errors): (TopicPartition, ListOffsetResponse.PartitionData) = {
(topicPartition, new ListOffsetResponse.PartitionData(
e,
ListOffsetResponse.UNKNOWN_TIMESTAMP,
ListOffsetResponse.UNKNOWN_OFFSET,
Optional.empty()))
}

try {
val fetchOnlyFromLeader = offsetRequest.replicaId != ListOffsetRequest.DEBUGGING_REPLICA_ID
val isolationLevelOpt = if (offsetRequest.replicaId == ListOffsetRequest.CONSUMER_REPLICA_ID)
val isClientRequest = offsetRequest.replicaId == ListOffsetRequest.CONSUMER_REPLICA_ID
val isolationLevelOpt = if (isClientRequest)
Some(offsetRequest.isolationLevel)
else
None
Expand All @@ -838,7 +849,8 @@ class KafkaApis(val requestChannel: RequestChannel,
partitionData.timestamp,
isolationLevelOpt,
partitionData.currentLeaderEpoch,
fetchOnlyFromLeader)
fetchOnlyFromLeader,
isFromClient = isClientRequest)

val response = foundOpt match {
case Some(found) =>
Expand All @@ -859,16 +871,19 @@ class KafkaApis(val requestChannel: RequestChannel,
_ : UnsupportedForMessageFormatException) =>
debug(s"Offset request with correlation id $correlationId from client $clientId on " +
s"partition $topicPartition failed due to ${e.getMessage}")
(topicPartition, new ListOffsetResponse.PartitionData(Errors.forException(e),
ListOffsetResponse.UNKNOWN_TIMESTAMP,
ListOffsetResponse.UNKNOWN_OFFSET,
Optional.empty()))
buildErrorResponse(Errors.forException(e))

// Only V5 and newer ListOffset calls should get OFFSET_NOT_AVAILABLE
case e: OffsetNotAvailableException =>
if(isV5Schema) {
buildErrorResponse(Errors.forException(e))
Comment thread
hachikuji marked this conversation as resolved.
} else {
buildErrorResponse(Errors.LEADER_NOT_AVAILABLE)
}

case e: Throwable =>
error("Error while responding to offset request", e)
(topicPartition, new ListOffsetResponse.PartitionData(Errors.forException(e),
ListOffsetResponse.UNKNOWN_TIMESTAMP,
ListOffsetResponse.UNKNOWN_OFFSET,
Optional.empty()))
buildErrorResponse(Errors.forException(e))
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/scala/kafka/server/ReplicaFetcherThread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kafka.server

import java.util.Optional

import kafka.api
import kafka.api._
import kafka.cluster.BrokerEndPoint
import kafka.log.LogAppendInfo
Expand Down Expand Up @@ -80,7 +81,7 @@ class ReplicaFetcherThread(name: String,

// Visible for testing
private[server] val listOffsetRequestVersion: Short =
if (brokerConfig.interBrokerProtocolVersion >= KAFKA_2_1_IV1) 4
if (brokerConfig.interBrokerProtocolVersion >= KAFKA_2_1_IV1) 5
Comment thread
mumrah marked this conversation as resolved.
Outdated
else if (brokerConfig.interBrokerProtocolVersion >= KAFKA_2_0_IV1) 3
else if (brokerConfig.interBrokerProtocolVersion >= KAFKA_0_11_0_IV0) 2
else if (brokerConfig.interBrokerProtocolVersion >= KAFKA_0_10_1_IV2) 1
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/scala/kafka/server/ReplicaManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,10 @@ class ReplicaManager(val config: KafkaConfig,
timestamp: Long,
isolationLevel: Option[IsolationLevel],
currentLeaderEpoch: Optional[Integer],
fetchOnlyFromLeader: Boolean): Option[TimestampAndOffset] = {
fetchOnlyFromLeader: Boolean,
isFromClient: Boolean): Option[TimestampAndOffset] = {
val partition = getPartitionOrException(topicPartition, expectLeader = fetchOnlyFromLeader)
partition.fetchOffsetForTimestamp(timestamp, isolationLevel, currentLeaderEpoch, fetchOnlyFromLeader)
partition.fetchOffsetForTimestamp(timestamp, isolationLevel, currentLeaderEpoch, fetchOnlyFromLeader, isFromClient)
}

def legacyFetchOffsetsForTimestamp(topicPartition: TopicPartition,
Expand Down
Loading