From 5ca7a4f8b7a5aa33a7638573117fa6ce98578b15 Mon Sep 17 00:00:00 2001 From: Pierre Guillot <50145663+pierre-guillot-sonarsource@users.noreply.github.com> Date: Wed, 2 Dec 2020 12:25:40 +0100 Subject: [PATCH] SONAR-13793 improve ncloc count performance by using projects instead of components --- .../org/sonar/db/measure/LiveMeasureDao.java | 2 +- .../sonar/db/measure/LiveMeasureMapper.java | 1 - .../org/sonar/db/measure/SumNclocDbQuery.java | 13 ---------- .../sonar/db/measure/LiveMeasureMapper.xml | 24 +++++++++---------- .../sonar/db/measure/LiveMeasureDaoTest.java | 3 --- .../telemetry/TelemetryDataLoaderImpl.java | 9 +++---- .../TelemetryDataLoaderImplTest.java | 4 ++-- .../sonar/server/ui/ws/MarketplaceAction.java | 5 +--- .../server/ui/ws/MarketplaceActionTest.java | 2 +- 9 files changed, 19 insertions(+), 44 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java index d4abbce6721..e3191e225af 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java @@ -100,7 +100,7 @@ public class LiveMeasureDao implements Dao { */ public long sumNclocOfBiggestBranch(DbSession dbSession, SumNclocDbQuery dbQuery) { Long ncloc = mapper(dbSession).sumNclocOfBiggestBranch( - NCLOC_KEY, BranchType.BRANCH, dbQuery.getOrganizationUuid(), dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude()); + NCLOC_KEY, BranchType.BRANCH, dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude()); return ncloc == null ? 0L : ncloc; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java index 22e5ee92cc1..a06836fa5f1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java @@ -58,7 +58,6 @@ public interface LiveMeasureMapper { Long sumNclocOfBiggestBranch( @Param("ncloc") String nclocKey, @Param("branchType") BranchType branchType, - @Param("organizationUuid") String organizationUuid, @Param("private") Boolean privateProject, @Nullable @Param("projectUuidToExclude") String projectUuidToExclude); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/SumNclocDbQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/SumNclocDbQuery.java index 892b4ebf374..9f5fe625c8c 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/SumNclocDbQuery.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/SumNclocDbQuery.java @@ -27,12 +27,10 @@ import static com.google.common.base.Preconditions.checkNotNull; public class SumNclocDbQuery { private final String projectUuidToExclude; - private final String organizationUuid; private final Boolean onlyPrivateProjects; public SumNclocDbQuery(Builder builder) { projectUuidToExclude = builder.projectUuidToExclude; - organizationUuid = builder.organizationUuid; onlyPrivateProjects = builder.onlyPrivateProjects; } @@ -41,10 +39,6 @@ public class SumNclocDbQuery { return projectUuidToExclude; } - public String getOrganizationUuid() { - return organizationUuid; - } - public Boolean getOnlyPrivateProjects() { return onlyPrivateProjects; } @@ -55,7 +49,6 @@ public class SumNclocDbQuery { public static class Builder { private String projectUuidToExclude; - private String organizationUuid; private Boolean onlyPrivateProjects; private Builder() { @@ -67,18 +60,12 @@ public class SumNclocDbQuery { return this; } - public Builder setOrganizationUuid(String organizationUuid) { - this.organizationUuid = organizationUuid; - return this; - } - public Builder setOnlyPrivateProjects(Boolean onlyPrivateProjects) { this.onlyPrivateProjects = onlyPrivateProjects; return this; } public SumNclocDbQuery build() { - checkNotNull(organizationUuid); checkNotNull(onlyPrivateProjects); return new SumNclocDbQuery(this); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml index 499640bead1..6350e358734 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml @@ -16,7 +16,8 @@ + resultSetType="FORWARD_ONLY"> select from live_measures lm inner join metrics m on m.uuid = lm.metric_uuid where @@ -186,7 +181,8 @@ and lm.component_uuid = #{componentUuid, jdbcType=VARCHAR} - select from live_measures lm inner join components p on p.uuid = lm.component_uuid @@ -194,7 +190,8 @@ lm.metric_uuid in - #{metricUuid,jdbcType=VARCHAR} + #{metricUuid,jdbcType=VARCHAR} and p.enabled = ${_true} @@ -219,7 +216,8 @@ lm.metric_uuid in - #{metricUuid,jdbcType=VARCHAR} + #{metricUuid,jdbcType=VARCHAR} and p.enabled = ${_true} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java index 16cbecdfe0e..004a7fc1f94 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java @@ -321,7 +321,6 @@ public class LiveMeasureDaoTest { SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) - .setOrganizationUuid(organization.getUuid()) .build(); long result = underTest.sumNclocOfBiggestBranch(db.getSession(), query); @@ -334,7 +333,6 @@ public class LiveMeasureDaoTest { db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString())); SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) - .setOrganizationUuid(db.getDefaultOrganization().getUuid()) .build(); long result = underTest.sumNclocOfBiggestBranch(db.getSession(), query); @@ -360,7 +358,6 @@ public class LiveMeasureDaoTest { db.measures().insertLiveMeasure(projectToExcludeBranch, ncloc, m -> m.setValue(400d)); SumNclocDbQuery query = SumNclocDbQuery.builder() - .setOrganizationUuid(organization.getUuid()) .setProjectUuidToExclude(projectToExclude.uuid()) .setOnlyPrivateProjects(false) .build(); diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index 0eda7898f94..202835901f8 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -58,19 +58,18 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { private final UserIndex userIndex; private final ProjectMeasuresIndex projectMeasuresIndex; private final PlatformEditionProvider editionProvider; - private final DefaultOrganizationProvider defaultOrganizationProvider; private final InternalProperties internalProperties; private final DockerSupport dockerSupport; @CheckForNull private final LicenseReader licenseReader; public TelemetryDataLoaderImpl(Server server, DbClient dbClient, PluginRepository pluginRepository, UserIndex userIndex, ProjectMeasuresIndex projectMeasuresIndex, - PlatformEditionProvider editionProvider, DefaultOrganizationProvider defaultOrganizationProvider, InternalProperties internalProperties, DockerSupport dockerSupport) { - this(server, dbClient, pluginRepository, userIndex, projectMeasuresIndex, editionProvider, defaultOrganizationProvider, internalProperties, dockerSupport, null); + PlatformEditionProvider editionProvider, InternalProperties internalProperties, DockerSupport dockerSupport) { + this(server, dbClient, pluginRepository, userIndex, projectMeasuresIndex, editionProvider, internalProperties, dockerSupport, null); } public TelemetryDataLoaderImpl(Server server, DbClient dbClient, PluginRepository pluginRepository, UserIndex userIndex, ProjectMeasuresIndex projectMeasuresIndex, - PlatformEditionProvider editionProvider, DefaultOrganizationProvider defaultOrganizationProvider, InternalProperties internalProperties, + PlatformEditionProvider editionProvider, InternalProperties internalProperties, DockerSupport dockerSupport, @Nullable LicenseReader licenseReader) { this.server = server; this.dbClient = dbClient; @@ -78,7 +77,6 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { this.userIndex = userIndex; this.projectMeasuresIndex = projectMeasuresIndex; this.editionProvider = editionProvider; - this.defaultOrganizationProvider = defaultOrganizationProvider; this.internalProperties = internalProperties; this.dockerSupport = dockerSupport; this.licenseReader = licenseReader; @@ -115,7 +113,6 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { data.setUsingBranches(dbClient.branchDao().hasNonMainBranches(dbSession)); SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) - .setOrganizationUuid(defaultOrganizationProvider.get().getUuid()) .build(); data.setNcloc(dbClient.liveMeasureDao().sumNclocOfBiggestBranch(dbSession, query)); long numberOfUnanalyzedCMeasures = dbClient.liveMeasureDao().countProjectsHavingMeasure(dbSession, UNANALYZED_C_KEY); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java index 5e47becce65..581b78ee09e 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java @@ -81,9 +81,9 @@ public class TelemetryDataLoaderImplTest { private final LicenseReader licenseReader = mock(LicenseReader.class); private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2), - new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, new DefaultOrganizationProviderImpl(db.getDbClient()), internalProperties, dockerSupport, null); + new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, dockerSupport, null); private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2), - new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, new DefaultOrganizationProviderImpl(db.getDbClient()), internalProperties, dockerSupport, licenseReader); + new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, dockerSupport, licenseReader); @Test public void send_telemetry_data() { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/MarketplaceAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/MarketplaceAction.java index e6124ef291d..91191e57b8c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/MarketplaceAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/MarketplaceAction.java @@ -36,13 +36,11 @@ public class MarketplaceAction implements NavigationWsAction { private final UserSession userSession; private final Server server; private final DbClient dbClient; - private final DefaultOrganizationProvider defaultOrganizationProvider; - public MarketplaceAction(UserSession userSession, Server server, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider) { + public MarketplaceAction(UserSession userSession, Server server, DbClient dbClient) { this.userSession = userSession; this.server = server; this.dbClient = dbClient; - this.defaultOrganizationProvider = defaultOrganizationProvider; } @Override @@ -73,7 +71,6 @@ public class MarketplaceAction implements NavigationWsAction { try (DbSession dbSession = dbClient.openSession(false)) { SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) - .setOrganizationUuid(defaultOrganizationProvider.get().getUuid()) .build(); return dbClient.liveMeasureDao().sumNclocOfBiggestBranch(dbSession, query); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java index 8590131e376..6dca76a8206 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java @@ -58,7 +58,7 @@ public class MarketplaceActionTest { private Server server = mock(Server.class); private DbClient dbClient = db.getDbClient(); - private MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient, new DefaultOrganizationProviderImpl(dbClient)); + private MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient); private WsActionTester ws = new WsActionTester(underTest); -- 2.39.5