summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/txn/LocalTransactionalContext.java
blob: df6520e0a86cebf1aabdfae3260fad002fa61e24 (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
/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed 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.txn;

import org.apache.qpid.AMQException;
import org.apache.qpid.server.ack.TxAck;
import org.apache.qpid.server.ack.UnacknowledgedMessageMap;
import org.apache.qpid.server.queue.AMQMessage;
import org.apache.qpid.server.queue.AMQQueue;
import org.apache.qpid.server.RequiredDeliveryException;
import org.apache.qpid.server.store.MessageStore;

import java.util.List;

/**
 * A transactional context that only supports local transactions.
 */
public class LocalTransactionalContext implements TransactionalContext
{
    private final TxnBuffer _txnBuffer;

    /**
     * We keep hold of the ack operation so that we can consolidate acks, i.e. multiple acks within a txn are
     * consolidated into a single operation
     */
    private TxAck _ackOp;

    private List<RequiredDeliveryException> _returnMessages;

    private final MessageStore _messageStore;

    private boolean _inTran = false;

    public LocalTransactionalContext(MessageStore messageStore,
                                     TxnBuffer txnBuffer, List<RequiredDeliveryException> returnMessages)
    {
        _messageStore = messageStore;
        _txnBuffer = txnBuffer;
        _returnMessages = returnMessages;
        _txnBuffer.enlist(new StoreMessageOperation(messageStore));
    }

    public void rollback() throws AMQException
    {
        _txnBuffer.rollback();
    }

    public void deliver(AMQMessage message, AMQQueue queue) throws AMQException
    {
        // don't create a transaction unless needed
        if (message.isPersistent())
        {
            _txnBuffer.containsPersistentChanges();
        }

        // A publication will result in the enlisting of several
        // TxnOps. The first is an op that will store the message.
        // Following that (and ordering is important), an op will
        // be added for every queue onto which the message is
        // enqueued. Finally a cleanup op will be added to decrement
        // the reference associated with the routing.

        _txnBuffer.enlist(new DeliverMessageOperation(message, queue));
        _txnBuffer.enlist(new CleanupMessageOperation(message, _returnMessages));
    }

    private void checkAck(long deliveryTag, UnacknowledgedMessageMap unacknowledgedMessageMap) throws AMQException
    {
        if (!unacknowledgedMessageMap.contains(deliveryTag))
        {
            throw new AMQException("Ack with delivery tag " + deliveryTag + " not known for channel");
        }
    }

    public void acknowledgeMessage(long deliveryTag, long lastDeliveryTag, boolean multiple,
                                   UnacknowledgedMessageMap unacknowledgedMessageMap) throws AMQException
    {
        //check that the tag exists to give early failure
        if (!multiple || deliveryTag > 0)
        {
            checkAck(deliveryTag, unacknowledgedMessageMap);
        }
        //we use a single txn op for all acks and update this op
        //as new acks come in. If this is the first ack in the txn
        //we will need to create and enlist the op.
        if (_ackOp == null)
        {
            _ackOp = new TxAck(unacknowledgedMessageMap);
            _txnBuffer.enlist(_ackOp);
        }
        // update the op to include this ack request
        if (multiple && deliveryTag == 0)
        {
            // if have signalled to ack all, that refers only
            // to all at this time
            _ackOp.update(lastDeliveryTag, multiple);
        }
        else
        {
            _ackOp.update(deliveryTag, multiple);
        }
    }

    public void messageFullyReceived(boolean persistent) throws AMQException
    {
        // Not required in this transactional context
    }

    public void beginTranIfNecessary() throws AMQException
    {
        if (!_inTran)
        {
            _messageStore.beginTran();
            _inTran = true;
        }
    }

    public void commit() throws AMQException
    {
        if (_ackOp != null)
        {
            _ackOp.consolidate();
            if (_ackOp.checkPersistent())
            {
                _txnBuffer.containsPersistentChanges();
            }
            //already enlisted, after commit will reset regardless of outcome
            _ackOp = null;
        }

        _txnBuffer.commit();
        //TODO: may need to return 'immediate' messages at this point
    }
}