Search in sources :

Example 16 with SpeculativeExecutionPolicy

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();
    }
}
Also used : AsyncResultSet(com.datastax.oss.driver.api.core.cql.AsyncResultSet) Node(com.datastax.oss.driver.api.core.metadata.Node) Error(com.datastax.oss.protocol.internal.response.Error) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) CapturedTimeout(com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 17 with SpeculativeExecutionPolicy

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);
}
Also used : TypesafeDriverConfig(com.datastax.oss.driver.internal.core.config.typesafe.TypesafeDriverConfig) ConstantSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy) NoSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) ConstantSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy) Test(org.junit.Test)

Example 18 with SpeculativeExecutionPolicy

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;
}
Also used : DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) ProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder) ConstantSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy) NoSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy) SpeculativeExecutionPolicy(com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy) NoSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy) ConstantSpeculativeExecutionPolicy(com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Aggregations

SpeculativeExecutionPolicy (com.datastax.oss.driver.api.core.specex.SpeculativeExecutionPolicy)18 Test (org.junit.Test)17 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)16 Node (com.datastax.oss.driver.api.core.metadata.Node)14 CapturedTimeout (com.datastax.oss.driver.internal.core.util.concurrent.CapturingTimer.CapturedTimeout)12 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)8 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)8 Error (com.datastax.oss.protocol.internal.response.Error)8 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)7 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)7 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)6 AsyncResultSet (com.datastax.oss.driver.api.core.cql.AsyncResultSet)6 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)4 List (java.util.List)4 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)2 ConstantSpeculativeExecutionPolicy (com.datastax.oss.driver.internal.core.specex.ConstantSpeculativeExecutionPolicy)2 NoSpeculativeExecutionPolicy (com.datastax.oss.driver.internal.core.specex.NoSpeculativeExecutionPolicy)2 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 ProgrammaticDriverConfigLoaderBuilder (com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder)1