yihua commented on code in PR #11889:
URL: https://github.com/apache/hudi/pull/11889#discussion_r1757269798


##########
hudi-client/hudi-client-common/src/test/java/org/apache/hudi/table/upgrade/TestEightToSevenDowngradeHandler.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.table.upgrade;
+
+import org.apache.hudi.common.config.ConfigProperty;
+import org.apache.hudi.common.engine.HoodieEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.metadata.HoodieTableMetadataUtil;
+import org.apache.hudi.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.hudi.storage.hadoop.HoodieHadoopStorage;
+
+import org.apache.hadoop.conf.Configuration;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.hudi.common.table.HoodieTableConfig.TABLE_METADATA_PARTITIONS;
+import static org.apache.hudi.metadata.MetadataPartitionType.FUNCTIONAL_INDEX;
+import static org.apache.hudi.metadata.MetadataPartitionType.PARTITION_STATS;
+import static org.apache.hudi.metadata.MetadataPartitionType.SECONDARY_INDEX;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class TestEightToSevenDowngradeHandler {
+  private static final List<String> SAMPLE_METADATA_PATHS = Arrays.asList(
+      FUNCTIONAL_INDEX.getPartitionPath(),
+      SECONDARY_INDEX.getPartitionPath(),
+      PARTITION_STATS.getPartitionPath(),
+      "metadata_path1",
+      "metadata_path2");
+
+  @Mock
+  HoodieTableMetaClient mdtMetaClient;
+  @Mock
+  HoodieEngineContext context;
+
+  @Test
+  void testDeleteMetadataPartition() {
+    try (MockedStatic<HoodieTableMetadataUtil> mockedMetadataUtils = 
mockStatic(HoodieTableMetadataUtil.class)) {
+      List<String> leftPartitionPaths =
+          EightToSevenDowngradeHandler.deleteMetadataPartition(context, 
mdtMetaClient, SAMPLE_METADATA_PATHS);
+
+      mockedMetadataUtils.verify(
+          () -> HoodieTableMetadataUtil.deleteMetadataTablePartition(
+              mdtMetaClient, context, FUNCTIONAL_INDEX.getPartitionPath(), 
true),
+          times(1));
+      mockedMetadataUtils.verify(
+          () -> HoodieTableMetadataUtil.deleteMetadataTablePartition(
+              mdtMetaClient, context, SECONDARY_INDEX.getPartitionPath(), 
true),
+          times(1));
+      mockedMetadataUtils.verify(
+          () -> HoodieTableMetadataUtil.deleteMetadataTablePartition(
+              mdtMetaClient, context, PARTITION_STATS.getPartitionPath(), 
true),
+          times(1));
+
+      assertArrayEquals(new String[]{"metadata_path1", "metadata_path2"}, 
leftPartitionPaths.toArray());
+    }
+  }
+
+  @Test
+  void testDowngradeMetadataPartitions() {
+    HoodieStorage hoodieStorage = new HoodieHadoopStorage("any_path", new 
Configuration(false));

Review Comment:
   Use `HoodieStorageUtils.getStorage(getDefaultStorageConf())`



##########
hudi-client/hudi-client-common/src/test/java/org/apache/hudi/table/upgrade/TestEightToSevenDowngradeHandler.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.table.upgrade;
+
+import org.apache.hudi.common.config.ConfigProperty;
+import org.apache.hudi.common.engine.HoodieEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.metadata.HoodieTableMetadataUtil;
+import org.apache.hudi.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.hudi.storage.hadoop.HoodieHadoopStorage;
+
+import org.apache.hadoop.conf.Configuration;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.hudi.common.table.HoodieTableConfig.TABLE_METADATA_PARTITIONS;
+import static org.apache.hudi.metadata.MetadataPartitionType.FUNCTIONAL_INDEX;
+import static org.apache.hudi.metadata.MetadataPartitionType.PARTITION_STATS;
+import static org.apache.hudi.metadata.MetadataPartitionType.SECONDARY_INDEX;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class TestEightToSevenDowngradeHandler {
+  private static final List<String> SAMPLE_METADATA_PATHS = Arrays.asList(
+      FUNCTIONAL_INDEX.getPartitionPath(),
+      SECONDARY_INDEX.getPartitionPath(),

Review Comment:
   This does not represent the actual partition paths.  Use 
`MetadataPartitionType#getPartitionPath(HoodieTableMetaClient metaClient, 
String indexName)` to generate the partition paths.



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/upgrade/EightToSevenDowngradeHandler.java:
##########
@@ -92,4 +115,52 @@ private static void 
downgradePartitionFields(HoodieWriteConfig config, HoodieEng
       tablePropsToAdd.put(HoodieTableConfig.PARTITION_FIELDS, 
tableConfig.getPartitionFieldProp());
     }
   }
+
+  static void downgradeMetadataPartitions(HoodieEngineContext context,
+                                          HoodieStorage hoodieStorage,
+                                          HoodieTableMetaClient mdtMetaClient,
+                                          Map<ConfigProperty, String> 
tablePropsToAdd) {
+    // Fetch metadata partition paths.
+    List<String> metadataPartitions = FSUtils.getAllPartitionPaths(
+        context,
+        hoodieStorage,
+        mdtMetaClient.getBasePath(),
+        true);
+
+    // Delete partitions.
+    List<String> validPartitionPaths = deleteMetadataPartition(context, 
mdtMetaClient, metadataPartitions);
+
+    // Clean the configuration.
+    tablePropsToAdd.put(TABLE_METADATA_PARTITIONS, String.join(",", 
validPartitionPaths));
+  }
+
+  static List<String> deleteMetadataPartition(HoodieEngineContext context,
+                                              HoodieTableMetaClient 
mdtMetaClient,
+                                              List<String> metadataPartitions) 
{
+    List<String> validPartitionPaths = new ArrayList<>();
+
+    for (String partitionPath : metadataPartitions) {
+      if (partitionPath.equals(FUNCTIONAL_INDEX.getPartitionPath())
+          || partitionPath.equals(SECONDARY_INDEX.getPartitionPath())
+          || partitionPath.equals(PARTITION_STATS.getPartitionPath())) {
+        HoodieTableMetadataUtil.deleteMetadataTablePartition(
+            mdtMetaClient, context, partitionPath, true);
+      } else {
+        validPartitionPaths.add(partitionPath);
+      }
+    }
+    return validPartitionPaths;
+  }
+
+  private static HoodieTableMetaClient 
createMetadataTableMetaClient(HoodieEngineContext context,
+                                                                     
StoragePath basePath) {
+    return HoodieTableMetaClient.builder()
+        .setConf(HadoopFSUtils.getStorageConfWithCopy(
+            (Configuration) context.getStorageConf().unwrap()))
+        .setBasePath(
+            new Path(basePath.toString(),
+                HoodieTableMetaClient.METADATA_TABLE_FOLDER_PATH).toString())
+        .setLoadActiveTimelineOnLoad(true)

Review Comment:
   Is timeline load required?



##########
hudi-client/hudi-client-common/src/test/java/org/apache/hudi/table/upgrade/TestEightToSevenDowngradeHandler.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.table.upgrade;
+
+import org.apache.hudi.common.config.ConfigProperty;
+import org.apache.hudi.common.engine.HoodieEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.metadata.HoodieTableMetadataUtil;
+import org.apache.hudi.storage.HoodieStorage;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.hudi.storage.hadoop.HoodieHadoopStorage;
+
+import org.apache.hadoop.conf.Configuration;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.hudi.common.table.HoodieTableConfig.TABLE_METADATA_PARTITIONS;
+import static org.apache.hudi.metadata.MetadataPartitionType.FUNCTIONAL_INDEX;
+import static org.apache.hudi.metadata.MetadataPartitionType.PARTITION_STATS;
+import static org.apache.hudi.metadata.MetadataPartitionType.SECONDARY_INDEX;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class TestEightToSevenDowngradeHandler {
+  private static final List<String> SAMPLE_METADATA_PATHS = Arrays.asList(
+      FUNCTIONAL_INDEX.getPartitionPath(),
+      SECONDARY_INDEX.getPartitionPath(),
+      PARTITION_STATS.getPartitionPath(),
+      "metadata_path1",
+      "metadata_path2");
+
+  @Mock
+  HoodieTableMetaClient mdtMetaClient;
+  @Mock
+  HoodieEngineContext context;
+
+  @Test
+  void testDeleteMetadataPartition() {
+    try (MockedStatic<HoodieTableMetadataUtil> mockedMetadataUtils = 
mockStatic(HoodieTableMetadataUtil.class)) {
+      List<String> leftPartitionPaths =
+          EightToSevenDowngradeHandler.deleteMetadataPartition(context, 
mdtMetaClient, SAMPLE_METADATA_PATHS);
+
+      mockedMetadataUtils.verify(
+          () -> HoodieTableMetadataUtil.deleteMetadataTablePartition(
+              mdtMetaClient, context, FUNCTIONAL_INDEX.getPartitionPath(), 
true),
+          times(1));
+      mockedMetadataUtils.verify(
+          () -> HoodieTableMetadataUtil.deleteMetadataTablePartition(
+              mdtMetaClient, context, SECONDARY_INDEX.getPartitionPath(), 
true),
+          times(1));
+      mockedMetadataUtils.verify(
+          () -> HoodieTableMetadataUtil.deleteMetadataTablePartition(
+              mdtMetaClient, context, PARTITION_STATS.getPartitionPath(), 
true),
+          times(1));
+
+      assertArrayEquals(new String[]{"metadata_path1", "metadata_path2"}, 
leftPartitionPaths.toArray());
+    }
+  }
+
+  @Test
+  void testDowngradeMetadataPartitions() {
+    HoodieStorage hoodieStorage = new HoodieHadoopStorage("any_path", new 
Configuration(false));
+    StoragePath basePath = new 
StoragePath("file:///base_path/.hoodie/metadata/.hoodie");

Review Comment:
   Use the path to create `HoodieStorage` instance as well.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to