-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-10427: Fetch snapshot #9553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,980
−35
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
277e6a5
KAFKA-10427: Basic fetch snapshot implementation
jsancio e648377
Implement FetchSnapshot response handling
jsancio 4ea3c9b
Include leader epoch in the FetchSnapshot request
jsancio 4609922
Fix FetchSnapsot request and response class
jsancio a96d16a
Implement better snapshot id validation in fetch response
jsancio 79fed61
Improve documentation
jsancio 33be3a4
Add position out of range error
jsancio 2393cf7
Add the remaining tests
jsancio 61de910
Merge remote-tracking branch 'upstream/trunk' into kafka-10427-fetch-…
jsancio cad8284
Invalid snapshot should back off instead of reporting success
jsancio 22133d1
Add missing parsing code
jsancio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
clients/src/main/java/org/apache/kafka/common/errors/SnapshotNotFoundException.java
This file contains hidden or 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,31 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public class SnapshotNotFoundException extends ApiException { | ||
|
|
||
| private static final long serialVersionUID = 1; | ||
|
|
||
| public SnapshotNotFoundException(String s) { | ||
| super(s); | ||
| } | ||
|
|
||
| public SnapshotNotFoundException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
clients/src/main/java/org/apache/kafka/common/requests/FetchSnapshotRequest.java
This file contains hidden or 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,97 @@ | ||
| /* | ||
| * 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.requests; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
| import java.util.function.UnaryOperator; | ||
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.common.message.FetchSnapshotRequestData; | ||
| import org.apache.kafka.common.message.FetchSnapshotResponseData; | ||
| import org.apache.kafka.common.protocol.ApiKeys; | ||
| import org.apache.kafka.common.protocol.Errors; | ||
| import org.apache.kafka.common.protocol.Message; | ||
|
|
||
| final public class FetchSnapshotRequest extends AbstractRequest { | ||
| public final FetchSnapshotRequestData data; | ||
|
|
||
| public FetchSnapshotRequest(FetchSnapshotRequestData data) { | ||
| super(ApiKeys.FETCH_SNAPSHOT, (short) (FetchSnapshotRequestData.SCHEMAS.length - 1)); | ||
| this.data = data; | ||
| } | ||
|
|
||
| @Override | ||
| public FetchSnapshotResponse getErrorResponse(int throttleTimeMs, Throwable e) { | ||
| return new FetchSnapshotResponse( | ||
| new FetchSnapshotResponseData() | ||
| .setThrottleTimeMs(throttleTimeMs) | ||
| .setErrorCode(Errors.forException(e).code()) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected Message data() { | ||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a FetchSnapshotRequestData with a single PartitionSnapshot for the topic partition. | ||
| * | ||
| * The partition index will already be populated when calling operator. | ||
| * | ||
| * @param topicPartition the topic partition to include | ||
| * @param operator unary operator responsible for populating all the appropriate fields | ||
| * @return the created fetch snapshot request data | ||
| */ | ||
| public static FetchSnapshotRequestData singleton( | ||
| TopicPartition topicPartition, | ||
| UnaryOperator<FetchSnapshotRequestData.PartitionSnapshot> operator | ||
| ) { | ||
| FetchSnapshotRequestData.PartitionSnapshot partitionSnapshot = operator.apply( | ||
| new FetchSnapshotRequestData.PartitionSnapshot().setPartition(topicPartition.partition()) | ||
| ); | ||
|
|
||
| return new FetchSnapshotRequestData() | ||
| .setTopics( | ||
| Collections.singletonList( | ||
| new FetchSnapshotRequestData.TopicSnapshot() | ||
| .setName(topicPartition.topic()) | ||
| .setPartitions(Collections.singletonList(partitionSnapshot)) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Finds the PartitionSnapshot for a given topic partition. | ||
| * | ||
| * @param data the fetch snapshot request data | ||
| * @param topicPartition the topic partition to find | ||
| * @return the request partition snapshot if found, otherwise an empty Optional | ||
| */ | ||
| public static Optional<FetchSnapshotRequestData.PartitionSnapshot> forTopicPartition( | ||
| FetchSnapshotRequestData data, | ||
| TopicPartition topicPartition | ||
| ) { | ||
| return data | ||
| .topics() | ||
| .stream() | ||
| .filter(topic -> topic.name().equals(topicPartition.topic())) | ||
| .flatMap(topic -> topic.partitions().stream()) | ||
| .filter(partition -> partition.partition() == topicPartition.partition()) | ||
| .findAny(); | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
clients/src/main/java/org/apache/kafka/common/requests/FetchSnapshotResponse.java
This file contains hidden or 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,124 @@ | ||
| /* | ||
| * 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.requests; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.UnaryOperator; | ||
| import org.apache.kafka.common.TopicPartition; | ||
| import org.apache.kafka.common.message.FetchSnapshotResponseData; | ||
| import org.apache.kafka.common.protocol.ApiKeys; | ||
| import org.apache.kafka.common.protocol.Errors; | ||
| import org.apache.kafka.common.protocol.Message; | ||
|
|
||
| final public class FetchSnapshotResponse extends AbstractResponse { | ||
| public final FetchSnapshotResponseData data; | ||
|
|
||
| public FetchSnapshotResponse(FetchSnapshotResponseData data) { | ||
| super(ApiKeys.FETCH_SNAPSHOT); | ||
|
|
||
| this.data = data; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<Errors, Integer> errorCounts() { | ||
| Map<Errors, Integer> errors = new HashMap<>(); | ||
|
|
||
| Errors topLevelError = Errors.forCode(data.errorCode()); | ||
| if (topLevelError != Errors.NONE) { | ||
| errors.put(topLevelError, 1); | ||
| } | ||
|
|
||
| for (FetchSnapshotResponseData.TopicSnapshot topicResponse : data.topics()) { | ||
| for (FetchSnapshotResponseData.PartitionSnapshot partitionResponse : topicResponse.partitions()) { | ||
| errors.compute(Errors.forCode(partitionResponse.errorCode()), | ||
| (error, count) -> count == null ? 1 : count + 1); | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| @Override | ||
| public int throttleTimeMs() { | ||
| return data.throttleTimeMs(); | ||
| } | ||
|
|
||
| @Override | ||
| protected Message data() { | ||
| return data; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a FetchSnapshotResponseData with a top level error. | ||
| * | ||
| * @param error the top level error | ||
| * @return the created fetch snapshot response data | ||
| */ | ||
| public static FetchSnapshotResponseData withTopError(Errors error) { | ||
| return new FetchSnapshotResponseData().setErrorCode(error.code()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a FetchSnapshotResponseData with a single PartitionSnapshot for the topic partition. | ||
| * | ||
| * The partition index will already by populated when calling operator. | ||
| * | ||
| * @param topicPartition the topic partition to include | ||
| * @param operator unary operator responsible for populating all of the appropriate fields | ||
| * @return the created fetch snapshot response data | ||
| */ | ||
| public static FetchSnapshotResponseData singleton( | ||
| TopicPartition topicPartition, | ||
| UnaryOperator<FetchSnapshotResponseData.PartitionSnapshot> operator | ||
| ) { | ||
| FetchSnapshotResponseData.PartitionSnapshot partitionSnapshot = operator.apply( | ||
| new FetchSnapshotResponseData.PartitionSnapshot().setIndex(topicPartition.partition()) | ||
| ); | ||
|
|
||
| return new FetchSnapshotResponseData() | ||
| .setTopics( | ||
| Collections.singletonList( | ||
| new FetchSnapshotResponseData.TopicSnapshot() | ||
| .setName(topicPartition.topic()) | ||
| .setPartitions(Collections.singletonList(partitionSnapshot)) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Finds the PartitionSnapshot for a given topic partition. | ||
| * | ||
| * @param data the fetch snapshot response data | ||
| * @param topicPartition the topic partition to find | ||
| * @return the response partition snapshot if found, otherwise an empty Optional | ||
| */ | ||
| public static Optional<FetchSnapshotResponseData.PartitionSnapshot> forTopicPartition( | ||
| FetchSnapshotResponseData data, | ||
| TopicPartition topicPartition | ||
| ) { | ||
| return data | ||
| .topics() | ||
| .stream() | ||
| .filter(topic -> topic.name().equals(topicPartition.topic())) | ||
| .flatMap(topic -> topic.partitions().stream()) | ||
| .filter(parition -> parition.index() == topicPartition.partition()) | ||
| .findAny(); | ||
| } | ||
| } | ||
This file contains hidden or 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
50 changes: 50 additions & 0 deletions
50
clients/src/main/resources/common/message/FetchSnapshotRequest.json
This file contains hidden or 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,50 @@ | ||
| // 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. | ||
|
|
||
| { | ||
| "apiKey": 59, | ||
| "type": "request", | ||
| "name": "FetchSnapshotRequest", | ||
| "validVersions": "0", | ||
| "flexibleVersions": "0+", | ||
| "fields": [ | ||
| { "name": "ClusterId", "type": "string", "versions": "0+", "nullableVersions": "0+", "default": "null", "taggedVersions": "0+", "tag": 0, | ||
| "about": "The clusterId if known, this is used to validate metadata fetches prior to broker registration" }, | ||
| { "name": "ReplicaId", "type": "int32", "versions": "0+", "default": "-1", | ||
| "about": "The broker ID of the follower" }, | ||
| { "name": "MaxBytes", "type": "int32", "versions": "0+", "default": "0x7fffffff", | ||
| "about": "The maximum bytes to fetch from all of the snapshots" }, | ||
| { "name": "Topics", "type": "[]TopicSnapshot", "versions": "0+", | ||
| "about": "The topics to fetch", "fields": [ | ||
| { "name": "Name", "type": "string", "versions": "0+", "entityType": "topicName", | ||
| "about": "The name of the topic to fetch" }, | ||
| { "name": "Partitions", "type": "[]PartitionSnapshot", "versions": "0+", | ||
| "about": "The partitions to fetch", "fields": [ | ||
| { "name": "Partition", "type": "int32", "versions": "0+", | ||
| "about": "The partition index" }, | ||
| { "name": "CurrentLeaderEpoch", "type": "int32", "versions": "0+", | ||
| "about": "The current leader epoch of the partition, -1 for unknown leader epoch" }, | ||
| { "name": "SnapshotId", "type": "SnapshotId", "versions": "0+", | ||
| "about": "The snapshot endOffset and epoch to fetch", | ||
| "fields": [ | ||
| { "name": "EndOffset", "type": "int64", "versions": "0+" }, | ||
| { "name": "Epoch", "type": "int32", "versions": "0+" } | ||
| ]}, | ||
| { "name": "Position", "type": "int64", "versions": "0+", | ||
| "about": "The byte position within the snapshot to start fetching from" } | ||
| ]} | ||
| ]} | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.