Search in sources :

Example 1 with ApiMessage

use of org.apache.kafka.common.protocol.ApiMessage in project kafka by apache.

the class KafkaRaftClient method handleRequest.

private void handleRequest(RaftRequest.Inbound request, long currentTimeMs) {
    ApiKeys apiKey = ApiKeys.forId(request.data.apiKey());
    final CompletableFuture<? extends ApiMessage> responseFuture;
    switch(apiKey) {
        case FETCH:
            responseFuture = handleFetchRequest(request, currentTimeMs);
            break;
        case VOTE:
            responseFuture = completedFuture(handleVoteRequest(request));
            break;
        case BEGIN_QUORUM_EPOCH:
            responseFuture = completedFuture(handleBeginQuorumEpochRequest(request, currentTimeMs));
            break;
        case END_QUORUM_EPOCH:
            responseFuture = completedFuture(handleEndQuorumEpochRequest(request, currentTimeMs));
            break;
        case DESCRIBE_QUORUM:
            responseFuture = completedFuture(handleDescribeQuorumRequest(request, currentTimeMs));
            break;
        case FETCH_SNAPSHOT:
            responseFuture = completedFuture(handleFetchSnapshotRequest(request));
            break;
        default:
            throw new IllegalArgumentException("Unexpected request type " + apiKey);
    }
    responseFuture.whenComplete((response, exception) -> {
        final ApiMessage message;
        if (response != null) {
            message = response;
        } else {
            message = RaftUtil.errorResponse(apiKey, Errors.forException(exception));
        }
        RaftResponse.Outbound responseMessage = new RaftResponse.Outbound(request.correlationId(), message);
        request.completion.complete(responseMessage);
        logger.trace("Sent response {} to inbound request {}", responseMessage, request);
    });
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ApiMessage(org.apache.kafka.common.protocol.ApiMessage)

Example 2 with ApiMessage

use of org.apache.kafka.common.protocol.ApiMessage in project kafka by apache.

the class AbstractApiMessageSerde method read.

@Override
public ApiMessageAndVersion read(Readable input, int size) {
    short frameVersion = unsignedIntToShort(input, "frame version");
    if (frameVersion == 0) {
        throw new MetadataParseException("Could not deserialize metadata record with frame version 0. " + "Note that upgrades from the preview release of KRaft in 2.8 to newer versions are not supported.");
    } else if (frameVersion != DEFAULT_FRAME_VERSION) {
        throw new MetadataParseException("Could not deserialize metadata record due to unknown frame version " + frameVersion + "(only frame version " + DEFAULT_FRAME_VERSION + " is supported)");
    }
    short apiKey = unsignedIntToShort(input, "type");
    short version = unsignedIntToShort(input, "version");
    ApiMessage record;
    try {
        record = apiMessageFor(apiKey);
    } catch (Exception e) {
        throw new MetadataParseException(e);
    }
    try {
        record.read(input, version);
    } catch (Exception e) {
        throw new MetadataParseException("Failed to deserialize record with type " + apiKey, e);
    }
    if (input.remaining() > 0) {
        throw new MetadataParseException("Found " + input.remaining() + " byte(s) of garbage after " + apiKey);
    }
    return new ApiMessageAndVersion(record, version);
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ApiMessage(org.apache.kafka.common.protocol.ApiMessage)

Example 3 with ApiMessage

use of org.apache.kafka.common.protocol.ApiMessage in project kafka by apache.

the class MockMetaLogManagerListener method handleCommit.

@Override
public synchronized void handleCommit(BatchReader<ApiMessageAndVersion> reader) {
    try {
        while (reader.hasNext()) {
            Batch<ApiMessageAndVersion> batch = reader.next();
            long lastCommittedOffset = batch.lastOffset();
            for (ApiMessageAndVersion messageAndVersion : batch.records()) {
                ApiMessage message = messageAndVersion.message();
                StringBuilder bld = new StringBuilder();
                bld.append(COMMIT).append(" ").append(message.toString());
                serializedEvents.add(bld.toString());
            }
            StringBuilder bld = new StringBuilder();
            bld.append(LAST_COMMITTED_OFFSET).append(" ").append(lastCommittedOffset);
            serializedEvents.add(bld.toString());
        }
    } finally {
        reader.close();
    }
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ApiMessage(org.apache.kafka.common.protocol.ApiMessage)

Example 4 with ApiMessage

use of org.apache.kafka.common.protocol.ApiMessage in project kafka by apache.

the class MockMetaLogManagerListener method handleSnapshot.

@Override
public synchronized void handleSnapshot(SnapshotReader<ApiMessageAndVersion> reader) {
    long lastCommittedOffset = reader.lastContainedLogOffset();
    try {
        while (reader.hasNext()) {
            Batch<ApiMessageAndVersion> batch = reader.next();
            for (ApiMessageAndVersion messageAndVersion : batch.records()) {
                ApiMessage message = messageAndVersion.message();
                StringBuilder bld = new StringBuilder();
                bld.append(SNAPSHOT).append(" ").append(message.toString());
                serializedEvents.add(bld.toString());
            }
            StringBuilder bld = new StringBuilder();
            bld.append(LAST_COMMITTED_OFFSET).append(" ").append(lastCommittedOffset);
            serializedEvents.add(bld.toString());
        }
    } finally {
        reader.close();
    }
}
Also used : ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ApiMessage(org.apache.kafka.common.protocol.ApiMessage)

Example 5 with ApiMessage

use of org.apache.kafka.common.protocol.ApiMessage in project kafka by apache.

the class KafkaRaftClient method maybeSendRequest.

/**
 * Attempt to send a request. Return the time to wait before the request can be retried.
 */
private long maybeSendRequest(long currentTimeMs, int destinationId, Supplier<ApiMessage> requestSupplier) {
    ConnectionState connection = requestManager.getOrCreate(destinationId);
    if (connection.isBackingOff(currentTimeMs)) {
        long remainingBackoffMs = connection.remainingBackoffMs(currentTimeMs);
        logger.debug("Connection for {} is backing off for {} ms", destinationId, remainingBackoffMs);
        return remainingBackoffMs;
    }
    if (connection.isReady(currentTimeMs)) {
        int correlationId = channel.newCorrelationId();
        ApiMessage request = requestSupplier.get();
        RaftRequest.Outbound requestMessage = new RaftRequest.Outbound(correlationId, request, destinationId, currentTimeMs);
        requestMessage.completion.whenComplete((response, exception) -> {
            if (exception != null) {
                ApiKeys api = ApiKeys.forId(request.apiKey());
                Errors error = Errors.forException(exception);
                ApiMessage errorResponse = RaftUtil.errorResponse(api, error);
                response = new RaftResponse.Inbound(correlationId, errorResponse, destinationId);
            }
            messageQueue.add(response);
        });
        channel.send(requestMessage);
        logger.trace("Sent outbound request: {}", requestMessage);
        connection.onRequestSent(correlationId, currentTimeMs);
        return Long.MAX_VALUE;
    }
    return connection.remainingRequestTimeMs(currentTimeMs);
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Errors(org.apache.kafka.common.protocol.Errors) ApiMessage(org.apache.kafka.common.protocol.ApiMessage) ConnectionState(org.apache.kafka.raft.RequestManager.ConnectionState)

Aggregations

ApiMessage (org.apache.kafka.common.protocol.ApiMessage)8 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)5 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 Errors (org.apache.kafka.common.protocol.Errors)1 ConnectionState (org.apache.kafka.raft.RequestManager.ConnectionState)1 RawSnapshotWriter (org.apache.kafka.snapshot.RawSnapshotWriter)1