Search in sources :

Example 6 with InvalidRecordException

use of org.apache.kafka.common.InvalidRecordException in project kafka by apache.

the class EndTransactionMarker method deserializeValue.

static EndTransactionMarker deserializeValue(ControlRecordType type, ByteBuffer value) {
    ensureTransactionMarkerControlType(type);
    if (value.remaining() < CURRENT_END_TXN_MARKER_VALUE_SIZE)
        throw new InvalidRecordException("Invalid value size found for end transaction marker. Must have " + "at least " + CURRENT_END_TXN_MARKER_VALUE_SIZE + " bytes, but found only " + value.remaining());
    short version = value.getShort(0);
    if (version < 0)
        throw new InvalidRecordException("Invalid version found for end transaction marker: " + version + ". May indicate data corruption");
    if (version > CURRENT_END_TXN_MARKER_VERSION)
        log.debug("Received end transaction marker value version {}. Parsing as version {}", version, CURRENT_END_TXN_MARKER_VERSION);
    int coordinatorEpoch = value.getInt(2);
    return new EndTransactionMarker(type, coordinatorEpoch);
}
Also used : InvalidRecordException(org.apache.kafka.common.InvalidRecordException)

Example 7 with InvalidRecordException

use of org.apache.kafka.common.InvalidRecordException in project kafka by apache.

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);
        ByteBuffer headerKeyBuffer = buffer.slice();
        headerKeyBuffer.limit(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(headerKeyBuffer, headerValue);
    }
    return headers;
}
Also used : RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) InvalidRecordException(org.apache.kafka.common.InvalidRecordException) ByteBuffer(java.nio.ByteBuffer) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader)

Example 8 with InvalidRecordException

use of org.apache.kafka.common.InvalidRecordException in project kafka by apache.

the class DefaultRecord method readFrom.

public static DefaultRecord readFrom(ByteBuffer buffer, long baseOffset, long baseTimestamp, int baseSequence, Long logAppendTime) {
    int sizeOfBodyInBytes = ByteUtils.readVarint(buffer);
    if (buffer.remaining() < sizeOfBodyInBytes)
        throw new InvalidRecordException("Invalid record size: expected " + sizeOfBodyInBytes + " bytes in record payload, but instead the buffer has only " + buffer.remaining() + " remaining bytes.");
    int totalSizeInBytes = ByteUtils.sizeOfVarint(sizeOfBodyInBytes) + sizeOfBodyInBytes;
    return readFrom(buffer, totalSizeInBytes, sizeOfBodyInBytes, baseOffset, baseTimestamp, baseSequence, logAppendTime);
}
Also used : InvalidRecordException(org.apache.kafka.common.InvalidRecordException)

Example 9 with InvalidRecordException

use of org.apache.kafka.common.InvalidRecordException in project kafka by apache.

the class SenderTest method testRecordErrorPropagatedToApplication.

@Test
public void testRecordErrorPropagatedToApplication() throws InterruptedException {
    int recordCount = 5;
    setup();
    Map<Integer, FutureRecordMetadata> futures = new HashMap<>(recordCount);
    for (int i = 0; i < recordCount; i++) {
        futures.put(i, appendToAccumulator(tp0));
    }
    // send request
    sender.runOnce();
    assertEquals(1, client.inFlightRequestCount());
    assertEquals(1, sender.inFlightBatches(tp0).size());
    OffsetAndError offsetAndError = new OffsetAndError(-1L, Errors.INVALID_RECORD, Arrays.asList(new BatchIndexAndErrorMessage().setBatchIndex(0).setBatchIndexErrorMessage("0"), new BatchIndexAndErrorMessage().setBatchIndex(2).setBatchIndexErrorMessage("2"), new BatchIndexAndErrorMessage().setBatchIndex(3)));
    client.respond(produceResponse(Collections.singletonMap(tp0, offsetAndError)));
    sender.runOnce();
    for (Map.Entry<Integer, FutureRecordMetadata> futureEntry : futures.entrySet()) {
        FutureRecordMetadata future = futureEntry.getValue();
        assertTrue(future.isDone());
        KafkaException exception = TestUtils.assertFutureThrows(future, KafkaException.class);
        Integer index = futureEntry.getKey();
        if (index == 0 || index == 2) {
            assertTrue(exception instanceof InvalidRecordException);
            assertEquals(index.toString(), exception.getMessage());
        } else if (index == 3) {
            assertTrue(exception instanceof InvalidRecordException);
            assertEquals(Errors.INVALID_RECORD.message(), exception.getMessage());
        } else {
            assertEquals(KafkaException.class, exception.getClass());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) KafkaException(org.apache.kafka.common.KafkaException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) InvalidRecordException(org.apache.kafka.common.InvalidRecordException) BatchIndexAndErrorMessage(org.apache.kafka.common.message.ProduceResponseData.BatchIndexAndErrorMessage) Test(org.junit.jupiter.api.Test)

Aggregations

InvalidRecordException (org.apache.kafka.common.InvalidRecordException)9 ByteBuffer (java.nio.ByteBuffer)4 BufferUnderflowException (java.nio.BufferUnderflowException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 KafkaException (org.apache.kafka.common.KafkaException)2 Header (org.apache.kafka.common.header.Header)2 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)2 RecordBatch (org.apache.kafka.common.record.RecordBatch)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 IdentityHashMap (java.util.IdentityHashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Objects (java.util.Objects)1 OptionalLong (java.util.OptionalLong)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1