-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-10817; Add clusterId validation to Fetch handling #10129
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
Changes from all commits
940e401
41cfb38
6fe989d
9d09af1
9cb9137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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 InconsistentClusterIdException extends ApiException { | ||
|
|
||
| public InconsistentClusterIdException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public InconsistentClusterIdException(String message, Throwable throwable) { | ||
| super(message, throwable); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -148,6 +148,7 @@ public class KafkaRaftClient<T> implements RaftClient<T> { | |
| private final LogContext logContext; | ||
| private final Time time; | ||
| private final int fetchMaxWaitMs; | ||
| private final String clusterId; | ||
| private final OptionalInt nodeId; | ||
| private final NetworkChannel channel; | ||
| private final ReplicatedLog log; | ||
|
|
@@ -184,6 +185,7 @@ public KafkaRaftClient( | |
| Metrics metrics, | ||
| ExpirationService expirationService, | ||
| LogContext logContext, | ||
| String clusterId, | ||
| OptionalInt nodeId, | ||
| RaftConfig raftConfig | ||
| ) { | ||
|
|
@@ -197,6 +199,7 @@ public KafkaRaftClient( | |
| metrics, | ||
| expirationService, | ||
| FETCH_MAX_WAIT_MS, | ||
| clusterId, | ||
| nodeId, | ||
| logContext, | ||
| new Random(), | ||
|
|
@@ -214,6 +217,7 @@ public KafkaRaftClient( | |
| Metrics metrics, | ||
| ExpirationService expirationService, | ||
| int fetchMaxWaitMs, | ||
| String clusterId, | ||
| OptionalInt nodeId, | ||
| LogContext logContext, | ||
| Random random, | ||
|
|
@@ -228,6 +232,7 @@ public KafkaRaftClient( | |
| this.fetchPurgatory = new ThresholdPurgatory<>(expirationService); | ||
| this.appendPurgatory = new ThresholdPurgatory<>(expirationService); | ||
| this.time = time; | ||
| this.clusterId = clusterId; | ||
| this.nodeId = nodeId; | ||
| this.metrics = metrics; | ||
| this.fetchMaxWaitMs = fetchMaxWaitMs; | ||
|
|
@@ -940,6 +945,14 @@ private FetchResponseData buildEmptyFetchResponse( | |
| ); | ||
| } | ||
|
|
||
| private boolean hasValidClusterId(FetchRequestData request) { | ||
| // We don't enforce the cluster id if it is not provided. | ||
| if (request.clusterId() == null) { | ||
| return true; | ||
| } | ||
| return clusterId.equals(request.clusterId()); | ||
| } | ||
|
|
||
| /** | ||
| * Handle a Fetch request. The fetch offset and last fetched epoch are always | ||
| * validated against the current log. In the case that they do not match, the response will | ||
|
|
@@ -959,6 +972,10 @@ private CompletableFuture<FetchResponseData> handleFetchRequest( | |
| ) { | ||
| FetchRequestData request = (FetchRequestData) requestMetadata.data; | ||
|
|
||
| if (!hasValidClusterId(request)) { | ||
| return completedFuture(new FetchResponseData().setErrorCode(Errors.INCONSISTENT_CLUSTER_ID.code())); | ||
| } | ||
|
|
||
| if (!hasValidTopicPartition(request, log.topicPartition())) { | ||
| // Until we support multi-raft, we treat topic partition mismatches as invalid requests | ||
| return completedFuture(new FetchResponseData().setErrorCode(Errors.INVALID_REQUEST.code())); | ||
|
|
@@ -1766,6 +1783,7 @@ private FetchRequestData buildFetchRequest() { | |
| }); | ||
| return request | ||
| .setMaxWaitMs(fetchMaxWaitMs) | ||
| .setClusterId(clusterId.toString()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One small detail which is probably ok. The clusterId field in the fetch schema is not currently marked as ignorable. That should be ok since it is only used in the raft implementation which can guarantee that we will have version 12 and above. On the other hand, I don't see any harm making the field ignorable since we are accepting a null value anyway. Is it worth changing that? |
||
| .setReplicaId(quorum.localIdOrSentinel()); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only validate
FetchRequesthere? should we also add validation to the other 4 rpcs?