From 2e42d82b6033763f1d7bd410cd13f6c65b2cb094 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 4 May 2015 17:35:58 +0200 Subject: [PATCH] SONAR-6365 implement DB check in /api/system/status --- .../platform/ws/SystemStatusWsAction.java | 19 ++++++- .../platform/ws/SystemStatusWsActionTest.java | 47 +++++++++++++++- .../sonar/core/persistence/IsAliveMapper.java | 28 ++++++++++ .../org/sonar/core/persistence/MyBatis.java | 11 ++-- .../sonar/core/persistence/IsAliveMapper.xml | 16 ++++++ .../core/persistence/IsAliveMapperTest.java | 55 +++++++++++++++++++ 6 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/persistence/IsAliveMapper.java create mode 100644 sonar-core/src/main/resources/org/sonar/core/persistence/IsAliveMapper.xml create mode 100644 sonar-core/src/test/java/org/sonar/core/persistence/IsAliveMapperTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemStatusWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemStatusWsAction.java index 6b4eadc3334..89e08b4dce5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemStatusWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemStatusWsAction.java @@ -23,7 +23,12 @@ import org.sonar.api.platform.Server; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.IsAliveMapper; +import org.sonar.server.db.DbClient; import org.sonar.server.db.migrations.DatabaseMigration; import org.sonar.server.platform.Platform; @@ -34,14 +39,18 @@ import com.google.common.io.Resources; */ public class SystemStatusWsAction implements SystemWsAction { + private static final Logger LOGGER = Loggers.get(SystemStatusWsAction.class); + private final Server server; private final DatabaseMigration databaseMigration; private final Platform platform; + private final DbClient dbClient; - public SystemStatusWsAction(Server server, DatabaseMigration databaseMigration, Platform platform) { + public SystemStatusWsAction(Server server, DatabaseMigration databaseMigration, Platform platform, DbClient dbClient) { this.server = server; this.databaseMigration = databaseMigration; this.platform = platform; + this.dbClient = dbClient; } @Override @@ -98,8 +107,12 @@ public class SystemStatusWsAction implements SystemWsAction { } private boolean isConnectedToDB() { - // TODO check DB connection is up - return true; + try (DbSession dbSession = dbClient.openSession(false)) { + return dbSession.getMapper(IsAliveMapper.class).isAlive() == IsAliveMapper.IS_ALIVE_RETURNED_VALUE; + } catch (RuntimeException e) { + LOGGER.error("DB connection is down", e); + return false; + } } private Status computeFromDbMigrationStatus() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemStatusWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemStatusWsActionTest.java index 2abcce7797e..ddcdaa9f9bd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemStatusWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemStatusWsActionTest.java @@ -22,10 +22,14 @@ package org.sonar.server.platform.ws; import java.io.File; import java.util.Date; import java.util.Set; +import org.junit.Before; import org.junit.Test; import org.sonar.api.platform.Server; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.WebService; +import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.IsAliveMapper; +import org.sonar.server.db.DbClient; import org.sonar.server.db.migrations.DatabaseMigration; import org.sonar.server.platform.Platform; import org.sonar.server.ws.WsTester; @@ -36,6 +40,7 @@ import static com.google.common.collect.ImmutableSet.of; import static com.google.common.collect.Iterables.filter; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.anyBoolean; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.test.JsonAssert.assertJson; @@ -56,8 +61,18 @@ public class SystemStatusWsActionTest { private static Server server = new Dummy51Server(); private DatabaseMigration databaseMigration = mock(DatabaseMigration.class); private Platform platform = mock(Platform.class); + private DbClient dbClient = mock(DbClient.class); + private DbSession dbSession = mock(DbSession.class); + private IsAliveMapper isAliveMapper = mock(IsAliveMapper.class); + private SystemStatusWsAction underTest = new SystemStatusWsAction(server, databaseMigration, platform, dbClient); + private Request request = mock(Request.class); - private SystemStatusWsAction underTest = new SystemStatusWsAction(server, databaseMigration, platform); + + @Before + public void wireMocks() throws Exception { + when(dbClient.openSession(anyBoolean())).thenReturn(dbSession); + when(dbSession.getMapper(IsAliveMapper.class)).thenReturn(isAliveMapper); + } @Test public void action_status_is_defined() throws Exception { @@ -80,6 +95,7 @@ public class SystemStatusWsActionTest { @Test public void verify_example() throws Exception { + when(isAliveMapper.isAlive()).thenReturn(IsAliveMapper.IS_ALIVE_RETURNED_VALUE); when(platform.status()).thenReturn(Platform.Status.UP); WsTester.TestResponse response = new WsTester.TestResponse(); @@ -122,6 +138,34 @@ public class SystemStatusWsActionTest { verifyStatus(Platform.Status.SAFEMODE, DatabaseMigration.Status.FAILED, STATUS_DOWN); } + @Test + public void status_is_DOWN_if_any_error_occurs_when_checking_DB() throws Exception { + when(isAliveMapper.isAlive()).thenThrow(new RuntimeException("simulated runtime exception when querying DB")); + + WsTester.TestResponse response = new WsTester.TestResponse(); + underTest.handle(request, response); + + assertJson(response.outputAsString()).isSimilarTo("{" + + " \"status\": \"DOWN\"\n" + + "}"); + } + + /** + * By contract {@link IsAliveMapper#isAlive()} can not return anything but 1. Still we write this test as a + * protection against change in this contract. + */ + @Test + public void status_is_DOWN_if_isAlive_does_not_return_1() throws Exception { + when(isAliveMapper.isAlive()).thenReturn(12); + + WsTester.TestResponse response = new WsTester.TestResponse(); + underTest.handle(request, response); + + assertJson(response.outputAsString()).isSimilarTo("{" + + " \"status\": \"" + STATUS_DOWN + "\"\n" + + "}"); + } + @Test public void safety_test_for_new_platform_status() throws Exception { for (Platform.Status platformStatus : filter(asList(Platform.Status.values()), not(in(SUPPORTED_PLATFORM_STATUSES)))) { @@ -143,6 +187,7 @@ public class SystemStatusWsActionTest { } private void verifyStatus(Platform.Status platformStatus, DatabaseMigration.Status databaseMigrationStatus, String expectedStatus) throws Exception { + when(isAliveMapper.isAlive()).thenReturn(IsAliveMapper.IS_ALIVE_RETURNED_VALUE); when(platform.status()).thenReturn(platformStatus); when(databaseMigration.status()).thenReturn(databaseMigrationStatus); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/IsAliveMapper.java b/sonar-core/src/main/java/org/sonar/core/persistence/IsAliveMapper.java new file mode 100644 index 00000000000..5c8814abf91 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/persistence/IsAliveMapper.java @@ -0,0 +1,28 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.core.persistence; + +public interface IsAliveMapper { + static final int IS_ALIVE_RETURNED_VALUE = 1; + /** + * Always return {@link #IS_ALIVE_RETURNED_VALUE} unless a database or connection error occurs. + */ + int isAlive(); +} diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java index fefd39960ce..f231431ce7f 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java @@ -20,8 +20,10 @@ package org.sonar.core.persistence; -import ch.qos.logback.classic.Level; -import com.google.common.io.Closeables; +import java.io.InputStream; + +import javax.annotation.Nullable; + import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.mapping.Environment; @@ -139,9 +141,9 @@ import org.sonar.core.user.UserGroupMapper; import org.sonar.core.user.UserMapper; import org.sonar.core.user.UserRoleDto; -import javax.annotation.Nullable; +import ch.qos.logback.classic.Level; -import java.io.InputStream; +import com.google.common.io.Closeables; public class MyBatis implements BatchComponent, ServerComponent { @@ -251,6 +253,7 @@ public class MyBatis implements BatchComponent, ServerComponent { Class[] mappers = {ActivityMapper.class, ActiveDashboardMapper.class, AuthorMapper.class, DashboardMapper.class, FileDependencyMapper.class, DuplicationMapper.class, GraphDtoMapper.class, IssueMapper.class, IssueChangeMapper.class, IssueFilterMapper.class, IssueFilterFavouriteMapper.class, + IsAliveMapper.class, LoadedTemplateMapper.class, MeasureFilterMapper.class, Migration44Mapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class, ResourceKeyUpdaterMapper.class, ResourceIndexerMapper.class, RoleMapper.class, RuleMapper.class, SchemaMigrationMapper.class, SemaphoreMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, WidgetMapper.class, WidgetPropertyMapper.class, diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/IsAliveMapper.xml b/sonar-core/src/main/resources/org/sonar/core/persistence/IsAliveMapper.xml new file mode 100644 index 00000000000..4fd531e52d1 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/IsAliveMapper.xml @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/IsAliveMapperTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/IsAliveMapperTest.java new file mode 100644 index 00000000000..ff5b27a0271 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/persistence/IsAliveMapperTest.java @@ -0,0 +1,55 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.core.persistence; + +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.sonar.test.DbTests; + +import static org.assertj.core.api.Assertions.assertThat; + +@Category(DbTests.class) +public class IsAliveMapperTest { + + @ClassRule + public static DbTester dbTester = new DbTester(); + + DbSession session; + IsAliveMapper underTest; + + @Before + public void setUp() throws Exception { + session = dbTester.myBatis().openSession(false); + underTest = session.getMapper(IsAliveMapper.class); + } + + @After + public void tearDown() throws Exception { + session.close(); + } + + @Test + public void isAlive_works_for_current_vendors() throws Exception { + assertThat(underTest.isAlive()).isEqualTo(IsAliveMapper.IS_ALIVE_RETURNED_VALUE); + } +} -- 2.39.5