Search in sources :

Example 6 with LoadBalancingPolicy

use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.

the class LoadBalancingPolicyWrapperTest method should_init_policies_with_all_nodes.

@Test
public void should_init_policies_with_all_nodes() {
    // Given
    node1.state = NodeState.UP;
    node2.state = NodeState.UNKNOWN;
    node3.state = NodeState.DOWN;
    // When
    wrapper.init();
    // Then
    for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
        verify(policy).init(initNodesCaptor.capture(), any(DistanceReporter.class));
        Map<UUID, Node> initNodes = initNodesCaptor.getValue();
        assertThat(initNodes.values()).containsOnly(node1, node2, node3);
    }
}
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) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with LoadBalancingPolicy

use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.

the class LoadBalancingPolicyWrapperTest method should_accumulate_events_during_init_and_replay.

@Test
public void should_accumulate_events_during_init_and_replay() throws InterruptedException {
    // Given
    // Hack to obtain concurrency: the main thread releases another thread and blocks; then the
    // other thread fires an event on the bus and unblocks the main thread.
    CountDownLatch eventLatch = new CountDownLatch(1);
    CountDownLatch initLatch = new CountDownLatch(1);
    // When
    Runnable runnable = () -> {
        try {
            eventLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        eventBus.fire(NodeStateEvent.changed(NodeState.UNKNOWN, NodeState.DOWN, node1));
        initLatch.countDown();
    };
    Thread thread = new Thread(runnable);
    thread.start();
    wrapper.init();
    // Then
    // unblock the thread that will fire the event, and waits until it finishes
    eventLatch.countDown();
    boolean ok = initLatch.await(500, TimeUnit.MILLISECONDS);
    assertThat(ok).isTrue();
    for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
        verify(policy).onDown(node1);
    }
    thread.join(500);
    assertThat(thread.isAlive()).isFalse();
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with LoadBalancingPolicy

use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.

the class LoadBalancingPolicyWrapperTest method should_propagate_node_states_to_policies_after_init.

@Test
public void should_propagate_node_states_to_policies_after_init() {
    // Given
    wrapper.init();
    // When
    eventBus.fire(NodeStateEvent.changed(NodeState.UNKNOWN, NodeState.UP, node1));
    // Then
    for (LoadBalancingPolicy policy : ImmutableList.of(policy1, policy2, policy3)) {
        verify(policy).onUp(node1);
    }
}
Also used : LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Test(org.junit.Test)

Example 9 with LoadBalancingPolicy

use of com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy in project java-driver by datastax.

the class PerProfileLoadBalancingPolicyIT method setup.

@BeforeClass
public static void setup() {
    // sanity checks
    DriverContext context = SESSION_RULE.session().getContext();
    DriverConfig config = context.getConfig();
    assertThat(config.getProfiles()).containsKeys("profile1", "profile2");
    assertThat(context.getLoadBalancingPolicies()).hasSize(3).containsKeys(DriverExecutionProfile.DEFAULT_NAME, "profile1", "profile2");
    LoadBalancingPolicy defaultPolicy = context.getLoadBalancingPolicy(DriverExecutionProfile.DEFAULT_NAME);
    LoadBalancingPolicy policy1 = context.getLoadBalancingPolicy("profile1");
    LoadBalancingPolicy policy2 = context.getLoadBalancingPolicy("profile2");
    assertThat(defaultPolicy).isSameAs(policy2).isNotSameAs(policy1);
    for (Node node : SESSION_RULE.session().getMetadata().getNodes().values()) {
        // if node is in dc2 it should be ignored, otherwise (dc1, dc3) it should be local.
        NodeDistance expectedDistance = Objects.equals(node.getDatacenter(), "dc2") ? NodeDistance.IGNORED : NodeDistance.LOCAL;
        assertThat(node.getDistance()).isEqualTo(expectedDistance);
    }
}
Also used : DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) Node(com.datastax.oss.driver.api.core.metadata.Node) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) BeforeClass(org.junit.BeforeClass)

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