{"record":{"id":"2e4a9bfb9113d497","repo":"MyCATApache/Mycat-Server","slug":"buffersize-must-be-a-power-of-2-2e4a9b","errorCode":null,"errorMessage":"bufferSize must be a power of 2","messagePattern":"bufferSize must be a power of 2","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/ringbuffer/producer/AbstractSequencer.java","lineNumber":35,"sourceCode":"public abstract class AbstractSequencer implements Sequencer {\n\n    private static final AtomicReferenceFieldUpdater<AbstractSequencer, Sequence[]> SEQUENCE_UPDATER =\n            AtomicReferenceFieldUpdater.newUpdater(AbstractSequencer.class, Sequence[].class, \"gatingSequences\");\n\n    protected final int bufferSize;\n    protected final WaitStrategy waitStrategy;\n    protected final Sequence cursor = new Sequence(Sequencer.INITIAL_CURSOR_VALUE);\n    protected volatile Sequence[] gatingSequences = new Sequence[0];\n\n    public AbstractSequencer(int bufferSize, WaitStrategy waitStrategy)\n    {\n        if (bufferSize < 1)\n        {\n            throw new IllegalArgumentException(\"bufferSize must not be less than 1\");\n        }\n        if (Integer.bitCount(bufferSize) != 1)\n        {\n            throw new IllegalArgumentException(\"bufferSize must be a power of 2\");\n        }\n\n        this.bufferSize = bufferSize;\n        this.waitStrategy = waitStrategy;\n    }\n\n    @Override\n    public final long getCursor()\n    {\n        return cursor.get();\n    }\n\n\n    @Override\n    public final int getBufferSize()\n    {\n        return bufferSize;\n    }","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/ringbuffer/producer/AbstractSequencer.java#L17-L53","documentation":"AbstractSequencer requires the ring buffer size to be a power of 2 (Integer.bitCount(bufferSize) == 1). Powers of two let the sequencer replace modulo with a cheap mask (index = sequence & (bufferSize-1)). Any non-power-of-two size fails fast with IllegalArgumentException at construction.","triggerScenarios":"Constructing any AbstractSequencer subclass or RingBuffer via its factories with bufferSize such as 3, 1000, 1023, 1500 — anything whose binary representation has more than one set bit.","commonSituations":"Developers picking 'round' decimal sizes like 1000 or 10000 instead of 1024/16384; computing capacity from a quota that isn't rounded down to a power of 2.","solutions":["Use the nearest power of two: 1024, 2048, 4096, etc.","If capacity must be derived, round down with Integer.highestOneBit(requested) (min 1)","Log/normalize the configured size before constructing the sequencer"],"exampleFix":"// before\nint size = 1000;\nRingBuffer<Event> rb = RingBuffer.createSingleProducer(factory, size);\n// after\nint size = Math.max(1, Integer.highestOneBit(1000)); // 512\nRingBuffer<Event> rb = RingBuffer.createSingleProducer(factory, size);","handlingStrategy":"validation","validationCode":"if (Integer.bitCount(bufferSize) != 1) throw new IllegalArgumentException(\"bufferSize must be a power of 2, got \" + bufferSize);","typeGuard":"boolean isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }","tryCatchPattern":null,"preventionTips":["Pick sizes like 1024/4096/16384 by habit","Round requested sizes with Integer.highestOneBit","Add a unit test asserting buffer-size validation on any custom sequencer"],"tags":["java","ringbuffer","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}