YARN-11506.The formatted yarn queue list is displayed on the command line

This commit is contained in:
yl09099 2023-06-09 17:04:36 +08:00
parent 7d4f476c1b
commit fd5de598b1
4 changed files with 51 additions and 62 deletions

View File

@ -175,7 +175,6 @@
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -26,7 +26,6 @@ import java.text.DecimalFormat;
import java.util.List;
import java.util.Set;
import com.blinkfox.minitable.MiniTable;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
@ -224,17 +223,18 @@ public class QueueCLI extends YarnCLI {
}
private void printQueueInfos(PrintWriter writer, List<QueueInfo> queueInfos) {
FormattingCLIUtils formattingCLIUtils = new FormattingCLIUtils(queueInfos.size() + " queues were found")
.addHeaders("Queue Name", "Queue Path", "State", "Capacity",
"Current Capacity", "Maximum Capacity", "Weight", "Maximum Parallel Apps");
FormattingCLIUtils formattingCLIUtils =
new FormattingCLIUtils(queueInfos.size() + " queues were found")
.addHeaders("Queue Name", "Queue Path", "State", "Capacity"
, "Current Capacity", "Maximum Capacity", "Weight", "Maximum Parallel Apps");
DecimalFormat df = new DecimalFormat("#.00");
for (QueueInfo queueInfo : queueInfos) {
formattingCLIUtils.addDatas(queueInfo.getQueueName(),queueInfo.getQueuePath()
,queueInfo.getQueueState(),df.format(queueInfo.getCapacity() * 100) + "%"
,df.format(queueInfo.getCurrentCapacity() * 100) + "%"
,df.format(queueInfo.getMaximumCapacity() * 100) + "%"
,df.format(queueInfo.getWeight())
,queueInfo.getMaxParallelApps());
formattingCLIUtils.addDatas(queueInfo.getQueueName(), queueInfo.getQueuePath()
, queueInfo.getQueueState(), df.format(queueInfo.getCapacity() * 100) + "%"
, df.format(queueInfo.getCurrentCapacity() * 100) + "%"
, df.format(queueInfo.getMaximumCapacity() * 100) + "%"
, df.format(queueInfo.getWeight())
, queueInfo.getMaxParallelApps());
}
writer.print(formattingCLIUtils.render());
}

View File

@ -1,3 +1,20 @@
/**
* 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.yarn.client.util;
import java.util.ArrayList;
@ -6,53 +23,41 @@ import java.util.List;
import java.util.Map;
public final class FormattingCLIUtils {
private String title;
private TableRowType lastTableRowType;
private StringBuilder join;
private List<TableRow> tableRows;
private Map<Integer, Integer> maxColMap;
public FormattingCLIUtils() {
this.init();
}
public FormattingCLIUtils(String title) {
this.init();
this.title = title;
}
private void init() {
this.join = new StringBuilder();
this.tableRows = new ArrayList<>();
this.maxColMap = new HashMap<>();
}
public FormattingCLIUtils addHeaders(List<?> headers) {
return this.appendRows(TableRowType.HEADER, headers.toArray());
}
public FormattingCLIUtils addHeaders(Object... objects) {
return this.appendRows(TableRowType.HEADER, objects);
}
public FormattingCLIUtils addDatas(List<?> datas) {
return this.appendRows(TableRowType.DATA, datas.toArray());
}
public FormattingCLIUtils addDatas(Object... objects) {
return this.appendRows(TableRowType.DATA, objects);
}
private FormattingCLIUtils appendRows(TableRowType tableRowType, Object... objects) {
int len;
if (objects != null && (len = objects.length) > 0) {
int len = objects.length;
if (objects != null && len > 0) {
if (this.maxColMap.size() > len) {
throw new IllegalArgumentException("The number of columns that inserted a row of data into the table is different from the number of previous columns, check!");
throw new IllegalArgumentException("The number of columns that inserted a row " +
"of data into the table is different from the number of previous columns, check!");
}
List<String> datas = new ArrayList<>();
for (int i = 0; i < len; i++) {
@ -72,7 +77,6 @@ public final class FormattingCLIUtils {
}
return this;
}
private void buildTitle() {
if (this.title != null) {
int maxTitleSize = 0;
@ -80,11 +84,9 @@ public final class FormattingCLIUtils {
maxTitleSize += maxColSize;
}
maxTitleSize += 3 * (this.maxColMap.size() - 1);
if (this.title.length() > maxTitleSize) {
this.title = this.title.substring(0, maxTitleSize);
}
this.join.append("+");
for (int i = 0; i < maxTitleSize + 2; i++) {
this.join.append("-");
@ -93,36 +95,32 @@ public final class FormattingCLIUtils {
.append("|")
.append(StrUtils.center(this.title, maxTitleSize + 2, ' '))
.append("|\n");
this.lastTableRowType = TableRowType.TITLE;
}
}
private void buildTable() {
this.buildTitle();
for (int i = 0, len = this.tableRows.size(); i < len; i++) {
List<String> datas = this.tableRows.get(i).datas;
switch (this.tableRows.get(i).tableRowType) {
case HEADER:
if (this.lastTableRowType != TableRowType.HEADER) {
this.buildRowBorder(datas);
}
this.buildRowData(datas);
case HEADER:
if (this.lastTableRowType != TableRowType.HEADER) {
this.buildRowBorder(datas);
break;
}
this.buildRowData(datas);
this.buildRowBorder(datas);
break;
case DATA:
this.buildRowData(datas);
if (i == len - 1) {
this.buildRowBorder(datas);
}
break;
this.buildRowData(datas);
if (i == len - 1) {
this.buildRowBorder(datas);
}
break;
default:
break;
break;
}
}
}
private void buildRowBorder(List<String> datas) {
this.join.append("+");
for (int i = 0, len = datas.size(); i < len; i++) {
@ -133,7 +131,6 @@ public final class FormattingCLIUtils {
}
this.join.append("\n");
}
private void buildRowData(List<String> datas) {
this.join.append("|");
for (int i = 0, len = datas.size(); i < len; i++) {
@ -142,12 +139,10 @@ public final class FormattingCLIUtils {
}
this.join.append("\n");
}
public String render() {
this.buildTable();
return this.join.toString();
}
private static class TableRow {
private TableRowType tableRowType;
private List<String> datas;
@ -156,13 +151,10 @@ public final class FormattingCLIUtils {
this.datas = datas;
}
}
private enum TableRowType {
TITLE, HEADER, DATA
}
private static final class StrUtils {
private static String center(String str, int size, char padChar) {
if (str != null && size > 0) {
int strLen = str.length();
@ -174,17 +166,14 @@ public final class FormattingCLIUtils {
}
return str;
}
private static String leftPad(final String str, int size, char padChar) {
int pads = size - str.length();
return pads <= 0 ? str : repeat(padChar, pads).concat(str);
}
private static String rightPad(final String str, int size, char padChar) {
int pads = size - str.length();
return pads <= 0 ? str : str.concat(repeat(padChar, pads));
}
private static String repeat(char ch, int repeat) {
char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {

View File

@ -1775,17 +1775,18 @@ public class TestYarnCLI {
verify(client).getAllQueues();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(baos);
FormattingCLIUtils formattingCLIUtils = new FormattingCLIUtils(queueInfos.size() + " queues were found")
FormattingCLIUtils formattingCLIUtils =
new FormattingCLIUtils(queueInfos.size() + " queues were found")
.addHeaders("Queue Name", "Queue Path", "State", "Capacity",
"Current Capacity", "Maximum Capacity", "Weight", "Maximum Parallel Apps");
DecimalFormat df = new DecimalFormat("#.00");
for (QueueInfo queueInfoe : queueInfos) {
formattingCLIUtils.addDatas(queueInfoe.getQueueName(),queueInfoe.getQueuePath()
,queueInfoe.getQueueState(),df.format(queueInfoe.getCapacity() * 100) + "%"
,df.format(queueInfoe.getCurrentCapacity() * 100) + "%"
,df.format(queueInfoe.getMaximumCapacity() * 100) + "%"
,df.format(queueInfoe.getWeight())
,queueInfoe.getMaxParallelApps());
formattingCLIUtils.addDatas(queueInfoe.getQueueName(), queueInfoe.getQueuePath()
, queueInfoe.getQueueState(), df.format(queueInfoe.getCapacity() * 100) + "%"
, df.format(queueInfoe.getCurrentCapacity() * 100) + "%"
, df.format(queueInfoe.getMaximumCapacity() * 100) + "%"
, df.format(queueInfoe.getWeight())
, queueInfoe.getMaxParallelApps());
}
writer.print(formattingCLIUtils.render());
writer.close();