public int sizeOf() { return toStruct().sizeOf(); }
/** * Visible for testing, typically {@link #toSend(String, ResponseHeader, short)} should be used instead. */ public ByteBuffer serialize(short version, ResponseHeader responseHeader) { return serialize(responseHeader.toStruct(), toStruct(version)); }
@Override protected Send toSend(String dest, ResponseHeader responseHeader, short apiVersion) { Struct responseHeaderStruct = responseHeader.toStruct(); Struct responseBodyStruct = toStruct(apiVersion); // write the total size and the response header ByteBuffer buffer = ByteBuffer.allocate(responseHeaderStruct.sizeOf() + 4); buffer.putInt(responseHeaderStruct.sizeOf() + responseBodyStruct.sizeOf()); responseHeaderStruct.writeTo(buffer); buffer.rewind(); Queue<Send> sends = new ArrayDeque<>(); sends.add(new ByteBufferSend(dest, buffer)); addResponseData(responseBodyStruct, throttleTimeMs, dest, sends); return new MultiRecordsSend(dest, sends); }
@Test public void testResponseHeader() { ResponseHeader header = createResponseHeader(); ByteBuffer buffer = toBuffer(header.toStruct()); ResponseHeader deserialized = ResponseHeader.parse(buffer); assertEquals(header.correlationId(), deserialized.correlationId()); }
private void checkSimpleRequestResponse(NetworkClient networkClient) { awaitReady(networkClient, node); // has to be before creating any request, as it may send ApiVersionsRequest and its response is mocked with correlation id 0 ProduceRequest.Builder builder = ProduceRequest.Builder.forCurrentMagic((short) 1, 1000, Collections.emptyMap()); TestCallbackHandler handler = new TestCallbackHandler(); ClientRequest request = networkClient.newClientRequest( node.idString(), builder, time.milliseconds(), true, defaultRequestTimeoutMs, handler); networkClient.send(request, time.milliseconds()); networkClient.poll(1, time.milliseconds()); assertEquals(1, networkClient.inFlightRequestCount()); ResponseHeader respHeader = new ResponseHeader(request.correlationId()); Struct resp = new Struct(ApiKeys.PRODUCE.responseSchema(ApiKeys.PRODUCE.latestVersion())); resp.set("responses", new Object[0]); Struct responseHeaderStruct = respHeader.toStruct(); int size = responseHeaderStruct.sizeOf() + resp.sizeOf(); ByteBuffer buffer = ByteBuffer.allocate(size); responseHeaderStruct.writeTo(buffer); resp.writeTo(buffer); buffer.flip(); selector.completeReceive(new NetworkReceive(node.idString(), buffer)); List<ClientResponse> responses = networkClient.poll(1, time.milliseconds()); assertEquals(1, responses.size()); assertTrue("The handler should have executed.", handler.executed); assertTrue("Should have a response body.", handler.response.hasResponse()); assertEquals("Should be correlated to the original request", request.correlationId(), handler.response.requestHeader().correlationId()); }
resp.set("responses", new Object[0]); resp.set(CommonFields.THROTTLE_TIME_MS, 100); Struct responseHeaderStruct = respHeader.toStruct(); int size = responseHeaderStruct.sizeOf() + resp.sizeOf(); ByteBuffer buffer = ByteBuffer.allocate(size);
resp.set("responses", new Object[0]); resp.set(CommonFields.THROTTLE_TIME_MS, 100); Struct responseHeaderStruct = respHeader.toStruct(); int size = responseHeaderStruct.sizeOf() + resp.sizeOf(); ByteBuffer buffer = ByteBuffer.allocate(size);