From 8bc3e72c0b4ad94cf25dc3fdf53f82cc6cf52808 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 24 Sep 2017 22:27:08 +0200 Subject: [PATCH] SONAR-9802 improve name of Database sections --- .../container/ComputeEngineContainerImpl.java | 4 +- ...eSection.java => DbConnectionSection.java} | 32 +++------ ...ean.java => DbConnectionSectionMBean.java} | 2 +- .../server/platform/monitoring/DbSection.java | 60 ++++++++++++++++ .../monitoring/WebSystemInfoModule.java | 10 ++- .../server/platform/ws/BaseInfoWsAction.java | 7 +- .../monitoring/DbConnectionSectionTest.java | 70 +++++++++++++++++++ ...aseSectionTest.java => DbSectionTest.java} | 25 +------ .../monitoring/SystemInfoTesting.java | 5 +- .../pageobjects/SystemInfoPageItem.java | 3 +- .../sonarqube/tests/cluster/ClusterTest.java | 3 +- .../tests/serverSystem/SystemInfoTest.java | 2 +- 12 files changed, 163 insertions(+), 60 deletions(-) rename server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/{DatabaseSection.java => DbConnectionSection.java} (74%) rename server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/{DatabaseSectionMBean.java => DbConnectionSectionMBean.java} (98%) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbSection.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbConnectionSectionTest.java rename server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/{DatabaseSectionTest.java => DbSectionTest.java} (68%) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index c5e45edfa25..3cf5f769c7f 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -126,7 +126,7 @@ import org.sonar.server.platform.UrlSettings; import org.sonar.server.platform.WebServerImpl; import org.sonar.server.platform.db.migration.MigrationConfigurationModule; import org.sonar.server.platform.db.migration.version.DatabaseVersion; -import org.sonar.server.platform.monitoring.DatabaseSection; +import org.sonar.server.platform.monitoring.DbSection; import org.sonar.server.platform.monitoring.cluster.LoggingSection; import org.sonar.server.platform.monitoring.cluster.ProcessInfoProvider; import org.sonar.server.plugins.InstalledPluginReferentialFactory; @@ -430,7 +430,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { CeDistributedInformationImpl.class, // system info - DatabaseSection.class, + DbSection.class, ProcessInfoProvider.class, LoggingSection.class); } else { diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DatabaseSection.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbConnectionSection.java similarity index 74% rename from server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DatabaseSection.java rename to server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbConnectionSection.java index a64e8d25bb1..4cf013014c5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DatabaseSection.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbConnectionSection.java @@ -19,27 +19,28 @@ */ package org.sonar.server.platform.monitoring; -import java.sql.DatabaseMetaData; -import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; +import org.sonar.api.SonarQubeSide; +import org.sonar.api.SonarRuntime; import org.sonar.db.DbClient; -import org.sonar.db.DbSession; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section; import org.sonar.server.platform.db.migration.version.DatabaseVersion; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; /** - * Information about database and connection pool + * Information about database connection pool */ -public class DatabaseSection extends BaseSectionMBean implements DatabaseSectionMBean { +public class DbConnectionSection extends BaseSectionMBean implements DbConnectionSectionMBean { private final DatabaseVersion dbVersion; private final DbClient dbClient; + private final SonarRuntime runtime; - public DatabaseSection(DatabaseVersion dbVersion, DbClient dbClient) { + public DbConnectionSection(DatabaseVersion dbVersion, DbClient dbClient, SonarRuntime runtime) { this.dbVersion = dbVersion; this.dbClient = dbClient; + this.runtime = runtime; } @Override @@ -100,8 +101,8 @@ public class DatabaseSection extends BaseSectionMBean implements DatabaseSection @Override public Section toProtobuf() { Section.Builder protobuf = Section.newBuilder(); - protobuf.setName(name()); - completeDbAttributes(protobuf); + String side = runtime.getSonarQubeSide() == SonarQubeSide.COMPUTE_ENGINE ? "Compute Engine" : "Web"; + protobuf.setName(side + " Database Connection"); completePoolAttributes(protobuf); return protobuf.build(); } @@ -121,19 +122,4 @@ public class DatabaseSection extends BaseSectionMBean implements DatabaseSection private BasicDataSource commonsDbcp() { return (BasicDataSource) dbClient.getDatabase().getDataSource(); } - - private void completeDbAttributes(Section.Builder protobuf) { - try (DbSession dbSession = dbClient.openSession(false)) { - DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); - setAttribute(protobuf, "Database", metadata.getDatabaseProductName()); - setAttribute(protobuf, "Database Version", metadata.getDatabaseProductVersion()); - setAttribute(protobuf, "Username", metadata.getUserName()); - setAttribute(protobuf, "URL", metadata.getURL()); - setAttribute(protobuf, "Driver", metadata.getDriverName()); - setAttribute(protobuf, "Driver Version", metadata.getDriverVersion()); - setAttribute(protobuf, "Version Status", getMigrationStatus()); - } catch (SQLException e) { - throw new IllegalStateException("Fail to get DB metadata", e); - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DatabaseSectionMBean.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbConnectionSectionMBean.java similarity index 98% rename from server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DatabaseSectionMBean.java rename to server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbConnectionSectionMBean.java index 0ddd97fff68..5f225ccb56d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DatabaseSectionMBean.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbConnectionSectionMBean.java @@ -19,7 +19,7 @@ */ package org.sonar.server.platform.monitoring; -public interface DatabaseSectionMBean { +public interface DbConnectionSectionMBean { /** * Is database schema up-to-date or should it be upgraded ? diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbSection.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbSection.java new file mode 100644 index 00000000000..4162c074a5b --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/DbSection.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.platform.monitoring; + +import java.sql.DatabaseMetaData; +import java.sql.SQLException; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.process.systeminfo.Global; +import org.sonar.process.systeminfo.SystemInfoSection; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section; + +import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; + +/** + * Information about database + */ +public class DbSection implements SystemInfoSection, Global { + + private final DbClient dbClient; + + public DbSection(DbClient dbClient) { + this.dbClient = dbClient; + } + + @Override + public Section toProtobuf() { + Section.Builder protobuf = Section.newBuilder(); + protobuf.setName("Database"); + try (DbSession dbSession = dbClient.openSession(false)) { + DatabaseMetaData metadata = dbSession.getConnection().getMetaData(); + setAttribute(protobuf, "Database", metadata.getDatabaseProductName()); + setAttribute(protobuf, "Database Version", metadata.getDatabaseProductVersion()); + setAttribute(protobuf, "Username", metadata.getUserName()); + setAttribute(protobuf, "URL", metadata.getURL()); + setAttribute(protobuf, "Driver", metadata.getDriverName()); + setAttribute(protobuf, "Driver Version", metadata.getDriverVersion()); + } catch (SQLException e) { + throw new IllegalStateException("Fail to get DB metadata", e); + } + return protobuf.build(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/WebSystemInfoModule.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/WebSystemInfoModule.java index aae7dc5c087..9335402242a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/WebSystemInfoModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/WebSystemInfoModule.java @@ -22,11 +22,12 @@ package org.sonar.server.platform.monitoring; import org.sonar.process.systeminfo.JvmPropertiesSection; import org.sonar.process.systeminfo.JvmStateSection; import org.sonar.server.platform.monitoring.cluster.AppNodesInfoLoaderImpl; +import org.sonar.server.platform.monitoring.cluster.CeQueueGlobalSection; import org.sonar.server.platform.monitoring.cluster.GlobalInfoLoader; import org.sonar.server.platform.monitoring.cluster.GlobalSystemSection; -import org.sonar.server.platform.monitoring.cluster.ProcessInfoProvider; import org.sonar.server.platform.monitoring.cluster.LoggingSection; import org.sonar.server.platform.monitoring.cluster.NodeSystemSection; +import org.sonar.server.platform.monitoring.cluster.ProcessInfoProvider; import org.sonar.server.platform.monitoring.cluster.SearchNodesInfoLoaderImpl; import org.sonar.server.platform.ws.ClusterInfoAction; import org.sonar.server.platform.ws.InfoAction; @@ -41,7 +42,8 @@ public class WebSystemInfoModule { return new Object[] { new JvmPropertiesSection("Web JVM Properties"), new JvmStateSection("Web JVM State"), - DatabaseSection.class, + DbSection.class, + DbConnectionSection.class, EsStateSection.class, EsIndexesSection.class, PluginsSection.class, @@ -58,7 +60,9 @@ public class WebSystemInfoModule { return new Object[] { new JvmPropertiesSection("Web JVM Properties"), new JvmStateSection("Web JVM State"), - DatabaseSection.class, + CeQueueGlobalSection.class, + DbSection.class, + DbConnectionSection.class, EsIndexesSection.class, GlobalSystemSection.class, LoggingSection.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/BaseInfoWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/BaseInfoWsAction.java index f50aebe0c65..f6c8ff5aa05 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/BaseInfoWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/BaseInfoWsAction.java @@ -35,10 +35,13 @@ import static org.sonar.server.telemetry.TelemetryDataJsonWriter.writeTelemetryD public abstract class BaseInfoWsAction implements SystemWsAction { private static final String[] ORDERED_SECTION_NAMES = { + // standalone "System", "Database", "Plugins", + + // cluster "Web JVM State", "Web Database Connection", "Web Logging", "Web JVM Properties", - "Search State", "Search Indexes", - "Compute Engine Tasks", "Compute Engine JVM State", "Compute Engine Database Connection", "Compute Engine Logging", "Compute Engine JVM Properties"}; + "Compute Engine Tasks", "Compute Engine JVM State", "Compute Engine Database Connection", "Compute Engine Logging", "Compute Engine JVM Properties", + "Search State", "Search Indexes"}; private final UserSession userSession; private final TelemetryDataLoader telemetry; diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbConnectionSectionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbConnectionSectionTest.java new file mode 100644 index 00000000000..cb9e71112a6 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbConnectionSectionTest.java @@ -0,0 +1,70 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 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.platform.monitoring; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.SonarQubeSide; +import org.sonar.api.SonarRuntime; +import org.sonar.api.utils.System2; +import org.sonar.db.DbTester; +import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; +import org.sonar.server.platform.db.migration.version.DatabaseVersion; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; + +public class DbConnectionSectionTest { + + @Rule + public DbTester dbTester = DbTester.create(System2.INSTANCE); + + private DatabaseVersion databaseVersion = mock(DatabaseVersion.class); + private SonarRuntime runtime = mock(SonarRuntime.class); + private DbConnectionSection underTest = new DbConnectionSection(databaseVersion, dbTester.getDbClient(), runtime); + + @Test + public void jmx_name_is_not_empty() { + assertThat(underTest.name()).isEqualTo("Database"); + } + + @Test + public void pool_info() { + ProtobufSystemInfo.Section section = underTest.toProtobuf(); + assertThat(attribute(section, "Pool Max Connections").getLongValue()).isGreaterThan(0L); + assertThat(attribute(section, "Pool Idle Connections").getLongValue()).isGreaterThanOrEqualTo(0L); + assertThat(attribute(section, "Pool Min Idle Connections").getLongValue()).isGreaterThanOrEqualTo(0L); + assertThat(attribute(section, "Pool Max Idle Connections").getLongValue()).isGreaterThanOrEqualTo(0L); + assertThat(attribute(section, "Pool Max Wait (ms)")).isNotNull(); + assertThat(attribute(section, "Pool Remove Abandoned")).isNotNull(); + assertThat(attribute(section, "Pool Remove Abandoned Timeout (seconds)").getLongValue()).isGreaterThanOrEqualTo(0L); + } + + @Test + public void section_name_depends_on_runtime_side() { + when(runtime.getSonarQubeSide()).thenReturn(SonarQubeSide.COMPUTE_ENGINE); + assertThat(underTest.toProtobuf().getName()).isEqualTo("Compute Engine Database Connection"); + + when(runtime.getSonarQubeSide()).thenReturn(SonarQubeSide.SERVER ); + assertThat(underTest.toProtobuf().getName()).isEqualTo("Web Database Connection"); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseSectionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbSectionTest.java similarity index 68% rename from server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseSectionTest.java rename to server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbSectionTest.java index f4139c81f07..429c7ffb21d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DatabaseSectionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/DbSectionTest.java @@ -19,37 +19,22 @@ */ package org.sonar.server.platform.monitoring; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; -import org.sonar.server.platform.db.migration.version.DatabaseVersion; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs; -public class DatabaseSectionTest { +public class DbSectionTest { @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - private DatabaseVersion databaseVersion = mock(DatabaseVersion.class); - private DatabaseSection underTest = new DatabaseSection(databaseVersion, dbTester.getDbClient()); - - @Before - public void setUp() throws Exception { - when(databaseVersion.getStatus()).thenReturn(DatabaseVersion.Status.UP_TO_DATE); - } - - @Test - public void name_is_not_empty() { - assertThat(underTest.name()).isNotEmpty(); - } + private DbSection underTest = new DbSection(dbTester.getDbClient()); @Test public void db_info() { @@ -59,10 +44,4 @@ public class DatabaseSectionTest { assertThatAttributeIs(section, "Username", "SONAR"); assertThat(attribute(section, "Driver Version").getStringValue()).startsWith("1."); } - - @Test - public void pool_info() { - ProtobufSystemInfo.Section section = underTest.toProtobuf(); - assertThat(attribute(section, "Pool Max Connections").getLongValue()).isGreaterThan(0L); - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java index 2a2c0d9fb9a..528ba9785f0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java @@ -19,7 +19,6 @@ */ package org.sonar.server.platform.monitoring; -import java.util.Collection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import static org.assertj.core.api.Assertions.assertThat; @@ -43,9 +42,9 @@ public class SystemInfoTesting { assertThat(value.getBooleanValue()).isEqualTo(expectedValue); } - public static void assertThatAttributeHasOnlyValues(ProtobufSystemInfo.Section section, String key, Collection expectedValues) { + public static void assertThatAttributeIs(ProtobufSystemInfo.Section section, String key, long expectedValue) { ProtobufSystemInfo.Attribute value = attribute(section, key); assertThat(value).as(key).isNotNull(); - assertThat((Collection)value.getStringValuesList()).containsExactlyInAnyOrder(expectedValues.toArray(new String[expectedValues.size()])); + assertThat(value.getLongValue()).isEqualTo(expectedValue); } } diff --git a/tests/src/test/java/org/sonarqube/pageobjects/SystemInfoPageItem.java b/tests/src/test/java/org/sonarqube/pageobjects/SystemInfoPageItem.java index 19a023e165f..c477c874cb3 100644 --- a/tests/src/test/java/org/sonarqube/pageobjects/SystemInfoPageItem.java +++ b/tests/src/test/java/org/sonarqube/pageobjects/SystemInfoPageItem.java @@ -21,6 +21,7 @@ package org.sonarqube.pageobjects; import com.codeborne.selenide.SelenideElement; +import static com.codeborne.selenide.Condition.exactText; import static com.codeborne.selenide.Condition.exist; import static com.codeborne.selenide.Condition.text; @@ -62,7 +63,7 @@ public class SystemInfoPageItem { public SystemInfoPageItem shouldNotHaveField(String field) { ensureOpen(); - elt.$$(".system-info-section-item-name").findBy(text(field)).shouldNot(exist); + elt.$$(".system-info-section-item-name").findBy(exactText(field)).shouldNot(exist); return this; } diff --git a/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java b/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java index b7221c8bb03..9ddde1ac32d 100644 --- a/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java +++ b/tests/src/test/java/org/sonarqube/tests/cluster/ClusterTest.java @@ -141,7 +141,8 @@ public class ClusterTest { page.getCardItem("System") .shouldHaveHealth() .shouldHaveMainSection() - .shouldNotHaveSection("Database") + .shouldHaveSection("Database") + .shouldHaveField("Database Version") .shouldNotHaveSection("Settings") .shouldNotHaveSection("Plugins") .shouldHaveField("High Availability") diff --git a/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemInfoTest.java b/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemInfoTest.java index 605b058df26..c3d7a888f47 100644 --- a/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemInfoTest.java +++ b/tests/src/test/java/org/sonarqube/tests/serverSystem/SystemInfoTest.java @@ -77,7 +77,7 @@ public class SystemInfoTest { page.getCardItem("Search") .shouldHaveSection("Search State") - .shouldHaveSection("Search Statistics"); + .shouldHaveSection("Search Indexes"); } @Test -- 2.39.5