aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>2025-07-14 14:01:39 +0200
committersonartech <sonartech@sonarsource.com>2025-07-14 20:04:13 +0000
commita4fee4df20cff74a536e743f7519c03f1092d48b (patch)
treeb310ff3ebfe5981710232da81eb9e3ac850bf07c
parentc65be009977b2f092e493d3257cf34870bb52d31 (diff)
downloadsonarqube-a4fee4df20cff74a536e743f7519c03f1092d48b.tar.gz
sonarqube-a4fee4df20cff74a536e743f7519c03f1092d48b.zip
SONAR-25456 Fixed an issue where telemetry for failing indexing tasks was sometimes not sent
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetry.java28
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetryTest.java25
2 files changed, 36 insertions, 17 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetry.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetry.java
index 213c4e153fc..a1fffb536c7 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetry.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetry.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.issue.index;
+import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.time.Clock;
import java.util.Set;
@@ -50,7 +51,6 @@ public class AsyncIssueIndexCreationTelemetry {
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncIssueIndexCreationTelemetry.class);
- private final IssueIndexSyncProgressChecker issueIndexSyncProgressChecker;
private final DbClient dbClient;
private final TelemetryClient telemetryClient;
private final Server server;
@@ -63,9 +63,8 @@ public class AsyncIssueIndexCreationTelemetry {
private long startTime;
private int nbIndexingTasks;
- public AsyncIssueIndexCreationTelemetry(IssueIndexSyncProgressChecker issueIndexSyncProgressChecker, DbClient dbClient,
- TelemetryClient telemetryClient, Server server, UuidFactory uuidFactory, Clock clock, IssueIndexMonitoringScheduler scheduler, Configuration config) {
- this.issueIndexSyncProgressChecker = issueIndexSyncProgressChecker;
+ public AsyncIssueIndexCreationTelemetry(DbClient dbClient, TelemetryClient telemetryClient, Server server, UuidFactory uuidFactory, Clock clock,
+ IssueIndexMonitoringScheduler scheduler, Configuration config) {
this.dbClient = dbClient;
this.telemetryClient = telemetryClient;
this.server = server;
@@ -86,18 +85,21 @@ public class AsyncIssueIndexCreationTelemetry {
}
startTime = clock.millis();
this.nbIndexingTasks = nbIndexingTasks;
- if(currentMonitoring != null) {
+ if (currentMonitoring != null) {
currentMonitoring.cancel(false);
}
- currentMonitoring = scheduler.scheduleAtFixedRate(() -> {
- try (DbSession dbSession = dbClient.openSession(false)) {
- if (!issueIndexSyncProgressChecker.isIssueSyncInProgress(dbSession)) {
- sendIssueIndexationTelemetry(dbSession);
- currentMonitoring.cancel(false);
- }
- }
- }, 0, 5, TimeUnit.SECONDS);
+ currentMonitoring = scheduler.scheduleAtFixedRate(this::tryToSendTelemetry, 0, 5, TimeUnit.SECONDS);
+
+ }
+ @VisibleForTesting
+ void tryToSendTelemetry() {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ if (!dbClient.ceQueueDao().hasAnyIssueSyncTaskPendingOrInProgress(dbSession)) {
+ sendIssueIndexationTelemetry(dbSession);
+ currentMonitoring.cancel(false);
+ }
+ }
}
private void sendIssueIndexationTelemetry(DbSession dbSession) {
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetryTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetryTest.java
index cb8bab7a24d..631d0f5464e 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetryTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/issue/index/AsyncIssueIndexCreationTelemetryTest.java
@@ -20,11 +20,13 @@
package org.sonar.server.issue.index;
import java.io.IOException;
+import java.lang.reflect.Field;
import java.time.Clock;
import java.util.Optional;
import java.util.concurrent.ScheduledFuture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.platform.commons.util.ReflectionUtils;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.sonar.api.config.Configuration;
@@ -32,6 +34,7 @@ import org.sonar.api.platform.Server;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
+import org.sonar.db.ce.CeQueueDao;
import org.sonar.telemetry.core.TelemetryClient;
import static org.assertj.core.api.Assertions.assertThat;
@@ -58,8 +61,7 @@ class AsyncIssueIndexCreationTelemetryTest {
private final IssueIndexMonitoringScheduler scheduler = mock();
private final ArgumentCaptor<Runnable> telemetrySyncRunnable = ArgumentCaptor.forClass(Runnable.class);
private final Configuration configuration = mock();
- private final AsyncIssueIndexCreationTelemetry asyncIssueIndexCreationTelemetry = new AsyncIssueIndexCreationTelemetry(
- issueIndexSyncProgressChecker,
+ private final AsyncIssueIndexCreationTelemetry underTest = new AsyncIssueIndexCreationTelemetry(
dbClient,
telemetryClient,
server,
@@ -84,14 +86,14 @@ class AsyncIssueIndexCreationTelemetryTest {
reset(configuration);
when(configuration.getBoolean(SONAR_TELEMETRY_ENABLE.getKey())).thenReturn(Optional.of(false));
- asyncIssueIndexCreationTelemetry.startIndexCreationMonitoringToSendTelemetry(100);
+ underTest.startIndexCreationMonitoringToSendTelemetry(100);
verify(scheduler, never()).scheduleAtFixedRate(any(), anyLong(), anyLong(), any());
}
@Test
void whenSynchroIsNotFinished_thenRetry() throws IOException {
- asyncIssueIndexCreationTelemetry.startIndexCreationMonitoringToSendTelemetry(100);
+ underTest.startIndexCreationMonitoringToSendTelemetry(100);
verify(scheduler).scheduleAtFixedRate(any(), anyLong(), anyLong(), any());
@@ -107,4 +109,19 @@ class AsyncIssueIndexCreationTelemetryTest {
.contains(AsyncIssueIndexCreationTelemetry.KEY_ASYNC_ISSUE_INDEXING_TASK_TOTAL_COUNT)
.contains(AsyncIssueIndexCreationTelemetry.KEY_ASYNC_ISSUE_INDEXING_TASK_FAILURE_COUNT);
}
+
+ @Test
+ void tryToSendTelemetry_whenNoPendingTasks_thenSendTelemetry() throws IOException, IllegalAccessException {
+ CeQueueDao ceQueueDao = mock();
+ when(ceQueueDao.hasAnyIssueSyncTaskPendingOrInProgress(any())).thenReturn(false);
+ when(dbClient.ceQueueDao()).thenReturn(ceQueueDao);
+
+ Field field = ReflectionUtils.findFields(underTest.getClass(), f -> f.getName().equals("currentMonitoring"), ReflectionUtils.HierarchyTraversalMode.TOP_DOWN).get(0);
+ field.setAccessible(true);
+ field.set(underTest, mock(ScheduledFuture.class));
+
+ underTest.tryToSendTelemetry();
+
+ verify(telemetryClient).uploadMetric(any());
+ }
}