hudi-agent commented on code in PR #19199: URL: https://github.com/apache/hudi/pull/19199#discussion_r3528668519
########## 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) { Review Comment: 🤖 nit: could you rename `validateNoNativeCdcLogs` → `validateNoNativeCDCLogs` (and `findNativeCdcLogFile` → `findNativeCDCLogFile` on line 61)? The adjacent call to `FSUtils.isNativeCDCLogFile` uses all-caps `CDC`, and so does the existing `isCDCLogFile` — mixing `Cdc` here creates a small but real inconsistency that'll show up in searches and diffs. <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: it might be worth adding a brief comment here explaining why the `HoodieException` is intentionally swallowed — e.g. `// processFiles throws HoodieException when the callback returns false for early exit; if we already found a CDC log, that's the expected path and the exception can be ignored`. Without it, future readers will almost certainly assume this is an accidental swallow. <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` because `prevVersion` is computed as `checkVersion.versionCode() - 1` on the line above. Could you simplify to just `if (checkVersion == HoodieTableVersion.TEN)` to avoid the misleading impression that there's a meaningful independent check here? <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]
