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();
}
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();
}
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());
}
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);
}
}
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();
}
}
Aggregations