This is an automated email from the ASF dual-hosted git repository.
ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new 4bf3011d70 [ISSUE 10871] fix(common): validate statistics brief
percentile metadata (#11567)
4bf3011d70 is described below
commit 4bf3011d70a2a8c7ba19ec3d94e1a36ed8e6d91e
Author: 龙骑士k <[email protected]>
AuthorDate: Mon Nov 23 12:30:32 2026 +0000
[ISSUE 10871] fix(common): validate statistics brief percentile metadata
(#11567)
### Which Issue(s) This PR Fixes
Closes issue 10871
### Brief Description
`StatisticsBrief` previously checked only that the metadata array is
non-empty and each row has two elements. Malformed bucket configurations were
accepted at construction time and later caused division by zero in slot index
lookup, out-of-bounds or cross-bucket writes during sampling, corrupted
percentile bounds, or array-size integer overflow.
Bucket validation now enforces these invariants, treating the first bucket
as starting at 0 and reserving one trailing overflow slot in the total slot
count:
- Reject a non-positive first boundary and a non-increasing later range
maximum, including a later Long.MIN_VALUE that underflows the width subtraction
to a large positive value, because non-monotonic ranges break width
calculations.
- Reject a non-positive slot count and a slot count larger than the range
width, because integer division of the width by the slot count would truncate
to zero and throw ArithmeticException during sampling. The non-positive slot
count check is evaluated first so the later remainder test cannot divide by
zero.
- Reject a slot count that does not evenly divide the range width. Integer
division otherwise truncates the slot step, so {{10, 6}} then sample(9) throws
ArrayIndexOutOfBoundsException and {{10, 3}, {20, 10}} then sample(9) writes
into the next bucket. Production metadata in BrokerStatsManager is evenly
divisible and remains accepted.
- Reject a slot count that would make the running total exceed
Integer.MAX_VALUE before accumulation, because the array-size helper casts the
total to int, and subtracting from Integer.MAX_VALUE avoids long overflow when
a slot count approaches Long.MAX_VALUE.
### How Did You Test This Change?
Unit tests in `StatisticsBriefTest` cover the rejected and accepted
contracts.
The rejected cases include null or malformed shapes, a zero or negative
slot count, non-increasing or non-positive range bounds (including a later
Long.MIN_VALUE that underflows the width subtraction), a slot count larger than
the range width, a slot count that does not evenly divide the range width, and
single- or multi-bucket totals that exceed Integer.MAX_VALUE (including
Long.MAX_VALUE). The accepted path uses production-like metadata in which each
slot count evenly divides its ra [...]
Signed-off-by: dragonknightk
<[email protected]>
---
.../common/statistics/StatisticsBrief.java | 13 +++
.../common/statistics/StatisticsBriefTest.java | 108 +++++++++++++++++++++
2 files changed, 121 insertions(+)
diff --git
a/common/src/main/java/org/apache/rocketmq/common/statistics/StatisticsBrief.java
b/common/src/main/java/org/apache/rocketmq/common/statistics/StatisticsBrief.java
index 970bff98a0..96ed4c8428 100644
---
a/common/src/main/java/org/apache/rocketmq/common/statistics/StatisticsBrief.java
+++
b/common/src/main/java/org/apache/rocketmq/common/statistics/StatisticsBrief.java
@@ -68,10 +68,23 @@ public class StatisticsBrief {
return false;
}
+ long previousRangeMax = 0;
+ long totalSlotNum = 1;
for (long[] line : meta) {
if (ArrayUtils.isEmpty(line) || line.length != 2) {
return false;
}
+
+ long rangeMax = line[META_RANGE_INDEX];
+ long slotNum = line[META_SLOT_NUM_INDEX];
+ if (rangeMax <= previousRangeMax || slotNum <= 0
+ || slotNum > rangeMax - previousRangeMax
+ || (rangeMax - previousRangeMax) % slotNum != 0
+ || slotNum > Integer.MAX_VALUE - totalSlotNum) {
+ return false;
+ }
+ previousRangeMax = rangeMax;
+ totalSlotNum += slotNum;
}
return true;
diff --git
a/common/src/test/java/org/apache/rocketmq/common/statistics/StatisticsBriefTest.java
b/common/src/test/java/org/apache/rocketmq/common/statistics/StatisticsBriefTest.java
new file mode 100644
index 0000000000..e566d8002c
--- /dev/null
+++
b/common/src/test/java/org/apache/rocketmq/common/statistics/StatisticsBriefTest.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.common.statistics;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+public class StatisticsBriefTest {
+
+ @Test
+ public void testConstructorRejectsEmptyOrMalformedShape() {
+ // Existing outer-shape contract: null, empty, or non-pair rows must
throw IllegalArgumentException.
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(null));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[0][]));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {null}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 1, 1}}));
+ }
+
+ @Test
+ public void testConstructorRejectsNonPositiveSlotCount() {
+ // Issue 10871: slotNum <= 0 causes division by zero in getSlotIndex
or invalid array sizing.
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 0}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, -1}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 2}, {20, 0}}));
+ }
+
+ @Test
+ public void testConstructorRejectsNonIncreasingRanges() {
+ // Issue 10871: bucket 0 implicitly starts at 0 and each subsequent
rangeMax must strictly increase.
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{0, 1}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{-5, 1}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 2}, {10, 2}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 2}, {5, 2}}));
+ // Long.MIN_VALUE underflows rangeMax - previousRangeMax to a large
positive width; the monotonic check must catch it first.
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 2}, {Long.MIN_VALUE, 1}}));
+ }
+
+ @Test
+ public void testConstructorRejectsSlotsExceedingRangeWidth() {
+ // Issue 10871: slotNum > (rangeMax - previousRangeMax) truncates slot
width to 0 and divides by zero.
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 11}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 2}, {12, 3}}));
+ }
+
+ @Test
+ public void testConstructorRejectsNonDivisibleRangeWidth() {
+ // Issue 10871: a width that does not divide evenly truncates the slot
step and can index past the current bucket.
+ // {{10, 6}} then sample(9) throws ArrayIndexOutOfBoundsException;
{{10, 3}, {20, 10}} then sample(9) writes into the next bucket.
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 6}}));
+ assertThrows(IllegalArgumentException.class, () -> new
StatisticsBrief(new long[][] {{10, 3}, {20, 10}}));
+ }
+
+ @Test
+ public void testConstructorRejectsSlotCountOverflow() {
+ // Issue 10871: slotNum(meta) starts at 1 and casts total slots to
int; exceeding Integer.MAX_VALUE overflows.
+ assertThrows(IllegalArgumentException.class,
+ () -> new StatisticsBrief(new long[][] {{Integer.MAX_VALUE,
Integer.MAX_VALUE}}));
+ assertThrows(IllegalArgumentException.class,
+ () -> new StatisticsBrief(new long[][] {{Integer.MAX_VALUE,
Integer.MAX_VALUE - 1}, {Integer.MAX_VALUE + 1L, 1}}));
+ assertThrows(IllegalArgumentException.class,
+ () -> new StatisticsBrief(new long[][] {{Long.MAX_VALUE,
Long.MAX_VALUE}}));
+ }
+
+ @Test
+ public void testValidBoundaryAndSamplingBehavior() {
+ // Aligns with BrokerStatsManager metadata where first bucket allows
slotNum == rangeMax (width 1 per slot).
+ StatisticsBrief brief = new StatisticsBrief(new long[][] {{10, 10},
{100, 10}});
+
+ brief.sample(3);
+ brief.sample(10);
+ brief.sample(55);
+ brief.sample(120);
+
+ assertEquals(4, brief.getCnt());
+ assertEquals(3, brief.getMin());
+ assertEquals(120, brief.getMax());
+ assertEquals(188, brief.getTotal());
+ assertEquals(47.0, brief.getAvg(), 0.0);
+ assertEquals(120, brief.tp999());
+ // 4 samples, excludes = (long)(4 - 4 * 0.5f) = 2. Walking counts from
the overflow slot (index 20)
+ // downward first exceeds 2 at index 10, whose upper bound is 10 +
(100 - 10) / 10 * 1 = 19.
+ assertEquals(19, brief.getTPValue(0.5f));
+
+ brief.reset();
+ assertEquals(0, brief.getCnt());
+ assertEquals(0, brief.getMin());
+ assertEquals(0, brief.getMax());
+ assertEquals(0, brief.getTotal());
+ assertEquals(0.0, brief.getAvg(), 0.0);
+ }
+}