]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16239 Log analysis cache download
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 23 May 2022 10:10:40 +0000 (12:10 +0200)
committersonartech <sonartech@sonarsource.com>
Sat, 4 Jun 2022 20:02:46 +0000 (20:02 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoaderTest.java

index 7e04a91c30f2f9d1bfdc07f6b04b4dbe2c67a4aa..1a308f48ce09a80feb40b463f2916a6bc3488764 100644 (file)
@@ -25,6 +25,9 @@ import java.util.Optional;
 import java.util.zip.InflaterInputStream;
 import org.sonar.api.scanner.fs.InputProject;
 import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.api.utils.log.Profiler;
 import org.sonar.core.util.Protobuf;
 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
 import org.sonar.scanner.protocol.internal.ScannerInternal;
@@ -34,11 +37,16 @@ import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.HttpException;
 import org.sonarqube.ws.client.WsResponse;
 
+import static org.sonar.core.util.FileUtils.humanReadableByteCountSI;
+
 /**
  * Loads plugin cache into the local storage
  */
 public class DefaultAnalysisCacheLoader implements AnalysisCacheLoader {
+  private static final Logger LOG = Loggers.get(DefaultAnalysisCacheLoader.class);
+  private static final String LOG_MSG = "Load analysis cache";
   static final String CONTENT_ENCODING = "Content-Encoding";
+  static final String CONTENT_LENGTH = "Content-Length";
   static final String ACCEPT_ENCODING = "Accept-Encoding";
   private static final String URL = "api/analysis_cache/get";
 
@@ -52,23 +60,31 @@ public class DefaultAnalysisCacheLoader implements AnalysisCacheLoader {
     this.wsClient = wsClient;
   }
 
-  @Override public Optional<AnalysisCacheMsg> load() {
+  @Override
+  public Optional<AnalysisCacheMsg> load() {
     String url = URL + "?project=" + project.key();
     if (branchConfiguration.referenceBranchName() != null) {
       url = url + "&branch=" + branchConfiguration.referenceBranchName();
     }
 
+    Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
     GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip");
 
     try (WsResponse response = wsClient.call(request); InputStream is = response.contentStream()) {
       Optional<String> contentEncoding = response.header(CONTENT_ENCODING);
-      if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) {
-        return Optional.of(decompress(is));
+      Optional<Integer> length = response.header(CONTENT_LENGTH).map(Integer::parseInt);
+      boolean hasGzipEncoding = contentEncoding.isPresent() && contentEncoding.get().equals("gzip");
+
+      AnalysisCacheMsg msg = hasGzipEncoding ? decompress(is) : Protobuf.read(is, AnalysisCacheMsg.parser());
+      if (length.isPresent()) {
+        profiler.stopInfo(LOG_MSG + String.format(" (%s)", humanReadableByteCountSI(length.get())));
       } else {
-        return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser()));
+        profiler.stopInfo(LOG_MSG);
       }
+      return Optional.of(msg);
     } catch (HttpException e) {
       if (e.code() == 404) {
+        profiler.stopInfo(LOG_MSG + " (404)");
         return Optional.empty();
       }
       throw MessageException.of("Failed to download analysis cache: " + DefaultScannerWsClient.createErrorMessage(e));
index 3281d457a7dcdab1661f0e1fb27c4f48083a883e..4f666a58a537d60d6f04e27d0778ff88319cf0b3 100644 (file)
@@ -28,10 +28,12 @@ import java.nio.charset.StandardCharsets;
 import java.util.Optional;
 import java.util.zip.DeflaterInputStream;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 import org.sonar.api.scanner.fs.InputProject;
 import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.LogTester;
 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
 import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg;
 import org.sonar.scanner.scan.branch.BranchConfiguration;
@@ -56,6 +58,8 @@ public class DefaultAnalysisCacheLoaderTest {
   private final InputProject project = mock(InputProject.class);
   private final BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
   private final DefaultAnalysisCacheLoader loader = new DefaultAnalysisCacheLoader(wsClient, project, branchConfiguration);
+  @Rule
+  public LogTester logs = new LogTester();
 
   @Before
   public void before() {
@@ -64,11 +68,13 @@ public class DefaultAnalysisCacheLoaderTest {
   }
 
   @Test
-  public void loads_content() throws IOException {
+  public void loads_content_and_logs_size() throws IOException {
     setResponse(MSG);
+    when(response.header("Content-Length")).thenReturn(Optional.of("123"));
     AnalysisCacheMsg msg = loader.load().get();
     assertThat(msg).isEqualTo(MSG);
     assertRequestPath("api/analysis_cache/get?project=myproject");
+    assertThat(logs.logs()).anyMatch(s -> s.startsWith("Load analysis cache (123 bytes)"));
   }
 
   @Test
@@ -80,6 +86,7 @@ public class DefaultAnalysisCacheLoaderTest {
 
     assertThat(msg).isEqualTo(MSG);
     assertRequestPath("api/analysis_cache/get?project=myproject&branch=name");
+    assertThat(logs.logs()).anyMatch(s -> s.startsWith("Load analysis cache | time="));
   }
 
   @Test
@@ -93,6 +100,7 @@ public class DefaultAnalysisCacheLoaderTest {
   public void returns_empty_if_404() {
     when(wsClient.call(any())).thenThrow(new HttpException("url", 404, "content"));
     assertThat(loader.load()).isEmpty();
+    assertThat(logs.logs()).anyMatch(s -> s.startsWith("Load analysis cache (404) | time="));
   }
 
   @Test