Search in sources :

Example 91 with Struct

use of org.apache.kafka.common.protocol.types.Struct in project starlight-for-kafka by datastax.

the class GroupMetadataConstants method groupMetadataKey.

/**
 * Generates the key for group metadata message for given group.
 *
 * @return key bytes for group metadata message
 */
static byte[] groupMetadataKey(String group) {
    Struct key = new Struct(CURRENT_GROUP_KEY_SCHEMA);
    key.set(GROUP_KEY_GROUP_FIELD, group);
    ByteBuffer byteBuffer = ByteBuffer.allocate(2 + /* version */
    key.sizeOf());
    byteBuffer.putShort(CURRENT_GROUP_KEY_SCHEMA_VERSION);
    key.writeTo(byteBuffer);
    return byteBuffer.array();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 92 with Struct

use of org.apache.kafka.common.protocol.types.Struct in project starlight-for-kafka by datastax.

the class KafkaCommandDecoder method byteBufToRequest.

protected KafkaHeaderAndRequest byteBufToRequest(ByteBuf msg, SocketAddress remoteAddress) {
    checkArgument(msg.readableBytes() > 0);
    ByteBuffer nio = msg.nioBuffer();
    RequestHeader header = RequestHeader.parse(nio);
    if (isUnsupportedApiVersionsRequest(header)) {
        ApiVersionsRequest apiVersionsRequest = new ApiVersionsRequest((short) 0, header.apiVersion());
        return new KafkaHeaderAndRequest(header, apiVersionsRequest, msg, remoteAddress);
    } else {
        ApiKeys apiKey = header.apiKey();
        short apiVersion = header.apiVersion();
        Struct struct = apiKey.parseRequest(apiVersion, nio);
        AbstractRequest body = AbstractRequest.parseRequest(apiKey, apiVersion, struct);
        return new KafkaHeaderAndRequest(header, body, msg, remoteAddress);
    }
}
Also used : ApiKeys(org.apache.kafka.common.protocol.ApiKeys) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) RequestHeader(org.apache.kafka.common.requests.RequestHeader) ByteBuffer(java.nio.ByteBuffer) ApiVersionsRequest(org.apache.kafka.common.requests.ApiVersionsRequest) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 93 with Struct

use of org.apache.kafka.common.protocol.types.Struct in project starlight-for-kafka by datastax.

the class TransactionLogKey method decode.

public static TransactionLogKey decode(ByteBuffer byteBuffer, short schemaVersion) {
    Schema schema = getSchema(schemaVersion);
    // skip version
    byteBuffer.getShort();
    Struct struct = schema.read(byteBuffer);
    return new TransactionLogKey(struct.getString(TXN_ID_FIELD));
}
Also used : Schema(org.apache.kafka.common.protocol.types.Schema) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 94 with Struct

use of org.apache.kafka.common.protocol.types.Struct in project starlight-for-kafka by datastax.

the class TransactionLogValue method toByteBuffer.

public ByteBuffer toByteBuffer(short schemaVersion) {
    if (this.getTransactionStatus() == TransactionState.EMPTY.getValue() && !transactionPartitions.isEmpty()) {
        throw new IllegalStateException("Transaction is not expected to have any partitions since its state is " + transactionStatus);
    }
    Struct struct = new Struct(getSchema(schemaVersion));
    struct.set(PRODUCER_ID_FIELD, producerId);
    struct.set(PRODUCER_EPOCH_FIELD, producerEpoch);
    struct.set(TXN_TIMEOUT_MS_FIELD, transactionTimeoutMs);
    struct.set(TXN_STATUS_FIELD, transactionStatus);
    struct.set(TXN_PARTITIONS_FIELD, transactionPartitions.stream().map(PartitionsSchema::toStruct).toArray());
    struct.set(TXN_LAST_UPDATE_TIMESTAMP_FIELD, transactionLastUpdateTimestampMs);
    struct.set(TXN_START_TIMESTAMP_FIELD, transactionStartTimestampMs);
    ByteBuffer byteBuffer = ByteBuffer.allocate(2 + /* version */
    struct.sizeOf());
    byteBuffer.putShort(schemaVersion);
    struct.writeTo(byteBuffer);
    byteBuffer.flip();
    return byteBuffer;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Example 95 with Struct

use of org.apache.kafka.common.protocol.types.Struct in project starlight-for-kafka by datastax.

the class TransactionMarkerChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
    ByteBuffer nio = ((ByteBuf) o).nioBuffer();
    ResponseHeader responseHeader = ResponseHeader.parse(nio);
    InFlightRequest inFlightRequest = inFlightRequestMap.remove(responseHeader.correlationId());
    if (inFlightRequest != null) {
        inFlightRequest.onComplete(nio);
        return;
    }
    PendingGenericRequest genericRequest = genericRequestMap.remove(responseHeader.correlationId());
    if (genericRequest != null) {
        Struct responseBody = genericRequest.apiKeys.parseResponse(genericRequest.apiVersion, nio);
        AbstractResponse response = AbstractResponse.parseResponse(genericRequest.apiKeys, responseBody);
        genericRequest.response.complete(response);
        return;
    }
    log.error("Miss the inFlightRequest with correlationId {}.", responseHeader.correlationId());
}
Also used : ResponseHeader(org.apache.kafka.common.requests.ResponseHeader) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Struct(org.apache.kafka.common.protocol.types.Struct)

Aggregations

Struct (org.apache.kafka.common.protocol.types.Struct)252 ArrayList (java.util.ArrayList)94 ByteBuffer (java.nio.ByteBuffer)91 Map (java.util.Map)73 HashMap (java.util.HashMap)68 TopicPartition (org.apache.kafka.common.TopicPartition)37 Schema (org.apache.kafka.common.protocol.types.Schema)23 List (java.util.List)22 ApiKeys (org.apache.kafka.common.protocol.ApiKeys)15 Test (org.junit.Test)15 Errors (org.apache.kafka.common.protocol.Errors)13 Field (org.apache.kafka.common.protocol.types.Field)13 ByteBuf (io.netty.buffer.ByteBuf)12 ArrayOf (org.apache.kafka.common.protocol.types.ArrayOf)12 RequestHeader (org.apache.kafka.common.requests.RequestHeader)11 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)10 GroupTopicPartition (io.streamnative.pulsar.handlers.kop.coordinator.group.GroupMetadataManager.GroupTopicPartition)8 ByteBufferSend (org.apache.kafka.common.network.ByteBufferSend)8 LinkedHashMap (java.util.LinkedHashMap)7 Lists (com.google.common.collect.Lists)6