summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/ha/PrimaryTxObserver.cpp
blob: 8a8364ac2267c1e846667d5b5ed1e60622118c4d (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
/*
 *
 * 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 "Event.h"
#include "HaBroker.h"
#include "Primary.h"
#include "PrimaryTxObserver.h"
#include "QueueGuard.h"
#include "RemoteBackup.h"
#include "ReplicatingSubscription.h"

#include "qpid/broker/Broker.h"
#include "qpid/broker/Queue.h"
#include <boost/lexical_cast.hpp>

namespace qpid {
namespace ha {

using namespace std;
using namespace boost;
using namespace broker;

PrimaryTxObserver::PrimaryTxObserver(HaBroker& hb) :
    haBroker(hb), broker(hb.getBroker()),
    id(true)          // FIXME aconway 2013-07-11: is UUID an appropriate TX ID?
{
    logPrefix = "Primary transaction "+id.str().substr(0,8)+": ";
    QPID_LOG(trace, logPrefix << "started");
    pair<shared_ptr<Queue>, bool> result =
        broker.getQueues().declare(
            TRANSACTION_REPLICATOR_PREFIX+id.str(),
            QueueSettings(/*durable*/false, /*autodelete*/true));
    assert(result.second);
    txQueue = result.first;
}

void PrimaryTxObserver::enqueue(const QueuePtr& q, const broker::Message& m)
{
    QPID_LOG(trace, logPrefix << "enqueue: " << LogMessageId(*q, m));
    enqueues[q] += m.getReplicationId();
    txQueue->deliver(TxEnqueueEvent(q->getName(), m.getReplicationId()).message());
    txQueue->deliver(m);
}

void PrimaryTxObserver::dequeue(
    const QueuePtr& q, QueuePosition pos, ReplicationId id)
{
    QPID_LOG(trace, logPrefix << "dequeue: " << LogMessageId(*q, pos, id));
    txQueue->deliver(TxDequeueEvent(q->getName(), id).message());
}

void PrimaryTxObserver::deduplicate() {
    shared_ptr<Primary> primary(boost::dynamic_pointer_cast<Primary>(haBroker.getRole()));
    assert(primary);
    // FIXME aconway 2013-07-29: need to verify which backups are *in* the transaction.
    // Use cluster membership for now
    BrokerInfo::Set brokers = haBroker.getMembership().getBrokers();
    types::Uuid selfId = haBroker.getMembership().getSelf();
    // Tell replicating subscriptions to skip IDs in the transaction.
    for (BrokerInfo::Set::iterator b = brokers.begin(); b != brokers.end(); ++b) {
        if (b->getSystemId() == selfId) continue;
        for (QueueIdsMap::iterator q = enqueues.begin(); q != enqueues.end(); ++q)
            primary->skip(b->getSystemId(), q->first, q->second);
    }
}

bool PrimaryTxObserver::prepare() {
    // FIXME aconway 2013-07-23: need to delay completion of prepare till all
    // backups have prepared.
    QPID_LOG(trace, logPrefix << "prepare");
    deduplicate();
    txQueue->deliver(TxPrepareEvent().message());
    return true;
}

void PrimaryTxObserver::commit() {
    QPID_LOG(trace, logPrefix << "commit");
    txQueue->deliver(TxCommitEvent().message());
}

void PrimaryTxObserver::rollback() {
    QPID_LOG(trace, logPrefix << "rollback");
    txQueue->deliver(TxRollbackEvent().message());
}

}} // namespace qpid::ha