private static Struct parseStructMaybeUpdateThrottleTimeMetrics(ByteBuffer responseBuffer, RequestHeader requestHeader, Sensor throttleTimeSensor, long now) { ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer); // Always expect the response version id to be the same as the request version id Struct responseBody = requestHeader.apiKey().parseResponse(requestHeader.apiVersion(), responseBuffer); correlate(requestHeader, responseHeader); if (throttleTimeSensor != null && responseBody.hasField(CommonFields.THROTTLE_TIME_MS)) throttleTimeSensor.record(responseBody.get(CommonFields.THROTTLE_TIME_MS), now); return responseBody; }
@Test public void produceResponseV5Test() { Map<TopicPartition, ProduceResponse.PartitionResponse> responseData = new HashMap<>(); TopicPartition tp0 = new TopicPartition("test", 0); responseData.put(tp0, new ProduceResponse.PartitionResponse(Errors.NONE, 10000, RecordBatch.NO_TIMESTAMP, 100)); ProduceResponse v5Response = new ProduceResponse(responseData, 10); short version = 5; ByteBuffer buffer = v5Response.serialize(version, new ResponseHeader(0)); buffer.rewind(); ResponseHeader.parse(buffer); // throw away. Struct deserializedStruct = ApiKeys.PRODUCE.parseResponse(version, buffer); ProduceResponse v5FromBytes = (ProduceResponse) AbstractResponse.parseResponse(ApiKeys.PRODUCE, deserializedStruct, version); assertEquals(1, v5FromBytes.responses().size()); assertTrue(v5FromBytes.responses().containsKey(tp0)); ProduceResponse.PartitionResponse partitionResponse = v5FromBytes.responses().get(tp0); assertEquals(100, partitionResponse.logStartOffset); assertEquals(10000, partitionResponse.baseOffset); assertEquals(10, v5FromBytes.throttleTimeMs()); assertEquals(responseData, v5Response.responses()); }
@Test public void testResponseHeader() { ResponseHeader header = createResponseHeader(); ByteBuffer buffer = toBuffer(header.toStruct()); ResponseHeader deserialized = ResponseHeader.parse(buffer); assertEquals(header.correlationId(), deserialized.correlationId()); }
ResponseHeader responseHeader = ResponseHeader.parse(responseBuffer); assertEquals(correlationId, responseHeader.correlationId());
private void verifyFetchResponseFullWrite(short apiVersion, FetchResponse fetchResponse) throws Exception { int correlationId = 15; Send send = fetchResponse.toSend("1", new ResponseHeader(correlationId), apiVersion); ByteBufferChannel channel = new ByteBufferChannel(send.size()); send.writeTo(channel); channel.close(); ByteBuffer buf = channel.buffer(); // read the size int size = buf.getInt(); assertTrue(size > 0); // read the header ResponseHeader responseHeader = ResponseHeader.parse(channel.buffer()); assertEquals(correlationId, responseHeader.correlationId()); // read the body Struct responseBody = ApiKeys.FETCH.responseSchema(apiVersion).read(buf); assertEquals(fetchResponse.toStruct(apiVersion), responseBody); assertEquals(size, responseHeader.sizeOf() + responseBody.sizeOf()); }
/** * Tests that unsupported version of ApiVersionsRequest before SASL handshake request * returns error response and does not result in authentication failure. This test * is similar to {@link #testUnauthenticatedApiVersionsRequest(SecurityProtocol, short)} * where a non-SASL client is used to send requests that are processed by * {@link SaslServerAuthenticator} of the server prior to client authentication. */ @Test public void testApiVersionsRequestWithUnsupportedVersion() throws Exception { short handshakeVersion = ApiKeys.SASL_HANDSHAKE.latestVersion(); SecurityProtocol securityProtocol = SecurityProtocol.SASL_PLAINTEXT; configureMechanisms("PLAIN", Arrays.asList("PLAIN")); server = createEchoServer(securityProtocol); // Send ApiVersionsRequest with unsupported version and validate error response. String node = "1"; createClientConnection(SecurityProtocol.PLAINTEXT, node); RequestHeader header = new RequestHeader(ApiKeys.API_VERSIONS, Short.MAX_VALUE, "someclient", 1); ApiVersionsRequest request = new ApiVersionsRequest.Builder().build(); selector.send(request.toSend(node, header)); ByteBuffer responseBuffer = waitForResponse(); ResponseHeader.parse(responseBuffer); ApiVersionsResponse response = ApiVersionsResponse.parse(responseBuffer, (short) 0); assertEquals(Errors.UNSUPPORTED_VERSION, response.error()); // Send ApiVersionsRequest with a supported version. This should succeed. sendVersionRequestReceiveResponse(node); // Test that client can authenticate successfully sendHandshakeRequestReceiveResponse(node, handshakeVersion); authenticateUsingSaslPlainAndCheckConnection(node, handshakeVersion > 0); }
/** * Handle any completed receives and update the response list with the responses received. * @param responses The list of responses to update * @param now The current time */ private void handleCompletedReceives(List<ClientResponse> responses, long now) { for (NetworkReceive receive : this.selector.completedReceives()) { int source = receive.source(); ClientRequest req = inFlightRequests.completeNext(source); ResponseHeader header = ResponseHeader.parse(receive.payload()); short apiKey = req.request().header().apiKey(); Struct body = (Struct) ProtoUtils.currentResponseSchema(apiKey).read(receive.payload()); correlate(req.request().header(), header); if (apiKey == ApiKeys.METADATA.id) { handleMetadataResponse(req.request().header(), body, now); } else { // need to add body/header to response here responses.add(new ClientResponse(req, now, false, body)); } } }