{"record":{"id":"60dc8dde585ab528","repo":"apache/flink","slug":"segment-size-must-be-a-power-of-2","errorCode":null,"errorMessage":"Segment size must be a power of 2!","messagePattern":"Segment size must be a power of 2!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/memory/RandomAccessOutputView.java","lineNumber":44,"sourceCode":"public class RandomAccessOutputView extends AbstractPagedOutputView\n        implements SeekableDataOutputView {\n    private final MemorySegment[] segments;\n\n    private int currentSegmentIndex;\n\n    private final int segmentSizeBits;\n\n    private final int segmentSizeMask;\n\n    public RandomAccessOutputView(MemorySegment[] segments, int segmentSize) {\n        this(segments, segmentSize, MathUtils.log2strict(segmentSize));\n    }\n\n    public RandomAccessOutputView(MemorySegment[] segments, int segmentSize, int segmentSizeBits) {\n        super(segments[0], segmentSize, 0);\n\n        if ((segmentSize & (segmentSize - 1)) != 0) {\n            throw new IllegalArgumentException(\"Segment size must be a power of 2!\");\n        }\n\n        this.segments = segments;\n        this.segmentSizeBits = segmentSizeBits;\n        this.segmentSizeMask = segmentSize - 1;\n    }\n\n    @Override\n    protected MemorySegment nextSegment(MemorySegment current, int positionInCurrent)\n            throws EOFException {\n        if (++this.currentSegmentIndex < this.segments.length) {\n            return this.segments[this.currentSegmentIndex];\n        } else {\n            throw new EOFException();\n        }\n    }\n\n    @Override","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/memory/RandomAccessOutputView.java#L26-L62","documentation":"RandomAccessOutputView(MemorySegment[], int segmentSize) requires every segment to be the same size and that size to be a power of two, because it derives segmentSizeBits/segmentSizeMask from log2strict(segmentSize) and masks offsets instead of dividing. Any non-power-of-two size (e.g. 1000) fails the check (segmentSize & (segmentSize-1)) != 0 and throws IllegalArgumentException('Segment size must be a power of 2!').","triggerScenarios":"Constructing a RandomAccessOutputView with a segmentSize that is not a power of two — e.g. 1000, 65536+1, or a size read from a misconfigured option; the three-arg constructor performs the same validation even if segmentSizeBits was supplied.","commonSituations":"Custom state/sort code copying a page size from configuration (queryable state, managed memory page sizes set to non-default values); unit tests constructing views with arbitrary round-number sizes; porting code that assumed byte-divisible segment math.","solutions":["Use a power-of-two segment size — Flink's default page size is 32 KB (32768); sizes like 4096/8192/32768/65536 all work.","If the size comes from configuration, round it to the next power of two (or reject it early) before constructing the view.","Ensure all MemorySegments in the array actually have that exact size; the view assumes uniform power-of-two segments."],"exampleFix":"// before\nnew RandomAccessOutputView(segments, 1000); // IllegalArgumentException\n\n// after\nnew RandomAccessOutputView(segments, 1024);","handlingStrategy":"validation","validationCode":"if ((segmentSize & (segmentSize - 1)) != 0) {\n    throw new IllegalArgumentException(\"segmentSize must be a power of 2, got \" + segmentSize);\n}\nnew RandomAccessOutputView(segments, segmentSize);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use Flink's standard page sizes: 4096, 8192, 32768 (default), 65536.","Validate sizes coming from configuration before constructing the view, not at runtime in the constructor path."],"tags":["memory","validation","flink-core","configuration"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}