MyCATApache/Mycat-Server · error · IllegalArgumentException

Initial capacity must be greater than 0

Error message

Initial capacity must be greater than 0

What it means

BytesToBytesMap's constructor validates its configuration before allocating pages. If initialCapacity is <= 0 it throws IllegalArgumentException 'Initial capacity must be greater than 0', because the hash map cannot be sized with a non-positive capacity.

Solutions

  1. Pass a positive initialCapacity (>= 1) when constructing BytesToBytesMap
  2. Clamp computed capacity: int cap = Math.max(1, requestedCapacity)
  3. Fix the configuration source so the capacity property has a sane positive default
  4. Validate/parse user input to reject 0/negative and overflow-producing values before construction

Example fix

// before
map = new BytesToBytesMap(mgr, serializerManager, 0, pageSize, loadFactor, false);
// after
int initialCapacity = Math.max(1, configuredCapacity);
map = new BytesToBytesMap(mgr, serializerManager, initialCapacity, pageSize, loadFactor, false);
Defensive patterns

Strategy: validation

Validate before calling

if (initialCapacity <= 0 || initialCapacity > MAX_CAPACITY) {
    throw new IllegalArgumentException("initialCapacity must be in (0, " + MAX_CAPACITY + "]");
}
int cap = Math.max(1, initialCapacity);

Try / catch

try {
    map = new BytesToBytesMap(mgr, serMgr, cap, pageSize, loadFactor, metrics);
} catch (IllegalArgumentException e) {
    map = new BytesToBytesMap(mgr, serMgr, Math.max(1, cap), pageSize, loadFactor, metrics);
}

Prevention

When it happens

Trigger: Constructing BytesToBytesMap with initialCapacity == 0 or a negative value (e.g. from a computed/configured size that evaluated to 0, integer overflow, or an unconfigured property defaulting to 0).

Common situations: Config file missing the initial-capacity entry so a default of 0 is used; computing capacity from a row count of 0; int overflow from multiplying user-supplied numbers.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/main/java/io/mycat/memory/unsafe/map/BytesToBytesMap.java:193

  public BytesToBytesMap(
      DataNodeMemoryManager dataNodeMemoryManager,
      DataNodeDiskManager blockManager,
      SerializerManager serializerManager,
      int initialCapacity,
      double loadFactor,
      long pageSizeBytes,
      boolean enablePerfMetrics) {
    super(dataNodeMemoryManager, pageSizeBytes);
    this.dataNodeMemoryManager = dataNodeMemoryManager;
    this.blockManager = blockManager;
    this.serializerManager = serializerManager;
    this.loadFactor = loadFactor;
    this.loc = new Location();
    this.pageSizeBytes = pageSizeBytes;
    this.enablePerfMetrics = enablePerfMetrics;
    if (initialCapacity <= 0) {
      throw new IllegalArgumentException("Initial capacity must be greater than 0");
    }
    if (initialCapacity > MAX_CAPACITY) {
      throw new IllegalArgumentException(
        "Initial capacity " + initialCapacity + " exceeds maximum capacity of " + MAX_CAPACITY);
    }
    if (pageSizeBytes > DataNodeMemoryManager.MAXIMUM_PAGE_SIZE_BYTES) {
      throw new IllegalArgumentException("Page size " + pageSizeBytes + " cannot exceed " +
        DataNodeMemoryManager.MAXIMUM_PAGE_SIZE_BYTES);
    }
    allocate(initialCapacity);
  }

  public BytesToBytesMap(
      DataNodeMemoryManager dataNodeMemoryManager,
      int initialCapacity,
      long pageSizeBytes) {
    this(dataNodeMemoryManager, initialCapacity, pageSizeBytes, false);
  }

View on GitHub (pinned to 65f8d8beb7)