Search in sources :

Example 16 with HAMonitorService

use of com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService in project openmq by eclipse-ee4j.

the class DestroyDestinationHandler 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() + ": " + "Destroying destination: " + cmd_props);
    }
    String destination = (String) cmd_props.get(MessageType.JMQ_DESTINATION);
    Integer destType = (Integer) cmd_props.get(MessageType.JMQ_DEST_TYPE);
    int status = Status.OK;
    String errMsg = null;
    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 {
            if (destType == null) {
                throw new Exception(rb.X_NO_DEST_TYPE_SET);
            }
            // audit logging for destroy destination
            Globals.getAuditSession().destinationOperation(con.getUserName(), con.remoteHostString(), MQAuditSession.DESTROY_DESTINATION, DestType.isQueue(destType.intValue()) ? MQAuditSession.QUEUE : MQAuditSession.TOPIC, destination);
            Destination[] ds = DL.removeDestination(null, destination, DestType.isQueue(destType.intValue()), rb.getString(rb.M_ADMIN_REQUEST));
            Destination d = ds[0];
            boolean ok = (d != null);
            if (!ok) {
                status = Status.ERROR;
                String subError = rb.getString(rb.E_NO_SUCH_DESTINATION, getDestinationType(destType.intValue()), destination);
                errMsg = rb.getString(rb.X_DESTROY_DEST_EXCEPTION, destination, subError);
            }
        } catch (Exception ex) {
            status = Status.ERROR;
            errMsg = rb.getString(rb.X_DESTROY_DEST_EXCEPTION, destination, getMessageFromException(ex));
            logger.logStack(Logger.WARNING, rb.X_DESTROY_DEST_EXCEPTION, destination, "", ex);
        }
    }
    // Send reply
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    setProperties(reply, MessageType.DESTROY_DESTINATION_REPLY, status, errMsg);
    parent.sendReply(con, cmd_msg, reply);
    return true;
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) HAMonitorService(com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)

Example 17 with HAMonitorService

use of com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService in project openmq by eclipse-ee4j.

the class DestroyDurableHandler 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.INFO, this.getClass().getName() + ": " + "DestroyDurable: " + cmd_props);
    }
    int status = Status.OK;
    String errMsg = null;
    String durable = (String) cmd_props.get(MessageType.JMQ_DURABLE_NAME);
    String clientID = (String) cmd_props.get(MessageType.JMQ_CLIENT_ID);
    try {
        if (clientID != null && clientID.trim().length() == 0) {
            throw new BrokerException(rb.getKString(rb.X_INVALID_CLIENTID, clientID), Status.BAD_REQUEST);
        }
        HAMonitorService hamonitor = Globals.getHAMonitorService();
        if (hamonitor != null && hamonitor.inTakeover()) {
            status = Status.UNAVAILABLE;
            errMsg = rb.getString(rb.E_CANNOT_PROCEED_TAKEOVER_IN_PROCESS);
            logger.log(Logger.WARNING, this.getClass().getName() + ": " + errMsg);
        } else {
            // audit logging for destroy durables
            Globals.getAuditSession().durableSubscriberOperation(con.getUserName(), con.remoteHostString(), MQAuditSession.DESTROY_DURABLE, durable, clientID);
            Subscription.unsubscribe(durable, clientID, true);
        }
    } catch (BrokerException ex) {
        errMsg = getMessageFromException(ex);
        status = ex.getStatusCode();
        if (status == Status.BAD_REQUEST || status == Status.NOT_FOUND || status == Status.UNAVAILABLE) {
            logger.log(logger.ERROR, errMsg);
        } else {
            logger.logStack(logger.ERROR, ex.getMessage(), ex);
        }
    }
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    setProperties(reply, MessageType.DESTROY_DURABLE_REPLY, status, errMsg);
    parent.sendReply(con, cmd_msg, reply);
    return true;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) HAMonitorService(com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)

Example 18 with HAMonitorService

use of com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService in project openmq by eclipse-ee4j.

the class Destination method load.

public synchronized LinkedHashMap load(boolean neverExpire, Map preparedAcks, Map transactionStates, Map committingTrans, Set takeoverMsgs, boolean noerrnotfound) throws BrokerException {
    if (Globals.getStore().getPartitionModeEnabled() && takeoverMsgs != null) {
        String emsg = br.getKString(br.E_INTERNAL_BROKER_ERROR, ": Unexpected call:Destination.load(takeoverMsgs) for partition mode");
        BrokerException ex = new BrokerException(emsg);
        logger.logStack(logger.ERROR, emsg, ex);
        throw ex;
    }
    if (loaded) {
        return null;
    }
    logger.log(Logger.INFO, br.getKString(br.I_LOADING_DESTINATION, toString(), String.valueOf(size)) + logsuffix);
    LinkedHashMap preparedTrans = null;
    boolean enforceLimit = true;
    Set deadMsgs = new HashSet();
    int maxloadcnt = size;
    int curcnt = 0;
    try {
        enforceLimit = destMessages.getEnforceLimits();
        destMessages.enforceLimits(false);
        Enumeration msgs = null;
        try {
            msgs = pstore.messageEnumeration(this);
        } catch (DestinationNotFoundException e) {
            if (noerrnotfound) {
                logger.log(Logger.INFO, br.getKString(BrokerResources.I_LOAD_DST_NOTFOUND_INSTORE, getName(), e.getMessage()));
                return null;
            }
            throw e;
        }
        MessageDeliveryTimeTimer dt = deliveryTimeTimer;
        SortedSet s = null;
        try {
            // no other store access should occur in this block
            HAMonitorService haMonitor = Globals.getHAMonitorService();
            boolean takingoverCheck = (takeoverMsgs == null && !Globals.getStore().getPartitionModeEnabled() && Globals.getHAEnabled() && haMonitor != null && haMonitor.checkTakingoverDestination(this));
            if (dt == null && !isDMQ()) {
                if (!isValid()) {
                    String emsg = br.getKString(br.X_LOAD_MSGS_TO_DESTROYED_DST, getDestinationUID());
                    logger.log(logger.WARNING, emsg);
                    throw new BrokerException(emsg);
                }
                String emsg = br.getKString(br.X_LOAD_MSGS_TO_DST_NO_DELIVERY_TIMER, getDestinationUID());
                logger.log(logger.WARNING, emsg);
                throw new BrokerException(emsg);
            }
            s = new TreeSet(new RefCompare());
            while (msgs.hasMoreElements()) {
                Packet p = (Packet) msgs.nextElement();
                PacketReference pr = PacketReference.createReference(pstore, p, uid, null);
                if (isDMQ()) {
                    pr.clearDeliveryTimeInfo();
                }
                if (takeoverMsgs != null && takeoverMsgs.contains(pr)) {
                    pr = null;
                    continue;
                }
                if (takingoverCheck && haMonitor.checkTakingoverMessage(p)) {
                    pr = null;
                    continue;
                }
                MessageDeliveryTimeInfo di = pr.getDeliveryTimeInfo();
                if (di != null) {
                    dt.removeMessage(di);
                }
                if (neverExpire) {
                    pr.overrideExpireTime(0);
                }
                // mark already stored and make packet a SoftReference to
                // prevent running out of memory if dest has lots of msgs
                pr.setLoaded();
                if (getDEBUG()) {
                    logger.log(Logger.INFO, "Loaded Message " + p + " into destination " + this);
                }
                try {
                    if (!isDMQ && !DL.addNewMessage(false, pr).getReturn()) {
                        // expired
                        deadMsgs.add(pr);
                    }
                } catch (Exception ex) {
                    String[] args = { pr.getSysMessageID().toString(), pr.getDestinationUID().toString(), ex.getMessage() };
                    logger.logStack(Logger.WARNING, BrokerResources.W_CAN_NOT_LOAD_MSG, args, ex);
                    continue;
                }
                s.add(pr);
                DL.packetlistAdd(pr.getSysMessageID(), pr.getDestinationUID(), null);
                curcnt++;
                if (curcnt > 0 && (curcnt % LOAD_COUNT == 0 || (curcnt > LOAD_COUNT && curcnt == size))) {
                    String[] args = { toString(), String.valueOf(curcnt), String.valueOf(maxloadcnt), String.valueOf((curcnt * 100) / maxloadcnt) };
                    logger.log(Logger.INFO, BrokerResources.I_LOADING_DEST_IN_PROCESS, args);
                }
            }
        } finally {
            pstore.closeEnumeration(msgs);
        }
        if (FaultInjection.getInjection().FAULT_INJECTION) {
            FaultInjection fi = FaultInjection.getInjection();
            try {
                fi.checkFaultAndThrowBrokerException(FaultInjection.FAULT_LOAD_DST_1_5, null);
            } catch (BrokerException e) {
                fi.unsetFault(fi.FAULT_LOAD_DST_1_5);
                throw e;
            }
        }
        // now we're sorted, process
        Iterator itr = s.iterator();
        while (itr.hasNext()) {
            PacketReference pr = (PacketReference) itr.next();
            // ok .. see if we need to remove the message
            ConsumerUID[] consumers = pstore.getConsumerUIDs(getDestinationUID(), pr.getSysMessageID());
            if (consumers == null) {
                consumers = new ConsumerUID[0];
            }
            if (getDEBUG()) {
                logger.log(Logger.INFO, consumers.length + " stored consumers for " + pr + ":" + getDestinationUID());
            }
            if (consumers.length == 0 && pstore.hasMessageBeenAcked(uid, pr.getSysMessageID())) {
                if (getDEBUG()) {
                    logger.log(Logger.INFO, "Message " + pr.getSysMessageID() + "[" + this + "] has been acked, destory..");
                }
                decrementDestinationSize(pr);
                DL.removePacketList(pr.getSysMessageID(), pr.getDestinationUID(), pr);
                pr.destroy();
                continue;
            }
            if (consumers.length > 0) {
                pr.setStoredWithInterest(true);
            } else {
                pr.setStoredWithInterest(false);
            }
            // first producer side transactions
            MessageDeliveryTimeInfo di = pr.getDeliveryTimeInfo();
            boolean dontRoute = false;
            boolean delayDelivery = false;
            if (di != null && !di.isDeliveryDue()) {
                dt.addMessage(di);
                delayDelivery = true;
                dontRoute = true;
            }
            TransactionUID sendtid = pr.getTransactionID();
            if (sendtid != null) {
                // if unrouted and not in rollback -> remove
                Boolean state = (Boolean) (transactionStates == null ? null : transactionStates.get(sendtid));
                // at this point, we should be down to 3 states
                if (state == null) {
                    if (consumers.length == 0 && !delayDelivery) {
                        // message
                        try {
                            consumers = routeLoadedTransactionMessage(pr);
                        } catch (Exception ex) {
                            logger.logStack(Logger.WARNING, br.getKString(br.W_EXCEPTION_ROUTE_LOADED_MSG, pr.getSysMessageID(), ex.getMessage()), ex);
                        }
                        if (consumers.length > 0) {
                            int[] states = new int[consumers.length];
                            for (int i = 0; i < states.length; i++) {
                                states[i] = PartitionedStore.INTEREST_STATE_ROUTED;
                            }
                            try {
                                pstore.storeInterestStates(getDestinationUID(), pr.getSysMessageID(), consumers, states, true, null);
                                pr.setStoredWithInterest(true);
                            } catch (Exception ex) {
                            // ok .. maybe weve already been routed
                            }
                        } else {
                            if (getDEBUG()) {
                                logger.log(Logger.INFO, "Message " + pr.getSysMessageID() + " [TUID=" + pr.getTransactionID() + ", " + this + "] no interest" + ", destroy...");
                            }
                            decrementDestinationSize(pr);
                            DL.removePacketList(pr.getSysMessageID(), pr.getDestinationUID(), pr);
                            pr.destroy();
                            continue;
                        }
                    }
                } else if (state.equals(Boolean.TRUE)) {
                    if (preparedTrans == null) {
                        preparedTrans = new LinkedHashMap();
                    }
                    preparedTrans.put(pr.getSysMessageID(), pr.getTransactionID());
                    dontRoute = true;
                } else {
                    // rolledback
                    if (getDEBUG()) {
                        logger.log(Logger.INFO, "Message " + pr.getSysMessageID() + " [TUID=" + pr.getTransactionID() + ", " + this + "] to be rolled back" + ", destroy...");
                    }
                    decrementDestinationSize(pr);
                    DL.removePacketList(pr.getSysMessageID(), pr.getDestinationUID(), pr);
                    pr.destroy();
                    continue;
                }
            }
            if (consumers.length == 0 && !dontRoute) {
                if (getDEBUG()) {
                    logger.log(Logger.INFO, "No consumer and dontRoute: Unrouted packet " + pr + ", " + this);
                }
                decrementDestinationSize(pr);
                DL.removePacketList(pr.getSysMessageID(), pr.getDestinationUID(), pr);
                pr.destroy();
                continue;
            }
            int[] states = new int[consumers.length];
            for (int i = 0; i < consumers.length; i++) {
                states[i] = pstore.getInterestState(getDestinationUID(), pr.getSysMessageID(), consumers[i]);
            }
            if (consumers.length > 0) {
                pr.update(consumers, states);
            }
            try {
                putMessage(pr, AddReason.LOADED);
            } catch (IllegalStateException | OutOfLimitsException ex) {
                String[] args = { pr.getSysMessageID().toString(), pr.getDestinationUID().toString(), ex.getMessage() };
                logger.logStack(Logger.WARNING, BrokerResources.W_CAN_NOT_LOAD_MSG, args, ex);
                continue;
            }
            ExpirationInfo ei = pr.getExpireInfo();
            if (ei != null && expireReaper != null) {
                expireReaper.addExpiringMessage(ei);
            }
            List<ConsumerUID> consumerList = Arrays.asList(consumers);
            // now, deal with consumer side transactions
            Map transCidToState = (Map) (preparedAcks == null ? null : preparedAcks.get(pr.getSysMessageID()));
            if (transCidToState != null) {
                // ok .. this isnt code focused on performance, but
                // its rarely called and only once
                // new a new list that allows itr.remove()
                consumerList = new ArrayList<>(consumerList);
                Iterator citr = consumerList.iterator();
                while (citr.hasNext()) {
                    ConsumerUID cuid = (ConsumerUID) citr.next();
                    TransactionUID tid = (TransactionUID) transCidToState.get(cuid);
                    Boolean state = Boolean.FALSE;
                    if (tid != null) {
                        state = (Boolean) (transactionStates == null ? null : transactionStates.get(tid));
                    }
                    // OK for committed transactions, acknowledge
                    if (state == null) {
                        if (getDEBUG()) {
                            logger.log(Logger.INFO, "Consumed message has committed state " + pr.getSysMessageID() + " [TUID=" + tid + ", " + this + "]" + ", consumer: " + cuid);
                        }
                        // acknowledge
                        if (pr.acknowledged(cuid, cuid, false, true)) {
                            if (committingTrans != null && committingTrans.get(tid) != null) {
                                unputMessage(pr, RemoveReason.ACKNOWLEDGED);
                            }
                            decrementDestinationSize(pr);
                            DL.removePacketList(pr.getSysMessageID(), pr.getDestinationUID(), pr);
                            pr.destroy();
                            if (getDEBUG()) {
                                logger.log(Logger.INFO, "Remove committed consumed message " + pr.getSysMessageID() + " [TUID=" + tid + ", " + this + "]" + ", consumer: " + cuid);
                            }
                        }
                        citr.remove();
                        continue;
                    } else if (state.equals(Boolean.TRUE)) {
                        // for prepared transactions, dont route
                        citr.remove();
                    } else if (state.equals(Boolean.FALSE)) {
                        // for rolled back transactions, do nothing
                        if (getDEBUG()) {
                            logger.log(Logger.INFO, "Redeliver message " + pr.getSysMessageID() + " [TUID=" + tid + ", " + this + "]" + " to consumer " + cuid);
                        }
                    }
                }
            // done processing acks
            }
            // dont recurse
            loaded = true;
            if (!dontRoute) {
                if (getDEBUG()) {
                    logger.log(Logger.INFO, "Route loaded message " + pr.getSysMessageID() + " [" + this + "]" + " to consumers " + consumerList);
                }
                if (di != null) {
                    forwardDeliveryDelayedMessage(new HashSet<>(consumerList), pr);
                } else {
                    routeLoadedMessage(pr, consumerList);
                }
            } else if (delayDelivery) {
                di.setDeliveryReady();
            }
        }
    } catch (Throwable ex) {
        String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_LOAD_DST_FAIL, getName());
        logger.logStack(Logger.ERROR, emsg, ex);
        loaded = true;
        unload(true);
        throw new BrokerException(emsg, ex);
    }
    destMessages.enforceLimits(enforceLimit);
    loaded = true;
    // clean up dead messages
    Iterator deaditr = deadMsgs.iterator();
    while (deaditr.hasNext()) {
        PacketReference pr = (PacketReference) deaditr.next();
        try {
            if (preparedTrans != null) {
                preparedTrans.remove(pr.getSysMessageID());
            }
            removeMessage(pr.getSysMessageID(), RemoveReason.EXPIRED);
        } catch (Exception ex) {
            logger.logStack(Logger.INFO, BrokerResources.E_INTERNAL_BROKER_ERROR, "Processing " + pr + " while loading destination " + this, ex);
        }
    }
    logger.log(Logger.INFO, br.getKString(br.I_LOADING_DEST_COMPLETE, toString(), String.valueOf(size)) + logsuffix);
    return preparedTrans;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) HAMonitorService(com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService) Packet(com.sun.messaging.jmq.io.Packet) DestinationNotFoundException(com.sun.messaging.jmq.jmsserver.util.DestinationNotFoundException) ConsumerAlreadyAddedException(com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) RefCompare(com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare) FaultInjection(com.sun.messaging.jmq.jmsserver.FaultInjection) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) DestinationNotFoundException(com.sun.messaging.jmq.jmsserver.util.DestinationNotFoundException)

Aggregations

HAMonitorService (com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)18 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)6 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)5 Packet (com.sun.messaging.jmq.io.Packet)4 SizeString (com.sun.messaging.jmq.util.SizeString)3 BrokerStateHandler (com.sun.messaging.jmq.jmsserver.BrokerStateHandler)2 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)2 ConnectionUID (com.sun.messaging.jmq.jmsserver.service.ConnectionUID)2 IMQConnection (com.sun.messaging.jmq.jmsserver.service.imq.IMQConnection)2 IMQService (com.sun.messaging.jmq.jmsserver.service.imq.IMQService)2 DestinationInfo (com.sun.messaging.jmq.util.admin.DestinationInfo)2 FailedLoginException (com.sun.messaging.jmq.auth.api.FailedLoginException)1 FaultInjection (com.sun.messaging.jmq.jmsserver.FaultInjection)1 AccessController (com.sun.messaging.jmq.jmsserver.auth.AccessController)1 AuthCacheData (com.sun.messaging.jmq.jmsserver.auth.AuthCacheData)1 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)1 Subscription (com.sun.messaging.jmq.jmsserver.core.Subscription)1 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)1 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)1 RefCompare (com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare)1