From 5750cd738ffe93225fb5619a6a2cba2d2ddb4ec2 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 29 Dec 2011 16:50:37 +0100 Subject: [PATCH] Indexation of resources during migration must be reentrant --- .../org/sonar/jpa/entity/SchemaMigration.java | 4 +- .../org/sonar/core/persistence/rows-derby.sql | 1 + .../org/sonar/server/platform/Platform.java | 4 +- .../sonar/server/startup/IndexProjects.java | 60 ---------------- .../java/org/sonar/server/ui/JRubyFacade.java | 9 ++- .../WEB-INF/db/migrate/241_index_resources.rb | 32 +++++++++ .../server/startup/IndexProjectsTest.java | 68 ------------------- 7 files changed, 45 insertions(+), 133 deletions(-) delete mode 100644 sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java create mode 100644 sonar-server/src/main/webapp/WEB-INF/db/migrate/241_index_resources.rb delete mode 100644 sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java index 13360df8748..f7429e9ad50 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java +++ b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java @@ -34,8 +34,8 @@ public class SchemaMigration { public final static int VERSION_UNKNOWN = -1; - public static final int LAST_VERSION = 240; - public static final int VERSION_2_13 = 240; + public static final int LAST_VERSION = 241; + public static final int VERSION_2_13 = 241; public final static String TABLE_NAME = "schema_migrations"; diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql index ac666f114d5..a1bf630ef60 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-derby.sql @@ -167,6 +167,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('237'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('238'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('239'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('240'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('241'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 8b4e966fb3c..492e5090f0c 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -126,6 +126,7 @@ public final class Platform { } private void startDatabaseConnectors(ServletContext servletContext) { + System.out.println("Start database connectors"); rootContainer = new ComponentContainer(); rootContainer.addSingleton(servletContext); rootContainer.addSingleton(IocContainer.class); // for backward compatibility @@ -134,7 +135,7 @@ public final class Platform { rootContainer.addSingleton(EmbeddedDatabaseFactory.class); rootContainer.addSingleton(DefaultDatabase.class); rootContainer.addSingleton(MyBatis.class); - rootContainer.addSingleton(ResourceIndexer.class); // for the migration -> see org.sonar.server.startup.IndexProjects + rootContainer.addSingleton(ResourceIndexer.class); // for the migration 241 rootContainer.addSingleton(DefaultDatabaseConnector.class); rootContainer.addSingleton(DefaultServerUpgradeStatus.class); rootContainer.addSingleton(DatabaseMigrator.class); @@ -235,7 +236,6 @@ public final class Platform { startupContainer.addSingleton(DeleteDeprecatedMeasures.class); startupContainer.addSingleton(GeneratePluginIndex.class); startupContainer.addSingleton(RegisterNewDashboards.class); - startupContainer.addSingleton(IndexProjects.class); startupContainer.startComponents(); startupContainer.getComponentByType(ServerLifecycleNotifier.class).notifyStart(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java b/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java deleted file mode 100644 index 14403cadb79..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/startup/IndexProjects.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.server.startup; - -import org.sonar.api.ServerComponent; -import org.sonar.api.platform.ServerUpgradeStatus; -import org.sonar.api.utils.TimeProfiler; -import org.sonar.core.resource.ResourceIndexer; -import org.sonar.jpa.entity.SchemaMigration; - -/** - * Index existing projects during migration to 2.13. Since this latter version, resources are automatically indexed - * during project analysis. - * - * @since 2.13 - */ -public class IndexProjects implements ServerComponent { - - private ServerUpgradeStatus upgradeStatus; - private ResourceIndexer indexer; - - public IndexProjects(ServerUpgradeStatus upgradeStatus, ResourceIndexer indexer) { - this.upgradeStatus = upgradeStatus; - this.indexer = indexer; - } - - public void start() { - if (shouldIndex()) { - index(); - } - } - - private boolean shouldIndex() { - return upgradeStatus.isUpgraded() && upgradeStatus.getInitialDbVersion() < SchemaMigration.VERSION_2_13; - } - - private void index() { - TimeProfiler profiler = new TimeProfiler().start("Index projects"); - indexer.indexAll(); - profiler.stop(); - } - -} diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index cad0e11e695..927defeac4c 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -34,9 +34,10 @@ import org.sonar.api.rules.RuleRepository; import org.sonar.api.utils.ValidationMessages; import org.sonar.api.web.*; import org.sonar.core.i18n.RuleI18nManager; -import org.sonar.markdown.Markdown; import org.sonar.core.persistence.Database; import org.sonar.core.persistence.DatabaseMigrator; +import org.sonar.core.resource.ResourceIndexer; +import org.sonar.markdown.Markdown; import org.sonar.server.configuration.Backup; import org.sonar.server.configuration.ProfilesManager; import org.sonar.server.filters.Filter; @@ -367,6 +368,10 @@ public final class JRubyFacade { return i18n.getJsDictionnary(rubyLocale); } + public void indexResources() { + getContainer().getComponentByType(ResourceIndexer.class).indexAll(); + } + public void logError(String message) { LoggerFactory.getLogger(getClass()).error(message); } @@ -378,4 +383,6 @@ public final class JRubyFacade { public ComponentContainer getContainer() { return Platform.getInstance().getContainer(); } + + } diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/241_index_resources.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/241_index_resources.rb new file mode 100644 index 00000000000..d0e43896e5a --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/241_index_resources.rb @@ -0,0 +1,32 @@ +# +# Sonar, entreprise quality control tool. +# Copyright (C) 2008-2011 SonarSource +# mailto:contact AT sonarsource DOT com +# +# Sonar 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. +# +# Sonar 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 Sonar; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# + +# +# Note: this migration must be executed after 240_delete_resource_orphans. +# +# Sonar 2.13 +# +class IndexResources < ActiveRecord::Migration + + def self.up + Java::OrgSonarServerUi::JRubyFacade.getInstance().indexResources() + end + +end diff --git a/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java deleted file mode 100644 index b3d1f4692a4..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/startup/IndexProjectsTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.server.startup; - -import org.junit.Test; -import org.sonar.api.platform.ServerUpgradeStatus; -import org.sonar.core.resource.ResourceIndexer; -import org.sonar.jpa.entity.SchemaMigration; - -import static org.mockito.Mockito.*; - -public class IndexProjectsTest { - - @Test - public void doNotIndexOnFreshInstalls() { - ResourceIndexer indexer = mock(ResourceIndexer.class); - ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); - when(status.isUpgraded()).thenReturn(false); - when(status.isFreshInstall()).thenReturn(true); - - new IndexProjects(status, indexer).start(); - - verifyZeroInteractions(indexer); - } - - @Test - public void doNotIndexOnUpgradesSince213() { - ResourceIndexer indexer = mock(ResourceIndexer.class); - ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); - when(status.isUpgraded()).thenReturn(true); - when(status.isFreshInstall()).thenReturn(false); - when(status.getInitialDbVersion()).thenReturn(SchemaMigration.VERSION_2_13 + 10); - - new IndexProjects(status, indexer).start(); - - verifyZeroInteractions(indexer); - } - - @Test - public void doIndexOnUpgradeBefore213() { - ResourceIndexer indexer = mock(ResourceIndexer.class); - ServerUpgradeStatus status = mock(ServerUpgradeStatus.class); - when(status.isUpgraded()).thenReturn(true); - when(status.isFreshInstall()).thenReturn(false); - when(status.getInitialDbVersion()).thenReturn(SchemaMigration.VERSION_2_13 - 10); - - new IndexProjects(status, indexer).start(); - - verify(indexer).indexAll(); - } -} -- 2.39.5