danny0405 commented on code in PR #19539: URL: https://github.com/apache/hudi/pull/19539#discussion_r3733254015
########## hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/replication/TestHiveSyncGlobalCommitToolUnit.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.hive.replication; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.OutputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class TestHiveSyncGlobalCommitToolUnit { + + @TempDir + Path tempDir; + + @Test + void commitDetectsDivergentReplicationStateAndClosesBothSides() throws Exception { + ReplicationStateSync local = mock(ReplicationStateSync.class); + ReplicationStateSync remote = mock(ReplicationStateSync.class); + when(local.getClusterId()).thenReturn("LOCALSYNC"); + when(remote.getClusterId()).thenReturn("REMOTESYNC"); + when(local.replicationStateIsInSync(remote)).thenReturn(false); + + HiveSyncGlobalCommitTool tool = mock(HiveSyncGlobalCommitTool.class, CALLS_REAL_METHODS); + setField(tool, "replicationStateSyncList", Arrays.asList(local, remote)); + + assertFalse(tool.commit()); + tool.close(); + + verify(local).sync(); + verify(remote).sync(); + verify(local).close(); + verify(remote).close(); + } + + @Test + void loadParamsReadsXmlConfiguration() throws Exception { + Path configFile = tempDir.resolve("global-sync.xml"); + Properties properties = new Properties(); + properties.setProperty("marker", "loaded"); + try (OutputStream output = Files.newOutputStream(configFile)) { + properties.storeToXML(output, "test"); + } + + Method loadParams = HiveSyncGlobalCommitTool.class.getDeclaredMethod("loadParams", String[].class); + loadParams.setAccessible(true); + HiveSyncGlobalCommitParams params = (HiveSyncGlobalCommitParams) loadParams.invoke(null, (Object) new String[] { + "--config-xml-file", configFile.toString(), "--replicated-timestamp", "100", + "--base-path", tempDir.resolve("table").toString() + }); + + assertEquals("loaded", params.loadedProps.getProperty("marker")); + assertEquals("100", params.globalHiveSyncConfigParams.globallyReplicatedTimeStamp); + } Review Comment: Updated in fbe3543e7034: the reflection helper now accepts Object, matching the other new test helpers, and the now-unused List import was removed. The focused Hive run passes 5 tests with 0 failures and 0 Checkstyle violations. ########## hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHoodieHiveSyncClientOperations.java: ########## @@ -0,0 +1,211 @@ +/* + * 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.hive; + +import org.apache.hudi.common.util.Option; +import org.apache.hudi.hive.ddl.DDLExecutor; +import org.apache.hudi.hive.ddl.JDBCBasedMetadataOperator; +import org.apache.hudi.sync.common.model.FieldSchema; +import org.apache.hudi.sync.common.model.Partition; + +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.SerDeInfo; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.thrift.TApplicationException; +import org.apache.thrift.TException; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.apache.hudi.hadoop.utils.HoodieHiveUtils.GLOBALLY_CONSISTENT_READ_TIMESTAMP; +import static org.apache.hudi.sync.common.HoodieMetaSyncOperations.HOODIE_LAST_COMMIT_COMPLETION_TIME_SYNC; +import static org.apache.hudi.sync.common.HoodieMetaSyncOperations.HOODIE_LAST_COMMIT_TIME_SYNC; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_BASE_FILE_FORMAT; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_BASE_PATH; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class TestHoodieHiveSyncClientOperations { + + @Test + void thriftMismatchActivatesJdbcMetadataFallback() throws Exception { + TestFixture fixture = newFixture(); + TApplicationException incompatible = new TApplicationException("unknown get_table method"); + when(fixture.metaStoreClient.tableExists("test_db", "table")) + .thenThrow(new TException(incompatible)); + when(fixture.jdbcMetadataOperator.tableExists("table")).thenReturn(true); + when(fixture.jdbcMetadataOperator.databaseExists("test_db")).thenReturn(true); + when(fixture.jdbcMetadataOperator.getTableProperty("table", HOODIE_LAST_COMMIT_TIME_SYNC)) + .thenReturn(Option.of("100")); + when(fixture.jdbcMetadataOperator.getTableProperty("table", HOODIE_LAST_COMMIT_COMPLETION_TIME_SYNC)) + .thenReturn(Option.of("101")); + when(fixture.jdbcMetadataOperator.getTableProperty("table", GLOBALLY_CONSISTENT_READ_TIMESTAMP)) + .thenReturn(Option.of("099")); + when(fixture.jdbcMetadataOperator.getTableLocation("table")).thenReturn("/warehouse/table"); + List<FieldSchema> fields = Collections.singletonList(new FieldSchema("id", "string", Option.empty())); + when(fixture.jdbcMetadataOperator.getFieldSchemas("table")).thenReturn(fields); + List<Partition> partitions = Collections.singletonList( + new Partition(Collections.singletonList("2026-08-06"), "/base/datestr=2026-08-06")); + when(fixture.jdbcMetadataOperator.getAllPartitions("table", "/base")).thenReturn(partitions); + + assertTrue(fixture.syncClient.tableExists("table")); + assertTrue(fixture.syncClient.updateTableProperties("table", Collections.singletonMap("owner", "hudi"))); + assertTrue(fixture.syncClient.updateSerdeProperties("table", + new HashMap<>(Collections.singletonMap("compression", "none")), false)); + assertEquals(partitions, fixture.syncClient.getAllPartitions("table")); + assertEquals(partitions, + fixture.syncClient.getPartitionsFromList("table", Collections.singletonList("datestr=2026-08-06"))); + assertTrue(fixture.syncClient.databaseExists("test_db")); + assertEquals("100", fixture.syncClient.getLastCommitTimeSynced("table").get()); + assertEquals("101", fixture.syncClient.getLastCommitCompletionTimeSynced("table").get()); + assertEquals("099", fixture.syncClient.getLastReplicatedTime("table").get()); + assertEquals(fields, fixture.syncClient.getMetastoreFieldSchemas("table")); + assertEquals("/warehouse/table", fixture.syncClient.getTableLocation("table")); + + fixture.syncClient.deleteLastReplicatedTimeStamp("table"); + fixture.syncClient.createOrReplaceTable("table", null, "input", "output", "serde", Review Comment: Updated in fbe3543e7034: both replacement tests now capture the generated temporary table name and use Mockito InOrder across the DDL and JDBC/metastore mocks to verify createTable → dropTable → renameTable/alter_table with the same temporary name. The focused Hive run passes 5 tests with 0 failures and 0 Checkstyle violations. -- 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]
