diff options
author | Belen Pruvost <belen.pruvost@sonarsource.com> | 2022-07-18 09:35:33 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-07-25 20:03:57 +0000 |
commit | 6bb86216fbd218cea009091f32addaeb2cafb555 (patch) | |
tree | 6fad06a6f3c74995dc6ed90887af790742c39309 /server/sonar-webserver-pushapi/src/main | |
parent | 9b1a191c91a20d5551c290e8bab6460824f135e5 (diff) | |
download | sonarqube-6bb86216fbd218cea009091f32addaeb2cafb555.tar.gz sonarqube-6bb86216fbd218cea009091f32addaeb2cafb555.zip |
SONAR-16645 - Push Events Scheduled Purge
Diffstat (limited to 'server/sonar-webserver-pushapi/src/main')
6 files changed, 240 insertions, 1 deletions
diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java index 0a48485466c..091b56d7d56 100644 --- a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/ServerPushWsModule.java @@ -20,6 +20,9 @@ package org.sonar.server.pushapi; import org.sonar.core.platform.Module; +import org.sonar.server.pushapi.scheduler.purge.PushEventsPurgeExecutorServiceImpl; +import org.sonar.server.pushapi.scheduler.purge.PushEventsPurgeInitializer; +import org.sonar.server.pushapi.scheduler.purge.PushEventsPurgeSchedulerImpl; import org.sonar.server.pushapi.sonarlint.SonarLintClientPermissionsValidator; import org.sonar.server.pushapi.sonarlint.SonarLintClientsRegistry; import org.sonar.server.pushapi.sonarlint.SonarLintPushAction; @@ -32,6 +35,11 @@ public class ServerPushWsModule extends Module { ServerPushWs.class, SonarLintClientPermissionsValidator.class, SonarLintClientsRegistry.class, - SonarLintPushAction.class); + SonarLintPushAction.class, + + // Push Events Purge + PushEventsPurgeSchedulerImpl.class, + PushEventsPurgeExecutorServiceImpl.class, + PushEventsPurgeInitializer.class); } } diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeExecutorService.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeExecutorService.java new file mode 100644 index 00000000000..39a1024450d --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeExecutorService.java @@ -0,0 +1,27 @@ +/* + * 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.server.pushapi.scheduler.purge; + +import java.util.concurrent.ScheduledExecutorService; +import org.sonar.api.server.ServerSide; + +@ServerSide +public interface PushEventsPurgeExecutorService extends ScheduledExecutorService { +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeExecutorServiceImpl.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeExecutorServiceImpl.java new file mode 100644 index 00000000000..1e1629f40d6 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeExecutorServiceImpl.java @@ -0,0 +1,38 @@ +/* + * 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.server.pushapi.scheduler.purge; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import org.sonar.server.util.AbstractStoppableScheduledExecutorServiceImpl; + +public class PushEventsPurgeExecutorServiceImpl + extends AbstractStoppableScheduledExecutorServiceImpl<ScheduledExecutorService> + implements PushEventsPurgeExecutorService { + + public PushEventsPurgeExecutorServiceImpl() { + super(Executors.newSingleThreadScheduledExecutor(r -> { + Thread thread = Executors.defaultThreadFactory().newThread(r); + thread.setDaemon(true); + thread.setName(String.format("PushEvents-Purge-%d", System.nanoTime())); + return thread; + })); + } +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeInitializer.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeInitializer.java new file mode 100644 index 00000000000..1abea71ea67 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeInitializer.java @@ -0,0 +1,41 @@ +/* + * 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.server.pushapi.scheduler.purge; + +import javax.annotation.Nullable; +import org.sonar.api.platform.Server; +import org.sonar.api.platform.ServerStartHandler; + +public class PushEventsPurgeInitializer implements ServerStartHandler { + + private final PushEventsPurgeScheduler pushEventsPurgeScheduler; + + public PushEventsPurgeInitializer(@Nullable PushEventsPurgeScheduler pushEventsPurgeScheduler) { + this.pushEventsPurgeScheduler = pushEventsPurgeScheduler; + } + + @Override + public void onServerStart(Server server) { + if (pushEventsPurgeScheduler != null) { + pushEventsPurgeScheduler.startScheduling(); + } + } + +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeScheduler.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeScheduler.java new file mode 100644 index 00000000000..d6ce454f753 --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeScheduler.java @@ -0,0 +1,27 @@ +/* + * 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.server.pushapi.scheduler.purge; + +import org.sonar.api.server.ServerSide; + +@ServerSide +public interface PushEventsPurgeScheduler { + void startScheduling(); +} diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeSchedulerImpl.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeSchedulerImpl.java new file mode 100644 index 00000000000..3a72765bbcb --- /dev/null +++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/scheduler/purge/PushEventsPurgeSchedulerImpl.java @@ -0,0 +1,98 @@ +/* + * 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.server.pushapi.scheduler.purge; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Set; +import org.sonar.api.config.Configuration; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.server.util.GlobalLockManager; + +import static java.util.concurrent.TimeUnit.SECONDS; + +public class PushEventsPurgeSchedulerImpl implements PushEventsPurgeScheduler { + private static final Logger LOG = Loggers.get(PushEventsPurgeSchedulerImpl.class); + private static final String LOCK_NAME = "PushEventsPurgeCheck"; + private static final String INITIAL_DELAY_IN_SECONDS = "sonar.push.events.purge.initial.delay"; + private static final String ENQUEUE_DELAY_IN_SECONDS = "sonar.push.events.purge.enqueue.delay"; + private static final int ENQUEUE_LOCK_DELAY_IN_SECONDS = 60; + + private final DbClient dbClient; + private final Configuration config; + private final GlobalLockManager lockManager; + private final PushEventsPurgeExecutorService executorService; + private final System2 system; + + + public PushEventsPurgeSchedulerImpl(DbClient dbClient, Configuration config, GlobalLockManager lockManager, + PushEventsPurgeExecutorService executorService, System2 system) { + this.dbClient = dbClient; + this.executorService = executorService; + this.config = config; + this.lockManager = lockManager; + this.system = system; + } + + @Override + public void startScheduling() { + executorService.scheduleAtFixedRate(this::checks, getInitialDelay(), + getEnqueueDelay(), SECONDS); + } + + private void checks() { + try { + // Avoid enqueueing push events purge task multiple times + if (!lockManager.tryLock(LOCK_NAME, ENQUEUE_LOCK_DELAY_IN_SECONDS)) { + return; + } + purgeExpiredPushEvents(); + } catch (Exception e) { + LOG.error("Error in Push Events Purge scheduler", e); + } + } + + private void purgeExpiredPushEvents() { + try (DbSession dbSession = dbClient.openSession(false)) { + Set<String> uuids = dbClient.pushEventDao().selectUuidsOfExpiredEvents(dbSession, getExpiredTimestamp()); + LOG.debug(String.format("%s push events to be deleted...", uuids.size())); + dbClient.pushEventDao().deleteByUuids(dbSession, uuids); + dbSession.commit(); + } + } + + public long getInitialDelay() { + return config.getLong(INITIAL_DELAY_IN_SECONDS).orElse(60 * 60L); + } + + public long getEnqueueDelay() { + return config.getLong(ENQUEUE_DELAY_IN_SECONDS).orElse(60 * 60L); + } + + private long getExpiredTimestamp() { + return Instant.ofEpochMilli(system.now()) + .minus(1, ChronoUnit.HOURS) + .toEpochMilli(); + } +} |