Search in sources :

Example 1 with OriginName

use of com.netflix.zuul.origins.OriginName 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 2 with OriginName

use of com.netflix.zuul.origins.OriginName 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)

Example 3 with OriginName

use of com.netflix.zuul.origins.OriginName 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 4 with OriginName

use of com.netflix.zuul.origins.OriginName in project zuul by Netflix.

the class ProxyEndpoint method getOrigin.

/**
 * Get the implementing origin.
 *
 * Note: this method gets called in the constructor so if overloading it or any methods called within, you cannot
 * rely on your own constructor parameters.
 */
@Nullable
protected NettyOrigin getOrigin(HttpRequestMessage request) {
    SessionContext context = request.getContext();
    OriginManager<NettyOrigin> originManager = (OriginManager<NettyOrigin>) context.get(CommonContextKeys.ORIGIN_MANAGER);
    if (Debug.debugRequest(context)) {
        ImmutableList.Builder<String> routingLogEntries = context.get(CommonContextKeys.ROUTING_LOG);
        if (routingLogEntries != null) {
            for (String entry : routingLogEntries.build()) {
                Debug.addRequestDebug(context, "RoutingLog: " + entry);
            }
        }
    }
    String primaryRoute = context.getRouteVIP();
    if (Strings.isNullOrEmpty(primaryRoute)) {
        // If no vip selected, leave origin null, then later the handleNoOriginSelected() method will be invoked.
        return null;
    }
    // make sure the restClientName will never be a raw VIP in cases where it's the fallback for another route assignment
    String restClientVIP = primaryRoute;
    boolean useFullName = context.getBoolean(CommonContextKeys.USE_FULL_VIP_NAME);
    String restClientName = useFullName ? restClientVIP : VipUtils.getVIPPrefix(restClientVIP);
    NettyOrigin origin = null;
    // allow implementors to override the origin with custom injection logic
    OriginName overrideOriginName = injectCustomOriginName(request);
    if (overrideOriginName != null) {
        // Use the custom vip instead if one has been provided.
        origin = getOrCreateOrigin(originManager, overrideOriginName, request.reconstructURI(), context);
    } else if (restClientName != null) {
        // This is the normal flow - that a RoutingFilter has assigned a route
        OriginName originName = OriginName.fromVip(restClientVIP, restClientName);
        origin = getOrCreateOrigin(originManager, originName, request.reconstructURI(), context);
    }
    verifyOrigin(context, request, restClientName, origin);
    // Update the routeVip on context to show the actual raw VIP from the clientConfig of the chosen Origin.
    if (origin != null) {
        context.set(CommonContextKeys.ACTUAL_VIP, origin.getClientConfig().get(IClientConfigKey.Keys.DeploymentContextBasedVipAddresses));
        context.set(CommonContextKeys.ORIGIN_VIP_SECURE, origin.getClientConfig().get(IClientConfigKey.Keys.IsSecure));
    }
    return origin;
}
Also used : OriginName(com.netflix.zuul.origins.OriginName) NettyOrigin(com.netflix.zuul.origins.NettyOrigin) ImmutableList(com.google.common.collect.ImmutableList) SessionContext(com.netflix.zuul.context.SessionContext) OriginManager(com.netflix.zuul.origins.OriginManager) Nullable(javax.annotation.Nullable)

Aggregations

OriginName (com.netflix.zuul.origins.OriginName)4 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)3 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)3 DiscoveryResult (com.netflix.zuul.discovery.DiscoveryResult)3 DynamicServerResolver (com.netflix.zuul.discovery.DynamicServerResolver)3 DefaultEventLoop (io.netty.channel.DefaultEventLoop)3 Test (org.junit.Test)3 InstanceInfo (com.netflix.appinfo.InstanceInfo)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ImmutableList (com.google.common.collect.ImmutableList)1 Registry (com.netflix.spectator.api.Registry)1 SessionContext (com.netflix.zuul.context.SessionContext)1 NettyOrigin (com.netflix.zuul.origins.NettyOrigin)1 OriginManager (com.netflix.zuul.origins.OriginManager)1 EventLoop (io.netty.channel.EventLoop)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Nullable (javax.annotation.Nullable)1