|
19 | 19 | package org.apache.flink.state.api.runtime; |
20 | 20 |
|
21 | 21 | import org.apache.flink.annotation.Internal; |
| 22 | +import org.apache.flink.core.fs.FSDataInputStream; |
| 23 | +import org.apache.flink.core.memory.DataInputViewStreamWrapper; |
22 | 24 | import org.apache.flink.runtime.checkpoint.Checkpoints; |
| 25 | +import org.apache.flink.runtime.checkpoint.OperatorState; |
23 | 26 | import org.apache.flink.runtime.checkpoint.metadata.CheckpointMetadata; |
24 | 27 | import org.apache.flink.runtime.state.CompletedCheckpointStorageLocation; |
| 28 | +import org.apache.flink.runtime.state.KeyGroupsStateHandle; |
| 29 | +import org.apache.flink.runtime.state.KeyedBackendSerializationProxy; |
| 30 | +import org.apache.flink.runtime.state.KeyedStateHandle; |
| 31 | +import org.apache.flink.runtime.state.StreamStateHandle; |
25 | 32 | import org.apache.flink.runtime.state.filesystem.AbstractFsCheckpointStorageAccess; |
| 33 | +import org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot; |
| 34 | +import org.apache.flink.state.api.OperatorIdentifier; |
26 | 35 |
|
27 | 36 | import java.io.DataInputStream; |
28 | 37 | import java.io.IOException; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.function.Function; |
| 40 | +import java.util.stream.Collectors; |
29 | 41 |
|
30 | | -/** Utility class for loading {@link CheckpointMetadata} metadata. */ |
| 42 | +/** Utility class for loading savepoint metadata and operator state information. */ |
31 | 43 | @Internal |
32 | 44 | public final class SavepointLoader { |
33 | 45 | private SavepointLoader() {} |
@@ -55,4 +67,70 @@ public static CheckpointMetadata loadSavepointMetadata(String savepointPath) |
55 | 67 | stream, Thread.currentThread().getContextClassLoader(), savepointPath); |
56 | 68 | } |
57 | 69 | } |
| 70 | + |
| 71 | + /** |
| 72 | + * Loads all state metadata for an operator in a single I/O operation. |
| 73 | + * |
| 74 | + * @param savepointPath Path to the savepoint directory |
| 75 | + * @param operatorIdentifier Operator UID or hash |
| 76 | + * @return Map from state name to StateMetaInfoSnapshot |
| 77 | + * @throws IOException If reading fails |
| 78 | + */ |
| 79 | + public static Map<String, StateMetaInfoSnapshot> loadOperatorStateMetadata( |
| 80 | + String savepointPath, OperatorIdentifier operatorIdentifier) throws IOException { |
| 81 | + |
| 82 | + CheckpointMetadata checkpointMetadata = loadSavepointMetadata(savepointPath); |
| 83 | + |
| 84 | + OperatorState operatorState = |
| 85 | + checkpointMetadata.getOperatorStates().stream() |
| 86 | + .filter( |
| 87 | + state -> |
| 88 | + operatorIdentifier |
| 89 | + .getOperatorId() |
| 90 | + .equals(state.getOperatorID())) |
| 91 | + .findFirst() |
| 92 | + .orElseThrow( |
| 93 | + () -> |
| 94 | + new IllegalArgumentException( |
| 95 | + "Operator " |
| 96 | + + operatorIdentifier |
| 97 | + + " not found in savepoint")); |
| 98 | + |
| 99 | + KeyedStateHandle keyedStateHandle = |
| 100 | + operatorState.getStates().stream() |
| 101 | + .flatMap(s -> s.getManagedKeyedState().stream()) |
| 102 | + .findFirst() |
| 103 | + .orElseThrow( |
| 104 | + () -> |
| 105 | + new IllegalArgumentException( |
| 106 | + "No keyed state found for operator " |
| 107 | + + operatorIdentifier)); |
| 108 | + |
| 109 | + KeyedBackendSerializationProxy<?> proxy = readSerializationProxy(keyedStateHandle); |
| 110 | + return proxy.getStateMetaInfoSnapshots().stream() |
| 111 | + .collect(Collectors.toMap(StateMetaInfoSnapshot::getName, Function.identity())); |
| 112 | + } |
| 113 | + |
| 114 | + private static KeyedBackendSerializationProxy<?> readSerializationProxy( |
| 115 | + KeyedStateHandle stateHandle) throws IOException { |
| 116 | + |
| 117 | + StreamStateHandle streamStateHandle; |
| 118 | + if (stateHandle instanceof KeyGroupsStateHandle) { |
| 119 | + streamStateHandle = ((KeyGroupsStateHandle) stateHandle).getDelegateStateHandle(); |
| 120 | + } else { |
| 121 | + throw new IllegalArgumentException( |
| 122 | + "Unsupported KeyedStateHandle type: " + stateHandle.getClass()); |
| 123 | + } |
| 124 | + |
| 125 | + try (FSDataInputStream inputStream = streamStateHandle.openInputStream()) { |
| 126 | + DataInputViewStreamWrapper inputView = new DataInputViewStreamWrapper(inputStream); |
| 127 | + |
| 128 | + KeyedBackendSerializationProxy<?> proxy = |
| 129 | + new KeyedBackendSerializationProxy<>( |
| 130 | + Thread.currentThread().getContextClassLoader()); |
| 131 | + proxy.read(inputView); |
| 132 | + |
| 133 | + return proxy; |
| 134 | + } |
| 135 | + } |
58 | 136 | } |
0 commit comments