Search in sources :

Example 6 with TransactionList

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

the class RaptorProtocol method sendPreparedTransactionInquiries.

private void sendPreparedTransactionInquiries(List<TransactionUID> tidsonly, BrokerAddress broker, UID partitionID) {
    TransactionList[] tls = Globals.getDestinationList().getTransactionList(null);
    TransactionList tl = null;
    ArrayList tids = null;
    TransactionUID tid = null;
    ClusterTxnInquiryInfo cii = null;
    TransactionBroker txnhome = null;
    UID txnhomess = null;
    for (int i = 0; i < tls.length; i++) {
        tl = tls[i];
        if (tl == null) {
            continue;
        }
        tids = tl.getPreparedRemoteTransactions(null);
        Iterator itr = tids.iterator();
        while (itr.hasNext()) {
            tid = (TransactionUID) itr.next();
            if (tidsonly != null && !tidsonly.contains(tid)) {
                continue;
            }
            txnhome = tl.getRemoteTransactionHomeBroker(tid);
            if (broker == null && txnhome == null) {
                continue;
            }
            txnhomess = null;
            if (txnhome != null) {
                txnhomess = txnhome.getBrokerAddress().getStoreSessionUID();
            }
            if (partitionID == null || txnhomess == null || txnhomess.equals(partitionID)) {
                cii = ClusterTxnInquiryInfo.newInstance(Long.valueOf(tid.longValue()), ((txnhome == null) ? null : txnhome.getCurrentBrokerAddress()), null);
                if (DEBUG_CLUSTER_TXN) {
                    logger.log(Logger.INFO, "Sending transaction inquiry: " + cii + " to " + broker);
                }
                BrokerAddress tobroker = (broker == null ? txnhome.getCurrentBrokerAddress() : broker);
                try {
                    c.unicast(tobroker, cii.getGPacket());
                } catch (Exception e) {
                    logger.log(Logger.WARNING, "Sending transaction inquiry " + cii + " to " + tobroker + " failed");
                }
            }
        }
    }
}
Also used : TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) ConnectionUID(com.sun.messaging.jmq.jmsserver.service.ConnectionUID) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) TransactionBroker(com.sun.messaging.jmq.jmsserver.data.TransactionBroker) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList) HandshakeInProgressException(com.sun.messaging.jmq.jmsserver.multibroker.HandshakeInProgressException)

Example 7 with TransactionList

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

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

the class DestinationList method init.

public static void init() throws BrokerException {
    if (inited) {
        if (shutdown) {
            throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_SHUTTING_DOWN_BROKER));
        }
        return;
    }
    shutdown = false;
    inited = true;
    if (canAutoCreate(true)) {
        Globals.getLogger().log(Logger.INFO, BrokerResources.I_QUEUE_AUTOCREATE_ENABLED);
    }
    if (!canAutoCreate(false)) {
        Globals.getLogger().log(Logger.INFO, BrokerResources.I_TOPIC_AUTOCREATE_DISABLED);
    }
    BrokerConfig cfg = Globals.getConfig();
    // OK add listeners for the properties
    cfg.addListener(SYSTEM_MAX_SIZE, cl);
    cfg.addListener(SYSTEM_MAX_COUNT, cl);
    cfg.addListener(MAX_MESSAGE_SIZE, cl);
    cfg.addListener(AUTO_QUEUE_STR, cl);
    cfg.addListener(AUTO_TOPIC_STR, cl);
    cfg.addListener(DST_REAP_STR, cl);
    cfg.addListener(MSG_REAP_STR, cl);
    cfg.addListener(AUTO_MAX_NUM_MSGS, cl);
    cfg.addListener(AUTO_MAX_TOTAL_BYTES, cl);
    cfg.addListener(AUTO_MAX_BYTES_MSG, cl);
    cfg.addListener(AUTO_MAX_NUM_PRODUCERS, cl);
    cfg.addListener(AUTO_LOCAL_ONLY, cl);
    cfg.addListener(AUTO_LIMIT_BEHAVIOR, cl);
    cfg.addListener(USE_DMQ_STR, cl);
    cfg.addListener(TRUNCATE_BODY_STR, cl);
    cfg.addListener(LOG_MSGS_STR, cl);
    cfg.addListener(DEBUG_LISTS_PROP, cl);
    cfg.addListener(CHECK_MSGS_RATE_AT_DEST_CAPACITY_RATIO_PROP, cl);
    cfg.addListener(CHECK_MSGS_RATE_FOR_ALL_PROP, cl);
    // now configure the system based on the properties
    setMaxSize(cfg.getSizeProperty(SYSTEM_MAX_SIZE));
    setMaxMessages(cfg.getIntProperty(SYSTEM_MAX_COUNT));
    setIndividualMessageMax(cfg.getSizeProperty(MAX_MESSAGE_SIZE));
    Queue.init();
    if (Globals.getStore().getPartitionModeEnabled()) {
        try {
            String typ = cfg.getProperty(CONN_STRATEGY_PROP, CONN_STRATEGY_DEFAULT);
            String cl = null;
            if (typ.equalsIgnoreCase(MIN_CONN_STRATEGY)) {
                cl = MIN_CONN_STRATEGY_CLASS;
            } else if (typ.equalsIgnoreCase(RR_CONN_STRATEGY)) {
                cl = RR_CONN_STRATEGY_CLASS;
            } else {
                Globals.getLogger().log(Logger.WARNING, "XXXIngore unknown " + typ + " for " + CONN_STRATEGY_PROP);
                cl = MIN_CONN_STRATEGY_CLASS;
            }
            if (Globals.isNucleusManagedBroker()) {
                partitionStrategy = Globals.getHabitat().getService(ConnToPartitionStrategy.class, cl);
            } else {
                partitionStrategy = (ConnToPartitionStrategy) Class.forName(cl).getDeclaredConstructor().newInstance();
            }
        } catch (Exception e) {
            throw new BrokerException(e.getMessage(), e);
        }
    }
    DestinationList dl = null;
    List<PartitionedStore> partitions = Globals.getStore().getAllStorePartitions();
    if (Globals.getStore().getPartitionModeEnabled()) {
        partitionMode = true;
        if (Globals.getStore().isPartitionMigratable()) {
            partitionMigratable = true;
        }
    } else if (partitions.size() > 1) {
        BrokerException ex = new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.E_INTERNAL_BROKER_ERROR, "Unexpected " + partitions.size() + "store partitions when partition mode disabled"));
        Globals.getLogger().logStack(Logger.ERROR, ex.getMessage(), ex);
        throw ex;
    }
    partitionModeInited = true;
    PartitionedStore partition = null;
    Iterator<PartitionedStore> itr1 = partitions.iterator();
    while (itr1.hasNext()) {
        partition = itr1.next();
        dl = new DestinationList(partition);
        destinationListList.put(partition, dl);
    }
    TransactionList tl = null;
    Iterator<DestinationList> itr2 = destinationListList.values().iterator();
    while (itr2.hasNext()) {
        dl = itr2.next();
        tl = new TransactionList(dl);
        dl.setTransactionList(tl);
    }
    Iterator<DestinationList> itr = destinationListList.values().iterator();
    while (itr.hasNext()) {
        dl = itr.next();
        dl.loadDestinations();
        addPartitionListener(dl.getTransactionList());
        notifyPartitionAdded(dl.getPartitionedStore(), dl);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) 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) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Example 9 with TransactionList

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

the class DestinationList method addStorePartition.

public static void addStorePartition(PartitionedStore ps, boolean errdup) throws BrokerException {
    acquirePartitionLock(false);
    try {
        synchronized (destinationListList) {
            if (destinationListList.get(ps) != null) {
                String emsg = "Partition " + ps + " has already been loaded";
                if (errdup) {
                    throw new BrokerException(emsg);
                }
                Globals.getLogger().log(Logger.INFO, emsg);
                return;
            }
            DestinationList dl = new DestinationList(ps);
            TransactionList tl = new TransactionList(dl);
            dl.setTransactionList(tl);
            dl.loadDestinations();
            Subscription.initDuraSubscriptions(dl);
            Subscription.initNonDuraSharedSubscriptions(dl);
            Consumer.attachConsumers(dl);
            destinationListList.put(ps, dl);
            tl.postProcess();
            addPartitionListener(tl);
            notifyPartitionAdded(ps, dl);
        }
    } finally {
        releasePartitionLock(false);
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList)

Example 10 with TransactionList

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

the class SessionOp method close.

@Override
public void close(Connection conn) {
    TransactionList[] tls = DL.getTransactionList(((IMQConnection) conn).getPartitionedStore());
    TransactionList translist = tls[0];
    // deal w/ old messages
    synchronized (deliveredMessages) {
        if (!deliveredMessages.isEmpty()) {
            // get the list by IDs
            HashMap openMsgs = new HashMap();
            Iterator itr = deliveredMessages.entrySet().iterator();
            while (itr.hasNext()) {
                Map.Entry entry = (Map.Entry) itr.next();
                ackEntry e = (ackEntry) entry.getValue();
                ConsumerUID cuid = e.getConsumerUID();
                ConsumerUID storeduid = (e.getStoredUID() == null ? cuid : e.getStoredUID());
                // deal w/ orphan messages
                TransactionUID tid = e.getTUID();
                if (tid != null) {
                    JMQXid jmqxid = translist.UIDToXid(tid);
                    if (jmqxid != null) {
                        translist.addOrphanAck(tid, e.getSysMessageID(), storeduid, cuid);
                        itr.remove();
                        continue;
                    }
                    TransactionState ts = translist.retrieveState(tid, true);
                    if (ts != null && ts.getState() == TransactionState.PREPARED) {
                        translist.addOrphanAck(tid, e.getSysMessageID(), storeduid, cuid);
                        itr.remove();
                        continue;
                    }
                    if (ts != null && ts.getState() == TransactionState.COMMITTED) {
                        itr.remove();
                        continue;
                    }
                    if (ts != null && ts.getState() == TransactionState.COMPLETE && conn.getConnectionState() >= Connection.STATE_CLOSED) {
                        String[] args = { "" + tid, TransactionState.toString(ts.getState()), session.getConnectionUID().toString() };
                        logger.log(Logger.INFO, Globals.getBrokerResources().getKString(BrokerResources.I_CONN_CLEANUP_KEEP_TXN, args));
                        translist.addOrphanAck(tid, e.getSysMessageID(), storeduid, cuid);
                        itr.remove();
                        continue;
                    }
                }
                PacketReference ref = e.getReference();
                if (ref == null) {
                    // PART
                    ref = DL.get(null, e.getSysMessageID());
                }
                if (ref != null && !ref.isLocal()) {
                    itr.remove();
                    try {
                        if ((ref = e.acknowledged(false)) != null) {
                            try {
                                Destination d = ref.getDestination();
                                d.removeRemoteMessage(ref.getSysMessageID(), RemoveReason.ACKNOWLEDGED, ref);
                            } finally {
                                ref.postAcknowledgedRemoval();
                            }
                        }
                    } catch (Exception ex) {
                        logger.logStack(session.DEBUG_CLUSTER_MSG ? Logger.WARNING : Logger.DEBUG, "Unable to clean up remote message " + e.getDebugMessage(false), ex);
                    }
                    continue;
                }
                // we arent in a transaction ID .. cool
                // add to redeliver list
                Set s = (Set) openMsgs.get(cuid);
                if (s == null) {
                    s = new LinkedHashSet();
                    openMsgs.put(cuid, s);
                }
                if (ref != null) {
                    ref.removeInDelivery(storeduid);
                }
                s.add(e);
            }
            // OK .. see if we ack or cleanup
            itr = openMsgs.entrySet().iterator();
            Map.Entry pair = null;
            while (itr.hasNext()) {
                pair = (Map.Entry) itr.next();
                ConsumerUID cuid = (ConsumerUID) pair.getKey();
                Map parentmp = (Map) cleanupList.get(cuid);
                ConsumerUID suid = (ConsumerUID) storeMap.get(cuid);
                if (parentmp == null || parentmp.size() == 0) {
                    Set s = (Set) pair.getValue();
                    Iterator sitr = s.iterator();
                    while (sitr.hasNext()) {
                        ackEntry e = (ackEntry) sitr.next();
                        try {
                            PacketReference ref = e.acknowledged(false);
                            if (ref != null) {
                                try {
                                    Destination d = ref.getDestination();
                                    try {
                                        if (ref.isLocal()) {
                                            d.removeMessage(ref.getSysMessageID(), RemoveReason.ACKNOWLEDGED);
                                        } else {
                                            d.removeRemoteMessage(ref.getSysMessageID(), RemoveReason.ACKNOWLEDGED, ref);
                                        }
                                    } catch (Exception ex) {
                                        Object[] args = { ref, this, ex.getMessage() };
                                        logger.logStack(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.X_CLEANUP_MSG_CLOSE_SESSION, args), ex);
                                    }
                                } finally {
                                    ref.postAcknowledgedRemoval();
                                }
                            }
                        } catch (Exception ex) {
                        // ignore
                        }
                    }
                } else {
                    Map mp = new LinkedHashMap();
                    Set msgs = null;
                    PartitionedStore ps = null;
                    Set s = (Set) openMsgs.get(cuid);
                    Iterator sitr = s.iterator();
                    while (sitr.hasNext()) {
                        ackEntry e = (ackEntry) sitr.next();
                        PacketReference ref = e.getReference();
                        if (ref != null) {
                            try {
                                if (!e.hasMarkConsumed()) {
                                    ref.consumed(suid, !session.isUnsafeAck(cuid), session.isAutoAck(cuid));
                                }
                            } catch (Exception ex) {
                                logger.logStack(Logger.WARNING, "Unable to consume " + suid + ":" + ref, ex);
                            }
                            ps = ref.getPartitionedStore();
                            if (!ref.getDestinationUID().isQueue()) {
                                ps = new NoPersistPartitionedStoreImpl(suid);
                            }
                            msgs = (Set) mp.get(ps);
                            if (msgs == null) {
                                msgs = new LinkedHashSet();
                                mp.put(ps, msgs);
                            }
                            msgs.add(ref);
                        } else {
                            sitr.remove();
                        }
                    }
                    SubSet pl = null;
                    itr = mp.entrySet().iterator();
                    pair = null;
                    while (itr.hasNext()) {
                        pair = (Map.Entry) itr.next();
                        ps = (PartitionedStore) pair.getKey();
                        pl = (SubSet) parentmp.get(ps);
                        if (pl != null) {
                            ((Prioritized) pl).addAllOrdered((Set) pair.getValue());
                        } else {
                            logger.log(logger.WARNING, "Message(s) " + mp.get(ps) + "[" + suid + ", " + cuid + "] parentlist not found on session closing");
                        }
                    }
                }
            }
            deliveredMessages.clear();
            cleanupList.clear();
            storeMap.clear();
        }
    }
    if (!session.isXATransacted()) {
        Iterator itr = null;
        synchronized (detachedRConsumerUIDs) {
            itr = (new LinkedHashSet(detachedRConsumerUIDs)).iterator();
        }
        while (itr.hasNext()) {
            Consumer c = Consumer.newInstance((ConsumerUID) itr.next());
            try {
                Globals.getClusterBroadcast().destroyConsumer(c, null, true);
            } catch (Exception e) {
                logger.log(Logger.WARNING, "Unable to send consumer [" + c + "] cleanup notification for closing of SessionOp[" + this + "].");
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Iterator(java.util.Iterator) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList) JMQXid(com.sun.messaging.jmq.util.JMQXid) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) NoPersistPartitionedStoreImpl(com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)48 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)25 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)23 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)23 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)10 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)9 DestinationList (com.sun.messaging.jmq.jmsserver.core.DestinationList)9 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)8 TransactionHandler (com.sun.messaging.jmq.jmsserver.data.handlers.TransactionHandler)8 List (java.util.List)8 PartitionedStore (com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)7 IOException (java.io.IOException)7 TransactionBroker (com.sun.messaging.jmq.jmsserver.data.TransactionBroker)6 CacheHashMap (com.sun.messaging.jmq.util.CacheHashMap)6 JMQXid (com.sun.messaging.jmq.util.JMQXid)6 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)6 Iterator (java.util.Iterator)6 Map (java.util.Map)6 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)5