apache/hadoop · error · HadoopIllegalArgumentException

n = {n} <= 0

Error message

n = {n} <= 0

What it means

ByteArrayManager pools byte arrays in power-of-two buckets, and leastPowerOfTwo(int) is its public rounding helper (used internally by newByteArray for lengths above 32). 'Least power of two >= n' is undefined for n <= 0, so such input is rejected immediately with HadoopIllegalArgumentException. Internal callers pre-validate (arrayLength >= 0, and 0 short-circuits to an empty array), so this throw comes from direct API use or from upstream code that computed a non-positive size.

Source

Thrown at hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/ByteArrayManager.java:65

  private static void logDebugMessage() {
    final StringBuilder b = DEBUG_MESSAGE.get();
    LOG.debug(b.toString());
    b.setLength(0);
  }

  static final int MIN_ARRAY_LENGTH = 32;
  static final byte[] EMPTY_BYTE_ARRAY = {};

  /**
   * @return the least power of two greater than or equal to n, i.e. return
   *         the least integer x with x &gt;= n and x a power of two.
   *
   * @throws HadoopIllegalArgumentException
   *           if n &lt;= 0.
   */
  public static int leastPowerOfTwo(final int n) {
    if (n <= 0) {
      throw new HadoopIllegalArgumentException("n = " + n + " <= 0");
    }

    final int highestOne = Integer.highestOneBit(n);
    if (highestOne == n) {
      return n; // n is a power of two.
    }
    final int roundUp = highestOne << 1;
    if (roundUp < 0) {
      final long overflow = ((long) highestOne) << 1;
      throw new ArithmeticException(
          "Overflow: for n = " + n + ", the least power of two (the least"
          + " integer x with x >= n and x a power of two) = "
          + overflow + " > Integer.MAX_VALUE = " + Integer.MAX_VALUE);
    }
    return roundUp;
  }

  /**

View on GitHub (pinned to 2add963021)

Solutions

  1. Trace the argument: log or inspect n at the call site - the real bug is upstream in how the size was computed
  2. Guard the call: only round sizes already validated as positive
  3. If zero is legitimate in your domain, branch on it before rounding (return a default bucket or your own domain error)

Example fix

// before
int bucket = ByteArrayManager.leastPowerOfTwo(size); // size == 0 -> throws

// after
int bucket = (size <= 0)
    ? 32  // sensible default bucket, or throw your own domain error
    : ByteArrayManager.leastPowerOfTwo(size);
Defensive patterns

Strategy: validation

Validate before calling

if (n <= 0) {
  throw new IllegalArgumentException("size must be positive, got " + n);
}
int rounded = ByteArrayManager.leastPowerOfTwo(n);

Try / catch

try {
  return ByteArrayManager.leastPowerOfTwo(n);
} catch (org.apache.hadoop.HadoopIllegalArgumentException e) {
  // surface a domain-meaningful error naming the offending size
  throw new IllegalArgumentException("invalid buffer size: " + n, e);
}

Prevention

When it happens

Trigger: Calling ByteArrayManager.leastPowerOfTwo(n) directly with n = 0 or negative - typically a size derived from configuration that defaulted to 0, or arithmetic that underflowed before the call.

Common situations: Buffer-size plumbing where an unset property yields 0; subtraction or overflow producing a negative length; unit tests probing the helper's edge cases.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/4fa3ac6fac2029cb. Report an issue: GitHub.