You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vk...@apache.org on 2020/12/31 19:58:15 UTC

[ignite-3] branch main updated: IGNITE-13938 - Fixed progress bar

This is an automated email from the ASF dual-hosted git repository.

vkulichenko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 51ddaba  IGNITE-13938 - Fixed progress bar
51ddaba is described below

commit 51ddabab6d350101c7fef43fe182a3d01f363b45
Author: Valentin Kulichenko <va...@gmail.com>
AuthorDate: Thu Dec 31 11:57:33 2020 -0800

    IGNITE-13938 - Fixed progress bar
---
 modules/cli/pom.xml                                |  6 --
 .../progressbar/IgniteProgressBarRenderer.java     | 54 --------------
 .../org/apache/ignite/cli/IgniteProgressBar.java   | 82 +++++++++++++++-------
 .../cli/builtins/module/MavenArtifactResolver.java |  2 +-
 .../ignite/cli/builtins/node/NodeManager.java      |  7 +-
 .../apache/ignite/cli/spec/NodeCommandSpec.java    |  3 +-
 .../apache/ignite/cli/IgniteCliInterfaceTest.java  |  9 +--
 7 files changed, 68 insertions(+), 95 deletions(-)

diff --git a/modules/cli/pom.xml b/modules/cli/pom.xml
index b36ba80..e79a94f 100644
--- a/modules/cli/pom.xml
+++ b/modules/cli/pom.xml
@@ -109,12 +109,6 @@
             <artifactId>config</artifactId>
             <version>1.4.1</version>
         </dependency>
-
-        <dependency>
-            <groupId>me.tongfei</groupId>
-            <artifactId>progressbar</artifactId>
-            <version>0.9.0</version>
-        </dependency>
     </dependencies>
 
     <build>
diff --git a/modules/cli/src/main/java/me/tongfei/progressbar/IgniteProgressBarRenderer.java b/modules/cli/src/main/java/me/tongfei/progressbar/IgniteProgressBarRenderer.java
deleted file mode 100644
index 95133c3..0000000
--- a/modules/cli/src/main/java/me/tongfei/progressbar/IgniteProgressBarRenderer.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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 me.tongfei.progressbar;
-
-import picocli.CommandLine.Help.Ansi;
-
-/**
- * Custom renderer for the {@link ProgressBar}.
- *
- * Located in the {@code me.tongfei.progressbar} package because
- * the required {@link ProgressState} class is package-private.
- */
-public class IgniteProgressBarRenderer implements ProgressBarRenderer {
-    @Override public String render(ProgressState progress, int maxLength) {
-        int completed = (int)(progress.getNormalizedProgress() * 100);
-
-        StringBuilder sb = new StringBuilder("|");
-
-        sb.append("=".repeat(completed));
-
-        String percentage;
-        int percentageLen;
-
-        if (completed < 100) {
-            sb.append('>').append(" ".repeat(99 - completed));
-
-            percentage = completed + "%";
-            percentageLen = percentage.length();
-        }
-        else {
-            percentage = "@|green,bold Done!|@";
-            percentageLen = 5;//percentageText.getCJKAdjustedLength();
-        }
-
-        sb.append("|").append(" ".repeat(6 - percentageLen)).append(percentage);
-
-        return Ansi.AUTO.string(sb.toString());
-    }
-}
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/IgniteProgressBar.java b/modules/cli/src/main/java/org/apache/ignite/cli/IgniteProgressBar.java
index 2375c94..572fd21 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/IgniteProgressBar.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/IgniteProgressBar.java
@@ -17,20 +17,21 @@
 
 package org.apache.ignite.cli;
 
-import java.time.Duration;
+import java.io.PrintWriter;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
-import me.tongfei.progressbar.ConsoleProgressBarConsumer;
-import me.tongfei.progressbar.IgniteProgressBarRenderer;
-import me.tongfei.progressbar.ProgressBar;
+import picocli.CommandLine.Help.Ansi;
 
 /**
- * Basic implementation of a progress bar. Based on
- * <a href="https://github.com/ctongfei/progressbar">https://github.com/ctongfei/progressbar</a>.
+ * Basic implementation of a progress bar.
  */
 public class IgniteProgressBar implements AutoCloseable {
-    private final ProgressBar impl;
+    private final PrintWriter out;
+
+    private int current;
+
+    private int max;
 
     private ScheduledExecutorService exec;
 
@@ -39,23 +40,23 @@ public class IgniteProgressBar implements AutoCloseable {
      *
      * @param initialMax Initial maximum number of steps.
      */
-    public IgniteProgressBar(long initialMax) {
-        impl = new ProgressBar(
-            null,
-            initialMax,
-            10,
-            0,
-            Duration.ZERO,
-            new IgniteProgressBarRenderer(),
-            new ConsoleProgressBarConsumer(System.out, 150)
-        );
+    public IgniteProgressBar(PrintWriter out, int initialMax) {
+        this.out = out;
+
+        assert initialMax > 0;
+
+        max = initialMax;
     }
 
     /**
      * Performs a single step.
      */
     public void step() {
-        impl.step();
+        if (current < max)
+            current++;
+
+        out.print('\r' + render());
+        out.flush();
     }
 
     /**
@@ -63,11 +64,12 @@ public class IgniteProgressBar implements AutoCloseable {
      *
      * @param interval Interval in milliseconds.
      */
-    public synchronized void stepPeriodically(long interval) {
-        if (exec == null)
+    public void stepPeriodically(long interval) {
+        if (exec == null) {
             exec = Executors.newSingleThreadScheduledExecutor();
 
-        exec.scheduleAtFixedRate(impl::step, interval, interval, TimeUnit.MILLISECONDS);
+            exec.scheduleAtFixedRate(this::step, interval, interval, TimeUnit.MILLISECONDS);
+        }
     }
 
     /**
@@ -75,12 +77,14 @@ public class IgniteProgressBar implements AutoCloseable {
      *
      * @param newMax New maximum.
      */
-    public void setMax(long newMax) {
-        impl.maxHint(newMax);
+    public void setMax(int newMax) {
+        assert newMax > 0;
+
+        max = newMax;
     }
 
     @Override public void close() {
-        while (impl.getCurrent() < impl.getMax()) {
+        while (current < max) {
             try {
                 Thread.sleep(10);
             }
@@ -91,6 +95,34 @@ public class IgniteProgressBar implements AutoCloseable {
             step();
         }
 
-        impl.close();
+        out.println();
+    }
+
+    private String render() {
+        assert current <= max;
+
+        int completed = (int)((double)current / (double)max * 100);
+
+        StringBuilder sb = new StringBuilder("|");
+
+        sb.append("=".repeat(completed));
+
+        String percentage;
+        int percentageLen;
+
+        if (completed < 100) {
+            sb.append('>').append(" ".repeat(99 - completed));
+
+            percentage = completed + "%";
+            percentageLen = percentage.length();
+        }
+        else {
+            percentage = "@|green,bold Done!|@";
+            percentageLen = 5;
+        }
+
+        sb.append("|").append(" ".repeat(6 - percentageLen)).append(percentage);
+
+        return Ansi.AUTO.string(sb.toString());
     }
 }
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/module/MavenArtifactResolver.java b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/module/MavenArtifactResolver.java
index 8f2cc47..15e3883 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/module/MavenArtifactResolver.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/module/MavenArtifactResolver.java
@@ -85,7 +85,7 @@ public class MavenArtifactResolver {
 
         out.println("Installing " + String.join(":", grpId, artifactId, version) + "...");
 
-        try (IgniteProgressBar bar = new IgniteProgressBar(100)) {
+        try (IgniteProgressBar bar = new IgniteProgressBar(out, 100)) {
             ivy.getEventManager().addIvyListener(event -> {
                 if (event instanceof EndResolveEvent) {
                     int count = ((EndResolveEvent)event).getReport().getArtifacts().size();
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java
index 9c0934d..9b5d4e2 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/builtins/node/NodeManager.java
@@ -19,6 +19,7 @@ package org.apache.ignite.cli.builtins.node;
 
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.time.Duration;
@@ -48,9 +49,7 @@ public class NodeManager {
         this.moduleStorage = moduleStorage;
     }
 
-    public RunningNode start(String consistentId,
-        Path workDir, Path pidsDir, Path serverConfig) {
-
+    public RunningNode start(String consistentId, Path workDir, Path pidsDir, Path serverConfig, PrintWriter out) {
         if (getRunningNodes(workDir, pidsDir).stream().anyMatch(n -> n.consistentId.equals(consistentId)))
             throw new IgniteCLIException("Node with consistentId " + consistentId + " is already exist");
         try {
@@ -80,7 +79,7 @@ public class NodeManager {
 
             Process p = pb.start();
 
-            try (var bar = new IgniteProgressBar(100)) {
+            try (var bar = new IgniteProgressBar(out, 100)) {
                 bar.stepPeriodically(300);
 
                 if (!waitForStart("Apache Ignite started successfully!", logFile, NODE_START_TIMEOUT)) {
diff --git a/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java b/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java
index 8903dee..a6280e6 100644
--- a/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java
+++ b/modules/cli/src/main/java/org/apache/ignite/cli/spec/NodeCommandSpec.java
@@ -65,7 +65,8 @@ public class NodeCommandSpec extends CategorySpec {
 
             NodeManager.RunningNode node = nodeManager.start(consistentId, ignitePaths.workDir,
                 ignitePaths.cliPidsDir(),
-                configPath);
+                configPath,
+                out);
 
             out.println();
             out.println("Node is successfully started. To stop, type " +
diff --git a/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java b/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java
index 0709570..17a114b 100644
--- a/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java
+++ b/modules/cli/src/test/java/org/apache/ignite/cli/IgniteCliInterfaceTest.java
@@ -242,17 +242,18 @@ public class IgniteCliInterfaceTest {
            var nodeName = "node1";
            var node =
                new NodeManager.RunningNode(1, nodeName, Path.of("logfile"));
-           when(nodeManager.start(any(), any(), any(), any()))
+           when(nodeManager.start(any(), any(), any(), any(), any()))
                .thenReturn(node);
 
             when(cliPathsConfigLoader.loadIgnitePathsOrThrowError())
                 .thenReturn(ignitePaths);
 
-            var exitCode =
-                commandLine(applicationContext).execute(("node start " + nodeName + " --config conf.json").split(" "));
+            CommandLine cli = commandLine(applicationContext);
+
+            var exitCode = cli.execute(("node start " + nodeName + " --config conf.json").split(" "));
 
             assertEquals(0, exitCode);
-            verify(nodeManager).start(nodeName, ignitePaths.workDir, ignitePaths.cliPidsDir(), Path.of("conf.json"));
+            verify(nodeManager).start(nodeName, ignitePaths.workDir, ignitePaths.cliPidsDir(), Path.of("conf.json"), cli.getOut());
             assertEquals("Starting a new Ignite node...\n\nNode is successfully started. To stop, type ignite node stop " + nodeName + "\n\n" +
                 "+---------------+---------+\n" +
                 "| Consistent ID | node1   |\n" +