Search in sources :

Example 1 with NoPersistPartitionedStoreImpl

use of com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl in project openmq by eclipse-ee4j.

the class TransactionList method getTransListByPartitionID.

/**
 * @param pid partition id
 * @return null if not found
 */
public static TransactionList getTransListByPartitionID(UID pid) {
    TransactionList tl = null;
    PartitionedStore ps = new NoPersistPartitionedStoreImpl(pid);
    TransactionList[] tls = Globals.getDestinationList().getTransactionList(ps);
    tl = tls[0];
    return tl;
}
Also used : NoPersistPartitionedStoreImpl(com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Example 2 with NoPersistPartitionedStoreImpl

use of com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl in project openmq by eclipse-ee4j.

the class DestinationList method movePartition.

public static void movePartition(UID partitionID, String brokerID) throws BrokerException {
    if (!isPartitionMigratable()) {
        throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_PARTITION_NOT_MIGRATABLE), Status.PRECONDITION_FAILED);
    }
    PartitionedStore pstore = null;
    synchronized (destinationListList) {
        pstore = new NoPersistPartitionedStoreImpl(partitionID);
        DestinationList dl = destinationListList.get(pstore);
        if (dl == null) {
            throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_PARTITION_NOT_FOUND, partitionID), Status.PRECONDITION_FAILED);
        }
        if (dl.getPartitionedStore().isPrimaryPartition()) {
            throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_MIGRATE_PRIMARY_PARTITION_NOT_ALLOWED, partitionID), Status.NOT_ALLOWED);
        }
        dl.valid = false;
        notifyPartitionRemoved(pstore, dl, brokerID);
        pstore = dl.getPartitionedStore();
        dl.closeAttachedConnections(GoodbyeReason.MIGRATE_PARTITION, "XXXAdmin request to move partition: " + partitionID);
        destinationListList.remove(pstore);
        Consumer con = null;
        Iterator itr = Consumer.getAllConsumers(true);
        while (itr.hasNext()) {
            con = (Consumer) itr.next();
            con.setParentList(pstore, null);
        }
        Destination d = null;
        itr = dl.getDestinations(null, ALL_DESTINATIONS_MASK);
        while (itr.hasNext()) {
            d = (Destination) itr.next();
            d.unload(true, true);
        }
        pstore.close();
        Globals.getStore().partitionDeparture(partitionID, brokerID);
        dl.clearDestinations();
    }
}
Also used : NoPersistPartitionedStoreImpl(com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Example 3 with NoPersistPartitionedStoreImpl

use of com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl in project openmq by eclipse-ee4j.

the class Consumer method destroyConsumer.

public void destroyConsumer(Set delivered, Map remotePendings, boolean remoteCleanup, boolean destroyingDest, boolean notify) {
    if (DEBUG) {
        logger.log(logger.INFO, "destroyConsumer(" + delivered.size() + ", " + remotePendings + ", " + remoteCleanup + ", " + destroyingDest + ", " + notify + ")");
    }
    synchronized (destroyLock) {
        if (!valid) {
            // already removed
            return;
        }
        // we are going into destroy, so we are invalid
        valid = false;
    }
    Subscription sub = parent;
    if (sub != null) {
        sub.pause("Consumer.java: destroy " + this);
    }
    pause("Consumer.java: destroy ");
    // clean up hooks to any parent list
    if (parentListMap.size() > 0 && plistenerMap.size() > 0) {
        Object pll = null;
        for (Map.Entry<PartitionedStore, SubSet> pair : parentListMap.entrySet()) {
            pll = plistenerMap.get(pair.getKey());
            if (pll != null) {
                pair.getValue().removeEventListener(pll);
                plistenerMap.remove(pair.getKey());
            }
        }
    }
    List<Destination> ds = getDestinations();
    Map<PartitionedStore, SubSet> oldParent = new LinkedHashMap<>();
    synchronized (parentListMap) {
        oldParent.putAll(parentListMap);
        parentListMap.clear();
    }
    if (parent != null) {
        parent.releaseConsumer(uid);
        parent = null;
        if (notify) {
            try {
                sendDestroyConsumerNotification(remotePendings, remoteCleanup);
            } catch (Exception ex) {
                logger.logStack(Logger.WARNING, "Sending detach notification for " + uid + " from " + parent, ex);
            }
        }
    } else {
        if ((ds == null || ds.isEmpty()) && !destroyingDest) {
            // destination already gone
            // can happen if the destination is destroyed
            logger.log(Logger.DEBUG, "Removing consumer from non-existant destination" + dest);
        } else if (!destroyingDest) {
            Iterator itr = ds.iterator();
            while (itr.hasNext()) {
                Destination d = null;
                try {
                    d = (Destination) itr.next();
                    d.removeConsumer(uid, remotePendings, remoteCleanup, notify);
                } catch (Exception ex) {
                    logger.logStack(Logger.WARNING, "removing consumer " + uid + " from " + d, ex);
                }
            }
        }
    }
    if (DEBUG) {
        logger.log(Logger.DEBUG, "Destroying consumer " + this + "[" + delivered.size() + ":" + msgs.size() + "]");
    }
    // now clean up and/or requeue messages
    // any consumed messages will stay on the session
    // until we are done
    Set<PacketReference> s = new LinkedHashSet<>(msgs);
    Reason cleanupReason = (ackMsgsOnDestroy ? RemoveReason.ACKNOWLEDGED : RemoveReason.UNLOADED);
    delivered.addAll(s);
    // automatically ack any remote or ack On Destroy messages
    Iterator itr = delivered.iterator();
    while (itr.hasNext()) {
        PacketReference r = (PacketReference) itr.next();
        if (r == null) {
            continue;
        }
        if (ackMsgsOnDestroy || !r.isLocal()) {
            itr.remove();
            try {
                if (r.acknowledged(getConsumerUID(), getStoredConsumerUID(), !uid.isUnsafeAck(), r.isLocal())) {
                    try {
                        Destination[] dds = DestinationList.getDestination(r.getPartitionedStore(), r.getDestinationUID());
                        Destination d = null;
                        for (int i = 0; i < dds.length; i++) {
                            d = dds[i];
                            if (d == null) {
                                continue;
                            }
                            if (r.isLocal()) {
                                d.removeMessage(r.getSysMessageID(), RemoveReason.ACKNOWLEDGED);
                            } else {
                                d.removeRemoteMessage(r.getSysMessageID(), RemoveReason.ACKNOWLEDGED, r);
                            }
                        }
                    } finally {
                        r.postAcknowledgedRemoval();
                    }
                }
            } catch (Exception ex) {
                logger.log(Logger.DEBUG, "Broker down Unable to acknowlege" + r.getSysMessageID() + ":" + uid, ex);
            }
        }
    }
    msgs.removeAll(s, cleanupReason);
    if (!ackMsgsOnDestroy) {
        Map<PartitionedStore, Set> map = new LinkedHashMap<>();
        Set set = null;
        PacketReference ref = null;
        PartitionedStore pstore = null;
        itr = delivered.iterator();
        while (itr.hasNext()) {
            ref = (PacketReference) itr.next();
            if (ref.getDestinationUID().isQueue()) {
                pstore = ref.getPartitionedStore();
            } else {
                pstore = new NoPersistPartitionedStoreImpl(getStoredConsumerUID());
            }
            set = map.get(pstore);
            if (set == null) {
                set = new LinkedHashSet();
                map.put(pstore, set);
            }
            set.add(ref);
        }
        SubSet pl = null;
        for (Map.Entry<PartitionedStore, Set> pair : map.entrySet()) {
            pl = oldParent.get(pair.getKey());
            if (pl != null) {
                ((Prioritized) pl).addAllOrdered(pair.getValue());
            }
        }
        // help gc
        delivered.clear();
    }
    destroy();
    if (msgs != null && mlistener != null) {
        msgs.removeEventListener(mlistener);
        mlistener = null;
    }
    if (sub != null) {
        sub.resume("Consumer.java: destroyConsumer " + this);
    }
    selstr = null;
    selector = null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) SelectorFormatException(com.sun.messaging.jmq.util.selector.SelectorFormatException) LinkedHashMap(java.util.LinkedHashMap) NoPersistPartitionedStoreImpl(com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Example 4 with NoPersistPartitionedStoreImpl

use of com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl 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)

Example 5 with NoPersistPartitionedStoreImpl

use of com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl in project openmq by eclipse-ee4j.

the class Subscription method attachConsumer.

public void attachConsumer(Consumer consumer, Connection conn) throws BrokerException {
    logger.log(Logger.DEBUG, "Attaching Consumer " + consumer + " to durable " + this + " with  " + msgs.size() + " msgs " + getDestinationUID() + "[" + getConsumerUID() + "]");
    synchronized (subLock) {
        if (activeConsumers.get(consumer.getConsumerUID()) != null) {
            throw new ConsumerAlreadyAddedException(Globals.getBrokerResources().getKString(BrokerResources.I_CONSUMER_ALREADY_ADDED, consumer.getConsumerUID(), consumer.getDestinationUID()));
        }
        if (maxNumActiveConsumers == 1) {
            ConsumerUID kidc = consumer.getConsumerUID();
            uid.setConnectionUID(kidc.getConnectionUID());
            conuid = kidc.getConnectionUID();
        } else {
            if (!activeConsumers.isEmpty() && consumer.noLocal != noLocal) {
                throw new IllegalStateException("nolocal must match on all consumers");
            }
        }
        if (maxNumActiveConsumers != -1 && (activeConsumers.size() >= maxNumActiveConsumers)) {
            String[] args = { getDestinations().toString(), this.toString(), String.valueOf(maxNumActiveConsumers), String.valueOf(activeConsumers.size()) };
            throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_TOO_MANY_SHARED, args), BrokerResources.X_TOO_MANY_SHARED, (Throwable) null, Status.CONFLICT);
        }
        boolean wasActive = isActive();
        consumer.setStoredConsumerUID(getConsumerUID());
        consumer.getConsumerUID().setShouldStore(true);
        activeConsumers.put(consumer.getConsumerUID(), consumer);
        if (msgsSubset == null) {
            msgsSubset = msgs.subSet((Filter) null);
        }
        consumer.setParentList(new NoPersistPartitionedStoreImpl(getStoredConsumerUID()), msgsSubset);
        consumer.setSubscription(this);
        // OK - get all matching destinations
        active = !activeConsumers.isEmpty();
        Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DestinationList.findMatchingDestinationMap(null, getDestinationUID());
        LinkedHashSet dset = null;
        for (Map.Entry<PartitionedStore, LinkedHashSet<Destination>> pair : dmap.entrySet()) {
            dset = pair.getValue();
            if (dset == null) {
                continue;
            }
            Iterator<Destination> itr1 = dset.iterator();
            while (itr1.hasNext()) {
                Destination d = itr1.next();
                if (d == null) {
                    continue;
                }
                if (isActive() && !wasActive) {
                    if (!d.isLoaded()) {
                        logger.log(Logger.DEBUG, "Loading " + d);
                        try {
                            d.load();
                        } catch (BrokerException e) {
                            logger.logStack(Logger.ERROR, e.getMessage() + " [" + pair.getKey() + "]", e);
                        }
                    }
                }
                d.notifyConsumerAdded(consumer, conn);
            }
        }
    }
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) NoPersistPartitionedStoreImpl(com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl) ConsumerAlreadyAddedException(com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException) CacheHashMap(com.sun.messaging.jmq.util.CacheHashMap) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Aggregations

NoPersistPartitionedStoreImpl (com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl)8 PartitionedStore (com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)7 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)2 HashSet (java.util.HashSet)2 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)1 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)1 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)1 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)1 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)1 TransactionState (com.sun.messaging.jmq.jmsserver.data.TransactionState)1 ConsumerAlreadyAddedException (com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException)1 PartitionNotFoundException (com.sun.messaging.jmq.jmsserver.util.PartitionNotFoundException)1 CacheHashMap (com.sun.messaging.jmq.util.CacheHashMap)1