Search in sources :

Example 71 with TransactionUID

use of com.sun.messaging.jmq.jmsserver.data.TransactionUID 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

TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)71 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)23 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)22 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)19 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)18 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)15 JMQXid (com.sun.messaging.jmq.util.JMQXid)13 IOException (java.io.IOException)13 HashMap (java.util.HashMap)12 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)10 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)10 ArrayList (java.util.ArrayList)10 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)8 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)7 TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)7 ConnectionUID (com.sun.messaging.jmq.jmsserver.service.ConnectionUID)7 Iterator (java.util.Iterator)7 LinkedHashMap (java.util.LinkedHashMap)7 Map (java.util.Map)7 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)6