You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "Russole (via GitHub)" <gi...@apache.org> on 2026/09/06 04:49:28 UTC

[PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Russole opened a new pull request, #23380:
URL: https://github.com/apache/kafka/pull/23380

   ## Summary
   
   This PR migrates `LeaderEpochFileCacheTest` from the `core` Scala unit test source set to the `storage` Java test source set so that it lives alongside `LeaderEpochFileCache`.
   
   The migrated test preserves the existing coverage while using Java/JUnit style assertions. It also cleans up a few issues inherited from the Scala test:
   
   - Removes a duplicate truncation test case.
   - Localizes an unused `MockTime` field.
   - Makes the empty-cache truncation tests explicitly assert no exception is thrown.
   - Renames a numbered test case to describe the scenario.
   
   Jira: https://issues.apache.org/jira/browse/KAFKA-21036
   
   ## Testing
   
   ```bash
   ./gradlew :storage:test --tests org.apache.kafka.storage.internals.epoch.LeaderEpochFileCacheTest


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub)" <gi...@apache.org>.
Russole commented on PR #23380:
URL: https://github.com/apache/kafka/pull/23380#issuecomment-5621311879

   > Thanks for the update, Could you also add tests for the `epochEntriesInRange` method?
   
   Sure, I've added tests for range boundaries, empty ranges, and cache immutability. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "m1a2st (via GitHub)" <gi...@apache.org>.
m1a2st commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3947451378


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,565 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldNotOverwriteLogEndOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        long logEndOffset = 9;
+
+        cache.assign(2, logEndOffset);
+
+        cache.assign(2, 10);
+
+        assertEquals(logEndOffset, cache.epochEntries().get(0).startOffset());
+        assertEquals(List.of(new EpochEntry(2, 9)), cache.epochEntries());
+    }

Review Comment:
   `shouldNotOverwriteLogEndOffsetForALeaderEpochOnceItHasBeenAssigned` and `shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned` verify the same behavior: assign() should not overwrite the startOffset for an epoch that has already been assigned. The first test name mentions LogEndOffset, but that is misleading, since logEndOffset is only passed as the startOffset argument to assign() and is unrelated to the log end offset concept. Could we merge these into a single test?



##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,565 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldNotOverwriteLogEndOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        long logEndOffset = 9;
+
+        cache.assign(2, logEndOffset);
+
+        cache.assign(2, 10);
+
+        assertEquals(logEndOffset, cache.epochEntries().get(0).startOffset());
+        assertEquals(List.of(new EpochEntry(2, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(6, cache.epochEntries().get(0).startOffset());
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecorded() {
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(0, 0L));
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecordedAndUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> offsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 73);
+
+        assertEquals(
+                Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET),
+                offsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Empty cache.");
+    }
+
+    @Test
+    public void shouldReturnFirstEpochIfRequestedEpochLessThanFirstEpoch() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        Map.Entry<Integer, Long> epochAndOffset = cache.endOffsetFor(4, 0L);
+
+        assertEquals(Map.entry(4, 11L), epochAndOffset);
+    }
+
+    @Test
+    public void shouldTruncateIfMatchingEpochButEarlierStartingOffset() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        cache.assign(7, 12);
+
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(5, 0L));
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(6, 0L));
+    }
+
+    @Test
+    public void shouldGetFirstOffsetOfSubsequentEpochWhenOffsetRequestedForPreviousEpoch() {
+        cache.assign(1, 11);
+        cache.assign(1, 12);
+        cache.assign(2, 13);
+        cache.assign(2, 14);
+        cache.assign(3, 15);
+        cache.assign(3, 16);
+
+        assertEquals(Map.entry(2, 15L), cache.endOffsetFor(2, 17));
+    }
+
+    @Test
+    public void shouldReturnNextAvailableEpochIfThereIsNoExactEpochForTheOneRequested() {
+        cache.assign(0, 10);
+        cache.assign(2, 13);
+        cache.assign(4, 17);
+
+        assertEquals(Map.entry(0, 13L), cache.endOffsetFor(1, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(2, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(3, 0L));
+    }
+
+    @Test
+    public void shouldNotUpdateEpochAndStartOffsetIfItDidNotChange() {
+        cache.assign(2, 6);
+        cache.assign(2, 7);
+
+        assertEquals(1, cache.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldReturnInvalidOffsetIfEpochIsRequestedWhichIsNotCurrentlyTracked() {
+        cache.assign(2, 100);
+
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(3, 100));
+    }
+
+    @Test
+    public void shouldSupportEpochsThatDoNotStartFromZero() {
+        cache.assign(2, 6);
+        long logEndOffset = 7;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(1, cache.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldPersistEpochsBetweenInstances() throws IOException {
+        String checkpointPath = TestUtils.tempFile().getAbsolutePath();
+        LeaderEpochCheckpointFile checkpoint = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+
+        LeaderEpochFileCache cache = new LeaderEpochFileCache(tp, checkpoint, new MockTime().scheduler);
+        cache.assign(2, 6);
+
+        LeaderEpochCheckpointFile checkpoint2 = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+        LeaderEpochFileCache cache2 = new LeaderEpochFileCache(tp, checkpoint2, new MockTime().scheduler);
+
+        assertEquals(1, cache2.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache2.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingEpochs() {
+        cache.assign(1, 5);
+        cache.assign(2, 6);
+        cache.assign(1, 7);

Review Comment:
   We should keep the original comment
   ```
       // When we update an epoch in the past with a different offset, the log has already reached
       // an inconsistent state. Our options are either to raise an error, ignore the new append,
       // or truncate the cached epochs to the point of conflict. We take this latter approach in
       // order to guarantee that epochs and offsets in the cache increase monotonically, which makes
       // the search logic simpler to reason about.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "m1a2st (via GitHub)" <gi...@apache.org>.
m1a2st merged PR #23380:
URL: https://github.com/apache/kafka/pull/23380


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub)" <gi...@apache.org>.
Russole commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3980797803


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,559 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }

Review Comment:
   Good catch. These tests cover the same behavior, so I've removed the duplicate and kept the full `epochEntries()` assertion in `shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned`. Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub)" <gi...@apache.org>.
Russole commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3950537332


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,565 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldNotOverwriteLogEndOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        long logEndOffset = 9;
+
+        cache.assign(2, logEndOffset);
+
+        cache.assign(2, 10);
+
+        assertEquals(logEndOffset, cache.epochEntries().get(0).startOffset());
+        assertEquals(List.of(new EpochEntry(2, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(6, cache.epochEntries().get(0).startOffset());
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecorded() {
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(0, 0L));
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecordedAndUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> offsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 73);
+
+        assertEquals(
+                Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET),
+                offsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Empty cache.");
+    }
+
+    @Test
+    public void shouldReturnFirstEpochIfRequestedEpochLessThanFirstEpoch() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        Map.Entry<Integer, Long> epochAndOffset = cache.endOffsetFor(4, 0L);
+
+        assertEquals(Map.entry(4, 11L), epochAndOffset);
+    }
+
+    @Test
+    public void shouldTruncateIfMatchingEpochButEarlierStartingOffset() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        cache.assign(7, 12);
+
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(5, 0L));
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(6, 0L));
+    }
+
+    @Test
+    public void shouldGetFirstOffsetOfSubsequentEpochWhenOffsetRequestedForPreviousEpoch() {
+        cache.assign(1, 11);
+        cache.assign(1, 12);
+        cache.assign(2, 13);
+        cache.assign(2, 14);
+        cache.assign(3, 15);
+        cache.assign(3, 16);
+
+        assertEquals(Map.entry(2, 15L), cache.endOffsetFor(2, 17));
+    }
+
+    @Test
+    public void shouldReturnNextAvailableEpochIfThereIsNoExactEpochForTheOneRequested() {
+        cache.assign(0, 10);
+        cache.assign(2, 13);
+        cache.assign(4, 17);
+
+        assertEquals(Map.entry(0, 13L), cache.endOffsetFor(1, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(2, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(3, 0L));
+    }
+
+    @Test
+    public void shouldNotUpdateEpochAndStartOffsetIfItDidNotChange() {
+        cache.assign(2, 6);
+        cache.assign(2, 7);
+
+        assertEquals(1, cache.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldReturnInvalidOffsetIfEpochIsRequestedWhichIsNotCurrentlyTracked() {
+        cache.assign(2, 100);
+
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(3, 100));
+    }
+
+    @Test
+    public void shouldSupportEpochsThatDoNotStartFromZero() {
+        cache.assign(2, 6);
+        long logEndOffset = 7;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(1, cache.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldPersistEpochsBetweenInstances() throws IOException {
+        String checkpointPath = TestUtils.tempFile().getAbsolutePath();
+        LeaderEpochCheckpointFile checkpoint = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+
+        LeaderEpochFileCache cache = new LeaderEpochFileCache(tp, checkpoint, new MockTime().scheduler);
+        cache.assign(2, 6);
+
+        LeaderEpochCheckpointFile checkpoint2 = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+        LeaderEpochFileCache cache2 = new LeaderEpochFileCache(tp, checkpoint2, new MockTime().scheduler);
+
+        assertEquals(1, cache2.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache2.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingEpochs() {
+        cache.assign(1, 5);
+        cache.assign(2, 6);
+        cache.assign(1, 7);

Review Comment:
   Agreed. This comment provides useful context for the truncation behavior, so I've restored it. Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub)" <gi...@apache.org>.
Russole commented on PR #23380:
URL: https://github.com/apache/kafka/pull/23380#issuecomment-5637463659

   Thanks @m1a2st for the review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3989514936


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;

Review Comment:
   We could streamline it using the assertion helper 
   ```java
       private final LeaderEpochCheckpointFile checkpoint = assertDoesNotThrow(() -> new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1)));
       private final LeaderEpochFileCache cache = new LeaderEpochFileCache(tp, checkpoint, new MockScheduler(Time.SYSTEM));
   ```



##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);

Review Comment:
   Another method `assign(List<EpochEntry> entries)` is not covered by any test either. @Russole could you add tests for it in KAFKA-21071?



##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);

Review Comment:
   It should be a static variable



##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecorded() {
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(0, 0L));
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecordedAndUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> offsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 73);
+
+        assertEquals(
+                Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET),
+                offsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Empty cache.");
+    }
+
+    @Test
+    public void shouldReturnFirstEpochIfRequestedEpochLessThanFirstEpoch() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        Map.Entry<Integer, Long> epochAndOffset = cache.endOffsetFor(4, 0L);
+
+        assertEquals(Map.entry(4, 11L), epochAndOffset);
+    }
+
+    @Test
+    public void shouldTruncateIfMatchingEpochButEarlierStartingOffset() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        cache.assign(7, 12);
+
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(5, 0L));
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(6, 0L));
+    }
+
+    @Test
+    public void shouldGetFirstOffsetOfSubsequentEpochWhenOffsetRequestedForPreviousEpoch() {
+        cache.assign(1, 11);
+        cache.assign(1, 12);
+        cache.assign(2, 13);
+        cache.assign(2, 14);
+        cache.assign(3, 15);
+        cache.assign(3, 16);
+
+        assertEquals(Map.entry(2, 15L), cache.endOffsetFor(2, 17));
+    }
+
+    @Test
+    public void shouldReturnNextAvailableEpochIfThereIsNoExactEpochForTheOneRequested() {
+        cache.assign(0, 10);
+        cache.assign(2, 13);
+        cache.assign(4, 17);
+
+        assertEquals(Map.entry(0, 13L), cache.endOffsetFor(1, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(2, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(3, 0L));
+    }
+
+    @Test
+    public void shouldReturnInvalidOffsetIfEpochIsRequestedWhichIsNotCurrentlyTracked() {
+        cache.assign(2, 100);
+
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(3, 100));
+    }
+
+    @Test
+    public void shouldSupportEpochsThatDoNotStartFromZero() {
+        cache.assign(2, 6);
+        long logEndOffset = 7;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(1, cache.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldPersistEpochsBetweenInstances() throws IOException {
+        String checkpointPath = TestUtils.tempFile().getAbsolutePath();
+        LeaderEpochCheckpointFile checkpoint = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+
+        LeaderEpochFileCache cache = new LeaderEpochFileCache(tp, checkpoint, new MockTime().scheduler);
+        cache.assign(2, 6);
+
+        LeaderEpochCheckpointFile checkpoint2 = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+        LeaderEpochFileCache cache2 = new LeaderEpochFileCache(tp, checkpoint2, new MockTime().scheduler);
+
+        assertEquals(1, cache2.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache2.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingEpochs() {
+        cache.assign(1, 5);
+        cache.assign(2, 6);
+
+        // When we update an epoch in the past with a different offset, the log has already reached
+        // an inconsistent state. Our options are either to raise an error, ignore the new append,
+        // or truncate the cached epochs to the point of conflict. We take this latter approach in
+        // order to guarantee that epochs and offsets in the cache increase monotonically, which makes
+        // the search logic simpler to reason about.
+        cache.assign(1, 7);
+
+        long logEndOffset = 8;
+
+        assertEquals(Optional.of(1), cache.latestEpoch());
+        assertEquals(Map.entry(1, logEndOffset), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(new EpochEntry(1, 7), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldEnforceOffsetsIncreaseMonotonically() {
+        cache.assign(2, 6);
+        cache.assign(3, 5);
+
+        assertEquals(new EpochEntry(3, 5), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldIncreaseAndTrackEpochsAsLeadersChangeManyTimes() {
+        long logEndOffset = 0L;
+
+        cache.assign(0, 0);
+
+        cache.assign(1, 0);
+
+        assertEquals(Optional.of(1), cache.latestEpoch());
+        assertEquals(Map.entry(1, 0L), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(0, 0L), cache.endOffsetFor(0, logEndOffset));
+
+        logEndOffset = 5L;
+
+        assertEquals(Map.entry(1, 5L), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(0, 0L), cache.endOffsetFor(0, logEndOffset));
+
+        cache.assign(2, 5);
+
+        logEndOffset = 10;
+
+        assertEquals(Map.entry(2, 10L), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(Map.entry(1, 5L), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(0, 0L), cache.endOffsetFor(0, logEndOffset));
+    }
+
+    @Test
+    public void shouldIncreaseAndTrackEpochsAsFollowerReceivesManyMessages() {
+        cache.assign(0, 0);
+        long logEndOffset = 1;
+        cache.assign(0, 1);
+        logEndOffset = 2;
+        cache.assign(0, 2);
+        logEndOffset = 3;
+
+        assertEquals(Optional.of(0), cache.latestEpoch());
+        assertEquals(Map.entry(0, logEndOffset), cache.endOffsetFor(0, logEndOffset));
+
+        cache.assign(1, 3);
+        logEndOffset = 4;
+        cache.assign(1, 4);
+        logEndOffset = 5;
+        cache.assign(1, 5);
+        logEndOffset = 6;
+
+        assertEquals(Optional.of(1), cache.latestEpoch());
+        assertEquals(Map.entry(1, logEndOffset), cache.endOffsetFor(1, logEndOffset));
+
+        cache.assign(2, 6);
+        logEndOffset = 7;
+        cache.assign(2, 7);
+        logEndOffset = 8;
+        cache.assign(2, 8);
+        logEndOffset = 9;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+
+        assertEquals(Map.entry(0, 3L), cache.endOffsetFor(0, logEndOffset));
+        assertEquals(Map.entry(1, 6L), cache.endOffsetFor(1, logEndOffset));
+    }
+
+    @Test
+    public void shouldDropEntriesOnEpochBoundaryWhenRemovingLatestEntries() {
+        cache.assign(2, 6);
+        cache.assign(3, 8);
+        cache.assign(4, 11);
+
+        cache.truncateFromEndAsyncFlush(8);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldPreserveResetOffsetOnClearEarliestIfOneExists() {
+        cache.assign(2, 6);
+        cache.assign(3, 8);
+        cache.assign(4, 11);
+
+        cache.truncateFromStartAsyncFlush(8);

Review Comment:
   Since we are using MockScheduler​, the "async" part of the flush is not actually covered. We could consider covering that in KAFKA-21071 as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub) via jira" <ji...@kafka.apache.org>.
Russole commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r4431358671


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);

Review Comment:
   Thanks for the suggestion. Addressed in https://github.com/apache/kafka/pull/24052.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub)" <gi...@apache.org>.
Russole commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3950525510


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,565 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldNotOverwriteLogEndOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        long logEndOffset = 9;
+
+        cache.assign(2, logEndOffset);
+
+        cache.assign(2, 10);
+
+        assertEquals(logEndOffset, cache.epochEntries().get(0).startOffset());
+        assertEquals(List.of(new EpochEntry(2, 9)), cache.epochEntries());
+    }

Review Comment:
   Good point. These tests cover the same behavior, and the `logEndOffset` name is misleading here. I've merged them into a single test and kept the full `epochEntries()` assertion. Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "m1a2st (via GitHub)" <gi...@apache.org>.
m1a2st commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r3975010934


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,559 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }

Review Comment:
   It seems that `shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned` and `shouldNotUpdateEpochAndStartOffsetIfItDidNotChange` are testing the same behavior as well. Would it make sense to combine these two tests too.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-21036: Migrate LeaderEpochFileCacheTest to storage module [kafka]

Posted by "Russole (via GitHub) via jira" <ji...@kafka.apache.org>.
Russole commented on code in PR #23380:
URL: https://github.com/apache/kafka/pull/23380#discussion_r4431359807


##########
storage/src/test/java/org/apache/kafka/storage/internals/epoch/LeaderEpochFileCacheTest.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.storage.internals.epoch;
+
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.server.util.MockTime;
+import org.apache.kafka.storage.internals.checkpoint.LeaderEpochCheckpointFile;
+import org.apache.kafka.storage.internals.log.EpochEntry;
+import org.apache.kafka.storage.internals.log.LogDirFailureChannel;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH;
+import static org.apache.kafka.common.requests.OffsetsForLeaderEpochResponse.UNDEFINED_EPOCH_OFFSET;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for the LeaderEpochFileCache.
+ */
+public class LeaderEpochFileCacheTest {
+    private final TopicPartition tp = new TopicPartition("TestTopic", 5);
+
+    private LeaderEpochCheckpointFile checkpoint;
+    private LeaderEpochFileCache cache;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        MockTime mockTime = new MockTime();
+        checkpoint = new LeaderEpochCheckpointFile(TestUtils.tempFile(), new LogDirFailureChannel(1));
+        cache = new LeaderEpochFileCache(tp, checkpoint, mockTime.scheduler);
+    }
+
+    @Test
+    public void testPreviousEpoch() {
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(2, 10);
+        assertEquals(OptionalInt.empty(), cache.previousEpoch());
+
+        cache.assign(4, 15);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+
+        cache.assign(10, 20);
+        assertEquals(OptionalInt.of(4), cache.previousEpoch());
+
+        cache.truncateFromEndAsyncFlush(18);
+        assertEquals(OptionalInt.of(2), cache.previousEpoch());
+    }
+
+    @Test
+    public void shouldAddEpochAndMessageOffsetToCache() {
+        cache.assign(2, 10);
+        long logEndOffset = 11;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(new EpochEntry(2, 10), cache.epochEntries().get(0));
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnLogEndOffsetIfLatestEpochRequested() {
+        cache.assign(2, 11);
+        cache.assign(2, 12);
+        long logEndOffset = 14;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+    }
+
+    @Test
+    public void shouldReturnUndefinedOffsetIfUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> expectedEpochEndOffset = Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET);
+
+        cache.assign(2, 11);
+        cache.assign(3, 12);
+
+        Map.Entry<Integer, Long> epochAndOffsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 0L);
+
+        assertEquals(
+                expectedEpochEndOffset,
+                epochAndOffsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Cache not empty.");
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingStartOffsets() {
+        cache.assign(2, 9);
+
+        cache.assign(3, 9);
+
+        assertEquals(List.of(new EpochEntry(3, 9)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldNotOverwriteOffsetForALeaderEpochOnceItHasBeenAssigned() {
+        cache.assign(2, 6);
+
+        cache.assign(2, 10);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecorded() {
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(0, 0L));
+    }
+
+    @Test
+    public void shouldReturnUnsupportedIfNoEpochRecordedAndUndefinedEpochRequested() {
+        Map.Entry<Integer, Long> offsetFor = cache.endOffsetFor(UNDEFINED_EPOCH, 73);
+
+        assertEquals(
+                Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET),
+                offsetFor,
+                "Expected undefined epoch and offset if undefined epoch requested. Empty cache.");
+    }
+
+    @Test
+    public void shouldReturnFirstEpochIfRequestedEpochLessThanFirstEpoch() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        Map.Entry<Integer, Long> epochAndOffset = cache.endOffsetFor(4, 0L);
+
+        assertEquals(Map.entry(4, 11L), epochAndOffset);
+    }
+
+    @Test
+    public void shouldTruncateIfMatchingEpochButEarlierStartingOffset() {
+        cache.assign(5, 11);
+        cache.assign(6, 12);
+        cache.assign(7, 13);
+
+        cache.assign(7, 12);
+
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(5, 0L));
+        assertEquals(Map.entry(5, 12L), cache.endOffsetFor(6, 0L));
+    }
+
+    @Test
+    public void shouldGetFirstOffsetOfSubsequentEpochWhenOffsetRequestedForPreviousEpoch() {
+        cache.assign(1, 11);
+        cache.assign(1, 12);
+        cache.assign(2, 13);
+        cache.assign(2, 14);
+        cache.assign(3, 15);
+        cache.assign(3, 16);
+
+        assertEquals(Map.entry(2, 15L), cache.endOffsetFor(2, 17));
+    }
+
+    @Test
+    public void shouldReturnNextAvailableEpochIfThereIsNoExactEpochForTheOneRequested() {
+        cache.assign(0, 10);
+        cache.assign(2, 13);
+        cache.assign(4, 17);
+
+        assertEquals(Map.entry(0, 13L), cache.endOffsetFor(1, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(2, 0L));
+        assertEquals(Map.entry(2, 17L), cache.endOffsetFor(3, 0L));
+    }
+
+    @Test
+    public void shouldReturnInvalidOffsetIfEpochIsRequestedWhichIsNotCurrentlyTracked() {
+        cache.assign(2, 100);
+
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(3, 100));
+    }
+
+    @Test
+    public void shouldSupportEpochsThatDoNotStartFromZero() {
+        cache.assign(2, 6);
+        long logEndOffset = 7;
+
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(1, cache.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldPersistEpochsBetweenInstances() throws IOException {
+        String checkpointPath = TestUtils.tempFile().getAbsolutePath();
+        LeaderEpochCheckpointFile checkpoint = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+
+        LeaderEpochFileCache cache = new LeaderEpochFileCache(tp, checkpoint, new MockTime().scheduler);
+        cache.assign(2, 6);
+
+        LeaderEpochCheckpointFile checkpoint2 = new LeaderEpochCheckpointFile(
+                new File(checkpointPath),
+                new LogDirFailureChannel(1));
+        LeaderEpochFileCache cache2 = new LeaderEpochFileCache(tp, checkpoint2, new MockTime().scheduler);
+
+        assertEquals(1, cache2.epochEntries().size());
+        assertEquals(new EpochEntry(2, 6), cache2.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldEnforceMonotonicallyIncreasingEpochs() {
+        cache.assign(1, 5);
+        cache.assign(2, 6);
+
+        // When we update an epoch in the past with a different offset, the log has already reached
+        // an inconsistent state. Our options are either to raise an error, ignore the new append,
+        // or truncate the cached epochs to the point of conflict. We take this latter approach in
+        // order to guarantee that epochs and offsets in the cache increase monotonically, which makes
+        // the search logic simpler to reason about.
+        cache.assign(1, 7);
+
+        long logEndOffset = 8;
+
+        assertEquals(Optional.of(1), cache.latestEpoch());
+        assertEquals(Map.entry(1, logEndOffset), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(UNDEFINED_EPOCH, UNDEFINED_EPOCH_OFFSET), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(new EpochEntry(1, 7), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldEnforceOffsetsIncreaseMonotonically() {
+        cache.assign(2, 6);
+        cache.assign(3, 5);
+
+        assertEquals(new EpochEntry(3, 5), cache.epochEntries().get(0));
+    }
+
+    @Test
+    public void shouldIncreaseAndTrackEpochsAsLeadersChangeManyTimes() {
+        long logEndOffset = 0L;
+
+        cache.assign(0, 0);
+
+        cache.assign(1, 0);
+
+        assertEquals(Optional.of(1), cache.latestEpoch());
+        assertEquals(Map.entry(1, 0L), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(0, 0L), cache.endOffsetFor(0, logEndOffset));
+
+        logEndOffset = 5L;
+
+        assertEquals(Map.entry(1, 5L), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(0, 0L), cache.endOffsetFor(0, logEndOffset));
+
+        cache.assign(2, 5);
+
+        logEndOffset = 10;
+
+        assertEquals(Map.entry(2, 10L), cache.endOffsetFor(2, logEndOffset));
+        assertEquals(Map.entry(1, 5L), cache.endOffsetFor(1, logEndOffset));
+        assertEquals(Map.entry(0, 0L), cache.endOffsetFor(0, logEndOffset));
+    }
+
+    @Test
+    public void shouldIncreaseAndTrackEpochsAsFollowerReceivesManyMessages() {
+        cache.assign(0, 0);
+        long logEndOffset = 1;
+        cache.assign(0, 1);
+        logEndOffset = 2;
+        cache.assign(0, 2);
+        logEndOffset = 3;
+
+        assertEquals(Optional.of(0), cache.latestEpoch());
+        assertEquals(Map.entry(0, logEndOffset), cache.endOffsetFor(0, logEndOffset));
+
+        cache.assign(1, 3);
+        logEndOffset = 4;
+        cache.assign(1, 4);
+        logEndOffset = 5;
+        cache.assign(1, 5);
+        logEndOffset = 6;
+
+        assertEquals(Optional.of(1), cache.latestEpoch());
+        assertEquals(Map.entry(1, logEndOffset), cache.endOffsetFor(1, logEndOffset));
+
+        cache.assign(2, 6);
+        logEndOffset = 7;
+        cache.assign(2, 7);
+        logEndOffset = 8;
+        cache.assign(2, 8);
+        logEndOffset = 9;
+
+        assertEquals(Optional.of(2), cache.latestEpoch());
+        assertEquals(Map.entry(2, logEndOffset), cache.endOffsetFor(2, logEndOffset));
+
+        assertEquals(Map.entry(0, 3L), cache.endOffsetFor(0, logEndOffset));
+        assertEquals(Map.entry(1, 6L), cache.endOffsetFor(1, logEndOffset));
+    }
+
+    @Test
+    public void shouldDropEntriesOnEpochBoundaryWhenRemovingLatestEntries() {
+        cache.assign(2, 6);
+        cache.assign(3, 8);
+        cache.assign(4, 11);
+
+        cache.truncateFromEndAsyncFlush(8);
+
+        assertEquals(List.of(new EpochEntry(2, 6)), cache.epochEntries());
+    }
+
+    @Test
+    public void shouldPreserveResetOffsetOnClearEarliestIfOneExists() {
+        cache.assign(2, 6);
+        cache.assign(3, 8);
+        cache.assign(4, 11);
+
+        cache.truncateFromStartAsyncFlush(8);

Review Comment:
   Thanks for the suggestion. This is now covered in https://github.com/apache/kafka/pull/24052.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org