summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/ha/TxReplicator.cpp
blob: 0a524c8ced7b0aa514a47fac8b4e6ed71490b049 (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
/*
 *
 * 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.
 *
 */


#include "TxReplicator.h"
#include "Role.h"
#include "Backup.h"
#include "BrokerReplicator.h"
#include "Event.h"
#include "HaBroker.h"
#include "types.h"
#include "qpid/broker/Broker.h"
#include "qpid/broker/Link.h"
#include "qpid/broker/QueueRegistry.h"
#include "qpid/broker/Queue.h"
#include "qpid/broker/SessionHandler.h"
#include "qpid/broker/TxBuffer.h"
#include "qpid/broker/TxAccept.h"
#include "qpid/broker/amqp_0_10/Connection.h"
#include "qpid/framing/BufferTypes.h"
#include "qpid/log/Statement.h"
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/make_shared.hpp>
#include "qpid/broker/amqp_0_10/MessageTransfer.h"
#include "qpid/framing/MessageTransferBody.h"

namespace qpid {
namespace ha {

using namespace std;
using namespace boost;
using namespace qpid::broker;
using namespace qpid::framing;
using qpid::broker::amqp_0_10::MessageTransfer;
using qpid::types::Uuid;

namespace {
const string QPID_HA(QPID_HA_PREFIX);
const string TYPE_NAME(QPID_HA+"tx-replicator");
const string PREFIX(TRANSACTION_REPLICATOR_PREFIX);

} // namespace



bool TxReplicator::isTxQueue(const string& q) {
    return startsWith(q, PREFIX);
}

string TxReplicator::getTxId(const string& q) {
    assert(isTxQueue(q));
    return q.substr(PREFIX.size());
}

string TxReplicator::getType() const { return TYPE_NAME; }

TxReplicator::TxReplicator(
    HaBroker& hb,
    const boost::shared_ptr<broker::Queue>& txQueue,
    const boost::shared_ptr<broker::Link>& link) :
    QueueReplicator(hb, txQueue, link),
    txBuffer(new broker::TxBuffer),
    store(hb.getBroker().hasStore() ? &hb.getBroker().getStore() : 0),
    channel(link->nextChannel()),
    dequeueState(hb.getBroker().getQueues())
{
    string shortId = getTxId(txQueue->getName()).substr(0, 8);
    logPrefix = "Backup of transaction "+shortId+": ";

    if (!store) throw Exception(QPID_MSG(logPrefix << "No message store loaded."));
    boost::shared_ptr<Backup> backup = dynamic_pointer_cast<Backup>(hb.getRole());
    if (!backup) throw Exception(QPID_MSG(logPrefix << "Broker is not in backup mode."));
    brokerReplicator = backup->getBrokerReplicator();

    // Dispatch transaction events.
    dispatch[TxEnqueueEvent::KEY] =
        boost::bind(&TxReplicator::enqueue, this, _1, _2);
    dispatch[TxDequeueEvent::KEY] =
        boost::bind(&TxReplicator::dequeue, this, _1, _2);
    dispatch[TxPrepareEvent::KEY] =
        boost::bind(&TxReplicator::prepare, this, _1, _2);
    dispatch[TxCommitEvent::KEY] =
        boost::bind(&TxReplicator::commit, this, _1, _2);
    dispatch[TxRollbackEvent::KEY] =
        boost::bind(&TxReplicator::rollback, this, _1, _2);
}

TxReplicator::~TxReplicator() {
    link->returnChannel(channel);
}

// Send a message to the primary tx.
void TxReplicator::sendMessage(const broker::Message& msg, sys::Mutex::ScopedLock&) {
    assert(sessionHandler);
    const MessageTransfer& transfer(MessageTransfer::get(msg));
    for (FrameSet::const_iterator i = transfer.getFrames().begin();
         i != transfer.getFrames().end();
         ++i)
    {
        sessionHandler->out.handle(const_cast<AMQFrame&>(*i));
    }
}

void TxReplicator::deliver(const broker::Message& m_) {
    sys::Mutex::ScopedLock l(lock);
    // Deliver message to the target queue, not the tx-queue.
    broker::Message m(m_);
    m.setReplicationId(enq.id); // Use replicated id.
    boost::shared_ptr<broker::Queue> queue =
        haBroker.getBroker().getQueues().get(enq.queue);
    QPID_LOG(trace, logPrefix << "Deliver " << LogMessageId(*queue, m));
    DeliverableMessage dm(m, txBuffer.get());
    dm.deliverTo(queue);
}

void TxReplicator::destroy() {
    {
        sys::Mutex::ScopedLock l(lock);
        if (context.get()) store->abort(*context);
        txBuffer->rollback();
    }
    QueueReplicator::destroy();
}

void TxReplicator::enqueue(const string& data, sys::Mutex::ScopedLock&) {
    sys::Mutex::ScopedLock l(lock);
    TxEnqueueEvent e;
    decodeStr(data, e);
    QPID_LOG(trace, logPrefix << "Enqueue: " << e);
    enq = e;
}

void TxReplicator::dequeue(const string& data, sys::Mutex::ScopedLock&) {
    sys::Mutex::ScopedLock l(lock);
    TxDequeueEvent e;
    decodeStr(data, e);
    QPID_LOG(trace, logPrefix << "Dequeue: " << e);
    // NOTE: Backup does not see transactional dequeues until the transaction is
    // prepared, then they are all receieved before the prepare event.
    // We collect the events here so we can do a single scan of the queue in prepare.
    dequeueState.add(e);
}

void TxReplicator::DequeueState::add(const TxDequeueEvent& event) {
    events[event.queue] += event.id;
}

// Use this function as a seek() predicate to find the dequeued messages.
bool TxReplicator::DequeueState::addRecord(
    const broker::Message& m, const boost::shared_ptr<Queue>& queue,
    const ReplicationIdSet& rids)
{
    if (rids.contains(m.getReplicationId())) {
        DeliveryRecord dr(cursor, m.getSequence(), m.getReplicationId(), queue,
                          string() /*tag*/,
                          boost::shared_ptr<Consumer>(),
                          true /*acquired*/,
                          false /*accepted*/,
                          false /*credit.isWindowMode()*/,
                          0 /*credit*/);
        // Generate record ids, unique within this transaction.
        dr.setId(nextId++);
        records.push_back(dr);
        recordIds += dr.getId();
    }
    return false;
}

void TxReplicator::DequeueState::addRecords(const EventMap::value_type& entry) {
    // Process all the dequeues for a single queue, in one pass of seek()
    boost::shared_ptr<broker::Queue> q = queues.get(entry.first);
    q->seek(cursor, boost::bind(&TxReplicator::DequeueState::addRecord,
                                this, _1, q, entry.second));
}

boost::shared_ptr<TxAccept> TxReplicator::DequeueState::makeAccept() {
    for_each(events.begin(), events.end(),
             boost::bind(&TxReplicator::DequeueState::addRecords, this, _1));
    return make_shared<TxAccept>(cref(recordIds), ref(records));
}

void TxReplicator::prepare(const string&, sys::Mutex::ScopedLock& l) {
    txBuffer->enlist(dequeueState.makeAccept());
    context = store->begin();
    if (txBuffer->prepare(context.get())) {
        QPID_LOG(debug, logPrefix << "Prepared OK");
        sendMessage(TxPrepareOkEvent(haBroker.getSystemId()).message(queue->getName()), l);
    } else {
        QPID_LOG(debug, logPrefix << "Prepare failed");
        sendMessage(TxPrepareFailEvent(haBroker.getSystemId()).message(queue->getName()), l);
    }
}

void TxReplicator::commit(const string&, sys::Mutex::ScopedLock& l) {
    QPID_LOG(debug, logPrefix << "Commit");
    if (context.get()) store->commit(*context);
    txBuffer->commit();
    end(l);
}

void TxReplicator::rollback(const string&, sys::Mutex::ScopedLock& l) {
    QPID_LOG(debug, logPrefix << "Rollback");
    if (context.get()) store->abort(*context);
    txBuffer->rollback();
    end(l);
}

void TxReplicator::end(sys::Mutex::ScopedLock&) {
    // Destroy the tx-queue, which will destroy this via QueueReplicator destroy.
    // FIXME aconway 2013-08-01: what about connection & user ID for destroy?
    haBroker.getBroker().getQueues().destroy(queue->getName());
}

}} // namespace qpid::ha