summaryrefslogtreecommitdiff
path: root/cpp/src/tests/legacystore/TransactionalTest.cpp
blob: 2d3f6f922c037019bde7cb3da160e7be0cfdc1e7 (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
/*
 *
 * 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 "unit_test.h"

#include "qpid/legacystore/MessageStoreImpl.h"
#include <iostream>
#include "MessageUtils.h"
#include "qpid/legacystore/StoreException.h"
#include "qpid/broker/Queue.h"
#include "qpid/broker/RecoveryManagerImpl.h"
#include "qpid/framing/AMQHeaderBody.h"
#include "qpid/log/Statement.h"
#include "qpid/log/Logger.h"
#include "qpid/sys/Timer.h"

using namespace mrg::msgstore;
using namespace qpid;
using namespace qpid::broker;
using namespace qpid::framing;
using namespace std;

namespace {
qpid::broker::Broker::Options opts;
qpid::broker::Broker br(opts);
}

QPID_AUTO_TEST_SUITE(TransactionalTest)

#define SET_LOG_LEVEL(level) \
    qpid::log::Options opts(""); \
    opts.selectors.clear(); \
    opts.selectors.push_back(level); \
    qpid::log::Logger::instance().configure(opts);

const string test_filename("TransactionalTest");
const char* tdp = getenv("TMP_DATA_DIR");
const string test_dir(tdp && strlen(tdp) > 0 ? tdp : "/tmp/TransactionalTest");

// Test txn context which has special setCompleteFailure() method which prevents entire "txn complete" process from hapenning
class TestTxnCtxt : public TxnCtxt
{
  public:
    TestTxnCtxt(IdSequence* _loggedtx) : TxnCtxt(_loggedtx) {}
    void setCompleteFailure(const unsigned num_queues_rem) {
        // Remove queue members from back of impactedQueues until queues_rem reamin.
        // to end to simulate multi-queue txn complete failure.
        while (impactedQueues.size() > num_queues_rem) impactedQueues.erase(impactedQueues.begin());
    }
    void resetPreparedXidStorePtr() { preparedXidStorePtr = 0; }
};

// Test store which has special begin() which returns a TestTPCTxnCtxt, and a method to check for
// remaining open transactions.
// begin(), commit(), and abort() all hide functions in MessageStoreImpl. To avoid the compiler
// warnings/errors these are renamed with a 'TMS' prefix.
class TestMessageStore: public MessageStoreImpl
{
  public:
    TestMessageStore(qpid::broker::Broker* br, const char* envpath = 0) : MessageStoreImpl(br, envpath) {}
    std::auto_ptr<qpid::broker::TransactionContext> TMSbegin() {
        checkInit();
        // pass sequence number for c/a
        return auto_ptr<TransactionContext>(new TestTxnCtxt(&messageIdSequence));
    }
    void TMScommit(TransactionContext& ctxt, const bool complete_prepared_list) {
        checkInit();
        TxnCtxt* txn(check(&ctxt));
        if (!txn->isTPC()) {
            localPrepare(dynamic_cast<TxnCtxt*>(txn));
            if (!complete_prepared_list) dynamic_cast<TestTxnCtxt*>(txn)->resetPreparedXidStorePtr();
        }
        completed(*dynamic_cast<TxnCtxt*>(txn), true);
    }
    void TMSabort(TransactionContext& ctxt, const bool complete_prepared_list)
    {
        checkInit();
        TxnCtxt* txn(check(&ctxt));
        if (!txn->isTPC()) {
            localPrepare(dynamic_cast<TxnCtxt*>(txn));
            if (!complete_prepared_list) dynamic_cast<TestTxnCtxt*>(txn)->resetPreparedXidStorePtr();
        }
        completed(*dynamic_cast<TxnCtxt*>(txn), false);
    }
};

// === Helper fns ===

const string nameA("queueA");
const string nameB("queueB");
//const Uuid messageId(true);
std::auto_ptr<MessageStoreImpl> store;
std::auto_ptr<QueueRegistry> queues;
Queue::shared_ptr queueA;
Queue::shared_ptr queueB;

template <class T>
void setup()
{
    store = std::auto_ptr<T>(new T(&br));
    store->init(test_dir, 4, 1, true); // truncate store

    //create two queues:
    queueA = Queue::shared_ptr(new Queue(nameA, 0, store.get(), 0));
    queueA->create();
    queueB = Queue::shared_ptr(new Queue(nameB, 0, store.get(), 0));
    queueB->create();
}

template <class T>
void restart()
{
    queueA.reset();
    queueB.reset();
    queues.reset();
    store.reset();

    store = std::auto_ptr<T>(new T(&br));
    store->init(test_dir, 4, 1);
    queues = std::auto_ptr<QueueRegistry>(new QueueRegistry);
    ExchangeRegistry exchanges;
    LinkRegistry links;
    sys::Timer t;
    DtxManager mgr(t);
    mgr.setStore (store.get());
    RecoveryManagerImpl recovery(*queues, exchanges, links, mgr, br.getProtocolRegistry());
    store->recover(recovery);

    queueA = queues->find(nameA);
    queueB = queues->find(nameB);
}

Message createMessage(const string& id, const string& exchange="exchange", const string& key="routing_key")
{
    return MessageUtils::createMessage(exchange, key, Uuid(), true, 0, id);
}

void checkMsg(Queue::shared_ptr& queue, u_int32_t size, const string& msgid = "<none>")
{
    BOOST_REQUIRE(queue);
    BOOST_CHECK_EQUAL(size, queue->getMessageCount());
    if (size > 0) {
        Message msg = MessageUtils::get(*queue);
        BOOST_REQUIRE(msg);
        BOOST_CHECK_EQUAL(msgid, MessageUtils::getCorrelationId(msg));
    }
}

void swap(bool commit)
{
    setup<MessageStoreImpl>();

    //create message and enqueue it onto first queue:
    Message msgA = createMessage("Message", "exchange", "routing_key");
    queueA->deliver(msgA);

    QueueCursor cursorB;
    Message msgB = MessageUtils::get(*queueA, &cursorB);
    BOOST_REQUIRE(msgB);
    //move the message from one queue to the other as a transaction
    std::auto_ptr<TransactionContext> txn = store->begin();
    TxBuffer tx;
    queueB->deliver(msgB, &tx);//note: need to enqueue it first to avoid message being deleted

    queueA->dequeue(txn.get(), cursorB);
    tx.prepare(txn.get());
    if (commit) {
        store->commit(*txn);
    } else {
        store->abort(*txn);
    }

    restart<MessageStoreImpl>();

    // Check outcome
    BOOST_REQUIRE(queueA);
    BOOST_REQUIRE(queueB);

    Queue::shared_ptr x;//the queue from which the message was swapped
    Queue::shared_ptr y;//the queue on which the message is expected to be

    if (commit) {
        x = queueA;
        y = queueB;
    } else {
        x = queueB;
        y = queueA;
    }

    checkMsg(x, 0);
    checkMsg(y, 1, "Message");
    checkMsg(y, 0);
}

void testMultiQueueTxn(const unsigned num_queues_rem, const bool complete_prepared_list, const bool commit)
{
    setup<TestMessageStore>();
    TestMessageStore* tmsp = static_cast<TestMessageStore*>(store.get());
    std::auto_ptr<TransactionContext> txn(tmsp->TMSbegin());
    TxBuffer tx;

    //create two messages and enqueue them onto both queues:
    Message msgA = createMessage("MessageA", "exchange", "routing_key");
    queueA->deliver(msgA, &tx);
    queueB->deliver(msgA, &tx);
    Message msgB = createMessage("MessageB", "exchange", "routing_key");
    queueA->deliver(msgB, &tx);
    queueB->deliver(msgB, &tx);

    tx.prepare(txn.get());
    static_cast<TestTxnCtxt*>(txn.get())->setCompleteFailure(num_queues_rem);
    if (commit)
        tmsp->TMScommit(*txn, complete_prepared_list);
    else
        tmsp->TMSabort(*txn, complete_prepared_list);
    restart<TestMessageStore>();

    // Check outcome
    if (commit)
    {
        checkMsg(queueA, 2, "MessageA");
        checkMsg(queueB, 2, "MessageA");
        checkMsg(queueA, 1, "MessageB");
        checkMsg(queueB, 1, "MessageB");
    }
    checkMsg(queueA, 0);
    checkMsg(queueB, 0);
}

// === Test suite ===

QPID_AUTO_TEST_CASE(Commit)
{
    SET_LOG_LEVEL("error+"); // This only needs to be set once.

    cout << test_filename << ".Commit: " << flush;
    swap(true);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(Abort)
{
    cout << test_filename << ".Abort: " << flush;
    swap(false);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueCommit)
{
    cout << test_filename << ".MultiQueueCommit: " << flush;
    testMultiQueueTxn(2, true, true);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueAbort)
{
    cout << test_filename << ".MultiQueueAbort: " << flush;
    testMultiQueueTxn(2, true, false);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueNoQueueCommitRecover)
{
    cout << test_filename << ".MultiQueueNoQueueCommitRecover: " << flush;
    testMultiQueueTxn(0, false, true);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueNoQueueAbortRecover)
{
    cout << test_filename << ".MultiQueueNoQueueAbortRecover: " << flush;
    testMultiQueueTxn(0, false, false);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueSomeQueueCommitRecover)
{
    cout << test_filename << ".MultiQueueSomeQueueCommitRecover: " << flush;
    testMultiQueueTxn(1, false, true);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueSomeQueueAbortRecover)
{
    cout << test_filename << ".MultiQueueSomeQueueAbortRecover: " << flush;
    testMultiQueueTxn(1, false, false);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueAllQueueCommitRecover)
{
    cout << test_filename << ".MultiQueueAllQueueCommitRecover: " << flush;
    testMultiQueueTxn(2, false, true);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(MultiQueueAllQueueAbortRecover)
{
    cout << test_filename << ".MultiQueueAllQueueAbortRecover: " << flush;
    testMultiQueueTxn(2, false, false);
    cout << "ok" << endl;
}

QPID_AUTO_TEST_CASE(LockedRecordTest)
{
    cout << test_filename << ".LockedRecordTest: " << flush;

    setup<MessageStoreImpl>();
    queueA->deliver(createMessage("Message", "exchange", "routingKey"));
    std::auto_ptr<TransactionContext> txn = store->begin();

    QueueCursor cursor;
    Message msg = MessageUtils::get(*queueA, &cursor);
    queueA->dequeue(txn.get(), cursor);

    try {
        store->dequeue(0, msg.getPersistentContext(), *queueA);
        BOOST_ERROR("Did not throw JERR_MAP_LOCKED exception as expected.");
    }
    catch (const mrg::msgstore::StoreException& e) {
        if (std::strstr(e.what(), "JERR_MAP_LOCKED") == 0)
            BOOST_ERROR("Unexpected StoreException: " << e.what());
    }
    catch (const std::exception& e) {
        BOOST_ERROR("Unexpected exception: " << e.what());
    }
    store->commit(*txn);
    checkMsg(queueA, 0);

    cout << "ok" << endl;
}

QPID_AUTO_TEST_SUITE_END()