{"record":{"id":"5ea87604a0f7b17e","repo":"thanos-io/thanos","slug":"writing-streamed-snappy-header","errorCode":null,"errorMessage":"writing streamed snappy header","messagePattern":"writing streamed snappy header","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/store/postings_codec.go","lineNumber":96,"sourceCode":"\tconst maxBlockSize = 65536\n\n\tlength = 5 * length / 4 // estimate 1.25B per posting.\n\n\tblocks := length / maxBlockSize\n\n\tret += blocks * snappy.MaxEncodedLen(maxBlockSize)\n\tlength -= blocks * maxBlockSize\n\tif length > 0 {\n\t\tret += snappy.MaxEncodedLen(length)\n\t}\n\n\treturn ret\n}\n\nfunc diffVarintSnappyStreamedEncode(p index.Postings, length int) ([]byte, error) {\n\tcompressedBuf := bytes.NewBuffer(make([]byte, 0, estimateSnappyStreamSize(length)))\n\tif n, err := compressedBuf.WriteString(codecHeaderStreamedSnappy); err != nil {\n\t\treturn nil, fmt.Errorf(\"writing streamed snappy header\")\n\t} else if n != len(codecHeaderStreamedSnappy) {\n\t\treturn nil, fmt.Errorf(\"short-write streamed snappy header\")\n\t}\n\n\tuvarintEncodeBuf := make([]byte, binary.MaxVarintLen64)\n\n\tsw, err := extsnappy.Compressor.Compress(compressedBuf)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"creating snappy compressor: %w\", err)\n\t}\n\n\tprev := storage.SeriesRef(0)\n\tfor p.Next() {\n\t\tv := p.At()\n\t\tif v < prev {\n\t\t\treturn nil, errors.Errorf(\"postings entries must be in increasing order, current: %d, previous: %d\", v, prev)\n\t\t}\n","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/store/postings_codec.go#L78-L114","documentation":"diffVarintSnappyStreamedEncode fails when writing the streamed-snappy magic header into the output buffer errors. bytes.Buffer.WriteString only errors on allocation failure, so this almost always indicates memory exhaustion.","triggerScenarios":"Calling encodePostingsToCache -> diffVarintSnappyStreamedEncode on a system where bytes.NewBuffer cannot allocate/append due to OOM conditions.","commonSituations":"Very large postings sets during cache encoding combined with memory pressure; container memory limits being hit.","solutions":["Increase available memory / container memory limits","Reduce cache encoding concurrency","Check cgroup OOM events around the failure"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if debug.FreeOSMemory != nil && runtime.MemStats{} /* check headroom */ {\n    var ms runtime.MemStats; runtime.ReadMemStats(&ms)\n    if ms.Sys > ms.Sys/10*9 { return errors.New(\"insufficient memory headroom for encoding\") }\n}","typeGuard":null,"tryCatchPattern":"b, err := diffVarintSnappyStreamedEncode(p, length)\nif err != nil {\n    if strings.Contains(err.Error(), \"writing streamed snappy header\") {\n        return retryWithBackoff(func() ([]byte, error) { return diffVarintSnappyStreamedEncode(p, length) })\n    }\n    return nil, err\n}","preventionTips":["Right-size memory limits for the store node relative to postings set sizes","Throttle concurrent cache encodings","Alert on container OOM kill events"],"tags":["snappy","encoding","memory"],"backgroundTag":"internal-invariant-violation","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}