{"record":{"id":"ba793084ead981bb","repo":"apache/kafka","slug":"invalid-negative-timestamp","errorCode":null,"errorMessage":"Invalid negative timestamp","messagePattern":"Invalid negative timestamp","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/OffsetAndTimestamp.java","lineNumber":58,"sourceCode":"     */\n    public OffsetAndTimestamp(long offset, long timestamp) {\n        this(offset, timestamp, Optional.empty());\n    }\n\n    /**\n     * Constructs a new OffsetAndTimestamp with the given offset, timestamp, and leader epoch.\n     *\n     * @param offset The offset\n     * @param timestamp The timestamp\n     * @param leaderEpoch The leader epoch\n     * @throws IllegalArgumentException If the offset or timestamp is negative\n     */\n    public OffsetAndTimestamp(long offset, long timestamp, Optional<Integer> leaderEpoch) {\n        if (offset < 0)\n            throw new IllegalArgumentException(\"Invalid negative offset\");\n\n        if (timestamp < 0)\n            throw new IllegalArgumentException(\"Invalid negative timestamp\");\n\n        this.offset = offset;\n        this.timestamp = timestamp;\n        this.leaderEpoch = leaderEpoch;\n    }\n\n    /**\n     * Returns the timestamp.\n     *\n     * @return The timestamp\n     */\n    public long timestamp() {\n        return timestamp;\n    }\n\n    /**\n     * Returns the offset.\n     *","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/OffsetAndTimestamp.java#L40-L76","documentation":"OffsetAndTimestamp constructor (same site as error 106) also enforces timestamp >= 0. Kafka record timestamps are non-negative epoch millis, so a negative timestamp would be meaningless as a seek target. The check throws IllegalArgumentException before assignment.","triggerScenarios":"new OffsetAndTimestamp(offset, -1L, leaderEpoch); or faking a result where the matched record's timestamp is unknown.","commonSituations":"Test doubles mocking offsetsForTimes with placeholder timestamps; misinterpreting a 'no match' as a sentinel timestamp; clock skew producing negative epoch values.","solutions":["Return null for unmatched partitions rather than constructing OffsetAndTimestamp with a sentinel timestamp.","Verify timestamp >= 0 (and ideally within a sane epoch range) before constructing.","When faking offsetsForTimes results in tests, use real epoch millis (e.g., System.currentTimeMillis())."],"exampleFix":"// before\nnew OffsetAndTimestamp(5L, -1L, Optional.empty()); // -> IllegalArgumentException\n\n// after\nnew OffsetAndTimestamp(5L, System.currentTimeMillis(), Optional.empty());","handlingStrategy":"validation","validationCode":"if (timestamp < 0) return null;\nreturn new OffsetAndTimestamp(offset, timestamp, leaderEpoch);","typeGuard":"static boolean isValidTimestamp(long ts) { return ts >= 0; }","tryCatchPattern":"null","preventionTips":["Use real epoch millis when faking offsetsForTimes results.","Return null for unmatched partitions instead of sentinel timestamps."],"tags":["consumer","offsets-for-times","validation","timestamp","kafka-clients"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}