{"record":{"id":"b000e2c4c9bdf7e0","repo":"weaviate/weaviate","slug":"write-key-length-encoding-for-node","errorCode":null,"errorMessage":"write key length encoding for node","messagePattern":"write key length encoding for node","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"adapters/repos/db/lsmkv/segment_serialization.go","lineNumber":441,"sourceCode":"\n\t\tvalueLen := uint64(len(value.value))\n\t\tbinary.LittleEndian.PutUint64(buf[1:9], valueLen)\n\t\tif _, err := w.Write(buf[0:9]); err != nil {\n\t\t\treturn segmentindex.KeyRedux{}, errors.Wrapf(err, \"write len of value %d\", i)\n\t\t}\n\t\twritten += 9\n\n\t\tn, err := w.Write(value.value)\n\t\tif err != nil {\n\t\t\treturn segmentindex.KeyRedux{}, errors.Wrapf(err, \"write value %d\", i)\n\t\t}\n\t\twritten += n\n\t}\n\n\tkeyLength := uint32(len(s.primaryKey))\n\tbinary.LittleEndian.PutUint32(buf[0:4], keyLength)\n\tif _, err := w.Write(buf[0:4]); err != nil {\n\t\treturn segmentindex.KeyRedux{}, errors.Wrapf(err, \"write key length encoding for node\")\n\t}\n\twritten += 4\n\n\tn, err := w.Write(s.primaryKey)\n\tif err != nil {\n\t\treturn segmentindex.KeyRedux{}, errors.Wrapf(err, \"write node\")\n\t}\n\twritten += n\n\n\treturn segmentindex.KeyRedux{\n\t\tValueEnd: s.offset + written,\n\t\tKey:      s.primaryKey,\n\t}, nil\n}\n\nfunc (s segmentCollectionNode) KeyIndexAndWriteTo(w io.Writer) (segmentindex.Key, error) {\n\tout := segmentindex.Key{}\n\twritten := 0","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/weaviate/weaviate/blob/75aa4b6d11f8818305aafd4440b4e32794f7ca04/adapters/repos/db/lsmkv/segment_serialization.go#L423-L459","documentation":"At the end of KeyIndexAndWriteToRedux the primary key is serialized: a 4-byte little-endian key length header first. This error wraps failure writing those 4 bytes. It means the destination writer failed after all values were written, so the node was left partially serialized — the segment on disk is incomplete and must not be trusted. Root cause is a storage-level failure of the writer.","triggerScenarios":"During collection flush/compaction, the writer fails while emitting the key-length header after values were written: ENOSPC, EIO, or closed file descriptor. Also possible when writing to an in-memory buffer that is exhausted (rare, mainly tests/benchmarks).","commonSituations":"Disk hitting capacity during the tail end of a compaction; disk error late in a large flush; process resource exhaustion causing fd errors.","solutions":["Fix the storage issue (space/IO/permissions) and retry the flush or compaction","Remove the partially written segment file so startup does not parse a torn node","Re-run compaction from the source segments once storage is healthy","Alert on disk usage; keep headroom for compaction (can temporarily need ~2x segment space)","Inspect the wrapped root error for the exact errno"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// check remaining space covers the tail of the node being written\nif freeBytes(dataDir) < int64(4+len(node.primaryKey))+minHeadroom {\n    return fmt.Errorf(\"insufficient space to finish node serialization\")\n}","typeGuard":null,"tryCatchPattern":"redux, err := node.KeyIndexAndWriteToRedux(w, buf)\nif err != nil {\n    if strings.Contains(err.Error(), \"write key length encoding\") {\n        // node left partially written: remove segment output, fix storage, retry\n    }\n    return err\n}","preventionTips":["Keep compaction headroom (~2x segment size) free on the data volume","Alert before disks reach capacity, not after writes fail","After failure, discard the partial compaction output and re-run","Verify mount writability after host maintenance","Capture the wrapped errno for root-cause analysis"],"tags":["lsmkv","io","disk-full","write","key-serialization"],"backgroundTag":"disk-full-write-failed","analyzedSha":"75aa4b6d11f8818305aafd4440b4e32794f7ca04","analyzedAt":"2026-09-04T14:58:20.392Z","contentChangedAt":"2026-09-04T14:58:20.392Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}