Skip to content

Commit

Permalink
HDDS-11463. Fix CheckStyle.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanshilun committed Nov 7, 2024
1 parent d235dc5 commit 6a4a1e8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.hadoop.hdds.scm.cli.datanode;

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;
Expand All @@ -31,13 +32,13 @@

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Handler of ozone admin scm volumesfailure command.
* We provide a set of volume commands to display the disk information of the DataNode.
*/
@Command(
name = "volumes",
Expand All @@ -49,23 +50,26 @@ public class VolumeSubCommand extends ScmSubcommand {
// Display it in JSON format.
@Option(names = { "--json" },
defaultValue = "false",
description = "Format output as JSON")
description = "Format output as JSON.")
private boolean json;

// Display it in TABLE format.
@Option(names = { "--table" },
defaultValue = "false",
description = "Format output as Table")
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 = "Format output as Table")
description = "The number of volume information items displayed per page.")
private int pageSize;

// The current page.
@Option(names = { "--currentPage" },
defaultValue = "1",
description = "Format output as Table")
description = "The current page.")
private int currentPage;

/**
Expand All @@ -80,12 +84,14 @@ public class VolumeSubCommand extends ScmSubcommand {
"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, " +
Expand All @@ -98,49 +104,76 @@ public class VolumeSubCommand extends ScmSubcommand {
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", "Capacity / Capacity Lost", "Failure Time");
"UUID", "Host Name", "Volume Name", "Volume Status", "Capacity / Capacity Lost",
"Failure Time");

private final static String[] VALID_MODES = {"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));
return;
}

// If displayed in TABLE format.
if (table) {
FormattingCLIUtils formattingCLIUtils = new FormattingCLIUtils(DATANODE_VOLUME_FAILURES_TITLE)
.addHeaders(DATANODE_VOLUME_FAILURES_HEADER);
for (VolumeInfo info : volumeInfos) {
String capacity = StringUtils.byteDesc(info.getCapacity());
String failureTime = sdf.format(info.getFailureTime());
String volumeStatus = getVolumeStatus(info.isFailed());
String[] values = new String[]{info.getUuid(), info.getHostName(), info.getVolumeName(),
capacity, failureTime};
volumeStatus, capacity, failureTime};
formattingCLIUtils.addLine(values);
}
System.out.println(formattingCLIUtils.render());
return;
}

System.out.printf("Datanode Volume Failures (%d Volumes)%n%n", volumeInfos.size());
System.out.printf("Datanode Volume (%d Volumes)%n%n", volumeInfos.size());
volumeInfos.forEach(this::printInfo);
}

/**
* Print the information of the volume.
*
* @param info volume information.
*/
private void printInfo(VolumeInfo info) {
System.out.printf("%-13s: %s %n", "Uuid", info.getUuid());
System.out.printf("%-13s: %s %n", "Failed Volume", info.getVolumeName());
System.out.printf("%-13s: %s (%s) %n", "Capacity Lost", info.getCapacity()
System.out.printf("%-13s: %s %n", "HostName", info.getHostName());
System.out.printf("%-13s: %s %n", "Volume Status", getVolumeStatus(info.isFailed()));
System.out.printf("%-13s: %s %n", "Volume Name", info.getVolumeName());
System.out.printf("%-13s: %s (%s) %n", "Capacity / Capacity Lost", info.getCapacity()
+ " B", StringUtils.byteDesc(info.getCapacity()));
System.out.printf("%-13s: %s %n%n", "Failure Date", sdf.format(info.getFailureTime()));
System.out.printf("%-13s: %s %n%n", "Failure Time", sdf.format(info.getFailureTime()));
}


/**
* Convert the Protobuf object to a displayable object.
*
* @param volumeInfosList The list of volume information.
* @return volume list.
*/
private List<VolumeInfo> convertToVolumeInfos(List<VolumeInfoProto> volumeInfosList) {
List<VolumeInfo> volumeInfoList = new ArrayList<>();
for (VolumeInfoProto volumeInfoProto : volumeInfosList) {
Expand All @@ -149,4 +182,42 @@ private List<VolumeInfo> convertToVolumeInfos(List<VolumeInfoProto> volumeInfosL
}
return volumeInfoList;
}

/**
* Validate whether the displayMode meets the expected values,
* where displayMode must be one of the following: all, normal, or failed.
*
* @param displayMode
* The displayMode parameter determines how the volume is displayed.
*
* @throws IOException
* If the parameter is invalid, we will throw an exception.
*/
private void validateDisplayMode(String displayMode) throws IOException {

boolean isValid = false;
for (String validMode : VALID_MODES) {
if (validMode.equals(displayMode)) {
isValid = true;
break;
}
}

if (!isValid) {
throw new IOException("Invalid displayMode. " +
"It must be one of the following: 'all', 'normal', or 'failed'.");
}
}

/**
* Retrieve the display information of the disk.
*
* @param failed If it is true, it will display as FAILED;
* if it is false, it will display as Normal.
*
* @return Volume Status.
*/
private String getVolumeStatus(boolean failed) {
return !failed ? "NORMAL" : "FAILED";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* This unit test is used to verify whether the output of
* `TestVolumeFailureSubCommand` meets the expected results.
*/
public class TestVolumeFailureSubCommand {
public class TestVolumeCommand {
private VolumeSubCommand cmd;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
Expand Down Expand Up @@ -128,6 +128,7 @@ private GetVolumeInfosResponseProto getUsageProto() {
new VolumeInfo.Builder().
setHostName(hostName).
setUuid(uuId).
setFailed(true).
setFailureTime(Time.now()).
setVolumeName("/data" + i + "/ozonedata/hdds").
setCapacity(7430477791683L).
Expand Down

0 comments on commit 6a4a1e8

Please sign in to comment.