Gargi-jais11 commented on code in PR #9256: URL: https://github.com/apache/ozone/pull/9256#discussion_r2517399823
########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/diskbalancer/DiskBalancerProtocolServer.java: ########## @@ -0,0 +1,162 @@ +/* + * 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.container.diskbalancer; + +import jakarta.annotation.Nonnull; +import java.io.IOException; +import java.util.Objects; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.protocol.DiskBalancerProtocol; +import org.apache.hadoop.hdds.protocol.proto.DiskBalancerProtocolProtos.DatanodeDiskBalancerInfoType; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos.DatanodeDiskBalancerInfoProto; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos.DiskBalancerConfigurationProto; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos.DiskBalancerRunningStatus; +import org.apache.hadoop.ozone.container.common.statemachine.DatanodeStateMachine; +import org.apache.hadoop.ozone.container.diskbalancer.DiskBalancerService.DiskBalancerOperationalState; +import org.apache.hadoop.ozone.container.ozoneimpl.OzoneContainer; + +/** + * Server-side implementation of {@link DiskBalancerProtocol} for datanodes. + */ +public class DiskBalancerProtocolServer implements DiskBalancerProtocol { + + private final DatanodeStateMachine datanodeStateMachine; + private final PrivilegedOperation adminChecker; + + /** + * Functional interface for checking admin privileges before executing operations. + */ + @FunctionalInterface + public interface PrivilegedOperation { + + /** + * Check if the current user has admin privileges for the specified operation. + * @param operation the operation name to check privileges for + * @throws IOException if the privilege check fails or user is not authorized + */ + void check(String operation) throws IOException; + } + + public DiskBalancerProtocolServer(DatanodeStateMachine datanodeStateMachine, + PrivilegedOperation adminChecker) { + this.datanodeStateMachine = Objects.requireNonNull(datanodeStateMachine, "datanodeStateMachine"); + this.adminChecker = Objects.requireNonNull(adminChecker, "adminChecker"); + } + + @Override + public DatanodeDiskBalancerInfoProto getDiskBalancerInfo( + DatanodeDiskBalancerInfoType infoType, int clientVersion) throws IOException { + // No admin check - both report and status are read-only + DiskBalancerInfo info = getDiskBalancerInfo(); + DatanodeDetails datanodeDetails = datanodeStateMachine.getDatanodeDetails(); + + DatanodeDiskBalancerInfoProto.Builder builder = + DatanodeDiskBalancerInfoProto.newBuilder() + .setNode(datanodeDetails.toProto(clientVersion)) + .setCurrentVolumeDensitySum(info.getVolumeDataDensity()); + + // For STATUS, include additional details + if (infoType == DatanodeDiskBalancerInfoType.STATUS) { + // Build configuration proto + DiskBalancerConfigurationProto.Builder confBuilder = + DiskBalancerConfigurationProto.newBuilder() + .setThreshold(info.getThreshold()) + .setDiskBandwidthInMB(info.getBandwidthInMB()) + .setParallelThread(info.getParallelThread()) + .setStopAfterDiskEven(info.isStopAfterDiskEven()); + + builder.setDiskBalancerConf(confBuilder.build()) + .setSuccessMoveCount(info.getSuccessCount()) + .setFailureMoveCount(info.getFailureCount()) + .setBytesToMove(info.getBytesToMove()) + .setBytesMoved(info.getBalancedBytes()); + + // Map operational state to running status + DiskBalancerOperationalState state = info.getOperationalState(); + DiskBalancerRunningStatus runningStatus; + switch (state) { + case RUNNING: + runningStatus = DiskBalancerRunningStatus.RUNNING; + break; + case PAUSED_BY_NODE_STATE: Review Comment: Do you mean to unify `PAUSED_BY_NODE_STATE` and `UNKNOWN`? -- 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]
