Search in sources :

Example 1 with DiscoveryResult

use of com.netflix.zuul.discovery.DiscoveryResult in project zuul by Netflix.

the class DefaultClientChannelManager method release.

@Override
public boolean release(final PooledConnection conn) {
    conn.stopRequestTimer();
    releaseConnCounter.increment();
    connsInUse.decrementAndGet();
    final DiscoveryResult discoveryResult = conn.getServer();
    discoveryResult.decrementActiveRequestsCount();
    discoveryResult.incrementNumRequests();
    if (shuttingDown) {
        return false;
    }
    boolean released = false;
    // if the connection has been around too long (i.e. too many requests), then close it
    // TODO(argha-c): Document what is a reasonable default here, and the class of origins that optimizes for
    final boolean connExpiredLifetime = conn.getUsageCount() > connPoolConfig.getMaxRequestsPerConnection();
    if (conn.isShouldClose() || connExpiredLifetime) {
        // Close and discard the connection, as it has been flagged (possibly due to receiving a non-channel error like a 503).
        conn.setInPool(false);
        conn.close();
        if (connExpiredLifetime) {
            closeExpiredConnLifetimeCounter.increment();
        }
    } else if (discoveryResult.isCircuitBreakerTripped()) {
        // Don't put conns for currently circuit-tripped servers back into the pool.
        conn.setInPool(false);
        conn.close();
    } else if (!conn.isActive()) {
        // Connection is already closed, so discard.
        alreadyClosedCounter.increment();
        // make sure to decrement OpenConnectionCounts
        conn.updateServerStats();
        conn.setInPool(false);
    } else {
        releaseHandlers(conn);
        // Attempt to return connection to the pool.
        IConnectionPool pool = perServerPools.get(discoveryResult);
        if (pool != null) {
            released = pool.release(conn);
        } else {
            // The pool for this server no longer exists (maybe due to it failling out of
            // discovery).
            conn.setInPool(false);
            released = false;
            conn.close();
        }
        if (LOG.isDebugEnabled())
            LOG.debug("PooledConnection released: " + conn.toString());
    }
    return released;
}
Also used : DiscoveryResult(com.netflix.zuul.discovery.DiscoveryResult)

Example 2 with DiscoveryResult

use of com.netflix.zuul.discovery.DiscoveryResult in project zuul by Netflix.

the class DefaultClientChannelManagerTest method updateServerRefOnEmptyDiscoveryResult.

@Test
public void updateServerRefOnEmptyDiscoveryResult() {
    OriginName originName = OriginName.fromVip("vip", "test");
    final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    final DynamicServerResolver resolver = mock(DynamicServerResolver.class);
    when(resolver.resolve(any())).thenReturn(DiscoveryResult.EMPTY);
    final DefaultClientChannelManager clientChannelManager = new DefaultClientChannelManager(originName, clientConfig, resolver, new DefaultRegistry());
    final AtomicReference<DiscoveryResult> serverRef = new AtomicReference<>();
    final Promise<PooledConnection> promise = clientChannelManager.acquire(new DefaultEventLoop(), null, CurrentPassport.create(), serverRef, new AtomicReference<>());
    Truth.assertThat(promise.isSuccess()).isFalse();
    Truth.assertThat(serverRef.get()).isSameInstanceAs(DiscoveryResult.EMPTY);
}
Also used : OriginName(com.netflix.zuul.origins.OriginName) DiscoveryResult(com.netflix.zuul.discovery.DiscoveryResult) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) DefaultEventLoop(io.netty.channel.DefaultEventLoop) DynamicServerResolver(com.netflix.zuul.discovery.DynamicServerResolver) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 3 with DiscoveryResult

use of com.netflix.zuul.discovery.DiscoveryResult in project zuul by Netflix.

the class DefaultClientChannelManagerTest method pickAddressInternal_discovery_unresolved.

@Test
public void pickAddressInternal_discovery_unresolved() {
    InstanceInfo instanceInfo = Builder.newBuilder().setAppName("app").setHostName("localhost").setPort(443).build();
    DiscoveryResult s = DiscoveryResult.from(instanceInfo, true);
    SocketAddress addr = DefaultClientChannelManager.pickAddressInternal(s, OriginName.fromVip("vip"));
    Truth.assertThat(addr).isInstanceOf(InetSocketAddress.class);
    InetSocketAddress socketAddress = (InetSocketAddress) addr;
    assertTrue(socketAddress.toString(), socketAddress.getAddress().isLoopbackAddress());
    assertEquals(443, socketAddress.getPort());
}
Also used : DiscoveryResult(com.netflix.zuul.discovery.DiscoveryResult) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InstanceInfo(com.netflix.appinfo.InstanceInfo) Test(org.junit.Test)

Example 4 with DiscoveryResult

use of com.netflix.zuul.discovery.DiscoveryResult in project zuul by Netflix.

the class DefaultClientChannelManagerTest method initializeAndShutdown.

@Test
public void initializeAndShutdown() throws Exception {
    final String appName = "app-" + UUID.randomUUID();
    final ServerSocket serverSocket = new ServerSocket(0);
    final InetSocketAddress serverSocketAddress = (InetSocketAddress) serverSocket.getLocalSocketAddress();
    final String serverHostname = serverSocketAddress.getHostName();
    final int serverPort = serverSocketAddress.getPort();
    final OriginName originName = OriginName.fromVipAndApp("vip", appName);
    final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    Server.defaultOutboundChannelType.set(NioSocketChannel.class);
    final InstanceInfo instanceInfo = Builder.newBuilder().setAppName(appName).setHostName(serverHostname).setPort(serverPort).build();
    DiscoveryResult discoveryResult = DiscoveryResult.from(instanceInfo, true);
    final DynamicServerResolver resolver = mock(DynamicServerResolver.class);
    when(resolver.resolve(any())).thenReturn(discoveryResult);
    when(resolver.hasServers()).thenReturn(true);
    final Registry registry = new DefaultRegistry();
    final DefaultClientChannelManager clientChannelManager = new DefaultClientChannelManager(originName, clientConfig, resolver, registry);
    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(10);
    final EventLoop eventLoop = eventLoopGroup.next();
    clientChannelManager.init();
    Truth.assertThat(clientChannelManager.getConnsInUse()).isEqualTo(0);
    final Promise<PooledConnection> promiseConn = clientChannelManager.acquire(eventLoop);
    promiseConn.await(200, TimeUnit.MILLISECONDS);
    assertTrue(promiseConn.isDone());
    assertTrue(promiseConn.isSuccess());
    final PooledConnection connection = promiseConn.get();
    assertTrue(connection.isActive());
    assertFalse(connection.isInPool());
    Truth.assertThat(clientChannelManager.getConnsInUse()).isEqualTo(1);
    final boolean releaseResult = clientChannelManager.release(connection);
    assertTrue(releaseResult);
    assertTrue(connection.isInPool());
    Truth.assertThat(clientChannelManager.getConnsInUse()).isEqualTo(0);
    clientChannelManager.shutdown();
    serverSocket.close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) Registry(com.netflix.spectator.api.Registry) InstanceInfo(com.netflix.appinfo.InstanceInfo) OriginName(com.netflix.zuul.origins.OriginName) DiscoveryResult(com.netflix.zuul.discovery.DiscoveryResult) DefaultEventLoop(io.netty.channel.DefaultEventLoop) EventLoop(io.netty.channel.EventLoop) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) DynamicServerResolver(com.netflix.zuul.discovery.DynamicServerResolver) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 5 with DiscoveryResult

use of com.netflix.zuul.discovery.DiscoveryResult in project zuul by Netflix.

the class DefaultClientChannelManagerTest method updateServerRefOnValidDiscoveryResult.

@Test
public void updateServerRefOnValidDiscoveryResult() {
    OriginName originName = OriginName.fromVip("vip", "test");
    final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    final DynamicServerResolver resolver = mock(DynamicServerResolver.class);
    final InstanceInfo instanceInfo = Builder.newBuilder().setAppName("server-equality").setHostName("server-equality").setPort(7777).build();
    final DiscoveryResult discoveryResult = DiscoveryResult.from(instanceInfo, false);
    when(resolver.resolve(any())).thenReturn(discoveryResult);
    final DefaultClientChannelManager clientChannelManager = new DefaultClientChannelManager(originName, clientConfig, resolver, new DefaultRegistry());
    final AtomicReference<DiscoveryResult> serverRef = new AtomicReference<>();
    // TODO(argha-c) capture and assert on the promise once we have a dummy with ServerStats initialized
    clientChannelManager.acquire(new DefaultEventLoop(), null, CurrentPassport.create(), serverRef, new AtomicReference<>());
    Truth.assertThat(serverRef.get()).isSameInstanceAs(discoveryResult);
}
Also used : OriginName(com.netflix.zuul.origins.OriginName) DiscoveryResult(com.netflix.zuul.discovery.DiscoveryResult) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) DefaultEventLoop(io.netty.channel.DefaultEventLoop) DynamicServerResolver(com.netflix.zuul.discovery.DynamicServerResolver) AtomicReference(java.util.concurrent.atomic.AtomicReference) InstanceInfo(com.netflix.appinfo.InstanceInfo) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Aggregations

DiscoveryResult (com.netflix.zuul.discovery.DiscoveryResult)7 Test (org.junit.Test)5 InstanceInfo (com.netflix.appinfo.InstanceInfo)4 InetSocketAddress (java.net.InetSocketAddress)4 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)3 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)3 DynamicServerResolver (com.netflix.zuul.discovery.DynamicServerResolver)3 OriginName (com.netflix.zuul.origins.OriginName)3 DefaultEventLoop (io.netty.channel.DefaultEventLoop)3 SocketAddress (java.net.SocketAddress)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Registry (com.netflix.spectator.api.Registry)1 EventLoop (io.netty.channel.EventLoop)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 ServerSocket (java.net.ServerSocket)1