org.apache.catalina.ha.ClusterMessage Java Examples

The following examples show how to use org.apache.catalina.ha.ClusterMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleTcpCluster.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * send a cluster message to one member
 * 
 * @param msg message to transfer
 * @param dest Receiver member
 * @see org.apache.catalina.ha.CatalinaCluster#send(org.apache.catalina.ha.ClusterMessage,
 *      org.apache.catalina.tribes.Member)
 */
@Override
public void send(ClusterMessage msg, Member dest) {
    try {
        msg.setAddress(getLocalMember());
        int sendOptions = channelSendOptions;
        if (msg instanceof SessionMessage
                && ((SessionMessage)msg).getEventType() == SessionMessage.EVT_ALL_SESSION_DATA) {
            sendOptions = Channel.SEND_OPTIONS_SYNCHRONIZED_ACK|Channel.SEND_OPTIONS_USE_ACK;
        }
        if (dest != null) {
            if (!getLocalMember().equals(dest)) {
                channel.send(new Member[] {dest}, msg, sendOptions);
            } else
                log.error("Unable to send message to local member " + msg);
        } else {
            Member[] destmembers = channel.getMembers();
            if (destmembers.length>0)
                channel.send(destmembers,msg, sendOptions);
            else if (log.isDebugEnabled()) 
                log.debug("No members in cluster, ignoring message:"+msg);
        }
    } catch (Exception x) {
        log.error("Unable to send message through cluster sender.", x);
    }
}
 
Example #2
Source File: SimpleTcpCluster.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * send a cluster message to one member
 * 
 * @param msg message to transfer
 * @param dest Receiver member
 * @see org.apache.catalina.ha.CatalinaCluster#send(org.apache.catalina.ha.ClusterMessage,
 *      org.apache.catalina.tribes.Member)
 */
@Override
public void send(ClusterMessage msg, Member dest) {
    try {
        msg.setAddress(getLocalMember());
        int sendOptions = channelSendOptions;
        if (msg instanceof SessionMessage
                && ((SessionMessage)msg).getEventType() == SessionMessage.EVT_ALL_SESSION_DATA) {
            sendOptions = Channel.SEND_OPTIONS_SYNCHRONIZED_ACK|Channel.SEND_OPTIONS_USE_ACK;
        }
        if (dest != null) {
            if (!getLocalMember().equals(dest)) {
                channel.send(new Member[] {dest}, msg, sendOptions);
            } else
                log.error("Unable to send message to local member " + msg);
        } else {
            Member[] destmembers = channel.getMembers();
            if (destmembers.length>0)
                channel.send(destmembers,msg, sendOptions);
            else if (log.isDebugEnabled()) 
                log.debug("No members in cluster, ignoring message:"+msg);
        }
    } catch (Exception x) {
        log.error("Unable to send message through cluster sender.", x);
    }
}
 
Example #3
Source File: SimpleTcpCluster.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * send a cluster message to one member
 *
 * @param msg message to transfer
 * @param dest Receiver member
 * @see org.apache.catalina.ha.CatalinaCluster#send(org.apache.catalina.ha.ClusterMessage,
 *      org.apache.catalina.tribes.Member)
 */
@Override
public void send(ClusterMessage msg, Member dest) {
    try {
        msg.setAddress(getLocalMember());
        int sendOptions = channelSendOptions;
        if (msg instanceof SessionMessage
                && ((SessionMessage)msg).getEventType() == SessionMessage.EVT_ALL_SESSION_DATA) {
            sendOptions = Channel.SEND_OPTIONS_SYNCHRONIZED_ACK|Channel.SEND_OPTIONS_USE_ACK;
        }
        if (dest != null) {
            if (!getLocalMember().equals(dest)) {
                channel.send(new Member[] {dest}, msg, sendOptions);
            } else
                log.error(sm.getString("simpleTcpCluster.unableSend.localMember", msg));
        } else {
            Member[] destmembers = channel.getMembers();
            if (destmembers.length>0)
                channel.send(destmembers,msg, sendOptions);
            else if (log.isDebugEnabled())
                log.debug("No members in cluster, ignoring message:"+msg);
        }
    } catch (Exception x) {
        log.error(sm.getString("simpleTcpCluster.sendFailed"), x);
    }
}
 
Example #4
Source File: ReplicationValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * send manager requestCompleted message to cluster
 * @param manager SessionManager
 * @param sessionId sessionid from the manager
 * @see DeltaManager#requestCompleted(String)
 * @see SimpleTcpCluster#send(ClusterMessage)
 */
protected void send(ClusterManager manager, String sessionId) {
    ClusterMessage msg = manager.requestCompleted(sessionId);
    if (msg != null && cluster != null) {
        cluster.send(msg);
        if(doStatistics()) {
            nrOfSendRequests++;
        }
    }
}
 
Example #5
Source File: DeltaManager.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * A message was received from another node, this is the callback method to
 * implement if you are interested in receiving replication messages.
 * 
 * @param cmsg -
 *            the message received.
 */
@Override
public void messageDataReceived(ClusterMessage cmsg) {
    if (cmsg != null && cmsg instanceof SessionMessage) {
        SessionMessage msg = (SessionMessage) cmsg;
        switch (msg.getEventType()) {
            case SessionMessage.EVT_GET_ALL_SESSIONS:
            case SessionMessage.EVT_SESSION_CREATED: 
            case SessionMessage.EVT_SESSION_EXPIRED: 
            case SessionMessage.EVT_SESSION_ACCESSED:
            case SessionMessage.EVT_SESSION_DELTA:
            case SessionMessage.EVT_CHANGE_SESSION_ID: {
                synchronized(receivedMessageQueue) {
                    if(receiverQueue) {
                        receivedMessageQueue.add(msg);
                        return ;
                    }
                }
               break;
            }
            default: {
                //we didn't queue, do nothing
                break;
            }
        } //switch
        
        messageReceived(msg, msg.getAddress() != null ? (Member) msg.getAddress() : null);
    }
}
 
Example #6
Source File: BackupManager.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterMessage requestCompleted(String sessionId) {
    if (!getState().isAvailable()) return null;
    LazyReplicatedMap<String,Session> map =
            (LazyReplicatedMap<String,Session>)sessions;
    map.replicate(sessionId,false);
    return null;
}
 
Example #7
Source File: BackupManager.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterMessage requestCompleted(String sessionId) {
    if (!getState().isAvailable()) return null;
    LazyReplicatedMap<String,Session> map =
            (LazyReplicatedMap<String,Session>)sessions;
    map.replicate(sessionId,false);
    return null;
}
 
Example #8
Source File: DeltaManager.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * A message was received from another node, this is the callback method to
 * implement if you are interested in receiving replication messages.
 * 
 * @param cmsg -
 *            the message received.
 */
@Override
public void messageDataReceived(ClusterMessage cmsg) {
    if (cmsg != null && cmsg instanceof SessionMessage) {
        SessionMessage msg = (SessionMessage) cmsg;
        switch (msg.getEventType()) {
            case SessionMessage.EVT_GET_ALL_SESSIONS:
            case SessionMessage.EVT_SESSION_CREATED: 
            case SessionMessage.EVT_SESSION_EXPIRED: 
            case SessionMessage.EVT_SESSION_ACCESSED:
            case SessionMessage.EVT_SESSION_DELTA:
            case SessionMessage.EVT_CHANGE_SESSION_ID: {
                synchronized(receivedMessageQueue) {
                    if(receiverQueue) {
                        receivedMessageQueue.add(msg);
                        return ;
                    }
                }
               break;
            }
            default: {
                //we didn't queue, do nothing
                break;
            }
        } //switch
        
        messageReceived(msg, msg.getAddress() != null ? (Member) msg.getAddress() : null);
    }
}
 
Example #9
Source File: SimpleTcpCluster.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void messageReceived(ClusterMessage message) {

        if (log.isDebugEnabled() && message != null)
            log.debug("Assuming clocks are synched: Replication for "
                    + message.getUniqueId() + " took="
                    + (System.currentTimeMillis() - (message).getTimestamp())
                    + " ms.");

        //invoke all the listeners
        boolean accepted = false;
        if (message != null) {
            for (Iterator<ClusterListener> iter = clusterListeners.iterator();
                    iter.hasNext();) {
                ClusterListener listener = iter.next();
                if (listener.accept(message)) {
                    accepted = true;
                    listener.messageReceived(message);
                }
            }
            if (!accepted && notifyLifecycleListenerOnFailure) {
                Member dest = message.getAddress();
                // Notify our interested LifecycleListeners
                fireLifecycleEvent(RECEIVE_MESSAGE_FAILURE_EVENT,
                        new SendMessageData(message, dest, null));
                if (log.isDebugEnabled()) {
                    log.debug("Message " + message.toString() + " from type "
                            + message.getClass().getName()
                            + " transferred but no listener registered");
                }
            }
        }
    }
 
Example #10
Source File: SimpleTcpCluster.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void messageReceived(ClusterMessage message) {

        if (log.isDebugEnabled() && message != null)
            log.debug("Assuming clocks are synched: Replication for "
                    + message.getUniqueId() + " took="
                    + (System.currentTimeMillis() - (message).getTimestamp())
                    + " ms.");

        //invoke all the listeners
        boolean accepted = false;
        if (message != null) {
            for (Iterator<ClusterListener> iter = clusterListeners.iterator();
                    iter.hasNext();) {
                ClusterListener listener = iter.next();
                if (listener.accept(message)) {
                    accepted = true;
                    listener.messageReceived(message);
                }
            }
            if (!accepted && notifyLifecycleListenerOnFailure) {
                Member dest = message.getAddress();
                // Notify our interested LifecycleListeners
                fireLifecycleEvent(RECEIVE_MESSAGE_FAILURE_EVENT,
                        new SendMessageData(message, dest, null));
                if (log.isDebugEnabled()) {
                    log.debug("Message " + message.toString() + " from type "
                            + message.getClass().getName()
                            + " transfered but no listener registered");
                }
            }
        }
        return;
    }
 
Example #11
Source File: ReplicationValve.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * send manager requestCompleted message to cluster
 * @param manager SessionManager
 * @param cluster replication cluster
 * @param sessionId sessionid from the manager
 * @see DeltaManager#requestCompleted(String)
 * @see SimpleTcpCluster#send(ClusterMessage)
 */
protected void send(ClusterManager manager, CatalinaCluster cluster, String sessionId) {
    ClusterMessage msg = manager.requestCompleted(sessionId);
    if (msg != null) {
        cluster.send(msg);
        if(doStatistics())
            nrOfSendRequests++;
    }
}
 
Example #12
Source File: SimpleTcpCluster.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void messageReceived(ClusterMessage message) {

        if (log.isDebugEnabled() && message != null)
            log.debug("Assuming clocks are synched: Replication for "
                    + message.getUniqueId() + " took="
                    + (System.currentTimeMillis() - (message).getTimestamp())
                    + " ms.");

        //invoke all the listeners
        boolean accepted = false;
        if (message != null) {
            for (Iterator<ClusterListener> iter = clusterListeners.iterator();
                    iter.hasNext();) {
                ClusterListener listener = iter.next();
                if (listener.accept(message)) {
                    accepted = true;
                    listener.messageReceived(message);
                }
            }
            if (!accepted && notifyLifecycleListenerOnFailure) {
                Member dest = message.getAddress();
                // Notify our interested LifecycleListeners
                fireLifecycleEvent(RECEIVE_MESSAGE_FAILURE_EVENT,
                        new SendMessageData(message, dest, null));
                if (log.isDebugEnabled()) {
                    log.debug("Message " + message.toString() + " from type "
                            + message.getClass().getName()
                            + " transfered but no listener registered");
                }
            }
        }
        return;
    }
 
Example #13
Source File: ClusterObserver.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void send(final ClusterMessage message, final AppInfo app) {
    for (final CatalinaCluster cluster : clusters) {
        final String path = app.path;
        if (new File(path).exists() && !app.autoDeploy) {
            cluster.send(message);
        }
    }
}
 
Example #14
Source File: DeltaManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * A message was received from another node, this is the callback method to
 * implement if you are interested in receiving replication messages.
 *
 * @param cmsg -
 *            the message received.
 */
@Override
public void messageDataReceived(ClusterMessage cmsg) {
    if (cmsg instanceof SessionMessage) {
        SessionMessage msg = (SessionMessage) cmsg;
        switch (msg.getEventType()) {
            case SessionMessage.EVT_GET_ALL_SESSIONS:
            case SessionMessage.EVT_SESSION_CREATED:
            case SessionMessage.EVT_SESSION_EXPIRED:
            case SessionMessage.EVT_SESSION_ACCESSED:
            case SessionMessage.EVT_SESSION_DELTA:
            case SessionMessage.EVT_CHANGE_SESSION_ID:
                synchronized(receivedMessageQueue) {
                    if(receiverQueue) {
                        receivedMessageQueue.add(msg);
                        return ;
                    }
                }
               break;
            default:
                //we didn't queue, do nothing
                break;
        } //switch

        messageReceived(msg, msg.getAddress());
    }
}
 
Example #15
Source File: BackupManager.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public ClusterMessage requestCompleted(String sessionId) {
    if (!getState().isAvailable()) return null;
    LazyReplicatedMap<String,Session> map =
            (LazyReplicatedMap<String,Session>)sessions;
    map.replicate(sessionId,false);
    return null;
}
 
Example #16
Source File: ReplicationValve.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * send manager requestCompleted message to cluster
 * @param manager SessionManager
 * @param cluster replication cluster
 * @param sessionId sessionid from the manager
 * @see DeltaManager#requestCompleted(String)
 * @see SimpleTcpCluster#send(ClusterMessage)
 */
protected void send(ClusterManager manager, CatalinaCluster cluster, String sessionId) {
    ClusterMessage msg = manager.requestCompleted(sessionId);
    if (msg != null) {
        cluster.send(msg);
        if(doStatistics())
            nrOfSendRequests++;
    }
}
 
Example #17
Source File: SimpleTcpCluster.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(Serializable message, Member sender) {
    ClusterMessage fwd = (ClusterMessage)message;
    fwd.setAddress(sender);
    messageReceived(fwd);
}
 
Example #18
Source File: ClusterSessionListener.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Callback from the cluster, when a message is received, The cluster will
 * broadcast it invoking the messageReceived on the receiver.
 * 
 * @param myobj
 *            ClusterMessage - the message received from the cluster
 */
@Override
public void messageReceived(ClusterMessage myobj) {
    if (myobj != null && myobj instanceof SessionMessage) {
        SessionMessage msg = (SessionMessage) myobj;
        String ctxname = msg.getContextName();
        //check if the message is a EVT_GET_ALL_SESSIONS,
        //if so, wait until we are fully started up
        Map<String,ClusterManager> managers = cluster.getManagers() ;
        if (ctxname == null) {
            for (Map.Entry<String, ClusterManager> entry :
                    managers.entrySet()) {
                if (entry.getValue() != null)
                    entry.getValue().messageDataReceived(msg);
                else {
                    //this happens a lot before the system has started
                    // up
                    if (log.isDebugEnabled())
                        log.debug("Context manager doesn't exist:"
                                + entry.getKey());
                }
            }
        } else {
            ClusterManager mgr = managers.get(ctxname);
            if (mgr != null) {
                mgr.messageDataReceived(msg);
            } else {
                if (log.isWarnEnabled())
                    log.warn("Context manager doesn't exist:" + ctxname);

                // A no context manager message is replied in order to avoid
                // timeout of GET_ALL_SESSIONS sync phase.
                if (msg.getEventType() == SessionMessage.EVT_GET_ALL_SESSIONS) {
                    SessionMessage replymsg = new SessionMessageImpl(ctxname,
                            SessionMessage.EVT_ALL_SESSION_NOCONTEXTMANAGER,
                            null, "NO-CONTEXT-MANAGER","NO-CONTEXT-MANAGER-" + ctxname);
                    cluster.send(replymsg, msg.getAddress());
                }
            }
        }
    }
    return;
}
 
Example #19
Source File: DeltaSession.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public void expire(boolean notify, boolean notifyCluster) {

        // Check to see if session has already been invalidated.
        // Do not check expiring at this point as expire should not return until
        // isValid is false
        if (!isValid)
            return;

        synchronized (this) {
            // Check again, now we are inside the sync so this code only runs once
            // Double check locking - isValid needs to be volatile
            if (!isValid)
                return;

            if (manager == null)
                return;

            String expiredId = getIdInternal();

            if(notifyCluster && expiredId != null &&
                    manager instanceof DeltaManager) {
                DeltaManager dmanager = (DeltaManager)manager;
                CatalinaCluster cluster = dmanager.getCluster();
                ClusterMessage msg = dmanager.requestCompleted(expiredId, true);
                if (msg != null) {
                    cluster.send(msg);
                }
            }

            super.expire(notify);

            if (notifyCluster) {
                if (log.isDebugEnabled())
                    log.debug(sm.getString("deltaSession.notifying",
                                           ((ClusterManager)manager).getName(),
                                           Boolean.valueOf(isPrimarySession()),
                                           expiredId));
                if ( manager instanceof DeltaManager ) {
                    ( (DeltaManager) manager).sessionExpired(expiredId);
                }
            }
        }
    }
 
Example #20
Source File: BackupManager.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void messageDataReceived(ClusterMessage msg) {
}
 
Example #21
Source File: DeltaManager.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
  * When the request has been completed, the replication valve will notify
  * the manager, and the manager will decide whether any replication is
  * needed or not. If there is a need for replication, the manager will
  * create a session message and that will be replicated. The cluster
  * determines where it gets sent.
  * 
  * Session expiration also calls this method, but with expires == true.
  * 
  * @param sessionId -
  *            the sessionId that just completed.
  * @param expires -
  *            whether this method has been called during session expiration
  * @return a SessionMessage to be sent,
  */
 public ClusterMessage requestCompleted(String sessionId, boolean expires) {
    DeltaSession session = null;
    SessionMessage msg = null;
    try {
        session = (DeltaSession) findSession(sessionId);
        if (session == null) {
            // A parallel request has called session.invalidate() which has
            // removed the session from the Manager.
            return null;
        }
        DeltaRequest deltaRequest = session.getDeltaRequest();
        session.lock();
        if (deltaRequest.getSize() > 0) {    
            counterSend_EVT_SESSION_DELTA++;
            byte[] data = serializeDeltaRequest(session,deltaRequest);
            msg = new SessionMessageImpl(getName(),
                                         SessionMessage.EVT_SESSION_DELTA, 
                                         data, 
                                         sessionId,
                                         sessionId + "-" + System.currentTimeMillis());
            session.resetDeltaRequest();
        }  
    } catch (IOException x) {
        log.error(sm.getString("deltaManager.createMessage.unableCreateDeltaRequest",sessionId), x);
        return null;
    }finally {
        if (session!=null) session.unlock();
    }
    if(msg == null) {
        if(!expires && !session.isPrimarySession()) {
            counterSend_EVT_SESSION_ACCESSED++;
            msg = new SessionMessageImpl(getName(),
                                         SessionMessage.EVT_SESSION_ACCESSED, 
                                         null, 
                                         sessionId,
                                         sessionId + "-" + System.currentTimeMillis());
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("deltaManager.createMessage.accessChangePrimary",getName(), sessionId));
            }
        }    
    } else { // log only outside synch block!
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("deltaManager.createMessage.delta",getName(), sessionId));
        }
    }
    if (!expires) session.setPrimarySession(true);
    //check to see if we need to send out an access message
    if (!expires && (msg == null)) {
        long replDelta = System.currentTimeMillis() - session.getLastTimeReplicated();
        if (session.getMaxInactiveInterval() >=0 && 
                replDelta > (session.getMaxInactiveInterval() * 1000L)) {
            counterSend_EVT_SESSION_ACCESSED++;
            msg = new SessionMessageImpl(getName(),
                                         SessionMessage.EVT_SESSION_ACCESSED, 
                                         null,
                                         sessionId, 
                                         sessionId + "-" + System.currentTimeMillis());
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("deltaManager.createMessage.access", getName(),sessionId));
            }
        }
    }

    //update last replicated time
    if (msg != null) {
       session.setLastTimeReplicated(System.currentTimeMillis());
       msg.setTimestamp(session.getLastTimeReplicated());
    }
    return msg;
}
 
Example #22
Source File: BackupManager.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void messageDataReceived(ClusterMessage msg) {
}
 
Example #23
Source File: JvmRouteSessionIDBinderListener.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Callback from the cluster, when a message is received, The cluster will
 * broadcast it invoking the messageReceived on the receiver.
 * 
 * @param msg
 *            ClusterMessage - the message received from the cluster
 */
@Override
public void messageReceived(ClusterMessage msg) {
    if (msg instanceof SessionIDMessage) {
        SessionIDMessage sessionmsg = (SessionIDMessage) msg;
        if (log.isDebugEnabled())
            log.debug(sm.getString(
                    "jvmRoute.receiveMessage.sessionIDChanged", sessionmsg
                            .getOrignalSessionID(), sessionmsg
                            .getBackupSessionID(), sessionmsg
                            .getContextName()));
        Container container = getCluster().getContainer();
        Container host = null ;
        if(container instanceof Engine) {
            host = container.findChild(sessionmsg.getHost());
        } else {
            host = container ;
        }
        if (host != null) {
            Context context = (Context) host.findChild(sessionmsg
                    .getContextName());
            if (context != null) {
                try {
                    Session session = context.getManager().findSession(
                            sessionmsg.getOrignalSessionID());
                    if (session != null) {
                        session.setId(sessionmsg.getBackupSessionID());
                    } else if (log.isInfoEnabled())
                        log.info(sm.getString("jvmRoute.lostSession",
                                sessionmsg.getOrignalSessionID(),
                                sessionmsg.getContextName()));
                } catch (IOException e) {
                    log.error(e);
                }

            } else if (log.isErrorEnabled())
                log.error(sm.getString("jvmRoute.contextNotFound",
                        sessionmsg.getContextName(), ((StandardEngine) host
                                .getParent()).getJvmRoute()));
        } else if (log.isErrorEnabled())
            log.error(sm.getString("jvmRoute.hostNotFound", sessionmsg.getContextName()));
    }
    return;
}
 
Example #24
Source File: SimpleTcpCluster.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(Serializable message, Member sender) {
    ClusterMessage fwd = (ClusterMessage)message;
    fwd.setAddress(sender);
    messageReceived(fwd);
}
 
Example #25
Source File: TomEEClusterListener.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final ClusterMessage clusterMessage) {
    return clusterMessage != null
        && (DeployMessage.class.equals(clusterMessage.getClass())
            || UndeployMessage.class.equals(clusterMessage.getClass()));
}
 
Example #26
Source File: DeltaSession.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public void expire(boolean notify, boolean notifyCluster) {

        // Check to see if session has already been invalidated.
        // Do not check expiring at this point as expire should not return until
        // isValid is false
        if (!isValid)
            return;

        synchronized (this) {
            // Check again, now we are inside the sync so this code only runs once
            // Double check locking - isValid needs to be volatile
            if (!isValid)
                return;

            if (manager == null)
                return;

            String expiredId = getIdInternal();

            if(notifyCluster && expiredId != null &&
                    manager instanceof DeltaManager) {
                DeltaManager dmanager = (DeltaManager)manager;
                CatalinaCluster cluster = dmanager.getCluster();
                ClusterMessage msg = dmanager.requestCompleted(expiredId, true);
                if (msg != null) {
                    cluster.send(msg);
                }
            }

            super.expire(notify);

            if (notifyCluster) {
                if (log.isDebugEnabled())
                    log.debug(sm.getString("deltaSession.notifying",
                                           ((ClusterManager)manager).getName(),
                                           Boolean.valueOf(isPrimarySession()),
                                           expiredId));
                if ( manager instanceof DeltaManager ) {
                    ( (DeltaManager) manager).sessionExpired(expiredId);
                }
            }
        }
    }
 
Example #27
Source File: JvmRouteSessionIDBinderListener.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Callback from the cluster, when a message is received, The cluster will
 * broadcast it invoking the messageReceived on the receiver.
 * 
 * @param msg
 *            ClusterMessage - the message received from the cluster
 */
@Override
public void messageReceived(ClusterMessage msg) {
    if (msg instanceof SessionIDMessage) {
        SessionIDMessage sessionmsg = (SessionIDMessage) msg;
        if (log.isDebugEnabled())
            log.debug(sm.getString(
                    "jvmRoute.receiveMessage.sessionIDChanged", sessionmsg
                            .getOrignalSessionID(), sessionmsg
                            .getBackupSessionID(), sessionmsg
                            .getContextName()));
        Container container = getCluster().getContainer();
        Container host = null ;
        if(container instanceof Engine) {
            host = container.findChild(sessionmsg.getHost());
        } else {
            host = container ;
        }
        if (host != null) {
            Context context = (Context) host.findChild(sessionmsg
                    .getContextName());
            if (context != null) {
                try {
                    Session session = context.getManager().findSession(
                            sessionmsg.getOrignalSessionID());
                    if (session != null) {
                        session.setId(sessionmsg.getBackupSessionID());
                    } else if (log.isInfoEnabled())
                        log.info(sm.getString("jvmRoute.lostSession",
                                sessionmsg.getOrignalSessionID(),
                                sessionmsg.getContextName()));
                } catch (IOException e) {
                    log.error(e);
                }

            } else if (log.isErrorEnabled())
                log.error(sm.getString("jvmRoute.contextNotFound",
                        sessionmsg.getContextName(), ((StandardEngine) host
                                .getParent()).getJvmRoute()));
        } else if (log.isErrorEnabled())
            log.error(sm.getString("jvmRoute.hostNotFound", sessionmsg.getContextName()));
    }
    return;
}
 
Example #28
Source File: DeltaManager.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * When the request has been completed, the replication valve will notify
 * the manager, and the manager will decide whether any replication is
 * needed or not. If there is a need for replication, the manager will
 * create a session message and that will be replicated. The cluster
 * determines where it gets sent.
 *
 * Session expiration also calls this method, but with expires == true.
 *
 * @param sessionId -
 *            the sessionId that just completed.
 * @param expires -
 *            whether this method has been called during session expiration
 * @return a SessionMessage to be sent,
 */
public ClusterMessage requestCompleted(String sessionId, boolean expires) {
    DeltaSession session = null;
    SessionMessage msg = null;
    try {
        session = (DeltaSession) findSession(sessionId);
        if (session == null) {
            // A parallel request has called session.invalidate() which has
            // removed the session from the Manager.
            return null;
        }
        if (session.isDirty()) {
            counterSend_EVT_SESSION_DELTA++;
            msg = new SessionMessageImpl(getName(),
                                         SessionMessage.EVT_SESSION_DELTA,
                                         session.getDiff(),
                                         sessionId,
                                         sessionId + "-" + System.currentTimeMillis());
        }
    } catch (IOException x) {
        log.error(sm.getString("deltaManager.createMessage.unableCreateDeltaRequest",
                sessionId), x);
        return null;
    }
    if(msg == null) {
        if(!expires && !session.isPrimarySession()) {
            counterSend_EVT_SESSION_ACCESSED++;
            msg = new SessionMessageImpl(getName(),
                                         SessionMessage.EVT_SESSION_ACCESSED,
                                         null,
                                         sessionId,
                                         sessionId + "-" + System.currentTimeMillis());
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("deltaManager.createMessage.accessChangePrimary",
                        getName(), sessionId));
            }
        }
    } else { // log only outside synch block!
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("deltaManager.createMessage.delta", getName(), sessionId));
        }
    }
    if (!expires) session.setPrimarySession(true);
    //check to see if we need to send out an access message
    if (!expires && (msg == null)) {
        long replDelta = System.currentTimeMillis() - session.getLastTimeReplicated();
        if (session.getMaxInactiveInterval() >=0 &&
                replDelta > (session.getMaxInactiveInterval() * 1000L)) {
            counterSend_EVT_SESSION_ACCESSED++;
            msg = new SessionMessageImpl(getName(),
                                         SessionMessage.EVT_SESSION_ACCESSED,
                                         null,
                                         sessionId,
                                         sessionId + "-" + System.currentTimeMillis());
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("deltaManager.createMessage.access",
                        getName(), sessionId));
            }
        }
    }

    //update last replicated time
    if (msg != null) {
       session.setLastTimeReplicated(System.currentTimeMillis());
       msg.setTimestamp(session.getLastTimeReplicated());
    }
    return msg;
}
 
Example #29
Source File: ClusterSessionListener.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Callback from the cluster, when a message is received, The cluster will
 * broadcast it invoking the messageReceived on the receiver.
 *
 * @param myobj
 *            ClusterMessage - the message received from the cluster
 */
@Override
public void messageReceived(ClusterMessage myobj) {
    if (myobj instanceof SessionMessage) {
        SessionMessage msg = (SessionMessage) myobj;
        String ctxname = msg.getContextName();
        //check if the message is a EVT_GET_ALL_SESSIONS,
        //if so, wait until we are fully started up
        Map<String,ClusterManager> managers = cluster.getManagers() ;
        if (ctxname == null) {
            for (Map.Entry<String, ClusterManager> entry :
                    managers.entrySet()) {
                if (entry.getValue() != null)
                    entry.getValue().messageDataReceived(msg);
                else {
                    //this happens a lot before the system has started
                    // up
                    if (log.isDebugEnabled())
                        log.debug(sm.getString("clusterSessionListener.noManager", entry.getKey()));
                }
            }
        } else {
            ClusterManager mgr = managers.get(ctxname);
            if (mgr != null) {
                mgr.messageDataReceived(msg);
            } else {
                if (log.isWarnEnabled())
                    log.warn(sm.getString("clusterSessionListener.noManager", ctxname));

                // A no context manager message is replied in order to avoid
                // timeout of GET_ALL_SESSIONS sync phase.
                if (msg.getEventType() == SessionMessage.EVT_GET_ALL_SESSIONS) {
                    SessionMessage replymsg = new SessionMessageImpl(ctxname,
                            SessionMessage.EVT_ALL_SESSION_NOCONTEXTMANAGER,
                            null, "NO-CONTEXT-MANAGER","NO-CONTEXT-MANAGER-" + ctxname);
                    cluster.send(replymsg, msg.getAddress());
                }
            }
        }
    }
}
 
Example #30
Source File: ClusterSessionListener.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Callback from the cluster, when a message is received, The cluster will
 * broadcast it invoking the messageReceived on the receiver.
 * 
 * @param myobj
 *            ClusterMessage - the message received from the cluster
 */
@Override
public void messageReceived(ClusterMessage myobj) {
    if (myobj != null && myobj instanceof SessionMessage) {
        SessionMessage msg = (SessionMessage) myobj;
        String ctxname = msg.getContextName();
        //check if the message is a EVT_GET_ALL_SESSIONS,
        //if so, wait until we are fully started up
        Map<String,ClusterManager> managers = cluster.getManagers() ;
        if (ctxname == null) {
            for (Map.Entry<String, ClusterManager> entry :
                    managers.entrySet()) {
                if (entry.getValue() != null)
                    entry.getValue().messageDataReceived(msg);
                else {
                    //this happens a lot before the system has started
                    // up
                    if (log.isDebugEnabled())
                        log.debug("Context manager doesn't exist:"
                                + entry.getKey());
                }
            }
        } else {
            ClusterManager mgr = managers.get(ctxname);
            if (mgr != null) {
                mgr.messageDataReceived(msg);
            } else {
                if (log.isWarnEnabled())
                    log.warn("Context manager doesn't exist:" + ctxname);

                // A no context manager message is replied in order to avoid
                // timeout of GET_ALL_SESSIONS sync phase.
                if (msg.getEventType() == SessionMessage.EVT_GET_ALL_SESSIONS) {
                    SessionMessage replymsg = new SessionMessageImpl(ctxname,
                            SessionMessage.EVT_ALL_SESSION_NOCONTEXTMANAGER,
                            null, "NO-CONTEXT-MANAGER","NO-CONTEXT-MANAGER-" + ctxname);
                    cluster.send(replymsg, msg.getAddress());
                }
            }
        }
    }
    return;
}