kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | cf32ad67f65d9069fcf9fdf3c3bdc1ff4e52b5ffff8136514d71c07be17c3e4a |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-10-13 11:30 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-10-13 11:30 +02:00 |
| Parents | d0cf52e93999, 88804a2cfe18 |
6 files changed
modified changelog/README.md +1 −0
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | - [bug] Fix implementation of UserType.hashCode() (JAVA-942) |
| 7 | 7 | - [improvement] Don't delay UP/ADDED notifications if protocol version = V4 (JAVA-877) |
| 8 | 8 | - [improvement] Parse 'extensions' column in table metadata (JAVA-938) |
| 9 | +- [bug] Fix Configuration builder to allow disabled metrics (JAVA-900) | |
| 9 | 10 | |
| 10 | 11 | |
| 11 | 12 | ### 3.0.0-alpha3 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +5 −4
| @@ -396,8 +396,8 @@ | ||
| 396 | 396 | * The cluster metrics. |
| 397 | 397 | * |
| 398 | 398 | * @return the cluster metrics, or {@code null} if metrics collection has |
| 399 | - * been disabled (that is if {@link Configuration#getMetricsOptions} | |
| 400 | - * returns {@code null}). | |
| 399 | + * been disabled (that is if {@code cluster.getConfiguration().getMetricsOptions().isEnabled()} | |
| 400 | + * returns {@code false}). | |
| 401 | 401 | */ |
| 402 | 402 | public Metrics getMetrics() { |
| 403 | 403 | checkNotClosed(manager); |
| @@ -1202,7 +1202,8 @@ | ||
| 1202 | 1202 | public Configuration getConfiguration() { |
| 1203 | 1203 | ProtocolOptions protocolOptions = new ProtocolOptions(port, protocolVersion, maxSchemaAgreementWaitSeconds, sslOptions, authProvider) |
| 1204 | 1204 | .setCompression(compression); |
| 1205 | - MetricsOptions metricsOptions = metricsEnabled ? new MetricsOptions(jmxEnabled) : null; | |
| 1205 | + | |
| 1206 | + MetricsOptions metricsOptions = new MetricsOptions(metricsEnabled, jmxEnabled); | |
| 1206 | 1207 | |
| 1207 | 1208 | return configurationBuilder |
| 1208 | 1209 | .withProtocolOptions(protocolOptions) |
| @@ -1328,7 +1329,7 @@ | ||
| 1328 | 1329 | this.metadata = new Metadata(this); |
| 1329 | 1330 | this.connectionFactory = new Connection.Factory(this, configuration); |
| 1330 | 1331 | this.controlConnection = new ControlConnection(this); |
| 1331 | - this.metrics = configuration.getMetricsOptions() == null ? null : new Metrics(this); | |
| 1332 | + this.metrics = configuration.getMetricsOptions().isEnabled() ? new Metrics(this) : null; | |
| 1332 | 1333 | this.preparedQueries = new MapMaker().weakValues().makeMap(); |
| 1333 | 1334 | |
| 1334 | 1335 | // create debouncers - at this stage, they are not running yet |
modified driver-core/src/main/java/com/datastax/driver/core/Configuration.java +9 −0
| @@ -35,6 +35,9 @@ | ||
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Returns a builder to create a new {@code Configuration} object. |
| 38 | + * <p> | |
| 39 | + * You only need this if you are building the configuration yourself. If you | |
| 40 | + * use {@link Cluster#builder()}, it will be done under the hood for you. | |
| 38 | 41 | * |
| 39 | 42 | * @return the builder. |
| 40 | 43 | */ |
| @@ -209,6 +212,12 @@ | ||
| 209 | 212 | |
| 210 | 213 | /** |
| 211 | 214 | * Sets the metrics options for this cluster. |
| 215 | + * <p> | |
| 216 | + * If this method doesn't get called, the configuration will use the | |
| 217 | + * defaults: metrics enabled with JMX reporting enabled. | |
| 218 | + * To disable metrics, call this method with an instance where | |
| 219 | + * {@link MetricsOptions#isEnabled() isEnabled()} returns false. | |
| 220 | + * | |
| 212 | 221 | * @param metricsOptions the metrics options. |
| 213 | 222 | * @return this builder. |
| 214 | 223 | */ |
modified driver-core/src/main/java/com/datastax/driver/core/MetricsOptions.java +17 −9
| @@ -20,14 +20,14 @@ | ||
| 20 | 20 | */ |
| 21 | 21 | public class MetricsOptions { |
| 22 | 22 | |
| 23 | + private final boolean metricsEnabled; | |
| 23 | 24 | private final boolean jmxEnabled; |
| 24 | 25 | |
| 25 | 26 | /** |
| 26 | - * Creates a new {@code MetricsOptions} object with default values. | |
| 27 | + * Creates a new {@code MetricsOptions} object with default values (metrics enabled, JMX reporting enabled). | |
| 27 | 28 | */ |
| 28 | - public MetricsOptions() | |
| 29 | - { | |
| 30 | - this(true); | |
| 29 | + public MetricsOptions() { | |
| 30 | + this(true, true); | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
| @@ -35,18 +35,26 @@ | ||
| 35 | 35 | * |
| 36 | 36 | * @param jmxEnabled whether to enable JMX reporting or not. |
| 37 | 37 | */ |
| 38 | - public MetricsOptions(boolean jmxEnabled) | |
| 39 | - { | |
| 38 | + public MetricsOptions(boolean enabled, boolean jmxEnabled) { | |
| 39 | + this.metricsEnabled = enabled; | |
| 40 | 40 | this.jmxEnabled = jmxEnabled; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Returns whether JMX reporting is enabled (the default). | |
| 44 | + * Returns whether metrics are enabled. | |
| 45 | + * | |
| 46 | + * @return whether metrics are enabled. | |
| 47 | + */ | |
| 48 | + public boolean isEnabled() { | |
| 49 | + return metricsEnabled; | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Returns whether JMX reporting is enabled. | |
| 45 | 54 | * |
| 46 | 55 | * @return whether JMX reporting is enabled. |
| 47 | 56 | */ |
| 48 | - public boolean isJMXReportingEnabled() | |
| 49 | - { | |
| 57 | + public boolean isJMXReportingEnabled() { | |
| 50 | 58 | return jmxEnabled; |
| 51 | 59 | } |
| 52 | 60 | } |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −1
| @@ -221,7 +221,7 @@ | ||
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | private boolean metricsEnabled() { |
| 224 | - return manager.configuration().getMetricsOptions() != null; | |
| 224 | + return manager.configuration().getMetricsOptions().isEnabled(); | |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | private Metrics metrics() { |
modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +77 −0
| @@ -15,9 +15,15 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.lang.management.ManagementFactory; | |
| 18 | 19 | import java.util.Collection; |
| 20 | +import java.util.Collections; | |
| 19 | 21 | |
| 20 | 22 | import com.google.common.collect.Lists; |
| 23 | +import javax.management.InstanceNotFoundException; | |
| 24 | +import javax.management.MBeanInfo; | |
| 25 | +import javax.management.MBeanServer; | |
| 26 | +import javax.management.ObjectName; | |
| 21 | 27 | import org.testng.annotations.Test; |
| 22 | 28 | |
| 23 | 29 | import static org.testng.Assert.assertEquals; |
| @@ -26,9 +32,13 @@ | ||
| 26 | 32 | import com.datastax.driver.core.policies.RetryPolicy; |
| 27 | 33 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision; |
| 28 | 34 | |
| 35 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 36 | + | |
| 29 | 37 | public class MetricsTest extends CCMBridge.PerClassSingleNodeCluster { |
| 30 | 38 | private volatile RetryDecision retryDecision; |
| 31 | 39 | |
| 40 | + MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | |
| 41 | + | |
| 32 | 42 | @Override |
| 33 | 43 | protected Cluster.Builder configure(Cluster.Builder builder) { |
| 34 | 44 | return builder.withRetryPolicy(new RetryPolicy() { |
| @@ -81,4 +91,71 @@ | ||
| 81 | 91 | assertEquals(errors.getIgnores().getCount(), 1); |
| 82 | 92 | assertEquals(errors.getIgnoresOnUnavailable().getCount(), 1); |
| 83 | 93 | } |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Validates that metrics are enabled and exposed by JMX by default by checking that | |
| 97 | + * {@link Cluster#getMetrics()} is not null and 'clusterName-metrics:name=connected-to' | |
| 98 | + * MBean is present. | |
| 99 | + * | |
| 100 | + * @test_category metrics | |
| 101 | + */ | |
| 102 | + @Test(groups="short") | |
| 103 | + public void should_enable_metrics_and_jmx_by_default() throws Exception { | |
| 104 | + assertThat(cluster.getMetrics()).isNotNull(); | |
| 105 | + ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); | |
| 106 | + MBeanInfo mBean = server.getMBeanInfo(clusterMetricsON); | |
| 107 | + assertThat(mBean).isNotNull(); | |
| 108 | + | |
| 109 | + assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue(); | |
| 110 | + assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isTrue(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Validates that when metrics are disabled using {@link Cluster.Builder#withoutMetrics()} | |
| 115 | + * that {@link Cluster#getMetrics()} returns null and 'clusterName-metrics:name=connected-to' | |
| 116 | + * MBean is not present. | |
| 117 | + * | |
| 118 | + * @test_category metrics | |
| 119 | + */ | |
| 120 | + @Test(groups="short", expectedExceptions=InstanceNotFoundException.class) | |
| 121 | + public void metrics_should_be_null_when_metrics_disabled() throws Exception { | |
| 122 | + Cluster cluster = super.configure(Cluster.builder()) | |
| 123 | + .addContactPointsWithPorts(Collections.singletonList(hostAddress)) | |
| 124 | + .withoutMetrics() | |
| 125 | + .build(); | |
| 126 | + try { | |
| 127 | + cluster.init(); | |
| 128 | + assertThat(cluster.getMetrics()).isNull(); | |
| 129 | + assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isFalse(); | |
| 130 | + ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); | |
| 131 | + server.getMBeanInfo(clusterMetricsON); | |
| 132 | + } finally { | |
| 133 | + cluster.close(); | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Validates that when metrics are enabled but JMX reporting is disabled via | |
| 139 | + * {@link Cluster.Builder#withoutJMXReporting()} that {@link Cluster#getMetrics()} | |
| 140 | + * is not null and 'clusterName-metrics:name=connected-to' MBean is present. | |
| 141 | + * | |
| 142 | + * @test_category metrics | |
| 143 | + */ | |
| 144 | + @Test(groups="short", expectedExceptions=InstanceNotFoundException.class) | |
| 145 | + public void should_be_no_jmx_mbean_when_jmx_is_disabled() throws Exception { | |
| 146 | + Cluster cluster = super.configure(Cluster.builder()) | |
| 147 | + .addContactPointsWithPorts(Collections.singletonList(hostAddress)) | |
| 148 | + .withoutJMXReporting() | |
| 149 | + .build(); | |
| 150 | + try { | |
| 151 | + cluster.init(); | |
| 152 | + assertThat(cluster.getMetrics()).isNotNull(); | |
| 153 | + assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue(); | |
| 154 | + assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isFalse(); | |
| 155 | + ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); | |
| 156 | + server.getMBeanInfo(clusterMetricsON); | |
| 157 | + } finally { | |
| 158 | + cluster.close(); | |
| 159 | + } | |
| 160 | + } | |
| 84 | 161 | } |