rahil-c commented on code in PR #13669:
URL: https://github.com/apache/hudi/pull/13669#discussion_r2250042112


##########
hudi-spark-datasource/hudi-spark/src/test/java/org/apache/hudi/table/upgrade/TestUpgradeDowngradeFixtures.java:
##########
@@ -0,0 +1,633 @@
+/*
+ * 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.HoodieConfig;
+import org.apache.hudi.common.config.RecordMergeMode;
+import org.apache.hudi.common.model.HoodieIndexMetadata;
+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.HoodieTableVersion;
+import org.apache.hudi.common.table.timeline.versioning.TimelineLayoutVersion;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.metadata.HoodieTableMetadata;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.hudi.testutils.HoodieSparkClientTestHarness;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Properties;
+import java.util.stream.Stream;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Test class for upgrade/downgrade operations using pre-created fixture tables
+ * from different Hudi releases. Tests round-trip operations: upgrade one 
version up,
+ * then downgrade back to original version.
+ */
+public class TestUpgradeDowngradeFixtures extends HoodieSparkClientTestHarness 
{
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(TestUpgradeDowngradeFixtures.class);
+  private static final String FIXTURES_BASE_PATH = 
"/upgrade-downgrade-fixtures/mor-tables/";
+  
+  @TempDir
+  java.nio.file.Path tempDir;
+  
+  private HoodieTableMetaClient metaClient;
+
+  @BeforeEach
+  public void setUp() throws IOException {
+    initSparkContexts();
+  }
+
+  @AfterEach
+  public void tearDown() throws IOException {
+    cleanupSparkContexts();
+  }
+
+  @ParameterizedTest
+  @MethodSource("fixtureVersions")
+  public void testRoundTripUpgradeDowngrade(HoodieTableVersion 
originalVersion) throws Exception {
+    LOG.info("Testing round-trip upgrade/downgrade for version {}", 
originalVersion);
+    
+    // Load fixture table
+    HoodieTableMetaClient originalMetaClient = 
loadFixtureTable(originalVersion);
+    assertEquals(originalVersion, 
originalMetaClient.getTableConfig().getTableVersion(),
+        "Fixture table should be at expected version");
+    
+    // Calculate target version (next version up)
+    HoodieTableVersion targetVersion = getNextVersion(originalVersion);
+    if (targetVersion == null) {
+      LOG.info("Skipping upgrade test for version {} (no higher version 
available)", originalVersion);
+      return;
+    }
+    
+    // Create write config for upgrade operations
+    HoodieWriteConfig config = createWriteConfig(originalMetaClient, true);
+    
+    // Step 1: Upgrade to next version
+    LOG.info("Step 1: Upgrading from {} to {}", originalVersion, 
targetVersion);
+    new UpgradeDowngrade(originalMetaClient, config, context, 
SparkUpgradeDowngradeHelper.getInstance())
+        .run(targetVersion, null);
+    
+    // Create fresh meta client to read updated table configuration after 
upgrade
+    HoodieTableMetaClient upgradedMetaClient = HoodieTableMetaClient.builder()
+        .setConf(storageConf.newInstance())
+        .setBasePath(originalMetaClient.getBasePath())
+        .build();
+    assertTableVersionOnDataAndMetadataTable(upgradedMetaClient, 
targetVersion);
+    validateVersionSpecificProperties(upgradedMetaClient, originalVersion, 
targetVersion);
+    performDataValidationOnTable(upgradedMetaClient, "after upgrade");
+
+    // Step 2: Downgrade back to original version
+    LOG.info("Step 2: Downgrading from {} back to {}", targetVersion, 
originalVersion);
+    new UpgradeDowngrade(upgradedMetaClient, config, context, 
SparkUpgradeDowngradeHelper.getInstance())
+        .run(originalVersion, null);
+    
+    // Create fresh meta client to read updated table configuration after 
downgrade
+    HoodieTableMetaClient finalMetaClient = HoodieTableMetaClient.builder()
+        .setConf(storageConf.newInstance())
+        .setBasePath(upgradedMetaClient.getBasePath())
+        .build();
+    assertTableVersionOnDataAndMetadataTable(finalMetaClient, originalVersion);
+    validateVersionSpecificProperties(finalMetaClient, targetVersion, 
originalVersion);
+    performDataValidationOnTable(finalMetaClient, "after round-trip");
+
+    LOG.info("Successfully completed round-trip test for version {}", 
originalVersion);
+  }
+
+  @ParameterizedTest
+  @MethodSource("fixtureVersions")
+  public void testAutoUpgradeDisabled(HoodieTableVersion originalVersion) 
throws Exception {
+    LOG.info("Testing auto-upgrade disabled for version {}", originalVersion);
+    
+    // Load fixture table
+    HoodieTableMetaClient originalMetaClient = 
loadFixtureTable(originalVersion);
+    
+    // Calculate target version (next version up)
+    HoodieTableVersion targetVersion = getNextVersion(originalVersion);
+    if (targetVersion == null) {
+      LOG.info("Skipping auto-upgrade test for version {} (no higher version 
available)", originalVersion);
+      return;
+    }
+    
+    // Create write config with auto-upgrade disabled
+    HoodieWriteConfig config = createWriteConfig(originalMetaClient, false);
+    
+    // Attempt upgrade with auto-upgrade disabled
+    new UpgradeDowngrade(originalMetaClient, config, context, 
SparkUpgradeDowngradeHelper.getInstance())
+        .run(targetVersion, null);
+    
+    // Create fresh meta client to validate that version remained unchanged 
+    HoodieTableMetaClient unchangedMetaClient = HoodieTableMetaClient.builder()
+        .setConf(storageConf.newInstance())
+        .setBasePath(originalMetaClient.getBasePath())
+        .build();
+    assertEquals(originalVersion, 
unchangedMetaClient.getTableConfig().getTableVersion(),
+        "Table version should remain unchanged when auto-upgrade is disabled");
+    performDataValidationOnTable(unchangedMetaClient, "after auto-upgrade 
disabled test");
+    
+    LOG.info("Auto-upgrade disabled test passed for version {}", 
originalVersion);
+  }
+
+  @ParameterizedTest
+  @MethodSource("fixtureVersions") 
+  public void testRollbackAndCompactionBehavior(HoodieTableVersion 
originalVersion) throws Exception {
+    LOG.info("Testing rollback and compaction behavior for version {}", 
originalVersion);
+    
+    // Load fixture table
+    HoodieTableMetaClient originalMetaClient = 
loadFixtureTable(originalVersion);
+    
+    // Calculate target version
+    HoodieTableVersion targetVersion = getNextVersion(originalVersion);
+    if (targetVersion == null) {
+      LOG.info("Skipping rollback/compaction test for version {} (no higher 
version available)", originalVersion);
+      return;
+    }
+    
+    // Create write config for upgrade operations
+    HoodieWriteConfig config = createWriteConfig(originalMetaClient, true);
+    
+    // Count initial timeline state
+    int initialPendingCommits = 
originalMetaClient.getCommitsTimeline().filterPendingExcludingCompaction().countInstants();
+    int initialCompletedCommits = 
originalMetaClient.getCommitsTimeline().filterCompletedInstants().countInstants();
+    
+    // Perform upgrade
+    new UpgradeDowngrade(originalMetaClient, config, context, 
SparkUpgradeDowngradeHelper.getInstance())
+        .run(targetVersion, null);
+    
+    // Create fresh meta client to validate timeline state after upgrade
+    HoodieTableMetaClient upgradedMetaClient = HoodieTableMetaClient.builder()
+        .setConf(storageConf.newInstance())
+        .setBasePath(originalMetaClient.getBasePath())
+        .build();
+    
+    // Perform data validation after upgrade
+    performDataValidationOnTable(upgradedMetaClient, "after upgrade in 
rollback/compaction test");
+    
+    // Verify rollback behavior - pending commits should be cleaned up or 
reduced
+    int finalPendingCommits = 
upgradedMetaClient.getCommitsTimeline().filterPendingExcludingCompaction().countInstants();
+    assertTrue(finalPendingCommits <= initialPendingCommits,
+        "Pending commits should be cleaned up or reduced after upgrade");
+    
+    // Verify we still have completed commits
+    int finalCompletedCommits = 
upgradedMetaClient.getCommitsTimeline().filterCompletedInstants().countInstants();
+    assertTrue(finalCompletedCommits >= initialCompletedCommits,
+        "Completed commits should be preserved or increased after upgrade");
+    
+    LOG.info("Rollback and compaction behavior validated for version {}", 
originalVersion);
+  }
+
+  /**
+   * Load a fixture table from resources and copy it to a temporary location 
for testing.
+   */
+  private HoodieTableMetaClient loadFixtureTable(HoodieTableVersion version) 
throws IOException {
+    String fixtureName = getFixtureName(version);
+    String resourcePath = FIXTURES_BASE_PATH + fixtureName;
+    
+    // Extract fixture zip from resources to temp directory
+    extractFixtureToTempDir(resourcePath, tempDir.toString());
+    
+    // Get the table name from fixture (remove .zip extension)
+    String tableName = fixtureName.replace(".zip", "");
+    String tablePath = tempDir.resolve(tableName).toString();
+    
+    // Initialize meta client for the copied fixture
+    metaClient = HoodieTableMetaClient.builder()
+        .setConf(storageConf.newInstance())
+        .setBasePath(tablePath)
+        .build();
+    
+    LOG.info("Loaded fixture table {} at version {}", fixtureName, 
metaClient.getTableConfig().getTableVersion());
+    return metaClient;
+  }
+
+  /**
+   * Extract fixture zip file from resources to temporary directory.
+   */
+  private void extractFixtureToTempDir(String resourcePath, String tempPath) 
throws IOException {

Review Comment:
   Have added a util to be used between both classes.



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