]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13793 improve ncloc count performance by using projects instead of components
authorPierre Guillot <50145663+pierre-guillot-sonarsource@users.noreply.github.com>
Wed, 2 Dec 2020 11:25:40 +0000 (12:25 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 2 Dec 2020 20:06:58 +0000 (20:06 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/measure/SumNclocDbQuery.java
server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/MarketplaceAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java

index d4abbce6721b47bea15edaee30ffd907efb2ff80..e3191e225af8987a383e0e879ccfdcc87a21f019 100644 (file)
@@ -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;
   }
 
index 22e5ee92cc10e79b27c3bae9d284ec726889f8a2..a06836fa5f1b26d92b753a4e50b3110ffd56754e 100644 (file)
@@ -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);
 
index 892b4ebf3741ece659586721a0750ee25560cdcb..9f5fe625c8c3609cf057d6c65fc4c6f08c815084 100644 (file)
@@ -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);
     }
index 499640bead1bd8a56343193f83ee587dfa024a89..6350e35873484902c790f0af57916ac7bdb4d221 100644 (file)
@@ -16,7 +16,8 @@
   <select id="selectByComponentUuidsAndMetricUuids" parameterType="map" resultType="org.sonar.db.measure.LiveMeasureDto">
     select <include refid="columns"/> from live_measures lm
     where
-    lm.metric_uuid in <foreach item="metricUuid" collection="metricUuids" open="(" separator="," close=")">#{metricUuid, jdbcType=VARCHAR}</foreach>
+    lm.metric_uuid in <foreach item="metricUuid" collection="metricUuids" open="(" separator=","
+                               close=")">#{metricUuid, jdbcType=VARCHAR}</foreach>
     and lm.component_uuid in
     <foreach item="componentUuid" collection="componentUuids" open="(" separator="," close=")">
       #{componentUuid, jdbcType=VARCHAR}
       select b.project_uuid as projectUuid, max(lm.value) as maxncloc
       from live_measures lm
       inner join metrics m on m.uuid = lm.metric_uuid
-      inner join components p on p.uuid = lm.component_uuid
-      inner join project_branches b on b.uuid = p.uuid
+      inner join project_branches b on b.uuid = lm.component_uuid and b.branch_type = #{branchType, jdbcType=VARCHAR}
+      inner join projects p on p.uuid = b.project_uuid and p.qualifier = 'TRK'
       <where>
         m.name = #{ncloc, jdbcType=VARCHAR}
         <if test="private">
           and p.private=${_true}
         </if>
-        and p.enabled = ${_true}
-        and p.scope = 'PRJ'
-        and p.qualifier = 'TRK'
-        and p.copy_component_uuid is null
-        and p.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
-        and b.branch_type = #{branchType, jdbcType=VARCHAR}
         <if test="projectUuidToExclude != null">
           and b.project_uuid &lt;&gt; #{projectUuidToExclude,jdbcType=VARCHAR}
         </if>
   </sql>
 
   <select id="scrollSelectByComponentUuidAndMetricKeys" resultType="org.sonar.db.measure.LiveMeasureDto" fetchSize="${_scrollFetchSize}"
-            resultSetType="FORWARD_ONLY">
+          resultSetType="FORWARD_ONLY">
      select <include refid="columns"/> from live_measures lm
      inner join metrics m on m.uuid = lm.metric_uuid
      where
      and lm.component_uuid = #{componentUuid, jdbcType=VARCHAR}
   </select>
 
-  <select id="selectTreeByQuery" parameterType="map" resultType="org.sonar.db.measure.LiveMeasureDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+  <select id="selectTreeByQuery" parameterType="map" resultType="org.sonar.db.measure.LiveMeasureDto" fetchSize="${_scrollFetchSize}"
+          resultSetType="FORWARD_ONLY">
     select <include refid="columns"/> from live_measures lm
     inner join components p on p.uuid = lm.component_uuid
     <!-- TODO do we really need another join on components ? Using lm.project_uuid should be enough -->
     <where>
       <if test="query.getMetricUuids() != null">
         lm.metric_uuid in
-        <foreach item="metricUuid" collection="query.getMetricUuids()" open="(" separator="," close=")">#{metricUuid,jdbcType=VARCHAR}</foreach>
+        <foreach item="metricUuid" collection="query.getMetricUuids()" open="(" separator=","
+                 close=")">#{metricUuid,jdbcType=VARCHAR}</foreach>
       </if>
       and p.enabled = ${_true}
       <if test="query.qualifiers != null">
     <where>
       <if test="query.getMetricUuids() != null">
         lm.metric_uuid in
-        <foreach item="metricUuid" collection="query.getMetricUuids()" open="(" separator="," close=")">#{metricUuid,jdbcType=VARCHAR}</foreach>
+        <foreach item="metricUuid" collection="query.getMetricUuids()" open="(" separator=","
+                 close=")">#{metricUuid,jdbcType=VARCHAR}</foreach>
       </if>
       and p.enabled = ${_true}
       <if test="query.qualifiers != null">
index 16cbecdfe0e9e66e673088137cc9181824eae11c..004a7fc1f94243e8f8530a6a855cc9089e6d8451 100644 (file)
@@ -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();
index 0eda7898f941807d257401b4e914e680df92f36e..202835901f84fc0142d3c3c9f7c8213297f40203 100644 (file)
@@ -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);
index 5e47becce65188c637f4a1287f4567a7f655c99f..581b78ee09e57b2d2c8c216ea70260537085ece7 100644 (file)
@@ -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() {
index e6124ef291d02261a8fe4f96d9c8e9b1ae0e654f..91191e57b8c26a44c4f587f28793e9f4dafd7795 100644 (file)
@@ -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);
     }
index 8590131e3766b7eeca24b85a910f71cbc7962615..6dca76a8206b5c61237c9b5dafc27079c80e3287 100644 (file)
@@ -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);