You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/06/18 14:33:56 UTC

[GitHub] houshengbo closed pull request #3776: Allow command runner to retry on network exit with custom retry

houshengbo closed pull request #3776: Allow command runner to retry on network exit with custom retry
URL: https://github.com/apache/incubator-openwhisk/pull/3776
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/scala/src/main/scala/whisk/utils/Retry.scala b/common/scala/src/main/scala/whisk/utils/Retry.scala
index 9ab3102dea..2931e71943 100644
--- a/common/scala/src/main/scala/whisk/utils/Retry.scala
+++ b/common/scala/src/main/scala/whisk/utils/Retry.scala
@@ -22,12 +22,13 @@ import scala.concurrent.duration._
 object retry {
 
   /**
-   * Retry a method which returns a value or throws an exception on failure, up to N times,
+   * Retries a method which returns a value or throws an exception on failure, up to N times,
    * and optionally sleeping up to specified duration between retries.
    *
-   * @param fn the method to retry, fn is expected to throw an exception if it fails, else should return a value of type T
+   * @param fn the function to retry; fn is expected to throw an exception if it fails, else should return a value of type T
    * @param N the maximum number of times to apply fn, must be >= 1
    * @param waitBeforeRetry an option specifying duration to wait before retrying method, will not wait if none given
+   * @param retryMessage an optional message to emit before retrying function
    * @return the result of fn iff it is successful
    * @throws Throwable exception from fn (or an illegal argument exception if N is < 1)
    */
diff --git a/tests/src/test/scala/common/TestUtils.java b/tests/src/test/scala/common/TestUtils.java
index d310ff46d4..a4e7a4bcaa 100644
--- a/tests/src/test/scala/common/TestUtils.java
+++ b/tests/src/test/scala/common/TestUtils.java
@@ -57,9 +57,9 @@
     public static final int SUCCESS_EXIT        = 0;
     public static final int ERROR_EXIT          = 1;
     public static final int MISUSE_EXIT         = 2;
+    public static final int NETWORK_ERROR_EXIT  = 3;
     public static final int DONTCARE_EXIT       = -1;       // any value is ok
     public static final int ANY_ERROR_EXIT      = -2;       // any non-zero value is ok
-    public static final int NETWORK_ERROR_EXIT  = 3;
 
     public static final int ACCEPTED        = 202;      // 202
     public static final int BAD_REQUEST     = 144;      // 400 - 256 = 144
diff --git a/tests/src/test/scala/common/Wsk.scala b/tests/src/test/scala/common/Wsk.scala
index 27ac636814..a335f03033 100644
--- a/tests/src/test/scala/common/Wsk.scala
+++ b/tests/src/test/scala/common/Wsk.scala
@@ -1032,32 +1032,22 @@ trait RunWskCmd extends BaseRunWsk {
           workingDir: File = new File("."),
           stdinFile: Option[File] = None,
           showCmd: Boolean = false,
-          hideFromOutput: Seq[String] = Seq()): RunResult = {
+          hideFromOutput: Seq[String] = Seq(),
+          retriesOnNetworkError: Int = 3): RunResult = {
     val args = baseCommand
     if (verbose) args += "--verbose"
     if (showCmd) println(args.mkString(" ") + " " + params.mkString(" "))
-    val rr =
-      retry(
-        {
-          val rr = TestUtils.runCmd(
-            DONTCARE_EXIT,
-            workingDir,
-            TestUtils.logger,
-            sys.env ++ env,
-            stdinFile.getOrElse(null),
-            args ++ params: _*)
-
-          if (expectedExitCode != NETWORK_ERROR_EXIT) {
-            withClue(hideStr(reportFailure(args ++ params, expectedExitCode, rr).toString(), hideFromOutput)) {
-              rr.exitCode should not be NETWORK_ERROR_EXIT
-            }
-          }
-
-          rr
-        },
-        3,
-        Some(1.second),
-        Some(s"CLI encountered a network error, retrying command..."))
+    val rr = retry(
+      0,
+      retriesOnNetworkError,
+      () =>
+        TestUtils.runCmd(
+          DONTCARE_EXIT,
+          workingDir,
+          TestUtils.logger,
+          sys.env ++ env,
+          stdinFile.getOrElse(null),
+          args ++ params: _*))
 
     withClue(hideStr(reportFailure(args ++ params, expectedExitCode, rr).toString(), hideFromOutput)) {
       if (expectedExitCode != TestUtils.DONTCARE_EXIT) {
@@ -1070,6 +1060,16 @@ trait RunWskCmd extends BaseRunWsk {
 
     rr
   }
+
+  /** Retries cmd on network error exit. */
+  private def retry(i: Int, N: Int, cmd: () => RunResult): RunResult = {
+    val rr = cmd()
+    if (rr.exitCode == NETWORK_ERROR_EXIT && i < N) {
+      Thread.sleep(1.second.toMillis)
+      println(s"command will retry to due to network error: $rr")
+      retry(i + 1, N, cmd)
+    } else rr
+  }
 }
 
 object WskAdmin {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services