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 2022/03/02 12:05:17 UTC

[GitHub] [flink-ml] zhipeng93 commented on a change in pull request #67: [FLINK-26383] Create model stream sink demo

zhipeng93 commented on a change in pull request #67:
URL: https://github.com/apache/flink-ml/pull/67#discussion_r817616144



##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))

Review comment:
       nits: How about we simplify the code as follows?
   - remove those meaningless name of columns? (e.g., `f0`, `f1`)?
   - move initialization of some variables to `before`?

##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(

Review comment:
       Can we remove the `valid data` here? It seems to be uncessary for testing load/save.

##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Before
+    public void before() {
+        Configuration config = new Configuration();
+        config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true);
+        env = StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+    }
+
+    @Test
+    public void saveAndLoadEverySingleModelAndValidate() throws Exception {

Review comment:
       Can you simplify this test, given that we want to test loading a single model?

##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Before
+    public void before() {
+        Configuration config = new Configuration();
+        config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true);
+        env = StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+    }
+
+    @Test
+    public void saveAndLoadEverySingleModelAndValidate() throws Exception {
+        String tmpPath = saveOnlineModel();
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {
+            LogisticRegressionModel lrModel = new LogisticRegressionModel().setFeaturesCol("vec");
+            if (!"metadata".equals(modelVersion)) {
+                System.out.println(modelVersion);

Review comment:
       nit: We do not print stuff in tests.

##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Before
+    public void before() {
+        Configuration config = new Configuration();
+        config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true);
+        env = StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+    }
+
+    @Test
+    public void saveAndLoadEverySingleModelAndValidate() throws Exception {
+        String tmpPath = saveOnlineModel();
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {
+            LogisticRegressionModel lrModel = new LogisticRegressionModel().setFeaturesCol("vec");
+            if (!"metadata".equals(modelVersion)) {
+                System.out.println(modelVersion);
+                Table modelData =
+                        tEnv.fromDataStream(
+                                        loadModelData(
+                                                env,
+                                                tmpPath,
+                                                new LogisticRegressionModelData.ModelDataDecoder(),
+                                                modelVersion))
+                                .as("label, vec");
+                lrModel.setModelData(modelData);
+                Table out = lrModel.transform(validDataTable)[0];
+                List<Row> result = IteratorUtils.toList(tEnv.toDataStream(out).executeAndCollect());
+                for (Row row : result) {
+                    Assert.assertEquals(row.getField(0), row.getField(2));
+                }
+            }
+        }
+    }
+
+    @Test
+    public void saveAndLoadAllOnlineModels() throws Exception {

Review comment:
       Is `testSaveAndLoadOnlineModel` a better function name?

##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Before
+    public void before() {
+        Configuration config = new Configuration();
+        config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true);
+        env = StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+    }
+
+    @Test
+    public void saveAndLoadEverySingleModelAndValidate() throws Exception {
+        String tmpPath = saveOnlineModel();
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {
+            LogisticRegressionModel lrModel = new LogisticRegressionModel().setFeaturesCol("vec");

Review comment:
       `featuresCol` should be loaded from `metadata`.

##########
File path: flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.ml.classification;
+
+import org.apache.flink.api.common.eventtime.WatermarkStrategy;
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.api.common.serialization.Encoder;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.sink.FileSink;
+import org.apache.flink.connector.file.src.FileSource;
+import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.ReadWriteUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.functions.sink.filesystem.bucketassigners.BasePathBucketAssigner;
+import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    StreamTableEnvironment tEnv;
+    StreamExecutionEnvironment env;
+    Schema modelSchema = Schema.newBuilder().column("f0", DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Before
+    public void before() {
+        Configuration config = new Configuration();
+        config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, true);
+        env = StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+    }
+
+    @Test
+    public void saveAndLoadEverySingleModelAndValidate() throws Exception {
+        String tmpPath = saveOnlineModel();
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {
+            LogisticRegressionModel lrModel = new LogisticRegressionModel().setFeaturesCol("vec");
+            if (!"metadata".equals(modelVersion)) {
+                System.out.println(modelVersion);
+                Table modelData =
+                        tEnv.fromDataStream(
+                                        loadModelData(
+                                                env,
+                                                tmpPath,
+                                                new LogisticRegressionModelData.ModelDataDecoder(),
+                                                modelVersion))
+                                .as("label, vec");
+                lrModel.setModelData(modelData);
+                Table out = lrModel.transform(validDataTable)[0];
+                List<Row> result = IteratorUtils.toList(tEnv.toDataStream(out).executeAndCollect());
+                for (Row row : result) {
+                    Assert.assertEquals(row.getField(0), row.getField(2));
+                }
+            }
+        }
+    }
+
+    @Test
+    public void saveAndLoadAllOnlineModels() throws Exception {
+        String tmpPath = saveOnlineModel();
+        Table modelData =
+                tEnv.fromDataStream(
+                                ReadWriteUtils.loadModelData(
+                                        env,
+                                        tmpPath,
+                                        new LogisticRegressionModelData.ModelDataDecoder()))
+                        .as("label, vec");
+
+        List<Row> result = IteratorUtils.toList(tEnv.toDataStream(modelData).executeAndCollect());
+        Assert.assertEquals(result.size(), 4);

Review comment:
       Can you further check the elements in each model data?




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