apache/kafka · error · IllegalArgumentException

Values less than 0.0 not accepted.

Error message

Values less than 0.0 not accepted.

What it means

Thrown by `LinearBinScheme.toBin(double x)` when a negative value is recorded. Linear binning assumes a non-negative domain starting at 0 and uses `Math.sqrt(1.0 + 8.0 * x / scale)` to invert the bin-width formula; negative x breaks the inversion. Kafka rejects negative inputs rather than silently clamping or returning a malformed bin index.

Source

Thrown at clients/src/main/java/org/apache/kafka/common/metrics/stats/Histogram.java:207

        }

        public int bins() {
            return this.bins;
        }

        public double fromBin(int b) {
            if (b > this.bins - 1) {
                return Float.POSITIVE_INFINITY;
            } else if (b < 0.0000d) {
                return Float.NEGATIVE_INFINITY;
            } else {
                return this.scale * (b * (b + 1.0)) / 2.0;
            }
        }

        public int toBin(double x) {
            if (x < 0.0d) {
                throw new IllegalArgumentException("Values less than 0.0 not accepted.");
            } else if (x > this.max) {
                return this.bins - 1;
            } else {
                return (int) (-0.5 + 0.5 * Math.sqrt(1.0 + 8.0 * x / this.scale));
            }
        }
    }
}

View on GitHub (pinned to c31c9215e1)

Solutions

  1. Clamp or filter negative values before recording them into the Sensor (record `Math.max(0, value)`).
  2. If the signal is genuinely signed, switch to `BucketSizing.CONSTANT` with a `min` below the lowest expected value.
  3. Fix upstream clock skew or counter resets so deltas never go negative.

Example fix

// before
sensor.record(deltaMs); // deltaMs can be negative on clock skew

// after
sensor.record(Math.max(0.0, deltaMs));
Defensive patterns

Strategy: validation

Validate before calling

// LinearBinScheme.toBin throws on x < 0.0, reached via Histogram.record / Sensor.record
if (value < 0.0d) {
    throw new IllegalArgumentException(
        "LinearBinScheme does not accept negative values: " + value);
}
histogram.record(value);

Try / catch

try {
    histogram.record(value);
} catch (IllegalArgumentException e) {
    // "Values less than 0.0 not accepted."
    // Only LinearBinScheme is affected; ConstantBinScheme tolerates negatives.
    histogram.record(Math.max(0.0d, value));
}

Prevention

When it happens

Trigger: Recording a negative value into a Histogram backed by a LinearBinScheme: `histogram.record(-1.0)`. Indirectly via a `Percentiles` with `BucketSizing.LINEAR` whose Sensor records a negative sample, or via `Frequencies` constructed with a ConstantBinScheme (no—Constant allows negatives); specifically only LinearBinScheme.toBin rejects negatives.

Common situations: A sensor records latency deltas or counter deltas that go negative due to clock skew, reset, or counter wraparound. Feeding raw gauge values (e.g. temperature in Celsius, signed integers) into a Percentiles metric configured with LINEAR bucketing. Reusing a percentile metric built for sizes/counts (always >=0) for a signed signal.

Related errors


AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03). Data as JSON: /data/errors/4b3d9abbc08d58e5.json. Report an issue: GitHub.