hudi-agent commented on code in PR #19199:
URL: https://github.com/apache/hudi/pull/19199#discussion_r3533025256


##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/upgrade/UpgradeDowngrade.java:
##########
@@ -183,6 +184,8 @@ public void run(HoodieTableVersion toVersion, String 
instantTime) {
       return;
     }
 
+    validateDowngradePrerequisites(fromVersion, toVersion, isUpgrade);

Review Comment:
   🤖 The native CDC scan in `validateDowngradePrerequisites` runs here, before 
`performRollbackAndCompactionIfRequired` rolls back failed/inflight writes. If 
a native `.cdc.parquet` file is left over from an uncommitted write, wouldn't 
that block the downgrade even though the rollback on the next line would have 
cleaned it up? Might be worth running `validateNoNativeCdcLogs` after 
`rollbackFailedWrites` (still before compaction) so only committed CDC logs 
fail fast.
   
   <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/upgrade/TenToNineDowngradeHandler.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.engine.HoodieEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.HoodieUpgradeDowngradeException;
+import org.apache.hudi.storage.StoragePath;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Version 10 writes native log files by default. Downgrading to version 9 
requires
+ * full compaction of native data/delete logs before the downgrade completes. 
Native
+ * CDC log conversion is intentionally unsupported.
+ */
+public class TenToNineDowngradeHandler implements DowngradeHandler {
+  @Override
+  public UpgradeDowngrade.TableConfigChangeSet downgrade(
+      HoodieWriteConfig config,
+      HoodieEngineContext context,
+      String instantTime,
+      SupportsUpgradeDowngrade upgradeDowngradeHelper) {
+    return new UpgradeDowngrade.TableConfigChangeSet(
+        Collections.emptyMap(),
+        Collections.singleton(HoodieTableConfig.TABLE_STORAGE_LAYOUT));
+  }
+
+  static void validateNoNativeCdcLogs(HoodieTableMetaClient metaClient) {
+    findNativeCdcLogFile(metaClient).ifPresent(nativeCdcLogFile -> {
+      throw new HoodieUpgradeDowngradeException(String.format(
+          "Cannot downgrade table from version 10 to 9 because native CDC log 
file %s exists. "
+              + "Native CDC log downgrade is unsupported.",
+          nativeCdcLogFile));
+    });
+  }
+
+  static Option<StoragePath> findNativeCdcLogFile(HoodieTableMetaClient 
metaClient) {
+    AtomicReference<StoragePath> nativeCdcLogFile = new AtomicReference<>();
+    try {
+      FSUtils.processFiles(metaClient.getStorage(), 
metaClient.getBasePath().toString(), pathInfo -> {
+        if (pathInfo.isFile() && 
FSUtils.isNativeCDCLogFile(pathInfo.getPath().getName())) {
+          nativeCdcLogFile.set(pathInfo.getPath());
+          return false;
+        }
+        return true;
+      }, true);
+    } catch (IOException e) {
+      throw new HoodieUpgradeDowngradeException(
+          "Failed to scan table for native CDC log files before downgrading 
from version 10 to 9", e);
+    } catch (HoodieException e) {

Review Comment:
   🤖 nit: the `HoodieException` catch is swallowed silently when 
`nativeCdcLogFile.get() != null` — could you add a brief comment explaining 
that `FSUtils.processFiles` throws a `HoodieException` when the callback 
returns `false` (early-exit signal), so catching and discarding it here is 
intentional? Without that context, this reads like exception suppression.
   
   <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/upgrade/TenToNineDowngradeHandler.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.engine.HoodieEngineContext;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.table.HoodieTableConfig;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.HoodieUpgradeDowngradeException;
+import org.apache.hudi.storage.StoragePath;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Version 10 writes native log files by default. Downgrading to version 9 
requires
+ * full compaction of native data/delete logs before the downgrade completes. 
Native
+ * CDC log conversion is intentionally unsupported.
+ */
+public class TenToNineDowngradeHandler implements DowngradeHandler {
+  @Override
+  public UpgradeDowngrade.TableConfigChangeSet downgrade(
+      HoodieWriteConfig config,
+      HoodieEngineContext context,
+      String instantTime,
+      SupportsUpgradeDowngrade upgradeDowngradeHelper) {
+    return new UpgradeDowngrade.TableConfigChangeSet(
+        Collections.emptyMap(),
+        Collections.singleton(HoodieTableConfig.TABLE_STORAGE_LAYOUT));
+  }
+
+  static void validateNoNativeCdcLogs(HoodieTableMetaClient metaClient) {
+    findNativeCdcLogFile(metaClient).ifPresent(nativeCdcLogFile -> {
+      throw new HoodieUpgradeDowngradeException(String.format(
+          "Cannot downgrade table from version 10 to 9 because native CDC log 
file %s exists. "
+              + "Native CDC log downgrade is unsupported.",
+          nativeCdcLogFile));
+    });
+  }
+
+  static Option<StoragePath> findNativeCdcLogFile(HoodieTableMetaClient 
metaClient) {
+    AtomicReference<StoragePath> nativeCdcLogFile = new AtomicReference<>();
+    try {
+      FSUtils.processFiles(metaClient.getStorage(), 
metaClient.getBasePath().toString(), pathInfo -> {
+        if (pathInfo.isFile() && 
FSUtils.isNativeCDCLogFile(pathInfo.getPath().getName())) {
+          nativeCdcLogFile.set(pathInfo.getPath());
+          return false;
+        }
+        return true;
+      }, true);
+    } catch (IOException e) {
+      throw new HoodieUpgradeDowngradeException(
+          "Failed to scan table for native CDC log files before downgrading 
from version 10 to 9", e);
+    } catch (HoodieException e) {
+      if (nativeCdcLogFile.get() == null) {
+        throw e;
+      }
+    }
+    return nativeCdcLogFile.get() == null ? Option.empty() : 
Option.of(nativeCdcLogFile.get());

Review Comment:
   🤖 nit: `nativeCdcLogFile.get()` is called twice on the same line — 
extracting it to a local `StoragePath found = nativeCdcLogFile.get()` before 
the return would be slightly cleaner.
   
   <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/upgrade/UpgradeDowngrade.java:
##########
@@ -408,4 +413,26 @@ private void 
performRollbackAndCompactionIfRequired(HoodieTableVersion fromVersi
           tableVersion);
     }
   }
+
+  /**
+   * Checks if any handlers in the downgrade path require validation before 
rollback and compaction.
+   *
+   * @param fromVersion the current table version
+   * @param toVersion   the target table version
+   * @param isUpgrade   whether the current operation is an upgrade
+   */
+  private void validateDowngradePrerequisites(HoodieTableVersion fromVersion, 
HoodieTableVersion toVersion, boolean isUpgrade) {
+    if (isUpgrade) {
+      return;
+    }
+
+    HoodieTableVersion checkVersion = fromVersion;
+    while (checkVersion.versionCode() > toVersion.versionCode()) {
+      HoodieTableVersion prevVersion = 
HoodieTableVersion.fromVersionCode(checkVersion.versionCode() - 1);
+      if (checkVersion == HoodieTableVersion.TEN && prevVersion == 
HoodieTableVersion.NINE) {

Review Comment:
   🤖 nit: `prevVersion == HoodieTableVersion.NINE` is always true when 
`checkVersion == TEN` (since `prevVersion` is computed as `checkVersion - 1`), 
so the second clause is redundant and might make a reader wonder if there's an 
edge case where it could differ. `if (checkVersion == HoodieTableVersion.TEN)` 
alone would be clearer.
   
   <sub><i>⚠️ AI-generated; verify before applying. React 👍/👎 to flag 
quality.</i></sub>



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