{"record":{"id":"44b8e80fc010efeb","repo":"apache/pulsar","slug":"invalid-metavalue-data-size-size","errorCode":null,"errorMessage":"Invalid MetaValue data, size=${size}","messagePattern":"Invalid MetaValue data, size=(.+?)","errorType":"exception","errorClass":"MetadataStoreException","httpStatus":null,"severity":"error","filePath":"pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/RocksdbMetadataStore.java","lineNumber":157,"sourceCode":"            byte[] result = new byte[HEADER_SIZE + data.length];\n            ByteBuffer buffer = ByteBuffer.wrap(result);\n            buffer.putInt(HEADER_SIZE);\n            buffer.putInt(FORMAT_VERSION_V1);\n            buffer.putLong(version);\n            buffer.putLong(owner);\n            buffer.putLong(createdTimestamp);\n            buffer.putLong(modifiedTimestamp);\n            buffer.put((byte) (ephemeral ? 1 : 0));\n            buffer.put(data);\n            return result;\n        }\n\n        public static MetaValue parse(byte[] dataBytes) throws MetadataStoreException {\n            if (dataBytes == null) {\n                return null;\n            }\n            if (dataBytes.length < 4) {\n                throw new MetadataStoreException(\"Invalid MetaValue data, size=\" + dataBytes.length);\n            }\n            ByteBuffer buffer = ByteBuffer.wrap(dataBytes);\n            MetaValue metaValue = new MetaValue();\n            int headerSize = buffer.getInt();\n            if (dataBytes.length < headerSize) {\n                throw new MetadataStoreException(\n                        String.format(\"Invalid MetaValue data, no enough header data. expect %d, actual %d\",\n                                headerSize, dataBytes.length));\n            }\n            int formatVersion = buffer.getInt();\n            if (formatVersion >= FORMAT_VERSION_V1) {\n                metaValue.version = buffer.getLong();\n                metaValue.owner = buffer.getLong();\n                metaValue.createdTimestamp = buffer.getLong();\n                metaValue.modifiedTimestamp = buffer.getLong();\n                metaValue.ephemeral = buffer.get() > 0;\n            } else {\n                throw new MetadataStoreException(\"Invalid MetaValue format version=\" + formatVersion);","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/RocksdbMetadataStore.java#L139-L175","documentation":"RocksdbMetadataStore serializes each value as a MetaValue: a 4-byte big-endian headerSize int followed by the header/serialized metadata. MetaValue.parse() rejects byte arrays shorter than 4 bytes because they cannot even contain the header length field, throwing MetadataStoreException(\"Invalid MetaValue data, size=N\").","triggerScenarios":"Reading a RocksDB record whose stored bytes are shorter than 4 bytes — corrupted database files, manual writes of raw values bypassing MetaValue encoding, truncation, or a store written by an incompatible format version.","commonSituations":"A RocksDB metadata directory corrupted by an earlier crash or copied while the store was live; scripts writing plain JSON/bytes directly into RocksDB; mixing store versions that changed the on-disk format.","solutions":["Restore the RocksDB metadata directory from a backup taken while the store was stopped","Re-serialize values through MetaValue's write path instead of writing raw bytes into the store","If the directory is corrupted, rebuild the metadata store and re-import metadata (or re-run migration tooling)","Do not copy/edit RocksDB files while the metadata store is running"],"exampleFix":"// before\nrocksDBStore.put(path, \"{\\\"value\\\":1}\".getBytes()); // raw bytes, not MetaValue\n// after\nrocksDBStore.put(path, new MetaValue(/* header */).serialize(/* value */)); // proper MetaValue encoding","handlingStrategy":"try-catch","validationCode":"// Java: sanity-check record size before parsing\ncurrent = rocksDB.get(readOptions, key);\nif (current != null && current.length < 4) {\n    throw new IOException(\"Corrupted record (size \" + current.length + \") for key \" + key);\n}\n","typeGuard":"boolean looksLikeMetaValue(byte[] data) {\n    return data != null && data.length >= 4\n        && data.length >= ByteBuffer.wrap(data).getInt(); // length >= declared headerSize\n}\n","tryCatchPattern":"try {\n    MetaValue mv = MetaValue.parse(bytes);\n} catch (MetadataStoreException e) {\n    // record corrupted/foreign: restore from backup or rebuild store\n}\n","preventionTips":["Never write raw bytes into the RocksDB metadata store — always go through the store's encode/put path","Take offline backups of the RocksDB metadata directory","Do not copy RocksDB files while the store is running","Keep store format versions consistent across upgrades"],"tags":["rocksdb","corruption","serialization","metadata-store"],"backgroundTag":"invalid-serialized-metadata-value","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}