]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6365 implement DB check in /api/system/status 288/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 4 May 2015 15:35:58 +0000 (17:35 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 11 May 2015 10:29:48 +0000 (12:29 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemStatusWsAction.java
server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemStatusWsActionTest.java
sonar-core/src/main/java/org/sonar/core/persistence/IsAliveMapper.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
sonar-core/src/main/resources/org/sonar/core/persistence/IsAliveMapper.xml [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/persistence/IsAliveMapperTest.java [new file with mode: 0644]

index 6b4eadc33344ab56f683b49c72520e390d11df80..89e08b4dce58d62bd63bd2959e1f6cddbf8746d8 100644 (file)
@@ -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() {
index 2abcce7797ed1bc04a30038aa399e26298b12828..ddcdaa9f9bdee94695f8873598f3e47d05ea29e3 100644 (file)
@@ -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 (file)
index 0000000..5c8814a
--- /dev/null
@@ -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();
+}
index fefd39960cee5ed5c866955004e58e0e6eb7f883..f231431ce7f62e60c530f39aa7304bd4fafdaab2 100644 (file)
 
 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 (file)
index 0000000..4fd531e
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.persistence.IsAliveMapper">
+
+  <select id="isAlive" resultType="int" >
+    select 1
+    <choose>
+      <when test="_databaseId == 'oracle'">
+        from dual
+      </when>
+    </choose>
+  </select>
+
+</mapper>
+
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 (file)
index 0000000..ff5b27a
--- /dev/null
@@ -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);
+  }
+}