3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration;
22 import java.util.Date;
23 import java.util.concurrent.Semaphore;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.sonar.core.util.logs.Profiler;
27 import org.sonar.server.platform.Platform;
28 import org.sonar.server.platform.db.migration.DatabaseMigrationState.Status;
29 import org.sonar.server.platform.db.migration.engine.MigrationEngine;
30 import org.sonar.server.platform.db.migration.step.MigrationStepExecutionException;
33 * Handles concurrency to make sure only one DB migration can run at a time.
35 public class DatabaseMigrationImpl implements DatabaseMigration {
37 private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseMigrationImpl.class);
40 * ExecutorService implements threads management.
42 private final DatabaseMigrationExecutorService executorService;
43 private final MigrationEngine migrationEngine;
44 private final Platform platform;
45 private final MutableDatabaseMigrationState migrationState;
47 * This semaphore implements thread safety from concurrent calls of method {@link #startIt()}
49 private final Semaphore semaphore = new Semaphore(1);
51 public DatabaseMigrationImpl(DatabaseMigrationExecutorService executorService, MutableDatabaseMigrationState migrationState,
52 MigrationEngine migrationEngine, Platform platform) {
53 this.executorService = executorService;
54 this.migrationState = migrationState;
55 this.migrationEngine = migrationEngine;
56 this.platform = platform;
60 public void startIt() {
61 if (semaphore.tryAcquire()) {
63 executorService.execute(this::doDatabaseMigration);
64 } catch (RuntimeException e) {
69 LOGGER.trace("{}: lock is already taken or process is already running", Thread.currentThread().getName());
73 private void doDatabaseMigration() {
74 migrationState.setStatus(Status.RUNNING);
75 migrationState.setStartedAt(new Date());
76 migrationState.setError(null);
77 Profiler profiler = Profiler.create(LOGGER);
79 profiler.startInfo("Starting DB Migration and container restart");
82 migrationState.setStatus(Status.SUCCEEDED);
83 profiler.stopInfo("DB Migration and container restart: success");
84 } catch (MigrationStepExecutionException e) {
85 profiler.stopError("DB migration failed");
86 LOGGER.error("DB migration ended with an exception", e);
88 } catch (Throwable t) {
89 profiler.stopError("Container restart failed");
90 LOGGER.error("Container restart failed", t);
97 private void saveStatus(Throwable e) {
98 migrationState.setStatus(Status.FAILED);
99 migrationState.setError(e);
102 private void doUpgradeDb() {
103 Profiler profiler = Profiler.createIfTrace(LOGGER);
104 profiler.startTrace("Starting DB Migration");
105 migrationEngine.execute();
106 profiler.stopTrace("DB Migration ended");
109 private void doRestartContainer() {
110 Profiler profiler = Profiler.createIfTrace(LOGGER);
111 profiler.startTrace("Restarting container");
113 profiler.stopTrace("Container restarted successfully");