InvisibleProgrammer commented on code in PR #6487: URL: https://github.com/apache/hive/pull/6487#discussion_r3383483058
########## itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/txn/compactor/TestRebalanceCompactor.java: ########## @@ -0,0 +1,671 @@ +/* + * 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.hadoop.hive.ql.txn.compactor; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.api.GetTableRequest; +import org.apache.hadoop.hive.metastore.api.ShowCompactResponseElement; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.txn.TxnStore; +import org.apache.hadoop.hive.ql.Driver; +import org.apache.hadoop.hive.ql.io.AcidOutputFormat; +import org.apache.hadoop.hive.ql.io.AcidUtils; +import org.apache.hadoop.hive.ql.io.BucketCodec; +import org.apache.hadoop.hive.ql.processors.CommandProcessorException; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.apache.hadoop.hive.ql.ErrorMsg.TXN_ABORTED; +import static org.apache.hadoop.hive.ql.TxnCommandsBaseForTests.runWorker; +import static org.apache.hadoop.hive.ql.txn.compactor.TestCompactorBase.dropTables; +import static org.apache.hadoop.hive.ql.txn.compactor.TestCompactorBase.execSelectAndDumpData; +import static org.apache.hadoop.hive.ql.txn.compactor.TestCompactorBase.executeStatementOnDriver; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; + +public class TestRebalanceCompactor extends CompactorOnTezTest { + + @Test + public void testRebalanceCompactionWithParallelDeleteAsSecondOptimisticLock() throws Exception { + testRebalanceCompactionWithParallelDeleteAsSecond(true); + } + + @Test + public void testRebalanceCompactionWithParallelDeleteAsSecondPessimisticLock() throws Exception { + testRebalanceCompactionWithParallelDeleteAsSecond(false); + } + + @Test + public void testRebalanceCompactionOfNotPartitionedImplicitlyBucketedTableWithOrder() throws Exception { + conf.setBoolVar(HiveConf.ConfVars.COMPACTOR_CRUD_QUERY_BASED, true); + conf.setBoolVar(HiveConf.ConfVars.HIVE_COMPACTOR_GATHER_STATS, false); + conf.setBoolVar(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER, false); + + //set grouping size to have 3 buckets, and re-create driver with the new config + conf.set("tez.grouping.min-size", "400"); + conf.set("tez.grouping.max-size", "5000"); + driver = new Driver(conf); + + final String tableName = "rebalance_test"; + TestDataProvider testDataProvider = prepareRebalanceTestData(tableName); + + //Try to do a rebalancing compaction + executeStatementOnDriver("ALTER TABLE " + tableName + " COMPACT 'rebalance' ORDER BY b DESC", driver); + runWorker(conf); + + driver.close(); + driver = new Driver(conf); + + //Check if the compaction succeed + verifyCompaction(1, TxnStore.CLEANING_RESPONSE); + + // Populate expected data + Set<RowData> expectedData = new HashSet<>(); + + expectedData.add(new RowData("17", 17L)); + expectedData.add(new RowData("16", 16L)); + expectedData.add(new RowData("15", 15L)); + expectedData.add(new RowData("14", 14L)); + expectedData.add(new RowData("13", 13L)); + expectedData.add(new RowData("12", 12L)); + + // Adding the '4' group + expectedData.addAll(List.of( + new RowData("6", 4L), + new RowData("3", 4L), + new RowData("4", 4L), + new RowData("2", 4L), + new RowData("5", 4L) + )); + + // Adding the '3' group + expectedData.addAll(List.of( + new RowData("2", 3L), + new RowData("3", 3L), + new RowData("6", 3L), + new RowData("4", 3L), + new RowData("5", 3L) + )); + + // Adding the '2' group + expectedData.add(new RowData("6", 2L)); + expectedData.add(new RowData("5", 2L)); + + verifyDataAfterCompaction(tableName, expectedData, testDataProvider); + } + + @Test + public void testRebalanceCompactionOfNotPartitionedImplicitlyBucketedTable() throws Exception { + conf.setBoolVar(HiveConf.ConfVars.COMPACTOR_CRUD_QUERY_BASED, true); + conf.setBoolVar(HiveConf.ConfVars.HIVE_COMPACTOR_GATHER_STATS, false); + conf.setBoolVar(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER, false); + + // set grouping size to have 3 buckets, and re-create driver with the new config + conf.set("tez.grouping.min-size", "400"); + conf.set("tez.grouping.max-size", "5000"); + driver = new Driver(conf); + + final String tableName = "rebalance_test"; + TestDataProvider testDataProvider = prepareRebalanceTestData(tableName); + + // Run rebalance compaction + executeStatementOnDriver("ALTER TABLE " + tableName + " COMPACT 'rebalance'", driver); + runWorker(conf); + + // Check if the compaction succeed + verifyCompaction(1, TxnStore.CLEANING_RESPONSE); + + String[][] expectedBuckets = new String[][] { + { + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":0}\t5\t4", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":1}\t6\t2", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":2}\t6\t3", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":3}\t6\t4", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":4}\t5\t2", + }, + { + + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":5}\t5\t3", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":6}\t2\t4", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":7}\t3\t3", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":8}\t4\t4", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":9}\t4\t3", + }, + { + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":10}\t2\t3", + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":11}\t3\t4", + "{\"writeid\":2,\"bucketid\":537001984,\"rowid\":12}\t12\t12", + "{\"writeid\":3,\"bucketid\":537001984,\"rowid\":13}\t13\t13", + "{\"writeid\":4,\"bucketid\":537001984,\"rowid\":14}\t14\t14", + }, + { + "{\"writeid\":5,\"bucketid\":537067520,\"rowid\":15}\t15\t15", + "{\"writeid\":6,\"bucketid\":537067520,\"rowid\":16}\t16\t16", + "{\"writeid\":7,\"bucketid\":537067520,\"rowid\":17}\t17\t17", + }, + }; + verifyRebalance(testDataProvider, tableName, null, expectedBuckets, + new String[] {"bucket_00000", "bucket_00001", "bucket_00002","bucket_00003"}); + } + + @Test + public void testRebalanceCompactionOfPartitionedImplicitlyBucketedTable() throws Exception { + conf.setBoolVar(HiveConf.ConfVars.COMPACTOR_CRUD_QUERY_BASED, true); + conf.setBoolVar(HiveConf.ConfVars.HIVE_COMPACTOR_GATHER_STATS, false); + conf.setBoolVar(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER, false); + + //set grouping size to have 3 buckets, and re-create driver with the new config + conf.set("tez.grouping.min-size", "1"); + driver = new Driver(conf); + + final String stageTableName = "stage_rebalance_test"; + final String tableName = "rebalance_test"; + AcidOutputFormat.Options options = new AcidOutputFormat.Options(conf); + + TestDataProvider testDataProvider = new TestDataProvider(); + testDataProvider.createFullAcidTable(stageTableName, true, false); + executeStatementOnDriver("insert into " + stageTableName +" values " + + "('1',1,'yesterday'), ('1',2,'yesterday'), ('1',3, 'yesterday'), ('1',4, 'yesterday'), " + + "('2',1,'today'), ('2',2,'today'), ('2',3,'today'), ('2',4, 'today'), " + + "('3',1,'tomorrow'), ('3',2,'tomorrow'), ('3',3,'tomorrow'), ('3',4,'tomorrow')", + driver); + + dropTables(driver, tableName); + executeStatementOnDriver("CREATE TABLE " + tableName + "(a string, b int) " + + "PARTITIONED BY (ds string) STORED AS ORC TBLPROPERTIES('transactional'='true')", driver); + executeStatementOnDriver( + "INSERT OVERWRITE TABLE " + tableName + " partition (ds='tomorrow') select a, b from " + stageTableName, driver + ); + + //do some single inserts to have more data in the first bucket. + executeStatementOnDriver("INSERT INTO TABLE " + tableName + " values ('12',12,'tomorrow')", driver); + executeStatementOnDriver("INSERT INTO TABLE " + tableName + " values ('13',13,'tomorrow')", driver); + executeStatementOnDriver("INSERT INTO TABLE " + tableName + " values ('14',14,'tomorrow')", driver); + executeStatementOnDriver("INSERT INTO TABLE " + tableName + " values ('15',15,'tomorrow')", driver); + executeStatementOnDriver("INSERT INTO TABLE " + tableName + " values ('16',16,'tomorrow')", driver); + executeStatementOnDriver("INSERT INTO TABLE " + tableName + " values ('17',17,'tomorrow')", driver); + + // Verify buckets and their content before rebalance in partition ds=tomorrow + GetTableRequest getTableRequest = new GetTableRequest("default", tableName); + Table table = msClient.getTable(getTableRequest); + FileSystem fs = FileSystem.get(conf); + assertEquals("Test setup does not match the expected: different buckets", + Arrays.asList("bucket_00000_0", "bucket_00001_0", "bucket_00002_0"), + CompactorTestUtil.getBucketFileNames(fs, table, "ds=tomorrow", "base_0000001")); + String[][] expectedBuckets = new String[][] { + { + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":0}\t2\t1\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":1}\t2\t2\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":2}\t2\t3\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":3}\t2\t4\ttomorrow", + "{\"writeid\":2,\"bucketid\":536870912,\"rowid\":0}\t12\t12\ttomorrow", + "{\"writeid\":3,\"bucketid\":536870912,\"rowid\":0}\t13\t13\ttomorrow", + "{\"writeid\":4,\"bucketid\":536870912,\"rowid\":0}\t14\t14\ttomorrow", + "{\"writeid\":5,\"bucketid\":536870912,\"rowid\":0}\t15\t15\ttomorrow", + "{\"writeid\":6,\"bucketid\":536870912,\"rowid\":0}\t16\t16\ttomorrow", + "{\"writeid\":7,\"bucketid\":536870912,\"rowid\":0}\t17\t17\ttomorrow", + }, + { + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":0}\t3\t1\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":1}\t3\t2\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":2}\t3\t3\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":3}\t3\t4\ttomorrow", + }, + { + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":0}\t1\t1\ttomorrow", + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":1}\t1\t2\ttomorrow", + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":2}\t1\t3\ttomorrow", + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":3}\t1\t4\ttomorrow", + }, + }; + for(int i = 0; i < 3; i++) { + assertEquals("rebalanced bucket " + i, Arrays.asList(expectedBuckets[i]), + testDataProvider.getBucketData(tableName, BucketCodec.V1.encode(options.bucket(i)) + "")); + } + + //Try to do a rebalancing compaction + executeStatementOnDriver("ALTER TABLE " + tableName + " PARTITION (ds='tomorrow') COMPACT 'rebalance'", driver); + runWorker(conf); + + //Check if the compaction succeed + verifyCompaction(1, TxnStore.CLEANING_RESPONSE); + + expectedBuckets = new String[][] { + { + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":0}\t2\t1\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":1}\t2\t2\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":2}\t2\t3\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":3}\t2\t4\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":4}\t3\t1\ttomorrow", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":5}\t3\t2\ttomorrow", + }, + { + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":6}\t3\t3\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":7}\t3\t4\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":8}\t1\t1\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":9}\t1\t2\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":10}\t1\t3\ttomorrow", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":11}\t1\t4\ttomorrow", + }, + { + "{\"writeid\":2,\"bucketid\":537001984,\"rowid\":12}\t12\t12\ttomorrow", + "{\"writeid\":3,\"bucketid\":537001984,\"rowid\":13}\t13\t13\ttomorrow", + "{\"writeid\":4,\"bucketid\":537001984,\"rowid\":14}\t14\t14\ttomorrow", + "{\"writeid\":5,\"bucketid\":537001984,\"rowid\":15}\t15\t15\ttomorrow", + "{\"writeid\":6,\"bucketid\":537001984,\"rowid\":16}\t16\t16\ttomorrow", + "{\"writeid\":7,\"bucketid\":537001984,\"rowid\":17}\t17\t17\ttomorrow", + }, + }; + verifyRebalance(testDataProvider, tableName, "ds=tomorrow", expectedBuckets, + new String[] {"bucket_00000", "bucket_00001", "bucket_00002"}); + } + + @Test + public void testRebalanceCompactionOfNotPartitionedExplicitlyBucketedTable() throws Exception { + conf.setBoolVar(HiveConf.ConfVars.COMPACTOR_CRUD_QUERY_BASED, true); + conf.setBoolVar(HiveConf.ConfVars.HIVE_COMPACTOR_GATHER_STATS, false); + conf.setBoolVar(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER, false); + + final String tableName = "rebalance_test"; + dropTables(driver, tableName); + executeStatementOnDriver("CREATE TABLE " + tableName + "(a string, b int) " + + "CLUSTERED BY(a) INTO 4 BUCKETS STORED AS ORC TBLPROPERTIES('transactional'='true')", driver); + executeStatementOnDriver( + "INSERT INTO TABLE " + tableName + " values ('11',11),('22',22),('33',33),('44',44)", driver + ); + + //Try to do a rebalancing compaction + executeStatementOnDriver("ALTER TABLE " + tableName + " COMPACT 'rebalance'", driver); + runWorker(conf); + + //Check if the compaction is refused + List<ShowCompactResponseElement> compacts = verifyCompaction(1, TxnStore.REFUSED_RESPONSE); + assertEquals( + "Expecting error message 'Cannot execute rebalancing compaction on bucketed tables.' and found:" + + compacts.get(0).getState(), + "Cannot execute rebalancing compaction on bucketed tables.", compacts.get(0).getErrorMessage()); + } + + @Test + public void testRebalanceCompactionNotPartitionedExplicitBucketNumbers() throws Exception { + conf.setBoolVar(HiveConf.ConfVars.COMPACTOR_CRUD_QUERY_BASED, true); + conf.setBoolVar(HiveConf.ConfVars.HIVE_COMPACTOR_GATHER_STATS, false); + conf.setBoolVar(HiveConf.ConfVars.HIVE_STATS_AUTOGATHER, false); + + //set grouping size to have 3 buckets, and re-create driver with the new config + conf.set("tez.grouping.min-size", "400"); + conf.set("tez.grouping.max-size", "5000"); + driver = new Driver(conf); + + final String tableName = "rebalance_test"; + TestDataProvider testDataProvider = prepareRebalanceTestData(tableName); + + //Try to do a rebalancing compaction + executeStatementOnDriver("ALTER TABLE " + tableName + " COMPACT 'rebalance' CLUSTERED INTO 4 BUCKETS", driver); + runWorker(conf); + + verifyCompaction(1, TxnStore.CLEANING_RESPONSE); + + String[][] expectedBuckets = new String[][] { + { + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":0}\t5\t4", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":1}\t6\t2", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":2}\t6\t3", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":3}\t6\t4", + "{\"writeid\":1,\"bucketid\":536870912,\"rowid\":4}\t5\t2", + }, + { + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":5}\t5\t3", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":6}\t2\t4", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":7}\t3\t3", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":8}\t4\t4", + "{\"writeid\":1,\"bucketid\":536936448,\"rowid\":9}\t4\t3", + }, + { + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":10}\t2\t3", + "{\"writeid\":1,\"bucketid\":537001984,\"rowid\":11}\t3\t4", + "{\"writeid\":2,\"bucketid\":537001984,\"rowid\":12}\t12\t12", + "{\"writeid\":3,\"bucketid\":537001984,\"rowid\":13}\t13\t13", + "{\"writeid\":4,\"bucketid\":537001984,\"rowid\":14}\t14\t14", + }, + { + "{\"writeid\":5,\"bucketid\":537067520,\"rowid\":15}\t15\t15", + "{\"writeid\":6,\"bucketid\":537067520,\"rowid\":16}\t16\t16", + "{\"writeid\":7,\"bucketid\":537067520,\"rowid\":17}\t17\t17", + }, + }; + verifyRebalance(testDataProvider, tableName, null, expectedBuckets, Review Comment: I kept test cases that are not affected by the flakiness problem. For example, in this test case we run a compaction that defines the buckets explicitly (`CLUSTERED INTO 4 BUCKETS`) so that rebalance compaction always produces the exact same clusters. The flaky test cases are flaky because we cannot guarantee if it ends with 3 or 4 buckets. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
