smjn commented on code in PR #18014:
URL: https://github.com/apache/kafka/pull/18014#discussion_r1878761584


##########
share-coordinator/src/test/java/org/apache/kafka/coordinator/share/ShareCoordinatorOffsetsManagerTest.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.coordinator.share;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.server.share.SharePartitionKey;
+import org.apache.kafka.timeline.SnapshotRegistry;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ShareCoordinatorOffsetsManagerTest {
+
+    private ShareCoordinatorOffsetsManager manager;
+    private static final SharePartitionKey KEY1 = 
SharePartitionKey.getInstance("gs1", Uuid.randomUuid(), 0);
+    private static final SharePartitionKey KEY2 = 
SharePartitionKey.getInstance("gs2", Uuid.randomUuid(), 0);
+    private static final SharePartitionKey KEY3 = 
SharePartitionKey.getInstance("gs1", Uuid.randomUuid(), 1);
+    private static final SharePartitionKey KEY4 = 
SharePartitionKey.getInstance("gs1", Uuid.randomUuid(), 7);
+
+    @BeforeEach
+    public void setUp() {
+        manager = new ShareCoordinatorOffsetsManager(new SnapshotRegistry(new 
LogContext()));
+    }
+
+    @Test
+    public void testUpdateStateAddsToInternalState() {
+        manager.updateState(KEY1, 0L);
+        assertEquals(Optional.empty(), manager.lastRedundantOffset());
+
+        manager.updateState(KEY1, 10L);
+        assertEquals(Optional.of(10L), manager.lastRedundantOffset()); // 
[0-9] offsets are redundant.
+
+        manager.updateState(KEY2, 15L);
+        assertEquals(Optional.empty(), manager.lastRedundantOffset());  // No 
update to last redundant after adding 15L so, empty.
+
+        assertEquals(10L, manager.curState().get(KEY1));
+        assertEquals(15L, manager.curState().get(KEY2));
+    }
+
+    private static class ShareOffsetTestHolder {
+        static class TestTuple {
+            final SharePartitionKey key;
+            final long offset;
+            final Optional<Long> expectedOffset;
+
+            private TestTuple(SharePartitionKey key, long offset, 
Optional<Long> expectedOffset) {
+                this.key = key;
+                this.offset = offset;
+                this.expectedOffset = expectedOffset;
+            }
+
+            static TestTuple instance(SharePartitionKey key, long offset, 
Optional<Long> expectedOffset) {
+                return new TestTuple(key, offset, expectedOffset);
+            }
+        }
+
+        private final String testName;
+        private final List<TestTuple> tuples;
+        private final boolean shouldRun;
+
+        ShareOffsetTestHolder(String testName, List<TestTuple> tuples) {
+            this(testName, tuples, true);
+        }
+
+        ShareOffsetTestHolder(String testName, List<TestTuple> tuples, boolean 
shouldRun) {
+            this.testName = testName;
+            this.tuples = tuples;
+            this.shouldRun = shouldRun;
+        }
+    }
+
+    static Stream<ShareOffsetTestHolder> generateNoRedundantStateCases() {
+        return Stream.of(
+            new ShareOffsetTestHolder(
+                "no redundant state single key",
+                List.of(
+                    ShareOffsetTestHolder.TestTuple.instance(KEY1, 10L, 
Optional.empty())
+                )
+            ),
+
+            new ShareOffsetTestHolder(
+                "no redundant state multiple keys",
+                List.of(
+                    ShareOffsetTestHolder.TestTuple.instance(KEY1, 10L, 
Optional.empty()),
+                    ShareOffsetTestHolder.TestTuple.instance(KEY4, 11L, 
Optional.empty()),
+                    ShareOffsetTestHolder.TestTuple.instance(KEY2, 13L, 
Optional.empty())

Review Comment:
   Small efficiency gain here as the offsets will be applied in increasing 
order (partition records auto inc offset) to the `updateState` method. If the 
case is such that 10L is the smallest one - it means there are no offsets 
smaller than that present in the topic partition, hence returning 10L will be 
of no consequence and save an extra `deleteRecords` call.
   
   In the algorithm we have chosen to get at least 2 offsets for any key before 
exposing the redundant offset. 
   
   Also since we are exposing the offset only once and then setting the boolean 
flag, the 2nd and 3rd line should be `empty`. 



-- 
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-unsubscr...@kafka.apache.org

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

Reply via email to