question

wederbrand avatar image
wederbrand asked smadhavan edited

How can I enable Speculative query execution in Java without using configuration files?

I'd like to configure Speculative query execution from my application.

So far I've done all the configuration in pure java (ie no configuration files) and I like it that way.

However, I can't find how to configure Speculative query execution. Is there a way?

Thanks.

java driver
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

smadhavan avatar image
smadhavan answered smadhavan edited

Hello @wederbrand, thank you for leveraging the community portal for your question/needs!

I am assuming you're using the latest DataStax Java Driver 4.10.0 here as that wasn't specified in the original question.

Here is a snippet with which you could enable speculative execution for your application needs. Remember that it will only be in operation if your queries are idempotent. For more details, read the documentation here.

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
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.ProgrammaticDriverConfigLoaderBuilder;

public class ProgrammaticDriverConfig {
    public static void main(String... args) {
ProgrammaticDriverConfigLoaderBuilder driverBldr = DriverConfigLoader.programmaticBuilder();
driverBldr
.withString(DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS,
"ConstantSpeculativeExecutionPolicy")
.withInt(DefaultDriverOption.SPECULATIVE_EXECUTION_MAX, 3)
.withDuration(DefaultDriverOption.SPECULATIVE_EXECUTION_DELAY, Duration.ofMillis(100));
CqlSessionBuilder cqlSessionBuilder = CqlSession.builder().withKeyspace("")
.withConfigLoader(driverBldr.build());
    }
}

Given the above configuration, an idempotent query would be handled this way:

  • start the initial execution at t0;

  • if no response has been received at t0 + 100 milliseconds, start a speculative execution on another node;

  • if no response has been received at t0 + 200 milliseconds, start another speculative execution on a third node;

  • past that point, don’t query other nodes, just wait for the first response to arrive.

Cheers!

2 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

wederbrand avatar image wederbrand commented ·

Thanks. It's 4.6.1 but I guess it's the same.

The ProgrammaticDriverConfig basically exposes a config file in code, right? I guess no part of the CqlSessionBuilder can set this.

0 Likes 0 ·
smadhavan avatar image smadhavan ♦ wederbrand commented ·

That is correct @wederbrand. You might want to use the latest version as it has got a few key features and improvements.

0 Likes 0 ·
Erick Ramirez avatar image
Erick Ramirez answered

I noted that you prefer configuring the driver programatically.

This isn't a direct answer to your question but I wanted to point out that we recommend using the configuration file to configure the driver options. Among a range of benefits doing so will:

  1. significantly reduce the amount of code in your application, and
  2. allow you to make configuration changes without needing to recompile your application.

Cheers!

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.