apache/beam · error

monogdbio.calculateBucketCount: bundle size must be greater

Error message

monogdbio.calculateBucketCount: bundle size must be greater than 0

What it means

calculateBucketCount computes how many buckets to split a collection into given total collection size and a per-bundle size. A bundleSize of 0 would divide by zero, so the function panics; note the guard actually checks bundleSize < 0, so a 0 also reaches the division and panics with an integer-divide-by-zero. The check enforces a positive bundle size.

Source

Thrown at sdks/go/pkg/beam/io/mongodbio/id_range_split.go:72

}

func getCollectionSize(ctx context.Context, collection *mongo.Collection) (int64, error) {
	cmd := bson.M{"collStats": collection.Name()}
	opts := options.RunCmd().SetReadPreference(readpref.Primary())

	var stats struct {
		Size int64 `bson:"size"`
	}
	if err := collection.Database().RunCommand(ctx, cmd, opts).Decode(&stats); err != nil {
		return 0, fmt.Errorf("error executing collStats command: %w", err)
	}

	return stats.Size, nil
}

func calculateBucketCount(totalSize int64, bundleSize int64) int32 {
	if bundleSize < 0 {
		panic("monogdbio.calculateBucketCount: bundle size must be greater than 0")
	}

	count := totalSize / bundleSize
	if totalSize%bundleSize != 0 {
		count++
	}

	if count > int64(maxBucketCount) {
		count = maxBucketCount
	}

	return int32(count)
}

type bucket struct {
	ID minMax `bson:"_id"`
}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Always leave BundleSize at its default (defaultReadBundleSize) or set it to a positive value
  2. Guard user option funcs: if size <= 0 { return fmt.Errorf(...) } so Read panics with the clearer invalid-option message
  3. Check the config source producing 0 (missing key, wrong unit)
  4. Prefer clamping: if option.BundleSize <= 0 { option.BundleSize = defaultReadBundleSize }

Example fix

// before
option := &mongodbio.ReadOption{} // BundleSize = 0
read := mongodbio.Read(s, scope, uri, db, col, typ, option)
// after
option := &mongodbio.ReadOption{BundleSize: 64 * 1024 * 1024}
read := mongodbio.Read(s, scope, uri, db, col, typ, option)
Defensive patterns

Strategy: validation

Validate before calling

if option.BundleSize <= 0 { return fmt.Errorf("BundleSize must be > 0") }

Prevention

When it happens

Trigger: Calling mongodbio.Read with a ReadOption whose BundleSize is 0 or negative, e.g. a user-set option or a computed size that ended up zero (uninitialized struct, division result, misread unit like MB vs bytes yielding 0).

Common situations: Custom option funcs setting BundleSize from config that is missing (0 default); unit conversion mistakes (e.g. MB value of 0); copying option struct with zero-value field.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/c0f960af563fce5f. Report an issue: GitHub.