com.netflix.client
Class AbstractLoadBalancerAwareClient<S extends ClientRequest,T extends IResponse>

java.lang.Object
  extended by com.netflix.client.AbstractLoadBalancerAwareClient<S,T>
All Implemented Interfaces:
IClient<S,T>, IClientConfigAware

public abstract class AbstractLoadBalancerAwareClient<S extends ClientRequest,T extends IResponse>
extends java.lang.Object
implements IClient<S,T>, IClientConfigAware

Abstract class that provides the integration of client with load balancers.


Constructor Summary
AbstractLoadBalancerAwareClient()
           
AbstractLoadBalancerAwareClient(IClientConfig clientConfig)
          Delegate to initWithNiwsConfig(IClientConfig)
 
Method Summary
protected  S computeFinalUriWithLoadBalancer(S original)
          Compute the final URI from a partial URI in the request.
protected  com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveHostAndPortFromVipAddress(java.lang.String vipAddress)
          Derive the host and port from virtual address if virtual address is indeed contains the actual host and port of the server.
protected abstract  com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveSchemeAndPortFromPartialUri(S task)
          Derive scheme and port from a partial URI.
protected  T executeOnSingleServer(S request)
          Execute the request on single server after the final URI is calculated.
 T executeWithLoadBalancer(S request)
          This method should be used when the caller wants to dispatch the request to a server chosen by the load balancer, instead of specifying the server in the request's URI.
 java.lang.String getClientName()
           
protected abstract  int getDefaultPort()
          Get the default port which is protocol specific if port is missing in the request URI.
 ILoadBalancer getLoadBalancer()
           
 void initWithNiwsConfig(IClientConfig clientConfig)
          Set necessary parameters from client configuration and register with Servo monitors.
protected abstract  boolean isCircuitBreakerException(java.lang.Exception e)
          Determine if an exception should contribute to circuit breaker trip.
protected abstract  boolean isRetriableException(java.lang.Exception e)
          Determine if operation can be retried if an exception is thrown.
protected  void noteOpenConnection(ServerStats serverStats, S task)
          Called just before IClient.execute(ClientRequest) call.
protected  void noteRequestCompletion(ServerStats stats, S task, IResponse response, java.lang.Exception e, long responseTime)
          This is called after a response is received or an exception is thrown from the IClient.execute(ClientRequest) to update related stats.
protected  void noteResponseReceived(ServerStats stats, T task, IResponse response)
          This method is called after a response (either success or not) is received to update certain stats.
 void setLoadBalancer(ILoadBalancer lb)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.netflix.client.IClient
execute
 

Constructor Detail

AbstractLoadBalancerAwareClient

public AbstractLoadBalancerAwareClient()

AbstractLoadBalancerAwareClient

public AbstractLoadBalancerAwareClient(IClientConfig clientConfig)
Delegate to initWithNiwsConfig(IClientConfig)

Parameters:
clientConfig -
Method Detail

initWithNiwsConfig

public void initWithNiwsConfig(IClientConfig clientConfig)
Set necessary parameters from client configuration and register with Servo monitors.

Specified by:
initWithNiwsConfig in interface IClientConfigAware

getClientName

public final java.lang.String getClientName()

getLoadBalancer

public ILoadBalancer getLoadBalancer()

setLoadBalancer

public void setLoadBalancer(ILoadBalancer lb)

isCircuitBreakerException

protected abstract boolean isCircuitBreakerException(java.lang.Exception e)
Determine if an exception should contribute to circuit breaker trip. If such exceptions happen consecutively on a server, it will be deemed as circuit breaker tripped and enter into a time out when it will be skipped by the AvailabilityFilteringRule, which is the default rule for load balancers.


isRetriableException

protected abstract boolean isRetriableException(java.lang.Exception e)
Determine if operation can be retried if an exception is thrown. For example, connect timeout related exceptions are typically retriable.


executeOnSingleServer

protected T executeOnSingleServer(S request)
                                             throws ClientException
Execute the request on single server after the final URI is calculated. This method takes care of retries and update server stats.

Throws:
ClientException

noteRequestCompletion

protected void noteRequestCompletion(ServerStats stats,
                                     S task,
                                     IResponse response,
                                     java.lang.Exception e,
                                     long responseTime)
This is called after a response is received or an exception is thrown from the IClient.execute(ClientRequest) to update related stats.


noteResponseReceived

protected void noteResponseReceived(ServerStats stats,
                                    T task,
                                    IResponse response)
This method is called after a response (either success or not) is received to update certain stats.


noteOpenConnection

protected void noteOpenConnection(ServerStats serverStats,
                                  S task)
Called just before IClient.execute(ClientRequest) call.


executeWithLoadBalancer

public T executeWithLoadBalancer(S request)
                                            throws ClientException
This method should be used when the caller wants to dispatch the request to a server chosen by the load balancer, instead of specifying the server in the request's URI. It calculates the final URI by calling computeFinalUriWithLoadBalancer(ClientRequest) and then calls IClient.execute(ClientRequest).

Parameters:
request - request to be dispatched to a server chosen by the load balancer. The URI can be a partial URI which does not contain the host name or the protocol.
Throws:
ClientException

deriveSchemeAndPortFromPartialUri

protected abstract com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveSchemeAndPortFromPartialUri(S task)
Derive scheme and port from a partial URI. For example, for HTTP based client, the URI with only path "/" should return "http" and 80, whereas the URI constructed with scheme "https" and path "/" should return "https" and 443. This method is called by computeFinalUriWithLoadBalancer(ClientRequest) to get the complete executable URI.


getDefaultPort

protected abstract int getDefaultPort()
Get the default port which is protocol specific if port is missing in the request URI.


deriveHostAndPortFromVipAddress

protected com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveHostAndPortFromVipAddress(java.lang.String vipAddress)
                                                                                             throws java.net.URISyntaxException,
                                                                                                    ClientException
Derive the host and port from virtual address if virtual address is indeed contains the actual host and port of the server. This is the final resort to compute the final URI in computeFinalUriWithLoadBalancer(ClientRequest) if there is no load balancer available and the request URI is incomplete. Sub classes can override this method to be more accurate or throws ClientException if it does not want to support virtual address to be the same as physical server address.

The virtual address is used by certain load balancers to filter the servers of the same function to form the server pool.

Throws:
java.net.URISyntaxException
ClientException

computeFinalUriWithLoadBalancer

protected S computeFinalUriWithLoadBalancer(S original)
                                                           throws ClientException
Compute the final URI from a partial URI in the request. The following steps are performed:
  • if host is missing and there is a load balancer, get the host/port from server chosen from load balancer
  • if host is missing and there is no load balancer, try to derive host/port from virtual address set with the client
  • if host is present and the authority part of the URI is a virtual address set for the client, and there is a load balancer, get the host/port from server chosen from load balancer
  • if host is present but none of the above applies, interpret the host as the actual physical address
  • if host is missing but none of the above applies, throws ClientException

    Parameters:
    original - Original URI passed from caller
    Returns:
    new request with the final URI
    Throws:
    ClientException