ChenSammi commented on code in PR #8661: URL: https://github.com/apache/ozone/pull/8661#discussion_r2185073385
########## hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/scm/node/TestVolumeChoosingPolicy.java: ########## @@ -0,0 +1,252 @@ +/* + * 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.ozone.scm.node; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.fs.MockSpaceUsageCheckFactory; +import org.apache.hadoop.hdds.fs.MockSpaceUsageSource; +import org.apache.hadoop.hdds.fs.SpaceUsageCheckFactory; +import org.apache.hadoop.hdds.fs.SpaceUsagePersistence; +import org.apache.hadoop.hdds.fs.SpaceUsageSource; +import org.apache.hadoop.hdds.scm.ScmConfigKeys; +import org.apache.hadoop.ozone.container.common.statemachine.StateContext; +import org.apache.hadoop.ozone.container.common.volume.HddsVolume; +import org.apache.hadoop.ozone.container.common.volume.MutableVolumeSet; +import org.apache.hadoop.ozone.container.common.volume.StorageVolume; +import org.apache.hadoop.ozone.container.diskbalancer.policy.DefaultVolumeChoosingPolicy; +import org.apache.hadoop.ozone.container.diskbalancer.policy.VolumeChoosingPolicy; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.api.io.TempDir; + +/** + * This class tests the VolumeChoosingPolicy. + */ +public class TestVolumeChoosingPolicy { + + private static final int NUM_VOLUMES = 20; + private static final int NUM_THREADS = 10; + private static final int NUM_ITERATIONS = 10000; + private static final double THRESHOLD = 0.1; // 10% threshold + + private static final OzoneConfiguration CONF = new OzoneConfiguration(); + + @TempDir + private Path baseDir; + + private MutableVolumeSet volumeSet; + private List<HddsVolume> hddsVolumes; + private VolumeChoosingPolicy volumeChoosingPolicy; + private ExecutorService executor; + + // delta sizes for source volumes + private Map<HddsVolume, Long> deltaSizes = new ConcurrentHashMap<>(); + + @BeforeEach + public void setup() throws Exception { + hddsVolumes = new ArrayList<>(); + initialiseVolumeSet(); + createVolumes(); + volumeChoosingPolicy = new DefaultVolumeChoosingPolicy(); + executor = Executors.newFixedThreadPool(NUM_THREADS); + } + + @AfterEach + public void cleanUp() { + hddsVolumes.forEach(HddsVolume::shutdown); + + // Shutdown executor service + if (executor != null && !executor.isShutdown()) { + executor.shutdownNow(); + } + + // Clear in-progress delta sizes + if (deltaSizes != null) { + deltaSizes.clear(); + } + + // Clear VolumeSet + if (volumeSet != null) { + volumeSet.shutdown(); + volumeSet = null; + } + } + + @Test + @Timeout(300) + public void testConcurrentVolumeChoosingPerformance() throws Exception { + testPolicyPerformance("DefaultVolumeChoosingPolicy", volumeChoosingPolicy); + } + + /** + * pairChosenCount: Number of successful volume pair choices from the policy. + * FailureCount: Failures due to any exceptions thrown during volume choice or null return. + */ + private void testPolicyPerformance(String policyName, VolumeChoosingPolicy policy) throws Exception { + CountDownLatch latch = new CountDownLatch(NUM_THREADS); + AtomicInteger pairChosenCount = new AtomicInteger(0); + AtomicInteger pairNotChosenCount = new AtomicInteger(0); + AtomicInteger failureCount = new AtomicInteger(0); + AtomicLong totalTimeNanos = new AtomicLong(0); + + Random rand = new Random(); + + for (int i = 0; i < NUM_THREADS; i++) { + executor.submit(() -> { + try { + long threadStart = System.nanoTime(); + int volumeChosen = 0; + int volumeNotChosen = 0; + int failures = 0; + + for (int j = 0; j < NUM_ITERATIONS; j++) { + if (rand.nextDouble() < 0.05 && hddsVolumes.size() >= 2) { Review Comment: Thanks @Gargi-jais11 , can you add a comment about the purpose of "rand.nextDouble() < 0.05"? -- 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]
