com.netflix.loadbalancer
Class LoadBalancerContext

java.lang.Object
  extended by com.netflix.loadbalancer.LoadBalancerContext
All Implemented Interfaces:
com.netflix.client.IClientConfigAware
Direct Known Subclasses:
LoadBalancerExecutor

public class LoadBalancerContext
extends java.lang.Object
implements com.netflix.client.IClientConfigAware

A class contains APIs intended to be used be load balancing client which is subclass of this class.


Field Summary
protected  java.lang.String clientName
           
protected  com.netflix.client.RetryHandler defaultRetryHandler
           
protected  int maxAutoRetries
           
protected  int maxAutoRetriesNextServer
           
protected  boolean okToRetryOnAllOperations
           
protected  java.lang.String vipAddresses
           
 
Constructor Summary
LoadBalancerContext(ILoadBalancer lb)
           
LoadBalancerContext(ILoadBalancer lb, com.netflix.client.config.IClientConfig clientConfig)
          Delegate to initWithNiwsConfig(IClientConfig)
LoadBalancerContext(ILoadBalancer lb, com.netflix.client.config.IClientConfig clientConfig, com.netflix.client.RetryHandler handler)
           
 
Method Summary
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  com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveSchemeAndPortFromPartialUri(java.net.URI uri)
          Derive scheme and port from a partial URI.
protected  com.netflix.client.ClientException generateNIWSException(java.lang.String uri, java.lang.Throwable e)
           
 java.lang.String getClientName()
           
protected  java.lang.Throwable getDeepestCause(java.lang.Throwable e)
           
protected  int getDefaultPortFromScheme(java.lang.String scheme)
          Get the default port of the target server given the scheme of vip address if it is available.
protected  com.netflix.servo.monitor.Timer getExecuteTracer()
           
 ILoadBalancer getLoadBalancer()
           
 int getMaxAutoRetries()
          Deprecated. 
 int getMaxAutoRetriesNextServer()
          Deprecated. 
protected  int getNumberRetriesOnSameServer(com.netflix.client.config.IClientConfig overriddenClientConfig)
           
protected  int getRetriesNextServer(com.netflix.client.config.IClientConfig overriddenClientConfig)
           
 com.netflix.client.RetryHandler getRetryHandler()
           
protected  Server getServerFromLoadBalancer(java.net.URI original, java.lang.Object loadBalancerKey)
          Compute the final URI from a partial URI in the request.
 ServerStats getServerStats(Server server)
           
protected  boolean handleSameServerRetry(Server server, int currentRetryCount, int maxRetries, java.lang.Throwable e)
           
 void initWithNiwsConfig(com.netflix.client.config.IClientConfig clientConfig)
          Set necessary parameters from client configuration and register with Servo monitors.
 boolean isOkToRetryOnAllOperations()
           
protected  void noteError(ServerStats stats, com.netflix.client.ClientRequest request, java.lang.Throwable e, long responseTime)
          This is called after an error is thrown from the client to update related stats.
protected  void noteOpenConnection(ServerStats serverStats)
          This is usually called just before client execute a request.
protected  void noteRequestCompletion(ServerStats stats, java.lang.Object response, java.lang.Throwable e, long responseTime)
           
protected  void noteRequestCompletion(ServerStats stats, java.lang.Object response, java.lang.Throwable e, long responseTime, com.netflix.client.RetryHandler errorHandler)
          This is called after a response is received or an exception is thrown from the client to update related stats.
protected  void noteResponse(ServerStats stats, com.netflix.client.ClientRequest request, java.lang.Object response, long responseTime)
          This is called after a response is received from the client to update related stats.
 java.net.URI reconstructURIWithServer(Server server, java.net.URI original)
           
 void setLoadBalancer(ILoadBalancer lb)
           
 void setMaxAutoRetries(int maxAutoRetries)
          Deprecated. 
 void setMaxAutoRetriesNextServer(int maxAutoRetriesNextServer)
          Deprecated. 
 void setOkToRetryOnAllOperations(boolean okToRetryOnAllOperations)
           
 void setRetryHandler(com.netflix.client.RetryHandler retryHandler)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientName

protected java.lang.String clientName

vipAddresses

protected java.lang.String vipAddresses

maxAutoRetriesNextServer

protected int maxAutoRetriesNextServer

maxAutoRetries

protected int maxAutoRetries

defaultRetryHandler

protected com.netflix.client.RetryHandler defaultRetryHandler

okToRetryOnAllOperations

protected boolean okToRetryOnAllOperations
Constructor Detail

LoadBalancerContext

public LoadBalancerContext(ILoadBalancer lb)

LoadBalancerContext

public LoadBalancerContext(ILoadBalancer lb,
                           com.netflix.client.config.IClientConfig clientConfig)
Delegate to initWithNiwsConfig(IClientConfig)

Parameters:
clientConfig -

LoadBalancerContext

public LoadBalancerContext(ILoadBalancer lb,
                           com.netflix.client.config.IClientConfig clientConfig,
                           com.netflix.client.RetryHandler handler)
Method Detail

initWithNiwsConfig

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

Specified by:
initWithNiwsConfig in interface com.netflix.client.IClientConfigAware

getExecuteTracer

protected com.netflix.servo.monitor.Timer getExecuteTracer()

getClientName

public java.lang.String getClientName()

getLoadBalancer

public ILoadBalancer getLoadBalancer()

setLoadBalancer

public void setLoadBalancer(ILoadBalancer lb)

getMaxAutoRetriesNextServer

@Deprecated
public int getMaxAutoRetriesNextServer()
Deprecated. 

Use getRetryHandler()


setMaxAutoRetriesNextServer

@Deprecated
public void setMaxAutoRetriesNextServer(int maxAutoRetriesNextServer)
Deprecated. 

Use setRetryHandler(RetryHandler)


getMaxAutoRetries

@Deprecated
public int getMaxAutoRetries()
Deprecated. 

Use getRetryHandler()


setMaxAutoRetries

@Deprecated
public void setMaxAutoRetries(int maxAutoRetries)
Deprecated. 

Use setRetryHandler(RetryHandler)


getDeepestCause

protected java.lang.Throwable getDeepestCause(java.lang.Throwable e)

generateNIWSException

protected com.netflix.client.ClientException generateNIWSException(java.lang.String uri,
                                                                   java.lang.Throwable e)

noteRequestCompletion

protected void noteRequestCompletion(ServerStats stats,
                                     java.lang.Object response,
                                     java.lang.Throwable e,
                                     long responseTime)

noteRequestCompletion

protected void noteRequestCompletion(ServerStats stats,
                                     java.lang.Object response,
                                     java.lang.Throwable e,
                                     long responseTime,
                                     com.netflix.client.RetryHandler errorHandler)
This is called after a response is received or an exception is thrown from the client to update related stats.


noteError

protected void noteError(ServerStats stats,
                         com.netflix.client.ClientRequest request,
                         java.lang.Throwable e,
                         long responseTime)
This is called after an error is thrown from the client to update related stats.


noteResponse

protected void noteResponse(ServerStats stats,
                            com.netflix.client.ClientRequest request,
                            java.lang.Object response,
                            long responseTime)
This is called after a response is received from the client to update related stats.


noteOpenConnection

protected void noteOpenConnection(ServerStats serverStats)
This is usually called just before client execute a request.


deriveSchemeAndPortFromPartialUri

protected com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveSchemeAndPortFromPartialUri(java.net.URI uri)
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 getServerFromLoadBalancer(java.net.URI, Object) and reconstructURIWithServer(Server, java.net.URI) methods to get the complete executable URI.


getDefaultPortFromScheme

protected int getDefaultPortFromScheme(java.lang.String scheme)
Get the default port of the target server given the scheme of vip address if it is available. Subclass should override it to provider protocol specific default port number if any.

Parameters:
scheme - from the vip address. null if not present.
Returns:
80 if scheme is http, 443 if scheme is https, -1 else.

deriveHostAndPortFromVipAddress

protected com.netflix.util.Pair<java.lang.String,java.lang.Integer> deriveHostAndPortFromVipAddress(java.lang.String vipAddress)
                                                                                             throws java.net.URISyntaxException,
                                                                                                    com.netflix.client.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 getServerFromLoadBalancer(java.net.URI, Object) 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
com.netflix.client.ClientException

getServerFromLoadBalancer

protected Server getServerFromLoadBalancer(@Nullable
                                           java.net.URI original,
                                           @Nullable
                                           java.lang.Object loadBalancerKey)
                                    throws com.netflix.client.ClientException
Compute the final URI from a partial URI in the request. The following steps are performed:

Parameters:
original - Original URI passed from caller
Throws:
com.netflix.client.ClientException

reconstructURIWithServer

public java.net.URI reconstructURIWithServer(Server server,
                                             java.net.URI original)

getRetriesNextServer

protected int getRetriesNextServer(com.netflix.client.config.IClientConfig overriddenClientConfig)

getServerStats

public final ServerStats getServerStats(Server server)

getNumberRetriesOnSameServer

protected int getNumberRetriesOnSameServer(com.netflix.client.config.IClientConfig overriddenClientConfig)

handleSameServerRetry

protected boolean handleSameServerRetry(Server server,
                                        int currentRetryCount,
                                        int maxRetries,
                                        java.lang.Throwable e)

getRetryHandler

public final com.netflix.client.RetryHandler getRetryHandler()

setRetryHandler

public final void setRetryHandler(com.netflix.client.RetryHandler retryHandler)

isOkToRetryOnAllOperations

public final boolean isOkToRetryOnAllOperations()

setOkToRetryOnAllOperations

public final void setOkToRetryOnAllOperations(boolean okToRetryOnAllOperations)