Search in sources :

Example 1 with RefCompare

use of com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare in project openmq by eclipse-ee4j.

the class DestinationList method loadTakeoverMsgs.

public static synchronized void loadTakeoverMsgs(PartitionedStore storep, Map<String, String> msgs, List txns, Map txacks) throws BrokerException {
    DestinationList dl = destinationListList.get(storep);
    Map m = new HashMap();
    Logger logger = Globals.getLogger();
    Map ackLookup = new HashMap();
    // ok create a hashtable for looking up txns
    if (txacks != null) {
        Iterator itr = txacks.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();
            TransactionUID tuid = (TransactionUID) entry.getKey();
            List l = (List) entry.getValue();
            Iterator litr = l.iterator();
            while (litr.hasNext()) {
                TransactionAcknowledgement ta = (TransactionAcknowledgement) litr.next();
                String key = ta.getSysMessageID() + ":" + ta.getStoredConsumerUID();
                ackLookup.put(key, tuid);
            }
        }
    }
    // Alright ...
    // all acks fail once takeover begins
    // we expect all transactions to rollback
    // here is the logic:
    // - load all messages
    // - remove any messages in open transactions
    // - requeue all messages
    // - resort (w/ load comparator)
    // 
    // 
    // OK, first get msgs and sort by destination
    HashMap openMessages = new HashMap();
    Iterator itr = msgs.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry me = (Map.Entry) itr.next();
        String msgID = (String) me.getKey();
        String dst = (String) me.getValue();
        DestinationUID dUID = new DestinationUID(dst);
        Packet p = null;
        try {
            p = storep.getMessage(dUID, msgID);
        } catch (BrokerException ex) {
            Throwable cause = ex.getCause();
            if (cause instanceof InvalidPacketException) {
                String[] args = { msgID, dst, cause.toString() };
                String emsg = Globals.getBrokerResources().getKString(BrokerResources.X_MSG_CORRUPTED_IN_STORE, args);
                logger.logStack(Logger.ERROR, emsg, ex);
                handleInvalidPacket(msgID, dst, emsg, (InvalidPacketException) cause, storep);
                itr.remove();
                continue;
            }
            // Check if dst even exists!
            if (ex.getStatusCode() == Status.NOT_FOUND) {
                Destination[] ds = getDestination(storep, dUID);
                Destination d = ds[0];
                if (d == null) {
                    String[] args = { msgID, dst, Globals.getBrokerResources().getString(BrokerResources.E_DESTINATION_NOT_FOUND_IN_STORE, dst) };
                    logger.log(Logger.ERROR, BrokerResources.W_CAN_NOT_LOAD_MSG, args, ex);
                }
            }
            throw ex;
        }
        dUID = DestinationUID.getUID(p.getDestination(), p.getIsQueue());
        PacketReference pr = PacketReference.createReference(storep, p, dUID, null);
        // mark already stored and make packet a SoftReference to
        // prevent running out of memory if dest has lots of msgs
        pr.setLoaded();
        logger.log(Logger.DEBUG, "Loading message " + pr.getSysMessageID() + " on " + pr.getDestinationUID());
        // check transactions
        TransactionUID tid = pr.getTransactionID();
        if (tid != null) {
            // see if in transaction list
            if (txns.contains(tid)) {
                // open transaction
                TransactionState ts = dl.getTransactionList().retrieveState(pr.getTransactionID());
                if (ts != null && ts.getState() != TransactionState.ROLLEDBACK && ts.getState() != TransactionState.COMMITTED) {
                    // in transaction ...
                    logger.log(Logger.DEBUG, "Processing open transacted message " + pr.getSysMessageID() + " on " + tid + "[" + TransactionState.toString(ts.getState()) + "]");
                    openMessages.put(pr.getSysMessageID(), tid);
                } else if (ts != null && ts.getState() == TransactionState.ROLLEDBACK) {
                    pr.destroy();
                    continue;
                } else {
                }
            }
        }
        dl.packetlistAdd(pr.getSysMessageID(), pr.getDestinationUID(), null);
        Set l = null;
        if ((l = (Set) m.get(dUID)) == null) {
            l = new TreeSet(new RefCompare());
            m.put(dUID, l);
        }
        l.add(pr);
    }
    // OK, handle determining how to queue the messages
    Map<PacketReference, MessageDeliveryTimeInfo> deliveryDelays = new HashMap<>();
    // first add all messages
    Iterator dsts = m.entrySet().iterator();
    while (dsts.hasNext()) {
        Map.Entry entry = (Map.Entry) dsts.next();
        DestinationUID dst = (DestinationUID) entry.getKey();
        Set l = (Set) entry.getValue();
        Destination[] ds = getDestination(storep, dst);
        Destination d = ds[0];
        if (d == null) {
            // create it
            String destinationName = dst.getName();
            try {
                ds = getDestination(storep, destinationName, (dst.isQueue() ? DestType.DEST_TYPE_QUEUE : DestType.DEST_TYPE_TOPIC), true, true);
                d = ds[0];
            } catch (IOException ex) {
                throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_CANT_LOAD_DEST, destinationName));
            }
        } else {
            synchronized (d) {
                if (d.isLoaded()) {
                    // Destination has already been loaded so just called
                    // initialize() to update the size and bytes variables
                    d.initialize();
                }
                d.load(l);
            }
        }
        logger.log(Logger.INFO, BrokerResources.I_LOADING_DST, d.getName(), String.valueOf(l.size()));
        MessageDeliveryTimeTimer dt = d.deliveryTimeTimer;
        if (dt == null && !d.isDMQ()) {
            if (!d.isValid()) {
                String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_UNABLE_LOAD_TAKEOVER_MSGS_TO_DESTROYED_DST, d.getDestinationUID());
                logger.log(Logger.WARNING, emsg);
                continue;
            }
            String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_UNABLE_LOAD_TAKEOVER_MSGS_NO_DST_DELIVERY_TIMER, d.getDestinationUID() + "[" + d.isValid() + "]");
            logger.log(Logger.WARNING, emsg);
            continue;
        }
        // now we're sorted, process
        Iterator litr = l.iterator();
        try {
            MessageDeliveryTimeInfo di = null;
            while (litr.hasNext()) {
                PacketReference pr = (PacketReference) litr.next();
                di = pr.getDeliveryTimeInfo();
                if (di != null) {
                    dt.removeMessage(di);
                }
                try {
                    // ok allow overrun
                    boolean el = d.destMessages.getEnforceLimits();
                    d.destMessages.enforceLimits(false);
                    if (DEBUG) {
                        logger.log(logger.INFO, "Put message " + pr + "[" + di + "] to destination " + d);
                    }
                    pr.lock();
                    d.acquireQueueRemoteLock();
                    try {
                        d.putMessage(pr, AddReason.LOADED, true);
                    } finally {
                        d.clearQueueRemoteLock();
                    }
                    // turn off overrun
                    d.destMessages.enforceLimits(el);
                } catch (IllegalStateException | OutOfLimitsException ex) {
                    // thats ok, we already exists
                    String[] args = { pr.getSysMessageID().toString(), pr.getDestinationUID().toString(), ex.getMessage() };
                    logger.logStack(Logger.WARNING, BrokerResources.W_CAN_NOT_LOAD_MSG, args, ex);
                    continue;
                } finally {
                    if (di != null && !di.isDeliveryDue()) {
                        dt.addMessage(di);
                        deliveryDelays.put(pr, di);
                    }
                }
            }
            // then resort the destination
            d.sort(new RefCompare());
        } catch (Exception ex) {
        }
    }
    // now route
    dsts = m.entrySet().iterator();
    while (dsts.hasNext()) {
        Map.Entry entry = (Map.Entry) dsts.next();
        DestinationUID dst = (DestinationUID) entry.getKey();
        Set l = (Set) entry.getValue();
        Destination d = dl.getDestination(dst);
        // now we're sorted, process
        Iterator litr = l.iterator();
        try {
            while (litr.hasNext()) {
                PacketReference pr = (PacketReference) litr.next();
                if (DEBUG) {
                    logger.log(logger.INFO, "Process takeover message " + pr + "[" + pr.getDeliveryTimeInfo() + "] for destination " + d);
                }
                TransactionUID tuid = (TransactionUID) openMessages.get(pr.getSysMessageID());
                if (tuid != null) {
                    dl.getTransactionList().addMessage(tuid, pr.getSysMessageID(), true);
                    pr.unlock();
                    continue;
                }
                ConsumerUID[] consumers = storep.getConsumerUIDs(dst, pr.getSysMessageID());
                if (consumers == null) {
                    consumers = new ConsumerUID[0];
                }
                if (consumers.length == 0 && storep.hasMessageBeenAcked(dst, pr.getSysMessageID())) {
                    logger.log(Logger.INFO, Globals.getBrokerResources().getString(BrokerResources.W_TAKEOVER_MSG_ALREADY_ACKED, pr.getSysMessageID()));
                    d.unputMessage(pr, RemoveReason.ACKNOWLEDGED);
                    pr.destroy();
                    pr.unlock();
                    continue;
                }
                if (consumers.length > 0) {
                    pr.setStoredWithInterest(true);
                } else {
                    pr.setStoredWithInterest(false);
                }
                int[] states = null;
                if (consumers.length == 0 && deliveryDelays.get(pr) == null) {
                    // message
                    try {
                        consumers = d.routeLoadedTransactionMessage(pr);
                    } catch (Exception ex) {
                        logger.logStack(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.W_EXCEPTION_ROUTE_LOADED_MSG, pr.getSysMessageID(), ex.getMessage()), ex);
                    }
                    states = new int[consumers.length];
                    for (int i = 0; i < states.length; i++) {
                        states[i] = PartitionedStore.INTEREST_STATE_ROUTED;
                    }
                    try {
                        storep.storeInterestStates(d.getDestinationUID(), pr.getSysMessageID(), consumers, states, true, null);
                        pr.setStoredWithInterest(true);
                    } catch (Exception ex) {
                        // message already routed
                        StringBuilder debuf = new StringBuilder();
                        for (int i = 0; i < consumers.length; i++) {
                            if (i > 0) {
                                debuf.append(", ");
                            }
                            debuf.append(consumers[i]);
                        }
                        logger.log(logger.WARNING, BrokerResources.W_TAKEOVER_MSG_ALREADY_ROUTED, pr.getSysMessageID(), debuf.toString(), ex);
                    }
                } else if (consumers.length > 0) {
                    states = new int[consumers.length];
                    for (int i = 0; i < consumers.length; i++) {
                        states[i] = storep.getInterestState(dst, pr.getSysMessageID(), consumers[i]);
                    }
                }
                pr.update(consumers, states, false);
                // OK deal w/ transsactions
                // LKS - XXX
                ExpirationInfo ei = pr.getExpireInfo();
                if (ei != null && d.expireReaper != null) {
                    d.expireReaper.addExpiringMessage(ei);
                }
                List<ConsumerUID> consumerList = new ArrayList(Arrays.asList(consumers));
                // OK ... see if we are in txn
                Iterator citr = consumerList.iterator();
                while (citr.hasNext()) {
                    logger.log(Logger.DEBUG, " Message " + pr.getSysMessageID() + " has " + consumerList.size() + " consumers ");
                    ConsumerUID cuid = (ConsumerUID) citr.next();
                    String key = pr.getSysMessageID() + ":" + cuid;
                    TransactionList tl = dl.getTransactionList();
                    TransactionUID tid = (TransactionUID) ackLookup.get(key);
                    if (DEBUG) {
                        logger.log(logger.INFO, "loadTakeoverMsgs: lookup " + key + " found tid=" + tid);
                    }
                    if (tid != null) {
                        boolean remote = false;
                        TransactionState ts = tl.retrieveState(tid);
                        if (ts == null) {
                            ts = tl.getRemoteTransactionState(tid);
                            remote = true;
                        }
                        if (DEBUG) {
                            logger.log(logger.INFO, "tid=" + tid + " has state=" + TransactionState.toString(ts.getState()));
                        }
                        if (ts != null && ts.getState() != TransactionState.ROLLEDBACK && ts.getState() != TransactionState.COMMITTED) {
                            // in transaction ...
                            if (DEBUG) {
                                logger.log(Logger.INFO, "loadTakeoverMsgs: Open transaction ack [" + key + "]" + (remote ? "remote" : "") + ", TUID=" + tid);
                            }
                            if (!remote) {
                                try {
                                    tl.addAcknowledgement(tid, pr.getSysMessageID(), cuid, cuid, true, false);
                                } catch (TransactionAckExistException e) {
                                    // can happen if takeover tid's remote txn after restart
                                    // then txn ack would have already been loaded
                                    logger.log(Logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_TAKINGOVER_TXN_ACK_ALREADY_EXIST, "[" + pr.getSysMessageID() + "]" + cuid + ":" + cuid, tid + "[" + TransactionState.toString(ts.getState()) + "]"));
                                }
                                tl.addOrphanAck(tid, pr.getSysMessageID(), cuid);
                            }
                            citr.remove();
                            logger.log(Logger.INFO, "Processing open ack " + pr.getSysMessageID() + ":" + cuid + " on " + tid);
                            continue;
                        } else if (ts != null && ts.getState() == TransactionState.COMMITTED) {
                            logger.log(Logger.INFO, "Processing committed ack " + pr.getSysMessageID() + ":" + cuid + " on " + tid);
                            if (pr.acknowledged(cuid, cuid, false, true)) {
                                d.unputMessage(pr, RemoveReason.ACKNOWLEDGED);
                                pr.destroy();
                                continue;
                            }
                            citr.remove();
                            continue;
                        }
                    }
                }
                // route msgs not in transaction
                if (DEBUG) {
                    StringBuilder buf = new StringBuilder();
                    ConsumerUID cid = null;
                    for (int j = 0; j < consumerList.size(); j++) {
                        cid = consumerList.get(j);
                        buf.append(cid);
                        buf.append(' ');
                    }
                    if (deliveryDelays.get(pr) == null) {
                        logger.log(Logger.INFO, "non-transacted: Routing Message " + pr.getSysMessageID() + " to " + consumerList.size() + " consumers:" + buf.toString());
                    } else {
                        logger.log(Logger.INFO, "non-transacted: deliver time not arrived for message " + pr.getSysMessageID());
                    }
                }
                pr.unlock();
                if (deliveryDelays.get(pr) == null) {
                    if (DEBUG) {
                        logger.log(logger.INFO, "Route takeover message " + pr + "[" + pr.getDeliveryTimeInfo() + "] for destination " + d + " to consumers " + consumerList);
                    }
                    if (pr.getDeliveryTimeInfo() != null) {
                        d.forwardDeliveryDelayedMessage(new HashSet<>(consumerList), pr);
                    } else {
                        d.routeLoadedMessage(pr, consumerList);
                    }
                } else {
                    MessageDeliveryTimeInfo di = pr.getDeliveryTimeInfo();
                    di.setDeliveryReady();
                }
                if (d.destReaper != null) {
                    d.destReaper.cancel();
                    d.destReaper = null;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) Logger(com.sun.messaging.jmq.util.log.Logger) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList) Packet(com.sun.messaging.jmq.io.Packet) TransactionAcknowledgement(com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList) InvalidSysMessageIDException(com.sun.messaging.jmq.io.InvalidSysMessageIDException) PartitionNotFoundException(com.sun.messaging.jmq.jmsserver.util.PartitionNotFoundException) ConflictException(com.sun.messaging.jmq.jmsserver.util.ConflictException) LoadException(com.sun.messaging.jmq.jmsserver.persist.api.LoadException) TransactionAckExistException(com.sun.messaging.jmq.jmsserver.util.TransactionAckExistException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException) InvalidPacketException(com.sun.messaging.jmq.io.InvalidPacketException) RefCompare(com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) TransactionAckExistException(com.sun.messaging.jmq.jmsserver.util.TransactionAckExistException)

Example 2 with RefCompare

use of com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare in project openmq by eclipse-ee4j.

the class Destination method getSysMessageIDs.

public SysMessageID[] getSysMessageIDs(Long startMsgIndex, Long maxMsgsRetrieved) throws BrokerException {
    SysMessageID[] ids = new SysMessageID[0];
    String errMsg;
    if (!loaded) {
        load();
    }
    /*
         * Check/Setup array params
         */
    long numMsgs = destMessages.size();
    /*
         * If destination is empty, return empty array.
         */
    if (numMsgs == 0) {
        return (ids);
    }
    if (startMsgIndex == null) {
        startMsgIndex = Long.valueOf(0);
    } else if ((startMsgIndex.longValue() < 0) || (startMsgIndex.longValue() > (numMsgs - 1))) {
        errMsg = " Start message index needs to be in between 0 and " + (numMsgs - 1);
        throw new BrokerException(errMsg);
    }
    if (maxMsgsRetrieved == null) {
        maxMsgsRetrieved = Long.valueOf(numMsgs - startMsgIndex.longValue());
    } else if (maxMsgsRetrieved.longValue() < 0) {
        errMsg = " Max number of messages retrieved value needs to be greater than 0.";
        throw new BrokerException(errMsg);
    }
    long maxIndex = startMsgIndex.longValue() + maxMsgsRetrieved.longValue();
    SortedSet s = new TreeSet(new RefCompare());
    try {
        Set<PacketReference> msgset = null;
        synchronized (destMessages) {
            msgset = new HashSet<>(destMessages.values());
        }
        Iterator<PacketReference> itr = msgset.iterator();
        while (itr.hasNext()) {
            PacketReference pr = itr.next();
            s.add(pr);
        }
    } catch (Throwable ex) {
        logger.log(Logger.DEBUG, "Error getting msg IDs ", ex);
    }
    ArrayList idsAl = new ArrayList();
    long i = 0;
    Iterator itr = s.iterator();
    while (itr.hasNext()) {
        PacketReference pr = (PacketReference) itr.next();
        if ((i >= startMsgIndex.longValue()) && (i < maxIndex)) {
            SysMessageID id = pr.getSysMessageID();
            idsAl.add(id);
        }
        if (i >= maxIndex) {
            break;
        }
        ++i;
    }
    ids = (SysMessageID[]) idsAl.toArray(ids);
    return (ids);
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) RefCompare(com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare) SysMessageID(com.sun.messaging.jmq.io.SysMessageID)

Example 3 with RefCompare

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

RefCompare (com.sun.messaging.jmq.jmsserver.data.handlers.RefCompare)3 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)3 SizeString (com.sun.messaging.jmq.util.SizeString)3 Packet (com.sun.messaging.jmq.io.Packet)2 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)2 InvalidPacketException (com.sun.messaging.jmq.io.InvalidPacketException)1 InvalidSysMessageIDException (com.sun.messaging.jmq.io.InvalidSysMessageIDException)1 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)1 FaultInjection (com.sun.messaging.jmq.jmsserver.FaultInjection)1 HAMonitorService (com.sun.messaging.jmq.jmsserver.cluster.api.ha.HAMonitorService)1 TransactionAcknowledgement (com.sun.messaging.jmq.jmsserver.data.TransactionAcknowledgement)1 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)1 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)1 LoadException (com.sun.messaging.jmq.jmsserver.persist.api.LoadException)1 ConflictException (com.sun.messaging.jmq.jmsserver.util.ConflictException)1 ConsumerAlreadyAddedException (com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException)1 DestinationNotFoundException (com.sun.messaging.jmq.jmsserver.util.DestinationNotFoundException)1 PartitionNotFoundException (com.sun.messaging.jmq.jmsserver.util.PartitionNotFoundException)1 TransactionAckExistException (com.sun.messaging.jmq.jmsserver.util.TransactionAckExistException)1 Logger (com.sun.messaging.jmq.util.log.Logger)1