{"record":{"id":"1ff55e1918f564a8","repo":"t8y2/dbx","slug":"kafka-partition-partition-does-not-exist-for-to","errorCode":null,"errorMessage":"Kafka partition ${partition} does not exist for topic '${topic}'. Available partitions: ${available}","messagePattern":"Kafka partition (.+?) does not exist for topic '(.+?)'\\. Available partitions: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/kafka/src/main/java/com/dbx/agent/kafka/KafkaAgent.java","lineNumber":2080,"sourceCode":"        String topic,\n        Integer partition,\n        Duration timeout\n    ) {\n        List<PartitionInfo> infos = consumer.partitionsFor(topic, timeout);\n        if (infos == null || infos.isEmpty()) {\n            return Collections.emptyList();\n        }\n        List<Integer> available = infos.stream().map(PartitionInfo::partition).collect(Collectors.toList());\n        return resolvePeekPartitions(topic, partition, available);\n    }\n\n    static List<TopicPartition> resolvePeekPartitions(String topic, Integer partition, List<Integer> availablePartitions) {\n        if (partition != null) {\n            if (availablePartitions == null || !availablePartitions.contains(partition)) {\n                String available = availablePartitions == null || availablePartitions.isEmpty()\n                    ? \"none\"\n                    : availablePartitions.stream().sorted().map(String::valueOf).collect(Collectors.joining(\", \"));\n                throw new IllegalArgumentException(\n                    \"Kafka partition \" + partition + \" does not exist for topic '\" + topic\n                        + \"'. Available partitions: \" + available\n                );\n            }\n            return Collections.singletonList(new TopicPartition(topic, partition));\n        }\n        if (availablePartitions == null || availablePartitions.isEmpty()) {\n            return Collections.emptyList();\n        }\n        return availablePartitions.stream()\n            .sorted()\n            .map(id -> new TopicPartition(topic, id))\n            .collect(Collectors.toList());\n    }\n\n    enum PeekStartPosition {\n        EARLIEST,\n        LATEST,","sourceCodeStart":2062,"sourceCodeEnd":2098,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/kafka/src/main/java/com/dbx/agent/kafka/KafkaAgent.java#L2062-L2098","documentation":"resolvePeekPartitions validates that a user-supplied partition exists on the target topic before constructing a TopicPartition. If the partition is not in the topic's available partition list, the driver throws this IllegalArgumentException listing the valid partitions. This catches typos and stale partition counts before a consumer fetch fails confusingly.","triggerScenarios":"Calling the peek API with partition=N where the topic has fewer than N+1 partitions, the topic does not exist (available partitions resolves to 'none'), or availablePartitions could not be fetched (null).","commonSituations":"Hard-coding partition 1 on a topic that only has one partition (0); partition count was reduced after topic re-creation; pointing at the wrong environment/topic name that has different partitioning; cluster metadata unavailable.","solutions":["Query topic partitions first (describeTopics) and pick a partition from the returned list.","Omit the partition parameter to peek across all partitions instead of pinning one.","Verify the topic name and environment; the available partitions in the error message show what is valid."],"exampleFix":"// before\nagent.peek(conn, \"orders\", /* partition */ 3); // topic has 2 partitions\n\n// after\nList<Integer> parts = agent.listPartitions(conn, \"orders\");\nagent.peek(conn, \"orders\", parts.get(0));","handlingStrategy":"validation","validationCode":"List<Integer> available = agent.listPartitions(conn, topic); // or describeTopics\nif (partition != null && !available.contains(partition)) {\n    throw new IllegalArgumentException(\"partition \" + partition + \" not in \" + available);\n}","typeGuard":"boolean partitionExists(int partition, List<Integer> available) {\n    return available != null && available.contains(partition);\n}","tryCatchPattern":"try {\n    return agent.peek(conn, topic, partition);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"does not exist for topic\")) {\n        return agent.peek(conn, topic, null); // fall back to all partitions\n    } else throw e;\n}","preventionTips":["Always resolve partitions dynamically via describeTopics instead of hard-coding indices","Remember Kafka partitions are 0-based; partition 0 is the first","Verify topic names/environments after re-creating topics with different partition counts"],"tags":["kafka","partition","validation"],"backgroundTag":"kafka-partition-not-found","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}