org.apache.catalina.ha.session

Class JvmRouteBinderValve

public class JvmRouteBinderValve extends ValveBase implements ClusterValve, Lifecycle

Valve to handle Tomcat jvmRoute takeover using mod_jk module after node failure. After a node crashes, subsequent requests go to other cluster nodes. That incurs a drop in performance. When this Valve is enabled on a backup node and sees a request, which was intended for another (thus failed) node, it will rewrite the cookie jsessionid information to use the route to this backup cluster node, that answered the request. After the response is delivered to the client, all subsequent client requests will go directly to the backup node. The change of sessionid is also sent to all other cluster nodes. After all that, the session stickyness will work directly to the backup node and the traffic will not go back to the failed node after it is restarted!

For this valve to function correctly, so that all nodes of the cluster receive the sessionid change notifications that it generates, the following ClusterListener MUST be configured at all nodes of the cluster: JvmRouteSessionIDBinderListener since Tomcat 5.5.10, and both JvmRouteSessionIDBinderListener and JvmRouteSessionIDBinderLifecycleListener for earlier versions of Tomcat.

Add this Valve to your host definition at conf/server.xml . Since 5.5.10 as direct cluster valve:

  <Cluster>
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />  
  </Cluster>
 

Before 5.5.10 as Host element:
  <Host>
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />  
  </Host>
 
A Trick:
You can enable this mod_jk turnover mode via JMX before you drop a node to all backup nodes! Set enable true on all JvmRouteBinderValve backups, disable worker at mod_jk and then drop node and restart it! Then enable mod_jk worker and disable JvmRouteBinderValves again. This use case means that only requested sessions are migrated.

Version: $Revision: 892545 $ $Date: 2009-12-20 02:04:17 +0100 (Sun, 20 Dec 2009) $

Author: Peter Rossbach

Field Summary
protected CatalinaClustercluster
the cluster
protected booleanenabled
enabled this component
protected static Stringinfo
The descriptive information about this implementation.
protected LifecycleSupportlifecycle
The lifecycle event support for this component.
static Loglog
protected longnumberOfSessions
number of session that no at this tomcat instanz hosted
protected StringsessionIdAttribute
protected StringManagersm
The string manager for this package.
protected booleanstarted
Has this component been started yet?
Method Summary
voidaddLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.
protected voidchangeRequestSessionID(Request request, Response response, String sessionId, String newSessionID)
Change Request Session id
protected voidchangeSessionID(Request request, Response response, String sessionId, String newSessionID, Session catalinaSession)
change session id and send to all cluster nodes
LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle.
CatalinaClustergetCluster()
booleangetEnabled()
StringgetInfo()
Return descriptive information about this implementation.
protected StringgetLocalJvmRoute(Request request)
get jvmroute from engine
protected ManagergetManager(Request request)
get Cluster DeltaManager
longgetNumberOfSessions()
StringgetSessionIdAttribute()
set session id attribute to failed node for request.
protected voidhandleJvmRoute(Request request, Response response, String sessionId, String localJvmRoute)
Handle jvmRoute stickyness after tomcat instance failed.
protected voidhandlePossibleTurnover(Request request, Response response)
handle possible session turn over.
voidinvoke(Request request, Response response)
Detect possible the JVMRoute change at cluster backup node..
voidremoveLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.
protected voidsendSessionIDClusterBackup(ClusterManager manager, Request request, String sessionId, String newSessionID)
Send the changed Sessionid to all clusternodes.
voidsetCluster(CatalinaCluster cluster)
voidsetEnabled(boolean enabled)
protected voidsetNewSessionCookie(Request request, Response response, String sessionId)
Sets a new cookie for the given session id and response and see configureSessionCookie
voidsetSessionIdAttribute(String sessionIdAttribute)
get name of failed reqeust session attribute
voidstart()
Prepare for the beginning of active use of the public methods of this component.
voidstop()
Gracefully terminate the active use of the public methods of this component.

Field Detail

cluster

protected CatalinaCluster cluster
the cluster

enabled

protected boolean enabled
enabled this component

info

protected static final String info
The descriptive information about this implementation.

lifecycle

protected LifecycleSupport lifecycle
The lifecycle event support for this component.

log

public static Log log

numberOfSessions

protected long numberOfSessions
number of session that no at this tomcat instanz hosted

sessionIdAttribute

protected String sessionIdAttribute

sm

protected StringManager sm
The string manager for this package.

started

protected boolean started
Has this component been started yet?

Method Detail

addLifecycleListener

public void addLifecycleListener(LifecycleListener listener)
Add a lifecycle event listener to this component.

Parameters: listener The listener to add

changeRequestSessionID

protected void changeRequestSessionID(Request request, Response response, String sessionId, String newSessionID)
Change Request Session id

Parameters: request current request response current response sessionId original session id newSessionID new session id for node migration

changeSessionID

protected void changeSessionID(Request request, Response response, String sessionId, String newSessionID, Session catalinaSession)
change session id and send to all cluster nodes

Parameters: request current request response current response sessionId original session id newSessionID new session id for node migration catalinaSession current session with original session id

findLifecycleListeners

public LifecycleListener[] findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.

getCluster

public CatalinaCluster getCluster()

Returns: Returns the cluster.

getEnabled

public boolean getEnabled()

Returns: Returns the enabled.

getInfo

public String getInfo()
Return descriptive information about this implementation.

getLocalJvmRoute

protected String getLocalJvmRoute(Request request)
get jvmroute from engine

Parameters: request current request

Returns: return jvmRoute from ManagerBase or null

getManager

protected Manager getManager(Request request)
get Cluster DeltaManager

Parameters: request current request

Returns: manager or null

getNumberOfSessions

public long getNumberOfSessions()

Returns: Returns the number of migrated sessions.

getSessionIdAttribute

public String getSessionIdAttribute()
set session id attribute to failed node for request.

Returns: Returns the sessionIdAttribute.

handleJvmRoute

protected void handleJvmRoute(Request request, Response response, String sessionId, String localJvmRoute)
Handle jvmRoute stickyness after tomcat instance failed. After this correction a new Cookie send to client with new jvmRoute and the SessionID change propage to the other cluster nodes.

Parameters: request current request response Tomcat Response sessionId request SessionID from Cookie localJvmRoute local jvmRoute

handlePossibleTurnover

protected void handlePossibleTurnover(Request request, Response response)
handle possible session turn over.

Parameters: request current request response current response

See Also: JvmRouteBinderValve

invoke

public void invoke(Request request, Response response)
Detect possible the JVMRoute change at cluster backup node..

Parameters: request tomcat request being processed response tomcat response being processed

Throws: IOException if an input/output error has occurred ServletException if a servlet error has occurred

removeLifecycleListener

public void removeLifecycleListener(LifecycleListener listener)
Remove a lifecycle event listener from this component.

Parameters: listener The listener to add

sendSessionIDClusterBackup

protected void sendSessionIDClusterBackup(ClusterManager manager, Request request, String sessionId, String newSessionID)
Send the changed Sessionid to all clusternodes.

Parameters: manager ClusterManager sessionId current failed sessionid newSessionID new session id, bind to the new cluster node

See Also: messageReceived

setCluster

public void setCluster(CatalinaCluster cluster)

Parameters: cluster The cluster to set.

setEnabled

public void setEnabled(boolean enabled)

Parameters: enabled The enabled to set.

setNewSessionCookie

protected void setNewSessionCookie(Request request, Response response, String sessionId)

Deprecated: Use changeSessionId

Sets a new cookie for the given session id and response and see configureSessionCookie

Parameters: request current request response Tomcat Response sessionId The session id

setSessionIdAttribute

public void setSessionIdAttribute(String sessionIdAttribute)
get name of failed reqeust session attribute

Parameters: sessionIdAttribute The sessionIdAttribute to set.

start

public void start()
Prepare for the beginning of active use of the public methods of this component. This method should be called after configure(), and before any of the public methods of the component are utilized.

Throws: LifecycleException if this component detects a fatal error that prevents this component from being used

stop

public void stop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component.

Throws: LifecycleException if this component detects a fatal error that needs to be reported

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.