MyCATApache/Mycat-Server · error · IllegalArgumentException

Conversion of exceeds Long.MAX_VALUE in . Try a larger…

Error message

Conversion of  exceeds Long.MAX_VALUE in . Try a larger unit (e.g. MiB instead of KiB)

What it means

ByteUnit.convertTo converts a value d from this unit to unit u. When converting downward (this unit has a larger multiplier than u) the result would be d * ratio, which can overflow a long; if Long.MAX_VALUE / ratio < d, this IllegalArgumentException is thrown advising a larger unit. Message shows the offending value and unit name.

Solutions

  1. Express the size in a larger unit first (e.g. convert to MiB/GiB instead of KiB) so the ratio shrinks
  2. Validate/sanity-check the input value against the expected magnitude before converting
  3. Catch IllegalArgumentException around the conversion and fall back to a bigger unit

Example fix

// before
long kib = ByteUnit.GiB.toKiB(hugeByteCount); // overflows
// after
long mib = ByteUnit.GiB.toMiB(hugeByteCount); // larger unit avoids overflow
Defensive patterns

Strategy: try-catch

Validate before calling

static long safeConvert(ByteUnit from, ByteUnit to, long d) {
  long ratio = from.multiplier / to.multiplier;
  if (from.multiplier > to.multiplier && Long.MAX_VALUE / ratio < d)
    throw new IllegalArgumentException(d + " too large to convert to " + to);
  return d * ratio;
}

Try / catch

long converted;
try {
  converted = ByteUnit.GiB.toKiB(bytes);
} catch (IllegalArgumentException e) {
  converted = ByteUnit.GiB.toMiB(bytes); // fall back to larger unit
  log.warn("Overflow converting to KiB; using MiB instead");
}

Prevention

When it happens

Trigger: Calling e.g. byteUnit.toKiB(veryLargeLong) or convertTo on a byte count whose KiB representation exceeds Long.MAX_VALUE, e.g. new ByteUnit("byte").convertTo(Long.MAX_VALUE, ByteUnit.KiB) — reached via toKiB/toMiB/toGiB/toTiB/toPiB helpers.

Common situations: Computing memory sizes from unvalidated configuration numbers, multiplying config values before conversion, Long.MAX_VALUE-sized inputs from misread metrics.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/bb8a15b7cd37a689. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/memory/unsafe/utils/ByteUnit.java:42

  TiB ((long) Math.pow(1024L, 4L)),
  PiB ((long) Math.pow(1024L, 5L)), ;

  ByteUnit(long multiplier) {
    this.multiplier = multiplier;
  }

  // Interpret the provided number (d) with suffix (u) as this unit type.
  // E.g. KiB.interpret(1, MiB) interprets 1MiB as its KiB representation = 1024k
  public long convertFrom(long d, ByteUnit u) {
    return u.convertTo(d, this);
  }

  // Convert the provided number (d) interpreted as this unit type to unit type (u).
  public long convertTo(long d, ByteUnit u) {
    if (multiplier > u.multiplier) {
      long ratio = multiplier / u.multiplier;
      if (Long.MAX_VALUE / ratio < d) {
        throw new IllegalArgumentException("Conversion of " + d + " exceeds Long.MAX_VALUE in "
          + name() + ". Try a larger unit (e.g. MiB instead of KiB)");
      }
      return d * ratio;
    } else {
      // Perform operations in this order to avoid potential overflow
      // when computing d * multiplier
      return d / (u.multiplier / multiplier);
    }
  }

  public double toBytes(long d) {
    if (d < 0) {
      throw new IllegalArgumentException("Negative size value. Size must be positive: " + d);
    }
    return d * multiplier;
  }

  public long toKiB(long d) { return convertTo(d, KiB); }

View on GitHub (pinned to 65f8d8beb7)