rkhachatryan commented on a change in pull request #13351:
URL: https://github.com/apache/flink/pull/13351#discussion_r494503948



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/channel/SequentialChannelStateReaderImpl.java
##########
@@ -0,0 +1,263 @@
+/*
+ * 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.runtime.checkpoint.channel;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.core.fs.FSDataInputStream;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.checkpoint.StateObjectCollection;
+import org.apache.flink.runtime.checkpoint.TaskStateSnapshot;
+import org.apache.flink.runtime.io.network.api.writer.ResultPartitionWriter;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferBuilder;
+import org.apache.flink.runtime.io.network.buffer.BufferConsumer;
+import org.apache.flink.runtime.io.network.partition.consumer.InputGate;
+import 
org.apache.flink.runtime.io.network.partition.consumer.RecoveredInputChannel;
+import org.apache.flink.runtime.state.AbstractChannelStateHandle;
+import org.apache.flink.runtime.state.StreamStateHandle;
+import org.apache.flink.util.Preconditions;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import static java.util.Comparator.comparingLong;
+import static java.util.stream.Collectors.groupingBy;
+import static java.util.stream.Collectors.toList;
+import static 
org.apache.flink.runtime.checkpoint.channel.ChannelStateByteBuffer.wrap;
+
+/**
+ * {@link SequentialChannelStateReader} implementation.
+ */
+public class SequentialChannelStateReaderImpl implements 
SequentialChannelStateReader {
+
+       private final TaskStateSnapshot taskStateSnapshot;
+       private final ChannelStateSerializer serializer;
+       private final ChannelStateChunkReader chunkReader;
+
+       public SequentialChannelStateReaderImpl(TaskStateSnapshot 
taskStateSnapshot) {
+               this(taskStateSnapshot, new ChannelStateSerializerImpl());
+       }
+
+       public SequentialChannelStateReaderImpl(TaskStateSnapshot 
taskStateSnapshot, ChannelStateSerializer serializer) {
+               this.taskStateSnapshot = taskStateSnapshot;
+               this.serializer = serializer;
+               this.chunkReader = new ChannelStateChunkReader(serializer);
+       }
+
+       public SequentialChannelStateReaderImpl(TaskStateSnapshot 
taskStateSnapshot, ChannelStateSerializer serializer, ChannelStateChunkReader 
chunkReader) {
+               this.taskStateSnapshot = taskStateSnapshot;
+               this.serializer = serializer;
+               this.chunkReader = chunkReader;
+       }
+
+       @Override
+       public boolean hasChannelStates() {
+               return 
taskStateSnapshot.getSubtaskStateMappings().stream().anyMatch(subtaskStateEntry 
->
+                       
subtaskStateEntry.getValue().getInputChannelState().stream().anyMatch(h -> 
!h.getOffsets().isEmpty()) ||
+                               
subtaskStateEntry.getValue().getResultSubpartitionState().stream().anyMatch(h 
-> !h.getOffsets().isEmpty()));
+       }
+
+       @Override
+       public void readInputData(InputGate[] inputGates) throws IOException {
+               try (InputChannelRecoveredStateHandler stateHandler = new 
InputChannelRecoveredStateHandler(inputGates)) {
+                       read(OperatorSubtaskState::getInputChannelState, 
stateHandler);
+               }
+       }
+
+       @Override
+       public void readOutputData(ResultPartitionWriter[] writers) throws 
IOException {
+               try (ResultSubpartitionRecoveredStateHandler stateHandler = new 
ResultSubpartitionRecoveredStateHandler(writers)) {
+                       read(OperatorSubtaskState::getResultSubpartitionState, 
stateHandler);
+               }
+       }
+
+       private <Info, Context, Handle extends 
AbstractChannelStateHandle<Info>> void read(
+                       Function<OperatorSubtaskState, 
StateObjectCollection<Handle>> stateHandleExtractor,
+                       RecoveredChannelStateHandler<Info, Context> 
stateHandler) throws IOException {
+               for (Map.Entry<StreamStateHandle, List<Handle>> 
delegateAndHandles : groupByDelegate(streamSubtaskStates(), 
stateHandleExtractor).entrySet()) {

Review comment:
       > What is the relation between delegate and handles
   
   1:M, that is one underlying delegate can be referenced by 1 or more handles.
   
   > we only have one state file per subtask 
   
   Yes, but on downscaling we can have more. 
   Does this answer your question?
   




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


Reply via email to