From 7475e9d7a6e89ebe552e101250518ab67b56531d Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 20 May 2022 10:55:22 +0200 Subject: SONAR-16395 Activate Analyzers Cache by default --- .../sonar/scanner/cache/AnalysisCacheEnabled.java | 2 +- .../sonar/scanner/cache/AnalysisCacheLoader.java | 65 +--------------- .../scanner/cache/DefaultAnalysisCacheLoader.java | 87 ++++++++++++++++++++++ .../scanner/scan/SpringProjectScanContainer.java | 4 +- 4 files changed, 92 insertions(+), 66 deletions(-) create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner') diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheEnabled.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheEnabled.java index b93150ae944..3333c767310 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheEnabled.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheEnabled.java @@ -30,6 +30,6 @@ public class AnalysisCacheEnabled { } public boolean isEnabled() { - return configuration.getBoolean(PROP_KEY).orElse(false); + return configuration.getBoolean(PROP_KEY).orElse(true); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java index 4832bd774b0..b44d2dd9cec 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/AnalysisCacheLoader.java @@ -19,70 +19,9 @@ */ package org.sonar.scanner.cache; -import java.io.IOException; -import java.io.InputStream; 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.core.util.Protobuf; -import org.sonar.scanner.bootstrap.DefaultScannerWsClient; import org.sonar.scanner.protocol.internal.ScannerInternal; -import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg; -import org.sonar.scanner.scan.branch.BranchConfiguration; -import org.sonar.scanner.scan.branch.BranchType; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.HttpException; -import org.sonarqube.ws.client.WsResponse; -/** - * Loads plugin cache into the local storage - */ -public class AnalysisCacheLoader { - static final String CONTENT_ENCODING = "Content-Encoding"; - static final String ACCEPT_ENCODING = "Accept-Encoding"; - private static final String URL = "api/analysis_cache/get"; - - private final DefaultScannerWsClient wsClient; - private final InputProject project; - private final BranchConfiguration branchConfiguration; - - public AnalysisCacheLoader(DefaultScannerWsClient wsClient, InputProject project, BranchConfiguration branchConfiguration) { - this.project = project; - this.branchConfiguration = branchConfiguration; - this.wsClient = wsClient; - } - - public Optional load() { - String url = URL + "?project=" + project.key(); - if (branchConfiguration.referenceBranchName() != null) { - url = url + "&branch=" + branchConfiguration.referenceBranchName(); - } - - GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip"); - - try (WsResponse response = wsClient.call(request); InputStream is = response.contentStream()) { - Optional contentEncoding = response.header(CONTENT_ENCODING); - if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) { - return Optional.of(decompress(is)); - } else { - return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser())); - } - } catch (HttpException e) { - if (e.code() == 404) { - return Optional.empty(); - } - throw MessageException.of("Failed to download analysis cache: " + DefaultScannerWsClient.createErrorMessage(e)); - } catch (Exception e) { - throw new IllegalStateException("Failed to download analysis cache", e); - } - } - - private static AnalysisCacheMsg decompress(InputStream is) { - try (InflaterInputStream iis = new InflaterInputStream(is)) { - return Protobuf.read(iis, ScannerInternal.AnalysisCacheMsg.parser()); - } catch (IOException e) { - throw new IllegalStateException("Failed to decompress analysis cache", e); - } - } +public interface AnalysisCacheLoader { + Optional load(); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java new file mode 100644 index 00000000000..7e04a91c30f --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cache/DefaultAnalysisCacheLoader.java @@ -0,0 +1,87 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.cache; + +import java.io.IOException; +import java.io.InputStream; +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.core.util.Protobuf; +import org.sonar.scanner.bootstrap.DefaultScannerWsClient; +import org.sonar.scanner.protocol.internal.ScannerInternal; +import org.sonar.scanner.protocol.internal.ScannerInternal.AnalysisCacheMsg; +import org.sonar.scanner.scan.branch.BranchConfiguration; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; +import org.sonarqube.ws.client.WsResponse; + +/** + * Loads plugin cache into the local storage + */ +public class DefaultAnalysisCacheLoader implements AnalysisCacheLoader { + static final String CONTENT_ENCODING = "Content-Encoding"; + static final String ACCEPT_ENCODING = "Accept-Encoding"; + private static final String URL = "api/analysis_cache/get"; + + private final DefaultScannerWsClient wsClient; + private final InputProject project; + private final BranchConfiguration branchConfiguration; + + public DefaultAnalysisCacheLoader(DefaultScannerWsClient wsClient, InputProject project, BranchConfiguration branchConfiguration) { + this.project = project; + this.branchConfiguration = branchConfiguration; + this.wsClient = wsClient; + } + + @Override public Optional load() { + String url = URL + "?project=" + project.key(); + if (branchConfiguration.referenceBranchName() != null) { + url = url + "&branch=" + branchConfiguration.referenceBranchName(); + } + + GetRequest request = new GetRequest(url).setHeader(ACCEPT_ENCODING, "gzip"); + + try (WsResponse response = wsClient.call(request); InputStream is = response.contentStream()) { + Optional contentEncoding = response.header(CONTENT_ENCODING); + if (contentEncoding.isPresent() && contentEncoding.get().equals("gzip")) { + return Optional.of(decompress(is)); + } else { + return Optional.of(Protobuf.read(is, AnalysisCacheMsg.parser())); + } + } catch (HttpException e) { + if (e.code() == 404) { + return Optional.empty(); + } + throw MessageException.of("Failed to download analysis cache: " + DefaultScannerWsClient.createErrorMessage(e)); + } catch (Exception e) { + throw new IllegalStateException("Failed to download analysis cache", e); + } + } + + private static AnalysisCacheMsg decompress(InputStream is) { + try (InflaterInputStream iis = new InflaterInputStream(is)) { + return Protobuf.read(iis, ScannerInternal.AnalysisCacheMsg.parser()); + } catch (IOException e) { + throw new IllegalStateException("Failed to decompress analysis cache", e); + } + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java index da491cf9f5a..aebe65007de 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java @@ -44,7 +44,7 @@ import org.sonar.scanner.bootstrap.ExtensionMatcher; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.PostJobExtensionDictionary; import org.sonar.scanner.cache.AnalysisCacheEnabled; -import org.sonar.scanner.cache.AnalysisCacheLoader; +import org.sonar.scanner.cache.DefaultAnalysisCacheLoader; import org.sonar.scanner.cache.AnalysisCacheMemoryStorage; import org.sonar.scanner.cache.AnalysisCacheProvider; import org.sonar.scanner.ci.CiConfigurationProvider; @@ -232,7 +232,7 @@ public class SpringProjectScanContainer extends SpringComponentContainer { // Plugin cache AnalysisCacheProvider.class, AnalysisCacheMemoryStorage.class, - AnalysisCacheLoader.class, + DefaultAnalysisCacheLoader.class, // Report ReferenceBranchSupplier.class, -- cgit v1.2.3