|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.datasource.hive.source; |
| 19 | + |
| 20 | +import org.apache.doris.analysis.TableSample; |
| 21 | +import org.apache.doris.analysis.TupleDescriptor; |
| 22 | +import org.apache.doris.analysis.TupleId; |
| 23 | +import org.apache.doris.catalog.Env; |
| 24 | +import org.apache.doris.common.util.LocationPath; |
| 25 | +import org.apache.doris.datasource.ExternalMetaCacheMgr; |
| 26 | +import org.apache.doris.datasource.FileQueryScanNode; |
| 27 | +import org.apache.doris.datasource.FileSplitter; |
| 28 | +import org.apache.doris.datasource.SplitAssignment; |
| 29 | +import org.apache.doris.datasource.hive.HMSExternalCatalog; |
| 30 | +import org.apache.doris.datasource.hive.HMSExternalTable; |
| 31 | +import org.apache.doris.datasource.hive.HiveExternalMetaCache; |
| 32 | +import org.apache.doris.datasource.hive.HiveExternalMetaCache.FileCacheValue; |
| 33 | +import org.apache.doris.datasource.hive.HiveExternalMetaCache.HiveFileStatus; |
| 34 | +import org.apache.doris.datasource.hive.HivePartition; |
| 35 | +import org.apache.doris.planner.PlanNodeId; |
| 36 | +import org.apache.doris.planner.ScanContext; |
| 37 | +import org.apache.doris.planner.ScanNode; |
| 38 | +import org.apache.doris.qe.SessionVariable; |
| 39 | +import org.apache.doris.thrift.TFileFormatType; |
| 40 | + |
| 41 | +import org.junit.Assert; |
| 42 | +import org.junit.Test; |
| 43 | +import org.mockito.MockedStatic; |
| 44 | +import org.mockito.Mockito; |
| 45 | + |
| 46 | +import java.lang.reflect.Field; |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.Collections; |
| 49 | +import java.util.HashMap; |
| 50 | +import java.util.List; |
| 51 | +import java.util.Map; |
| 52 | +import java.util.concurrent.CompletableFuture; |
| 53 | +import java.util.concurrent.ExecutorService; |
| 54 | +import java.util.concurrent.Executors; |
| 55 | +import java.util.concurrent.TimeUnit; |
| 56 | +import java.util.concurrent.atomic.AtomicInteger; |
| 57 | +import java.util.stream.Collectors; |
| 58 | + |
| 59 | +public class HiveSplitEstimateTest { |
| 60 | + @Test |
| 61 | + public void testUniformPartitionsAreIndependentOfBatchSize() throws Exception { |
| 62 | + for (int batchSize : new int[] {16, 2, 1}) { |
| 63 | + Assert.assertEquals(60, runBatchEstimate(batchSize, false, 10, 10, 10, 10, 10, 10)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testUnevenAndEmptyPartitionsKeepTheLargestPartitionEstimate() throws Exception { |
| 69 | + for (int batchSize : new int[] {16, 2, 1}) { |
| 70 | + Assert.assertEquals(100, runBatchEstimate(batchSize, false, 0, 1, 25, 3)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testSampledPartitionsKeepTheSameEstimate() throws Exception { |
| 76 | + for (int batchSize : new int[] {16, 2}) { |
| 77 | + Assert.assertEquals(60, runBatchEstimate(batchSize, true, 10, 10, 10, 10, 10, 10)); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testUnpartitionedScanKeepsItsSplitCount() throws Exception { |
| 83 | + Assert.assertEquals(25, runBatchEstimate(16, false, 25)); |
| 84 | + } |
| 85 | + |
| 86 | + private int runBatchEstimate(int batchSize, boolean sample, int... fileCounts) throws Exception { |
| 87 | + SessionVariable variables = new SessionVariable(); |
| 88 | + variables.splitPartitionBatchSize = batchSize; |
| 89 | + HMSExternalTable table = Mockito.mock(HMSExternalTable.class); |
| 90 | + HMSExternalCatalog catalog = Mockito.mock(HMSExternalCatalog.class); |
| 91 | + Mockito.when(table.getCatalog()).thenReturn(catalog); |
| 92 | + Mockito.when(table.getFileFormatType(variables)).thenReturn(TFileFormatType.FORMAT_PARQUET); |
| 93 | + Mockito.when(catalog.bindBrokerName()).thenReturn(""); |
| 94 | + TupleDescriptor descriptor = new TupleDescriptor(new TupleId(0)); |
| 95 | + descriptor.setTable(table); |
| 96 | + HiveScanNode node = new HiveScanNode( |
| 97 | + new PlanNodeId(0), descriptor, false, variables, null, ScanContext.EMPTY); |
| 98 | + Field splitter = FileQueryScanNode.class.getDeclaredField("fileSplitter"); |
| 99 | + splitter.setAccessible(true); |
| 100 | + splitter.set(node, new FileSplitter(variables.maxInitialSplitSize, |
| 101 | + variables.maxSplitSize, variables.maxInitialSplitNum)); |
| 102 | + if (sample) { |
| 103 | + node.setTableSample(new TableSample(true, 100L, 0L)); |
| 104 | + } |
| 105 | + List<HivePartition> partitions = new ArrayList<>(); |
| 106 | + Map<HivePartition, FileCacheValue> files = new HashMap<>(); |
| 107 | + int totalFiles = 0; |
| 108 | + for (int partition = 0; partition < fileCounts.length; partition++) { |
| 109 | + List<String> values = fileCounts.length == 1 |
| 110 | + ? null : Collections.singletonList(Integer.toString(partition)); |
| 111 | + String path = "hdfs://warehouse/t/p=" + partition; |
| 112 | + HivePartition hivePartition = new HivePartition(null, false, "parquet", path, |
| 113 | + values, Collections.emptyMap()); |
| 114 | + partitions.add(hivePartition); |
| 115 | + FileCacheValue cacheValue = new FileCacheValue(); |
| 116 | + cacheValue.setPartitionValues(values); |
| 117 | + for (int file = 0; file < fileCounts[partition]; file++) { |
| 118 | + HiveFileStatus status = new HiveFileStatus(); |
| 119 | + status.setPath(LocationPath.of(path + "/file" + file + ".parquet")); |
| 120 | + status.setLength(1024); |
| 121 | + cacheValue.getFiles().add(status); |
| 122 | + totalFiles++; |
| 123 | + } |
| 124 | + files.put(hivePartition, cacheValue); |
| 125 | + } |
| 126 | + Field pruned = HiveScanNode.class.getDeclaredField("prunedPartitions"); |
| 127 | + pruned.setAccessible(true); |
| 128 | + pruned.set(node, partitions); |
| 129 | + SplitAssignment assignment = Mockito.mock(SplitAssignment.class); |
| 130 | + Mockito.when(assignment.needMoreSplit()).thenReturn(true); |
| 131 | + Field assignmentField = ScanNode.class.getDeclaredField("splitAssignment"); |
| 132 | + assignmentField.setAccessible(true); |
| 133 | + assignmentField.set(node, assignment); |
| 134 | + CompletableFuture<Void> finished = new CompletableFuture<>(); |
| 135 | + Mockito.doAnswer(call -> { |
| 136 | + finished.complete(null); |
| 137 | + return null; |
| 138 | + }).when(assignment).finishSchedule(); |
| 139 | + Mockito.doAnswer(call -> { |
| 140 | + finished.completeExceptionally(call.getArgument(0)); |
| 141 | + return null; |
| 142 | + }).when(assignment).setException(Mockito.any()); |
| 143 | + AtomicInteger delivered = new AtomicInteger(); |
| 144 | + Mockito.doAnswer(call -> { |
| 145 | + delivered.addAndGet(call.<List<?>>getArgument(0).size()); |
| 146 | + return null; |
| 147 | + }).when(assignment).addToQueue(Mockito.anyList()); |
| 148 | + HiveExternalMetaCache cache = Mockito.mock(HiveExternalMetaCache.class); |
| 149 | + Mockito.when(cache.getFilesByPartitions(Mockito.anyList(), Mockito.anyBoolean(), Mockito.anyBoolean(), |
| 150 | + Mockito.isNull(), Mockito.same(table))).thenAnswer(call -> call.<List<HivePartition>>getArgument(0) |
| 151 | + .stream().map(files::get).collect(Collectors.toList())); |
| 152 | + Env env = Mockito.mock(Env.class); |
| 153 | + ExternalMetaCacheMgr manager = Mockito.mock(ExternalMetaCacheMgr.class); |
| 154 | + ExecutorService executor = Executors.newFixedThreadPool(2); |
| 155 | + Mockito.when(env.getExtMetaCacheMgr()).thenReturn(manager); |
| 156 | + Mockito.when(manager.hive(Mockito.anyLong())).thenReturn(cache); |
| 157 | + Mockito.when(manager.getScheduleExecutor()).thenReturn(executor); |
| 158 | + try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class)) { |
| 159 | + mockedEnv.when(Env::getCurrentEnv).thenReturn(env); |
| 160 | + node.startSplit(1); |
| 161 | + finished.get(5, TimeUnit.SECONDS); |
| 162 | + Assert.assertEquals(totalFiles, delivered.get()); |
| 163 | + Mockito.verify(assignment, Mockito.never()).setException(Mockito.any()); |
| 164 | + return node.numApproximateSplits(); |
| 165 | + } finally { |
| 166 | + executor.shutdownNow(); |
| 167 | + Assert.assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS)); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments