{"id":"c13c665265eb6cf1","repo":"apache/kafka","slug":"compression-levels-are-not-defined-for-this-compre","errorCode":null,"errorMessage":"Compression levels are not defined for this compression type: {}","messagePattern":"Compression levels are not defined for this compression type: (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"clients/src/main/java/org/apache/kafka/common/record/internal/CompressionType.java","lineNumber":177,"sourceCode":"    }\n\n    public static CompressionType forName(String name) {\n        if (NONE.name.equals(name))\n            return NONE;\n        else if (GZIP.name.equals(name))\n            return GZIP;\n        else if (SNAPPY.name.equals(name))\n            return SNAPPY;\n        else if (LZ4.name.equals(name))\n            return LZ4;\n        else if (ZSTD.name.equals(name))\n            return ZSTD;\n        else\n            throw new IllegalArgumentException(\"Unknown compression name: \" + name);\n    }\n\n    public int defaultLevel() {\n        throw new UnsupportedOperationException(\"Compression levels are not defined for this compression type: \" + name);\n    }\n\n    public int maxLevel() {\n        throw new UnsupportedOperationException(\"Compression levels are not defined for this compression type: \" + name);\n    }\n\n    public int minLevel() {\n        throw new UnsupportedOperationException(\"Compression levels are not defined for this compression type: \" + name);\n    }\n\n    public ConfigDef.Validator levelValidator() {\n        throw new UnsupportedOperationException(\"Compression levels are not defined for this compression type: \" + name);\n    }\n\n    @Override\n    public String toString() {\n        return name;\n    }","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/record/internal/CompressionType.java#L159-L195","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If compression.type is none or snappy, remove the compression.level config — it has no meaning for those codecs.","Switch compression.type to gzip, lz4, or zstd to make compression.level effective.","In code that queries level ranges generically, guard with a try/catch for UnsupportedOperationException or check the enum constant before calling."],"exampleFix":"// before\nprops.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, \"snappy\");\nprops.put(\"compression.level\", 5);\n\n// after\nprops.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, \"snappy\");\n// compression.level removed: snappy exposes no level knob","handlingStrategy":"type-guard","validationCode":"// NONE and SNAPPY do not define compression levels; only GZIP, LZ4, ZSTD do.\nCompressionType t = CompressionType.forName(name);\nif (t == CompressionType.GZIP || t == CompressionType.LZ4 || t == CompressionType.ZSTD) {\n    int level = t.defaultLevel();\n} else {\n    // levels not applicable; omit compression.level config\n}","typeGuard":"// Guard: only level-bearing codecs expose defaultLevel/minLevel/maxLevel\nstatic boolean supportsLevel(CompressionType t) {\n    return t == CompressionType.GZIP\n        || t == CompressionType.LZ4\n        || t == CompressionType.ZSTD;\n}","tryCatchPattern":"try {\n    int level = type.defaultLevel();\n} catch (UnsupportedOperationException e) {\n    // NONE / SNAPPY have no level concept; leave compression.level unset\n}","preventionTips":["Only GZIP, LZ4, and ZSTD support a configurable level; NONE and SNAPPY do not.","Do not set a compression.level config when compression.type is none or snappy.","Guard any level-related code path with supportsLevel(type) before calling defaultLevel/minLevel/maxLevel."],"tags":["compression","configuration","unsupported-operation","snappy"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}