summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/queue/SubscriptionImpl.java
blob: 05cd461582d701f5bd8464e4589e21e61be1632e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.server.queue;

import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
import org.apache.qpid.protocol.AMQConstant;
import org.apache.qpid.common.AMQPFilterTypes;
import org.apache.qpid.common.ClientProperties;
import org.apache.qpid.framing.AMQShortString;
import org.apache.qpid.framing.FieldTable;
import org.apache.qpid.server.AMQChannel;
import org.apache.qpid.server.output.ProtocolOutputConverter;
import org.apache.qpid.server.filter.FilterManager;
import org.apache.qpid.server.filter.FilterManagerFactory;
import org.apache.qpid.server.protocol.AMQProtocolSession;
import org.apache.qpid.server.store.StoreContext;
import org.apache.qpid.util.ConcurrentLinkedQueueAtomicSize;
import org.apache.qpid.util.MessageQueue;
import org.apache.qpid.util.ConcurrentLinkedMessageQueueAtomicSize;

/**
 * Encapsulation of a supscription to a queue. <p/> Ties together the protocol session of a subscriber, the consumer tag
 * that was given out by the broker and the channel id. <p/>
 */
public class SubscriptionImpl implements Subscription
{

    private static final Logger _suspensionlogger = Logger.getLogger("Suspension");
    private static final Logger _logger = Logger.getLogger(SubscriptionImpl.class);

    public final AMQChannel channel;

    public final AMQProtocolSession protocolSession;

    public final AMQShortString consumerTag;

    private final Object _sessionKey;

    private MessageQueue<QueueEntry> _messages;

    private Queue<QueueEntry> _resendQueue;

    private final boolean _noLocal;

    /** True if messages need to be acknowledged */
    private final boolean _acks;
    private FilterManager _filters;
    private final boolean _isBrowser;
    private final Boolean _autoClose;
    private boolean _sentClose = false;

    private static final String CLIENT_PROPERTIES_INSTANCE = ClientProperties.instance.toString();

    private AMQQueue _queue;
    private final AtomicBoolean _sendLock = new AtomicBoolean(false);


    public static class Factory implements SubscriptionFactory
    {
        public Subscription createSubscription(int channel, AMQProtocolSession protocolSession,
                                               AMQShortString consumerTag, boolean acks, FieldTable filters,
                                               boolean noLocal, AMQQueue queue) throws AMQException
        {
            return new SubscriptionImpl(channel, protocolSession, consumerTag, acks, filters, noLocal, queue);
        }

        public SubscriptionImpl createSubscription(int channel, AMQProtocolSession protocolSession, AMQShortString consumerTag)
                throws AMQException
        {
            return new SubscriptionImpl(channel, protocolSession, consumerTag, false, null, false, null);
        }
    }

    public SubscriptionImpl(int channelId, AMQProtocolSession protocolSession,
                            AMQShortString consumerTag, boolean acks)
            throws AMQException
    {
        this(channelId, protocolSession, consumerTag, acks, null, false, null);
    }

    public SubscriptionImpl(int channelId, AMQProtocolSession protocolSession,
                            AMQShortString consumerTag, boolean acks, FieldTable filters,
                            boolean noLocal, AMQQueue queue)
            throws AMQException
    {
        AMQChannel channel = protocolSession.getChannel(channelId);
        if (channel == null)
        {
            throw new AMQException(AMQConstant.NOT_FOUND, "channel :" + channelId + " not found in protocol session");
        }

        this.channel = channel;
        this.protocolSession = protocolSession;
        this.consumerTag = consumerTag;
        _sessionKey = protocolSession.getKey();
        _acks = acks;
        _noLocal = noLocal;
        _queue = queue;

        _filters = FilterManagerFactory.createManager(filters);


        if (_filters != null)
        {
            Object isBrowser = filters.get(AMQPFilterTypes.NO_CONSUME.getValue());
            if (isBrowser != null)
            {
                _isBrowser = (Boolean) isBrowser;
            }
            else
            {
                _isBrowser = false;
            }
        }
        else
        {
            _isBrowser = false;
        }


        if (_filters != null)
        {
            Object autoClose = filters.get(AMQPFilterTypes.AUTO_CLOSE.getValue());
            if (autoClose != null)
            {
                _autoClose = (Boolean) autoClose;
            }
            else
            {
                _autoClose = false;
            }
        }
        else
        {
            _autoClose = false;
        }


        if (filtersMessages())
        {
            _messages = new ConcurrentLinkedMessageQueueAtomicSize<QueueEntry>();
        }
        else
        {
            // Reference the DeliveryManager
            _messages = null;
        }
    }


    public SubscriptionImpl(int channel, AMQProtocolSession protocolSession,
                            AMQShortString consumerTag)
            throws AMQException
    {
        this(channel, protocolSession, consumerTag, false);
    }

    public boolean equals(Object o)
    {
        return (o instanceof SubscriptionImpl) && equals((SubscriptionImpl) o);
    }

    /**
     * Equality holds if the session matches and the channel and consumer tag are the same.
     *
     * @param psc The subscriptionImpl to compare
     *
     * @return equality
     */
    private boolean equals(SubscriptionImpl psc)
    {
        return _sessionKey.equals(psc._sessionKey)
               && psc.channel == channel
               && psc.consumerTag.equals(consumerTag);
    }

    public int hashCode()
    {
        return _sessionKey.hashCode();
    }

    public String toString()
    {
        String subscriber = "[channel=" + channel +
                            ", consumerTag=" + consumerTag +
                            ", session=" + protocolSession.getKey() +
                            ", resendQueue=" + (_resendQueue != null);

        if (_resendQueue != null)
        {
            subscriber += ", resendSize=" + _resendQueue.size();
        }


        return subscriber + "]";
    }

    /**
     * This method can be called by each of the publisher threads. As a result all changes to the channel object must be
     * thread safe.
     *
     * @param msg   The message to send
     * @param queue the Queue it has been sent from
     *
     * @throws AMQException
     */
    public void send(QueueEntry msg, AMQQueue queue) throws AMQException
    {
        if (msg != null)
        {
            if (_isBrowser)
            {
                sendToBrowser(msg, queue);
            }
            else
            {
                sendToConsumer(channel.getStoreContext(), msg, queue);
            }
        }
        else
        {
            _logger.error("Attempt to send Null message", new NullPointerException());
        }
    }

    private void sendToBrowser(QueueEntry msg, AMQQueue queue) throws AMQException
    {
        // We don't decrement the reference here as we don't want to consume the message
        // but we do want to send it to the client.

        synchronized (channel)
        {
            long deliveryTag = channel.getNextDeliveryTag();

            if (_sendLock.get())
            {
                _logger.error("Sending " + msg + " when subscriber(" + this + ") is closed!");
            }

            protocolSession.getProtocolOutputConverter().writeDeliver(msg.getMessage(), channel.getChannelId(), deliveryTag, consumerTag);
        }
    }

    private void sendToConsumer(StoreContext storeContext, QueueEntry entry, AMQQueue queue)
            throws AMQException
    {
        try
        { // if we do not need to wait for client acknowledgements
            // we can decrement the reference count immediately.

            // By doing this _before_ the send we ensure that it
            // doesn't get sent if it can't be dequeued, preventing
            // duplicate delivery on recovery.

            // The send may of course still fail, in which case, as
            // the message is unacked, it will be lost.
            final AMQMessage message = entry.getMessage();

            if (!_acks)
            {
                if (_logger.isDebugEnabled())
                {
                    _logger.debug("No ack mode so dequeuing message immediately: " + message.getMessageId());
                }
                queue.dequeue(storeContext, entry);
            }

            final ProtocolOutputConverter outputConverter = protocolSession.getProtocolOutputConverter();
            final int channelId = channel.getChannelId();

            synchronized (channel)
            {
                final long deliveryTag = channel.getNextDeliveryTag();


                if (_acks)
                {
                    channel.addUnacknowledgedMessage(entry, deliveryTag, consumerTag);
                }

                outputConverter.writeDeliver(message, channelId, deliveryTag, consumerTag);


            }
            if (!_acks)
            {
                message.decrementReference(storeContext);
            }
        }
        finally
        {
            //Only set delivered if it actually was writen successfully..
            // using a try->finally would set it even if an error occured.
            // Is this what we want? 

            entry.setDeliveredToConsumer();
        }
    }

    public boolean isSuspended()
    {
//        if (_suspensionlogger.isInfoEnabled())
//        {
//            if (channel.isSuspended())
//            {
//                _suspensionlogger.debug("Subscription(" + debugIdentity() + ") channel's is susupended");
//            }
//            if (_sendLock.get())
//            {
//                _suspensionlogger.debug("Subscription(" + debugIdentity() + ") has sendLock set so closing.");
//            }
//        }
        return channel.isSuspended() || _sendLock.get();
    }

    /**
     * Callback indicating that a queue has been deleted.
     *
     * @param queue The queue to delete
     */
    public void queueDeleted(AMQQueue queue) throws AMQException
    {
        channel.queueDeleted(queue);
    }

    public boolean filtersMessages()
    {
        return _filters != null || _noLocal;
    }

    public boolean hasInterest(QueueEntry entry)
    {
        //check that the message hasn't been rejected
        if (entry.isRejectedBy(this))
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("Subscription:" + debugIdentity() + " rejected message:" + entry.debugIdentity());
            }
//            return false;
        }



        //todo - client id should be recoreded and this test removed but handled below
        if (_noLocal)
        {

            final AMQProtocolSession publisher = entry.getMessage().getPublisher();
            if(publisher != null)

            {
                // We don't want local messages so check to see if message is one we sent
                Object localInstance;
                Object msgInstance;

                if ((protocolSession.getClientProperties() != null) &&
                    (localInstance = protocolSession.getClientProperties().getObject(CLIENT_PROPERTIES_INSTANCE)) != null)
                {

                    if ((publisher.getClientProperties() != null) &&
                        (msgInstance = publisher.getClientProperties().getObject(CLIENT_PROPERTIES_INSTANCE)) != null)
                    {
                        if (localInstance == msgInstance || localInstance.equals(msgInstance))
                        {
    //                        if (_logger.isTraceEnabled())
    //                        {
    //                            _logger.trace("(" + debugIdentity() + ") has no interest as it is a local message(" +
    //                                          msg.debugIdentity() + ")");
    //                        }
                            return false;
                        }
                    }
                }
                else
                {

                    localInstance = protocolSession.getClientIdentifier();
                    //todo - client id should be recoreded and this test removed but handled here

                    msgInstance = publisher.getClientIdentifier();
                    if (localInstance == msgInstance || ((localInstance != null) && localInstance.equals(msgInstance)))
                    {
    //                    if (_logger.isTraceEnabled())
    //                    {
    //                        _logger.trace("(" + debugIdentity() + ") has no interest as it is a local message(" +
    //                                      msg.debugIdentity() + ")");
    //                    }
                        return false;
                    }
                }

            }
        }


        return checkFilters(entry);

    }

    private String id = String.valueOf(System.identityHashCode(this));

    private String debugIdentity()
    {
        return id;
    }

    private boolean checkFilters(QueueEntry msg)
    {
        return (_filters == null) || _filters.allAllow(msg.getMessage());
    }

    public Queue<QueueEntry> getPreDeliveryQueue()
    {
        return _messages;
    }

    public void enqueueForPreDelivery(QueueEntry msg, boolean deliverFirst)
    {
        if (_messages != null)
        {
            if (deliverFirst)
            {
                _messages.pushHead(msg);
            }
            else
            {
                _messages.offer(msg);
            }
        }
    }

    private boolean isAutoClose()
    {
        return _autoClose;
    }

    public void close()
    {
        boolean closed = false;
        synchronized (_sendLock)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("Setting SendLock true:" + debugIdentity());
            }

            closed = _sendLock.getAndSet(true);
        }

        if (closed)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("Called close() on a closed subscription");
            }

            return;
        }

        if (_logger.isInfoEnabled())
        {
            _logger.info("Closing subscription (" + debugIdentity() + "):" + this);
        }

        if (_resendQueue != null && !_resendQueue.isEmpty())
        {
            if (_logger.isInfoEnabled())
            {
                _logger.info("Requeuing closing subscription (" + debugIdentity() + "):" + this);
            }
            requeue();
        }

        //remove references in PDQ
        if (_messages != null)
        {
            if (_logger.isInfoEnabled())
            {
                _logger.info("Clearing PDQ (" + debugIdentity() + "):" + this);
            }

            _messages.clear();
        }
    }

    private void autoclose()
    {
        close();

        if (_autoClose && !_sentClose)
        {
            _logger.info("Closing autoclose subscription (" + debugIdentity() + "):" + this);

            boolean unregisteredOK = false;
            try
            {
                unregisteredOK = channel.unsubscribeConsumer(protocolSession, consumerTag);
            }
            catch (AMQException e)
            {
                // Occurs if we cannot find the subscriber in the channel with protocolSession and consumerTag.
                _logger.info("Unable to UnsubscribeConsumer :" + consumerTag +" so not going to send CancelOK.");
            }

            if (unregisteredOK)
            {
                ProtocolOutputConverter converter = protocolSession.getProtocolOutputConverter();
                converter.confirmConsumerAutoClose(channel.getChannelId(), consumerTag);
                _sentClose = true;
            }

        }
    }

    private void requeue()
    {
        if (_queue != null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("Requeuing :" + _resendQueue.size() + " messages");
            }

            while (!_resendQueue.isEmpty())
            {
                QueueEntry resent = _resendQueue.poll();

                if (_logger.isDebugEnabled())
                {
                    _logger.debug("Removed for resending:" + resent.debugIdentity());
                }

                resent.release();
                _queue.subscriberHasPendingResend(false, this, resent);

                try
                {
                    channel.getTransactionalContext().deliver(resent, true);
                }
                catch (AMQException e)
                {
                    _logger.error("MESSAGE LOSS : Unable to re-deliver messages", e);
                }
            }

            if (!_resendQueue.isEmpty())
            {
                _logger.error("[MESSAGES LOST]Unable to re-deliver messages as queue is null.");
            }

            _queue.subscriberHasPendingResend(false, this, null);
        }
        else
        {
            if (!_resendQueue.isEmpty())
            {
                _logger.error("Unable to re-deliver messages as queue is null.");
            }
        }

        // Clear the messages
        _resendQueue = null;
    }


    public boolean isClosed()
    {
        return _sendLock.get(); // This rather than _close is used to signify the subscriber is now closed.
    }

    public boolean isBrowser()
    {
        return _isBrowser;
    }

    public boolean wouldSuspend(QueueEntry msg)
    {
        return _acks && channel.wouldSuspend(msg.getMessage());
    }

    public Queue<QueueEntry> getResendQueue()
    {
        if (_resendQueue == null)
        {
            _resendQueue = new ConcurrentLinkedQueueAtomicSize<QueueEntry>();
        }
        return _resendQueue;
    }


    public Queue<QueueEntry> getNextQueue(Queue<QueueEntry> messages)
    {
        if (_resendQueue != null && !_resendQueue.isEmpty())
        {
            return _resendQueue;
        }

        if (filtersMessages())
        {
            if (isAutoClose())
            {
                if (_messages.isEmpty())
                {
                    autoclose();
                    return null;
                }
            }
            return _messages;
        }
        else // we want the DM queue
        {
            return messages;
        }
    }

    public void addToResendQueue(QueueEntry msg)
    {
        // add to our resend queue
        getResendQueue().add(msg);

        // Mark Queue has having content.
        if (_queue == null)
        {
            _logger.error("Queue is null won't be able to resend messages");
        }
        else
        {
            _queue.subscriberHasPendingResend(true, this, msg);
        }
    }

    public Object getSendLock()
    {
        return _sendLock;
    }

    public AMQChannel getChannel()
    {
        return channel;
    }

    public void start()
    {
        //Check to see if we need to autoclose
        if (filtersMessages())
        {
            if (isAutoClose())
            {
                if (_messages.isEmpty())
                {
                    autoclose();
                }
            }
        }
    }

}