/** * Get the error counts corresponding to an error response. This is overridden for requests * where response may be null (e.g produce with acks=0). */ public Map<Errors, Integer> errorCounts(Throwable e) { AbstractResponse response = getErrorResponse(0, e); if (response == null) throw new IllegalStateException("Error counts could not be obtained for request " + this); else return response.errorCounts(); }
/** * If a response from a node includes a non-zero throttle delay and client-side throttling has been enabled for * the connection to the node, throttle the connection for the specified delay. * * @param response the response * @param apiVersion the API version of the response * @param nodeId the id of the node * @param now The current time */ private void maybeThrottle(AbstractResponse response, short apiVersion, String nodeId, long now) { int throttleTimeMs = response.throttleTimeMs(); if (throttleTimeMs > 0 && response.shouldClientThrottle(apiVersion)) { connectionStates.throttle(nodeId, now + throttleTimeMs); log.trace("Connection to node {} is throttled for {} ms until timestamp {}", nodeId, throttleTimeMs, now + throttleTimeMs); } }
public static AbstractResponse parseResponse(ByteBuffer responseBuffer, RequestHeader requestHeader) { Struct responseStruct = parseStructMaybeUpdateThrottleTimeMetrics(responseBuffer, requestHeader, null, 0); return AbstractResponse.parseResponse(requestHeader.apiKey(), responseStruct, requestHeader.apiVersion()); }
public String toString(short version) { return toStruct(version).toString(); } }
protected Map<Errors, Integer> errorCounts(Map<?, Errors> errors) { Map<Errors, Integer> errorCounts = new HashMap<>(); for (Errors error : errors.values()) updateErrorCounts(errorCounts, error); return errorCounts; }
protected Send toSend(String destination, ResponseHeader header, short apiVersion) { return new NetworkSend(destination, serialize(apiVersion, header)); }
public Send buildResponse(AbstractResponse body) { ResponseHeader responseHeader = header.toResponseHeader(); return body.toSend(connectionId, responseHeader, apiVersion()); }
if (log.isTraceEnabled()) log.trace("{} got response {}", call, response.responseBody().toString(response.requestHeader().apiVersion())); } catch (Throwable t) { if (log.isTraceEnabled())
/** * 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()) { String source = receive.source(); InFlightRequest req = inFlightRequests.completeNext(source); Struct responseStruct = parseStructMaybeUpdateThrottleTimeMetrics(receive.payload(), req.header, throttleTimeSensor, now); if (log.isTraceEnabled()) { log.trace("Completed receive from node {} for {} with correlation id {}, received {}", req.destination, req.header.apiKey(), req.header.correlationId(), responseStruct); } // If the received response includes a throttle delay, throttle the connection. AbstractResponse body = AbstractResponse. parseResponse(req.header.apiKey(), responseStruct, req.header.apiVersion()); maybeThrottle(body, req.header.apiVersion(), req.destination, now); if (req.isInternalRequest && body instanceof MetadataResponse) metadataUpdater.handleCompletedMetadataResponse(req.header, now, (MetadataResponse) body); else if (req.isInternalRequest && body instanceof ApiVersionsResponse) handleApiVersionsResponse(responses, req, now, (ApiVersionsResponse) body); else responses.add(req.completed(body, now)); } }
private void checkResponse(AbstractResponse response, int version) throws Exception { // Check that we can serialize, deserialize and serialize again // We don't check for equality or hashCode because it is likely to fail for any response containing a HashMap Struct struct = response.toStruct((short) version); AbstractResponse deserialized = (AbstractResponse) deserialize(response, struct, (short) version); Struct struct2 = deserialized.toStruct((short) version); }
protected Map<Errors, Integer> apiErrorCounts(Map<?, ApiError> errors) { Map<Errors, Integer> errorCounts = new HashMap<>(); for (ApiError apiError : errors.values()) updateErrorCounts(errorCounts, apiError.error()); return errorCounts; }
@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()); }
AbstractResponse.parseResponse(ApiKeys.API_VERSIONS, struct, (short) 0); assertEquals(Errors.UNSUPPORTED_VERSION, response.error()); assertTrue(response.apiVersions().isEmpty());