m1a2st commented on code in PR #19776: URL: https://github.com/apache/kafka/pull/19776#discussion_r2105755254
########## core/src/test/java/integration/kafka/server/IntegrationTestUtils.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.common.network.ListenerName; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.requests.ResponseHeader; +import org.apache.kafka.common.utils.Utils; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; + +public class IntegrationTestUtils { + + private static int correlationId = 0; + + public static void sendRequest(Socket socket, byte[] request) throws IOException { + DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream()); + outgoing.writeInt(request.length); + outgoing.write(request); + outgoing.flush(); + } + + private static void sendWithHeader(AbstractRequest request, RequestHeader header, Socket socket) throws IOException { + byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header)); + sendRequest(socket, serializedBytes); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion, String clientId, Integer correlationIdOpt) { + int correlationId = (correlationIdOpt != null) ? correlationIdOpt : ++IntegrationTestUtils.correlationId; + return new RequestHeader(apiKey, apiVersion, clientId, correlationId); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) { + + return new RequestHeader(apiKey, apiVersion, "client-id", 1); + } + + public static void send(AbstractRequest request, Socket socket, String clientId, Integer correlationId) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), clientId, correlationId); + sendWithHeader(request, header, socket); + } + + public static void send(AbstractRequest request, Socket socket) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), "client-id", 0); + sendWithHeader(request, header, socket); + } + + public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException { + DataInputStream incoming = new DataInputStream(socket.getInputStream()); + int len = incoming.readInt(); + + byte[] responseBytes = new byte[len]; + incoming.readFully(responseBytes); + + ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); + ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version)); + + AbstractResponse response = AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version); + if (response.getClass().isAssignableFrom(response.getClass())) { + return (T) response; + } else { + throw new ClassCastException("Expected response with type " + response.getClass() + ", but found " + response.getClass()); + } + } + + public static <T extends AbstractResponse> T sendAndReceive( + AbstractRequest request, + Socket socket, + String clientId, + Integer correlationId + ) throws IOException { + send(request, socket, clientId, correlationId); + return (T) receive(socket, request.apiKey(), request.version()); + } + + public static <T extends AbstractResponse> T sendAndReceive( + AbstractRequest request, + Socket socket + ) throws IOException { + return sendAndReceive(request, socket, "client-id", 0); + } + + + + public static <T extends AbstractResponse> T connectAndReceive( + AbstractRequest request, + SocketServer destination, + ListenerName listenerName + ) throws IOException { + Socket socket = connect(destination, listenerName); + try { + return sendAndReceive(request, socket); + } finally { + socket.close(); + } Review Comment: ```suggestion try (Socket socket = connect(destination, listenerName)) { return sendAndReceive(request, socket); } ``` ########## clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java: ########## @@ -2494,8 +2494,7 @@ void handleResponse(AbstractResponse abstractResponse) { DescribeClusterResponse response = (DescribeClusterResponse) abstractResponse; Errors error = Errors.forCode(response.data().errorCode()); if (error != Errors.NONE) { - ApiError apiError = new ApiError(error, response.data().errorMessage()); - handleFailure(apiError.exception()); + handleFailure(error.exception(response.data().errorMessage())); Review Comment: Just wondering, how is this change related to the main scope of this PR? ########## core/src/test/java/integration/kafka/server/IntegrationTestUtils.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.common.network.ListenerName; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.requests.ResponseHeader; +import org.apache.kafka.common.utils.Utils; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; + +public class IntegrationTestUtils { + + private static int correlationId = 0; + + public static void sendRequest(Socket socket, byte[] request) throws IOException { + DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream()); + outgoing.writeInt(request.length); + outgoing.write(request); + outgoing.flush(); + } + + private static void sendWithHeader(AbstractRequest request, RequestHeader header, Socket socket) throws IOException { + byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header)); + sendRequest(socket, serializedBytes); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion, String clientId, Integer correlationIdOpt) { + int correlationId = (correlationIdOpt != null) ? correlationIdOpt : ++IntegrationTestUtils.correlationId; + return new RequestHeader(apiKey, apiVersion, clientId, correlationId); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) { + + return new RequestHeader(apiKey, apiVersion, "client-id", 1); + } + + public static void send(AbstractRequest request, Socket socket, String clientId, Integer correlationId) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), clientId, correlationId); + sendWithHeader(request, header, socket); + } + + public static void send(AbstractRequest request, Socket socket) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), "client-id", 0); + sendWithHeader(request, header, socket); + } + + public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException { + DataInputStream incoming = new DataInputStream(socket.getInputStream()); + int len = incoming.readInt(); + + byte[] responseBytes = new byte[len]; + incoming.readFully(responseBytes); + + ByteBuffer responseBuffer = ByteBuffer.wrap(responseBytes); + ResponseHeader.parse(responseBuffer, apiKey.responseHeaderVersion(version)); + + AbstractResponse response = AbstractResponse.parseResponse(apiKey, new ByteBufferAccessor(responseBuffer), version); + if (response.getClass().isAssignableFrom(response.getClass())) { + return (T) response; + } else { + throw new ClassCastException("Expected response with type " + response.getClass() + ", but found " + response.getClass()); + } + } + + public static <T extends AbstractResponse> T sendAndReceive( + AbstractRequest request, + Socket socket, + String clientId, + Integer correlationId + ) throws IOException { + send(request, socket, clientId, correlationId); + return (T) receive(socket, request.apiKey(), request.version()); Review Comment: ```suggestion return receive(socket, request.apiKey(), request.version()); ``` ########## core/src/test/java/integration/kafka/server/IntegrationTestUtils.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.common.network.ListenerName; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.requests.ResponseHeader; +import org.apache.kafka.common.utils.Utils; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; + +public class IntegrationTestUtils { + + private static int correlationId = 0; + + public static void sendRequest(Socket socket, byte[] request) throws IOException { + DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream()); + outgoing.writeInt(request.length); + outgoing.write(request); + outgoing.flush(); + } + + private static void sendWithHeader(AbstractRequest request, RequestHeader header, Socket socket) throws IOException { + byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header)); + sendRequest(socket, serializedBytes); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion, String clientId, Integer correlationIdOpt) { + int correlationId = (correlationIdOpt != null) ? correlationIdOpt : ++IntegrationTestUtils.correlationId; + return new RequestHeader(apiKey, apiVersion, clientId, correlationId); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) { + + return new RequestHeader(apiKey, apiVersion, "client-id", 1); + } + + public static void send(AbstractRequest request, Socket socket, String clientId, Integer correlationId) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), clientId, correlationId); + sendWithHeader(request, header, socket); + } + + public static void send(AbstractRequest request, Socket socket) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), "client-id", 0); + sendWithHeader(request, header, socket); + } + + public static <T extends AbstractResponse> T receive(Socket socket, ApiKeys apiKey, short version) throws IOException, ClassCastException { Review Comment: Please add the `@SuppressWarnings("unchecked")` ########## core/src/test/java/integration/kafka/server/IntegrationTestUtils.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.common.network.ListenerName; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.requests.ResponseHeader; +import org.apache.kafka.common.utils.Utils; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; + +public class IntegrationTestUtils { + + private static int correlationId = 0; + + public static void sendRequest(Socket socket, byte[] request) throws IOException { + DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream()); + outgoing.writeInt(request.length); + outgoing.write(request); + outgoing.flush(); + } + + private static void sendWithHeader(AbstractRequest request, RequestHeader header, Socket socket) throws IOException { + byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header)); + sendRequest(socket, serializedBytes); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion, String clientId, Integer correlationIdOpt) { + int correlationId = (correlationIdOpt != null) ? correlationIdOpt : ++IntegrationTestUtils.correlationId; + return new RequestHeader(apiKey, apiVersion, clientId, correlationId); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) { + Review Comment: Extra blank line. ########## core/src/test/java/integration/kafka/server/IntegrationTestUtils.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.common.network.ListenerName; +import org.apache.kafka.common.protocol.ApiKeys; +import org.apache.kafka.common.protocol.ByteBufferAccessor; +import org.apache.kafka.common.requests.AbstractRequest; +import org.apache.kafka.common.requests.AbstractResponse; +import org.apache.kafka.common.requests.RequestHeader; +import org.apache.kafka.common.requests.ResponseHeader; +import org.apache.kafka.common.utils.Utils; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; + +public class IntegrationTestUtils { + + private static int correlationId = 0; + + public static void sendRequest(Socket socket, byte[] request) throws IOException { + DataOutputStream outgoing = new DataOutputStream(socket.getOutputStream()); + outgoing.writeInt(request.length); + outgoing.write(request); + outgoing.flush(); + } + + private static void sendWithHeader(AbstractRequest request, RequestHeader header, Socket socket) throws IOException { + byte[] serializedBytes = Utils.toArray(request.serializeWithHeader(header)); + sendRequest(socket, serializedBytes); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion, String clientId, Integer correlationIdOpt) { + int correlationId = (correlationIdOpt != null) ? correlationIdOpt : ++IntegrationTestUtils.correlationId; + return new RequestHeader(apiKey, apiVersion, clientId, correlationId); + } + + public static RequestHeader nextRequestHeader(ApiKeys apiKey, short apiVersion) { + + return new RequestHeader(apiKey, apiVersion, "client-id", 1); + } + + public static void send(AbstractRequest request, Socket socket, String clientId, Integer correlationId) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), clientId, correlationId); + sendWithHeader(request, header, socket); + } + + public static void send(AbstractRequest request, Socket socket) throws IOException { + RequestHeader header = nextRequestHeader(request.apiKey(), request.version(), "client-id", 0); + sendWithHeader(request, header, socket); + } Review Comment: This method is unused in project, I think we should remove it. -- 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