From 2e60bf3a340438607dd706ef0cad052853ae56be Mon Sep 17 00:00:00 2001 From: Alexandr Juncevich Date: Mon, 25 Nov 2024 22:55:23 +0300 Subject: [PATCH] HDDS-11367. Fix review notice. Refactor saving iteration statistic. --- .../balancer/ContainerBalancerTask.java | 61 ++++++++++++------- .../TestContainerBalancerStatusInfo.java | 2 +- .../ContainerBalancerStatusSubcommand.java | 18 ++++-- .../smoketest/balancer/testBalancer.robot | 12 ++-- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerTask.java b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerTask.java index 8f2775d44c1..f1eee8c6755 100644 --- a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerTask.java +++ b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancerTask.java @@ -274,8 +274,10 @@ private void balance() { return; } - IterationResult iR = doIteration(); - saveIterationStatistic(i + 1, iR); + IterationResult currentIterationResult = doIteration(); + ContainerBalancerTaskIterationStatusInfo iterationStatistic = + getIterationStatistic(i + 1, currentIterationResult, getCurrentIterationDuration()); + iterationsStatistic.offer(iterationStatistic); isCurrentIterationInProgress.compareAndSet(true, false); @@ -284,17 +286,17 @@ private void balance() { metrics.incrementNumIterations(1); - LOG.info("Result of this iteration of Container Balancer: {}", iR); + LOG.info("Result of this iteration of Container Balancer: {}", currentIterationResult); // if no new move option is generated, it means the cluster cannot be // balanced anymore; so just stop balancer - if (iR == IterationResult.CAN_NOT_BALANCE_ANY_MORE) { - tryStopWithSaveConfiguration(iR.toString()); + if (currentIterationResult == IterationResult.CAN_NOT_BALANCE_ANY_MORE) { + tryStopWithSaveConfiguration(currentIterationResult.toString()); return; } // persist next iteration index - if (iR == IterationResult.ITERATION_COMPLETED) { + if (currentIterationResult == IterationResult.ITERATION_COMPLETED) { try { saveConfiguration(config, true, i + 1); } catch (IOException | TimeoutException e) { @@ -325,30 +327,45 @@ private void balance() { tryStopWithSaveConfiguration("Completed all iterations."); } - private void saveIterationStatistic(Integer iterationNumber, IterationResult currentIterationResult) { - long iterationDuration = now().toEpochSecond() - currentIterationStarted.toEpochSecond(); + private ContainerBalancerTaskIterationStatusInfo getIterationStatistic(Integer iterationNumber, + IterationResult currentIterationResult, + long iterationDuration) { + String currentIterationResultName = currentIterationResult == null ? null : currentIterationResult.name(); Map sizeEnteringDataToNodes = convertToNodeIdToTrafficMap(findTargetStrategy.getSizeEnteringNodes()); Map sizeLeavingDataFromNodes = convertToNodeIdToTrafficMap(findSourceStrategy.getSizeLeavingNodes()); IterationInfo iterationInfo = new IterationInfo( iterationNumber, - currentIterationResult.name(), + currentIterationResultName, iterationDuration ); ContainerMoveInfo containerMoveInfo = new ContainerMoveInfo(metrics); - DataMoveInfo dataMoveInfo = new DataMoveInfo( - getSizeScheduledForMoveInLatestIteration(), - metrics.getDataSizeMovedInLatestIteration(), - sizeEnteringDataToNodes, - sizeLeavingDataFromNodes - ); - ContainerBalancerTaskIterationStatusInfo iterationStatistic = new ContainerBalancerTaskIterationStatusInfo( - iterationInfo, - containerMoveInfo, - dataMoveInfo - ); - iterationsStatistic.offer(iterationStatistic); + + DataMoveInfo dataMoveInfo = + getDataMoveInfo(currentIterationResultName, sizeEnteringDataToNodes, sizeLeavingDataFromNodes); + return new ContainerBalancerTaskIterationStatusInfo(iterationInfo, containerMoveInfo, dataMoveInfo); + } + + private DataMoveInfo getDataMoveInfo(String currentIterationResultName, Map sizeEnteringDataToNodes, + Map sizeLeavingDataFromNodes) { + if (currentIterationResultName == null) { + // For unfinished iteration + return new DataMoveInfo( + getSizeScheduledForMoveInLatestIteration(), + sizeActuallyMovedInLatestIteration, + sizeEnteringDataToNodes, + sizeLeavingDataFromNodes + ); + } else { + // For finished iteration + return new DataMoveInfo( + getSizeScheduledForMoveInLatestIteration(), + metrics.getDataSizeMovedInLatestIteration(), + sizeEnteringDataToNodes, + sizeLeavingDataFromNodes + ); + } } private Map convertToNodeIdToTrafficMap(Map nodeTrafficMap) { @@ -388,7 +405,7 @@ private ContainerBalancerTaskIterationStatusInfo createCurrentIterationStatistic long iterationDuration = getCurrentIterationDuration(); if (isCurrentIterationInProgress.get()) { - return getFilledCurrentIterationStatistic(lastIterationNumber, iterationDuration); + return getIterationStatistic(lastIterationNumber + 1, null, iterationDuration); } else { return null; } diff --git a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/balancer/TestContainerBalancerStatusInfo.java b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/balancer/TestContainerBalancerStatusInfo.java index 5110c93edec..fafd43c69ff 100644 --- a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/balancer/TestContainerBalancerStatusInfo.java +++ b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/balancer/TestContainerBalancerStatusInfo.java @@ -139,7 +139,7 @@ void testGetCurrentStatisticsWithDelay() throws Exception { config.setMaxSizeToMovePerIteration(50 * OzoneConsts.GB); OzoneConfiguration configuration = new OzoneConfiguration(); configuration.set(HddsConfigKeys.HDDS_SCM_WAIT_TIME_AFTER_SAFE_MODE_EXIT, "1"); - ContainerBalancerTask task = mockedScm.startBalancerTaskAsync(config, configuration,true); + ContainerBalancerTask task = mockedScm.startBalancerTaskAsync(config, configuration, true); // Delay in finishing the first iteration LambdaTestUtils.await(1100, 50, () -> task.getCurrentIterationsStatistic().size() == 1); List iterationsStatic = task.getCurrentIterationsStatistic(); diff --git a/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/ContainerBalancerStatusSubcommand.java b/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/ContainerBalancerStatusSubcommand.java index c3800ae3c4f..9d7c270c962 100644 --- a/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/ContainerBalancerStatusSubcommand.java +++ b/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/ContainerBalancerStatusSubcommand.java @@ -78,15 +78,25 @@ public void execute(ScmClient scmClient) throws IOException { = balancerStatusInfo.getIterationsStatusInfoList(); System.out.println("Current iteration info:"); - System.out.println( - getPrettyIterationStatusInfo(iterationsStatusInfoList.get(iterationsStatusInfoList.size() - 1)) - ); + ContainerBalancerTaskIterationStatusInfoProto currentIterationStatistic = iterationsStatusInfoList.stream() + .filter(it -> it.getIterationResult().isEmpty()) + .findFirst() + .orElse(null); + if (currentIterationStatistic == null) { + System.out.println("-\n"); + } else { + System.out.println( + getPrettyIterationStatusInfo(currentIterationStatistic) + ); + } + if (verboseWithHistory) { System.out.println("Iteration history list:"); System.out.println( - iterationsStatusInfoList.subList(0, iterationsStatusInfoList.size() - 1) + iterationsStatusInfoList .stream() + .filter(it -> !it.getIterationResult().isEmpty()) .map(this::getPrettyIterationStatusInfo) .collect(Collectors.joining("\n")) ); diff --git a/hadoop-ozone/dist/src/main/smoketest/balancer/testBalancer.robot b/hadoop-ozone/dist/src/main/smoketest/balancer/testBalancer.robot index ece820e67f6..608fd27f8e6 100644 --- a/hadoop-ozone/dist/src/main/smoketest/balancer/testBalancer.robot +++ b/hadoop-ozone/dist/src/main/smoketest/balancer/testBalancer.robot @@ -85,12 +85,12 @@ Verify Balancer Iteration Verify Balancer Iteration History [arguments] ${output} Should Contain ${output} Iteration history list: - Should Contain X Times ${output} Size scheduled to move 2 collapse_spaces=True - Should Contain X Times ${output} Moved data size 2 collapse_spaces=True - Should Contain X Times ${output} Scheduled to move containers 2 collapse_spaces=True - Should Contain X Times ${output} Already moved containers 2 collapse_spaces=True - Should Contain X Times ${output} Failed to move containers 0 2 collapse_spaces=True - Should Contain X Times ${output} Failed to move containers by timeout 0 2 collapse_spaces=True + Should Contain X Times ${output} Size scheduled to move 1 collapse_spaces=True + Should Contain X Times ${output} Moved data size 1 collapse_spaces=True + Should Contain X Times ${output} Scheduled to move containers 1 collapse_spaces=True + Should Contain X Times ${output} Already moved containers 1 collapse_spaces=True + Should Contain X Times ${output} Failed to move containers 0 1 collapse_spaces=True + Should Contain X Times ${output} Failed to move containers by timeout 0 1 collapse_spaces=True Should Contain ${output} Iteration result ITERATION_COMPLETED collapse_spaces=True Run Balancer Status