You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/10/27 04:28:53 UTC

[GitHub] [flink-ml] guoweiM commented on a change in pull request #16: [FLINK-9][iteration] Support per-round iteration

guoweiM commented on a change in pull request #16:
URL: https://github.com/apache/flink-ml/pull/16#discussion_r736240831



##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/PerRoundSubGraphBuilder.java
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.iteration;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.iteration.compile.DraftExecutionEnvironment;
+import org.apache.flink.iteration.operator.OperatorWrapper;
+import org.apache.flink.iteration.operator.perround.PerRoundOperatorWrapper;
+import org.apache.flink.streaming.api.datastream.DataStream;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** Allows to add per-round subgraph inside the iteration body. */
+@Internal
+public class PerRoundSubGraphBuilder {

Review comment:
       This class is not used.

##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/datacache/nonkeyed/DataCacheWriter.java
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.iteration.datacache.nonkeyed;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.fs.FSDataOutputStream;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Supplier;
+
+/** Records the data received and replayed them on required. */
+public class DataCacheWriter<T> {
+
+    private final TypeSerializer<T> serializer;
+
+    private final FileSystem fileSystem;
+
+    private final Supplier<Path> pathGenerator;
+
+    private final List<Segment> finishSegments;
+
+    private Path currentPath;
+
+    private FSDataOutputStream outputStream;
+
+    private DataOutputView outputView;
+
+    private int currentSegmentCount;
+
+    public DataCacheWriter(
+            TypeSerializer<T> serializer, FileSystem fileSystem, Supplier<Path> pathGenerator)
+            throws IOException {
+        this.serializer = serializer;
+        this.fileSystem = fileSystem;
+        this.pathGenerator = pathGenerator;
+
+        this.finishSegments = new ArrayList<>();
+
+        startNewSegment();
+    }
+
+    public void addRecord(T record) throws IOException {
+        serializer.serialize(record, outputView);
+        currentSegmentCount += 1;
+    }
+
+    public List<Segment> finishAddingRecords() throws IOException {
+        finishCurrentSegment();
+        return finishSegments;
+    }
+
+    public List<Segment> getFinishSegments() {
+        return finishSegments;
+    }
+
+    @VisibleForTesting
+    void startNewSegment() throws IOException {
+        this.currentPath = pathGenerator.get();
+        this.outputStream = fileSystem.create(currentPath, FileSystem.WriteMode.NO_OVERWRITE);
+        this.outputView = new DataOutputViewStreamWrapper(outputStream);

Review comment:
       I am a little curious why there is not have a `SegmentWriter` just as `SegmentReader`. 
   We might could remove some the mutable member such as `currentPath` & `outputStream` etc.

##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/IterationFactory.java
##########
@@ -125,6 +138,42 @@ public static DataStreamList createIteration(
         return addOutputs(getActualDataStreams(iterationBodyResult.getOutputStreams(), draftEnv));
     }
 
+    private static DataStreamList addReplayer(
+            DataStream<?> firstHeadStream,
+            DataStreamList originalDataStreams,
+            DataStreamList dataStreamInputs,
+            Set<Integer> replayedDataStreamIndices) {
+
+        List<DataStream<?>> result = new ArrayList<>(dataStreamInputs.size());
+        for (int i = 0; i < dataStreamInputs.size(); ++i) {
+            if (!replayedDataStreamIndices.contains(i)) {
+                result.add(dataStreamInputs.get(i));
+                continue;
+            }
+
+            DataStream<?> replayedInput =
+                    ((SingleOutputStreamOperator<IterationRecord<?>>) firstHeadStream)
+                            .getSideOutput(HeadOperator.ALIGN_NOTIFY_OUTPUT_TAG)
+                            .map(x -> x, dataStreamInputs.get(i).getType())
+                            .setParallelism(firstHeadStream.getParallelism())
+                            .name("signal-change-typeinfo")
+                            .broadcast()
+                            .union(dataStreamInputs.get(i))
+                            .transform(
+                                    "Replayer-"
+                                            + originalDataStreams
+                                                    .get(i)
+                                                    .getTransformation()
+                                                    .getName(),
+                                    dataStreamInputs.get(i).getType(),
+                                    (OneInputStreamOperator) new ReplayOperator<>())

Review comment:
       Maybe we could use the `StreamOperatorFactory`. WDYT?

##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/config/IterationOptions.java
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.iteration.config;
+
+import org.apache.flink.configuration.ConfigOption;
+
+import static org.apache.flink.configuration.ConfigOptions.key;
+
+/** The options for the iteration. */
+public class IterationOptions {
+
+    public static final ConfigOption<String> DATA_CACHE_PATH =

Review comment:
       Maybe we could use `REPLAY_DATA_CACHE_PATH` if this is only used by replaying the data 

##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/Iterations.java
##########
@@ -89,7 +98,12 @@
     public static DataStreamList iterateUnboundedStreams(
             DataStreamList initVariableStreams, DataStreamList dataStreams, IterationBody body) {
         return IterationFactory.createIteration(
-                initVariableStreams, dataStreams, body, new AllRoundOperatorWrapper(), false);
+                initVariableStreams,

Review comment:
       I notice that the `IterationFactory` is almost the same as the `Iterations`. Would you like to give some explaination that why we introduce `IterationFactory`.  IMHO it is a littler duplicated, which might introduce some burden for understanding.
   
   So maybe we could remove the `IterationFactory` WDYT?

##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/operator/ReplayOperator.java
##########
@@ -0,0 +1,191 @@
+/*
+ * 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.iteration.operator;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.iteration.IterationRecord;
+import org.apache.flink.iteration.config.IterationOptions;
+import org.apache.flink.iteration.datacache.nonkeyed.DataCacheReader;
+import org.apache.flink.iteration.datacache.nonkeyed.DataCacheWriter;
+import org.apache.flink.iteration.progresstrack.OperatorEpochWatermarkTracker;
+import org.apache.flink.iteration.progresstrack.OperatorEpochWatermarkTrackerFactory;
+import org.apache.flink.iteration.progresstrack.OperatorEpochWatermarkTrackerListener;
+import org.apache.flink.iteration.typeinfo.IterationRecordSerializer;
+import org.apache.flink.runtime.state.StateInitializationContext;
+import org.apache.flink.streaming.api.graph.StreamConfig;
+import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
+import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.api.operators.Output;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.streaming.runtime.tasks.StreamTask;
+import org.apache.flink.util.ExceptionUtils;
+
+import java.io.IOException;
+import java.util.UUID;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/** Replays the data received in the round 0 in the following round. */
+public class ReplayOperator<T> extends AbstractStreamOperator<IterationRecord<T>>
+        implements OneInputStreamOperator<IterationRecord<T>, IterationRecord<T>>,
+                OperatorEpochWatermarkTrackerListener {
+
+    private OperatorEpochWatermarkTracker progressTracker;

Review comment:
       I think maybe we could introduce the `OperatorFactory` to make the following member `final`.

##########
File path: flink-ml-iteration/src/main/java/org/apache/flink/iteration/datacache/nonkeyed/DataCacheReader.java
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.iteration.datacache.nonkeyed;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.core.fs.FSDataInputStream;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+
+/** Reads the cached data from a list of paths. */
+public class DataCacheReader<T> implements Iterator<T> {
+
+    private final TypeSerializer<T> serializer;
+
+    private final FileSystem fileSystem;
+
+    private final List<Segment> segments;
+
+    @Nullable private SegmentReader currentSegmentReader;
+
+    public DataCacheReader(
+            TypeSerializer<T> serializer, FileSystem fileSystem, List<Segment> segments)
+            throws IOException {
+
+        for (Segment segment : segments) {
+            checkArgument(segment.getCount() > 0, "Do not support empty segment");
+        }
+
+        this.serializer = serializer;
+        this.fileSystem = fileSystem;
+        this.segments = segments;
+
+        if (segments.size() > 0) {
+            this.currentSegmentReader = new SegmentReader(0);
+        }
+    }
+
+    @Override
+    public boolean hasNext() {
+        return currentSegmentReader != null && currentSegmentReader.hasNext();
+    }
+
+    @Override
+    public T next() {
+        try {
+            T next = currentSegmentReader.next();
+
+            if (!currentSegmentReader.hasNext()) {
+                currentSegmentReader.close();
+                if (currentSegmentReader.index < segments.size() - 1) {
+                    currentSegmentReader = new SegmentReader(currentSegmentReader.index + 1);
+                } else {
+                    currentSegmentReader = null;
+                }
+            }
+
+            return next;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private class SegmentReader {
+
+        private final int index;
+
+        private final FSDataInputStream inputStream;
+
+        private final DataInputView inputView;
+
+        private int offset;
+
+        public SegmentReader(int index) throws IOException {
+            this.index = index;
+            inputStream = fileSystem.open(segments.get(index).getPath());
+            inputView = new DataInputViewStreamWrapper(inputStream);
+        }
+
+        public boolean hasNext() {
+            return offset < segments.get(index).getCount();

Review comment:
       I think the meaning of `offset` is actually the same as the `count` record in the file?
   It might be better to understand use the `count` instead of the `offset`. 
   




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org