summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/broker/DtxManager.cpp
blob: 3b23ea290013c1303fce6b992c67ea0f9d0ac147 (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
/*
 *
 * 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 "qpid/broker/DtxManager.h"
#include "qpid/broker/DtxTimeout.h"
#include "qpid/framing/reply_exceptions.h"
#include "qpid/framing/StructHelper.h"
#include "qpid/log/Statement.h"
#include "qpid/sys/Timer.h"
#include "qpid/ptr_map.h"

#include <boost/format.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

#include <iostream>

using boost::intrusive_ptr;
using qpid::sys::Mutex;
using qpid::ptr_map_ptr;
using namespace qpid::broker;
using namespace qpid::framing;

namespace {
    typedef boost::function0<void> FireFunction;
    struct DtxCleanup : public qpid::sys::TimerTask
    {
        FireFunction fireFunction;

        DtxCleanup(uint32_t timeout, FireFunction f);
        void fire();
    };

    DtxCleanup::DtxCleanup(uint32_t _timeout, FireFunction f)
    : TimerTask(qpid::sys::Duration(_timeout * qpid::sys::TIME_SEC),"DtxCleanup"), fireFunction(f){}

    void DtxCleanup::fire()
    {
        try {
            fireFunction();
        } catch (qpid::ConnectionException& /*e*/) {
            //assume it was explicitly cleaned up after a call to prepare, commit or rollback
        }
    }

}

//DtxManager::DtxManager(qpid::sys::Timer& t) : store(0), timer(&t) {}
DtxManager::DtxManager(qpid::sys::Timer& t) : asyncTxnStore(0), timer(&t) {}

DtxManager::~DtxManager() {}

void DtxManager::start(const std::string& xid, DtxBuffer::shared_ptr ops)
{
    createWork(xid)->add(ops);
}

void DtxManager::join(const std::string& xid, DtxBuffer::shared_ptr ops)
{
    getWork(xid)->add(ops);
}

void DtxManager::recover(const std::string& xid, std::auto_ptr<TPCTransactionContext> txn, DtxBuffer::shared_ptr ops)
{
    createWork(xid)->recover(txn, ops);
}

bool DtxManager::prepare(const std::string& xid)
{
    QPID_LOG(debug, "preparing: " << convert(xid));
    try {
        return getWork(xid)->prepare();
    } catch (DtxTimeoutException& e) {
        remove(xid);
        throw e;
    }
}

bool DtxManager::commit(const std::string& xid, bool onePhase)
{
    QPID_LOG(debug, "committing: " << convert(xid));
    try {
        bool result = getWork(xid)->commit(onePhase);
        remove(xid);
        return result;
    } catch (DtxTimeoutException& e) {
        remove(xid);
        throw e;
    }
}

void DtxManager::rollback(const std::string& xid)
{
    QPID_LOG(debug, "rolling back: " << convert(xid));
    try {
        getWork(xid)->rollback();
        remove(xid);
    } catch (DtxTimeoutException& e) {
        remove(xid);
        throw e;
    }
}

DtxWorkRecord* DtxManager::getWork(const std::string& xid)
{
    Mutex::ScopedLock locker(lock);
    WorkMap::iterator i = work.find(xid);
    if (i == work.end()) {
        throw NotFoundException(QPID_MSG("Unrecognised xid " << convert(xid)));
    }
    return ptr_map_ptr(i);
}

bool DtxManager::exists(const std::string& xid) {
    Mutex::ScopedLock locker(lock);
    return  work.find(xid) != work.end();
}

void DtxManager::remove(const std::string& xid)
{
    Mutex::ScopedLock locker(lock);
    WorkMap::iterator i = work.find(xid);
    if (i == work.end()) {
        throw NotFoundException(QPID_MSG("Unrecognised xid " << convert(xid)));
    } else {
        work.erase(i);
    }
}

DtxWorkRecord* DtxManager::createWork(const std::string& xid)
{
    Mutex::ScopedLock locker(lock);
    WorkMap::iterator i = work.find(xid);
    if (i != work.end()) {
        throw NotAllowedException(QPID_MSG("Xid " << convert(xid) << " is already known (use 'join' to add work to an existing xid)"));
    } else {
        std::string ncxid = xid; // Work around const correctness problems in ptr_map.
//        return ptr_map_ptr(work.insert(ncxid, new DtxWorkRecord(ncxid, store)).first);
        return ptr_map_ptr(work.insert(ncxid, new DtxWorkRecord(ncxid, asyncTxnStore)).first);
    }
}

void DtxManager::setTimeout(const std::string& xid, uint32_t secs)
{
    DtxWorkRecord* record = getWork(xid);
    intrusive_ptr<DtxTimeout> timeout = record->getTimeout();
    if (timeout.get()) {
        if (timeout->timeout == secs) return;//no need to do anything further if timeout hasn't changed
        timeout->cancel();
    }
    timeout = intrusive_ptr<DtxTimeout>(new DtxTimeout(secs, *this, xid));
    record->setTimeout(timeout);
    timer->add(timeout);
}

uint32_t DtxManager::getTimeout(const std::string& xid)
{
    intrusive_ptr<DtxTimeout> timeout = getWork(xid)->getTimeout();
    return !timeout ? 0 : timeout->timeout;
}

void DtxManager::timedout(const std::string& xid)
{
    Mutex::ScopedLock locker(lock);
    WorkMap::iterator i = work.find(xid);
    if (i == work.end()) {
        QPID_LOG(warning, "Transaction timeout failed: no record for xid");
    } else {
        ptr_map_ptr(i)->timedout();
        //TODO: do we want to have a timed task to cleanup, or can we rely on an explicit completion?
        //timer->add(new DtxCleanup(60*30/*30 mins*/, boost::bind(&DtxManager::remove, this, xid)));
    }
}

//void DtxManager::setStore (TransactionalStore* _store)
void DtxManager::setStore (AsyncTransactionalStore* const _ats)
{
//    store = _store;
    asyncTxnStore = _ats;
}

std::string DtxManager::convert(const qpid::framing::Xid& xid)
{
    qpid::framing::StructHelper helper;
    std::string encoded;
    helper.encode(xid, encoded);
    return encoded;
}

qpid::framing::Xid DtxManager::convert(const std::string& xid)
{
    qpid::framing::StructHelper helper;
    qpid::framing::Xid decoded;
    helper.decode(decoded, xid);
    return decoded;
}