slfan1989 commented on code in PR #7266: URL: https://github.com/apache/ozone/pull/7266#discussion_r2078909590
########## hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/datanode/VolumeSubCommand.java: ########## @@ -0,0 +1,222 @@ +/* + * 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.hdds.scm.cli.datanode; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import org.apache.commons.collections.CollectionUtils; +import org.apache.hadoop.hdds.cli.HddsVersionProvider; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos.VolumeInfoProto; +import org.apache.hadoop.hdds.protocol.proto.StorageContainerLocationProtocolProtos.GetVolumeInfosResponseProto; +import org.apache.hadoop.hdds.scm.cli.ScmSubcommand; +import org.apache.hadoop.hdds.scm.client.ScmClient; +import org.apache.hadoop.hdds.scm.datanode.VolumeInfo; +import org.apache.hadoop.hdds.server.JsonUtils; +import org.apache.hadoop.ozone.utils.FormattingCLIUtils; +import org.apache.hadoop.util.StringUtils; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + +/** + * We provide a set of volume commands to display the disk information of the DataNode. + */ +@Command( + name = "volumes", + description = "Display the list of volumes on the DataNode.", + mixinStandardHelpOptions = true, + versionProvider = HddsVersionProvider.class) +public class VolumeSubCommand extends ScmSubcommand { + + // Display it in JSON format. + @Option(names = { "--json" }, + defaultValue = "false", + description = "Format output as JSON.") + private boolean json; + + // Display it in TABLE format. + @Option(names = { "--table" }, + defaultValue = "false", + description = "Format output as Table.") + private boolean table; + + // PageSize refers to the number of items displayed per page + // in a paginated view. + @Option(names = { "--pageSize" }, + defaultValue = "20", + description = "The number of volume information items displayed per page.") + private int pageSize; + + // The current page. + @Option(names = { "--currentPage" }, + defaultValue = "1", + description = "The current page.") + private int currentPage; + + /** + * We have designed a new option called 'show', + * which includes two selectable configurations: + * 'failed' is used to display failed disks, + * and 'normal' is used to display normal disks. + */ + @Option(names = { "--displayMode" }, + defaultValue = "all", + description = "failed is used to display failed disks, " + + "normal is used to display normal disks.") + private String displayMode; + + // The UUID identifier of the DataNode. + @Option(names = { "--uuid" }, + defaultValue = "", + description = "failed is used to display failed disks, " + + "normal is used to display normal disks.") + private String uuid; + + // The HostName identifier of the DataNode. + @Option(names = { "--hostName" }, + defaultValue = "", + description = "failed is used to display failed disks, " + + "normal is used to display normal disks.") + private String hostName; + + private SimpleDateFormat sdf = new SimpleDateFormat( + "EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH); + + private static final String DATANODE_VOLUME_FAILURES_TITLE = "Datanode Volume"; + + private static final List<String> DATANODE_VOLUME_FAILURES_HEADER = Arrays.asList( + "UUID", "Host Name", "Volume Name", "Volume Status", "Capacity / Capacity Lost", + "Failure Time"); + + private final String[] validModes = {"all", "normal", "failed"}; + + @Override + public void execute(ScmClient client) throws IOException { + + validateDisplayMode(displayMode); + + // Retrieve the volume data based on the conditions. + GetVolumeInfosResponseProto response = + client.getVolumeInfos(displayMode, uuid, hostName, pageSize, currentPage); + + // Print the relevant information if the return value is empty. + if (response == null || CollectionUtils.isEmpty(response.getVolumeInfosList())) { + System.out.println("No volume data was retrieved."); + return; + } + + List<VolumeInfoProto> volumeInfosList = response.getVolumeInfosList(); + List<VolumeInfo> volumeInfos = convertToVolumeInfos(volumeInfosList); + + // If displayed in JSON format. + if (json) { + System.out.print(JsonUtils.toJsonStringWithDefaultPrettyPrinter(volumeInfos)); Review Comment: This is a good suggestion and I have improved this part of the code. -- 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]
