{"id":"c9b1fe7e8a799558","repo":"apache/kafka","slug":"record-is-corrupt-crc-could-not-be-retrieved-as-t","errorCode":null,"errorMessage":"Record is corrupt (crc could not be retrieved as the record is too small, size = {})","messagePattern":"Record is corrupt \\(crc could not be retrieved as the record is too small, size = (.+?)\\)","errorType":"exception","errorClass":"CorruptRecordException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/record/internal/LegacyRecord.java","lineNumber":132,"sourceCode":"     * Retrieve the previously computed CRC for this record\n     */\n    public long checksum() {\n        return ByteUtils.readUnsignedInt(buffer, CRC_OFFSET);\n    }\n\n    /**\n     * Returns true if the crc stored with the record matches the crc computed off the record contents\n     */\n    public boolean isValid() {\n        return sizeInBytes() >= RECORD_OVERHEAD_V0 && checksum() == computeChecksum();\n    }\n\n    /**\n     * Throw an CorruptRecordException if isValid is false for this record\n     */\n    public void ensureValid() {\n        if (sizeInBytes() < RECORD_OVERHEAD_V0)\n            throw new CorruptRecordException(\"Record is corrupt (crc could not be retrieved as the record is too \"\n                    + \"small, size = \" + sizeInBytes() + \")\");\n\n        if (!isValid())\n            throw new CorruptRecordException(\"Record is corrupt (stored crc = \" + checksum()\n                    + \", computed crc = \" + computeChecksum() + \")\");\n    }\n\n    /**\n     * The complete serialized size of this record in bytes (including crc, header attributes, etc), but\n     * excluding the log overhead (offset and record size).\n     * @return the size in bytes\n     */\n    public int sizeInBytes() {\n        return buffer.limit();\n    }\n\n    /**\n     * The length of the key in bytes","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/record/internal/LegacyRecord.java#L114-L150","documentation":"Thrown by LegacyRecord.ensureValid when the record buffer is smaller than RECORD_OVERHEAD_V0 (14 bytes), so there is not even room for the CRC field. This means the on-disk record is truncated or corrupted below the minimum v0 header size, and the CRC cannot be read at all. It is a CorruptRecordException — Kafka treats the record as unparseable.","triggerScenarios":"Invoking LegacyRecord.ensureValid() on a record whose buffer.limit() < 14. Reached via the v0/v1 record iterator when reading legacy message-format segments, during log validation/recovery, or in clients consuming old-format records from a broker still on message.format.version 0 or 1.","commonSituations":"Reading a log segment truncated mid-record (power loss, disk full, unclean shutdown), partial writes from a crashed producer/broker, or a segment file whose last batch is incomplete. Common when message.format.version is still set to 0/1 on an old cluster being upgraded.","solutions":["Run the broker's log recovery (delete the recovery-point checkpoint so the segment is re-validated); the corrupt tail record will be truncated.","If the corruption is on a non-active segment, restore the segment from an in-sync replica rather than repairing by hand.","Upgrade message.format.version to v2 (2.x) so newer batches carry length prefixes that make truncation detection cleaner.","Enable unclean.leader.election only when you accept potential data loss; otherwise the ISR-based truncation avoids the partial write in the first place."],"exampleFix":"// before: validating a record from a possibly-truncated segment\nLegacyRecord record = new LegacyRecord(buffer);\nrecord.ensureValid();  // throws CorruptRecordException if buffer < 14 bytes\n\n// after: skip undersized buffers during recovery and log them\nLegacyRecord record = new LegacyRecord(buffer);\nif (buffer.remaining() >= LegacyRecord.RECORD_OVERHEAD_V0) {\n    record.ensureValid();\n} else {\n    log.warn(\"Skipping undersized legacy record of {} bytes during recovery\", buffer.remaining());\n}","handlingStrategy":"try-catch","validationCode":"if (record.sizeInBytes() < LegacyRecord.RECORD_OVERHEAD_V0) {\n    // undersized / truncated frame; skip rather than call ensureValid()\n    log.warn(\"Skipping record: {} bytes is smaller than v0 overhead {}\",\n             record.sizeInBytes(), LegacyRecord.RECORD_OVERHEAD_V0);\n    continue;\n}","typeGuard":null,"tryCatchPattern":"try {\n    record.ensureValid();\n} catch (CorruptRecordException e) {\n    // record frame is too small to even carry a CRC; quarantine and continue\n    log.warn(\"Discarding undersized corrupt record\", e);\n    metrics.corruptRecord();\n}","preventionTips":["Prefer message format v2 (magic=2) over legacy v0/v1; it has stronger framing.","Gate on record.sizeInBytes() >= LegacyRecord.RECORD_OVERHEAD_V0 before touching fields.","Handle CorruptRecordException in consumers and skip-and-commit rather than blocking the partition."],"tags":["legacy-record","crc","corruption","recovery","message-format"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}