Elffuss BraidHub

kikocis/java-driver public

Merge pull request #481 from datastax/java900

Fix Configuration builder to allow disabled metrics (JAVA-900).
Changesetcf32ad67f65d9069fcf9fdf3c3bdc1ff4e52b5ffff8136514d71c07be17c3e4a
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-13 11:30 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-13 11:30 +02:00
Parentsd0cf52e93999, 88804a2cfe18

Browse the files at this changeset

6 files changed

modified changelog/README.md +1 −0
@@ -6,6 +6,7 @@
66 - [bug] Fix implementation of UserType.hashCode() (JAVA-942)
77 - [improvement] Don't delay UP/ADDED notifications if protocol version = V4 (JAVA-877)
88 - [improvement] Parse 'extensions' column in table metadata (JAVA-938)
9+- [bug] Fix Configuration builder to allow disabled metrics (JAVA-900)
910
1011
1112 ### 3.0.0-alpha3
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +5 −4
@@ -396,8 +396,8 @@
396396 * The cluster metrics.
397397 *
398398 * @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}).
401401 */
402402 public Metrics getMetrics() {
403403 checkNotClosed(manager);
@@ -1202,7 +1202,8 @@
12021202 public Configuration getConfiguration() {
12031203 ProtocolOptions protocolOptions = new ProtocolOptions(port, protocolVersion, maxSchemaAgreementWaitSeconds, sslOptions, authProvider)
12041204 .setCompression(compression);
1205- MetricsOptions metricsOptions = metricsEnabled ? new MetricsOptions(jmxEnabled) : null;
1205+
1206+ MetricsOptions metricsOptions = new MetricsOptions(metricsEnabled, jmxEnabled);
12061207
12071208 return configurationBuilder
12081209 .withProtocolOptions(protocolOptions)
@@ -1328,7 +1329,7 @@
13281329 this.metadata = new Metadata(this);
13291330 this.connectionFactory = new Connection.Factory(this, configuration);
13301331 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;
13321333 this.preparedQueries = new MapMaker().weakValues().makeMap();
13331334
13341335 // 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 @@
3535
3636 /**
3737 * 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.
3841 *
3942 * @return the builder.
4043 */
@@ -209,6 +212,12 @@
209212
210213 /**
211214 * 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+ *
212221 * @param metricsOptions the metrics options.
213222 * @return this builder.
214223 */
modified driver-core/src/main/java/com/datastax/driver/core/MetricsOptions.java +17 −9
@@ -20,14 +20,14 @@
2020 */
2121 public class MetricsOptions {
2222
23+ private final boolean metricsEnabled;
2324 private final boolean jmxEnabled;
2425
2526 /**
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).
2728 */
28- public MetricsOptions()
29- {
30- this(true);
29+ public MetricsOptions() {
30+ this(true, true);
3131 }
3232
3333 /**
@@ -35,18 +35,26 @@
3535 *
3636 * @param jmxEnabled whether to enable JMX reporting or not.
3737 */
38- public MetricsOptions(boolean jmxEnabled)
39- {
38+ public MetricsOptions(boolean enabled, boolean jmxEnabled) {
39+ this.metricsEnabled = enabled;
4040 this.jmxEnabled = jmxEnabled;
4141 }
4242
4343 /**
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.
4554 *
4655 * @return whether JMX reporting is enabled.
4756 */
48- public boolean isJMXReportingEnabled()
49- {
57+ public boolean isJMXReportingEnabled() {
5058 return jmxEnabled;
5159 }
5260 }
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −1
@@ -221,7 +221,7 @@
221221 }
222222
223223 private boolean metricsEnabled() {
224- return manager.configuration().getMetricsOptions() != null;
224+ return manager.configuration().getMetricsOptions().isEnabled();
225225 }
226226
227227 private Metrics metrics() {
modified driver-core/src/test/java/com/datastax/driver/core/MetricsTest.java +77 −0
@@ -15,9 +15,15 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.lang.management.ManagementFactory;
1819 import java.util.Collection;
20+import java.util.Collections;
1921
2022 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;
2127 import org.testng.annotations.Test;
2228
2329 import static org.testng.Assert.assertEquals;
@@ -26,9 +32,13 @@
2632 import com.datastax.driver.core.policies.RetryPolicy;
2733 import com.datastax.driver.core.policies.RetryPolicy.RetryDecision;
2834
35+import static com.datastax.driver.core.Assertions.assertThat;
36+
2937 public class MetricsTest extends CCMBridge.PerClassSingleNodeCluster {
3038 private volatile RetryDecision retryDecision;
3139
40+ MBeanServer server = ManagementFactory.getPlatformMBeanServer();
41+
3242 @Override
3343 protected Cluster.Builder configure(Cluster.Builder builder) {
3444 return builder.withRetryPolicy(new RetryPolicy() {
@@ -81,4 +91,71 @@
8191 assertEquals(errors.getIgnores().getCount(), 1);
8292 assertEquals(errors.getIgnoresOnUnavailable().getCount(), 1);
8393 }
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+ }
84161 }