vikramahuja1001 commented on code in PR #4807: URL: https://github.com/apache/hive/pull/4807#discussion_r1418455883
########## ql/src/test/org/apache/hadoop/hive/ql/exec/TestGetPartitionAuthWithBatches.java: ########## @@ -0,0 +1,274 @@ +/* + * 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.exec; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; + +import org.apache.hadoop.hive.metastore.api.GetPartitionsPsWithAuthRequest; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.ql.metadata.Hive; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.metadata.Partition; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.junit.Assert; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.util.Arrays; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class TestGetPartitionAuthWithBatches { + + private final String catName = "hive"; + private final String dbName = "default"; + private final String tableName = "test_partition_batch_with_auth"; + private static HiveConf hiveConf; + private static HiveMetaStoreClient msc; + private static Hive hive; + private Table table; + + @BeforeClass + public static void setupClass() throws HiveException { + hiveConf = new HiveConf(TestGetPartitionAuthWithBatches.class); + hiveConf.set("hive.security.authorization.enabled", "true"); + hiveConf.set("hive.security.authorization.manager","org.apache.hadoop.hive.ql.security.authorization.DefaultHiveAuthorizationProvider"); + hive = Hive.get(); + SessionState.start(hiveConf); + try { + msc = new HiveMetaStoreClient(hiveConf); + } catch (MetaException e) { + throw new HiveException(e); + } + } + + @Before + public void before() throws Exception { + PartitionUtil.createPartitionedTable(msc, catName, dbName, tableName); + table = msc.getTable(catName, dbName, tableName); + PartitionUtil.addPartitions(msc, dbName, tableName, table.getSd().getLocation(), hiveConf); + } + + @After + public void after() throws Exception { + PartitionUtil.cleanUpTableQuietly(msc, catName, dbName, tableName); + } + + @Test + public void testNumberOfPartitionsRetrieved() throws HiveException { + List<String> numParts = hive.getPartitionNames(dbName, tableName, (short)-1); + Assert.assertEquals(numParts.size(), 30); + List<Partition> partitions = hive.getPartitionsAuthByNames(new org.apache.hadoop.hive.ql.metadata.Table(table), + numParts.subList(0,5), "username", new ArrayList<>(Arrays.asList("Grp1", "Grp2"))); + Assert.assertEquals(partitions.size(), 5); + } + + /** + * Tests the number of partitions recieved from the HMS + * + * @throws Exception + */ + @Test + public void testGetPartitionsAPI() throws Exception { + List<org.apache.hadoop.hive.ql.metadata.Partition> part = hive.getPartitions(hive.getTable(dbName, tableName)); + Assert.assertEquals(part.size(), 30); + } + + @Test + public void testGetPartitionsAPI2() throws Exception { + List<org.apache.hadoop.hive.ql.metadata.Partition> part = hive.getPartitions(hive.getTable(dbName, tableName), new HashMap() , (short) -1); + Assert.assertEquals(part.size(), 30); + } + + @Test + public void testGetPartitionsAPI2limit() throws Exception { + List<org.apache.hadoop.hive.ql.metadata.Partition> part = hive.getPartitions(hive.getTable(dbName, tableName), new HashMap() , (short) 1); + Assert.assertEquals(part.size(), 1); + } + + /** + * Tests the number of times Hive.getPartitions calls are executed with total number of + * partitions to be added are equally divisible by batch size + * + * @throws Exception + */ + @Test + public void testNumberOfGetPartitionCalls() throws Exception { + HiveMetaStoreClient spyMSC = spy(msc); + hive.setMSC(spyMSC); + // test with a batch size of 10 and decaying factor of 2 + hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName),10, 2, 0, null, true, "username", new ArrayList<>(Arrays.asList("Grp1", "Grp2"))); + ArgumentCaptor<GetPartitionsPsWithAuthRequest> req = ArgumentCaptor.forClass(GetPartitionsPsWithAuthRequest.class); + // there should be 3 calls to get partitions + verify(spyMSC, times(3)).listPartitionsWithAuthInfoRequest(req.capture()); + req.getAllValues().forEach(part-> Assert.assertEquals(part.getPartNames().size(),10)); + } + + @Test + public void testNumberOfGetPartitionCalls2() throws Exception { + HiveMetaStoreClient spyMSC = spy(msc); + hive.setMSC(spyMSC); + // test with a batch size of 10 and decaying factor of 2 + hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName),10, 2, 0, new HashMap(), true, "username", new ArrayList<>(Arrays.asList("Grp1", "Grp2"))); + ArgumentCaptor<GetPartitionsPsWithAuthRequest> req = ArgumentCaptor.forClass(GetPartitionsPsWithAuthRequest.class); + // there should be 3 calls to get partitions + verify(spyMSC, times(3)).listPartitionsWithAuthInfoRequest(req.capture()); + req.getAllValues().forEach(part-> Assert.assertEquals(part.getPartNames().size(),10)); + } + + /** + * Tests the number of times Hive.getAllPartitionsOf calls are executed with total number of + * partitions to be added are not exactly divisible by batch size + * + * @throws Exception + */ + @Test + public void testUnevenNumberOfGetPartitionCalls() throws Exception { + HiveMetaStoreClient spyMSC = spy(msc); + hive.setMSC(spyMSC); + // there should be 2 calls to get partitions with batch sizes of 19, 11 + hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName),19, 2, 0, null, true, "user", new ArrayList<>(Arrays.asList("Grp1", "Grp2"))); + ArgumentCaptor<GetPartitionsPsWithAuthRequest> req = ArgumentCaptor.forClass(GetPartitionsPsWithAuthRequest.class); + // there should be 2 calls to get partitions + verify(spyMSC, times(2)).listPartitionsWithAuthInfoRequest(req.capture()); + // confirm the batch sizes were 19, 11 in the two calls to get partitions + List<GetPartitionsPsWithAuthRequest> apds = req.getAllValues(); + Assert.assertEquals(19, apds.get(0).getPartNames().size()); + Assert.assertEquals(11, apds.get(1).getPartNames().size()); + } + + /** + * Tests the number of times Hive.getAllPartitionsOf calls are executed with total number of + * partitions to is less than batch size + * + * @throws Exception + */ + @Test + public void testSmallNumberOfPartitions() throws Exception { + HiveMetaStoreClient spyMSC = spy(msc); + hive.setMSC(spyMSC); + hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName),100, 2, 0, null, true, "user", new ArrayList<>(Arrays.asList("Grp1", "Grp2"))); + ArgumentCaptor<GetPartitionsPsWithAuthRequest> req = ArgumentCaptor.forClass(GetPartitionsPsWithAuthRequest.class); + // there should be 1 call to get partitions + verify(spyMSC, times(1)).listPartitionsWithAuthInfoRequest(req.capture()); + Assert.assertEquals(30, req.getValue().getPartNames().size()); + } + + /** + * Tests the retries exhausted case when getAllPartitionsOf method call always keep throwing + * HiveException. The batch sizes should exponentially decreased based on the decaying factor and + * ultimately give up when it reaches 0 + * + * @throws Exception + */ + @Test + public void testRetriesExhaustedBatchSize() throws Exception { + HiveMetaStoreClient spyMSC = spy(msc); + hive.setMSC(spyMSC); + doThrow(MetaException.class).when(spyMSC).listPartitionsWithAuthInfoRequest(any()); + try { + hive.getAllPartitionsInBatches(hive.getTable(dbName, tableName), 30, 2, 0, null, true, "user", new ArrayList<>(Arrays.asList("Grp1", "Grp2"))); + } catch (Exception ignored) {} Review Comment: done ########## ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java: ########## @@ -4204,9 +4223,11 @@ public Set<Partition> getAllPartitionsInBatches(Table tbl, int batchSize, int de public Void execute(int size) throws HiveException { try { result.clear(); - new PartitionIterable(Hive.get(), tbl, null, size).forEach(result::add); + new PartitionIterable(Hive.get(), tbl, partialPartitionSpec, size, isAuthRequired, userName, + groupNames).forEach(result::add); return null; } catch (HiveException e) { + LOG.error("Unable to get Partition List", e); Review Comment: changed -- 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]
