github-advanced-security[bot] commented on code in PR #18750: URL: https://github.com/apache/druid/pull/18750#discussion_r2621685690
########## indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/OffsetSnapshot.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.druid.indexing.seekablestream.supervisor; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; + +import javax.annotation.Nullable; +import java.util.Map; +import java.util.Objects; + +/** + * Immutable snapshot containing a consistent pair of offset maps: the highest ingested offsets + * reported by tasks and the latest end offsets fetched from the underlying stream. + * + * <p> + * The supervisor fetches task-reported ingested offsets first, then fetches end offsets from the stream. + * Because these two values are captured at slightly different instants, the reported lag + * (latestOffsetsFromStream - highestIngestedOffsets) may be slightly larger than the actual lag at any + * precise moment. + * + * <p> + * By publishing both maps together as a single atomic snapshot (using {@link java.util.concurrent.atomic.AtomicReference}), + * readers (such as lag metrics and supervisor status) always observe a coherent and consistent view. + * This produces stable and monotonic lag trends, avoiding artifacts like temporary negative lags. + * + * <p> + * This class is generic and can be reused by all seekable-stream supervisors (Kafka, Kinesis, etc.). + */ +public final class OffsetSnapshot<PartitionIdType, SequenceOffsetType> +{ + private final ImmutableMap<PartitionIdType, SequenceOffsetType> highestIngestedOffsets; + private final ImmutableMap<PartitionIdType, SequenceOffsetType> latestOffsetsFromStream; + + private OffsetSnapshot( + @Nullable Map<PartitionIdType, SequenceOffsetType> highestIngestedOffsets, + @Nullable Map<PartitionIdType, SequenceOffsetType> latestOffsetsFromStream + ) + { + this.highestIngestedOffsets = toImmutableOffsetMap(highestIngestedOffsets); + this.latestOffsetsFromStream = toImmutableOffsetMap(latestOffsetsFromStream); + } + + public static <PartitionIdType, SequenceOffsetType> OffsetSnapshot<PartitionIdType, SequenceOffsetType> of( + @Nullable Map<PartitionIdType, SequenceOffsetType> currentOffsets, + @Nullable Map<PartitionIdType, SequenceOffsetType> endOffsets + ) + { + return new OffsetSnapshot<>(currentOffsets, endOffsets); + } + + private ImmutableMap<PartitionIdType, SequenceOffsetType> toImmutableOffsetMap( + @Nullable Map<PartitionIdType, SequenceOffsetType> input + ) + { + if (input == null) { + return ImmutableMap.of(); + } + + return ImmutableMap.copyOf(Maps.filterValues(input, Objects::nonNull)); + } + + public ImmutableMap<PartitionIdType, SequenceOffsetType> getHighestIngestedOffsets() Review Comment: ## Exposing internal representation getHighestIngestedOffsets exposes the internal representation stored in field highestIngestedOffsets. The value may be modified [after this call to getHighestIngestedOffsets](1). [Show more details](https://github.com/apache/druid/security/code-scanning/10599) ########## indexing-service/src/test/java/org/apache/druid/indexing/seekablestream/supervisor/OffsetSnapshotTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.druid.indexing.seekablestream.supervisor; + +import com.google.common.collect.ImmutableMap; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class OffsetSnapshotTest +{ + @Test + public void testOffsetSnapshot_emptyInputReturnsEmptyMap() + { + OffsetSnapshot<String, Long> snapshot = OffsetSnapshot.of( + Collections.emptyMap(), + Collections.emptyMap() + ); + + Assert.assertTrue(snapshot.getHighestIngestedOffsets().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getHighestIngestedOffsets()); + Assert.assertTrue(snapshot.getLatestOffsetsFromStream().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_nullInputsReturnEmptyMaps() + { + OffsetSnapshot<Integer, Long> snapshot = OffsetSnapshot.of(null, null); + + Assert.assertTrue(snapshot.getHighestIngestedOffsets().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getHighestIngestedOffsets()); + Assert.assertTrue(snapshot.getLatestOffsetsFromStream().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_nullCurrentOffsetsReturnsEmptyCurrentMap() + { + Map<Integer, Long> endOffsets = ImmutableMap.of(0, 100L, 1, 200L); + + OffsetSnapshot<Integer, Long> snapshot = OffsetSnapshot.of(null, endOffsets); + + Assert.assertTrue(snapshot.getHighestIngestedOffsets().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getHighestIngestedOffsets()); + Assert.assertEquals(endOffsets, snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_nullEndOffsetsReturnsEmptyEndMap() + { + Map<Integer, Long> currentOffsets = ImmutableMap.of(0, 50L, 1, 150L); + + OffsetSnapshot<Integer, Long> snapshot = OffsetSnapshot.of(currentOffsets, null); + + Assert.assertEquals(currentOffsets, snapshot.getHighestIngestedOffsets()); + Assert.assertTrue(snapshot.getLatestOffsetsFromStream().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_copiesInputMapsAndReturnsImmutableCopies() + { + Map<String, String> current = new HashMap<>(); + current.put("p0", "100"); + current.put("p1", "200"); + + Map<String, String> end = new HashMap<>(); + end.put("p0", "150"); + end.put("p2", "300"); + + OffsetSnapshot<String, String> snapshot = OffsetSnapshot.of(current, end); + + Assert.assertEquals(ImmutableMap.of("p0", "100", "p1", "200"), snapshot.getHighestIngestedOffsets()); + Assert.assertEquals(ImmutableMap.of("p0", "150", "p2", "300"), snapshot.getLatestOffsetsFromStream()); + + // Returned maps must be immutable + Assert.assertThrows( + UnsupportedOperationException.class, + () -> snapshot.getHighestIngestedOffsets().put("x", "x") + ); + Assert.assertThrows( + UnsupportedOperationException.class, + () -> snapshot.getLatestOffsetsFromStream().put("x", "x") Review Comment: ## Deprecated method or constructor invocation Invoking [ImmutableMap.put](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/druid/security/code-scanning/10601) ########## indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/OffsetSnapshot.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.druid.indexing.seekablestream.supervisor; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; + +import javax.annotation.Nullable; +import java.util.Map; +import java.util.Objects; + +/** + * Immutable snapshot containing a consistent pair of offset maps: the highest ingested offsets + * reported by tasks and the latest end offsets fetched from the underlying stream. + * + * <p> + * The supervisor fetches task-reported ingested offsets first, then fetches end offsets from the stream. + * Because these two values are captured at slightly different instants, the reported lag + * (latestOffsetsFromStream - highestIngestedOffsets) may be slightly larger than the actual lag at any + * precise moment. + * + * <p> + * By publishing both maps together as a single atomic snapshot (using {@link java.util.concurrent.atomic.AtomicReference}), + * readers (such as lag metrics and supervisor status) always observe a coherent and consistent view. + * This produces stable and monotonic lag trends, avoiding artifacts like temporary negative lags. + * + * <p> + * This class is generic and can be reused by all seekable-stream supervisors (Kafka, Kinesis, etc.). + */ +public final class OffsetSnapshot<PartitionIdType, SequenceOffsetType> +{ + private final ImmutableMap<PartitionIdType, SequenceOffsetType> highestIngestedOffsets; + private final ImmutableMap<PartitionIdType, SequenceOffsetType> latestOffsetsFromStream; + + private OffsetSnapshot( + @Nullable Map<PartitionIdType, SequenceOffsetType> highestIngestedOffsets, + @Nullable Map<PartitionIdType, SequenceOffsetType> latestOffsetsFromStream + ) + { + this.highestIngestedOffsets = toImmutableOffsetMap(highestIngestedOffsets); + this.latestOffsetsFromStream = toImmutableOffsetMap(latestOffsetsFromStream); + } + + public static <PartitionIdType, SequenceOffsetType> OffsetSnapshot<PartitionIdType, SequenceOffsetType> of( + @Nullable Map<PartitionIdType, SequenceOffsetType> currentOffsets, + @Nullable Map<PartitionIdType, SequenceOffsetType> endOffsets + ) + { + return new OffsetSnapshot<>(currentOffsets, endOffsets); + } + + private ImmutableMap<PartitionIdType, SequenceOffsetType> toImmutableOffsetMap( + @Nullable Map<PartitionIdType, SequenceOffsetType> input + ) + { + if (input == null) { + return ImmutableMap.of(); + } + + return ImmutableMap.copyOf(Maps.filterValues(input, Objects::nonNull)); + } + + public ImmutableMap<PartitionIdType, SequenceOffsetType> getHighestIngestedOffsets() + { + return highestIngestedOffsets; + } + + public ImmutableMap<PartitionIdType, SequenceOffsetType> getLatestOffsetsFromStream() Review Comment: ## Exposing internal representation getLatestOffsetsFromStream exposes the internal representation stored in field latestOffsetsFromStream. The value may be modified [after this call to getLatestOffsetsFromStream](1). [Show more details](https://github.com/apache/druid/security/code-scanning/10598) ########## indexing-service/src/test/java/org/apache/druid/indexing/seekablestream/supervisor/OffsetSnapshotTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.druid.indexing.seekablestream.supervisor; + +import com.google.common.collect.ImmutableMap; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class OffsetSnapshotTest +{ + @Test + public void testOffsetSnapshot_emptyInputReturnsEmptyMap() + { + OffsetSnapshot<String, Long> snapshot = OffsetSnapshot.of( + Collections.emptyMap(), + Collections.emptyMap() + ); + + Assert.assertTrue(snapshot.getHighestIngestedOffsets().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getHighestIngestedOffsets()); + Assert.assertTrue(snapshot.getLatestOffsetsFromStream().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_nullInputsReturnEmptyMaps() + { + OffsetSnapshot<Integer, Long> snapshot = OffsetSnapshot.of(null, null); + + Assert.assertTrue(snapshot.getHighestIngestedOffsets().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getHighestIngestedOffsets()); + Assert.assertTrue(snapshot.getLatestOffsetsFromStream().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_nullCurrentOffsetsReturnsEmptyCurrentMap() + { + Map<Integer, Long> endOffsets = ImmutableMap.of(0, 100L, 1, 200L); + + OffsetSnapshot<Integer, Long> snapshot = OffsetSnapshot.of(null, endOffsets); + + Assert.assertTrue(snapshot.getHighestIngestedOffsets().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getHighestIngestedOffsets()); + Assert.assertEquals(endOffsets, snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_nullEndOffsetsReturnsEmptyEndMap() + { + Map<Integer, Long> currentOffsets = ImmutableMap.of(0, 50L, 1, 150L); + + OffsetSnapshot<Integer, Long> snapshot = OffsetSnapshot.of(currentOffsets, null); + + Assert.assertEquals(currentOffsets, snapshot.getHighestIngestedOffsets()); + Assert.assertTrue(snapshot.getLatestOffsetsFromStream().isEmpty()); + Assert.assertSame(ImmutableMap.of(), snapshot.getLatestOffsetsFromStream()); + } + + @Test + public void testOffsetSnapshot_copiesInputMapsAndReturnsImmutableCopies() + { + Map<String, String> current = new HashMap<>(); + current.put("p0", "100"); + current.put("p1", "200"); + + Map<String, String> end = new HashMap<>(); + end.put("p0", "150"); + end.put("p2", "300"); + + OffsetSnapshot<String, String> snapshot = OffsetSnapshot.of(current, end); + + Assert.assertEquals(ImmutableMap.of("p0", "100", "p1", "200"), snapshot.getHighestIngestedOffsets()); + Assert.assertEquals(ImmutableMap.of("p0", "150", "p2", "300"), snapshot.getLatestOffsetsFromStream()); + + // Returned maps must be immutable + Assert.assertThrows( + UnsupportedOperationException.class, + () -> snapshot.getHighestIngestedOffsets().put("x", "x") Review Comment: ## Deprecated method or constructor invocation Invoking [ImmutableMap.put](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/druid/security/code-scanning/10600) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
