[
https://issues.apache.org/jira/browse/HDDS-692?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16677190#comment-16677190
]
Nanda kumar commented on HDDS-692:
----------------------------------
[~horzsolt2006], the ProgressBar code can be refactored like below
{code:java}
/**
* Creates and runs a ProgressBar in new Thread which gets printed on
* the provided PrintStream.
*/
public class ProgressBar {
private static final Logger LOG = LoggerFactory.getLogger(ProgressBar.class);
private static final long REFRESH_INTERVAL = 1000L;
private final long maxValue;
private final Supplier<Long> currentValue;
private final Thread progressBar;
private volatile boolean running;
private long startTime;
/**
* Creates a new ProgressBar instance which prints the progress on the given
* PrintStream when started.
*
* @param stream to display the progress
* @param maxValue Maximum value of the progress
* @param currentValue Supplier that provides the current value
*/
public ProgressBar(final PrintStream stream, final Long maxValue,
final Supplier<Long> currentValue) {
this.maxValue = maxValue;
this.currentValue = currentValue;
this.progressBar = new Thread(getProgressBar(stream));
this.running = false;
}
/**
* Starts the ProgressBar in a new Thread.
* This is a non blocking call.
*/
public synchronized void start() {
if (!running) {
running = true;
startTime = System.nanoTime();
progressBar.start();
}
}
/**
* Graceful shutdown, waits for the progress bar to complete.
* This is a blocking call.
*/
public synchronized void shutdown() {
if (running) {
try {
progressBar.join();
running = false;
} catch (InterruptedException e) {
LOG.warn("Got interrupted while waiting for the progress bar to " +
"complete.");
}
}
}
/**
* Terminates the progress bar. This doesn't wait for the progress bar
* to complete.
*/
public synchronized void terminate() {
if (running) {
try {
running = false;
progressBar.join();
} catch (InterruptedException e) {
LOG.warn("Got interrupted while waiting for the progress bar to " +
"complete.");
}
}
}
private Runnable getProgressBar(final PrintStream stream) {
return () -> {
stream.println();
while (running && currentValue.get() < maxValue) {
print(stream, currentValue.get());
try {
Thread.sleep(REFRESH_INTERVAL);
} catch (InterruptedException e) {
LOG.warn("ProgressBar was interrupted.");
}
}
print(stream, maxValue);
stream.println();
running = false;
};
}
/**
* Given current value prints the progress bar.
*
* @param value current progress position
*/
private void print(final PrintStream stream, final long value) {
stream.print('\r');
double percent = 100.0 * value / maxValue;
StringBuilder sb = new StringBuilder();
sb.append(" " + String.format("%.2f", percent) + "% |");
for (int i = 0; i <= percent; i++) {
sb.append('█');
}
for (int j = 0; j < 100 - percent; j++) {
sb.append(' ');
}
sb.append("| ");
sb.append(value + "/" + maxValue);
long timeInSec = TimeUnit.SECONDS.convert(
System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
String timeToPrint = String.format("%d:%02d:%02d", timeInSec / 3600,
(timeInSec % 3600) / 60, timeInSec % 60);
sb.append(" Time: " + timeToPrint);
stream.print(sb.toString());
}
}
{code}
> Use the ProgressBar class in the RandomKeyGenerator freon test
> --------------------------------------------------------------
>
> Key: HDDS-692
> URL: https://issues.apache.org/jira/browse/HDDS-692
> Project: Hadoop Distributed Data Store
> Issue Type: Bug
> Components: Tools
> Reporter: Elek, Marton
> Assignee: Zsolt Horvath
> Priority: Major
> Attachments: HDDS-692.001.patch, HDDS-692.002.patch,
> HDDS-692.003.patch
>
>
> HDDS-443 provides a reusable progress bar to make it easier to add more freon
> tests, but the existing RandomKeyGenerator test
> (hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/freon/RandomKeyGenerator.java)
> still doesn't use it.
> It would be good to switch to use the new progress bar there.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]