Search in sources :

Example 1 with BrokerStateHandler

use of com.sun.messaging.jmq.jmsserver.BrokerStateHandler in project openmq by eclipse-ee4j.

the class BrokerConfig method restart.

public void restart() {
    // not allowed to restart an in-process broker
    if (Broker.isInProcess()) {
        String message = rb.getString(BrokerResources.E_CANNOT_RESTART_IN_PROCESS);
        logger.log(Logger.WARNING, "BrokerConfig MBean: " + message);
        return;
    }
    BrokerStateHandler bsh = Globals.getBrokerStateHandler();
    logger.log(Logger.INFO, "Restart request received by MBean " + getMBeanName());
    bsh.initiateShutdown("jmx", 0, true, bsh.getRestartCode(), true, Broker.isInProcess(), false);
}
Also used : BrokerStateHandler(com.sun.messaging.jmq.jmsserver.BrokerStateHandler)

Example 2 with BrokerStateHandler

use of com.sun.messaging.jmq.jmsserver.BrokerStateHandler in project openmq by eclipse-ee4j.

the class BrokerConfig method quiesce.

public void quiesce() throws MBeanException {
    BrokerStateHandler bsh = Globals.getBrokerStateHandler();
    logger.log(Logger.INFO, "Quiesce request received by MBean " + getMBeanName());
    try {
        bsh.quiesce();
    } catch (BrokerException e) {
        handleOperationException(BrokerOperations.QUIESCE, e);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) BrokerStateHandler(com.sun.messaging.jmq.jmsserver.BrokerStateHandler)

Example 3 with BrokerStateHandler

use of com.sun.messaging.jmq.jmsserver.BrokerStateHandler in project openmq by eclipse-ee4j.

the class MigrateStoreHandler method handle.

/**
 * Handle the incomming administration message.
 *
 * @param con The Connection the message came in on.
 * @param cmd_msg The administration message
 * @param cmd_props The properties from the administration message
 */
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
    boolean noop = true;
    int status = Status.OK;
    String errMsg = "";
    if (DEBUG) {
        logger.log(Logger.INFO, this.getClass().getName() + ": " + "Request migrate this broker''s store: " + cmd_props);
    }
    String brokerID = (String) cmd_props.get(MessageType.JMQ_BROKER_ID);
    String partition = (String) cmd_props.get(MessageType.JMQ_MIGRATESTORE_PARTITION);
    if (partition == null) {
        if (brokerID != null) {
            logger.log(Logger.INFO, BrokerResources.I_ADMIN_MIGRATESTORE_TO, brokerID);
        } else {
            logger.log(Logger.INFO, BrokerResources.I_ADMIN_MIGRATESTORE);
        }
    } else {
        logger.log(Logger.INFO, "XXXAdmin request migrate this broker's store partition " + partition + " to broker " + brokerID);
    }
    HAMonitorService hamonitor = Globals.getHAMonitorService();
    if (hamonitor != null && hamonitor.inTakeover()) {
        status = Status.NOT_MODIFIED;
        errMsg = rb.getString(rb.E_CANNOT_PROCEED_TAKEOVER_IN_PROCESS);
        logger.log(Logger.ERROR, this.getClass().getName() + ": " + errMsg);
    }
    if (status == Status.OK) {
        if (partition == null && Globals.getHAEnabled()) {
            status = Status.NOT_MODIFIED;
            errMsg = rb.getKString(rb.E_OPERATION_NOT_SUPPORTED_IN_HA, MessageType.getString(MessageType.MIGRATESTORE_BROKER));
            logger.log(Logger.ERROR, errMsg);
        }
    }
    if (status == Status.OK) {
        if (Globals.isJMSRAManagedBroker()) {
            status = Status.NOT_MODIFIED;
            errMsg = "Can not process migration store request because this broker's life cycle is JMSRA managed";
            logger.log(Logger.ERROR, this.getClass().getName() + ": " + errMsg);
        }
    }
    UID partitionID = null;
    if (status == Status.OK) {
        if (partition != null) {
            try {
                long v = Long.parseLong(partition);
                partitionID = new UID(v);
            } catch (Exception e) {
                partitionID = null;
                status = Status.NOT_MODIFIED;
                errMsg = "XXXCan not process migration partition " + partition + " request because unable to parse " + partition + ": " + e;
                logger.log(Logger.ERROR, errMsg);
            }
        }
    }
    if (status == Status.OK) {
        if (partitionID != null && brokerID == null) {
            status = Status.NOT_MODIFIED;
            errMsg = "XXXCan not process migration partition " + partitionID + " request because brokerID not specified";
            logger.log(Logger.ERROR, errMsg);
        }
    }
    if (status == Status.OK) {
        if (partitionID != null && !(DL.isPartitionMode() && DL.isPartitionMigratable())) {
            status = Status.NOT_MODIFIED;
            errMsg = "XXXCan not process migration partition " + partitionID + " request because partition mode not enabled";
            logger.log(Logger.ERROR, errMsg);
        }
    }
    if (status == Status.OK) {
        if (partitionID != null) {
            DestinationList dl = DL.getDestinationList(partitionID);
            if (dl == null) {
                status = Status.NOT_MODIFIED;
                errMsg = "XXXCan not process migration partition " + partitionID + " request because partition " + partitionID + " not found";
                logger.log(Logger.ERROR, errMsg);
            } else if (dl.getPartitionedStore().isPrimaryPartition()) {
                status = Status.NOT_MODIFIED;
                errMsg = "XXXCan not process migration partition " + partitionID + " request because partition " + partitionID + " is the primary partition";
                logger.log(Logger.ERROR, errMsg);
            }
        }
    }
    if (status == Status.OK) {
        if (brokerID == null) {
            try {
                brokerID = getBrokerID();
            } catch (Throwable t) {
                status = Status.NOT_MODIFIED;
                errMsg = "Unable to get a connected broker to takeover this broker's store: " + t.getMessage();
                if ((t instanceof OperationNotAllowedException) && ((OperationNotAllowedException) t).getOperation().equals(MessageType.getString(MessageType.MIGRATESTORE_BROKER))) {
                    logger.log(logger.ERROR, errMsg);
                } else {
                    logger.logStack(logger.ERROR, errMsg, t);
                }
            }
        }
    }
    if (status != Status.OK) {
        sendReply(con, cmd_msg, brokerID, null, errMsg, status, null);
        return true;
    }
    try {
        BrokerStateHandler.setExclusiveRequestLock(ExclusiveRequest.MIGRATE_STORE);
    } catch (Throwable t) {
        status = Status.PRECONDITION_FAILED;
        if (t instanceof BrokerException) {
            status = ((BrokerException) t).getStatusCode();
        }
        errMsg = MessageType.getString(MessageType.MIGRATESTORE_BROKER) + ": " + Status.getString(status) + " - " + t.getMessage();
        logger.log(Logger.ERROR, errMsg);
        status = Status.NOT_MODIFIED;
    }
    try {
        if (partitionID != null) {
            migratePartition(con, cmd_msg, partitionID, brokerID);
            return true;
        }
        Long syncTimeout = null;
        final BrokerStateHandler bsh = Globals.getBrokerStateHandler();
        if (status == Status.OK) {
            try {
                syncTimeout = (Long) cmd_props.get(MessageType.JMQ_MIGRATESTORE_SYNC_TIMEOUT);
                ClusterManager cm = Globals.getClusterManager();
                BrokerMQAddress self = (BrokerMQAddress) cm.getMQAddress();
                BrokerMQAddress master = (cm.getMasterBroker() == null ? null : (BrokerMQAddress) cm.getMasterBroker().getBrokerURL());
                if (self.equals(master)) {
                    throw new BrokerException(rb.getKString(rb.E_CHANGE_MASTER_BROKER_FIRST, MessageType.getString(MessageType.MIGRATESTORE_BROKER)), Status.NOT_ALLOWED);
                }
            } catch (Throwable t) {
                status = Status.PRECONDITION_FAILED;
                if (t instanceof BrokerException) {
                    status = ((BrokerException) t).getStatusCode();
                }
                errMsg = MessageType.getString(MessageType.MIGRATESTORE_BROKER) + ": " + Status.getString(status) + " - " + t.getMessage();
                logger.log(Logger.ERROR, errMsg);
                status = Status.NOT_MODIFIED;
            }
        }
        SysMessageID replyMessageID = null;
        String replyStatusStr = null;
        try {
            // shutdown if !noop
            String hostport = null;
            if (status == Status.OK) {
                try {
                    noop = false;
                    hostport = bsh.takeoverME(brokerID, syncTimeout, con);
                } catch (BrokerException ex) {
                    status = ex.getStatusCode();
                    if (status == Status.BAD_REQUEST || status == Status.NOT_ALLOWED || status == Status.NOT_MODIFIED || status == Status.UNAVAILABLE || status == Status.PRECONDITION_FAILED) {
                        status = Status.PRECONDITION_FAILED;
                        if (ex instanceof OperationNotAllowedException) {
                            if (((OperationNotAllowedException) ex).getOperation().equals(MessageType.getString(MessageType.MIGRATESTORE_BROKER))) {
                                status = Status.NOT_MODIFIED;
                                noop = true;
                            }
                        }
                        errMsg = Globals.getBrokerResources().getKString(BrokerResources.E_FAIL_MIGRATESTORE_NOT_MIGRATED, ex.getMessage());
                        if (noop) {
                            logger.log(Logger.ERROR, errMsg);
                        } else {
                            logger.logStack(Logger.ERROR, errMsg, ex);
                        }
                    } else {
                        status = Status.EXPECTATION_FAILED;
                        errMsg = Globals.getBrokerResources().getKString(BrokerResources.E_FAIL_TAKEOVERME, brokerID, ex.getMessage());
                        logger.logStack(Logger.ERROR, errMsg, ex);
                    }
                }
            }
            if (status == Status.OK) {
                try {
                    Globals.getClusterBroadcast().stopClusterIO(false, true, null);
                } catch (Throwable t) {
                    logger.logStack(Logger.WARNING, "Failed to stop cluster IO", t);
                }
            }
            List ret = sendReply(con, cmd_msg, brokerID, hostport, errMsg, status, null);
            replyMessageID = (SysMessageID) ret.get(0);
            replyStatusStr = (String) ret.get(1);
        } finally {
            final SysMessageID mid = replyMessageID;
            final String statusStr = replyStatusStr;
            if (!noop) {
                try {
                    if (con instanceof IMQBasicConnection) {
                        IMQBasicConnection ipCon = (IMQBasicConnection) con;
                        ipCon.flushControl(1000);
                    }
                    try {
                        Globals.getServiceManager().stopNewConnections(ServiceType.NORMAL);
                    } catch (Exception e) {
                        logger.logStack(logger.WARNING, rb.getKString(rb.W_STOP_SERVICE_FAIL, ServiceType.getServiceTypeString(ServiceType.NORMAL), e.getMessage()), e);
                    }
                    try {
                        Globals.getServiceManager().stopNewConnections(ServiceType.ADMIN);
                    } catch (Exception e) {
                        logger.logStack(logger.WARNING, rb.getKString(rb.W_STOP_SERVICE_FAIL, ServiceType.getServiceTypeString(ServiceType.ADMIN), e.getMessage()), e);
                    }
                    BrokerStateHandler.setShuttingDown(true);
                    bsh.prepareShutdown(false, true);
                    waitForHandlersToComplete(20);
                    if (mid == null) {
                        logger.log(Logger.INFO, BrokerResources.I_ADMIN_SHUTDOWN_REQUEST);
                        bsh.initiateShutdown("admin", 0, false, 0, true);
                        return true;
                    }
                    final String waitstr = rb.getKString(rb.I_WAIT_ADMIN_RECEIVE_REPLY, MessageType.getString(MessageType.MIGRATESTORE_BROKER_REPLY) + "[" + statusStr + "]");
                    final long totalwait = Globals.getConfig().getIntProperty(MAX_WAIT_ADMIN_CLIENT_PROP, DEFAULT_MAX_WAIT_ADMIN_CLIENT) * 1000L;
                    final IMQConnection conn = con;
                    Executors.newSingleThreadExecutor().execute(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                long waited = 0L;
                                while (conn.getConnectionState() < Connection.STATE_CLEANED && waited < totalwait) {
                                    logger.log(logger.INFO, waitstr);
                                    try {
                                        Thread.sleep(500);
                                        waited += 500L;
                                    } catch (Exception e) {
                                    /* ignore */
                                    }
                                }
                                logger.log(Logger.INFO, BrokerResources.I_ADMIN_SHUTDOWN_REQUEST);
                                bsh.initiateShutdown("admin-migratestore-shutdown", 0, false, 0, true);
                            } catch (Throwable t) {
                                bsh.initiateShutdown("admin-migratestore-shutdown::[" + t.toString() + "]", 0, false, 0, true);
                            }
                        }
                    });
                } catch (Throwable t) {
                    bsh.initiateShutdown("admin-migratestore-shutdown:[" + t.toString() + "]", 0, false, 0, true);
                }
            }
        }
    } finally {
        BrokerStateHandler.unsetExclusiveRequestLock(ExclusiveRequest.MIGRATE_STORE);
    }
    return true;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) BrokerStateHandler(com.sun.messaging.jmq.jmsserver.BrokerStateHandler) DestinationList(com.sun.messaging.jmq.jmsserver.core.DestinationList) IMQBasicConnection(com.sun.messaging.jmq.jmsserver.service.imq.IMQBasicConnection) OperationNotAllowedException(com.sun.messaging.jmq.jmsserver.util.OperationNotAllowedException) OperationNotAllowedException(com.sun.messaging.jmq.jmsserver.util.OperationNotAllowedException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) BrokerMQAddress(com.sun.messaging.jmq.jmsserver.core.BrokerMQAddress) UID(com.sun.messaging.jmq.util.UID) IMQConnection(com.sun.messaging.jmq.jmsserver.service.imq.IMQConnection) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) DestinationList(com.sun.messaging.jmq.jmsserver.core.DestinationList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with BrokerStateHandler

use of com.sun.messaging.jmq.jmsserver.BrokerStateHandler in project openmq by eclipse-ee4j.

the class QuiesceHandler method handle.

/**
 * Handle the incomming administration message.
 *
 * @param con The Connection the message came in on.
 * @param cmd_msg The administration message
 * @param cmd_props The properties from the administration message
 */
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
    if (DEBUG) {
        logger.log(Logger.DEBUG, this.getClass().getName() + ": " + "Quiescing broker: " + cmd_props);
    }
    int status = Status.OK;
    String errMsg = null;
    // Send reply
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    HAMonitorService hamonitor = Globals.getHAMonitorService();
    if (hamonitor != null && hamonitor.inTakeover()) {
        status = Status.ERROR;
        errMsg = rb.getString(rb.E_CANNOT_PROCEED_TAKEOVER_IN_PROCESS);
        logger.log(Logger.ERROR, this.getClass().getName() + ": " + errMsg);
    } else {
        try {
            BrokerStateHandler bsh = Globals.getBrokerStateHandler();
            bsh.quiesce();
        } catch (Exception ex) {
            errMsg = ex.toString();
            status = Status.ERROR;
        }
    }
    setProperties(reply, MessageType.QUIESCE_BROKER_REPLY, status, errMsg);
    parent.sendReply(con, cmd_msg, reply);
    return true;
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) BrokerStateHandler(com.sun.messaging.jmq.jmsserver.BrokerStateHandler) HAMonitorService(com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)

Example 5 with BrokerStateHandler

use of com.sun.messaging.jmq.jmsserver.BrokerStateHandler in project openmq by eclipse-ee4j.

the class ShutdownHandler method handle.

/**
 * Handle the incoming administration message.
 *
 * @param con The Connection the message came in on.
 * @param cmd_msg The administration message
 * @param cmd_props The properties from the administration message
 */
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
    if (DEBUG) {
        logger.log(Logger.DEBUG, this.getClass().getName() + ": " + "Shutting down broker: " + cmd_props);
    }
    boolean shouldRestart = true;
    Boolean noFailover;
    Integer time = null;
    // extract the properties from the administration message
    Boolean kill = (Boolean) cmd_props.get(MessageType.JMQ_KILL);
    noFailover = (Boolean) cmd_props.get(MessageType.JMQ_NO_FAILOVER);
    boolean failover = (noFailover == null || !(noFailover.booleanValue()));
    time = (Integer) cmd_props.get(MessageType.JMQ_TIME);
    Boolean restart = (Boolean) cmd_props.get(MessageType.JMQ_RESTART);
    // is this a restart?
    shouldRestart = (restart != null && restart.booleanValue());
    if (shouldRestart) {
        failover = false;
    }
    // Prepare reply
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    // not allowed to shutdown an in-process broker that has direct connections
    if (Broker.isInProcess() && !shouldRestart && hasDirectConnections()) {
        // TODO COrrect this message
        String error = rb.getString(BrokerResources.E_CANNOT_SHUTDOWN_IN_PROCESS);
        setProperties(reply, MessageType.SHUTDOWN_REPLY, Status.ERROR, error);
        parent.sendReply(con, cmd_msg, reply);
        return true;
    }
    // not allowed to restart an in-process broker
    if (Broker.isInProcess() && shouldRestart) {
        String error = rb.getString(BrokerResources.E_CANNOT_RESTART_IN_PROCESS);
        setProperties(reply, MessageType.SHUTDOWN_REPLY, Status.ERROR, error);
        parent.sendReply(con, cmd_msg, reply);
        return true;
    }
    // this could be done before the previous check if we wanted
    if (kill != null && kill.booleanValue()) {
        Broker.getBroker().removeBrokerShutdownHook();
        System.exit(1);
    }
    // audit logging for broker restart/shutdown
    Globals.getAuditSession().brokerOperation(con.getUserName(), con.remoteHostString(), shouldRestart ? MQAuditSession.BROKER_RESTART : MQAuditSession.BROKER_SHUTDOWN);
    // now begin the shutdown sequence
    try {
        // stop taking new requests
        Globals.getServiceManager().stopNewConnections(ServiceType.NORMAL);
        if (time == null || time.intValue() == 0) {
            Globals.getServiceManager().stopNewConnections(ServiceType.ADMIN);
        }
    } catch (Exception ex) {
        logger.logStack(Logger.WARNING, BrokerResources.E_INTERNAL_BROKER_ERROR, "unable to shutdown", ex);
    } finally {
        // send a reply to the client that we received the request
        // currently we return OK even if we had an error during the shutdown
        setProperties(reply, MessageType.SHUTDOWN_REPLY, Status.OK, null);
        parent.sendReply(con, cmd_msg, reply);
        if (con instanceof IMQBasicConnection) {
            IMQBasicConnection ipCon = (IMQBasicConnection) con;
            ipCon.flushControl(1000);
        }
        BrokerStateHandler bsh = Globals.getBrokerStateHandler();
        if (time == null || time.intValue() == 0) {
            // stop new connections
            // set the flag to notify everyone we are shutting down
            BrokerStateHandler.setShuttingDown(true);
            bsh.prepareShutdown(failover, false);
        }
        // if we aren't doing an unsafe exit, give us time to complete any operations
        waitForHandlersToComplete(20);
        // logging
        if (shouldRestart) {
            logger.log(Logger.INFO, BrokerResources.I_ADMIN_RESTART_REQUEST);
        } else {
            logger.log(Logger.INFO, BrokerResources.I_ADMIN_SHUTDOWN_REQUEST);
        }
        // shutdown the broker
        bsh.initiateShutdown("admin", (time == null ? 0 : time.longValue()) * 1000, failover, (shouldRestart ? BrokerStateHandler.getRestartCode() : 0), true);
    }
    return true;
}
Also used : Packet(com.sun.messaging.jmq.io.Packet) BrokerStateHandler(com.sun.messaging.jmq.jmsserver.BrokerStateHandler) IMQBasicConnection(com.sun.messaging.jmq.jmsserver.service.imq.IMQBasicConnection)

Aggregations

BrokerStateHandler (com.sun.messaging.jmq.jmsserver.BrokerStateHandler)10 Packet (com.sun.messaging.jmq.io.Packet)4 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)4 HAMonitorService (com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)2 IMQBasicConnection (com.sun.messaging.jmq.jmsserver.service.imq.IMQBasicConnection)2 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)1 BrokerMQAddress (com.sun.messaging.jmq.jmsserver.core.BrokerMQAddress)1 DestinationList (com.sun.messaging.jmq.jmsserver.core.DestinationList)1 IMQConnection (com.sun.messaging.jmq.jmsserver.service.imq.IMQConnection)1 OperationNotAllowedException (com.sun.messaging.jmq.jmsserver.util.OperationNotAllowedException)1 UID (com.sun.messaging.jmq.util.UID)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1