{"record":{"id":"974a8f313e9080e2","repo":"kopia/kopia","slug":"error-building-index-shard-974a8f","errorCode":null,"errorMessage":"error building index shard","messagePattern":"error building index shard","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/content/index/one_use_index_builder.go","lineNumber":117,"sourceCode":"\t\tdataShards      []gather.Bytes\n\t\trandomSuffix    [32]byte\n\t)\n\n\tcloseShards := func() {\n\t\tfor _, ds := range dataShardsBuf {\n\t\t\tds.Close()\n\t\t}\n\t}\n\n\tfor _, s := range shardedBuilders {\n\t\tbuf := gather.NewWriteBuffer()\n\n\t\tdataShardsBuf = append(dataShardsBuf, buf)\n\n\t\tif err := buildSortedContents(s, buf, indexVersion); err != nil {\n\t\t\tcloseShards()\n\n\t\t\treturn nil, nil, errors.Wrap(err, \"error building index shard\")\n\t\t}\n\n\t\tif !stable {\n\t\t\tif _, err := rand.Read(randomSuffix[:]); err != nil {\n\t\t\t\tcloseShards()\n\n\t\t\t\treturn nil, nil, errors.Wrap(err, \"error getting random bytes for suffix\")\n\t\t\t}\n\n\t\t\tif _, err := buf.Write(randomSuffix[:]); err != nil {\n\t\t\t\tcloseShards()\n\n\t\t\t\treturn nil, nil, errors.Wrap(err, \"error writing extra random suffix to ensure indexes are always globally unique\")\n\t\t\t}\n\t\t}\n\n\t\tdataShards = append(dataShards, buf.Bytes())\n\t}","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/repo/content/index/one_use_index_builder.go#L99-L135","documentation":"BuildShards writes each sorted content shard via buildSortedContents(s, buf, indexVersion). If serializing a shard's contents into its write buffer fails, the builder closes any already-open shard buffers via closeShards() and returns the error wrapped as \"error building index shard\". This indicates a failure while generating the binary index representation for one shard.","triggerScenarios":"Calling BuildShards when buildSortedContents fails for a shard — typically an unsupported/invalid indexVersion or an internal error while encoding sorted contents into the buffer.","commonSituations":"Passing an index version the builder cannot serialize (e.g. version not registered/supported); contents with invalid entries; buffer allocation failures.","solutions":["Check the wrapped cause and verify indexVersion is a supported value (e.g. the repo's allowed index versions v1/v2).","Validate the content set being sharded has no malformed entries.","Retry the build after fixing inputs; closeShards() already released partial buffers, so no manual cleanup is needed."],"exampleFix":"// before\nshards, cleanup, err := builder.BuildShards(99, stable, size) // unsupported version\n// after\nif indexVersion != 1 && indexVersion != 2 {\n\treturn fmt.Errorf(\"unsupported index version %d\", indexVersion)\n}\nshards, cleanup, err := builder.BuildShards(indexVersion, stable, size)","handlingStrategy":"validation","validationCode":"supported := map[int]bool{1: true, 2: true}\nif !supported[indexVersion] {\n\treturn fmt.Errorf(\"unsupported index version %d\", indexVersion)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only pass index versions supported by the running code version.","Test shard building for each index version you deploy.","Ensure content entries are validated before sharding."],"tags":["index","serialization","build","version"],"backgroundTag":"database-write-failed","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}