apache/kafka · warning · UnsupportedOperationException

Compression levels are not defined for this compression type

Error message

Compression levels are not defined for this compression type: {}

What it means

Thrown by the default CompressionType.defaultLevel(), maxLevel(), minLevel(), and levelValidator() implementations. Only GZIP, LZ4, and ZSTD override these to expose tunable levels; NONE and SNAPPY inherit the throwing defaults because their compression is fixed (none) or exposes no level knob (snappy). Calling any of these on the NONE or SNAPPY enum constants raises UnsupportedOperationException.

Source

Thrown at clients/src/main/java/org/apache/kafka/common/record/internal/CompressionType.java:177

    }

    public static CompressionType forName(String name) {
        if (NONE.name.equals(name))
            return NONE;
        else if (GZIP.name.equals(name))
            return GZIP;
        else if (SNAPPY.name.equals(name))
            return SNAPPY;
        else if (LZ4.name.equals(name))
            return LZ4;
        else if (ZSTD.name.equals(name))
            return ZSTD;
        else
            throw new IllegalArgumentException("Unknown compression name: " + name);
    }

    public int defaultLevel() {
        throw new UnsupportedOperationException("Compression levels are not defined for this compression type: " + name);
    }

    public int maxLevel() {
        throw new UnsupportedOperationException("Compression levels are not defined for this compression type: " + name);
    }

    public int minLevel() {
        throw new UnsupportedOperationException("Compression levels are not defined for this compression type: " + name);
    }

    public ConfigDef.Validator levelValidator() {
        throw new UnsupportedOperationException("Compression levels are not defined for this compression type: " + name);
    }

    @Override
    public String toString() {
        return name;
    }

View on GitHub (pinned to c31c9215e1)

Solutions

  1. If compression.type is none or snappy, remove the compression.level config — it has no meaning for those codecs.
  2. Switch compression.type to gzip, lz4, or zstd to make compression.level effective.
  3. In code that queries level ranges generically, guard with a try/catch for UnsupportedOperationException or check the enum constant before calling.

Example fix

// before
props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
props.put("compression.level", 5);

// after
props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
// compression.level removed: snappy exposes no level knob
Defensive patterns

Strategy: type-guard

Validate before calling

// NONE and SNAPPY do not define compression levels; only GZIP, LZ4, ZSTD do.
CompressionType t = CompressionType.forName(name);
if (t == CompressionType.GZIP || t == CompressionType.LZ4 || t == CompressionType.ZSTD) {
    int level = t.defaultLevel();
} else {
    // levels not applicable; omit compression.level config
}

Type guard

// Guard: only level-bearing codecs expose defaultLevel/minLevel/maxLevel
static boolean supportsLevel(CompressionType t) {
    return t == CompressionType.GZIP
        || t == CompressionType.LZ4
        || t == CompressionType.ZSTD;
}

Try / catch

try {
    int level = type.defaultLevel();
} catch (UnsupportedOperationException e) {
    // NONE / SNAPPY have no level concept; leave compression.level unset
}

Prevention

When it happens

Trigger: Invoking compressionType.defaultLevel()/maxLevel()/minLevel()/levelValidator() on CompressionType.NONE or CompressionType.SNAPPY. Commonly reached by generic config-validation code (e.g., parsing a compression.level property) that resolves the enum from compression.type and blindly asks for its level range.

Common situations: A producer or broker config sets compression.type=none or snappy together with a compression.level setting; the level validator then queries the enum for valid bounds and hits the unsupported operation. Also hit by tooling/dashboards that iterate all enum constants.

Related errors


AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03). Data as JSON: /data/errors/c13c665265eb6cf1.json. Report an issue: GitHub.