|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.hive.split; |
| 19 | + |
| 20 | +import org.apache.seatunnel.shade.com.google.common.collect.Lists; |
| 21 | +import org.apache.seatunnel.shade.com.google.common.collect.Maps; |
| 22 | + |
| 23 | +import org.apache.seatunnel.api.source.SourceSplitEnumerator; |
| 24 | +import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 25 | +import org.apache.seatunnel.api.table.catalog.TableIdentifier; |
| 26 | +import org.apache.seatunnel.connectors.seatunnel.file.source.split.FileSourceSplit; |
| 27 | +import org.apache.seatunnel.connectors.seatunnel.hive.source.config.HiveSourceConfig; |
| 28 | +import org.apache.seatunnel.connectors.seatunnel.hive.source.config.MultipleTableHiveSourceConfig; |
| 29 | +import org.apache.seatunnel.connectors.seatunnel.hive.source.split.MultipleTableHiveSourceSplitEnumerator; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Assertions; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.mockito.Mockito; |
| 34 | + |
| 35 | +import lombok.extern.slf4j.Slf4j; |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.concurrent.atomic.AtomicInteger; |
| 43 | +import java.util.stream.IntStream; |
| 44 | + |
| 45 | +@Slf4j |
| 46 | +public class MultipleTableHiveSourceSplitEnumeratorTest { |
| 47 | + |
| 48 | + @Test |
| 49 | + void assignSplitRoundTest() { |
| 50 | + int parallelism = 4; |
| 51 | + int fileSize = 50; |
| 52 | + |
| 53 | + MultipleTableHiveSourceConfig mockConfig = |
| 54 | + Mockito.mock(MultipleTableHiveSourceConfig.class); |
| 55 | + |
| 56 | + Map<String, List<String>> filePathMap = new HashMap<>(); |
| 57 | + List<String> filePaths = new ArrayList<>(); |
| 58 | + IntStream.range(0, fileSize).forEach(i -> filePaths.add("filePath" + i)); |
| 59 | + filePathMap.put("hive_table1", filePaths); |
| 60 | + |
| 61 | + HiveSourceConfig mockHiveSourceConfig = Mockito.mock(HiveSourceConfig.class); |
| 62 | + Mockito.when(mockHiveSourceConfig.getFilePaths()).thenReturn(filePaths); |
| 63 | + |
| 64 | + CatalogTable catalogTable = |
| 65 | + CatalogTable.of( |
| 66 | + TableIdentifier.of("catalog", "test", "hive_table1"), |
| 67 | + null, |
| 68 | + Maps.newHashMap(), |
| 69 | + Lists.newArrayList(), |
| 70 | + null); |
| 71 | + |
| 72 | + Mockito.when(mockHiveSourceConfig.getCatalogTable()).thenReturn(catalogTable); |
| 73 | + |
| 74 | + Mockito.when(mockConfig.getHiveSourceConfigs()) |
| 75 | + .thenReturn(Arrays.asList(mockHiveSourceConfig)); |
| 76 | + |
| 77 | + SourceSplitEnumerator.Context<FileSourceSplit> context = |
| 78 | + Mockito.mock(SourceSplitEnumerator.Context.class); |
| 79 | + |
| 80 | + Mockito.when(context.currentParallelism()).thenReturn(parallelism); |
| 81 | + MultipleTableHiveSourceSplitEnumerator enumerator = |
| 82 | + new MultipleTableHiveSourceSplitEnumerator(context, mockConfig); |
| 83 | + |
| 84 | + AtomicInteger unAssignedSplitSize = new AtomicInteger(fileSize); |
| 85 | + IntStream.range(0, parallelism) |
| 86 | + .forEach( |
| 87 | + id -> { |
| 88 | + enumerator.registerReader(id); |
| 89 | + |
| 90 | + // check the number of files assigned each time |
| 91 | + Assertions.assertEquals( |
| 92 | + enumerator.currentUnassignedSplitSize(), |
| 93 | + unAssignedSplitSize.get() |
| 94 | + - allocateFiles(id, parallelism, fileSize)); |
| 95 | + unAssignedSplitSize.set(enumerator.currentUnassignedSplitSize()); |
| 96 | + |
| 97 | + log.info( |
| 98 | + "unAssigned splits => {}, allocate files => {}", |
| 99 | + enumerator.currentUnassignedSplitSize(), |
| 100 | + allocateFiles(id, parallelism, fileSize)); |
| 101 | + }); |
| 102 | + |
| 103 | + // check no duplicate file assigned |
| 104 | + Assertions.assertEquals(0, enumerator.currentUnassignedSplitSize()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * calculate the number of files assigned each time |
| 109 | + * |
| 110 | + * @param id id |
| 111 | + * @param parallelism parallelism |
| 112 | + * @param fileSize file size |
| 113 | + * @return |
| 114 | + */ |
| 115 | + public int allocateFiles(int id, int parallelism, int fileSize) { |
| 116 | + int filesPerIteration = fileSize / parallelism; |
| 117 | + int remainder = fileSize % parallelism; |
| 118 | + |
| 119 | + if (id < remainder) { |
| 120 | + return filesPerIteration + 1; |
| 121 | + } else { |
| 122 | + return filesPerIteration; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments