Skip to content

Commit

Permalink
Test fixes for 4.0.0-beta3 (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolbertam authored and adutra committed Dec 17, 2018
1 parent c66b77d commit ece9ee1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 40 deletions.
Expand Up @@ -49,9 +49,9 @@ public DropwizardSessionMetricUpdater(
this.metricNamePrefix = context.getSessionName() + ".";

if (enabledMetrics.contains(DefaultSessionMetric.CONNECTED_NODES)) {
this.registry.register(
this.registry.gauge(
buildFullName(DefaultSessionMetric.CONNECTED_NODES, null),
(Gauge<Integer>)
() ->
() -> {
int count = 0;
for (Node node : context.getMetadataManager().getMetadata().getNodes().values()) {
Expand All @@ -63,26 +63,29 @@ public DropwizardSessionMetricUpdater(
});
}
if (enabledMetrics.contains(DefaultSessionMetric.THROTTLING_QUEUE_SIZE)) {
this.registry.register(
this.registry.gauge(
buildFullName(DefaultSessionMetric.THROTTLING_QUEUE_SIZE, null),
buildQueueGauge(context.getRequestThrottler(), context.getSessionName()));
() -> buildQueueGauge(context.getRequestThrottler(), context.getSessionName()));
}
if (enabledMetrics.contains(DefaultSessionMetric.CQL_PREPARED_CACHE_SIZE)) {
Cache<?, ?> cache = getPreparedStatementCache(context);
Gauge<Long> gauge;
if (cache == null) {
LOG.warn(
"[{}] Metric {} is enabled in the config, "
+ "but it looks like no CQL prepare processor is registered. "
+ "The gauge will always return 0",
context.getSessionName(),
DefaultSessionMetric.CQL_PREPARED_CACHE_SIZE.getPath());
gauge = () -> 0L;
} else {
gauge = cache::size;
}
this.registry.register(
buildFullName(DefaultSessionMetric.CQL_PREPARED_CACHE_SIZE, null), gauge);
this.registry.gauge(
buildFullName(DefaultSessionMetric.CQL_PREPARED_CACHE_SIZE, null),
() -> {
Cache<?, ?> cache = getPreparedStatementCache(context);
Gauge<Long> gauge;
if (cache == null) {
LOG.warn(
"[{}] Metric {} is enabled in the config, "
+ "but it looks like no CQL prepare processor is registered. "
+ "The gauge will always return 0",
context.getSessionName(),
DefaultSessionMetric.CQL_PREPARED_CACHE_SIZE.getPath());
gauge = () -> 0L;
} else {
gauge = cache::size;
}
return gauge;
});
}
initializeHdrTimer(
DefaultSessionMetric.CQL_REQUESTS,
Expand Down
Expand Up @@ -60,7 +60,7 @@ private void should_fail_to_init_with_invalid_policy(DefaultDriverOption option)
assertThat(error).isInstanceOf(DriverExecutionException.class);
assertThat(error.getCause())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(
.hasMessageContaining(
"Can't find class AClassThatDoesNotExist "
+ "(specified by "
+ option.getPath()
Expand Down
Expand Up @@ -78,6 +78,7 @@ public void should_fail_if_config_profile_specified_doesnt_exist() {
public void should_use_profile_request_timeout() {
DriverConfigLoader loader =
SessionUtils.configLoaderBuilder()
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(2))
.withProfile(
"olap",
DefaultDriverConfigLoaderBuilder.profileBuilder()
Expand All @@ -89,7 +90,7 @@ public void should_use_profile_request_timeout() {
// configure query with delay of 4 seconds.
simulacron.cluster().prime(when(query).then(noRows()).delay(4, TimeUnit.SECONDS));

// Execute query without profile, should timeout with default (2s).
// Execute query without profile, should timeout with default session timeout (2s).
try {
session.execute(query);
fail("Should have timed out");
Expand Down
Expand Up @@ -53,7 +53,9 @@ public void should_periodically_reload_configuration() throws Exception {
new DefaultDriverConfigLoader(
() ->
ConfigFactory.parseString(
"basic.config-reload-interval = 2s\n" + configSource.get())
"basic.config-reload-interval = 2s\n"
+ "basic.request.timeout = 2s\n"
+ configSource.get())
.withFallback(DEFAULT_CONFIG_SUPPLIER.get()));
try (CqlSession session =
(CqlSession)
Expand All @@ -63,7 +65,7 @@ public void should_periodically_reload_configuration() throws Exception {
.build()) {
simulacron.cluster().prime(when(query).then(noRows()).delay(4, TimeUnit.SECONDS));

// Expect timeout since default timeout is 2s
// Expect timeout since default session timeout is 2s
try {
session.execute(query);
fail("DriverTimeoutException expected");
Expand All @@ -88,7 +90,10 @@ public void should_reload_configuration_when_event_fired() throws Exception {
DefaultDriverConfigLoader loader =
new DefaultDriverConfigLoader(
() ->
ConfigFactory.parseString("basic.config-reload-interval = 0\n" + configSource.get())
ConfigFactory.parseString(
"basic.config-reload-interval = 0\n"
+ "basic.request.timeout = 2s\n"
+ configSource.get())
.withFallback(DEFAULT_CONFIG_SUPPLIER.get()));
try (CqlSession session =
(CqlSession)
Expand All @@ -98,7 +103,7 @@ public void should_reload_configuration_when_event_fired() throws Exception {
.build()) {
simulacron.cluster().prime(when(query).then(noRows()).delay(4, TimeUnit.SECONDS));

// Expect timeout since default timeout is 2s
// Expect timeout since default session timeout is 2s
try {
session.execute(query);
fail("DriverTimeoutException expected");
Expand Down Expand Up @@ -144,7 +149,7 @@ public void should_not_allow_dynamically_adding_profile() throws Exception {
}

// Bump up request timeout to 10 seconds on profile and wait for config to reload.
configSource.set("profiles.slow.basic.request.timeout = 2s");
configSource.set("profiles.slow.basic.request.timeout = 10s");
waitForConfigChange(session, 3, TimeUnit.SECONDS);

// Execute again, should expect to fail again because doesn't allow to dynamically define
Expand Down
Expand Up @@ -31,7 +31,6 @@
import com.datastax.oss.driver.api.testinfra.simulacron.SimulacronRule;
import com.datastax.oss.driver.categories.ParallelizableTests;
import com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoaderBuilder;
import com.datastax.oss.driver.internal.core.loadbalancing.DefaultLoadBalancingPolicy;
import com.datastax.oss.simulacron.common.cluster.ClusterSpec;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -88,13 +87,10 @@ public static void setup() {
.hasSize(3)
.containsKeys(DriverExecutionProfile.DEFAULT_NAME, "profile1", "profile2");

DefaultLoadBalancingPolicy defaultPolicy =
(DefaultLoadBalancingPolicy)
context.getLoadBalancingPolicy(DriverExecutionProfile.DEFAULT_NAME);
DefaultLoadBalancingPolicy policy1 =
(DefaultLoadBalancingPolicy) context.getLoadBalancingPolicy("profile1");
DefaultLoadBalancingPolicy policy2 =
(DefaultLoadBalancingPolicy) context.getLoadBalancingPolicy("profile2");
LoadBalancingPolicy defaultPolicy =
context.getLoadBalancingPolicy(DriverExecutionProfile.DEFAULT_NAME);
LoadBalancingPolicy policy1 = context.getLoadBalancingPolicy("profile1");
LoadBalancingPolicy policy2 = context.getLoadBalancingPolicy("profile2");

assertThat(defaultPolicy).isSameAs(policy2).isNotSameAs(policy1);

Expand Down
Expand Up @@ -19,6 +19,7 @@

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.Version;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
Expand All @@ -35,6 +36,7 @@
import com.datastax.oss.driver.categories.ParallelizableTests;
import java.util.Collections;
import java.util.Map;
import org.junit.AssumptionViolatedException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand Down Expand Up @@ -179,18 +181,28 @@ public void should_refresh_schema_manually() {
@Test
public void should_get_virtual_metadata() {
Metadata md = sessionRule.session().getMetadata();
// Special case: DSE 6.0 reports C* 4.0 but does not support virtual tables
if (ccmRule.getDseVersion().isPresent()) {
Version dseVersion = ccmRule.getDseVersion().get();
if (dseVersion.compareTo(Version.parse("6.7.0")) < 0) {
throw new AssumptionViolatedException("DSE 6.0 does not support virtual tables");
}
}
KeyspaceMetadata kmd = md.getKeyspace("system_views").get();

// Keyspace name should be set, marked as virtual, and have a clients table.
// Keyspace name should be set, marked as virtual, and have at least sstable_tasks table.
// All other values should be defaulted since they are not defined in the virtual schema tables.
assertThat(kmd.getTables().size()).isGreaterThanOrEqualTo(2);
assertThat(kmd.getTables().size()).isGreaterThanOrEqualTo(1);
assertThat(kmd.isVirtual()).isTrue();
assertThat(kmd.isDurableWrites()).isFalse();
assertThat(kmd.getName().asCql(true)).isEqualTo("system_views");

// Virtual tables lack User Types, Functions, Views and Aggregates
assertThat(kmd.getUserDefinedTypes().size()).isEqualTo(0);
assertThat(kmd.getFunctions().size()).isEqualTo(0);
assertThat(kmd.getViews().size()).isEqualTo(0);
assertThat(kmd.getAggregates().size()).isEqualTo(0);

assertThat(kmd.describe(true))
.isEqualTo(
"/* VIRTUAL KEYSPACE system_views WITH replication = { 'class' : 'null' } "
Expand Down
Expand Up @@ -19,8 +19,8 @@
import static com.datastax.oss.simulacron.common.stubbing.PrimeDsl.when;
import static org.assertj.core.api.Assertions.assertThat;

import com.datastax.oss.driver.api.core.AllNodesFailedException;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.NoNodeAvailableException;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.connection.ClosedConnectionException;
import com.datastax.oss.driver.api.testinfra.session.SessionUtils;
Expand Down Expand Up @@ -81,20 +81,34 @@ public void should_fail_requests_when_session_is_closed() throws Exception {
.whenComplete(
(ignoredResult, error) -> {
semaphore.release();
// Three things can happen:
// Four things can happen:
// - DefaultSession.execute() detects that it's closed and fails the
// request immediately
// - the request was in flight and gets aborted when its channel is
// force-closed => ClosedConnectionException
// - the request races with the shutdown: it gets past execute() but by
// the time it tries to acquire a channel the pool was closed
// => NoNodeAvailableException
// - the request races with the channel closing: it acquires a channel,
// but by the time it tries to write on it is closing
// => AllNodesFailedException wrapping IllegalStateException
if (error instanceof IllegalStateException
&& "Session is closed".equals(error.getMessage())) {
gotSessionClosedError.countDown();
} else if (error instanceof AllNodesFailedException) {
AllNodesFailedException anfe = (AllNodesFailedException) error;
// if there were 0 errors, its a NoNodeAvailableException which is
// acceptable.
if (anfe.getErrors().size() > 0) {
assertThat(anfe.getErrors()).hasSize(1);
error = anfe.getErrors().values().iterator().next();
if (!(error instanceof IllegalStateException)
&& !error.getMessage().endsWith("is closing")) {
unexpectedErrors.add(error.toString());
}
}
} else if (error != null
&& !(error instanceof ClosedConnectionException
|| error instanceof NoNodeAvailableException)) {
&& !(error instanceof ClosedConnectionException)) {
unexpectedErrors.add(error.toString());
}
});
Expand Down

0 comments on commit ece9ee1

Please sign in to comment.