apache/hadoop · error · RuntimeException

length can't be negative

Error message

length can't be negative

What it means

SortedRanges.Range represents an inclusive start index plus a length (length 0 allowed) and is used by the skip-bad-records machinery to skip record index ranges. The constructor rejects any negative length with this RuntimeException, because a negative span makes iteration semantics undefined. In practice the bad value almost always comes from range arithmetic on record indices rather than from user configuration directly.

Source

Thrown at hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred/SortedRanges.java:230

    while(it.hasNext()) {
      Range range = it.next();
      sb.append(range.toString()+"\n");
    }
    return sb.toString();
  }
  
  /**
   * Index Range. Comprises of start index and length.
   * A Range can be of 0 length also. The Range stores indices 
   * of type long.
   */
  public static class Range implements Comparable<Range>, Writable{
    private long startIndex;
    private long length;
        
    Range(long startIndex, long length) {
      if(length<0) {
        throw new RuntimeException("length can't be negative");
      }
      this.startIndex = startIndex;
      this.length = length;
    }
    
    public Range() {
      this(0,0);
    }
    
    /**
     * Get the start index. Start index in inclusive.
     * @return startIndex. 
     */
    long getStartIndex() {
      return startIndex;
    }
    
    /**

View on GitHub (pinned to 2add963021)

Solutions

  1. If this happens with skip-bad-records enabled, disable skip mode for the job (remove mapreduce.map.skip.maxrecords) and clean the job's attempt directories so stale skip data is not reused.
  2. In custom code, validate/clamp the computed length to >= 0 before creating a Range.
  3. Check for long overflow in index arithmetic (start + length) that can wrap negative.

Example fix

// before
ranges.add(new SortedRanges.Range(start, end - cursor)); // can be negative

// after
long len = Math.max(0L, end - cursor);
ranges.add(new SortedRanges.Range(start, len));
Defensive patterns

Strategy: validation

Validate before calling

if (length < 0) {
  throw new IllegalArgumentException("Range length must be >= 0, got " + length);
}
ranges.add(new SortedRanges.Range(startIndex, length));

Prevention

When it happens

Trigger: Constructing new Range(start, len) with len < 0 — e.g. subtracting a larger index from a smaller one when computing a skip window, or feeding corrupted skip metadata (from a previous failed attempt) back into SortedRanges.add().

Common situations: Skip-mode jobs (mapreduce.map.skip.maxrecords > 0) after an earlier attempt wrote malformed skip information; custom RecordReaders that manipulate SortedRanges; integer overflow when start + length wraps.

Related errors


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