com.caucho.network.balance
Class ClientSocketFactory

java.lang.Object
  extended by com.caucho.network.balance.ClientSocketFactory
All Implemented Interfaces:
ClientSocketFactoryApi

public class ClientSocketFactory
extends java.lang.Object
implements ClientSocketFactoryApi

A pool of connections to a server.

Fail Recover Time

The fail recover time is dynamic. The first timeout is 1s. After the 1s, the client tries again. If that fails, the timeout is doubled until reaching the maximum _loadBalanceRecoverTime.


Constructor Summary
ClientSocketFactory(java.lang.String address, int port)
           
ClientSocketFactory(java.lang.String address, int port, boolean isSecure)
           
ClientSocketFactory(java.lang.String sourceId, java.lang.String targetId, java.lang.String statCategory, java.lang.String statId, java.lang.String address, int port, boolean isSecure)
           
 
Method Summary
 void allocateLoadBalance()
          Allocate a connection for load balancing.
 void busy()
          Called when the server responds with "busy", e.g.
 boolean canConnect()
          Returns true if can connect to the client.
 boolean canOpen()
          Returns true if the server can open a connection.
 boolean canOpenWarm()
          Returns true if the server can open a connection.
 boolean canOpenWarmOrRecycle()
          Returns true if the server can open a connection.
 void clearRecycle()
          Clears the recycled connections, e.g.
 void close()
          Close the client
 void disable()
          Disable the client
 void enable()
          Enable the client
 void enableSessionOnly()
          Session only
 void failConnect()
          Called when the socket read/write fails.
 void failSocket()
          Called when the socket read/write fails.
 void freeLoadBalance()
          Free a connection for load balancing.
 int getActiveCount()
          Returns the number of active connections.
 java.lang.String getAddress()
          Returns the hostname of the target server.
 long getBusyCountTotal()
          Returns the count of busy connections.
 long getConnectCountTotal()
          Returns the total number of successful socket connections
 CountMeter getConnectionFailProbe()
           
 ActiveMeter getConnectionProbe()
           
 double getCpuLoadAvg()
          Gets the CPU load avg
 java.lang.String getDebugId()
          Returns the debug id.
 long getFailCountTotal()
          Returns the total number of failed connect attempts.
 java.lang.String getId()
          Returns the user-readable id of the target server.
 int getIdleCount()
          Returns the number of idle connections.
 ActiveMeter getIdleProbe()
           
 long getKeepaliveCountTotal()
          Returns the number of times a keepalive connection has been used.
 java.util.Date getLastBusyTime()
          Returns the time of the last busy.
 java.util.Date getLastFailConnectTime()
          Returns the time of the last failure.
 java.util.Date getLastFailTime()
          Returns the time of the last failure.
 long getLastSuccessTime()
          Returns the time of the last failure.
 double getLatencyFactor()
          Returns the latency factory
 int getLoadBalanceAllocateCount()
          Returns the number of load balance allocations
 long getLoadBalanceConnectionMin()
          The minimum connections for green load balancing.
 long getLoadBalanceConnectTimeout()
          The socket timeout when connecting to the target server.
 long getLoadBalanceIdleTime()
          How long the connection can be cached in the free pool.
 long getLoadBalanceRecoverTime()
           
 long getLoadBalanceSocketTimeout()
          The socket timeout when reading from the target server.
 int getLoadBalanceWeight()
          The load balance weight.
 int getPort()
          Gets the port of the target server.
 CountMeter getRequestBusyProbe()
           
 CountMeter getRequestFailProbe()
           
 ActiveTimeMeter getRequestTimeProbe()
           
 int getStartSequenceId()
          Returns the server start/stop sequence.
 java.lang.String getState()
          Returns the lifecycle state.
 void init()
          Initialize
 boolean isActive()
          Returns true if the server is active.
 boolean isDead()
          Returns true if the server is dead.
 boolean isEnabled()
          Return true if active.
 boolean isHeartbeatActive()
          Returns true if the target server's heartbeat is active.
 void notifyHeartbeatStart()
          Notify that a heartbeat start has occurred.
 void notifyHeartbeatStop()
          Notify that a heartbeat stop has occurred.
 ClientSocket open()
          Open a stream to the target server for the load balancer.
 ClientSocket openIfHeartbeatActive()
          Open a stream if the target server's heartbeat is active.
 ClientSocket openIfLive()
          Open a stream to the target server object persistence.
 ClientSocket openSticky()
          Open a stream to the target server for a session.
 ClientSocket openWarm()
          Open a stream to the target server, restricted by warmup.
 void setCpuLoadAvg(double load)
          Sets the CPU load avg (from backend).
 void setHeartbeatServer(boolean isHeartbeatServer)
          Foreign server is in the pod's heartbeat range.
 void setLoadBalanceConnectionMin(int connectionMin)
          The minimum connections for green load balancing.
 void setLoadBalanceConnectTimeout(long timeout)
          The socket timeout when connecting to the target server.
 void setLoadBalanceIdleTime(long timeout)
          How long the connection can be cached in the free pool.
 void setLoadBalanceRecoverTime(long timeout)
          Returns how long the connection will be treated as dead.
 void setLoadBalanceSocketTimeout(long timeout)
          The socket timeout when reading from the target server.
 void setLoadBalanceWarmupTime(long timeout)
          Returns the time in milliseconds for the slow start throttling.
 void setLoadBalanceWeight(int weight)
          The load balance weight.
 void start()
          Enable the client.
 void stop()
          Disable the client.
 void success()
          Called when the server has a successful response
 void toBusy()
           
 void toFail()
           
 java.lang.String toString()
           
 void wake()
          We now know that the server is live, e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClientSocketFactory

public ClientSocketFactory(java.lang.String address,
                           int port)

ClientSocketFactory

public ClientSocketFactory(java.lang.String address,
                           int port,
                           boolean isSecure)

ClientSocketFactory

public ClientSocketFactory(java.lang.String sourceId,
                           java.lang.String targetId,
                           java.lang.String statCategory,
                           java.lang.String statId,
                           java.lang.String address,
                           int port,
                           boolean isSecure)
Method Detail

getId

public java.lang.String getId()
Returns the user-readable id of the target server.

Specified by:
getId in interface ClientSocketFactoryApi

getDebugId

public java.lang.String getDebugId()
Returns the debug id.

Specified by:
getDebugId in interface ClientSocketFactoryApi

getAddress

public java.lang.String getAddress()
Returns the hostname of the target server.

Specified by:
getAddress in interface ClientSocketFactoryApi

getPort

public int getPort()
Gets the port of the target server.

Specified by:
getPort in interface ClientSocketFactoryApi

setHeartbeatServer

public void setHeartbeatServer(boolean isHeartbeatServer)
Foreign server is in the pod's heartbeat range.


getLoadBalanceConnectTimeout

public long getLoadBalanceConnectTimeout()
The socket timeout when connecting to the target server.


setLoadBalanceConnectTimeout

public void setLoadBalanceConnectTimeout(long timeout)
The socket timeout when connecting to the target server.


getLoadBalanceConnectionMin

public long getLoadBalanceConnectionMin()
The minimum connections for green load balancing.


setLoadBalanceConnectionMin

public void setLoadBalanceConnectionMin(int connectionMin)
The minimum connections for green load balancing.


getLoadBalanceSocketTimeout

public long getLoadBalanceSocketTimeout()
The socket timeout when reading from the target server.


setLoadBalanceSocketTimeout

public void setLoadBalanceSocketTimeout(long timeout)
The socket timeout when reading from the target server.


getLoadBalanceIdleTime

public long getLoadBalanceIdleTime()
How long the connection can be cached in the free pool.


setLoadBalanceIdleTime

public void setLoadBalanceIdleTime(long timeout)
How long the connection can be cached in the free pool.


setLoadBalanceRecoverTime

public void setLoadBalanceRecoverTime(long timeout)
Returns how long the connection will be treated as dead.


getLoadBalanceRecoverTime

public long getLoadBalanceRecoverTime()

setLoadBalanceWarmupTime

public void setLoadBalanceWarmupTime(long timeout)
Returns the time in milliseconds for the slow start throttling.


getLoadBalanceWeight

public int getLoadBalanceWeight()
The load balance weight.


setLoadBalanceWeight

public void setLoadBalanceWeight(int weight)
The load balance weight.


getStartSequenceId

public int getStartSequenceId()
Returns the server start/stop sequence. Each enable/disable increments the sequence, allowing old streams to be purge.


init

public void init()
Initialize


getActiveCount

public int getActiveCount()
Returns the number of active connections.


getIdleCount

public int getIdleCount()
Returns the number of idle connections.


getLoadBalanceAllocateCount

public int getLoadBalanceAllocateCount()
Returns the number of load balance allocations


allocateLoadBalance

public void allocateLoadBalance()
Allocate a connection for load balancing.


freeLoadBalance

public void freeLoadBalance()
Free a connection for load balancing.


getConnectCountTotal

public long getConnectCountTotal()
Returns the total number of successful socket connections


getKeepaliveCountTotal

public long getKeepaliveCountTotal()
Returns the number of times a keepalive connection has been used.


getFailCountTotal

public long getFailCountTotal()
Returns the total number of failed connect attempts.


getLastFailTime

public java.util.Date getLastFailTime()
Returns the time of the last failure.


getLastFailConnectTime

public java.util.Date getLastFailConnectTime()
Returns the time of the last failure.


getLastSuccessTime

public long getLastSuccessTime()
Returns the time of the last failure.


getLatencyFactor

public double getLatencyFactor()
Returns the latency factory


getBusyCountTotal

public long getBusyCountTotal()
Returns the count of busy connections.


getLastBusyTime

public java.util.Date getLastBusyTime()
Returns the time of the last busy.


setCpuLoadAvg

public void setCpuLoadAvg(double load)
Sets the CPU load avg (from backend).


getCpuLoadAvg

public double getCpuLoadAvg()
Gets the CPU load avg


isActive

public final boolean isActive()
Returns true if the server is active.

Specified by:
isActive in interface ClientSocketFactoryApi

isHeartbeatActive

public final boolean isHeartbeatActive()
Returns true if the target server's heartbeat is active.


isDead

public boolean isDead()
Returns true if the server is dead.

Specified by:
isDead in interface ClientSocketFactoryApi

enable

public void enable()
Enable the client

Specified by:
enable in interface ClientSocketFactoryApi

disable

public void disable()
Disable the client

Specified by:
disable in interface ClientSocketFactoryApi

getState

public java.lang.String getState()
Returns the lifecycle state.

Specified by:
getState in interface ClientSocketFactoryApi

canOpen

public boolean canOpen()
Returns true if the server can open a connection.


canOpenWarmOrRecycle

public boolean canOpenWarmOrRecycle()
Returns true if the server can open a connection.

Specified by:
canOpenWarmOrRecycle in interface ClientSocketFactoryApi

canOpenWarm

public boolean canOpenWarm()
Returns true if the server can open a connection.

Specified by:
canOpenWarm in interface ClientSocketFactoryApi

isEnabled

public boolean isEnabled()
Return true if active.

Specified by:
isEnabled in interface ClientSocketFactoryApi

toBusy

public void toBusy()
Specified by:
toBusy in interface ClientSocketFactoryApi

toFail

public void toFail()
Specified by:
toFail in interface ClientSocketFactoryApi

failSocket

public void failSocket()
Called when the socket read/write fails.

Specified by:
failSocket in interface ClientSocketFactoryApi

failConnect

public void failConnect()
Called when the socket read/write fails.

Specified by:
failConnect in interface ClientSocketFactoryApi

busy

public void busy()
Called when the server responds with "busy", e.g. HTTP 503

Specified by:
busy in interface ClientSocketFactoryApi

success

public void success()
Called when the server has a successful response

Specified by:
success in interface ClientSocketFactoryApi

start

public void start()
Enable the client.

Specified by:
start in interface ClientSocketFactoryApi

stop

public void stop()
Disable the client.

Specified by:
stop in interface ClientSocketFactoryApi

enableSessionOnly

public void enableSessionOnly()
Session only

Specified by:
enableSessionOnly in interface ClientSocketFactoryApi

openWarm

public ClientSocket openWarm()
Open a stream to the target server, restricted by warmup.

Specified by:
openWarm in interface ClientSocketFactoryApi
Returns:
the socket's read/write pair.

openIfLive

public ClientSocket openIfLive()
Open a stream to the target server object persistence.

Specified by:
openIfLive in interface ClientSocketFactoryApi
Returns:
the socket's read/write pair.

openIfHeartbeatActive

public ClientSocket openIfHeartbeatActive()
Open a stream if the target server's heartbeat is active.

Returns:
the socket's read/write pair.

openSticky

public ClientSocket openSticky()
Open a stream to the target server for a session.

Specified by:
openSticky in interface ClientSocketFactoryApi
Returns:
the socket's read/write pair.

open

public ClientSocket open()
Open a stream to the target server for the load balancer.

Specified by:
open in interface ClientSocketFactoryApi
Returns:
the socket's read/write pair.

wake

public void wake()
We now know that the server is live, e.g. if a sibling has contacted us.

Specified by:
wake in interface ClientSocketFactoryApi

notifyHeartbeatStart

public void notifyHeartbeatStart()
Notify that a heartbeat start has occurred.

Specified by:
notifyHeartbeatStart in interface ClientSocketFactoryApi

notifyHeartbeatStop

public void notifyHeartbeatStop()
Notify that a heartbeat stop has occurred.

Specified by:
notifyHeartbeatStop in interface ClientSocketFactoryApi

clearRecycle

public void clearRecycle()
Clears the recycled connections, e.g. on detection of backend server going down.

Specified by:
clearRecycle in interface ClientSocketFactoryApi

close

public void close()
Close the client

Specified by:
close in interface ClientSocketFactoryApi

canConnect

public boolean canConnect()
Returns true if can connect to the client.


getConnectionProbe

public ActiveMeter getConnectionProbe()

getConnectionFailProbe

public CountMeter getConnectionFailProbe()

getRequestTimeProbe

public ActiveTimeMeter getRequestTimeProbe()

getRequestFailProbe

public CountMeter getRequestFailProbe()

getRequestBusyProbe

public CountMeter getRequestBusyProbe()

getIdleProbe

public ActiveMeter getIdleProbe()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object