Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 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
Expand Up @@ -811,7 +811,9 @@ private void handleListOffsetResponse(Map<TopicPartition, ListOffsetRequest.Part
"is before 0.10.0", topicPartition);
} else if (error == Errors.NOT_LEADER_FOR_PARTITION ||
error == Errors.REPLICA_NOT_AVAILABLE ||
error == Errors.KAFKA_STORAGE_ERROR) {
error == Errors.KAFKA_STORAGE_ERROR ||
error == Errors.OFFSET_NOT_AVAILABLE ||
Comment thread
mumrah marked this conversation as resolved.
error == Errors.LEADER_NOT_AVAILABLE) {
log.debug("Attempt to fetch offsets for partition {} failed due to {}, retrying.",
topicPartition, error);
partitionsToRetry.add(topicPartition);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 the high watermark lagging behind the epoch start offset after a recent leader election
*/
public class OffsetNotAvailableException extends RetriableException {
private static final long serialVersionUID = 1L;

public OffsetNotAvailableException(String message) {
super(message);
}
}
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 watermark 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 (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
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,45 @@ public void testUpdateFetchPositionResetToLatestOffset() {
assertEquals(5, subscriptions.position(tp0).longValue());
}

/**
* Make sure the client behaves appropriately when receiving an exception for unavailable offsets
*/
@Test
public void testFetchOffsetErrors() {
subscriptions.assignFromUser(singleton(tp0));
subscriptions.requestOffsetReset(tp0, OffsetResetStrategy.LATEST);

// Fail with OFFSET_NOT_AVAILABLE
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.OFFSET_NOT_AVAILABLE, 1L, 5L), false);
fetcher.resetOffsetsIfNeeded();
consumerClient.pollNoWakeup();
assertFalse(subscriptions.hasValidPosition(tp0));
assertTrue(subscriptions.isOffsetResetNeeded(tp0));
assertFalse(subscriptions.isFetchable(tp0));

// Fail with LEADER_NOT_AVAILABLE
time.sleep(retryBackoffMs);
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.LEADER_NOT_AVAILABLE, 1L, 5L), false);
fetcher.resetOffsetsIfNeeded();
consumerClient.pollNoWakeup();
assertFalse(subscriptions.hasValidPosition(tp0));
assertTrue(subscriptions.isOffsetResetNeeded(tp0));
assertFalse(subscriptions.isFetchable(tp0));

// Back to normal
time.sleep(retryBackoffMs);
client.prepareResponse(listOffsetRequestMatcher(ListOffsetRequest.LATEST_TIMESTAMP),
listOffsetResponse(Errors.NONE, 1L, 5L), false);
fetcher.resetOffsetsIfNeeded();
consumerClient.pollNoWakeup();
assertTrue(subscriptions.hasValidPosition(tp0));
assertFalse(subscriptions.isOffsetResetNeeded(tp0));
assertTrue(subscriptions.isFetchable(tp0));
assertEquals(subscriptions.position(tp0).longValue(), 5L);
}

@Test
public void testListOffsetsSendsIsolationLevel() {
for (final IsolationLevel isolationLevel : IsolationLevel.values()) {
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/scala/kafka/api/ApiVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ object ApiVersion {
KAFKA_2_1_IV2,
// Introduced broker generation (KIP-380), and
// LeaderAdnIsrRequest V2, UpdateMetadataRequest V5, StopReplicaRequest V1
KAFKA_2_2_IV0
KAFKA_2_2_IV0,
// New error code for ListOffsets when a new leader is lagging behind former HW (KIP-207)
KAFKA_2_2_IV1
Comment thread
hachikuji marked this conversation as resolved.
)

// Map keys are the union of the short and full versions
Expand Down Expand Up @@ -289,6 +291,13 @@ case object KAFKA_2_2_IV0 extends DefaultApiVersion {
val id: Int = 20
}

case object KAFKA_2_2_IV1 extends DefaultApiVersion {
val shortVersion: String = "2.2"
val subVersion = "IV1"
val recordVersion = RecordVersion.V2
val id: Int = 21
}

object ApiVersionValidator extends Validator {

override def ensureValid(name: String, value: Any): Unit = {
Expand Down
35 changes: 27 additions & 8 deletions core/src/main/scala/kafka/cluster/Partition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -817,17 +817,36 @@ class Partition(val topicPartition: TopicPartition,
case None => localReplica.logEndOffset.messageOffset
}

if (timestamp == ListOffsetRequest.LATEST_TIMESTAMP) {
Some(new TimestampAndOffset(RecordBatch.NO_TIMESTAMP, lastFetchableOffset, Optional.of(leaderEpoch)))
val epochLogString = if(currentLeaderEpoch.isPresent) {
s"epoch ${currentLeaderEpoch.get}"
} else {
def allowed(timestampOffset: TimestampAndOffset): Boolean =
timestamp == ListOffsetRequest.EARLIEST_TIMESTAMP || timestampOffset.offset < lastFetchableOffset
"unknown epoch"
}

val fetchedOffset = logManager.getLog(topicPartition).flatMap { log =>
log.fetchOffsetByTimestamp(timestamp)
}
// Only consider throwing an error if we get a client request (isolationLevel is defined) and the start offset
// is lagging behind the high watermark
val maybeOffsetsError: Option[ApiException] = leaderEpochStartOffsetOpt
.filter(epochLSO => isolationLevel.isDefined && epochLSO > localReplica.highWatermark.messageOffset)

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: LSO annoyingly can be mean either "last stable offset" or "log start offset". How about just startOffset instead?

.map(epochLSO => Errors.OFFSET_NOT_AVAILABLE.exception(s"Failed to fetch offsets for " +
s"partition $topicPartition with leader $epochLogString as this partition's " +
s"high watermark (${localReplica.highWatermark.messageOffset}) is lagging behind the " +
s"LSO at the beginning of this epoch ($epochLSO)."))

def getOffsetByTimestamp: Option[TimestampAndOffset] = {
logManager.getLog(topicPartition).flatMap(log => log.fetchOffsetByTimestamp(timestamp))
}

fetchedOffset.filter(allowed)
// 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 ListOffsetRequest.LATEST_TIMESTAMP =>
maybeOffsetsError.map(e => throw e)
.orElse(Some(new TimestampAndOffset(RecordBatch.NO_TIMESTAMP, lastFetchableOffset, Optional.of(leaderEpoch))))
case ListOffsetRequest.EARLIEST_TIMESTAMP =>
getOffsetByTimestamp
case _ =>
getOffsetByTimestamp.filter(timestampAndOffset => timestampAndOffset.offset < lastFetchableOffset)
.orElse(maybeOffsetsError.map(e => throw e))
}
}

Expand Down
31 changes: 22 additions & 9 deletions core/src/main/scala/kafka/server/KafkaApis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -827,9 +827,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 Down Expand Up @@ -859,16 +869,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(request.header.apiVersion >= 5) {
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
4 changes: 3 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,8 @@ 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_2_IV1) 5
else if (brokerConfig.interBrokerProtocolVersion >= KAFKA_2_1_IV1) 4
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
18 changes: 18 additions & 0 deletions core/src/test/scala/unit/kafka/api/ApiVersionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package kafka.api

import org.apache.commons.collections.CollectionUtils
import org.apache.kafka.common.record.RecordVersion
import org.junit.Test
import org.junit.Assert._

import scala.collection.JavaConverters

class ApiVersionTest {

@Test
Expand Down Expand Up @@ -84,6 +87,21 @@ class ApiVersionTest {
assertEquals(KAFKA_2_1_IV0, ApiVersion("2.1-IV0"))
assertEquals(KAFKA_2_1_IV1, ApiVersion("2.1-IV1"))
assertEquals(KAFKA_2_1_IV2, ApiVersion("2.1-IV2"))

assertEquals(KAFKA_2_2_IV1, ApiVersion("2.2"))
assertEquals(KAFKA_2_2_IV0, ApiVersion("2.2-IV0"))
assertEquals(KAFKA_2_2_IV1, ApiVersion("2.2-IV1"))
}

@Test
def testApiVersionUniqueIds(): Unit = {
val allIds: Seq[Int] = ApiVersion.allVersions.map(apiVersion => {
apiVersion.id
})

val uniqueIds: Set[Int] = allIds.toSet

assertEquals(allIds.size, uniqueIds.size)
}

@Test
Expand Down
Loading