the-other-tim-brown commented on code in PR #8044:
URL: https://github.com/apache/hudi/pull/8044#discussion_r1117955249


##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/MORRestoreTool.java:
##########
@@ -0,0 +1,384 @@
+/*
+ * 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.hudi.utilities;
+
+import org.apache.hudi.client.common.HoodieSparkEngineContext;
+import org.apache.hudi.common.config.HoodieMetadataConfig;
+import org.apache.hudi.common.config.SerializableConfiguration;
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.engine.HoodieLocalEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.function.SerializableFunction;
+import org.apache.hudi.common.model.FileSlice;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.timeline.HoodieTimeline;
+import org.apache.hudi.common.table.view.FileSystemViewManager;
+import org.apache.hudi.common.table.view.HoodieTableFileSystemView;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.HoodieIOException;
+import org.apache.hudi.exception.HoodieMetadataException;
+
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.spark.SparkConf;
+import org.apache.spark.api.java.JavaSparkContext;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+
+import static 
org.apache.hudi.common.table.HoodieTableMetaClient.METADATA_TABLE_FOLDER_PATH;
+import static 
org.apache.hudi.metadata.HoodieTableMetadataUtil.deleteMetadataTable;
+import static 
org.apache.hudi.table.HoodieTable.clearMetadataTablePartitionsConfig;
+
+/**
+ * Sample command
+ * ./bin/spark-submit
+ * --driver-memory 5g
+ * --executor-memory 6g
+ * --num-executors 1
+ * --executor-cores 2
+ * --deploy-mode client
+ * --class org.apache.hudi.utilities.MORRestoreTool 
UTILITIES_BUNDLE/hudi-utilities-bundle_2.11-0.14.0-SNAPSHOT.jar
+ * --base-path /tmp/hudi_trips_more_restore/
+ * --commitTime 20230225091008404
+ * --spark-master local[2]
+ * --execute
+ * --cleanUpMetadata
+ */
+public class MORRestoreTool implements Serializable {
+
+  private static final Logger LOG = LogManager.getLogger(MORRestoreTool.class);
+
+  // Spark context
+  private transient JavaSparkContext jsc;
+  // config
+  private Config cfg;
+  // Properties with source, hoodie client, key generator etc.
+  private TypedProperties props;
+
+  private final HoodieTableMetaClient metaClient;
+
+  public MORRestoreTool(HoodieTableMetaClient metaClient) {
+    this.metaClient = metaClient;

Review Comment:
   This constructor isn't setting the other instance vars, can we set those to 
avoid NPEs? Are there some vars that only need to exist within the constructor?



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/HoodieTable.java:
##########
@@ -1021,7 +1021,7 @@ private boolean shouldExecuteMetadataTableDeletion() {
   /**
    * Clears hoodie.table.metadata.partitions in hoodie.properties
    */
-  private void 
clearMetadataTablePartitionsConfig(Option<MetadataPartitionType> partitionType, 
boolean clearAll) {
+  public static void 
clearMetadataTablePartitionsConfig(Option<MetadataPartitionType> partitionType, 
boolean clearAll, HoodieTableMetaClient metaClient) {

Review Comment:
   Let's avoid moving methods to static. This only makes the code harder to 
unit test. Also changing from private to public static seems like maybe we 
should move this functionality outside of the HoodieTable class, what do you 
think?



##########
hudi-utilities/src/test/java/org/apache/hudi/utilities/TestMorRestoreTool.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.hudi.utilities;
+
+import org.apache.hudi.client.SparkRDDWriteClient;
+import org.apache.hudi.client.WriteStatus;
+import org.apache.hudi.common.config.HoodieMetadataConfig;
+import org.apache.hudi.common.model.FileSlice;
+import org.apache.hudi.common.model.HoodieFileFormat;
+import org.apache.hudi.common.model.HoodieLogFile;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieTableType;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.timeline.HoodieActiveTimeline;
+import org.apache.hudi.common.table.timeline.HoodieTimeline;
+import org.apache.hudi.common.testutils.HoodieTestDataGenerator;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.config.HoodieCompactionConfig;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.table.HoodieSparkTable;
+import org.apache.hudi.table.HoodieTable;
+import org.apache.hudi.testutils.SparkClientFunctionalTestHarness;
+import org.apache.hudi.testutils.providers.SparkProvider;
+
+import org.apache.spark.api.java.JavaRDD;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.testutils.Assertions.assertNoWriteErrors;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestMorRestoreTool extends SparkClientFunctionalTestHarness 
implements SparkProvider {
+
+  private static final HoodieTestDataGenerator DATA_GENERATOR = new 
HoodieTestDataGenerator(0L);
+  private HoodieTableMetaClient metaClient;
+
+  @BeforeEach
+  public void init() throws IOException {
+    this.metaClient = getHoodieMetaClient(HoodieTableType.MERGE_ON_READ);
+  }
+
+  @AfterAll
+  public static void cleanup() {
+    DATA_GENERATOR.close();
+  }
+
+  @Test

Review Comment:
   Add a sanity check test that the "dry run" is taken into account



##########
hudi-utilities/src/test/java/org/apache/hudi/utilities/TestMorRestoreTool.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.hudi.utilities;
+
+import org.apache.hudi.client.SparkRDDWriteClient;
+import org.apache.hudi.client.WriteStatus;
+import org.apache.hudi.common.config.HoodieMetadataConfig;
+import org.apache.hudi.common.model.FileSlice;
+import org.apache.hudi.common.model.HoodieFileFormat;
+import org.apache.hudi.common.model.HoodieLogFile;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.model.HoodieTableType;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.timeline.HoodieActiveTimeline;
+import org.apache.hudi.common.table.timeline.HoodieTimeline;
+import org.apache.hudi.common.testutils.HoodieTestDataGenerator;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.config.HoodieCompactionConfig;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.table.HoodieSparkTable;
+import org.apache.hudi.table.HoodieTable;
+import org.apache.hudi.testutils.SparkClientFunctionalTestHarness;
+import org.apache.hudi.testutils.providers.SparkProvider;
+
+import org.apache.spark.api.java.JavaRDD;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.testutils.Assertions.assertNoWriteErrors;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestMorRestoreTool extends SparkClientFunctionalTestHarness 
implements SparkProvider {
+
+  private static final HoodieTestDataGenerator DATA_GENERATOR = new 
HoodieTestDataGenerator(0L);
+  private HoodieTableMetaClient metaClient;
+
+  @BeforeEach
+  public void init() throws IOException {
+    this.metaClient = getHoodieMetaClient(HoodieTableType.MERGE_ON_READ);
+  }
+
+  @AfterAll
+  public static void cleanup() {
+    DATA_GENERATOR.close();
+  }
+
+  @Test
+  public void testSimpleRestore() throws Exception {
+    HoodieFileFormat fileFormat = HoodieFileFormat.PARQUET;
+
+    Properties properties = new Properties();
+    properties.setProperty(HoodieTableConfig.BASE_FILE_FORMAT.key(), 
fileFormat.toString());
+    HoodieTableMetaClient metaClient = 
getHoodieMetaClient(HoodieTableType.MERGE_ON_READ, properties);
+
+    HoodieWriteConfig.Builder cfgBuilder = getConfigBuilder(true)
+        
.withMetadataConfig(HoodieMetadataConfig.newBuilder().enable(false).build())
+        
.withCompactionConfig(HoodieCompactionConfig.newBuilder().withMaxNumDeltaCommitsBeforeCompaction(2).build());
+    HoodieWriteConfig cfg = cfgBuilder.build();
+    try (SparkRDDWriteClient client = getHoodieWriteClient(cfg)) {
+      /*
+       * Write 1 (only inserts)
+       */
+      String newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+
+      List<HoodieRecord> records = 
DATA_GENERATOR.generateInserts(newCommitTime, 200);
+      upsertToMorTable(client, records, newCommitTime);
+
+      /*
+       * Write 2 (updates)
+       */
+      newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+      records = DATA_GENERATOR.generateUpdates(newCommitTime, 100);
+      upsertToMorTable(client, records, newCommitTime);
+
+      newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+      records = DATA_GENERATOR.generateUpdates(newCommitTime, 50);
+      upsertToMorTable(client, records, newCommitTime);
+      String toRestoreCommit = newCommitTime;
+
+      // collect file slices to validate later
+      HoodieTable table = HoodieSparkTable.create(cfg, context(), metaClient);
+      table.getHoodieView().sync();
+      Map<String, List<HoodieLogFile>> beforeRestoreFileSlice = new 
HashMap<>();
+      for (String partitionPath : DATA_GENERATOR.getPartitionPaths()) {
+        List<FileSlice> groupedLogFiles =
+            
table.getSliceView().getLatestFileSlices(partitionPath).collect(Collectors.toList());
+        beforeRestoreFileSlice.put(partitionPath, new ArrayList<>());
+        for (FileSlice fileSlice : groupedLogFiles) {
+          
beforeRestoreFileSlice.get(partitionPath).addAll(fileSlice.getLogFiles().collect(Collectors.toList()));
+        }
+      }
+
+      String firstCompactionInstant = 
client.scheduleCompaction(Option.empty()).get().toString();
+      client.compact(firstCompactionInstant);
+
+      // verify that there is a commit
+      metaClient = HoodieTableMetaClient.reload(metaClient);
+      HoodieTimeline timeline = 
metaClient.getCommitTimeline().filterCompletedInstants();
+      assertEquals(1, timeline.findInstantsAfter("000", 
Integer.MAX_VALUE).countInstants(),
+          "Expecting a single commit.");
+      String latestCompactionCommitTime = 
timeline.lastInstant().get().getTimestamp();
+      assertTrue(HoodieTimeline.compareTimestamps("000", 
HoodieTimeline.LESSER_THAN, latestCompactionCommitTime));
+      /*
+       * Write 1 (only inserts)
+       */
+      newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+      records = DATA_GENERATOR.generateUpdates(newCommitTime, 100);
+      upsertToMorTable(client, records, newCommitTime);
+
+      newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+      records = DATA_GENERATOR.generateUpdates(newCommitTime, 50);
+      upsertToMorTable(client, records, newCommitTime);
+
+      String secondCompactionInstant = 
client.scheduleCompaction(Option.empty()).get().toString();
+      client.compact(secondCompactionInstant);
+
+      // verify that there is a commit
+      metaClient = HoodieTableMetaClient.reload(metaClient);
+      timeline = metaClient.getCommitTimeline().filterCompletedInstants();
+      assertEquals(2, timeline.findInstantsAfter("000", 
Integer.MAX_VALUE).countInstants(),
+          "Expecting two commits.");
+
+      newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+      records = DATA_GENERATOR.generateUpdates(newCommitTime, 100);
+      upsertToMorTable(client, records, newCommitTime);
+
+      newCommitTime = HoodieActiveTimeline.createNewInstantTime();
+      client.startCommitWithTime(newCommitTime);
+      records = DATA_GENERATOR.generateUpdates(newCommitTime, 50);
+      upsertToMorTable(client, records, newCommitTime);
+
+      // trigger restore
+      MORRestoreTool.Config restoreToolConfig = new MORRestoreTool.Config();
+      restoreToolConfig.basePath = cfg.getBasePath();
+      restoreToolConfig.commitTime = toRestoreCommit;
+      restoreToolConfig.execute = true;
+      MORRestoreTool restoreTool = new MORRestoreTool(jsc(), 
restoreToolConfig);
+      restoreTool.run();
+
+      metaClient = HoodieTableMetaClient.reload(metaClient);
+      table = HoodieSparkTable.create(cfg, context(), metaClient);
+      table.getHoodieView().sync();
+      Map<String, List<HoodieLogFile>> afterRestoreFileSlice = new HashMap<>();
+      for (String partitionPath : DATA_GENERATOR.getPartitionPaths()) {
+        List<FileSlice> groupedLogFiles =
+            
table.getSliceView().getLatestFileSlices(partitionPath).collect(Collectors.toList());
+        afterRestoreFileSlice.put(partitionPath, new ArrayList<>());
+        for (FileSlice fileSlice : groupedLogFiles) {
+          
afterRestoreFileSlice.get(partitionPath).addAll(fileSlice.getLogFiles().collect(Collectors.toList()));
+        }
+        // validate
+        List<HoodieLogFile> beforeLogFiles = 
beforeRestoreFileSlice.get(partitionPath);
+        List<HoodieLogFile> afterLogFiles = 
afterRestoreFileSlice.get(partitionPath);
+        for (HoodieLogFile beforeLogFile : beforeLogFiles) {
+          assertTrue(afterLogFiles.contains(beforeLogFile));
+        }
+        for (HoodieLogFile afterLogFile : afterLogFiles) {
+          assertTrue(beforeLogFiles.contains(afterLogFile));
+        }
+      }
+    }
+  }

Review Comment:
   Can you add some assertions that the table can still be read and that the 
records retrieved match expectations?



-- 
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: commits-unsubscr...@hudi.apache.org

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

Reply via email to