-
Notifications
You must be signed in to change notification settings - Fork 15.2k
KAFKA-20246: Add clusterId and nodeId to ApiVersionsRequest (2/N) #22187
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
Open
AndrewJSchofield
wants to merge
1
commit into
apache:trunk
Choose a base branch
from
AndrewJSchofield:KAFKA-20246-3
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package org.apache.kafka.clients; | ||
|
|
||
| import org.apache.kafka.common.Cluster; | ||
| import org.apache.kafka.common.ClusterResource; | ||
| import org.apache.kafka.common.KafkaException; | ||
| import org.apache.kafka.common.Node; | ||
| import org.apache.kafka.common.TopicPartition; | ||
|
|
@@ -120,6 +121,9 @@ private enum State { | |
|
|
||
| private final MetadataRecoveryStrategy metadataRecoveryStrategy; | ||
|
|
||
| /* Whether to send the cluster ID and node ID on ApiVersions RPC for checking by the broker */ | ||
| private final boolean metadataClusterCheckEnable; | ||
|
|
||
| private final Time time; | ||
|
|
||
| /** | ||
|
|
@@ -154,7 +158,8 @@ public NetworkClient(Selectable selector, | |
| boolean discoverBrokerVersions, | ||
| ApiVersions apiVersions, | ||
| LogContext logContext, | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy) { | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy, | ||
| boolean metadataClusterCheckEnable) { | ||
| this(selector, | ||
| metadata, | ||
| clientId, | ||
|
|
@@ -171,7 +176,8 @@ public NetworkClient(Selectable selector, | |
| apiVersions, | ||
| logContext, | ||
| Long.MAX_VALUE, | ||
| metadataRecoveryStrategy); | ||
| metadataRecoveryStrategy, | ||
| metadataClusterCheckEnable); | ||
| } | ||
|
|
||
| public NetworkClient(Selectable selector, | ||
|
|
@@ -190,7 +196,8 @@ public NetworkClient(Selectable selector, | |
| ApiVersions apiVersions, | ||
| LogContext logContext, | ||
| long rebootstrapTriggerMs, | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy) { | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy, | ||
| boolean metadataClusterCheckEnable) { | ||
| this(null, | ||
| metadata, | ||
| selector, | ||
|
|
@@ -211,7 +218,8 @@ public NetworkClient(Selectable selector, | |
| new DefaultHostResolver(), | ||
| null, | ||
| rebootstrapTriggerMs, | ||
| metadataRecoveryStrategy); | ||
| metadataRecoveryStrategy, | ||
| metadataClusterCheckEnable); | ||
| } | ||
|
|
||
| public NetworkClient(Selectable selector, | ||
|
|
@@ -230,7 +238,8 @@ public NetworkClient(Selectable selector, | |
| ApiVersions apiVersions, | ||
| Sensor throttleTimeSensor, | ||
| LogContext logContext, | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy) { | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy, | ||
| boolean metadataClusterCheckEnable) { | ||
| this(null, | ||
| metadata, | ||
| selector, | ||
|
|
@@ -251,7 +260,8 @@ public NetworkClient(Selectable selector, | |
| new DefaultHostResolver(), | ||
| null, | ||
| Long.MAX_VALUE, | ||
| metadataRecoveryStrategy); | ||
| metadataRecoveryStrategy, | ||
| metadataClusterCheckEnable); | ||
| } | ||
|
|
||
| public NetworkClient(Selectable selector, | ||
|
|
@@ -269,7 +279,8 @@ public NetworkClient(Selectable selector, | |
| boolean discoverBrokerVersions, | ||
| ApiVersions apiVersions, | ||
| LogContext logContext, | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy) { | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy, | ||
| boolean metadataClusterCheckEnable) { | ||
| this(metadataUpdater, | ||
| null, | ||
| selector, | ||
|
|
@@ -290,7 +301,8 @@ public NetworkClient(Selectable selector, | |
| new DefaultHostResolver(), | ||
| null, | ||
| Long.MAX_VALUE, | ||
| metadataRecoveryStrategy); | ||
| metadataRecoveryStrategy, | ||
| metadataClusterCheckEnable); | ||
| } | ||
|
|
||
| public NetworkClient(MetadataUpdater metadataUpdater, | ||
|
|
@@ -313,7 +325,8 @@ public NetworkClient(MetadataUpdater metadataUpdater, | |
| HostResolver hostResolver, | ||
| ClientTelemetrySender clientTelemetrySender, | ||
| long rebootstrapTriggerMs, | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy) { | ||
| MetadataRecoveryStrategy metadataRecoveryStrategy, | ||
| boolean metadataClusterCheckEnable) { | ||
| /* It would be better if we could pass `DefaultMetadataUpdater` from the public constructor, but it's not | ||
| * possible because `DefaultMetadataUpdater` is an inner class and it can only be instantiated after the | ||
| * super constructor is invoked. | ||
|
|
@@ -346,6 +359,7 @@ public NetworkClient(MetadataUpdater metadataUpdater, | |
| this.telemetrySender = (clientTelemetrySender != null) ? new TelemetrySender(clientTelemetrySender) : null; | ||
| this.rebootstrapTriggerMs = rebootstrapTriggerMs; | ||
| this.metadataRecoveryStrategy = metadataRecoveryStrategy; | ||
| this.metadataClusterCheckEnable = metadataClusterCheckEnable; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1024,7 +1038,10 @@ private void handleApiVersionsResponse(List<ClientResponse> responses, | |
| InFlightRequest req, long now, ApiVersionsResponse apiVersionsResponse) { | ||
| final String node = req.destination; | ||
| if (apiVersionsResponse.data().errorCode() != Errors.NONE.code()) { | ||
| if (req.request.version() == 0 || apiVersionsResponse.data().errorCode() != Errors.UNSUPPORTED_VERSION.code()) { | ||
| if (metadataRecoveryStrategy == MetadataRecoveryStrategy.REBOOTSTRAP && apiVersionsResponse.data().errorCode() == Errors.REBOOTSTRAP_REQUIRED.code()) { | ||
| log.info("Rebootstrap requested by server due to cluster metadata mismatch."); | ||
| metadataUpdater.rebootstrap(now); | ||
| } else if (req.request.version() == 0 || apiVersionsResponse.data().errorCode() != Errors.UNSUPPORTED_VERSION.code()) { | ||
| log.warn("Received error {} from node {} when making an ApiVersionsRequest with correlation id {}. Disconnecting.", | ||
| Errors.forCode(apiVersionsResponse.data().errorCode()), node, req.header.correlationId()); | ||
| this.selector.close(node); | ||
|
|
@@ -1107,6 +1124,19 @@ private void handleInitiateApiVersionRequests(long now) { | |
| // not before ready. | ||
| this.connectionStates.checkingApiVersions(node); | ||
| ApiVersionsRequest.Builder apiVersionRequestBuilder = entry.getValue(); | ||
| // If we know the cluster ID and node ID we are connecting to, we can include | ||
| // those details in the ApiVersions request for checking in the broker, | ||
| // provided that the metadata recovery strategy is not NONE. (KIP-1242) | ||
| if (metadataRecoveryStrategy != MetadataRecoveryStrategy.NONE && metadataClusterCheckEnable) { | ||
| String clusterId = this.metadataUpdater.clusterId(); | ||
| int nodeId = Integer.parseInt(node); | ||
| // When connecting to coordinators, the client uses large positive node ID | ||
| // values which do not match the target broker's node ID. Exclude those. | ||
| if (clusterId != null && nodeId > 0 && nodeId < Integer.MAX_VALUE / 2) { | ||
|
Member
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. Should it be |
||
| apiVersionRequestBuilder.setClusterId(clusterId); | ||
| apiVersionRequestBuilder.setNodeId(nodeId); | ||
| } | ||
| } | ||
| ClientRequest clientRequest = newClientRequest(node, apiVersionRequestBuilder, now, true); | ||
| doSend(clientRequest, true, now); | ||
| iter.remove(); | ||
|
|
@@ -1193,6 +1223,15 @@ class DefaultMetadataUpdater implements MetadataUpdater { | |
| this.inProgress = null; | ||
| } | ||
|
|
||
| @Override | ||
| public String clusterId() { | ||
| ClusterResource clusterResource = metadata.fetch().clusterResource(); | ||
| if (clusterResource != null) { | ||
| return clusterResource.clusterId(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public List<Node> fetchNodes() { | ||
| return metadata.fetch().nodes(); | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -56,6 +56,14 @@ public Builder( | |
| this.data = data.duplicate(); | ||
| } | ||
|
|
||
| public void setClusterId(String clusterId) { | ||
| this.data.setClusterId(clusterId); | ||
| } | ||
|
|
||
| public void setNodeId(int nodeId) { | ||
| this.data.setNodeId(nodeId); | ||
| } | ||
|
|
||
| @Override | ||
| public ApiVersionsRequest build(short version) { | ||
| return new ApiVersionsRequest(data, version); | ||
|
|
@@ -94,6 +102,13 @@ public boolean hasUnsupportedRequestVersion() { | |
| } | ||
|
|
||
| public boolean isValid() { | ||
| if (version() >= 5) { | ||
| // Either cluster ID and node ID are both specified, or neither is. | ||
| if ((data.clusterId() == null && data.nodeId() != -1) || (data.clusterId() != null && data.nodeId() == -1)) { | ||
|
Member
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. Should we document in ApiVersionsRequest.json that both fields must be defined together? |
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| if (version() >= 3) { | ||
| return SOFTWARE_NAME_VERSION_PATTERN.matcher(data.clientSoftwareName()).matches() && | ||
| SOFTWARE_NAME_VERSION_PATTERN.matcher(data.clientSoftwareVersion()).matches(); | ||
|
|
||
Oops, something went wrong.
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.
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.
Should we clean up the connection here like
handleRebootstrapdoes?kafka/clients/src/main/java/org/apache/kafka/clients/NetworkClient.java
Lines 1117 to 1129 in 162b3a1