001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.region;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.Iterator;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025import java.util.concurrent.ConcurrentHashMap;
026import java.util.concurrent.locks.ReentrantReadWriteLock;
027
028import javax.jms.JMSException;
029import org.apache.activemq.broker.ConnectionContext;
030import org.apache.activemq.broker.ConsumerBrokerExchange;
031import org.apache.activemq.DestinationDoesNotExistException;
032import org.apache.activemq.broker.ProducerBrokerExchange;
033import org.apache.activemq.broker.region.policy.PolicyEntry;
034import org.apache.activemq.broker.region.virtual.CompositeDestinationFilter;
035import org.apache.activemq.command.ActiveMQDestination;
036import org.apache.activemq.command.ConsumerControl;
037import org.apache.activemq.command.ConsumerId;
038import org.apache.activemq.command.ConsumerInfo;
039import org.apache.activemq.command.Message;
040import org.apache.activemq.command.MessageAck;
041import org.apache.activemq.command.MessageDispatchNotification;
042import org.apache.activemq.command.MessagePull;
043import org.apache.activemq.command.ProducerInfo;
044import org.apache.activemq.command.RemoveSubscriptionInfo;
045import org.apache.activemq.command.Response;
046import org.apache.activemq.filter.DestinationFilter;
047import org.apache.activemq.filter.DestinationMap;
048import org.apache.activemq.security.SecurityContext;
049import org.apache.activemq.thread.TaskRunnerFactory;
050import org.apache.activemq.usage.SystemUsage;
051import org.slf4j.Logger;
052import org.slf4j.LoggerFactory;
053
054/**
055 *
056 */
057public abstract class AbstractRegion implements Region {
058
059    private static final Logger LOG = LoggerFactory.getLogger(AbstractRegion.class);
060
061    protected final Map<ActiveMQDestination, Destination> destinations = new ConcurrentHashMap<ActiveMQDestination, Destination>();
062    protected final DestinationMap destinationMap = new DestinationMap();
063    protected final Map<ConsumerId, Subscription> subscriptions = new ConcurrentHashMap<ConsumerId, Subscription>();
064    protected final SystemUsage usageManager;
065    protected final DestinationFactory destinationFactory;
066    protected final DestinationStatistics destinationStatistics;
067    protected final RegionBroker broker;
068    protected boolean autoCreateDestinations = true;
069    protected final TaskRunnerFactory taskRunnerFactory;
070    protected final ReentrantReadWriteLock destinationsLock = new ReentrantReadWriteLock();
071    protected final Map<ConsumerId, Object> consumerChangeMutexMap = new HashMap<ConsumerId, Object>();
072    protected boolean started;
073
074    public AbstractRegion(RegionBroker broker, DestinationStatistics destinationStatistics, SystemUsage memoryManager,
075            TaskRunnerFactory taskRunnerFactory, DestinationFactory destinationFactory) {
076        if (broker == null) {
077            throw new IllegalArgumentException("null broker");
078        }
079        this.broker = broker;
080        this.destinationStatistics = destinationStatistics;
081        this.usageManager = memoryManager;
082        this.taskRunnerFactory = taskRunnerFactory;
083        if (destinationFactory == null) {
084            throw new IllegalArgumentException("null destinationFactory");
085        }
086        this.destinationFactory = destinationFactory;
087    }
088
089    public final void start() throws Exception {
090        started = true;
091
092        Set<ActiveMQDestination> inactiveDests = getInactiveDestinations();
093        for (Iterator<ActiveMQDestination> iter = inactiveDests.iterator(); iter.hasNext();) {
094            ActiveMQDestination dest = iter.next();
095
096            ConnectionContext context = new ConnectionContext();
097            context.setBroker(broker.getBrokerService().getBroker());
098            context.setSecurityContext(SecurityContext.BROKER_SECURITY_CONTEXT);
099            context.getBroker().addDestination(context, dest, false);
100        }
101        destinationsLock.readLock().lock();
102        try{
103            for (Iterator<Destination> i = destinations.values().iterator(); i.hasNext();) {
104                Destination dest = i.next();
105                dest.start();
106            }
107        } finally {
108            destinationsLock.readLock().unlock();
109        }
110    }
111
112    public void stop() throws Exception {
113        started = false;
114        destinationsLock.readLock().lock();
115        try{
116            for (Iterator<Destination> i = destinations.values().iterator(); i.hasNext();) {
117                Destination dest = i.next();
118                dest.stop();
119            }
120        } finally {
121            destinationsLock.readLock().unlock();
122        }
123        destinations.clear();
124    }
125
126    public Destination addDestination(ConnectionContext context, ActiveMQDestination destination,
127            boolean createIfTemporary) throws Exception {
128
129        destinationsLock.writeLock().lock();
130        try {
131            Destination dest = destinations.get(destination);
132            if (dest == null) {
133                if (destination.isTemporary() == false || createIfTemporary) {
134                    LOG.debug("{} adding destination: {}", broker.getBrokerName(), destination);
135                    dest = createDestination(context, destination);
136                    // intercept if there is a valid interceptor defined
137                    DestinationInterceptor destinationInterceptor = broker.getDestinationInterceptor();
138                    if (destinationInterceptor != null) {
139                        dest = destinationInterceptor.intercept(dest);
140                    }
141                    dest.start();
142                    destinations.put(destination, dest);
143                    destinationMap.put(destination, dest);
144                    addSubscriptionsForDestination(context, dest);
145                }
146                if (dest == null) {
147                    throw new DestinationDoesNotExistException(destination.getQualifiedName());
148                }
149            }
150            return dest;
151        } finally {
152            destinationsLock.writeLock().unlock();
153        }
154    }
155
156    public Map<ConsumerId, Subscription> getSubscriptions() {
157        return subscriptions;
158    }
159
160    protected List<Subscription> addSubscriptionsForDestination(ConnectionContext context, Destination dest)
161            throws Exception {
162
163        List<Subscription> rc = new ArrayList<Subscription>();
164        // Add all consumers that are interested in the destination.
165        for (Iterator<Subscription> iter = subscriptions.values().iterator(); iter.hasNext();) {
166            Subscription sub = iter.next();
167            if (sub.matches(dest.getActiveMQDestination())) {
168                try {
169                    dest.addSubscription(context, sub);
170                    rc.add(sub);
171                } catch (SecurityException e) {
172                    if (sub.isWildcard()) {
173                        LOG.debug("Subscription denied for " + sub + " to destination " +
174                            dest.getActiveMQDestination() +  ": " + e.getMessage());
175                    } else {
176                        throw e;
177                    }
178                }
179            }
180        }
181        return rc;
182
183    }
184
185    public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout)
186            throws Exception {
187
188        // No timeout.. then try to shut down right way, fails if there are
189        // current subscribers.
190        if (timeout == 0) {
191            for (Iterator<Subscription> iter = subscriptions.values().iterator(); iter.hasNext();) {
192                Subscription sub = iter.next();
193                if (sub.matches(destination)) {
194                    throw new JMSException("Destination still has an active subscription: " + destination);
195                }
196            }
197        }
198
199        if (timeout > 0) {
200            // TODO: implement a way to notify the subscribers that we want to
201            // take the down
202            // the destination and that they should un-subscribe.. Then wait up
203            // to timeout time before
204            // dropping the subscription.
205        }
206
207        LOG.debug("{} removing destination: {}", broker.getBrokerName(), destination);
208
209        destinationsLock.writeLock().lock();
210        try {
211            Destination dest = destinations.remove(destination);
212            if (dest != null) {
213                // timeout<0 or we timed out, we now force any remaining
214                // subscriptions to un-subscribe.
215                for (Iterator<Subscription> iter = subscriptions.values().iterator(); iter.hasNext();) {
216                    Subscription sub = iter.next();
217                    if (sub.matches(destination)) {
218                        dest.removeSubscription(context, sub, 0l);
219                    }
220                }
221                destinationMap.remove(destination, dest);
222                dispose(context, dest);
223                DestinationInterceptor destinationInterceptor = broker.getDestinationInterceptor();
224                if (destinationInterceptor != null) {
225                    destinationInterceptor.remove(dest);
226                }
227
228            } else {
229                LOG.debug("Cannot remove a destination that doesn't exist: {}", destination);
230            }
231        } finally {
232            destinationsLock.writeLock().unlock();
233        }
234    }
235
236    /**
237     * Provide an exact or wildcard lookup of destinations in the region
238     *
239     * @return a set of matching destination objects.
240     */
241    @SuppressWarnings("unchecked")
242    public Set<Destination> getDestinations(ActiveMQDestination destination) {
243        destinationsLock.readLock().lock();
244        try{
245            return destinationMap.get(destination);
246        } finally {
247            destinationsLock.readLock().unlock();
248        }
249    }
250
251    public Map<ActiveMQDestination, Destination> getDestinationMap() {
252        return destinations;
253    }
254
255    @SuppressWarnings("unchecked")
256    public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
257        LOG.debug("{} adding consumer: {} for destination: {}", new Object[]{ broker.getBrokerName(), info.getConsumerId(), info.getDestination() });
258        ActiveMQDestination destination = info.getDestination();
259        if (destination != null && !destination.isPattern() && !destination.isComposite()) {
260            // lets auto-create the destination
261            lookup(context, destination,true);
262        }
263
264        Object addGuard;
265        synchronized (consumerChangeMutexMap) {
266            addGuard = consumerChangeMutexMap.get(info.getConsumerId());
267            if (addGuard == null) {
268                addGuard = new Object();
269                consumerChangeMutexMap.put(info.getConsumerId(), addGuard);
270            }
271        }
272        synchronized (addGuard) {
273            Subscription o = subscriptions.get(info.getConsumerId());
274            if (o != null) {
275                LOG.warn("A duplicate subscription was detected. Clients may be misbehaving. Later warnings you may see about subscription removal are a consequence of this.");
276                return o;
277            }
278
279            // We may need to add some destinations that are in persistent store
280            // but not active
281            // in the broker.
282            //
283            // TODO: think about this a little more. This is good cause
284            // destinations are not loaded into
285            // memory until a client needs to use the queue, but a management
286            // agent viewing the
287            // broker will not see a destination that exists in persistent
288            // store. We may want to
289            // eagerly load all destinations into the broker but have an
290            // inactive state for the
291            // destination which has reduced memory usage.
292            //
293            DestinationFilter.parseFilter(info.getDestination());
294
295            Subscription sub = createSubscription(context, info);
296
297            // At this point we're done directly manipulating subscriptions,
298            // but we need to retain the synchronized block here. Consider
299            // otherwise what would happen if at this point a second
300            // thread added, then removed, as would be allowed with
301            // no mutex held. Remove is only essentially run once
302            // so everything after this point would be leaked.
303
304            // Add the subscription to all the matching queues.
305            // But copy the matches first - to prevent deadlocks
306            List<Destination> addList = new ArrayList<Destination>();
307            destinationsLock.readLock().lock();
308            try {
309                for (Destination dest : (Set<Destination>) destinationMap.get(info.getDestination())) {
310                    addList.add(dest);
311                }
312            } finally {
313                destinationsLock.readLock().unlock();
314            }
315
316            List<Destination> removeList = new ArrayList<Destination>();
317            for (Destination dest : addList) {
318                try {
319                    dest.addSubscription(context, sub);
320                    removeList.add(dest);
321                } catch (SecurityException e){
322                    if (sub.isWildcard()) {
323                        LOG.debug("Subscription denied for " + sub + " to destination " +
324                            dest.getActiveMQDestination() + ": " + e.getMessage());
325                    } else {
326                        // remove partial subscriptions
327                        for (Destination remove : removeList) {
328                            try {
329                                remove.removeSubscription(context, sub, info.getLastDeliveredSequenceId());
330                            } catch (Exception ex) {
331                                LOG.error("Error unsubscribing " + sub + " from " + remove + ": " + ex.getMessage(), ex);
332                            }
333                        }
334                        throw e;
335                    }
336                }
337            }
338            removeList.clear();
339
340            if (info.isBrowser()) {
341                ((QueueBrowserSubscription) sub).destinationsAdded();
342            }
343
344            subscriptions.put(info.getConsumerId(), sub);
345
346            return sub;
347        }
348    }
349
350    /**
351     * Get all the Destinations that are in storage
352     *
353     * @return Set of all stored destinations
354     */
355    @SuppressWarnings("rawtypes")
356    public Set getDurableDestinations() {
357        return destinationFactory.getDestinations();
358    }
359
360    /**
361     * @return all Destinations that don't have active consumers
362     */
363    protected Set<ActiveMQDestination> getInactiveDestinations() {
364        Set<ActiveMQDestination> inactiveDests = destinationFactory.getDestinations();
365        destinationsLock.readLock().lock();
366        try {
367            inactiveDests.removeAll(destinations.keySet());
368        } finally {
369            destinationsLock.readLock().unlock();
370        }
371        return inactiveDests;
372    }
373
374    @SuppressWarnings("unchecked")
375    public void removeConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
376        LOG.debug("{} removing consumer: {} for destination: {}", new Object[]{ broker.getBrokerName(), info.getConsumerId(), info.getDestination() });
377
378        Subscription sub = subscriptions.remove(info.getConsumerId());
379        // The sub could be removed elsewhere - see ConnectionSplitBroker
380        if (sub != null) {
381
382            // remove the subscription from all the matching queues.
383            List<Destination> removeList = new ArrayList<Destination>();
384            destinationsLock.readLock().lock();
385            try {
386                for (Destination dest : (Set<Destination>) destinationMap.get(info.getDestination())) {
387                    removeList.add(dest);
388                }
389            } finally {
390                destinationsLock.readLock().unlock();
391            }
392            for (Destination dest : removeList) {
393                dest.removeSubscription(context, sub, info.getLastDeliveredSequenceId());
394            }
395
396            destroySubscription(sub);
397        }
398        synchronized (consumerChangeMutexMap) {
399            consumerChangeMutexMap.remove(info.getConsumerId());
400        }
401    }
402
403    protected void destroySubscription(Subscription sub) {
404        sub.destroy();
405    }
406
407    public void removeSubscription(ConnectionContext context, RemoveSubscriptionInfo info) throws Exception {
408        throw new JMSException("Invalid operation.");
409    }
410
411    public void send(final ProducerBrokerExchange producerExchange, Message messageSend) throws Exception {
412        final ConnectionContext context = producerExchange.getConnectionContext();
413
414        if (producerExchange.isMutable() || producerExchange.getRegionDestination() == null) {
415            final Destination regionDestination = lookup(context, messageSend.getDestination(),false);
416            producerExchange.setRegionDestination(regionDestination);
417        }
418
419        producerExchange.getRegionDestination().send(producerExchange, messageSend);
420
421        if (producerExchange.getProducerState() != null && producerExchange.getProducerState().getInfo() != null){
422            producerExchange.getProducerState().getInfo().incrementSentCount();
423        }
424    }
425
426    public void acknowledge(ConsumerBrokerExchange consumerExchange, MessageAck ack) throws Exception {
427        Subscription sub = consumerExchange.getSubscription();
428        if (sub == null) {
429            sub = subscriptions.get(ack.getConsumerId());
430            if (sub == null) {
431                if (!consumerExchange.getConnectionContext().isInRecoveryMode()) {
432                    LOG.warn("Ack for non existent subscription, ack: {}", ack);
433                    throw new IllegalArgumentException("The subscription does not exist: " + ack.getConsumerId());
434                } else {
435                    LOG.debug("Ack for non existent subscription in recovery, ack: {}", ack);
436                    return;
437                }
438            }
439            consumerExchange.setSubscription(sub);
440        }
441        sub.acknowledge(consumerExchange.getConnectionContext(), ack);
442    }
443
444    public Response messagePull(ConnectionContext context, MessagePull pull) throws Exception {
445        Subscription sub = subscriptions.get(pull.getConsumerId());
446        if (sub == null) {
447            throw new IllegalArgumentException("The subscription does not exist: " + pull.getConsumerId());
448        }
449        return sub.pullMessage(context, pull);
450    }
451
452    protected Destination lookup(ConnectionContext context, ActiveMQDestination destination,boolean createTemporary) throws Exception {
453        Destination dest = null;
454
455        destinationsLock.readLock().lock();
456        try {
457            dest = destinations.get(destination);
458        } finally {
459            destinationsLock.readLock().unlock();
460        }
461
462        if (dest == null) {
463            if (isAutoCreateDestinations()) {
464                // Try to auto create the destination... re-invoke broker
465                // from the
466                // top so that the proper security checks are performed.
467                context.getBroker().addDestination(context, destination, createTemporary);
468                dest = addDestination(context, destination, false);
469                // We should now have the dest created.
470                destinationsLock.readLock().lock();
471                try {
472                    dest = destinations.get(destination);
473                } finally {
474                    destinationsLock.readLock().unlock();
475                }
476            }
477
478            if (dest == null) {
479                throw new JMSException("The destination " + destination + " does not exist.");
480            }
481        }
482        return dest;
483    }
484
485    public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
486        Subscription sub = subscriptions.get(messageDispatchNotification.getConsumerId());
487        if (sub != null) {
488            sub.processMessageDispatchNotification(messageDispatchNotification);
489        } else {
490            throw new JMSException("Slave broker out of sync with master - Subscription: "
491                    + messageDispatchNotification.getConsumerId() + " on "
492                    + messageDispatchNotification.getDestination() + " does not exist for dispatch of message: "
493                    + messageDispatchNotification.getMessageId());
494        }
495    }
496
497    /*
498     * For a Queue/TempQueue, dispatch order is imperative to match acks, so the
499     * dispatch is deferred till the notification to ensure that the
500     * subscription chosen by the master is used. AMQ-2102
501     */
502    protected void processDispatchNotificationViaDestination(MessageDispatchNotification messageDispatchNotification)
503            throws Exception {
504        Destination dest = null;
505        destinationsLock.readLock().lock();
506        try {
507            dest = destinations.get(messageDispatchNotification.getDestination());
508        } finally {
509            destinationsLock.readLock().unlock();
510        }
511
512        if (dest != null) {
513            dest.processDispatchNotification(messageDispatchNotification);
514        } else {
515            throw new JMSException("Slave broker out of sync with master - Destination: "
516                    + messageDispatchNotification.getDestination() + " does not exist for consumer "
517                    + messageDispatchNotification.getConsumerId() + " with message: "
518                    + messageDispatchNotification.getMessageId());
519        }
520    }
521
522    public void gc() {
523        for (Subscription sub : subscriptions.values()) {
524            sub.gc();
525        }
526
527        destinationsLock.readLock().lock();
528        try {
529            for (Destination dest : destinations.values()) {
530                dest.gc();
531            }
532        } finally {
533            destinationsLock.readLock().unlock();
534        }
535    }
536
537    protected abstract Subscription createSubscription(ConnectionContext context, ConsumerInfo info) throws Exception;
538
539    protected Destination createDestination(ConnectionContext context, ActiveMQDestination destination)
540            throws Exception {
541        return destinationFactory.createDestination(context, destination, destinationStatistics);
542    }
543
544    public boolean isAutoCreateDestinations() {
545        return autoCreateDestinations;
546    }
547
548    public void setAutoCreateDestinations(boolean autoCreateDestinations) {
549        this.autoCreateDestinations = autoCreateDestinations;
550    }
551
552    @SuppressWarnings("unchecked")
553    public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception {
554        destinationsLock.readLock().lock();
555        try {
556            for (Destination dest : (Set<Destination>) destinationMap.get(info.getDestination())) {
557                dest.addProducer(context, info);
558            }
559        } finally {
560            destinationsLock.readLock().unlock();
561        }
562    }
563
564    /**
565     * Removes a Producer.
566     *
567     * @param context
568     *            the environment the operation is being executed under.
569     * @throws Exception
570     *             TODO
571     */
572    @SuppressWarnings("unchecked")
573    public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception {
574        destinationsLock.readLock().lock();
575        try {
576            for (Destination dest : (Set<Destination>) destinationMap.get(info.getDestination())) {
577                dest.removeProducer(context, info);
578            }
579        } finally {
580            destinationsLock.readLock().unlock();
581        }
582    }
583
584    protected void dispose(ConnectionContext context, Destination dest) throws Exception {
585        dest.dispose(context);
586        dest.stop();
587        destinationFactory.removeDestination(dest);
588    }
589
590    public void processConsumerControl(ConsumerBrokerExchange consumerExchange, ConsumerControl control) {
591        Subscription sub = subscriptions.get(control.getConsumerId());
592        if (sub != null && sub instanceof AbstractSubscription) {
593            ((AbstractSubscription) sub).setPrefetchSize(control.getPrefetch());
594            if (broker.getDestinationPolicy() != null) {
595                PolicyEntry entry = broker.getDestinationPolicy().getEntryFor(control.getDestination());
596                if (entry != null) {
597                    entry.configurePrefetch(sub);
598                }
599            }
600            LOG.debug("setting prefetch: {}, on subscription: {}; resulting value: {}", new Object[]{ control.getPrefetch(), control.getConsumerId(), sub.getConsumerInfo().getCurrentPrefetchSize()});
601            try {
602                lookup(consumerExchange.getConnectionContext(), control.getDestination(),false).wakeup();
603            } catch (Exception e) {
604                LOG.warn("failed to deliver post consumerControl dispatch-wakeup, to destination: {}", control.getDestination(), e);
605            }
606        }
607    }
608
609    public void reapplyInterceptor() {
610        destinationsLock.writeLock().lock();
611        try {
612            DestinationInterceptor destinationInterceptor = broker.getDestinationInterceptor();
613            Map<ActiveMQDestination, Destination> map = getDestinationMap();
614            for (ActiveMQDestination key : map.keySet()) {
615                Destination destination = map.get(key);
616                if (destination instanceof CompositeDestinationFilter) {
617                    destination = ((CompositeDestinationFilter) destination).next;
618                }
619                if (destinationInterceptor != null) {
620                    destination = destinationInterceptor.intercept(destination);
621                }
622                getDestinationMap().put(key, destination);
623                destinations.put(key, destination);
624            }
625        } finally {
626            destinationsLock.writeLock().unlock();
627        }
628    }
629}