tzulitai commented on a change in pull request #8618:  
[FLINK-12732][state-processor-api] Add savepoint reader for consuming 
partitioned operator state
URL: https://github.com/apache/flink/pull/8618#discussion_r296548546
 
 

 ##########
 File path: 
flink-libraries/flink-state-processing-api/src/main/java/org/apache/flink/state/api/input/KeyedStateInputFormat.java
 ##########
 @@ -0,0 +1,253 @@
+/*
+ * 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.input;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.functions.util.FunctionUtils;
+import org.apache.flink.api.common.state.StateDescriptor;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.CloseableRegistry;
+import org.apache.flink.runtime.checkpoint.OperatorState;
+import org.apache.flink.runtime.checkpoint.StateAssignmentOperation;
+import org.apache.flink.runtime.execution.Environment;
+import org.apache.flink.runtime.state.AbstractKeyedStateBackend;
+import org.apache.flink.runtime.state.DefaultKeyedStateStore;
+import org.apache.flink.runtime.state.KeyGroupRange;
+import org.apache.flink.runtime.state.KeyedStateHandle;
+import org.apache.flink.runtime.state.StateBackend;
+import org.apache.flink.state.api.functions.KeyedStateReaderFunction;
+import org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit;
+import org.apache.flink.state.api.runtime.NeverFireProcessingTimeService;
+import org.apache.flink.state.api.runtime.SavepointEnvironment;
+import org.apache.flink.state.api.runtime.SavepointRuntimeContext;
+import org.apache.flink.streaming.api.operators.KeyContext;
+import org.apache.flink.streaming.api.operators.StreamOperatorStateContext;
+import org.apache.flink.streaming.api.operators.StreamTaskStateInitializer;
+import org.apache.flink.streaming.api.operators.StreamTaskStateInitializerImpl;
+import org.apache.flink.util.CollectionUtil;
+import org.apache.flink.util.Preconditions;
+
+import javax.annotation.Nonnull;
+
+import java.io.IOException;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Input format for reading partitioned state.
+ *
+ * @param <K> The type of the key.
+ * @param <OUT> The type of the output of the {@link KeyedStateReaderFunction}.
+ */
+@Internal
+public class KeyedStateInputFormat<K, OUT> extends SavepointInputFormat<OUT, 
KeyGroupRangeInputSplit> implements KeyContext {
+
+       private static final String USER_TIMERS_NAME = "user-timers";
+
+       private final StateBackend stateBackend;
+
+       private final TypeInformation<K> keyType;
+
+       private final KeyedStateReaderFunction<K, OUT> userFunction;
+
+       private transient TypeSerializer<K> keySerializer;
+
+       private transient CloseableRegistry registry;
+
+       private transient BufferingCollector<OUT> out;
+
+       private transient Iterator<K> keys;
+
+       private transient AbstractKeyedStateBackend<K> keyedStateBackend;
+
+       private transient Context ctx;
+
+       /**
+        * Creates an input format for reading partitioned state from an 
operator in a savepoint.
+        *
+        * @param savepointPath The path to an existing savepoint.
+        * @param uid           The uid of an operator.
+        * @param stateBackend  The state backed used to snapshot the operator.
+        * @param keyType       The type information describing the key type.
+        * @param userFunction  The {@link KeyedStateReaderFunction} called for 
each key in the operator.
+        */
+       public KeyedStateInputFormat(
+               String savepointPath,
+               String uid,
+               StateBackend stateBackend,
+               TypeInformation<K> keyType,
+               KeyedStateReaderFunction<K, OUT> userFunction) {
+               super(savepointPath, uid);
+               this.stateBackend = stateBackend;
+               this.keyType = keyType;
+               this.userFunction = userFunction;
+       }
+
+       @Override
+       public KeyGroupRangeInputSplit[] createInputSplits(int minNumSplits) 
throws IOException {
+               final OperatorState operatorState = getOperatorState();
+
+               return getKeyGroupRangeInputSplits(minNumSplits, operatorState);
+       }
+
+       @Override
+       public void openInputFormat() {
+               out = new BufferingCollector<>();
+               keySerializer = 
keyType.createSerializer(getRuntimeContext().getExecutionConfig());
+       }
+
+       @Override
+       @SuppressWarnings("unchecked")
+       public void open(KeyGroupRangeInputSplit split) throws IOException {
+               registry = new CloseableRegistry();
+
+               final Environment environment = new SavepointEnvironment
+                       .Builder(getRuntimeContext(), split.getNumKeyGroups())
+                       .setSubtaskIndex(split.getSplitNumber())
+                       
.setPrioritizedOperatorSubtaskState(split.getPrioritizedOperatorSubtaskState())
+                       .build();
+
+               final StreamOperatorStateContext context = 
getStreamOperatorStateContext(environment);
+
+               keyedStateBackend = (AbstractKeyedStateBackend<K>) 
context.keyedStateBackend();
+               keys = getKeyIterator();
+               ctx = new Context();
+       }
+
+       @SuppressWarnings("unchecked")
+       private Iterator<K> getKeyIterator() throws IOException {
+               Preconditions.checkNotNull(keyedStateBackend);
+               final DefaultKeyedStateStore keyedStateStore = new 
DefaultKeyedStateStore(keyedStateBackend, 
getRuntimeContext().getExecutionConfig());
+
+               final List<StateDescriptor<?, ?>> stateDescriptors;
+               try (SavepointRuntimeContext ctx = new 
SavepointRuntimeContext(getRuntimeContext(), keyedStateStore)) {
 
 Review comment:
   Please see my comment concerning whether `SavepointRuntimeContext` should 
really be an `AutoClosable`.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to