Class JvmRouteBinderValve
java.lang.Object
org.apache.catalina.util.LifecycleBase
org.apache.catalina.util.LifecycleMBeanBase
org.apache.catalina.valves.ValveBase
org.apache.catalina.ha.session.JvmRouteBinderValve
- All Implemented Interfaces:
MBeanRegistration, Contained, ClusterValve, JmxEnabled, Lifecycle, Valve
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 stickiness will work directly to the backup node and the
traffic will not go back to the failed node after it is restarted!
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.
Add this Valve to your cluster definition at conf/server.xml .
<Cluster> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" /> </Cluster>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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface Lifecycle
Lifecycle.SingleUse -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CatalinaClusterthe clusterprotected booleanenabled this componentstatic final LogLogger for this valve.protected longnumber of session that no at this tomcat instance hostedprotected StringThe session ID attribute name.protected static final StringManagerThe string manager for this package.Fields inherited from class ValveBase
asyncSupported, container, containerLog, nextFields inherited from interface Lifecycle
AFTER_DESTROY_EVENT, AFTER_INIT_EVENT, AFTER_START_EVENT, AFTER_STOP_EVENT, BEFORE_DESTROY_EVENT, BEFORE_INIT_EVENT, BEFORE_START_EVENT, BEFORE_STOP_EVENT, CONFIGURE_START_EVENT, CONFIGURE_STOP_EVENT, PERIODIC_EVENT, START_EVENT, STOP_EVENT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidchangeRequestSessionID(Request request, String sessionId, String newSessionID) Change Request Session idprotected voidchangeSessionAuthenticationNote(String sessionId, String newSessionID, Session catalinaSession) Change the current session ID that is stored in a session note during authentication.protected voidchangeSessionID(Request request, String sessionId, String newSessionID, Session catalinaSession) change session id and send to all cluster nodesReturns the cluster the valve is associated withbooleanReturns whether this valve is enabled.protected StringgetLocalJvmRoute(Request request) get jvmroute from engineprotected ManagergetManager(Request request) get ClusterManagerlongReturns the number of migrated sessions.set session id attribute to failed node for request.protected voidhandleJvmRoute(Request request, String sessionId, String localJvmRoute) Handle jvmRoute stickiness after tomcat instance failed.protected voidhandlePossibleTurnover(Request request) handle possible session turn over.voidDetect possible the JVMRoute change at cluster backup node.voidsetCluster(CatalinaCluster cluster) Associates the cluster valve with a clustervoidsetEnabled(boolean enabled) Sets whether this valve is enabled.voidsetSessionIdAttribute(String sessionIdAttribute) get name of failed request session attributeprotected voidStart this component and implement the requirements ofLifecycleBase.startInternal().protected voidStop this component and implement the requirements ofLifecycleBase.stopInternal().Methods inherited from class ValveBase
backgroundProcess, getContainer, getDomainInternal, getNext, getObjectNameKeyProperties, initInternal, isAsyncSupported, setAsyncSupported, setContainer, setNext, toStringMethods inherited from class LifecycleMBeanBase
destroyInternal, getDomain, getObjectName, postDeregister, postRegister, preDeregister, preRegister, register, setDomain, unregister, unregisterMethods inherited from class LifecycleBase
addLifecycleListener, destroy, findLifecycleListeners, fireLifecycleEvent, getState, getStateName, getThrowOnFailure, init, removeLifecycleListener, setState, setState, setThrowOnFailure, start, stopMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Valve
backgroundProcess, getNext, isAsyncSupported, setNext
-
Field Details
-
log
Logger for this valve. -
cluster
the cluster -
sm
The string manager for this package. -
enabled
protected boolean enabledenabled this component -
numberOfSessions
protected long numberOfSessionsnumber of session that no at this tomcat instance hosted -
sessionIdAttribute
The session ID attribute name.
-
-
Constructor Details
-
JvmRouteBinderValve
public JvmRouteBinderValve()Default constructor.
-
-
Method Details
-
getSessionIdAttribute
set session id attribute to failed node for request.- Returns:
- Returns the sessionIdAttribute.
-
setSessionIdAttribute
get name of failed request session attribute- Parameters:
sessionIdAttribute- The sessionIdAttribute to set.
-
getNumberOfSessions
public long getNumberOfSessions()Returns the number of migrated sessions.- Returns:
- the number of migrated sessions
-
getEnabled
public boolean getEnabled()Returns whether this valve is enabled.- Returns:
trueif this valve is enabled
-
setEnabled
public void setEnabled(boolean enabled) Sets whether this valve is enabled.- Parameters:
enabled-trueto enable this valve
-
invoke
Detect possible the JVMRoute change at cluster backup node.- Specified by:
invokein interfaceValve- Parameters:
request- tomcat request being processedresponse- tomcat response being processed- Throws:
IOException- if an input/output error has occurredServletException- if a servlet error has occurred
-
handlePossibleTurnover
handle possible session turn over.- Parameters:
request- current request- See Also:
-
getLocalJvmRoute
-
getManager
-
getCluster
Description copied from interface:ClusterValveReturns the cluster the valve is associated with- Specified by:
getClusterin interfaceClusterValve- Returns:
- CatalinaCluster
-
setCluster
Description copied from interface:ClusterValveAssociates the cluster valve with a cluster- Specified by:
setClusterin interfaceClusterValve- Parameters:
cluster- CatalinaCluster
-
handleJvmRoute
Handle jvmRoute stickiness after tomcat instance failed. After this correction a new Cookie send to client with new jvmRoute and the SessionID change propagate to the other cluster nodes.- Parameters:
request- current requestsessionId- request SessionID from CookielocalJvmRoute- local jvmRoute
-
changeSessionID
protected void changeSessionID(Request request, String sessionId, String newSessionID, Session catalinaSession) change session id and send to all cluster nodes- Parameters:
request- current requestsessionId- original session idnewSessionID- new session id for node migrationcatalinaSession- current session with original session id
-
changeRequestSessionID
-
changeSessionAuthenticationNote
protected void changeSessionAuthenticationNote(String sessionId, String newSessionID, Session catalinaSession) Change the current session ID that is stored in a session note during authentication. It is part of the CSRF protection.- Parameters:
sessionId- The original session IDnewSessionID- The new session ID for node migrationcatalinaSession- The session object (that will be using the new session ID at the point this method is called)
-
startInternal
Start this component and implement the requirements ofLifecycleBase.startInternal().- Overrides:
startInternalin classValveBase- Throws:
LifecycleException- if this component detects a fatal error that prevents this component from being used
-
stopInternal
Stop this component and implement the requirements ofLifecycleBase.stopInternal().- Overrides:
stopInternalin classValveBase- Throws:
LifecycleException- if this component detects a fatal error that prevents this component from being used
-