Search in sources :

Example 6 with DriverExecutionProfile

use of com.datastax.oss.driver.api.core.config.DriverExecutionProfile in project java-driver by datastax.

the class Reflection method buildFromConfigList.

/**
 * @param profileName if null, this is a global policy, use the default profile and look for a
 *     one-arg constructor. If not null, this is a per-profile policy, look for a two-arg
 *     constructor.
 */
public static <ComponentT> ImmutableList<ComponentT> buildFromConfigList(InternalDriverContext context, String profileName, DriverOption classNamesOption, Class<ComponentT> expectedSuperType, String... defaultPackages) {
    DriverExecutionProfile config = (profileName == null) ? context.getConfig().getDefaultProfile() : context.getConfig().getProfile(profileName);
    String configPath = classNamesOption.getPath();
    LOG.debug("Creating a list of {} from config option {}", expectedSuperType.getSimpleName(), configPath);
    if (!config.isDefined(classNamesOption)) {
        LOG.debug("Option is not defined, skipping");
        return ImmutableList.of();
    }
    List<String> classNames = config.getStringList(classNamesOption);
    ImmutableList.Builder<ComponentT> components = ImmutableList.builder();
    for (String className : classNames) {
        components.add(resolveClass(context, profileName, expectedSuperType, configPath, className, defaultPackages));
    }
    return components.build();
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)

Example 7 with DriverExecutionProfile

use of com.datastax.oss.driver.api.core.config.DriverExecutionProfile in project java-driver by datastax.

the class Reflection method buildFromConfigProfiles.

/**
 * Tries to create multiple instances of a class, given options defined in the driver
 * configuration and possibly overridden in profiles.
 *
 * <p>For example:
 *
 * <pre>
 * my-policy.class = package1.PolicyImpl1
 * profiles {
 *   my-profile { my-policy.class = package2.PolicyImpl2 }
 * }
 * </pre>
 *
 * The class will be instantiated via reflection, it must have a constructor that takes two
 * arguments: the {@link DriverContext}, and a string representing the profile name.
 *
 * <p>This method assumes the policy is mandatory, the class option must be present at least for
 * the default profile.
 *
 * @param context the driver context.
 * @param classNameOption the option that indicates the class (my-policy.class in the example
 *     above).
 * @param rootOption the root of the section containing the policy's configuration (my-policy in
 *     the example above). Profiles that have the same contents under that section will share the
 *     same policy instance.
 * @param expectedSuperType a super-type that the class is expected to implement/extend.
 * @param defaultPackages the default packages to prepend to the class name if it's not qualified.
 *     They will be tried in order, the first one that matches an existing class will be used.
 * @return the policy instances by profile name. If multiple profiles share the same
 *     configuration, a single instance will be shared by all their entries.
 */
public static <ComponentT> Map<String, ComponentT> buildFromConfigProfiles(InternalDriverContext context, DriverOption classNameOption, DriverOption rootOption, Class<ComponentT> expectedSuperType, String... defaultPackages) {
    // Find out how many distinct configurations we have
    ListMultimap<Object, String> profilesByConfig = MultimapBuilder.hashKeys().arrayListValues().build();
    for (DriverExecutionProfile profile : context.getConfig().getProfiles().values()) {
        profilesByConfig.put(profile.getComparisonKey(rootOption), profile.getName());
    }
    // Instantiate each distinct configuration, and associate it with the corresponding profiles
    ImmutableMap.Builder<String, ComponentT> result = ImmutableMap.builder();
    for (Collection<String> profiles : profilesByConfig.asMap().values()) {
        // Since all profiles use the same config, we can use any of them
        String profileName = profiles.iterator().next();
        ComponentT policy = buildFromConfig(context, profileName, classNameOption, expectedSuperType, defaultPackages).orElseThrow(() -> new IllegalArgumentException(String.format("Missing configuration for %s in profile %s", rootOption.getPath(), profileName)));
        for (String profile : profiles) {
            result.put(profile, policy);
        }
    }
    return result.build();
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)

Example 8 with DriverExecutionProfile

use of com.datastax.oss.driver.api.core.config.DriverExecutionProfile in project java-driver by datastax.

the class DefaultReactiveResultSetIT method should_retrieve_all_rows.

@Test
@DataProvider(value = { "1", "10", "100", "999", "1000", "1001", "2000" }, format = "%m [page size %p[0]]")
public void should_retrieve_all_rows(int pageSize) {
    DriverExecutionProfile profile = sessionRule.session().getContext().getConfig().getDefaultProfile().withInt(DefaultDriverOption.REQUEST_PAGE_SIZE, pageSize);
    SimpleStatement statement = SimpleStatement.builder("SELECT cc, v FROM test_reactive_read WHERE pk = 0").setExecutionProfile(profile).build();
    ReactiveResultSet rs = sessionRule.session().executeReactive(statement);
    List<ReactiveRow> results = Flowable.fromPublisher(rs).toList().blockingGet();
    assertThat(results.size()).isEqualTo(1000);
    Set<ExecutionInfo> expectedExecInfos = new LinkedHashSet<>();
    for (int i = 0; i < results.size(); i++) {
        ReactiveRow row = results.get(i);
        assertThat(row.getColumnDefinitions()).isNotNull();
        assertThat(row.getExecutionInfo()).isNotNull();
        assertThat(row.wasApplied()).isTrue();
        assertThat(row.getInt("cc")).isEqualTo(i);
        assertThat(row.getInt("v")).isEqualTo(i);
        expectedExecInfos.add(row.getExecutionInfo());
    }
    List<ExecutionInfo> execInfos = Flowable.<ExecutionInfo>fromPublisher(rs.getExecutionInfos()).toList().blockingGet();
    // DSE may send an empty page as it can't always know if it's done paging or not yet.
    // See: CASSANDRA-8871. In this case, this page's execution info appears in
    // rs.getExecutionInfos(), but is not present in expectedExecInfos since the page did not
    // contain any rows.
    assertThat(execInfos).containsAll(expectedExecInfos);
    List<ColumnDefinitions> colDefs = Flowable.<ColumnDefinitions>fromPublisher(rs.getColumnDefinitions()).toList().blockingGet();
    ReactiveRow first = results.get(0);
    assertThat(colDefs).hasSize(1).containsExactly(first.getColumnDefinitions());
    List<Boolean> wasApplied = Flowable.fromPublisher(rs.wasApplied()).toList().blockingGet();
    assertThat(wasApplied).hasSize(1).containsExactly(first.wasApplied());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) EmptyColumnDefinitions(com.datastax.oss.driver.internal.core.cql.EmptyColumnDefinitions) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) ReactiveResultSet(com.datastax.dse.driver.api.core.cql.reactive.ReactiveResultSet) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 9 with DriverExecutionProfile

use of com.datastax.oss.driver.api.core.config.DriverExecutionProfile in project java-driver by datastax.

the class CqlPrepareHandlerTest method should_propagate_custom_payload_on_all_nodes.

@Test
public void should_propagate_custom_payload_on_all_nodes() {
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder();
    DefaultPrepareRequest prepareRequest = new DefaultPrepareRequest(SimpleStatement.newInstance("irrelevant").setCustomPayload(payload));
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
    PoolBehavior node3Behavior = harnessBuilder.customBehavior(node3);
    node1Behavior.setResponseSuccess(defaultFrameOf(simplePrepared()));
    node2Behavior.setResponseSuccess(defaultFrameOf(simplePrepared()));
    node3Behavior.setResponseSuccess(defaultFrameOf(simplePrepared()));
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        DriverExecutionProfile config = harness.getContext().getConfig().getDefaultProfile();
        when(config.getBoolean(DefaultDriverOption.PREPARE_ON_ALL_NODES)).thenReturn(true);
        CompletionStage<PreparedStatement> prepareFuture = new CqlPrepareHandler(prepareRequest, harness.getSession(), harness.getContext(), "test").handle();
        verify(node1Behavior.channel).write(any(Prepare.class), anyBoolean(), eq(payload), any(ResponseCallback.class));
        verify(node2Behavior.channel).write(any(Prepare.class), anyBoolean(), eq(payload), any(ResponseCallback.class));
        verify(node3Behavior.channel).write(any(Prepare.class), anyBoolean(), eq(payload), any(ResponseCallback.class));
        assertThatStage(prepareFuture).isSuccess(CqlPrepareHandlerTest::assertMatchesSimplePrepared);
    }
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) ResponseCallback(com.datastax.oss.driver.internal.core.channel.ResponseCallback) Prepare(com.datastax.oss.protocol.internal.request.Prepare) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Test(org.junit.Test)

Example 10 with DriverExecutionProfile

use of com.datastax.oss.driver.api.core.config.DriverExecutionProfile in project java-driver by datastax.

the class CqlPrepareHandlerTest method should_not_reprepare_on_other_nodes_if_disabled_in_config.

@Test
public void should_not_reprepare_on_other_nodes_if_disabled_in_config() {
    RequestHandlerTestHarness.Builder harnessBuilder = RequestHandlerTestHarness.builder();
    PoolBehavior node1Behavior = harnessBuilder.customBehavior(node1);
    PoolBehavior node2Behavior = harnessBuilder.customBehavior(node2);
    PoolBehavior node3Behavior = harnessBuilder.customBehavior(node3);
    try (RequestHandlerTestHarness harness = harnessBuilder.build()) {
        DriverExecutionProfile config = harness.getContext().getConfig().getDefaultProfile();
        when(config.getBoolean(DefaultDriverOption.PREPARE_ON_ALL_NODES)).thenReturn(false);
        CompletionStage<PreparedStatement> prepareFuture = new CqlPrepareHandler(PREPARE_REQUEST, harness.getSession(), harness.getContext(), "test").handle();
        node1Behavior.verifyWrite();
        node1Behavior.setWriteSuccess();
        node1Behavior.setResponseSuccess(defaultFrameOf(simplePrepared()));
        // The future should complete immediately:
        assertThatStage(prepareFuture).isSuccess();
        // And the other nodes should not be contacted:
        node2Behavior.verifyNoWrite();
        node3Behavior.verifyNoWrite();
    }
}
Also used : DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Test(org.junit.Test)

Aggregations

DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)140 Test (org.junit.Test)81 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)29 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)20 CqlSession (com.datastax.oss.driver.api.core.CqlSession)19 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)17 Node (com.datastax.oss.driver.api.core.metadata.Node)14 ByteBuffer (java.nio.ByteBuffer)13 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)12 Duration (java.time.Duration)11 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)10 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)10 Row (com.datastax.oss.driver.api.core.cql.Row)9 Test (org.junit.jupiter.api.Test)9 LoggerTest (com.datastax.oss.driver.internal.core.util.LoggerTest)8 DefaultNodeMetric (com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric)7 NodeMetric (com.datastax.oss.driver.api.core.metrics.NodeMetric)7 Message (com.datastax.oss.protocol.internal.Message)7 DriverTimeoutException (com.datastax.oss.driver.api.core.DriverTimeoutException)6 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)6