{"record":{"id":"c35d19eb04d662ee","repo":"MyCATApache/Mycat-Server","slug":"buffersize-must-not-be-less-than-1","errorCode":null,"errorMessage":"bufferSize must not be less than 1","messagePattern":"bufferSize must not be less than 1","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/ringbuffer/RingBuffer.java","lineNumber":47,"sourceCode":"        } else {\n            throw new IllegalStateException(\"Unknown pointer size\");\n        }\n        //需要填充128字节，缓存行长度一般是128字节\n        BUFFER_PAD = 128 / scale;\n        REF_ARRAY_BASE = Platform.arrayBaseOffset(Object[].class) + (BUFFER_PAD << REF_ELEMENT_SHIFT);\n    }\n\n    private final long indexMask;\n    private final Object[] entries;\n    protected final int bufferSize;\n    protected final Sequencer sequencer;\n\n    public RingBuffer(EventFactory<E> eventFactory, Sequencer sequencer) {\n        this.sequencer = sequencer;\n        this.bufferSize = sequencer.getBufferSize();\n        //保证buffer大小不小于1\n        if (bufferSize < 1) {\n            throw new IllegalArgumentException(\"bufferSize must not be less than 1\");\n        }\n        //保证buffer大小为2的n次方\n        if (Integer.bitCount(bufferSize) != 1) {\n            throw new IllegalArgumentException(\"bufferSize must be a power of 2\");\n        }\n        //m % 2^n  <=>  m & (2^n - 1)\n        this.indexMask = bufferSize - 1;\n        /**\n         * 结构：缓存行填充，避免频繁访问的任一entry与另一被修改的无关变量写入同一缓存行\n         * --------------\n         * *   数组头   * BASE\n         * *   Padding  * 128字节\n         * * reference1 * SCALE\n         * * reference2 * SCALE\n         * * reference3 * SCALE\n         * ..........\n         * *   Padding  * 128字节\n         * --------------","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/ringbuffer/RingBuffer.java#L29-L65","documentation":"RingBuffer's constructor validates the sequencer's buffer size: it must be at least 1 (and, in the next check, a power of 2, so indices wrap with bitmask arithmetic m & (2^n - 1)). A bufferSize < 1 cannot be indexed or wrapped, so the constructor throws IllegalArgumentException.","triggerScenarios":"Constructing `new RingBuffer(eventFactory, sequencer)` where sequencer.getBufferSize() returns 0 or a negative value — typically via `RingBuffer.createMultiProducer(create(eventFactory, bufferSize), ...)`-style helpers with a bad bufferSize argument.","commonSituations":"Reading bufferSize from config that defaulted to 0 (unset property, failed parse); computing bufferSize as `(long) size/threads` with size < threads; passing an uncomputed/zero placeholder.","solutions":["Pass a bufferSize >= 1 (conventionally a power of 2 like 1024) when creating the sequencer/ring buffer.","Fix the config property supplying bufferSize so it parses to a valid positive integer: `Math.max(1, configuredSize)`.","If bufferSize is computed, guard the calculation: fall back to a default (e.g. 1024) when the result is < 1.","Validate early at config-load time that bufferSize >= 1 and Integer.bitCount(bufferSize) == 1."],"exampleFix":"// before\nint bufferSize = Integer.getInteger(\"mycat.ring.buffer.size\", 0);\nRingBuffer<Event> rb = new RingBuffer<>(factory, create(factory, bufferSize));\n// after\nint bufferSize = Math.max(1024,\n    Integer.getInteger(\"mycat.ring.buffer.size\", 1024));\nRingBuffer<Event> rb = new RingBuffer<>(factory, create(factory, bufferSize));","handlingStrategy":"validation","validationCode":"int bufferSize = sequencer.getBufferSize();\nif (bufferSize < 1 || Integer.bitCount(bufferSize) != 1) {\n  bufferSize = 1024; // safe default power of 2\n  sequencer = new MultiProducerSequencer(bufferSize, waitStrategy);\n}\nRingBuffer<E> rb = new RingBuffer<>(eventFactory, sequencer);","typeGuard":null,"tryCatchPattern":"try {\n  rb = new RingBuffer<>(eventFactory, sequencer);\n} catch (IllegalArgumentException e) {\n  sequencer = new MultiProducerSequencer(1024, waitStrategy);\n  rb = new RingBuffer<>(eventFactory, sequencer);\n}","preventionTips":["Validate bufferSize >= 1 (and power of 2) at config load.","Never let an unset property default to 0 for buffer sizes.","Guard computed sizes: fall back to a default like 1024.","Round user-supplied sizes up to the next power of 2."],"tags":["java","ringbuffer","illegal-argument","configuration"],"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"}