microsoft/FASTER · error · FasterException

Invalid segment size

Error message

Invalid segment size: {segmentSize}

What it means

Segment sizes must be powers of two because the address space uses bit shifting/masking (segmentSizeBits/segmentSizeMask) for fast segment arithmetic. Initialize checks Utility.IsPowerOfTwo and throws FasterException for any non-power-of-two size other than the special value -1 (which disables segmentation).

Solutions

  1. Use a power-of-two segment size: 64MB = 1<<26, 1GB = 1<<30, etc.
  2. Use the bits-based constructor/factory helpers (segmentSizeBits) to guarantee power-of-two.
  3. Pass segmentSize = -1 if unbounded segment growth is desired.
  4. Round the intended size up to the next power of two (e.g., 100MB -> 128MB).

Example fix

// before
device.Initialize(segmentSize: 100 * 1024 * 1024); // 100 MiB, not a power of two
// after
device.Initialize(segmentSize: 1L << 27); // 128 MiB, power of two
Defensive patterns

Strategy: validation

Validate before calling

static bool IsPowerOfTwo(long x) => x > 0 && (x & (x - 1)) == 0;
if (segmentSize != -1 && !IsPowerOfTwo(segmentSize))
    throw new ArgumentException("segmentSize must be a power of two");

Try / catch

try { device.Initialize(segmentSize, epoch); } catch (FasterException e) when (e.Message.Contains("Invalid segment size")) { /* round to next power of two */ }

Prevention

When it happens

Trigger: Calling Initialize with segmentSize values like 100MB (100 * 1024 * 1024), 10MB, 3GB, or any non-power-of-two byte count.

Common situations: Users converting from sizes in 'round decimal' units (10MB/100MB) instead of binary powers; hardcoding sizes like 500MB; misconfigured bits-to-bytes conversion (e.g., using segmentSizeBits as bytes).

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of microsoft/FASTER@321d872eab (2026-09-15). Data as JSON: /api/errors/f118df81fa3a0dce. Report an issue: GitHub.

Appendix: source

Thrown at cs/src/core/Device/StorageDeviceBase.cs:116

        /// <param name="segmentSize"></param>
        /// <param name="epoch"></param>
        /// <param name="omitSegmentIdFromFilename"></param>
        public virtual void Initialize(long segmentSize, LightEpoch epoch = null, bool omitSegmentIdFromFilename = false)
        {
            if (segmentSize != -1)
            { 
                if (Capacity != -1 && Capacity % segmentSize != 0)
                    throw new FasterException("capacity must be a multiple of segment sizes");
                if (omitSegmentIdFromFilename)
                    throw new FasterException("omitSegmentIdInFilename requires a segment size of -1");
            }
            this.segmentSize = segmentSize;
            this.epoch = epoch;
            this.OmitSegmentIdFromFileName = omitSegmentIdFromFilename;
            if (!Utility.IsPowerOfTwo(segmentSize))
            {
                if (segmentSize != -1)
                    throw new FasterException("Invalid segment size: " + segmentSize);
                segmentSizeBits = 64;
                segmentSizeMask = ~0UL;
            }
            else
            {
                segmentSizeBits = Utility.GetLogBase2((ulong)segmentSize);
                segmentSizeMask = (ulong)segmentSize - 1;
            }
        }

        /// <summary>
        /// Create a filename that may or may not include the segmentId
        /// </summary>
        protected internal string GetSegmentFilename(string filename, int segmentId) => GetSegmentFilename(filename, segmentId, OmitSegmentIdFromFileName);

        /// <summary>
        /// Create a filename that may or may not include the segmentId
        /// </summary>

View on GitHub (pinned to 321d872eab)