Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -110,6 +111,7 @@ private static Stream<Arguments> testArgsForUpgradeDowngradeCommand() {
}).map(Arguments::of);
}

@Disabled
@ParameterizedTest
@MethodSource("testArgsForUpgradeDowngradeCommand")
public void testUpgradeDowngradeCommand(HoodieTableVersion fromVersion, HoodieTableVersion toVersion) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,10 @@ public HoodieTableVersion getWriteVersion() {
return HoodieTableVersion.fromVersionCode(getIntOrDefault(WRITE_TABLE_VERSION));
}

public void setWriteVersion(HoodieTableVersion version) {
setValue(WRITE_TABLE_VERSION, String.valueOf(version.versionCode()));
}

public boolean autoUpgrade() {
return getBoolean(AUTO_UPGRADE_VERSION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.hudi.common.engine.HoodieEngineContext;
import org.apache.hudi.common.model.HoodieIndexMetadata;
import org.apache.hudi.common.table.HoodieTableMetaClient;
import org.apache.hudi.common.table.HoodieTableVersion;
import org.apache.hudi.common.util.Option;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.metadata.HoodieIndexVersion;
Expand All @@ -40,11 +39,6 @@ public Map<ConfigProperty, String> upgrade(HoodieWriteConfig config,
SupportsUpgradeDowngrade upgradeDowngradeHelper) {
final HoodieTable table = upgradeDowngradeHelper.getTable(config, context);

// If auto upgrade is disabled, set writer version to 8 and return
if (!config.autoUpgrade()) {
config.setValue(HoodieWriteConfig.WRITE_TABLE_VERSION, String.valueOf(HoodieTableVersion.EIGHT.versionCode()));
return Collections.emptyMap();
}
HoodieTableMetaClient metaClient = table.getMetaClient();

// Populate missing index versions indexes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ public Map<ConfigProperty, String> upgrade(HoodieWriteConfig config, HoodieEngin
HoodieTable table = upgradeDowngradeHelper.getTable(config, context);
HoodieTableMetaClient metaClient = table.getMetaClient();
HoodieTableConfig tableConfig = metaClient.getTableConfig();
// If auto upgrade is disabled, set writer version to 6 and return
if (!config.autoUpgrade()) {
config.setValue(HoodieWriteConfig.WRITE_TABLE_VERSION, String.valueOf(HoodieTableVersion.SIX.versionCode()));
return tablePropsToAdd;
}


// If metadata is enabled for the data table, and existing metadata table is behind the data table, then delete it
if (!table.isMetadataTable() && config.isMetadataTableEnabled() && isMetadataTableBehindDataTable(config, metaClient)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,44 @@ public UpgradeDowngrade(

public static boolean needsUpgradeOrDowngrade(HoodieTableMetaClient metaClient, HoodieWriteConfig config, HoodieTableVersion toWriteVersion) {
HoodieTableVersion fromTableVersion = metaClient.getTableConfig().getTableVersion();
return needsUpgrade(metaClient, config, toWriteVersion) || toWriteVersion.versionCode() < fromTableVersion.versionCode();
return needsUpgrade(metaClient, config, toWriteVersion) || needsDowngrade(fromTableVersion, toWriteVersion);
}

public static boolean needsDowngrade(HoodieTableVersion fromTableVersion, HoodieTableVersion toWriteVersion) {
if (toWriteVersion.lesserThan(HoodieTableVersion.SIX)) {
// for 1.1 we will do not allow downgrades to below SIX
// user will have to downgrade the table using a prior hudi version.
throw new HoodieUpgradeDowngradeException(
String.format("1.1.0 only supports table version greater then version SIX or above."
+ " Please downgrade table from version %s to %s using a hudi version prior to 1.1.0", fromTableVersion, toWriteVersion));
}
return toWriteVersion.lesserThan(fromTableVersion);
}

public static boolean needsUpgrade(HoodieTableMetaClient metaClient, HoodieWriteConfig config, HoodieTableVersion toWriteVersion) {
HoodieTableVersion fromTableVersion = metaClient.getTableConfig().getTableVersion();
// If table version is less than SIX, then we need to upgrade to SIX first before upgrading to any other version, irrespective of autoUpgrade flag
if (fromTableVersion.versionCode() < HoodieTableVersion.SIX.versionCode() && toWriteVersion.versionCode() >= HoodieTableVersion.EIGHT.versionCode()) {
if (fromTableVersion.greaterThanOrEquals(toWriteVersion)) {
LOG.warn("Table version {} is greater than or equal to write version {}. No upgrade needed", fromTableVersion, toWriteVersion);
return false;
}
if (fromTableVersion.lesserThan(HoodieTableVersion.SIX)) {
// for 1.1 we require table version be SIX at the minimum before upgrading.
// user will have to upgrade the table to SIX by using a prior hudi version.
throw new HoodieUpgradeDowngradeException(
String.format("Please upgrade table from version %s to %s before upgrading to version %s.", fromTableVersion, HoodieTableVersion.SIX.versionCode(), toWriteVersion));
String.format("1.1.0 only supports table version greater then version SIX or above."
+ " Please upgrade table from version %s to %s using a hudi version prior to 1.1.0", fromTableVersion, HoodieTableVersion.SIX));
}

// allow upgrades otherwise.
return toWriteVersion.versionCode() > fromTableVersion.versionCode();
if (!config.autoUpgrade()) {
// if autoUpgrade is disabled and table version is greater than SIX, then we must ensure the write version is set to the table version.
// and skip the upgrade
config.setWriteVersion(fromTableVersion);
LOG.warn("hoodie.write.auto.upgrade was disabled. Table version {} does not match write version {}. "
+ "Setting hoodie.write.table.version={} to match hoodie.table.version, and skipping upgrade",
fromTableVersion.versionCode(), toWriteVersion.versionCode(), fromTableVersion.versionCode());
return false;
}
// if we have passed all the checks, then this is valid to upgrade
return true;
}

public boolean needsUpgradeOrDowngrade(HoodieTableVersion toWriteVersion) {
Expand Down Expand Up @@ -149,12 +174,6 @@ public void run(HoodieTableVersion toVersion, String instantTime) {
HoodieTableVersion fromVersion = metaClient.getTableConfig().getTableVersion();
// Determine if we are upgrading or downgrading
boolean isUpgrade = fromVersion.versionCode() < toVersion.versionCode();
if (isUpgrade && !config.autoUpgrade()) {
// if we are attempting to upgrade and auto-upgrade is disabled
// we exit out the upgrade process
LOG.warn("AUTO_UPGRADE_VERSION was explicitly disabled, skipping table version upgrade process");
return;
}

if (!needsUpgradeOrDowngrade(toVersion)) {
return;
Expand Down Expand Up @@ -220,12 +239,8 @@ public void run(HoodieTableVersion toVersion, String instantTime) {
for (ConfigProperty configProperty : tablePropsToRemove) {
metaClient.getTableConfig().clearValue(configProperty);
}
// user could have disabled auto upgrade (probably to deploy the new binary only),
// in which case, we should not update the table version
if (config.autoUpgrade()) {
metaClient.getTableConfig().setTableVersion(toVersion);
}

metaClient.getTableConfig().setTableVersion(toVersion);
HoodieTableConfig.update(metaClient.getStorage(),
metaClient.getMetaPath(), metaClient.getTableConfig().getProps());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.hudi.keygen.KeyGenerator;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -50,6 +51,7 @@ void setUp() {
.build();
}

@Disabled
@ParameterizedTest
@ValueSource(strings = {"hoodie.table.keygenerator.class", "hoodie.datasource.write.keygenerator.class"})
void upgradeHandlerShouldRetrieveKeyGeneratorConfig(String keyGenConfigKey) {
Expand All @@ -59,6 +61,7 @@ void upgradeHandlerShouldRetrieveKeyGeneratorConfig(String keyGenConfigKey) {
assertEquals(KeyGenerator.class.getName(), kv.get(HoodieTableConfig.KEY_GENERATOR_CLASS_NAME));
}

@Disabled
@ParameterizedTest
@EnumSource(EngineType.class)
void upgradeHandlerWhenKeyGeneratorNotSet(EngineType engineType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.apache.spark.sql.Row;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -186,11 +187,13 @@ public void testWithoutAutoUpgrade() throws IOException {
assertEquals(HoodieTableVersion.SIX, metaClient.getTableConfig().getTableVersion());
}

@Disabled
@Test
public void testLeftOverUpdatedPropFileCleanup() throws IOException {
testUpgradeZeroToOneInternal(true, true, HoodieTableType.MERGE_ON_READ);
}

@Disabled
@ParameterizedTest(name = TEST_NAME_WITH_PARAMS)
@MethodSource("configParams")
public void testUpgradeZeroToOne(boolean deletePartialMarkerFiles, HoodieTableType tableType) throws IOException {
Expand Down Expand Up @@ -259,6 +262,7 @@ public void testUpgradeZeroToOneInternal(boolean induceResiduesFromPrevUpgrade,
}*/
}

@Disabled
@ParameterizedTest
@EnumSource(value = HoodieTableType.class)
public void testUpgradeOneToTwo(HoodieTableType tableType) throws IOException {
Expand Down Expand Up @@ -291,6 +295,7 @@ public void testUpgradeOneToTwo(HoodieTableType tableType) throws IOException {
assertTableProps(cfg);
}

@Disabled
@ParameterizedTest
@MethodSource("twoToThreeUpgradeConfigParams")
public void testUpgradeTwoToThree(
Expand Down Expand Up @@ -336,6 +341,7 @@ public void testUpgradeTwoToThree(
HoodieWriteConfig.KEYGENERATOR_CLASS_NAME.key(), SimpleKeyGenerator.class.getName()));
}

@Disabled
@Test
public void testUpgradeDowngradeBetweenThreeAndCurrentVersion() throws IOException {
// init config, table and client.
Expand Down Expand Up @@ -370,26 +376,31 @@ public void testUpgradeDowngradeBetweenThreeAndCurrentVersion() throws IOExcepti
assertEquals(checksum, metaClient.getTableConfig().getProps().getString(HoodieTableConfig.TABLE_CHECKSUM.key()));
}

@Disabled
@Test
public void testUpgradeFourtoFive() throws Exception {
testUpgradeFourToFiveInternal(false, false, false);
}

@Disabled
@Test
public void testUpgradeFourtoFiveWithDefaultPartition() throws Exception {
testUpgradeFourToFiveInternal(true, false, false);
}

@Disabled
@Test
public void testUpgradeFourtoFiveWithDefaultPartitionWithSkipValidation() throws Exception {
testUpgradeFourToFiveInternal(true, true, false);
}

@Disabled
@Test
public void testUpgradeFourtoFiveWithHiveStyleDefaultPartition() throws Exception {
testUpgradeFourToFiveInternal(true, false, true);
}

@Disabled
@Test
public void testUpgradeFourtoFiveWithHiveStyleDefaultPartitionWithSkipValidation() throws Exception {
testUpgradeFourToFiveInternal(true, true, true);
Expand Down Expand Up @@ -549,6 +560,7 @@ private void assertTableProps(HoodieWriteConfig cfg) {
assertEquals(tableConfig.getBaseFileFormat().name(), originalProps.getProperty(BASE_FILE_FORMAT.key()));
}

@Disabled
@Test
public void testDowngradeSixToFiveShouldDeleteRecordIndexPartition() throws Exception {
HoodieWriteConfig config = getConfigBuilder()
Expand Down Expand Up @@ -592,6 +604,7 @@ public void testDowngradeSixToFiveShouldDeleteRecordIndexPartition() throws Exce

}

@Disabled
@ParameterizedTest(name = TEST_NAME_WITH_DOWNGRADE_PARAMS)
@MethodSource("downGradeConfigParams")
public void testDowngrade(
Expand Down Expand Up @@ -680,6 +693,7 @@ void testNeedsUpgrade() {
when(metaClient.getTableConfig()).thenReturn(tableConfig);
HoodieWriteConfig writeConfig = mock(HoodieWriteConfig.class);
when(writeConfig.autoUpgrade()).thenReturn(true);
when(writeConfig.getWriteVersion()).thenReturn(HoodieTableVersion.EIGHT);

// assert no downgrade for table version 7 from table version 8
boolean shouldDowngrade = new UpgradeDowngrade(metaClient, writeConfig, context, null)
Expand Down Expand Up @@ -709,10 +723,11 @@ void testNeedsUpgrade() {
assertTrue(shouldUpgrade);

// assert upgrade for table version 8 from table version 6 with auto upgrade set to false
// should return false
when(writeConfig.autoUpgrade()).thenReturn(false);
shouldUpgrade = new UpgradeDowngrade(metaClient, writeConfig, context, null)
.needsUpgrade(HoodieTableVersion.EIGHT);
assertTrue(shouldUpgrade);
assertFalse(shouldUpgrade);
}

private void assertMarkerFilesForDowngrade(HoodieTable table, HoodieInstant commitInstant, boolean assertExists) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.hudi.common.util.collection.Triple;
import org.apache.hudi.exception.HoodieException;
import org.apache.hudi.exception.HoodieIOException;
import org.apache.hudi.exception.HoodieNotSupportedException;
import org.apache.hudi.exception.TableNotFoundException;
import org.apache.hudi.keygen.constant.KeyGeneratorType;
import org.apache.hudi.metadata.HoodieTableMetadata;
Expand Down Expand Up @@ -1473,10 +1474,7 @@ public Properties build() {
tableConfig.setValue(HoodieTableConfig.NAME, tableName);
tableConfig.setValue(HoodieTableConfig.TYPE, tableType.name());

if (null == tableVersion) {
tableVersion = HoodieTableVersion.current();
}

tableVersion = getTableVersionProperly();
tableConfig.setTableVersion(tableVersion);
tableConfig.setInitialVersion(tableVersion);

Expand Down Expand Up @@ -1591,6 +1589,20 @@ public Properties build() {
return tableConfig.getProps();
}

HoodieTableVersion getTableVersionProperly() {
if (tableVersion == null) {
return HoodieTableVersion.current();
} else if (tableVersion.greaterThanOrEquals(HoodieTableVersion.SIX)) {
return tableVersion;
} else {
String errorMessage = String.format(
"Creating a table in version \"%d\" is not allowed in current Hudi binary. "
+ "Please set \"hoodie.write.table.version\" config with a value >= 6",
tableVersion.versionCode());
throw new HoodieNotSupportedException(errorMessage);
}
}

public HoodieTableMetaClient initTable(StorageConfiguration<?> configuration, String basePath) throws IOException {
return initTable(configuration, new StoragePath(basePath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.hudi.common.testutils.HoodieTestUtils;
import org.apache.hudi.common.util.FileIOUtils;
import org.apache.hudi.common.util.Option;
import org.apache.hudi.exception.HoodieNotSupportedException;
import org.apache.hudi.metadata.HoodieIndexVersion;
import org.apache.hudi.metadata.MetadataPartitionType;
import org.apache.hudi.storage.StoragePath;
Expand Down Expand Up @@ -283,4 +284,71 @@ void testDeleteDefinition() throws IOException {
new String(FileIOUtils.readDataFromPath(metaClient.getStorage(), new StoragePath(metaClient.getIndexDefinitionPath())).get()));
assertTrue(indexMetadata.getIndexDefinitions().isEmpty());
}

@Test
void testGetTableVersionProperlyWithNullTableVersion() {
HoodieTableMetaClient.TableBuilder tableBuilder = HoodieTableMetaClient.newTableBuilder()
.setTableName("test_table")
.setTableType(HoodieTableType.COPY_ON_WRITE);

// tableVersion is null by default
HoodieTableVersion result = tableBuilder.getTableVersionProperly();
assertEquals(HoodieTableVersion.current(), result);
}

@Test
void testGetTableVersionProperlyWithCurrentVersion() {
HoodieTableMetaClient.TableBuilder tableBuilder = HoodieTableMetaClient.newTableBuilder()
.setTableName("test_table")
.setTableType(HoodieTableType.COPY_ON_WRITE)
.setTableVersion(HoodieTableVersion.current());
HoodieTableVersion result = tableBuilder.getTableVersionProperly();
assertEquals(HoodieTableVersion.current(), result);
}

@Test
void testGetTableVersionProperlyWithValidVersions() {
HoodieTableVersion[] lowerVersions = {
HoodieTableVersion.SIX,
HoodieTableVersion.SEVEN,
HoodieTableVersion.EIGHT,
HoodieTableVersion.NINE
};

for (HoodieTableVersion version : lowerVersions) {
HoodieTableMetaClient.TableBuilder tableBuilder = HoodieTableMetaClient.newTableBuilder()
.setTableName("test_table")
.setTableType(HoodieTableType.COPY_ON_WRITE)
.setTableVersion(version);

HoodieTableVersion result = tableBuilder.getTableVersionProperly();
assertEquals(version, result);
}
}

@Test
void testGetTableVersionProperlyWithInvalidValues() {
// Test with different lower versions and autoUpgrade=true
HoodieTableVersion[] lowerVersions = {
HoodieTableVersion.ZERO,
HoodieTableVersion.ONE,
HoodieTableVersion.TWO,
HoodieTableVersion.THREE,
HoodieTableVersion.FOUR,
HoodieTableVersion.FIVE
};

for (HoodieTableVersion version : lowerVersions) {
HoodieTableMetaClient.TableBuilder tableBuilder = HoodieTableMetaClient.newTableBuilder()
.setTableName("test_table")
.setTableType(HoodieTableType.COPY_ON_WRITE)
.setTableVersion(version);

HoodieNotSupportedException exception = assertThrows(
HoodieNotSupportedException.class,
() -> tableBuilder.getTableVersionProperly());
assertTrue(exception.getMessage().contains(
String.format("Creating a table in version \"%d\"", version.versionCode())));
}
}
}
Loading
Loading