apache/flink · error · IllegalArgumentException

multiplier must be >= 0

Error message

multiplier must be >= 0

What it means

MemorySize.multiply(double multiplier) throws IllegalArgumentException when the multiplier is negative, because memory sizes cannot be negative. The check runs before the BigDecimal multiplication, so any negative factor fails immediately even if the magnitude is tiny.

Source

Thrown at flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java:209

    public int compareTo(MemorySize that) {
        return Long.compare(this.bytes, that.bytes);
    }

    // ------------------------------------------------------------------------
    //  Calculations
    // ------------------------------------------------------------------------

    public MemorySize add(MemorySize that) {
        return new MemorySize(Math.addExact(this.bytes, that.bytes));
    }

    public MemorySize subtract(MemorySize that) {
        return new MemorySize(Math.subtractExact(this.bytes, that.bytes));
    }

    public MemorySize multiply(double multiplier) {
        if (multiplier < 0) {
            throw new IllegalArgumentException("multiplier must be >= 0");
        }

        BigDecimal product =
                BigDecimal.valueOf(this.bytes).multiply(BigDecimal.valueOf(multiplier));
        if (product.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
            throw new ArithmeticException("long overflow");
        }
        return new MemorySize(product.longValue());
    }

    public MemorySize divide(long by) {
        if (by < 0) {
            throw new IllegalArgumentException("divisor must be != 0");
        }
        return new MemorySize(bytes / by);
    }

    // ------------------------------------------------------------------------

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Pass a non-negative multiplier; for 'shrink' semantics use a positive fraction < 1
  2. Validate user-supplied fractions/percentages are >= 0 before use
  3. If negative scaling is meaningful in your domain, clamp to 0 explicitly rather than relying on the exception

Example fix

// before
MemorySize scaled = base.multiply(deltaFactor); // deltaFactor may be -0.25

// after
double factor = Math.max(0, deltaFactor);
MemorySize scaled = base.multiply(factor);
Defensive patterns

Strategy: validation

Validate before calling

if (multiplier < 0) {
    throw new IllegalArgumentException("multiplier must be >= 0, got " + multiplier);
}
MemorySize scaled = base.multiply(multiplier);

Try / catch

catch (IllegalArgumentException e) { /* negative factor is a caller bug — fix the producer, do not catch-and-continue */ }

Prevention

When it happens

Trigger: size.multiply(-0.5); passing a fraction computed as (used - limit)/limit that goes negative; feeding a percentage/100 where the percentage was negative.

Common situations: Sizing derived from relative deltas (e.g. scaling a memory segment by a change factor that dips below zero); configuration code that accepts user-supplied fractions without range validation.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/a92ed04669e0336d. Report an issue: GitHub.