use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class DefaultDriverContext method buildRequestTracker.
protected RequestTracker buildRequestTracker(RequestTracker requestTrackerFromBuilder) {
List<RequestTracker> trackers = new ArrayList<>();
if (requestTrackerFromBuilder != null) {
trackers.add(requestTrackerFromBuilder);
}
for (LoadBalancingPolicy lbp : this.getLoadBalancingPolicies().values()) {
lbp.getRequestTracker().ifPresent(trackers::add);
}
DefaultDriverOption newOption = DefaultDriverOption.REQUEST_TRACKER_CLASSES;
@SuppressWarnings("deprecation") DefaultDriverOption legacyOption = DefaultDriverOption.REQUEST_TRACKER_CLASS;
DriverExecutionProfile profile = config.getDefaultProfile();
if (profile.isDefined(newOption)) {
trackers.addAll(Reflection.buildFromConfigList(this, newOption, RequestTracker.class, "com.datastax.oss.driver.internal.core.tracker"));
}
if (profile.isDefined(legacyOption)) {
LOG.warn("Option {} has been deprecated and will be removed in a future release; please use option {} instead.", legacyOption, newOption);
Reflection.buildFromConfig(this, legacyOption, RequestTracker.class, "com.datastax.oss.driver.internal.core.tracker").ifPresent(trackers::add);
}
if (trackers.isEmpty()) {
return new NoopRequestTracker(this);
} else if (trackers.size() == 1) {
return trackers.get(0);
} else {
return new MultiplexingRequestTracker(trackers);
}
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapperTest method should_build_query_plan_from_contact_points_before_init.
@Test
public void should_build_query_plan_from_contact_points_before_init() {
// When
Queue<Node> queryPlan = wrapper.newQueryPlan();
// Then
for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
verify(policy, never()).newQueryPlan(null, null);
}
assertThat(queryPlan).hasSameElementsAs(contactPoints);
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapper method init.
public void init() {
if (stateRef.compareAndSet(State.BEFORE_INIT, State.DURING_INIT)) {
LOG.debug("[{}] Initializing policies", logPrefix);
// State events can happen concurrently with init, so we must record them and replay once the
// policy is initialized.
eventFilter.start();
MetadataManager metadataManager = context.getMetadataManager();
Metadata metadata = metadataManager.getMetadata();
for (LoadBalancingPolicy policy : policies) {
policy.init(metadata.getNodes(), reporters.get(policy));
}
if (stateRef.compareAndSet(State.DURING_INIT, State.RUNNING)) {
eventFilter.markReady();
} else {
// closed during init
assert stateRef.get() == State.CLOSING;
for (LoadBalancingPolicy policy : policies) {
policy.close();
}
}
}
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapper method newQueryPlan.
/**
* Note: we could infer the profile name from the request again in this method, but since that's
* already done in request processors, pass the value directly.
*
* @see LoadBalancingPolicy#newQueryPlan(Request, Session)
*/
@NonNull
public Queue<Node> newQueryPlan(@Nullable Request request, @NonNull String executionProfileName, @Nullable Session session) {
switch(stateRef.get()) {
case BEFORE_INIT:
case DURING_INIT:
// The contact points are not stored in the metadata yet:
List<Node> nodes = new ArrayList<>(context.getMetadataManager().getContactPoints());
Collections.shuffle(nodes);
return new ConcurrentLinkedQueue<>(nodes);
case RUNNING:
LoadBalancingPolicy policy = policiesPerProfile.get(executionProfileName);
if (policy == null) {
policy = policiesPerProfile.get(DriverExecutionProfile.DEFAULT_NAME);
}
return policy.newQueryPlan(request, session);
default:
return new ConcurrentLinkedQueue<>();
}
}
use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.
the class LoadBalancingPolicyWrapperTest method should_fetch_query_plan_from_policy_after_init.
@Test
public void should_fetch_query_plan_from_policy_after_init() {
// Given
wrapper.init();
for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
verify(policy).init(anyMap(), any(DistanceReporter.class));
}
// When
Queue<Node> queryPlan = wrapper.newQueryPlan();
// Then
// no-arg newQueryPlan() uses the default profile
verify(policy1).newQueryPlan(null, null);
assertThat(queryPlan).isEqualTo(defaultPolicyQueryPlan);
}
Aggregations