Search in sources :

Example 6 with Header

use of org.apache.kafka.common.header.Header in project apache-kafka-on-k8s by banzaicloud.

the class DefaultRecord method writeTo.

/**
 * Write the record to `out` and return its size.
 */
public static int writeTo(DataOutputStream out, int offsetDelta, long timestampDelta, ByteBuffer key, ByteBuffer value, Header[] headers) throws IOException {
    int sizeInBytes = sizeOfBodyInBytes(offsetDelta, timestampDelta, key, value, headers);
    ByteUtils.writeVarint(sizeInBytes, out);
    // there are no used record attributes at the moment
    byte attributes = 0;
    out.write(attributes);
    ByteUtils.writeVarlong(timestampDelta, out);
    ByteUtils.writeVarint(offsetDelta, out);
    if (key == null) {
        ByteUtils.writeVarint(-1, out);
    } else {
        int keySize = key.remaining();
        ByteUtils.writeVarint(keySize, out);
        Utils.writeTo(out, key, keySize);
    }
    if (value == null) {
        ByteUtils.writeVarint(-1, out);
    } else {
        int valueSize = value.remaining();
        ByteUtils.writeVarint(valueSize, out);
        Utils.writeTo(out, value, valueSize);
    }
    if (headers == null)
        throw new IllegalArgumentException("Headers cannot be null");
    ByteUtils.writeVarint(headers.length, out);
    for (Header header : headers) {
        String headerKey = header.key();
        if (headerKey == null)
            throw new IllegalArgumentException("Invalid null header key found in headers");
        byte[] utf8Bytes = Utils.utf8(headerKey);
        ByteUtils.writeVarint(utf8Bytes.length, out);
        out.write(utf8Bytes);
        byte[] headerValue = header.value();
        if (headerValue == null) {
            ByteUtils.writeVarint(-1, out);
        } else {
            ByteUtils.writeVarint(headerValue.length, out);
            out.write(headerValue);
        }
    }
    return ByteUtils.sizeOfVarint(sizeInBytes) + sizeInBytes;
}
Also used : RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header)

Example 7 with Header

use of org.apache.kafka.common.header.Header in project incubator-skywalking by apache.

the class KafkaConsumerInterceptor method afterMethod.

@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
    Map<TopicPartition, List<ConsumerRecord<?, ?>>> records = (Map<TopicPartition, List<ConsumerRecord<?, ?>>>) ret;
    // 
    if (records.size() > 0) {
        ConsumerEnhanceRequiredInfo requiredInfo = (ConsumerEnhanceRequiredInfo) objInst.getSkyWalkingDynamicField();
        AbstractSpan activeSpan = ContextManager.createEntrySpan(OPERATE_NAME_PREFIX + requiredInfo.getTopics() + CONSUMER_OPERATE_NAME_SUFFIX, null).start(requiredInfo.getStartTime());
        activeSpan.setComponent(ComponentsDefine.KAFKA);
        SpanLayer.asMQ(activeSpan);
        Tags.MQ_BROKER.set(activeSpan, requiredInfo.getBrokerServers());
        Tags.MQ_TOPIC.set(activeSpan, requiredInfo.getTopics());
        for (List<ConsumerRecord<?, ?>> consumerRecords : records.values()) {
            for (ConsumerRecord<?, ?> record : consumerRecords) {
                ContextCarrier contextCarrier = new ContextCarrier();
                CarrierItem next = contextCarrier.items();
                while (next.hasNext()) {
                    next = next.next();
                    Iterator<Header> iterator = record.headers().headers(next.getHeadKey()).iterator();
                    if (iterator.hasNext()) {
                        next.setHeadValue(new String(iterator.next().value()));
                    }
                }
                ContextManager.extract(contextCarrier);
            }
        }
        ContextManager.stopSpan();
    }
    return ret;
}
Also used : ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan) CarrierItem(org.apache.skywalking.apm.agent.core.context.CarrierItem) Header(org.apache.kafka.common.header.Header) TopicPartition(org.apache.kafka.common.TopicPartition) List(java.util.List) Map(java.util.Map)

Example 8 with Header

use of org.apache.kafka.common.header.Header in project apache-kafka-on-k8s by banzaicloud.

the class DefaultRecord method readFrom.

private static DefaultRecord readFrom(ByteBuffer buffer, int sizeInBytes, int sizeOfBodyInBytes, long baseOffset, long baseTimestamp, int baseSequence, Long logAppendTime) {
    try {
        int recordStart = buffer.position();
        byte attributes = buffer.get();
        long timestampDelta = ByteUtils.readVarlong(buffer);
        long timestamp = baseTimestamp + timestampDelta;
        if (logAppendTime != null)
            timestamp = logAppendTime;
        int offsetDelta = ByteUtils.readVarint(buffer);
        long offset = baseOffset + offsetDelta;
        int sequence = baseSequence >= 0 ? DefaultRecordBatch.incrementSequence(baseSequence, offsetDelta) : RecordBatch.NO_SEQUENCE;
        ByteBuffer key = null;
        int keySize = ByteUtils.readVarint(buffer);
        if (keySize >= 0) {
            key = buffer.slice();
            key.limit(keySize);
            buffer.position(buffer.position() + keySize);
        }
        ByteBuffer value = null;
        int valueSize = ByteUtils.readVarint(buffer);
        if (valueSize >= 0) {
            value = buffer.slice();
            value.limit(valueSize);
            buffer.position(buffer.position() + valueSize);
        }
        int numHeaders = ByteUtils.readVarint(buffer);
        if (numHeaders < 0)
            throw new InvalidRecordException("Found invalid number of record headers " + numHeaders);
        final Header[] headers;
        if (numHeaders == 0)
            headers = Record.EMPTY_HEADERS;
        else
            headers = readHeaders(buffer, numHeaders);
        // validate whether we have read all header bytes in the current record
        if (buffer.position() - recordStart != sizeOfBodyInBytes)
            throw new InvalidRecordException("Invalid record size: expected to read " + sizeOfBodyInBytes + " bytes in record payload, but instead read " + (buffer.position() - recordStart));
        return new DefaultRecord(sizeInBytes, attributes, offset, timestamp, sequence, key, value, headers);
    } catch (BufferUnderflowException | IllegalArgumentException e) {
        throw new InvalidRecordException("Found invalid record structure", e);
    }
}
Also used : RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 9 with Header

use of org.apache.kafka.common.header.Header in project apache-kafka-on-k8s by banzaicloud.

the class DefaultRecord method sizeOf.

private static int sizeOf(int keySize, int valueSize, Header[] headers) {
    int size = 0;
    if (keySize < 0)
        size += NULL_VARINT_SIZE_BYTES;
    else
        size += ByteUtils.sizeOfVarint(keySize) + keySize;
    if (valueSize < 0)
        size += NULL_VARINT_SIZE_BYTES;
    else
        size += ByteUtils.sizeOfVarint(valueSize) + valueSize;
    if (headers == null)
        throw new IllegalArgumentException("Headers cannot be null");
    size += ByteUtils.sizeOfVarint(headers.length);
    for (Header header : headers) {
        String headerKey = header.key();
        if (headerKey == null)
            throw new IllegalArgumentException("Invalid null header key found in headers");
        int headerKeySize = Utils.utf8Length(headerKey);
        size += ByteUtils.sizeOfVarint(headerKeySize) + headerKeySize;
        byte[] headerValue = header.value();
        if (headerValue == null) {
            size += NULL_VARINT_SIZE_BYTES;
        } else {
            size += ByteUtils.sizeOfVarint(headerValue.length) + headerValue.length;
        }
    }
    return size;
}
Also used : RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header)

Example 10 with Header

use of org.apache.kafka.common.header.Header in project apache-kafka-on-k8s by banzaicloud.

the class DefaultRecord method readHeaders.

private static Header[] readHeaders(ByteBuffer buffer, int numHeaders) {
    Header[] headers = new Header[numHeaders];
    for (int i = 0; i < numHeaders; i++) {
        int headerKeySize = ByteUtils.readVarint(buffer);
        if (headerKeySize < 0)
            throw new InvalidRecordException("Invalid negative header key size " + headerKeySize);
        String headerKey = Utils.utf8(buffer, headerKeySize);
        buffer.position(buffer.position() + headerKeySize);
        ByteBuffer headerValue = null;
        int headerValueSize = ByteUtils.readVarint(buffer);
        if (headerValueSize >= 0) {
            headerValue = buffer.slice();
            headerValue.limit(headerValueSize);
            buffer.position(buffer.position() + headerValueSize);
        }
        headers[i] = new RecordHeader(headerKey, headerValue);
    }
    return headers;
}
Also used : RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) ByteBuffer(java.nio.ByteBuffer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader)

Aggregations

Header (org.apache.kafka.common.header.Header)13 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)9 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)5 TopicPartition (org.apache.kafka.common.TopicPartition)4 DataOutputStream (java.io.DataOutputStream)3 ByteBufferOutputStream (org.apache.kafka.common.utils.ByteBufferOutputStream)3 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)2 BufferUnderflowException (java.nio.BufferUnderflowException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 ConnectionContext (loghub.ConnectionContext)1 DecodeException (loghub.Decoder.DecodeException)1 Event (loghub.Event)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 OffsetCommitCallback (org.apache.kafka.clients.consumer.OffsetCommitCallback)1