chia7712 commented on code in PR #20338:
URL: https://github.com/apache/kafka/pull/20338#discussion_r2274997289


##########
core/src/test/java/kafka/server/BrokerRegistrationRequestTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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 kafka.server;
+
+import kafka.network.SocketServer;
+
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.message.BrokerRegistrationRequestData;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractRequest;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.BrokerRegistrationRequest;
+import org.apache.kafka.common.requests.BrokerRegistrationResponse;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.server.common.ControllerRequestCompletionHandler;
+import org.apache.kafka.server.common.Feature;
+import org.apache.kafka.server.common.MetadataVersion;
+import org.apache.kafka.server.common.MetadataVersionTestUtils;
+import org.apache.kafka.server.common.NodeToControllerChannelManager;
+
+import org.junit.jupiter.api.Assertions;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import scala.Option;
+
+/**
+ * This test simulates a broker registering with the KRaft quorum under 
different configurations.
+ */
+public class BrokerRegistrationRequestTest {
+
+    public NodeToControllerChannelManager 
brokerToControllerChannelManager(ClusterInstance clusterInstance) {
+        var controllerSocketServer = 
clusterInstance.controllers().values().stream()
+            .map(ControllerServer::socketServer)
+            .findFirst()
+            .orElseThrow();
+
+        return new NodeToControllerChannelManagerImpl(
+                new TestControllerNodeProvider(controllerSocketServer, 
clusterInstance),
+                Time.SYSTEM,
+                new Metrics(),
+                controllerSocketServer.config(),
+                "heartbeat",
+                "test-heartbeat-",
+                10000L
+        );
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends AbstractRequest, R extends AbstractResponse> R 
sendAndReceive(
+        NodeToControllerChannelManager channelManager,
+        AbstractRequest.Builder<T> reqBuilder,
+        int timeoutMs
+    ) throws Exception {
+        var responseFuture = new CompletableFuture<R>();
+        channelManager.sendRequest(reqBuilder, new 
ControllerRequestCompletionHandler() {
+            @Override
+            public void onTimeout() {
+                responseFuture.completeExceptionally(new TimeoutException());
+            }
+
+            @Override
+            public void onComplete(ClientResponse response) {
+                responseFuture.complete((R) response.responseBody());
+            }
+        });
+        return responseFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
+    }
+
+    public Errors registerBroker(
+        NodeToControllerChannelManager channelManager,
+        String clusterId,
+        int brokerId,
+        Long zkEpoch,
+        FeatureLevel featureLevelToSend
+    ) throws Exception {
+        var features = new BrokerRegistrationRequestData.FeatureCollection();
+
+        if (featureLevelToSend != null) {
+            features.add(new BrokerRegistrationRequestData.Feature()
+                    .setName(MetadataVersion.FEATURE_NAME)
+                    .setMinSupportedVersion(featureLevelToSend.min())
+                    .setMaxSupportedVersion(featureLevelToSend.max())
+            );
+        }
+        
+        Feature.PRODUCTION_FEATURES.stream()
+            .filter(feature -> 
!feature.featureName().equals(MetadataVersion.FEATURE_NAME))
+            .forEach(feature -> features.add(new 
BrokerRegistrationRequestData.Feature()
+                .setName(feature.featureName())
+                .setMinSupportedVersion(feature.minimumProduction())
+                .setMaxSupportedVersion(feature.latestTesting())));
+
+        var listener = new BrokerRegistrationRequestData.Listener()
+                .setName("EXTERNAL")
+                .setHost("example.com")
+                .setPort(8082)
+                .setSecurityProtocol(SecurityProtocol.PLAINTEXT.id);
+        
+        var req = new BrokerRegistrationRequestData()
+            .setBrokerId(brokerId)
+            .setLogDirs(List.of(Uuid.randomUuid()))
+            .setClusterId(clusterId)
+            .setIncarnationId(Uuid.randomUuid())
+            .setIsMigratingZkBroker(zkEpoch != null)
+            .setFeatures(features)
+            .setListeners(new 
BrokerRegistrationRequestData.ListenerCollection(List.of(listener).iterator()));
+
+        BrokerRegistrationResponse resp = this.sendAndReceive(
+            channelManager, 
+            new BrokerRegistrationRequest.Builder(req), 
+            30000
+        );
+        return Errors.forCode(resp.data().errorCode());
+    }
+
+    @ClusterTest(types = {Type.KRAFT}, controllers = 1, metadataVersion = 
MetadataVersion.IBP_3_3_IV3)
+    public void testRegisterZkWith33Controller(ClusterInstance 
clusterInstance) throws Exception {

Review Comment:
   This test name is no longer accurate, since the RPCs are not all related to 
ZK. Perhaps you could split the scenarios into separate test cases?



##########
core/src/test/java/kafka/server/BrokerRegistrationRequestTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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 kafka.server;

Review Comment:
   Could you please move it to server module?



##########
core/src/test/java/kafka/server/BrokerRegistrationRequestTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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 kafka.server;
+
+import kafka.network.SocketServer;
+
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.message.BrokerRegistrationRequestData;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractRequest;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.BrokerRegistrationRequest;
+import org.apache.kafka.common.requests.BrokerRegistrationResponse;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.server.common.ControllerRequestCompletionHandler;
+import org.apache.kafka.server.common.Feature;
+import org.apache.kafka.server.common.MetadataVersion;
+import org.apache.kafka.server.common.MetadataVersionTestUtils;
+import org.apache.kafka.server.common.NodeToControllerChannelManager;
+
+import org.junit.jupiter.api.Assertions;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import scala.Option;
+
+/**
+ * This test simulates a broker registering with the KRaft quorum under 
different configurations.
+ */
+public class BrokerRegistrationRequestTest {
+
+    public NodeToControllerChannelManager 
brokerToControllerChannelManager(ClusterInstance clusterInstance) {
+        var controllerSocketServer = 
clusterInstance.controllers().values().stream()
+            .map(ControllerServer::socketServer)
+            .findFirst()
+            .orElseThrow();
+
+        return new NodeToControllerChannelManagerImpl(
+                new TestControllerNodeProvider(controllerSocketServer, 
clusterInstance),
+                Time.SYSTEM,
+                new Metrics(),
+                controllerSocketServer.config(),
+                "heartbeat",
+                "test-heartbeat-",
+                10000L
+        );
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends AbstractRequest, R extends AbstractResponse> R 
sendAndReceive(
+        NodeToControllerChannelManager channelManager,
+        AbstractRequest.Builder<T> reqBuilder,
+        int timeoutMs
+    ) throws Exception {
+        var responseFuture = new CompletableFuture<R>();
+        channelManager.sendRequest(reqBuilder, new 
ControllerRequestCompletionHandler() {
+            @Override
+            public void onTimeout() {
+                responseFuture.completeExceptionally(new TimeoutException());
+            }
+
+            @Override
+            public void onComplete(ClientResponse response) {
+                responseFuture.complete((R) response.responseBody());
+            }
+        });
+        return responseFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
+    }
+
+    public Errors registerBroker(
+        NodeToControllerChannelManager channelManager,
+        String clusterId,
+        int brokerId,
+        Long zkEpoch,
+        FeatureLevel featureLevelToSend
+    ) throws Exception {
+        var features = new BrokerRegistrationRequestData.FeatureCollection();
+
+        if (featureLevelToSend != null) {
+            features.add(new BrokerRegistrationRequestData.Feature()
+                    .setName(MetadataVersion.FEATURE_NAME)
+                    .setMinSupportedVersion(featureLevelToSend.min())
+                    .setMaxSupportedVersion(featureLevelToSend.max())
+            );
+        }
+        
+        Feature.PRODUCTION_FEATURES.stream()
+            .filter(feature -> 
!feature.featureName().equals(MetadataVersion.FEATURE_NAME))
+            .forEach(feature -> features.add(new 
BrokerRegistrationRequestData.Feature()
+                .setName(feature.featureName())
+                .setMinSupportedVersion(feature.minimumProduction())
+                .setMaxSupportedVersion(feature.latestTesting())));
+
+        var listener = new BrokerRegistrationRequestData.Listener()
+                .setName("EXTERNAL")
+                .setHost("example.com")
+                .setPort(8082)
+                .setSecurityProtocol(SecurityProtocol.PLAINTEXT.id);
+        
+        var req = new BrokerRegistrationRequestData()
+            .setBrokerId(brokerId)
+            .setLogDirs(List.of(Uuid.randomUuid()))
+            .setClusterId(clusterId)
+            .setIncarnationId(Uuid.randomUuid())
+            .setIsMigratingZkBroker(zkEpoch != null)
+            .setFeatures(features)
+            .setListeners(new 
BrokerRegistrationRequestData.ListenerCollection(List.of(listener).iterator()));
+
+        BrokerRegistrationResponse resp = this.sendAndReceive(
+            channelManager, 
+            new BrokerRegistrationRequest.Builder(req), 
+            30000
+        );
+        return Errors.forCode(resp.data().errorCode());
+    }
+
+    @ClusterTest(types = {Type.KRAFT}, controllers = 1, metadataVersion = 
MetadataVersion.IBP_3_3_IV3)
+    public void testRegisterZkWith33Controller(ClusterInstance 
clusterInstance) throws Exception {
+        // Verify that a controller running an old metadata.version cannot 
register a ZK broker
+        var clusterId = clusterInstance.clusterId();
+        var channelManager = brokerToControllerChannelManager(clusterInstance);
+        try {
+            channelManager.start();
+            // Invalid registration (isMigratingZkBroker, but MV does not 
support migrations)
+            Assertions.assertEquals(
+                Errors.BROKER_ID_NOT_REGISTERED,
+                registerBroker(channelManager, clusterId, 100, 1L, 
+                    new 
FeatureLevel(MetadataVersionTestUtils.IBP_3_3_IV0_FEATURE_LEVEL, 
MetadataVersionTestUtils.IBP_3_3_IV3_FEATURE_LEVEL))
+            );
+
+            // No features (MV) sent with registration, controller can't verify
+            Assertions.assertEquals(
+                Errors.INVALID_REGISTRATION,
+                registerBroker(channelManager, clusterId, 100, null, null)
+            );
+
+            // Given MV is too high for controller to support
+            Assertions.assertEquals(
+                Errors.UNSUPPORTED_VERSION,
+                registerBroker(channelManager, clusterId, 100, null, 
+                    new 
FeatureLevel(MetadataVersionTestUtils.IBP_3_4_IV0_FEATURE_LEVEL, 
MetadataVersionTestUtils.IBP_3_4_IV0_FEATURE_LEVEL))
+            );
+
+            // Controller supports this MV and isMigratingZkBroker is false, 
so this one works
+            Assertions.assertEquals(
+                Errors.NONE,
+                registerBroker(channelManager, clusterId, 100, null, 
+                    new 
FeatureLevel(MetadataVersionTestUtils.IBP_3_3_IV3_FEATURE_LEVEL, 
MetadataVersionTestUtils.IBP_3_4_IV0_FEATURE_LEVEL))

Review Comment:
   ditto to `IBP_3_4_IV0_FEATURE_LEVEL`



##########
core/src/test/java/kafka/server/BrokerRegistrationRequestTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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 kafka.server;
+
+import kafka.network.SocketServer;
+
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.message.BrokerRegistrationRequestData;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractRequest;
+import org.apache.kafka.common.requests.AbstractResponse;
+import org.apache.kafka.common.requests.BrokerRegistrationRequest;
+import org.apache.kafka.common.requests.BrokerRegistrationResponse;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import org.apache.kafka.common.test.ClusterInstance;
+import org.apache.kafka.common.test.api.ClusterTest;
+import org.apache.kafka.common.test.api.Type;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.server.common.ControllerRequestCompletionHandler;
+import org.apache.kafka.server.common.Feature;
+import org.apache.kafka.server.common.MetadataVersion;
+import org.apache.kafka.server.common.MetadataVersionTestUtils;
+import org.apache.kafka.server.common.NodeToControllerChannelManager;
+
+import org.junit.jupiter.api.Assertions;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import scala.Option;
+
+/**
+ * This test simulates a broker registering with the KRaft quorum under 
different configurations.
+ */
+public class BrokerRegistrationRequestTest {
+
+    public NodeToControllerChannelManager 
brokerToControllerChannelManager(ClusterInstance clusterInstance) {
+        var controllerSocketServer = 
clusterInstance.controllers().values().stream()
+            .map(ControllerServer::socketServer)
+            .findFirst()
+            .orElseThrow();
+
+        return new NodeToControllerChannelManagerImpl(
+                new TestControllerNodeProvider(controllerSocketServer, 
clusterInstance),
+                Time.SYSTEM,
+                new Metrics(),
+                controllerSocketServer.config(),
+                "heartbeat",
+                "test-heartbeat-",
+                10000L
+        );
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends AbstractRequest, R extends AbstractResponse> R 
sendAndReceive(
+        NodeToControllerChannelManager channelManager,
+        AbstractRequest.Builder<T> reqBuilder,
+        int timeoutMs
+    ) throws Exception {
+        var responseFuture = new CompletableFuture<R>();
+        channelManager.sendRequest(reqBuilder, new 
ControllerRequestCompletionHandler() {
+            @Override
+            public void onTimeout() {
+                responseFuture.completeExceptionally(new TimeoutException());
+            }
+
+            @Override
+            public void onComplete(ClientResponse response) {
+                responseFuture.complete((R) response.responseBody());
+            }
+        });
+        return responseFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
+    }
+
+    public Errors registerBroker(
+        NodeToControllerChannelManager channelManager,
+        String clusterId,
+        int brokerId,
+        Long zkEpoch,
+        FeatureLevel featureLevelToSend
+    ) throws Exception {
+        var features = new BrokerRegistrationRequestData.FeatureCollection();
+
+        if (featureLevelToSend != null) {
+            features.add(new BrokerRegistrationRequestData.Feature()
+                    .setName(MetadataVersion.FEATURE_NAME)
+                    .setMinSupportedVersion(featureLevelToSend.min())
+                    .setMaxSupportedVersion(featureLevelToSend.max())
+            );
+        }
+        
+        Feature.PRODUCTION_FEATURES.stream()
+            .filter(feature -> 
!feature.featureName().equals(MetadataVersion.FEATURE_NAME))
+            .forEach(feature -> features.add(new 
BrokerRegistrationRequestData.Feature()
+                .setName(feature.featureName())
+                .setMinSupportedVersion(feature.minimumProduction())
+                .setMaxSupportedVersion(feature.latestTesting())));
+
+        var listener = new BrokerRegistrationRequestData.Listener()
+                .setName("EXTERNAL")
+                .setHost("example.com")
+                .setPort(8082)
+                .setSecurityProtocol(SecurityProtocol.PLAINTEXT.id);
+        
+        var req = new BrokerRegistrationRequestData()
+            .setBrokerId(brokerId)
+            .setLogDirs(List.of(Uuid.randomUuid()))
+            .setClusterId(clusterId)
+            .setIncarnationId(Uuid.randomUuid())
+            .setIsMigratingZkBroker(zkEpoch != null)
+            .setFeatures(features)
+            .setListeners(new 
BrokerRegistrationRequestData.ListenerCollection(List.of(listener).iterator()));
+
+        BrokerRegistrationResponse resp = this.sendAndReceive(
+            channelManager, 
+            new BrokerRegistrationRequest.Builder(req), 
+            30000
+        );
+        return Errors.forCode(resp.data().errorCode());
+    }
+
+    @ClusterTest(types = {Type.KRAFT}, controllers = 1, metadataVersion = 
MetadataVersion.IBP_3_3_IV3)
+    public void testRegisterZkWith33Controller(ClusterInstance 
clusterInstance) throws Exception {
+        // Verify that a controller running an old metadata.version cannot 
register a ZK broker
+        var clusterId = clusterInstance.clusterId();
+        var channelManager = brokerToControllerChannelManager(clusterInstance);
+        try {
+            channelManager.start();
+            // Invalid registration (isMigratingZkBroker, but MV does not 
support migrations)
+            Assertions.assertEquals(
+                Errors.BROKER_ID_NOT_REGISTERED,
+                registerBroker(channelManager, clusterId, 100, 1L, 
+                    new 
FeatureLevel(MetadataVersionTestUtils.IBP_3_3_IV0_FEATURE_LEVEL, 
MetadataVersionTestUtils.IBP_3_3_IV3_FEATURE_LEVEL))
+            );
+
+            // No features (MV) sent with registration, controller can't verify
+            Assertions.assertEquals(
+                Errors.INVALID_REGISTRATION,
+                registerBroker(channelManager, clusterId, 100, null, null)
+            );
+
+            // Given MV is too high for controller to support
+            Assertions.assertEquals(
+                Errors.UNSUPPORTED_VERSION,
+                registerBroker(channelManager, clusterId, 100, null, 
+                    new 
FeatureLevel(MetadataVersionTestUtils.IBP_3_4_IV0_FEATURE_LEVEL, 
MetadataVersionTestUtils.IBP_3_4_IV0_FEATURE_LEVEL))
+            );
+
+            // Controller supports this MV and isMigratingZkBroker is false, 
so this one works
+            Assertions.assertEquals(
+                Errors.NONE,
+                registerBroker(channelManager, clusterId, 100, null, 
+                    new 
FeatureLevel(MetadataVersionTestUtils.IBP_3_3_IV3_FEATURE_LEVEL, 
MetadataVersionTestUtils.IBP_3_4_IV0_FEATURE_LEVEL))

Review Comment:
   `MetadataVersionTestUtils.IBP_3_3_IV3_FEATURE_LEVEL` -> 
`MetadataVersion.IBP_3_3_IV3.featureLevel()`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to