slachiewicz commented on code in PR #289: URL: https://github.com/apache/flink-connector-kafka/pull/289#discussion_r3967604045
########## flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/dynamic/source/enumerator/ReaderRecoveryGate.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.flink.connector.kafka.dynamic.source.enumerator; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.connector.kafka.dynamic.source.split.DynamicKafkaSourceSplit; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.TreeMap; + +/** + * Tracks the recovery-time reader registration state of the {@link DynamicKafkaSourceEnumerator}. + * + * <p>When the enumerator is restored from checkpointed state, split assignment and metadata update + * events must be deferred until the first metadata discovery has completed and every reader has + * (re-)registered, so that restored reader splits can be redistributed consistently. This class + * owns that gating state; the enumerator remains responsible for acting on it. Review Comment: Updated class Javadoc in `a9ec7c19` to detail both recovery triggers (restore and partial failover re-registration with reported splits) and their respective gating conditions. ########## flink-connector-kafka/src/main/java/org/apache/flink/connector/kafka/dynamic/source/enumerator/DynamicKafkaSourceEnumerator.java: ########## @@ -830,7 +819,7 @@ private void reassignReportedSplits() { long currentTimeMillis = System.currentTimeMillis(); for (Entry<Integer, List<DynamicKafkaSourceSplit>> readerSplits : - new TreeMap<>(pendingReportedSplitsByReader).entrySet()) { + readerRecoveryGate.drainReportedSplits().entrySet()) { Review Comment: Added a note on `drainReportedSplits` in `a9ec7c19` that the state is cleared eagerly and must not be consulted while reassigning. ########## flink-connector-kafka/src/test/java/org/apache/flink/connector/kafka/dynamic/source/enumerator/ReaderRecoveryGateTest.java: ########## @@ -0,0 +1,138 @@ +/* + * 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.flink.connector.kafka.dynamic.source.enumerator; + +import org.apache.flink.connector.kafka.dynamic.source.split.DynamicKafkaSourceSplit; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit; + +import org.apache.kafka.common.TopicPartition; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.NavigableMap; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Tests for {@link ReaderRecoveryGate}. */ +class ReaderRecoveryGateTest { + + @Test + void testFreshStartHasNoPendingRecovery() { + ReaderRecoveryGate gate = new ReaderRecoveryGate(false); + + assertThat(gate.hasPendingRecovery()).isFalse(); + assertThat(gate.shouldDeferMetadataUpdateEvents(false)).isFalse(); + assertThat(gate.shouldDeferMetadataUpdateEvents(true)).isFalse(); + assertThat(gate.hasReportedSplits()).isFalse(); + } + + @Test + void testRestoredStartGatesUntilInitialRegistrationCompletes() { + ReaderRecoveryGate gate = new ReaderRecoveryGate(true); + + assertThat(gate.hasPendingRecovery()).isTrue(); + assertThat(gate.shouldDeferMetadataUpdateEvents(true)).isTrue(); + assertThat(gate.shouldDeferMetadataUpdateEvents(false)).isTrue(); + + gate.markInitialRegistrationComplete(); + + assertThat(gate.hasPendingRecovery()).isFalse(); + assertThat(gate.shouldDeferMetadataUpdateEvents(true)).isFalse(); + } + + @Test + void testReportedSplitsGateUntilAllReadersRegistered() { + ReaderRecoveryGate gate = new ReaderRecoveryGate(false); + gate.recordReportedSplits(1, Collections.singletonList(split("topic", 0))); + + assertThat(gate.hasPendingRecovery()).isTrue(); + assertThat(gate.hasReportedSplits()).isTrue(); + assertThat(gate.shouldDeferMetadataUpdateEvents(false)).isTrue(); + assertThat(gate.shouldDeferMetadataUpdateEvents(true)).isFalse(); + } Review Comment: Added `testCompletingInitialRegistrationDoesNotReleaseGateWithPendingReportedSplits` in `a9ec7c19` to test this interaction. -- 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]
