Search in sources :

Example 1 with LoadBalancingPolicy

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);
    }
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) NoopRequestTracker(com.datastax.oss.driver.internal.core.tracker.NoopRequestTracker) ArrayList(java.util.ArrayList) MultiplexingRequestTracker(com.datastax.oss.driver.internal.core.tracker.MultiplexingRequestTracker) RequestTracker(com.datastax.oss.driver.api.core.tracker.RequestTracker) NoopRequestTracker(com.datastax.oss.driver.internal.core.tracker.NoopRequestTracker) MultiplexingRequestTracker(com.datastax.oss.driver.internal.core.tracker.MultiplexingRequestTracker)

Example 2 with LoadBalancingPolicy

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);
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Node(com.datastax.oss.driver.api.core.metadata.Node) Test(org.junit.Test)

Example 3 with LoadBalancingPolicy

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();
            }
        }
    }
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Metadata(com.datastax.oss.driver.api.core.metadata.Metadata)

Example 4 with LoadBalancingPolicy

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<>();
    }
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Node(com.datastax.oss.driver.api.core.metadata.Node) ArrayList(java.util.ArrayList) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with LoadBalancingPolicy

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);
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Node(com.datastax.oss.driver.api.core.metadata.Node) DistanceReporter(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy.DistanceReporter) Test(org.junit.Test)

Aggregations

LoadBalancingPolicy (com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy)9 Node (com.datastax.oss.driver.api.core.metadata.Node)5 Test (org.junit.Test)5 DistanceReporter (com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy.DistanceReporter)2 ArrayList (java.util.ArrayList)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)1 NodeDistance (com.datastax.oss.driver.api.core.loadbalancing.NodeDistance)1 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)1 RequestTracker (com.datastax.oss.driver.api.core.tracker.RequestTracker)1 MultiplexingRequestTracker (com.datastax.oss.driver.internal.core.tracker.MultiplexingRequestTracker)1 NoopRequestTracker (com.datastax.oss.driver.internal.core.tracker.NoopRequestTracker)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 UUID (java.util.UUID)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BeforeClass (org.junit.BeforeClass)1