From 1b8cf02eb09e2f2c30ea4043b5b499fbdb6bb402 Mon Sep 17 00:00:00 2001
From: Jakob Buchgraber <buchgr@google.com>
Date: Thu, 21 Mar 2019 09:22:29 -0700
Subject: [PATCH] remote: small refactoring that splits download and spawn
 result

Closes #7792.

PiperOrigin-RevId: 239608769
---
 .../build/lib/remote/RemoteSpawnRunner.java   | 25 ++++++++-----------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnRunner.java
index edc5088ec8f9ed..9b489d1f6518b3 100644
--- a/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnRunner.java
+++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnRunner.java
@@ -196,10 +196,8 @@ public SpawnResult exec(Spawn spawn, SpawnExecutionContext context)
             acceptCachedResult = false;
           } else {
             try (SilentCloseable c = Profiler.instance().profile("Remote.downloadRemoteResults")) {
-              return downloadRemoteResults(cachedResult, context.getFileOutErr())
-                  .setCacheHit(true)
-                  .setRunnerName("remote cache hit")
-                  .build();
+              remoteCache.download(cachedResult, execRoot, context.getFileOutErr());
+              return createSpawnResult(cachedResult.getExitCode(), /* cacheHit= */ true);
             } catch (CacheNotFoundException e) {
               // No cache hit, so we fall through to local or remote execution.
               // We set acceptCachedResult to false in order to force the action re-execution.
@@ -251,7 +249,8 @@ public SpawnResult exec(Spawn spawn, SpawnExecutionContext context)
 
               FileOutErr outErr = context.getFileOutErr();
               String message = reply.getMessage();
-              if ((reply.getResult().getExitCode() != 0
+              ActionResult actionResult = reply.getResult();
+              if ((actionResult.getExitCode() != 0
                       || reply.getStatus().getCode() != Code.OK.value())
                   && !message.isEmpty()) {
                 outErr.printErr(message + "\n");
@@ -264,11 +263,9 @@ public SpawnResult exec(Spawn spawn, SpawnExecutionContext context)
 
               try (SilentCloseable c =
                   Profiler.instance().profile("Remote.downloadRemoteResults")) {
-                return downloadRemoteResults(reply.getResult(), outErr)
-                    .setRunnerName(reply.getCachedResult() ? "remote cache hit" : getName())
-                    .setCacheHit(reply.getCachedResult())
-                    .build();
+                remoteCache.download(actionResult, execRoot, outErr);
               }
+              return createSpawnResult(actionResult.getExitCode(), reply.getCachedResult());
             });
       } catch (IOException e) {
         return execLocallyAndUploadOrFail(
@@ -330,13 +327,13 @@ private void maybeDownloadServerLogs(ExecuteResponse resp, ActionKey actionKey)
     }
   }
 
-  private SpawnResult.Builder downloadRemoteResults(ActionResult result, FileOutErr outErr)
-      throws ExecException, IOException, InterruptedException {
-    remoteCache.download(result, execRoot, outErr);
-    int exitCode = result.getExitCode();
+  private SpawnResult createSpawnResult(int exitCode, boolean cacheHit) {
     return new SpawnResult.Builder()
         .setStatus(exitCode == 0 ? Status.SUCCESS : Status.NON_ZERO_EXIT)
-        .setExitCode(exitCode);
+        .setExitCode(exitCode)
+        .setRunnerName(cacheHit ? getName() + " cache hit" : getName())
+        .setCacheHit(cacheHit)
+        .build();
   }
 
   private SpawnResult execLocally(Spawn spawn, SpawnExecutionContext context)