KnightChess commented on code in PR #11578: URL: https://github.com/apache/hudi/pull/11578#discussion_r1667596861
########## hudi-common/src/test/java/org/apache/hudi/common/util/hash/TestBucketIndexUtil.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.hudi.common.util.hash; + +import org.apache.hudi.common.util.Functions; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestBucketIndexUtil { + + private static Stream<Arguments> configParams() { + List<Arguments> argsList = new ArrayList<>(); + + argsList.add(Arguments.of(6, 12)); + argsList.add(Arguments.of(5, 12)); + argsList.add(Arguments.of(24, 12)); + argsList.add(Arguments.of(23, 12)); + argsList.add(Arguments.of(100, 5)); + + return argsList.stream(); + } + + @ParameterizedTest + @MethodSource("configParams") + void test(int parallelism, int bucketNumber) { + Map<Integer, Integer> parallelism2TaskCount = new HashMap<>(); + final Functions.Function2<String, Integer, Integer> partitionIndexFunc = + BucketIndexUtil.getPartitionIndexFunc(bucketNumber, parallelism); + initData(parallelism2TaskCount, bucketNumber, partitionIndexFunc); + checkResult(parallelism2TaskCount, parallelism, bucketNumber); + } + + private static void putIndexCount(Map<Integer, Integer> parallelism2TaskCount, int workIndex) { + if (parallelism2TaskCount.containsKey(workIndex)) { + parallelism2TaskCount.put(workIndex, parallelism2TaskCount.get(workIndex) + 1); + } else { + parallelism2TaskCount.put(workIndex, 1); + } + } + + private void checkResult(Map<Integer, Integer> parallelism2TaskCount, int parallelism, int bucketNumber) { + int sum = 0; + for (int v : parallelism2TaskCount.values()) { + sum = sum + v; + } + int avg = sum / parallelism; + double minToleranceValue = avg * 0.8; + double maxToleranceValue = avg * 1.2; + final ArrayList<Integer> outOfLimit = new ArrayList<>(); + final ArrayList<Integer> inLimit = new ArrayList<>(); + for (int v : parallelism2TaskCount.values()) { + // if parallelism is too bigger, first condition will false although the diff is 1 + // for example, avg is 4, v is 5 or 3 all will out of limit, so add second condition + if ((v >= minToleranceValue) && (v <= maxToleranceValue) || ((Math.abs(v - avg) <= 2))) { + inLimit.add(v); + } else { + outOfLimit.add(v); + } + } + System.out.println("avg : " + avg); Review Comment: forgot delete, will delete latter -- 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]
