summaryrefslogtreecommitdiff
path: root/cpp/src/tests/storePerftools/asyncPerf/SimpleQueue.h
blob: 81ea8b022bf61d5bd6c2d7db07e88a9d3b06abcd (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
/*
 * 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.
 */

/**
 * \file SimpleQueue.h
 */

#ifndef tests_storePerftools_asyncPerf_SimpleQueue_h_
#define tests_storePerftools_asyncPerf_SimpleQueue_h_

#include "qpid/asyncStore/AtomicCounter.h" // AsyncOpCounter
#include "qpid/broker/AsyncStore.h" // qpid::broker::DataSource
#include "qpid/broker/PersistableQueue.h"
#include "qpid/broker/QueueHandle.h"
#include "qpid/sys/Monitor.h"

#include <boost/intrusive_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

namespace qpid {
namespace asyncStore {
class AsyncStoreImpl;
}
namespace broker {
class AsyncResultQueue;
}
namespace framing {
class FieldTable;
}}

namespace tests {
namespace storePerftools {
namespace asyncPerf {

class MessageConsumer;
class Messages;
class PersistableQueuedMessage;
class QueueAsyncContext;
class QueuedMessage;
class SimpleMessage;

class SimpleQueue : public boost::enable_shared_from_this<SimpleQueue>,
                    public qpid::broker::PersistableQueue,
                    public qpid::broker::DataSource
{
public:
    SimpleQueue(const std::string& name,
                const qpid::framing::FieldTable& args,
                qpid::broker::AsyncStore* store,
                qpid::broker::AsyncResultQueue& arq);
    virtual ~SimpleQueue();

    static void handleAsyncResult(const qpid::broker::AsyncResultHandle* const res);
    const qpid::broker::QueueHandle& getHandle() const;
    qpid::broker::QueueHandle& getHandle();
    qpid::broker::AsyncStore* getStore();

    void asyncCreate();
    void asyncDestroy(const bool deleteQueue);

    // --- Methods in msg handling path from qpid::Queue ---
    void deliver(boost::intrusive_ptr<SimpleMessage> msg);
    bool dispatch(MessageConsumer& mc);
    bool enqueue(boost::shared_ptr<QueuedMessage> qm);
    bool enqueue(qpid::broker::TxnHandle& th,
                 boost::shared_ptr<QueuedMessage> qm);
    bool dequeue(boost::shared_ptr<QueuedMessage> qm);
    bool dequeue(qpid::broker::TxnHandle& th,
                 boost::shared_ptr<QueuedMessage> qm);
    void process(boost::intrusive_ptr<SimpleMessage> msg);
    void enqueueAborted(boost::intrusive_ptr<SimpleMessage> msg);

    // --- Interface qpid::broker::Persistable ---
    virtual void encode(qpid::framing::Buffer& buffer) const;
    virtual uint32_t encodedSize() const;
    virtual uint64_t getPersistenceId() const;
    virtual void setPersistenceId(uint64_t persistenceId) const;

    // --- Interface qpid::broker::PersistableQueue ---
    virtual void flush();
    virtual const std::string& getName() const;
    virtual void setExternalQueueStore(qpid::broker::ExternalQueueStore* inst);

    // --- Interface DataStore ---
    virtual uint64_t getSize();
    virtual void write(char* target);

private:
    static qpid::broker::TxnHandle s_nullTxnHandle; // used for non-txn operations

    const std::string m_name;
    qpid::broker::AsyncStore* m_store;
    qpid::broker::AsyncResultQueue& m_resultQueue;
    qpid::asyncStore::AsyncOpCounter m_asyncOpCounter; // TODO: change this to non-async store counter!
    mutable uint64_t m_persistenceId;
    std::string m_persistableData;
    qpid::broker::QueueHandle m_queueHandle;
    bool m_destroyPending;
    bool m_destroyed;

    // --- Members & methods in msg handling path copied from qpid::Queue ---
    struct UsageBarrier
    {
        SimpleQueue& m_parent;
        uint32_t m_count;
        qpid::sys::Monitor m_monitor;
        UsageBarrier(SimpleQueue& q);
        bool acquire();
        void release();
        void destroy();
    };
    struct ScopedUse
    {
        UsageBarrier& m_barrier;
        const bool m_acquired;
        ScopedUse(UsageBarrier& b);
        ~ScopedUse();
    };
    UsageBarrier m_barrier;
    std::auto_ptr<Messages> m_messages;
    void push(boost::shared_ptr<QueuedMessage> qm,
              bool isRecovery = false);

    // -- Async ops ---
    bool asyncEnqueue(qpid::broker::TxnHandle& th,
                      boost::shared_ptr<PersistableQueuedMessage> pqm);
    bool asyncDequeue(qpid::broker::TxnHandle& th,
                      boost::shared_ptr<PersistableQueuedMessage> pqm);

    // --- Async op counter ---
    void destroyCheck(const std::string& opDescr) const;

    // --- Async op completions (called through handleAsyncResult) ---
    void createComplete(const boost::shared_ptr<QueueAsyncContext> qc);
    void flushComplete(const boost::shared_ptr<QueueAsyncContext> qc);
    void destroyComplete(const boost::shared_ptr<QueueAsyncContext> qc);
    void enqueueComplete(const boost::shared_ptr<QueueAsyncContext> qc);
    void dequeueComplete(const boost::shared_ptr<QueueAsyncContext> qc);
};

}}} // namespace tests::storePerftools::asyncPerf

#endif // tests_storePerftools_asyncPerf_SimpleQueue_h_