gyfora commented on code in PR #28837:
URL: https://github.com/apache/flink/pull/28837#discussion_r3789825630


##########
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/StateTableUtils.java:
##########
@@ -0,0 +1,641 @@
+/*
+ * 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.state.api;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.state.StateDescriptor;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+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.IncrementalKeyedStateHandle;
+import org.apache.flink.runtime.state.KeyGroupsSavepointStateHandle;
+import org.apache.flink.runtime.state.KeyGroupsStateHandle;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.StateBackendLoader;
+import org.apache.flink.runtime.state.VoidNamespaceSerializer;
+import org.apache.flink.runtime.state.changelog.ChangelogStateBackendHandle;
+import org.apache.flink.state.api.schema.KeyedStateSchemaInfo;
+import 
org.apache.flink.state.api.schema.SerializerSnapshotToLogicalTypeConverter;
+import org.apache.flink.state.api.schema.StateSchemaExtractor;
+import org.apache.flink.state.api.schema.StateSchemaInfo;
+import org.apache.flink.state.table.SavepointConnectorOptions;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.factories.FactoryUtil;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.BigIntType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.VarBinaryType;
+import org.apache.flink.table.types.utils.LogicalTypeDataTypeConverter;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * High-level utility for inspecting and reading keyed state from a checkpoint 
/ savepoint without
+ * requiring user POJO classes on the classpath.
+ */
+@Internal
+public final class StateTableUtils {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(StateTableUtils.class);
+
+    private StateTableUtils() {}
+
+    /**
+     * Returns the {@link OperatorIdentifier}s of all operators present in the 
given checkpoint
+     * metadata that have at least one non-internal keyed state.
+     *
+     * @param metadata the checkpoint metadata to inspect
+     * @return list of operator identifiers; never null, may be empty
+     */
+    public static List<OperatorIdentifier> 
getOperatorIdentifiers(CheckpointMetadata metadata) {
+        return metadata.getOperatorStates().stream()
+                .filter(StateTableUtils::hasNonInternalKeyedState)
+                .map(
+                        op ->
+                                op.getOperatorUid()
+                                        .map(OperatorIdentifier::forUid)
+                                        .orElseGet(
+                                                () ->
+                                                        
OperatorIdentifier.forUidHash(
+                                                                
op.getOperatorID().toHexString())))
+                .collect(Collectors.toList());
+    }
+
+    private static boolean hasNonInternalKeyedState(OperatorState op) {
+        try {
+            List<StateSchemaInfo> schemas = 
StateSchemaExtractor.extractSchema(op);
+            ClassifiedStates classified = 
classifyStates(op.getOperatorID().toHexString(), schemas);
+            return !classified.voidNamespaceStates.isEmpty()
+                    || !classified.windowNamespaceStates.isEmpty();
+        } catch (Exception e) {
+            LOG.warn(
+                    "Could not extract state schema for operator '{}': {}. 
Excluding from catalog.",
+                    op.getOperatorID(),
+                    e.getMessage());
+            return false;
+        }
+    }
+
+    /**
+     * Returns the names of all keyed states registered by the given operator.
+     *
+     * @param metadata the checkpoint metadata to inspect
+     * @param operatorId identifies the operator
+     * @param classLoader the class loader used when reading serializer 
snapshots
+     * @return list of state names; never null, may be empty
+     * @throws IOException if the state header cannot be read
+     */
+    public static List<String> getKeyedStates(
+            CheckpointMetadata metadata, OperatorIdentifier operatorId) throws 
IOException {
+        OperatorState opState = findOperatorState(metadata, operatorId);
+        List<StateSchemaInfo> schemaInfos = 
StateSchemaExtractor.extractSchema(opState);
+        ClassifiedStates classified = classifyStates(operatorId.toString(), 
schemaInfos);
+        return classified.voidNamespaceStates.stream()
+                .map(info -> info.stateName)
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Returns the {@link KeyedStateSchemaInfo} for the plain per-key 
(void-namespace) states of the
+     * given operator — the ones exposed by the {@code _keyed}/{@code 
_keyed_flat} tables.
+     *
+     * <p>Schema extraction is lenient: POJO field names and types are derived 
from the serializer
+     * snapshot and do not require the user POJO class to be on the classpath.
+     *
+     * @param metadata the checkpoint metadata to inspect
+     * @param operatorId identifies the operator
+     * @return schema information covering the key type and all registered 
state entries
+     * @throws IOException if the state header cannot be read
+     */
+    public static KeyedStateSchemaInfo getKeyedStateSchema(
+            CheckpointMetadata metadata, OperatorIdentifier operatorId) throws 
IOException {
+        OperatorState opState = findOperatorState(metadata, operatorId);
+        List<StateSchemaInfo> schemas = 
StateSchemaExtractor.extractSchema(opState);
+        ClassifiedStates classified = classifyStates(operatorId.toString(), 
schemas);
+        return buildKeyedStateSchemaInfo(schemas, 
classified.voidNamespaceStates, null);
+    }
+
+    private static KeyedStateSchemaInfo buildKeyedStateSchemaInfo(
+            List<StateSchemaInfo> allSchemas,
+            List<StateSchemaInfo> statesToInclude,
+            @Nullable LogicalType windowLogicalType) {
+        LogicalType keyType =
+                allSchemas.isEmpty()
+                        ? new VarBinaryType(true, VarBinaryType.MAX_LENGTH)
+                        : SerializerSnapshotToLogicalTypeConverter.convert(
+                                allSchemas.get(0).keySnapshot);
+
+        LinkedHashMap<String, KeyedStateSchemaInfo.StateEntryInfo> 
stateSchemas =
+                new LinkedHashMap<>();
+        for (StateSchemaInfo info : statesToInclude) {
+            SavepointConnectorOptions.StateType stateType;
+            if (info.stateKind == StateDescriptor.Type.LIST) {
+                stateType = SavepointConnectorOptions.StateType.LIST;
+            } else if (info.stateKind == StateDescriptor.Type.MAP) {
+                stateType = SavepointConnectorOptions.StateType.MAP;
+            } else {
+                stateType = SavepointConnectorOptions.StateType.VALUE;
+            }
+
+            try {
+                LogicalType logicalType =
+                        
SerializerSnapshotToLogicalTypeConverter.convert(info.valueSnapshot);
+                stateSchemas.put(
+                        info.stateName,
+                        new KeyedStateSchemaInfo.StateEntryInfo(
+                                stateType, logicalType, windowLogicalType));
+            } catch (Exception e) {

Review Comment:
   Catching the error here simply skips a state column that may not be 
supported at the moment. Seems like the desired behaviour instead of making the 
whole table unreadable



-- 
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]

Reply via email to