leonardBang commented on code in PR #250: URL: https://github.com/apache/flink-connector-kafka/pull/250#discussion_r3234468367
########## flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/GetKafkaSourceOffsetsTableFunction.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.streaming.connectors.kafka.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplitSerializer; +import org.apache.flink.core.fs.CloseableRegistry; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.runtime.checkpoint.OperatorState; +import org.apache.flink.runtime.checkpoint.OperatorSubtaskState; +import org.apache.flink.runtime.checkpoint.metadata.CheckpointMetadata; +import org.apache.flink.runtime.state.DefaultOperatorStateBackend; +import org.apache.flink.runtime.state.DefaultOperatorStateBackendBuilder; +import org.apache.flink.runtime.state.OperatorStateHandle; +import org.apache.flink.state.api.OperatorIdentifier; +import org.apache.flink.state.api.runtime.SavepointLoader; +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.annotation.FunctionHint; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinition; +import org.apache.flink.table.functions.SpecializedFunction; +import org.apache.flink.table.functions.TableFunction; +import org.apache.flink.table.types.inference.TypeStrategies; +import org.apache.flink.types.Row; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.StreamSupport; + +import static org.apache.flink.table.functions.FunctionKind.TABLE; + +/** + * Table function to extract Kafka source offsets from savepoints. + * + * <p>This function reads Kafka partition splits from a savepoint and extracts the offset + * information for each partition. The function is designed to work with the Flink state processing + * API and provides visibility into the Kafka source state. + */ +@Internal +@FunctionHint( + output = + @DataTypeHint( + "ROW<topic STRING NOT NULL, " + + "partition INT NOT NULL, " + + "starting-offset BIGINT NOT NULL, " + + "stopping-offset BIGINT NULL>")) +public class GetKafkaSourceOffsetsTableFunction extends TableFunction<Row> { + + public static final BuiltInFunctionDefinition FUNCTION_DEFINITION = + BuiltInFunctionDefinition.newBuilder() + .name("savepoint_get_kafka_offsets") + .kind(TABLE) + .runtimeClass(GetKafkaSourceOffsetsTableFunction.class.getName()) + .outputTypeStrategy( + TypeStrategies.explicit( + DataTypes.ROW( + DataTypes.FIELD("topic", DataTypes.STRING().notNull()), + DataTypes.FIELD("partition", DataTypes.INT().notNull()), + DataTypes.FIELD( + "starting-offset", + DataTypes.BIGINT().notNull()), + DataTypes.FIELD( + "stopping-offset", + DataTypes.BIGINT().nullable())))) + .build(); + + public GetKafkaSourceOffsetsTableFunction(SpecializedFunction.SpecializedContext context) {} + + public void eval(String savepointPath, String operatorUid) { + try { + CheckpointMetadata checkpointMetadata = + SavepointLoader.loadSavepointMetadata(savepointPath); + + OperatorIdentifier operatorIdentifier = OperatorIdentifier.forUid(operatorUid); + OperatorState operatorState = + checkpointMetadata.getOperatorStates().stream() + .filter( + os -> + os.getOperatorID() + .equals(operatorIdentifier.getOperatorId())) + .findFirst() + .orElseThrow(); Review Comment: .orElseThrow(() -> new IllegalArgumentException( "Operator with UID '" + operatorUid + "' not found in savepoint: " + savepointPath)); ########## flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/GetKafkaSourceOffsetsTableFunction.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.streaming.connectors.kafka.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplitSerializer; +import org.apache.flink.core.fs.CloseableRegistry; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.runtime.checkpoint.OperatorState; +import org.apache.flink.runtime.checkpoint.OperatorSubtaskState; +import org.apache.flink.runtime.checkpoint.metadata.CheckpointMetadata; +import org.apache.flink.runtime.state.DefaultOperatorStateBackend; +import org.apache.flink.runtime.state.DefaultOperatorStateBackendBuilder; +import org.apache.flink.runtime.state.OperatorStateHandle; Review Comment: These are some `@Internal` classes that can easily break across Flink versions. While flink-state-processor-api itself is user-facing, this implementation bypasses its high-level APIs and directly operates on internals. Could we implement through the public API of flink-state-processor-api (e.g., SavepointReader)? This would reduce coupling to internal implementations. ########## flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/GetKafkaSourceOffsetsTableFunction.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.streaming.connectors.kafka.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplitSerializer; +import org.apache.flink.core.fs.CloseableRegistry; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.runtime.checkpoint.OperatorState; +import org.apache.flink.runtime.checkpoint.OperatorSubtaskState; +import org.apache.flink.runtime.checkpoint.metadata.CheckpointMetadata; +import org.apache.flink.runtime.state.DefaultOperatorStateBackend; +import org.apache.flink.runtime.state.DefaultOperatorStateBackendBuilder; +import org.apache.flink.runtime.state.OperatorStateHandle; +import org.apache.flink.state.api.OperatorIdentifier; +import org.apache.flink.state.api.runtime.SavepointLoader; +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.annotation.FunctionHint; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinition; +import org.apache.flink.table.functions.SpecializedFunction; +import org.apache.flink.table.functions.TableFunction; +import org.apache.flink.table.types.inference.TypeStrategies; +import org.apache.flink.types.Row; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.StreamSupport; + +import static org.apache.flink.table.functions.FunctionKind.TABLE; + +/** + * Table function to extract Kafka source offsets from savepoints. + * + * <p>This function reads Kafka partition splits from a savepoint and extracts the offset + * information for each partition. The function is designed to work with the Flink state processing + * API and provides visibility into the Kafka source state. + */ +@Internal +@FunctionHint( + output = + @DataTypeHint( + "ROW<topic STRING NOT NULL, " + + "partition INT NOT NULL, " + + "starting-offset BIGINT NOT NULL, " + + "stopping-offset BIGINT NULL>")) +public class GetKafkaSourceOffsetsTableFunction extends TableFunction<Row> { + + public static final BuiltInFunctionDefinition FUNCTION_DEFINITION = + BuiltInFunctionDefinition.newBuilder() + .name("savepoint_get_kafka_offsets") + .kind(TABLE) + .runtimeClass(GetKafkaSourceOffsetsTableFunction.class.getName()) + .outputTypeStrategy( + TypeStrategies.explicit( + DataTypes.ROW( + DataTypes.FIELD("topic", DataTypes.STRING().notNull()), + DataTypes.FIELD("partition", DataTypes.INT().notNull()), + DataTypes.FIELD( + "starting-offset", + DataTypes.BIGINT().notNull()), + DataTypes.FIELD( + "stopping-offset", + DataTypes.BIGINT().nullable())))) + .build(); + + public GetKafkaSourceOffsetsTableFunction(SpecializedFunction.SpecializedContext context) {} + + public void eval(String savepointPath, String operatorUid) { Review Comment: ```suggestion public void eval(String savepointPath, String operatorUid) { Preconditions.checkNotNull(savepointPath, "savepointPath must not be null"); Preconditions.checkNotNull(operatorUid, "operatorUid must not be null"); ``` ########## .github/workflows/push_pr.yml: ########## @@ -28,7 +28,7 @@ jobs: compile_and_test: strategy: matrix: - flink: &flink_versions [ 2.2.0, 2.1.1 ] + flink: &flink_versions [ 2.2.0 ] Review Comment: Bumping to 2.2.1 is fine since it's just a bugfix version upgrade. However, dropping 2.1.1 support here is not appropriate, as it means the Kafka connector would no longer support the Flink 2.1.x series. I'd suggest we open a separate PR for the version drop, since it has user-facing implications. ########## flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/GetKafkaSourceOffsetsTableFunction.java: ########## @@ -0,0 +1,157 @@ +/* + * 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.streaming.connectors.kafka.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit; +import org.apache.flink.connector.kafka.source.split.KafkaPartitionSplitSerializer; +import org.apache.flink.core.fs.CloseableRegistry; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.runtime.checkpoint.OperatorState; +import org.apache.flink.runtime.checkpoint.OperatorSubtaskState; +import org.apache.flink.runtime.checkpoint.metadata.CheckpointMetadata; +import org.apache.flink.runtime.state.DefaultOperatorStateBackend; +import org.apache.flink.runtime.state.DefaultOperatorStateBackendBuilder; +import org.apache.flink.runtime.state.OperatorStateHandle; +import org.apache.flink.state.api.OperatorIdentifier; +import org.apache.flink.state.api.runtime.SavepointLoader; +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.annotation.FunctionHint; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinition; +import org.apache.flink.table.functions.SpecializedFunction; +import org.apache.flink.table.functions.TableFunction; +import org.apache.flink.table.types.inference.TypeStrategies; +import org.apache.flink.types.Row; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.StreamSupport; + +import static org.apache.flink.table.functions.FunctionKind.TABLE; + +/** + * Table function to extract Kafka source offsets from savepoints. + * + * <p>This function reads Kafka partition splits from a savepoint and extracts the offset + * information for each partition. The function is designed to work with the Flink state processing + * API and provides visibility into the Kafka source state. + */ +@Internal +@FunctionHint( + output = + @DataTypeHint( + "ROW<topic STRING NOT NULL, " + + "partition INT NOT NULL, " + + "starting-offset BIGINT NOT NULL, " + + "stopping-offset BIGINT NULL>")) +public class GetKafkaSourceOffsetsTableFunction extends TableFunction<Row> { + + public static final BuiltInFunctionDefinition FUNCTION_DEFINITION = + BuiltInFunctionDefinition.newBuilder() + .name("savepoint_get_kafka_offsets") + .kind(TABLE) + .runtimeClass(GetKafkaSourceOffsetsTableFunction.class.getName()) + .outputTypeStrategy( + TypeStrategies.explicit( + DataTypes.ROW( + DataTypes.FIELD("topic", DataTypes.STRING().notNull()), + DataTypes.FIELD("partition", DataTypes.INT().notNull()), + DataTypes.FIELD( + "starting-offset", + DataTypes.BIGINT().notNull()), + DataTypes.FIELD( + "stopping-offset", + DataTypes.BIGINT().nullable())))) + .build(); + + public GetKafkaSourceOffsetsTableFunction(SpecializedFunction.SpecializedContext context) {} + + public void eval(String savepointPath, String operatorUid) { + try { + CheckpointMetadata checkpointMetadata = + SavepointLoader.loadSavepointMetadata(savepointPath); + + OperatorIdentifier operatorIdentifier = OperatorIdentifier.forUid(operatorUid); + OperatorState operatorState = + checkpointMetadata.getOperatorStates().stream() + .filter( + os -> + os.getOperatorID() + .equals(operatorIdentifier.getOperatorId())) + .findFirst() + .orElseThrow(); + List<OperatorStateHandle> stateHandles = + operatorState.getStates().stream() + .map(OperatorSubtaskState::getManagedOperatorState) + .collect(ArrayList::new, List::addAll, List::addAll); + CloseableRegistry cancelStreamRegistry = new CloseableRegistry(); Review Comment: The `CloseableRegistry` is created but never closed. While this may not cause resource leaks in this specific scenario? -- 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]
