{"record":{"id":"7b4446aeb621d326","repo":"apache/beam","slug":"error-executing-collstats-command-w","errorCode":null,"errorMessage":"error executing collStats command: %w","messagePattern":"error executing collStats command: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/id_range_split.go","lineNumber":64,"sourceCode":"\tbucketCount := calculateBucketCount(collSize, bundleSize)\n\n\tbuckets, err := getBuckets(ctx, collection, outerRange.Filter(), bucketCount)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn idRangesFromBuckets(buckets, outerRange), nil\n}\n\nfunc getCollectionSize(ctx context.Context, collection *mongo.Collection) (int64, error) {\n\tcmd := bson.M{\"collStats\": collection.Name()}\n\topts := options.RunCmd().SetReadPreference(readpref.Primary())\n\n\tvar stats struct {\n\t\tSize int64 `bson:\"size\"`\n\t}\n\tif err := collection.Database().RunCommand(ctx, cmd, opts).Decode(&stats); err != nil {\n\t\treturn 0, fmt.Errorf(\"error executing collStats command: %w\", err)\n\t}\n\n\treturn stats.Size, nil\n}\n\nfunc calculateBucketCount(totalSize int64, bundleSize int64) int32 {\n\tif bundleSize < 0 {\n\t\tpanic(\"monogdbio.calculateBucketCount: bundle size must be greater than 0\")\n\t}\n\n\tcount := totalSize / bundleSize\n\tif totalSize%bundleSize != 0 {\n\t\tcount++\n\t}\n\n\tif count > int64(maxBucketCount) {\n\t\tcount = maxBucketCount\n\t}","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/id_range_split.go#L46-L82","documentation":"getCollectionSize runs the server-side collStats command (with primary read preference) to learn the collection's byte size, which bucketAutoSplits uses to decide how many buckets to create. When the RunCommand call or BSON decode fails, the error is wrapped as 'error executing collStats command'. This aborts the bucketAuto split strategy.","triggerScenarios":"bucketAutoSplits calls getCollectionSize; collection.Database().RunCommand(ctx, {collStats: ...}) fails — server unavailable, command not authorized, context deadline exceeded, or the reply cannot be decoded into the stats struct.","commonSituations":"Connected user lacks the collStats privilege; MongoDB version or topology where the command is rejected; network interruption during split planning; context canceled by the runner before the command completes.","solutions":["Check the wrapped cause: authorization errors require granting collStats on the database","Verify connectivity to the primary (the command forces readpref.Primary)","Increase the context timeout for split planning if the deadline is being exceeded","Fall back to the splitVector or single-split strategy by configuring the splitter accordingly","Confirm server version supports collStats for the target namespace"],"exampleFix":"// before\nclient, err := mongo.Connect(ctx, options.Client().ApplyURI(uri)) // user without collStats\n// after — grant role in mongo shell:\n// db.grantRolesToUser(\"beamReader\", [{role: \"read\", db: \"mydb\"}])\nclient, err := mongo.Connect(ctx, options.Client().ApplyURI(uri), options.Client().SetAuth(options.Credential{Username: \"beamReader\", Password: pwd}))","handlingStrategy":"try-catch","validationCode":"// check privileges ahead of time\nvar ok struct{ Ok int }\nerr := db.RunCommand(ctx, bson.D{{Key:\"collStats\", Value: coll.Name()}}).Decode(&ok)","typeGuard":null,"tryCatchPattern":"size, err := getCollectionSize(ctx, coll)\nif err != nil {\n\treturn fallbackSplitStrategy(ctx, coll, r, n) // e.g. splitVector or single split\n}","preventionTips":["Grant read (includes collStats) to the pipeline user","Use splitVector strategy on managed services with restricted commands","Set adequate command timeouts","Verify server version before choosing bucketAuto splitting"],"tags":["mongodb","go","collstats","split"],"backgroundTag":"database-query-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}