wombatu-kun commented on code in PR #19407: URL: https://github.com/apache/hudi/pull/19407#discussion_r3687720204
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestHiveSyncProcedure.scala: ########## @@ -0,0 +1,98 @@ +/* + * 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.spark.sql.hudi.procedure + +import org.apache.hudi.common.testutils.HoodieTestUtils +import org.apache.hudi.hive.{HiveSyncConfig, HiveSyncConfigHolder} +import org.apache.hudi.sync.common.HoodieSyncConfig + +import org.apache.hadoop.hive.conf.HiveConf +import org.junit.jupiter.api.Assertions.{assertNotNull, assertTrue} + +class TestHiveSyncProcedure extends HoodieSparkProcedureTestBase { + + // Session-conf keys the procedure writes from its optional arguments; reset after the call so + // they do not leak into sibling tests sharing the session. + private val sessionSyncConfKeys = Seq( + HiveSyncConfig.HIVE_USER.key, HiveSyncConfig.HIVE_PASS.key, HiveSyncConfig.HIVE_USE_JDBC.key, + HiveSyncConfigHolder.HIVE_SYNC_MODE.key, HoodieSyncConfig.META_SYNC_PARTITION_FIELDS.key, + HoodieSyncConfig.META_SYNC_PARTITION_EXTRACTOR_CLASS.key, + HiveSyncConfigHolder.HIVE_SYNC_TABLE_STRATEGY.key, HoodieSyncConfig.META_SYNC_INCREMENTAL.key) + + private def createHudiTable(tableName: String, basePath: String): Unit = { + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | options ( + | primaryKey = 'id', + | type = 'cow', + | preCombineField = 'ts' + | ) + | location '$basePath' + """.stripMargin) + spark.sql(s"insert into $tableName values(1, 'a1', 10.0, 1000)") + } + + test("Test call hive_sync procedure surfaces a sync failure as HoodieException") { + // The procedure constructs a HiveConf; Hive 2.3.7 is compiled with Java 1.8 and its class + // loader throws when the Hive APIs are exercised on Java 17, so the test is skipped there. + if (HoodieTestUtils.getJavaVersion < 17) { + withTempDir { tmp => + val tableName = generateTableName + val basePath = s"${tmp.getCanonicalPath}/$tableName" + createHudiTable(tableName, basePath) + + try { + // Every optional argument is supplied so each argument branch of the procedure runs. The + // unresolvable partition_extractor_class makes the HiveSyncTool constructor throw while + // loading the extractor (in the HoodieSyncClient constructor, before any metastore + // connection is attempted). + var thrown: Throwable = null + try { + spark.sql( + s"call hive_sync(table => '$tableName', metastore_uri => 'thrift://localhost:9083'," + + s" username => 'hive', password => 'hive', use_jdbc => 'false', mode => 'hms'," + + s" partition_fields => 'name', strategy => 'ALL', sync_incremental => 'false'," + + s" partition_extractor_class => 'org.apache.hudi.hive.MissingPartitionExtractor')") + } catch { + case e: Throwable => thrown = e + } + assertNotNull(thrown, "hive_sync should fail when the partition extractor cannot be loaded") + + // The procedure must wrap the failure as HoodieException("hive sync failed"), and the + // cause must be the unresolvable extractor supplied as an argument. Asserting both pins + // the wrapping contract and that the argument is applied and fails before the metastore. + val chain = Iterator.iterate(thrown)(_.getCause).takeWhile(_ != null) + .flatMap(t => Option(t.getMessage)).mkString(" | ") + assertTrue(chain.contains("hive sync failed"), + s"expected the procedure to wrap the failure as HoodieException, but got: $chain") + assertTrue(chain.contains("org.apache.hudi.hive.MissingPartitionExtractor"), + s"expected the failure to originate from loading the supplied extractor, but got: $chain") Review Comment: Only `partition_extractor_class` is actually observed here - deleting any of the other eight `setConfString` calls in `HiveSyncProcedure` leaves this test green. Assert the values the procedure wrote (`HIVE_USER`, `HIVE_PASS`, `HIVE_USE_JDBC`, `HIVE_SYNC_MODE`, partition fields, strategy, incremental) on the session conf and `METASTOREURIS` on the hadoop conf before the finally block clears them. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestHiveSyncProcedure.scala: ########## @@ -0,0 +1,98 @@ +/* + * 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.spark.sql.hudi.procedure + +import org.apache.hudi.common.testutils.HoodieTestUtils +import org.apache.hudi.hive.{HiveSyncConfig, HiveSyncConfigHolder} +import org.apache.hudi.sync.common.HoodieSyncConfig + +import org.apache.hadoop.hive.conf.HiveConf +import org.junit.jupiter.api.Assertions.{assertNotNull, assertTrue} + +class TestHiveSyncProcedure extends HoodieSparkProcedureTestBase { + + // Session-conf keys the procedure writes from its optional arguments; reset after the call so + // they do not leak into sibling tests sharing the session. + private val sessionSyncConfKeys = Seq( + HiveSyncConfig.HIVE_USER.key, HiveSyncConfig.HIVE_PASS.key, HiveSyncConfig.HIVE_USE_JDBC.key, + HiveSyncConfigHolder.HIVE_SYNC_MODE.key, HoodieSyncConfig.META_SYNC_PARTITION_FIELDS.key, + HoodieSyncConfig.META_SYNC_PARTITION_EXTRACTOR_CLASS.key, + HiveSyncConfigHolder.HIVE_SYNC_TABLE_STRATEGY.key, HoodieSyncConfig.META_SYNC_INCREMENTAL.key) + + private def createHudiTable(tableName: String, basePath: String): Unit = { + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | options ( + | primaryKey = 'id', + | type = 'cow', + | preCombineField = 'ts' + | ) + | location '$basePath' + """.stripMargin) + spark.sql(s"insert into $tableName values(1, 'a1', 10.0, 1000)") + } + + test("Test call hive_sync procedure surfaces a sync failure as HoodieException") { + // The procedure constructs a HiveConf; Hive 2.3.7 is compiled with Java 1.8 and its class + // loader throws when the Hive APIs are exercised on Java 17, so the test is skipped there. + if (HoodieTestUtils.getJavaVersion < 17) { Review Comment: Behind this `if` the test reports as passed rather than skipped on Java 17, and the java17 scala-other-tests CI leg does run this package. Switch to ScalaTest's `assume(HoodieTestUtils.getJavaVersion < 17)` so the run is cancelled and shows up as skipped. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestHiveSyncProcedure.scala: ########## @@ -0,0 +1,98 @@ +/* + * 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.spark.sql.hudi.procedure + +import org.apache.hudi.common.testutils.HoodieTestUtils +import org.apache.hudi.hive.{HiveSyncConfig, HiveSyncConfigHolder} +import org.apache.hudi.sync.common.HoodieSyncConfig + +import org.apache.hadoop.hive.conf.HiveConf +import org.junit.jupiter.api.Assertions.{assertNotNull, assertTrue} + +class TestHiveSyncProcedure extends HoodieSparkProcedureTestBase { + + // Session-conf keys the procedure writes from its optional arguments; reset after the call so + // they do not leak into sibling tests sharing the session. + private val sessionSyncConfKeys = Seq( + HiveSyncConfig.HIVE_USER.key, HiveSyncConfig.HIVE_PASS.key, HiveSyncConfig.HIVE_USE_JDBC.key, + HiveSyncConfigHolder.HIVE_SYNC_MODE.key, HoodieSyncConfig.META_SYNC_PARTITION_FIELDS.key, + HoodieSyncConfig.META_SYNC_PARTITION_EXTRACTOR_CLASS.key, + HiveSyncConfigHolder.HIVE_SYNC_TABLE_STRATEGY.key, HoodieSyncConfig.META_SYNC_INCREMENTAL.key) + + private def createHudiTable(tableName: String, basePath: String): Unit = { + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | options ( + | primaryKey = 'id', + | type = 'cow', + | preCombineField = 'ts' + | ) + | location '$basePath' + """.stripMargin) + spark.sql(s"insert into $tableName values(1, 'a1', 10.0, 1000)") + } + + test("Test call hive_sync procedure surfaces a sync failure as HoodieException") { + // The procedure constructs a HiveConf; Hive 2.3.7 is compiled with Java 1.8 and its class + // loader throws when the Hive APIs are exercised on Java 17, so the test is skipped there. + if (HoodieTestUtils.getJavaVersion < 17) { + withTempDir { tmp => + val tableName = generateTableName + val basePath = s"${tmp.getCanonicalPath}/$tableName" + createHudiTable(tableName, basePath) + + try { + // Every optional argument is supplied so each argument branch of the procedure runs. The + // unresolvable partition_extractor_class makes the HiveSyncTool constructor throw while + // loading the extractor (in the HoodieSyncClient constructor, before any metastore + // connection is attempted). + var thrown: Throwable = null + try { + spark.sql( + s"call hive_sync(table => '$tableName', metastore_uri => 'thrift://localhost:9083'," + + s" username => 'hive', password => 'hive', use_jdbc => 'false', mode => 'hms'," + + s" partition_fields => 'name', strategy => 'ALL', sync_incremental => 'false'," + + s" partition_extractor_class => 'org.apache.hudi.hive.MissingPartitionExtractor')") + } catch { + case e: Throwable => thrown = e + } + assertNotNull(thrown, "hive_sync should fail when the partition extractor cannot be loaded") + + // The procedure must wrap the failure as HoodieException("hive sync failed"), and the + // cause must be the unresolvable extractor supplied as an argument. Asserting both pins + // the wrapping contract and that the argument is applied and fails before the metastore. + val chain = Iterator.iterate(thrown)(_.getCause).takeWhile(_ != null) + .flatMap(t => Option(t.getMessage)).mkString(" | ") + assertTrue(chain.contains("hive sync failed"), Review Comment: The exception type is never checked, so replacing the `HoodieException` in `HiveSyncProcedure` with any `RuntimeException` carrying the same message would still pass. Walk the cause chain for a `HoodieException` instance in addition to matching the message. -- 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]
