Search in sources :

Example 1 with UnexpectedEndpointDisconnectedEvent

use of com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent in project couchbase-jvm-clients by couchbase.

the class BaseEndpointIntegrationTest method mustReconnectWhenChannelCloses.

/**
 * When the underlying channel closes, the endpoint must continue to reconnect until being instructed
 * to stop with an explicit disconnect command.
 */
@Test
void mustReconnectWhenChannelCloses() {
    LocalServerController localServerController = startLocalServer(eventLoopGroup);
    ServiceContext serviceContext = new ServiceContext(new CoreContext(null, 1, env, authenticator()), "127.0.0.1", 1234, ServiceType.KV, Optional.empty());
    BaseEndpoint endpoint = new BaseEndpoint("127.0.0.1", 1234, eventLoopGroup, serviceContext, CircuitBreakerConfig.enabled(false).build(), ServiceType.QUERY, false) {

        @Override
        protected PipelineInitializer pipelineInitializer() {
            return (endpoint, pipeline) -> {
            };
        }

        @Override
        protected SocketAddress remoteAddress() {
            return new LocalAddress("server");
        }
    };
    List<EndpointState> transitions = Collections.synchronizedList(new ArrayList<>());
    endpoint.states().subscribe(transitions::add);
    assertEquals(0, localServerController.connectAttempts.get());
    assertNull(localServerController.channel.get());
    endpoint.connect();
    waitUntilCondition(() -> endpoint.state() == EndpointState.CONNECTED);
    waitUntilCondition(() -> localServerController.connectAttempts.get() == 1);
    assertNotNull(localServerController.channel.get());
    localServerController.channel.get().close().awaitUninterruptibly();
    List<EndpointState> expectedTransitions = Arrays.asList(// initial state
    EndpointState.DISCONNECTED, // initial connect attempt
    EndpointState.CONNECTING, // properly connected the first time
    EndpointState.CONNECTED, // disconnected when we kill the channel from the server side
    EndpointState.DISCONNECTED, // endpoint should be reconnecting now
    EndpointState.CONNECTING, // finally, we are able to reconnect completely
    EndpointState.CONNECTED);
    waitUntilCondition(() -> transitions.size() == expectedTransitions.size());
    assertEquals(expectedTransitions, transitions);
    waitUntilCondition(() -> localServerController.connectAttempts.get() >= 2);
    endpoint.disconnect();
    waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
    boolean hasDisconnectEvent = false;
    for (Event event : eventBus.publishedEvents()) {
        if (event instanceof UnexpectedEndpointDisconnectedEvent) {
            hasDisconnectEvent = true;
            break;
        }
    }
    assertTrue(hasDisconnectEvent);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) SocketAddress(java.net.SocketAddress) SimpleChannelInboundHandler(com.couchbase.client.core.deps.io.netty.channel.SimpleChannelInboundHandler) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ServerBootstrap(com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) ServiceContext(com.couchbase.client.core.service.ServiceContext) Channel(com.couchbase.client.core.deps.io.netty.channel.Channel) ChannelHandlerContext(com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext) Util.waitUntilCondition(com.couchbase.client.test.Util.waitUntilCondition) ChannelInitializer(com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer) Event(com.couchbase.client.core.cnc.Event) LocalServerChannel(com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) DefaultEventLoopGroup(com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup) Test(org.junit.jupiter.api.Test) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Collections(java.util.Collections) CoreContext(com.couchbase.client.core.CoreContext) LocalAddress(com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress) ServiceContext(com.couchbase.client.core.service.ServiceContext) UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent) Event(com.couchbase.client.core.cnc.Event) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with UnexpectedEndpointDisconnectedEvent

use of com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent in project couchbase-jvm-clients by couchbase.

the class BaseEndpoint method notifyChannelInactive.

/**
 * This method is called from inside the channel to tell the endpoint hat it got inactive.
 *
 * <p>The endpoint needs to perform certain steps when the channel is inactive so that it quickly tries
 * to reconnect, as long as it should (i.e. don't do it if already disconnected)</p>
 */
@Stability.Internal
public void notifyChannelInactive() {
    int outstandingBeforeInactive = outstandingRequests.getAndSet(0);
    if (disconnect.get()) {
        // We don't need to do anything if we've been already instructed to disconnect.
        return;
    }
    long connectedSince = System.nanoTime() - lastConnectedAt;
    if (state() == EndpointState.CONNECTED) {
        endpointContext.get().environment().eventBus().publish(new UnexpectedEndpointDisconnectedEvent(endpointContext.get(), outstandingBeforeInactive, connectedSince));
        state.transition(EndpointState.DISCONNECTED);
        connect();
    }
}
Also used : UnexpectedEndpointDisconnectedEvent(com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent)

Aggregations

UnexpectedEndpointDisconnectedEvent (com.couchbase.client.core.cnc.events.endpoint.UnexpectedEndpointDisconnectedEvent)2 CoreContext (com.couchbase.client.core.CoreContext)1 Event (com.couchbase.client.core.cnc.Event)1 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)1 ServerBootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.ServerBootstrap)1 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)1 Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)1 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)1 ChannelInitializer (com.couchbase.client.core.deps.io.netty.channel.ChannelInitializer)1 DefaultEventLoopGroup (com.couchbase.client.core.deps.io.netty.channel.DefaultEventLoopGroup)1 SimpleChannelInboundHandler (com.couchbase.client.core.deps.io.netty.channel.SimpleChannelInboundHandler)1 LocalAddress (com.couchbase.client.core.deps.io.netty.channel.local.LocalAddress)1 LocalServerChannel (com.couchbase.client.core.deps.io.netty.channel.local.LocalServerChannel)1 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)1 ServiceContext (com.couchbase.client.core.service.ServiceContext)1 ServiceType (com.couchbase.client.core.service.ServiceType)1 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)1 Util.waitUntilCondition (com.couchbase.client.test.Util.waitUntilCondition)1 SocketAddress (java.net.SocketAddress)1 ArrayList (java.util.ArrayList)1