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> <groupId>org.jline</groupId>
<artifactId>jline</artifactId> <artifactId>jline</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -26,7 +26,6 @@ import java.text.DecimalFormat;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.blinkfox.minitable.MiniTable;
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.HelpFormatter;
@ -224,17 +223,18 @@ public class QueueCLI extends YarnCLI {
} }
private void printQueueInfos(PrintWriter writer, List<QueueInfo> queueInfos) { private void printQueueInfos(PrintWriter writer, List<QueueInfo> queueInfos) {
FormattingCLIUtils formattingCLIUtils = new FormattingCLIUtils(queueInfos.size() + " queues were found") FormattingCLIUtils formattingCLIUtils =
.addHeaders("Queue Name", "Queue Path", "State", "Capacity", new FormattingCLIUtils(queueInfos.size() + " queues were found")
"Current Capacity", "Maximum Capacity", "Weight", "Maximum Parallel Apps"); .addHeaders("Queue Name", "Queue Path", "State", "Capacity"
, "Current Capacity", "Maximum Capacity", "Weight", "Maximum Parallel Apps");
DecimalFormat df = new DecimalFormat("#.00"); DecimalFormat df = new DecimalFormat("#.00");
for (QueueInfo queueInfo : queueInfos) { for (QueueInfo queueInfo : queueInfos) {
formattingCLIUtils.addDatas(queueInfo.getQueueName(),queueInfo.getQueuePath() formattingCLIUtils.addDatas(queueInfo.getQueueName(), queueInfo.getQueuePath()
,queueInfo.getQueueState(),df.format(queueInfo.getCapacity() * 100) + "%" , queueInfo.getQueueState(), df.format(queueInfo.getCapacity() * 100) + "%"
,df.format(queueInfo.getCurrentCapacity() * 100) + "%" , df.format(queueInfo.getCurrentCapacity() * 100) + "%"
,df.format(queueInfo.getMaximumCapacity() * 100) + "%" , df.format(queueInfo.getMaximumCapacity() * 100) + "%"
,df.format(queueInfo.getWeight()) , df.format(queueInfo.getWeight())
,queueInfo.getMaxParallelApps()); , queueInfo.getMaxParallelApps());
} }
writer.print(formattingCLIUtils.render()); 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; package org.apache.hadoop.yarn.client.util;
import java.util.ArrayList; import java.util.ArrayList;
@ -6,53 +23,41 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public final class FormattingCLIUtils { public final class FormattingCLIUtils {
private String title; private String title;
private TableRowType lastTableRowType; private TableRowType lastTableRowType;
private StringBuilder join; private StringBuilder join;
private List<TableRow> tableRows; private List<TableRow> tableRows;
private Map<Integer, Integer> maxColMap; private Map<Integer, Integer> maxColMap;
public FormattingCLIUtils() { public FormattingCLIUtils() {
this.init(); this.init();
} }
public FormattingCLIUtils(String title) { public FormattingCLIUtils(String title) {
this.init(); this.init();
this.title = title; this.title = title;
} }
private void init() { private void init() {
this.join = new StringBuilder(); this.join = new StringBuilder();
this.tableRows = new ArrayList<>(); this.tableRows = new ArrayList<>();
this.maxColMap = new HashMap<>(); this.maxColMap = new HashMap<>();
} }
public FormattingCLIUtils addHeaders(List<?> headers) { public FormattingCLIUtils addHeaders(List<?> headers) {
return this.appendRows(TableRowType.HEADER, headers.toArray()); return this.appendRows(TableRowType.HEADER, headers.toArray());
} }
public FormattingCLIUtils addHeaders(Object... objects) { public FormattingCLIUtils addHeaders(Object... objects) {
return this.appendRows(TableRowType.HEADER, objects); return this.appendRows(TableRowType.HEADER, objects);
} }
public FormattingCLIUtils addDatas(List<?> datas) { public FormattingCLIUtils addDatas(List<?> datas) {
return this.appendRows(TableRowType.DATA, datas.toArray()); return this.appendRows(TableRowType.DATA, datas.toArray());
} }
public FormattingCLIUtils addDatas(Object... objects) { public FormattingCLIUtils addDatas(Object... objects) {
return this.appendRows(TableRowType.DATA, objects); return this.appendRows(TableRowType.DATA, objects);
} }
private FormattingCLIUtils appendRows(TableRowType tableRowType, Object... objects) { private FormattingCLIUtils appendRows(TableRowType tableRowType, Object... objects) {
int len; int len = objects.length;
if (objects != null && (len = objects.length) > 0) { if (objects != null && len > 0) {
if (this.maxColMap.size() > len) { 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<>(); List<String> datas = new ArrayList<>();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@ -72,7 +77,6 @@ public final class FormattingCLIUtils {
} }
return this; return this;
} }
private void buildTitle() { private void buildTitle() {
if (this.title != null) { if (this.title != null) {
int maxTitleSize = 0; int maxTitleSize = 0;
@ -80,11 +84,9 @@ public final class FormattingCLIUtils {
maxTitleSize += maxColSize; maxTitleSize += maxColSize;
} }
maxTitleSize += 3 * (this.maxColMap.size() - 1); maxTitleSize += 3 * (this.maxColMap.size() - 1);
if (this.title.length() > maxTitleSize) { if (this.title.length() > maxTitleSize) {
this.title = this.title.substring(0, maxTitleSize); this.title = this.title.substring(0, maxTitleSize);
} }
this.join.append("+"); this.join.append("+");
for (int i = 0; i < maxTitleSize + 2; i++) { for (int i = 0; i < maxTitleSize + 2; i++) {
this.join.append("-"); this.join.append("-");
@ -93,36 +95,32 @@ public final class FormattingCLIUtils {
.append("|") .append("|")
.append(StrUtils.center(this.title, maxTitleSize + 2, ' ')) .append(StrUtils.center(this.title, maxTitleSize + 2, ' '))
.append("|\n"); .append("|\n");
this.lastTableRowType = TableRowType.TITLE; this.lastTableRowType = TableRowType.TITLE;
} }
} }
private void buildTable() { private void buildTable() {
this.buildTitle(); this.buildTitle();
for (int i = 0, len = this.tableRows.size(); i < len; i++) { for (int i = 0, len = this.tableRows.size(); i < len; i++) {
List<String> datas = this.tableRows.get(i).datas; List<String> datas = this.tableRows.get(i).datas;
switch (this.tableRows.get(i).tableRowType) { switch (this.tableRows.get(i).tableRowType) {
case HEADER: case HEADER:
if (this.lastTableRowType != TableRowType.HEADER) { if (this.lastTableRowType != TableRowType.HEADER) {
this.buildRowBorder(datas);
}
this.buildRowData(datas);
this.buildRowBorder(datas); this.buildRowBorder(datas);
break; }
this.buildRowData(datas);
this.buildRowBorder(datas);
break;
case DATA: case DATA:
this.buildRowData(datas); this.buildRowData(datas);
if (i == len - 1) { if (i == len - 1) {
this.buildRowBorder(datas); this.buildRowBorder(datas);
} }
break; break;
default: default:
break; break;
} }
} }
} }
private void buildRowBorder(List<String> datas) { private void buildRowBorder(List<String> datas) {
this.join.append("+"); this.join.append("+");
for (int i = 0, len = datas.size(); i < len; i++) { for (int i = 0, len = datas.size(); i < len; i++) {
@ -133,7 +131,6 @@ public final class FormattingCLIUtils {
} }
this.join.append("\n"); this.join.append("\n");
} }
private void buildRowData(List<String> datas) { private void buildRowData(List<String> datas) {
this.join.append("|"); this.join.append("|");
for (int i = 0, len = datas.size(); i < len; i++) { for (int i = 0, len = datas.size(); i < len; i++) {
@ -142,12 +139,10 @@ public final class FormattingCLIUtils {
} }
this.join.append("\n"); this.join.append("\n");
} }
public String render() { public String render() {
this.buildTable(); this.buildTable();
return this.join.toString(); return this.join.toString();
} }
private static class TableRow { private static class TableRow {
private TableRowType tableRowType; private TableRowType tableRowType;
private List<String> datas; private List<String> datas;
@ -156,13 +151,10 @@ public final class FormattingCLIUtils {
this.datas = datas; this.datas = datas;
} }
} }
private enum TableRowType { private enum TableRowType {
TITLE, HEADER, DATA TITLE, HEADER, DATA
} }
private static final class StrUtils { private static final class StrUtils {
private static String center(String str, int size, char padChar) { private static String center(String str, int size, char padChar) {
if (str != null && size > 0) { if (str != null && size > 0) {
int strLen = str.length(); int strLen = str.length();
@ -174,17 +166,14 @@ public final class FormattingCLIUtils {
} }
return str; return str;
} }
private static String leftPad(final String str, int size, char padChar) { private static String leftPad(final String str, int size, char padChar) {
int pads = size - str.length(); int pads = size - str.length();
return pads <= 0 ? str : repeat(padChar, pads).concat(str); return pads <= 0 ? str : repeat(padChar, pads).concat(str);
} }
private static String rightPad(final String str, int size, char padChar) { private static String rightPad(final String str, int size, char padChar) {
int pads = size - str.length(); int pads = size - str.length();
return pads <= 0 ? str : str.concat(repeat(padChar, pads)); return pads <= 0 ? str : str.concat(repeat(padChar, pads));
} }
private static String repeat(char ch, int repeat) { private static String repeat(char ch, int repeat) {
char[] buf = new char[repeat]; char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) { for (int i = repeat - 1; i >= 0; i--) {

View File

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