use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy in project java-driver by datastax.
the class CqlRequestHandlerSpeculativeExecutionTest method should_stop_retrying_other_executions_if_result_complete.
@Test
@UseDataProvider("idempotentConfig")
public void should_stop_retrying_other_executions_if_result_complete(boolean defaultIdempotence, Statement<?> statement) throws Exception {
RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder().withDefaultIdempotence(defaultIdempotence);
PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
PoolBehavior node3Behavior = harnessBuilder.customBehavior(node3);
try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
SpeculativeExecutionPolicy speculativeExecutionPolicy = harness.getContext().getSpeculativeExecutionPolicy(DriverExecutionProfile.DEFAULT_NAME);
long firstExecutionDelay = 100L;
when(speculativeExecutionPolicy.nextExecution(any(Node.class), eq(null), eq(statement), eq(1))).thenReturn(firstExecutionDelay);
CompletionStage<AsyncResultSet> resultSetFuture = new CqlRequestHandler(statement, harness.getSession(), harness.getContext(), "test").handle();
node1Behavior.verifyWrite();
node1Behavior.setWriteSuccess();
// Discard the timeout task
harness.nextScheduledTimeout();
// next scheduled timeout should be the first speculative execution. Get it and run it.
CapturedTimeout speculativeExecution1 = harness.nextScheduledTimeout();
assertThat(speculativeExecution1.getDelay(TimeUnit.MILLISECONDS)).isEqualTo(firstExecutionDelay);
speculativeExecution1.task().run(speculativeExecution1);
node2Behavior.verifyWrite();
node2Behavior.setWriteSuccess();
// Complete the request from the initial execution
node1Behavior.setResponseSuccess(defaultFrameOf(singleRow()));
assertThatStage(resultSetFuture).isSuccess();
// node2 replies with a response that would trigger a RETRY_NEXT if the request was still
// running
node2Behavior.setResponseSuccess(defaultFrameOf(new Error(ProtocolConstants.ErrorCode.IS_BOOTSTRAPPING, "mock message")));
// The speculative execution should not move to node3 because it is stopped
node3Behavior.verifyNoWrite();
}
}
use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy in project java-driver by datastax.
the class ReflectionTest method should_build_policies_per_profile.
@Test
public void should_build_policies_per_profile() {
String configSource = "advanced.speculative-execution-policy {\n" + " class = ConstantSpeculativeExecutionPolicy\n" + " max-executions = 3\n" + " delay = 100 milliseconds\n" + "}\n" + "profiles {\n" + // Inherits from default profile
" profile1 {}\n" + // Inherits but changes one option
" profile2 { \n" + " advanced.speculative-execution-policy.max-executions = 2" + " }\n" + // Same as previous profile, should share the same policy instance
" profile3 { \n" + " advanced.speculative-execution-policy.max-executions = 2" + " }\n" + // Completely overrides default profile
" profile4 { \n" + " advanced.speculative-execution-policy.class = NoSpeculativeExecutionPolicy\n" + " }\n" + "}\n";
InternalDriverContext context = mock(InternalDriverContext.class);
TypesafeDriverConfig config = new TypesafeDriverConfig(ConfigFactory.parseString(configSource));
when(context.getConfig()).thenReturn(config);
Map<String, SpeculativeExecutionPolicy> policies = Reflection.buildFromConfigProfiles(context, DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS, DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY, SpeculativeExecutionPolicy.class, "com.datastax.oss.driver.internal.core.specex");
assertThat(policies).hasSize(5);
SpeculativeExecutionPolicy defaultPolicy = policies.get(DriverExecutionProfile.DEFAULT_NAME);
SpeculativeExecutionPolicy policy1 = policies.get("profile1");
SpeculativeExecutionPolicy policy2 = policies.get("profile2");
SpeculativeExecutionPolicy policy3 = policies.get("profile3");
SpeculativeExecutionPolicy policy4 = policies.get("profile4");
assertThat(defaultPolicy).isInstanceOf(ConstantSpeculativeExecutionPolicy.class).isSameAs(policy1);
assertThat(policy2).isInstanceOf(ConstantSpeculativeExecutionPolicy.class).isSameAs(policy3);
assertThat(policy4).isInstanceOf(NoSpeculativeExecutionPolicy.class);
}
use of com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy in project java-driver by datastax.
the class SpeculativeExecutionIT method buildSessionWithProfile.
private CqlSession buildSessionWithProfile(int defaultMaxSpeculativeExecutions, long defaultSpeculativeDelayMs, int profile1MaxSpeculativeExecutions, long profile1SpeculativeDelayMs) {
ProgrammaticDriverConfigLoaderBuilder builder = SessionUtils.configLoaderBuilder().withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(SPECULATIVE_DELAY * 10)).withBoolean(DefaultDriverOption.REQUEST_DEFAULT_IDEMPOTENCE, true).withClass(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS, SortingLoadBalancingPolicy.class);
if (defaultMaxSpeculativeExecutions != -1 || defaultSpeculativeDelayMs != -1) {
builder = builder.withClass(DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS, ConstantSpeculativeExecutionPolicy.class);
if (defaultMaxSpeculativeExecutions != -1) {
builder = builder.withInt(DefaultDriverOption.SPECULATIVE_EXECUTION_MAX, defaultMaxSpeculativeExecutions);
}
if (defaultSpeculativeDelayMs != -1) {
builder = builder.withDuration(DefaultDriverOption.SPECULATIVE_EXECUTION_DELAY, Duration.ofMillis(defaultSpeculativeDelayMs));
}
} else {
builder = builder.withClass(DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS, NoSpeculativeExecutionPolicy.class);
}
builder = builder.startProfile("profile1");
if (profile1MaxSpeculativeExecutions != -1 || profile1SpeculativeDelayMs != -1) {
builder = builder.withClass(DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS, ConstantSpeculativeExecutionPolicy.class);
if (profile1MaxSpeculativeExecutions != -1) {
builder = builder.withInt(DefaultDriverOption.SPECULATIVE_EXECUTION_MAX, profile1MaxSpeculativeExecutions);
}
if (profile1SpeculativeDelayMs != -1) {
builder = builder.withDuration(DefaultDriverOption.SPECULATIVE_EXECUTION_DELAY, Duration.ofMillis(profile1SpeculativeDelayMs));
}
} else {
builder = builder.withClass(DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS, NoSpeculativeExecutionPolicy.class);
}
builder = builder.startProfile("profile2").withString(DefaultDriverOption.REQUEST_CONSISTENCY, "ONE");
CqlSession session = SessionUtils.newSession(SIMULACRON_RULE, builder.build());
// validate profile data
DriverContext context = session.getContext();
DriverConfig driverConfig = context.getConfig();
assertThat(driverConfig.getProfiles()).containsKeys("profile1", "profile2");
assertThat(context.getSpeculativeExecutionPolicies()).hasSize(3).containsKeys(DriverExecutionProfile.DEFAULT_NAME, "profile1", "profile2");
SpeculativeExecutionPolicy defaultPolicy = context.getSpeculativeExecutionPolicy(DriverExecutionProfile.DEFAULT_NAME);
SpeculativeExecutionPolicy policy1 = context.getSpeculativeExecutionPolicy("profile1");
SpeculativeExecutionPolicy policy2 = context.getSpeculativeExecutionPolicy("profile2");
Class<? extends SpeculativeExecutionPolicy> expectedDefaultPolicyClass = defaultMaxSpeculativeExecutions != -1 || defaultSpeculativeDelayMs != -1 ? ConstantSpeculativeExecutionPolicy.class : NoSpeculativeExecutionPolicy.class;
assertThat(defaultPolicy).isInstanceOf(expectedDefaultPolicyClass).isSameAs(policy2);
// If configuration was same, same policy instance should be used.
if (defaultMaxSpeculativeExecutions == profile1MaxSpeculativeExecutions && defaultSpeculativeDelayMs == profile1SpeculativeDelayMs) {
assertThat(defaultPolicy).isSameAs(policy1);
} else {
assertThat(defaultPolicy).isNotSameAs(policy1);
}
Class<? extends SpeculativeExecutionPolicy> expectedProfile1PolicyClass = profile1MaxSpeculativeExecutions != -1 || profile1SpeculativeDelayMs != -1 ? ConstantSpeculativeExecutionPolicy.class : NoSpeculativeExecutionPolicy.class;
assertThat(policy1).isInstanceOf(expectedProfile1PolicyClass);
return session;
}
Aggregations