/** * {@inheritDoc} */ @Override public void close() { if (cluster != null && !cluster.isClosed()) { LOG.info("Try to close connection to cluster: {}", cluster.getClusterName()); session.close(); cluster.close(); } }
/** * {@inheritDoc} */ @Override public synchronized Session connect() throws NoHostAvailableException { if (isDisconnected()) { LOG.info("Connected to cluster: {}", cluster.getClusterName()); for (Host host : getAllHosts()) LOG.info("Datacenter: {}; Host: {}; Rack: {}", host.getDatacenter(), host.getAddress(), host.getRack()); LOG.info("Connect to cluster using keyspace %s", keyspace); session = cluster.connect(keyspace); } else { LOG.warn("{} - Already connected to cluster: {}", getExecutorName(), cluster.getClusterName()); } if (session.isClosed()) { LOG.warn("Session has been closed - create new one!"); this.session = cluster.newSession(); } return session; }
@Test public void testGetClusterSession() { Session session = dataStaxCluster.getClusterSession(); String clusterName = session.getCluster().getClusterName(); String keyspaceName = session.getLoggedKeyspace(); // cluster session is not logged to a keyspace assertNull(keyspaceName); assertNotNull(clusterName); }
protected void maybeLogNormalQuery(Host host, Statement statement, long latencyMs) { if (NORMAL_LOGGER.isDebugEnabled()) { String message = String.format( NORMAL_TEMPLATE, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, null, NORMAL_LOGGER, message); } }
protected void maybeLogSlowQuery(Host host, Statement statement, long latencyMs) { if (SLOW_LOGGER.isDebugEnabled()) { String message = String.format( SLOW_TEMPLATE_MILLIS, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, null, SLOW_LOGGER, message); } } }
protected void maybeLogErrorQuery( Host host, Statement statement, Exception exception, long latencyMs) { if (ERROR_LOGGER.isDebugEnabled() && !(exception instanceof CancelledSpeculativeExecutionException)) { String message = String.format( ERROR_TEMPLATE, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, exception, ERROR_LOGGER, message); } }
protected void maybeLogSlowQuery( Host host, Statement statement, long latencyMs, long threshold) { if (SLOW_LOGGER.isDebugEnabled()) { String message = String.format( SLOW_TEMPLATE_PERCENTILE, cluster.getClusterName(), host, latencyMs, slowQueryLatencyThresholdPercentile, threshold, statementAsString(statement)); logQuery(statement, null, SLOW_LOGGER, message); } }
/** * Validates that metrics are enabled and exposed by JMX by default by checking that {@link * Cluster#getMetrics()} is not null and 'clusterName-metrics:name=connected-to' MBean is present. * * @test_category metrics */ @Test(groups = "short") public void should_enable_metrics_and_jmx_by_default() throws Exception { assertThat(cluster().getMetrics()).isNotNull(); ObjectName clusterMetricsON = ObjectName.getInstance(cluster().getClusterName() + "-metrics:name=connected-to"); MBeanInfo mBean = server.getMBeanInfo(clusterMetricsON); assertThat(mBean).isNotNull(); assertThat(cluster().getConfiguration().getMetricsOptions().isEnabled()).isTrue(); assertThat(cluster().getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isTrue(); }
.build(); try { String clusterName = cluster.getClusterName(); cluster.init();
/** * Validates that when metrics are enabled but JMX reporting is disabled via {@link * Cluster.Builder#withoutJMXReporting()} that {@link Cluster#getMetrics()} is not null and * 'clusterName-metrics:name=connected-to' MBean is present. * * @test_category metrics */ @Test(groups = "short", expectedExceptions = InstanceNotFoundException.class) public void should_be_no_jmx_mbean_when_jmx_is_disabled() throws Exception { Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withoutJMXReporting() .build()); try { cluster.init(); assertThat(cluster.getMetrics()).isNotNull(); assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isTrue(); assertThat(cluster.getConfiguration().getMetricsOptions().isJMXReportingEnabled()).isFalse(); ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); server.getMBeanInfo(clusterMetricsON); } finally { cluster.close(); } } }
/** * Validates that when metrics are disabled using {@link Cluster.Builder#withoutMetrics()} that * {@link Cluster#getMetrics()} returns null and 'clusterName-metrics:name=connected-to' MBean is * not present. * * @test_category metrics */ @Test(groups = "short", expectedExceptions = InstanceNotFoundException.class) public void metrics_should_be_null_when_metrics_disabled() throws Exception { Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withoutMetrics() .build()); try { cluster.init(); assertThat(cluster.getMetrics()).isNull(); assertThat(cluster.getConfiguration().getMetricsOptions().isEnabled()).isFalse(); ObjectName clusterMetricsON = ObjectName.getInstance(cluster.getClusterName() + "-metrics:name=connected-to"); server.getMBeanInfo(clusterMetricsON); } finally { cluster.close(); } }
protected void maybeLogErrorQuery(Host host, Statement statement, Exception exception, long latencyMs) { if (ERROR_LOGGER.isDebugEnabled()) { String message = String.format(ERROR_TEMPLATE, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, exception, ERROR_LOGGER, message); } }
protected void maybeLogSlowQuery(Host host, Statement statement, long latencyMs) { if (SLOW_LOGGER.isDebugEnabled()) { String message = String.format(SLOW_TEMPLATE_MILLIS, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, null, SLOW_LOGGER, message); } } }
protected void maybeLogSlowQuery(Host host, Statement statement, long latencyMs, long threshold) { if (SLOW_LOGGER.isDebugEnabled()) { String message = String.format(SLOW_TEMPLATE_PERCENTILE, cluster.getClusterName(), host, latencyMs, slowQueryLatencyThresholdPercentile, threshold, statementAsString(statement)); logQuery(statement, null, SLOW_LOGGER, message); } }
private static KeyspaceMetadata getKeyspaceMetadata(String keyspace, Cluster cluster) { KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace); if (keyspaceMetadata == null) { throw new IllegalStateException(String.format( "Cannot read keyspace metadata for give keyspace: %s and cluster: %s", keyspace, cluster.getClusterName())); } return keyspaceMetadata; }
protected void maybeLogSlowQuery(Host host, Statement statement, long latencyMs) { if (SLOW_LOGGER.isDebugEnabled()) { String message = String.format(SLOW_TEMPLATE_MILLIS, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, null, SLOW_LOGGER, message); } } }
protected void maybeLogNormalQuery(Host host, Statement statement, long latencyMs) { if (NORMAL_LOGGER.isDebugEnabled()) { String message = String.format(NORMAL_TEMPLATE, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, null, NORMAL_LOGGER, message); } }
protected void maybeLogSlowQuery(Host host, Statement statement, long latencyMs) { if (SLOW_LOGGER.isDebugEnabled()) { String message = String.format(SLOW_TEMPLATE_MILLIS, cluster.getClusterName(), host, latencyMs, statementAsString(statement)); logQuery(statement, null, SLOW_LOGGER, message); } } }
public CassandraMetricSet(Cluster cluster) { final String clusterName = cluster.getClusterName(); Map<String, Metric> driverMetrics = cluster.getMetrics().getRegistry().getMetrics(); ImmutableMap.Builder<String, Metric> builder = ImmutableMap.builder(); driverMetrics.forEach((k, v) -> builder.put(name(Cluster.class, clusterName, k), v)); metrics = builder.build(); }
static KeyspaceMetadata getKeyspaceMetadata(Session session) { String keyspace = session.getLoggedKeyspace(); Cluster cluster = session.getCluster(); KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace); if (keyspaceMetadata == null) { throw new IllegalStateException(String.format( "Cannot read keyspace metadata for give keyspace: %s and cluster: %s", keyspace, cluster.getClusterName())); } return keyspaceMetadata; }