Search in sources :

Example 6 with EventBus

use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.

the class MetricsITBase method should_not_evict_down_node_metrics_when_node_is_back_up_before_timeout.

@Test
public void should_not_evict_down_node_metrics_when_node_is_back_up_before_timeout() throws Exception {
    // given
    Duration expireAfter = Duration.ofSeconds(2);
    DriverConfigLoader loader = allMetricsEnabled().withDuration(DefaultDriverOption.METRICS_NODE_EXPIRE_AFTER, expireAfter).build();
    AbstractMetricUpdater.MIN_EXPIRE_AFTER = expireAfter;
    try (CqlSession session = CqlSession.builder().addContactEndPoints(simulacron().getContactPoints()).withConfigLoader(loader).withMetricRegistry(newMetricRegistry()).build()) {
        queryAllNodes(session);
        DefaultNode node1 = findNode(session, 0);
        DefaultNode node2 = findNode(session, 1);
        DefaultNode node3 = findNode(session, 2);
        EventBus eventBus = ((InternalDriverContext) session.getContext()).getEventBus();
        // trigger nodes UP -> DOWN
        eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.DOWN, node1));
        eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.FORCED_DOWN, node2));
        eventBus.fire(NodeStateEvent.removed(node3));
        Thread.sleep(500);
        // trigger nodes DOWN -> UP, should cancel the timeouts
        eventBus.fire(NodeStateEvent.changed(NodeState.DOWN, NodeState.UP, node1));
        eventBus.fire(NodeStateEvent.changed(NodeState.FORCED_DOWN, NodeState.UP, node2));
        eventBus.fire(NodeStateEvent.added(node3));
        Thread.sleep(expireAfter.toMillis());
        // then no node-level metrics should be evicted
        assertNodeMetricsNotEvicted(session, node1);
        assertNodeMetricsNotEvicted(session, node2);
        assertNodeMetricsNotEvicted(session, node3);
    } finally {
        AbstractMetricUpdater.MIN_EXPIRE_AFTER = Duration.ofMinutes(5);
    }
}
Also used : DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Duration(java.time.Duration) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) CqlSession(com.datastax.oss.driver.api.core.CqlSession) InternalDriverContext(com.datastax.oss.driver.internal.core.context.InternalDriverContext) Test(org.junit.Test)

Example 7 with EventBus

use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.

the class DefaultSessionPoolsTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    DefaultEventLoopGroup adminEventLoopGroup = new DefaultEventLoopGroup(1);
    when(nettyOptions.adminEventExecutorGroup()).thenReturn(adminEventLoopGroup);
    when(context.getNettyOptions()).thenReturn(nettyOptions);
    // Config:
    when(defaultProfile.getBoolean(DefaultDriverOption.REQUEST_WARN_IF_SET_KEYSPACE)).thenReturn(true);
    when(defaultProfile.getBoolean(DefaultDriverOption.REPREPARE_ENABLED)).thenReturn(false);
    when(defaultProfile.isDefined(DefaultDriverOption.PROTOCOL_VERSION)).thenReturn(true);
    when(defaultProfile.getDuration(DefaultDriverOption.METADATA_TOPOLOGY_WINDOW)).thenReturn(Duration.ZERO);
    when(defaultProfile.getInt(DefaultDriverOption.METADATA_TOPOLOGY_MAX_EVENTS)).thenReturn(1);
    when(config.getDefaultProfile()).thenReturn(defaultProfile);
    when(context.getConfig()).thenReturn(config);
    // Init sequence:
    when(metadataManager.refreshNodes()).thenReturn(CompletableFuture.completedFuture(null));
    when(metadataManager.refreshSchema(null, false, true)).thenReturn(CompletableFuture.completedFuture(null));
    when(context.getMetadataManager()).thenReturn(metadataManager);
    when(topologyMonitor.init()).thenReturn(CompletableFuture.completedFuture(null));
    when(context.getTopologyMonitor()).thenReturn(topologyMonitor);
    when(context.getLoadBalancingPolicyWrapper()).thenReturn(loadBalancingPolicyWrapper);
    when(context.getConfigLoader()).thenReturn(configLoader);
    when(context.getMetricsFactory()).thenReturn(metricsFactory);
    // Runtime behavior:
    when(context.getSessionName()).thenReturn("test");
    when(context.getChannelPoolFactory()).thenReturn(channelPoolFactory);
    eventBus = spy(new EventBus("test"));
    when(context.getEventBus()).thenReturn(eventBus);
    node1 = mockLocalNode(1);
    node2 = mockLocalNode(2);
    node3 = mockLocalNode(3);
    @SuppressWarnings("ConstantConditions") ImmutableMap<UUID, Node> nodes = ImmutableMap.of(node1.getHostId(), node1, node2.getHostId(), node2, node3.getHostId(), node3);
    when(metadata.getNodes()).thenReturn(nodes);
    when(metadataManager.getMetadata()).thenReturn(metadata);
    PoolManager poolManager = new PoolManager(context);
    when(context.getPoolManager()).thenReturn(poolManager);
    // Shutdown sequence:
    when(context.getReconnectionPolicy()).thenReturn(reconnectionPolicy);
    when(context.getRetryPolicy(DriverExecutionProfile.DEFAULT_NAME)).thenReturn(retryPolicy);
    when(context.getSpeculativeExecutionPolicies()).thenReturn(ImmutableMap.of(DriverExecutionProfile.DEFAULT_NAME, speculativeExecutionPolicy));
    when(context.getAddressTranslator()).thenReturn(addressTranslator);
    when(context.getNodeStateListener()).thenReturn(nodeStateListener);
    when(context.getSchemaChangeListener()).thenReturn(schemaChangeListener);
    when(context.getRequestTracker()).thenReturn(requestTracker);
    when(metadataManager.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
    when(metadataManager.forceCloseAsync()).thenReturn(CompletableFuture.completedFuture(null));
    when(topologyMonitor.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
    when(topologyMonitor.forceCloseAsync()).thenReturn(CompletableFuture.completedFuture(null));
    when(context.getControlConnection()).thenReturn(controlConnection);
    when(controlConnection.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
    when(controlConnection.forceCloseAsync()).thenReturn(CompletableFuture.completedFuture(null));
    DefaultPromise<Void> nettyCloseFuture = new DefaultPromise<>(GlobalEventExecutor.INSTANCE);
    nettyCloseFuture.setSuccess(null);
    when(nettyOptions.onClose()).thenAnswer(invocation -> nettyCloseFuture);
}
Also used : DefaultPromise(io.netty.util.concurrent.DefaultPromise) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) Node(com.datastax.oss.driver.api.core.metadata.Node) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) UUID(java.util.UUID) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Before(org.junit.Before)

Example 8 with EventBus

use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.

the class PoolManagerTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    DefaultEventLoopGroup adminEventLoopGroup = new DefaultEventLoopGroup(1);
    when(nettyOptions.adminEventExecutorGroup()).thenReturn(adminEventLoopGroup);
    when(context.getNettyOptions()).thenReturn(nettyOptions);
    when(context.getEventBus()).thenReturn(new EventBus("test"));
    when(config.getDefaultProfile()).thenReturn(defaultProfile);
    when(context.getConfig()).thenReturn(config);
}
Also used : EventBus(com.datastax.oss.driver.internal.core.context.EventBus) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Before(org.junit.Before)

Example 9 with EventBus

use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.

the class EventBusTest method setup.

@Before
public void setup() {
    bus = new EventBus("test");
    results = new HashMap<>();
}
Also used : EventBus(com.datastax.oss.driver.internal.core.context.EventBus) Before(org.junit.Before)

Example 10 with EventBus

use of com.datastax.oss.driver.internal.core.context.EventBus in project java-driver by datastax.

the class ChannelPoolTestBase method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    adminEventLoopGroup = new DefaultEventLoopGroup(1);
    when(context.getNettyOptions()).thenReturn(nettyOptions);
    when(nettyOptions.adminEventExecutorGroup()).thenReturn(adminEventLoopGroup);
    when(context.getConfig()).thenReturn(config);
    when(config.getDefaultProfile()).thenReturn(defaultProfile);
    this.eventBus = spy(new EventBus("test"));
    when(context.getEventBus()).thenReturn(eventBus);
    when(context.getChannelFactory()).thenReturn(channelFactory);
    when(context.getReconnectionPolicy()).thenReturn(reconnectionPolicy);
    when(reconnectionPolicy.newNodeSchedule(any(Node.class))).thenReturn(reconnectionSchedule);
    // By default, set a large reconnection delay. Tests that care about reconnection will override
    // it.
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofDays(1));
    when(context.getMetricsFactory()).thenReturn(metricsFactory);
    when(metricsFactory.newNodeUpdater(any(Node.class))).thenReturn(nodeMetricUpdater);
    node = TestNodeFactory.newNode(1, context);
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultNode(com.datastax.oss.driver.internal.core.metadata.DefaultNode) EventBus(com.datastax.oss.driver.internal.core.context.EventBus) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Before(org.junit.Before)

Aggregations

EventBus (com.datastax.oss.driver.internal.core.context.EventBus)11 Before (org.junit.Before)8 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)6 Node (com.datastax.oss.driver.api.core.metadata.Node)5 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)5 InternalDriverContext (com.datastax.oss.driver.internal.core.context.InternalDriverContext)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)3 Duration (java.time.Duration)3 Test (org.junit.Test)3 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)1 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 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)1 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)1 TestResponses (com.datastax.oss.driver.internal.core.TestResponses)1 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)1