1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "test/librados_test_stub/TestRadosClient.h"
5 #include "test/librados_test_stub/TestIoCtxImpl.h"
6 #include "librados/AioCompletionImpl.h"
7 #include "include/assert.h"
8 #include "common/ceph_json.h"
9 #include "common/Finisher.h"
10 #include <boost/bind.hpp>
11 #include <boost/thread.hpp>
14 static int get_concurrency() {
16 char *env
= getenv("LIBRADOS_CONCURRENCY");
18 concurrency
= atoi(env
);
20 if (concurrency
== 0) {
21 concurrency
= boost::thread::thread::hardware_concurrency();
23 if (concurrency
== 0) {
31 static void finish_aio_completion(AioCompletionImpl
*c
, int r
) {
37 rados_callback_t cb_complete
= c
->callback_complete
;
38 void *cb_complete_arg
= c
->callback_complete_arg
;
40 cb_complete(c
, cb_complete_arg
);
43 rados_callback_t cb_safe
= c
->callback_safe
;
44 void *cb_safe_arg
= c
->callback_safe_arg
;
46 cb_safe(c
, cb_safe_arg
);
50 c
->callback_complete
= NULL
;
51 c
->callback_safe
= NULL
;
56 class AioFunctionContext
: public Context
{
58 AioFunctionContext(const TestRadosClient::AioFunction
&callback
,
59 Finisher
*finisher
, AioCompletionImpl
*c
)
60 : m_callback(callback
), m_finisher(finisher
), m_comp(c
)
67 void finish(int r
) override
{
68 int ret
= m_callback();
70 if (m_finisher
!= NULL
) {
71 m_finisher
->queue(new FunctionContext(boost::bind(
72 &finish_aio_completion
, m_comp
, ret
)));
74 finish_aio_completion(m_comp
, ret
);
79 TestRadosClient::AioFunction m_callback
;
81 AioCompletionImpl
*m_comp
;
84 TestRadosClient::TestRadosClient(CephContext
*cct
,
85 TestWatchNotify
*watch_notify
)
86 : m_cct(cct
->get()), m_watch_notify(watch_notify
),
87 m_aio_finisher(new Finisher(m_cct
))
91 // simulate multiple OSDs
92 int concurrency
= get_concurrency();
93 for (int i
= 0; i
< concurrency
; ++i
) {
94 m_finishers
.push_back(new Finisher(m_cct
));
95 m_finishers
.back()->start();
98 // replicate AIO callback processing
99 m_aio_finisher
->start();
102 TestRadosClient::~TestRadosClient() {
103 flush_aio_operations();
105 for (size_t i
= 0; i
< m_finishers
.size(); ++i
) {
106 m_finishers
[i
]->stop();
107 delete m_finishers
[i
];
109 m_aio_finisher
->stop();
110 delete m_aio_finisher
;
116 void TestRadosClient::get() {
120 void TestRadosClient::put() {
121 if (m_refcount
.dec() == 0) {
127 CephContext
*TestRadosClient::cct() {
131 int TestRadosClient::connect() {
135 void TestRadosClient::shutdown() {
138 int TestRadosClient::wait_for_latest_osdmap() {
142 int TestRadosClient::mon_command(const std::vector
<std::string
>& cmd
,
143 const bufferlist
&inbl
,
144 bufferlist
*outbl
, std::string
*outs
) {
145 for (std::vector
<std::string
>::const_iterator it
= cmd
.begin();
146 it
!= cmd
.end(); ++it
) {
148 if (!parser
.parse(it
->c_str(), it
->length())) {
152 JSONObjIter j_it
= parser
.find("prefix");
157 if ((*j_it
)->get_data() == "osd tier add") {
159 } else if ((*j_it
)->get_data() == "osd tier cache-mode") {
161 } else if ((*j_it
)->get_data() == "osd tier set-overlay") {
163 } else if ((*j_it
)->get_data() == "osd tier remove-overlay") {
165 } else if ((*j_it
)->get_data() == "osd tier remove") {
172 void TestRadosClient::add_aio_operation(const std::string
& oid
,
174 const AioFunction
&aio_function
,
175 AioCompletionImpl
*c
) {
176 AioFunctionContext
*ctx
= new AioFunctionContext(
177 aio_function
, queue_callback
? m_aio_finisher
: NULL
, c
);
178 get_finisher(oid
)->queue(ctx
);
181 struct WaitForFlush
{
183 if (count
.dec() == 0) {
184 aio_finisher
->queue(new FunctionContext(boost::bind(
185 &finish_aio_completion
, c
, 0)));
192 Finisher
*aio_finisher
;
193 AioCompletionImpl
*c
;
196 void TestRadosClient::flush_aio_operations() {
197 AioCompletionImpl
*comp
= new AioCompletionImpl();
198 flush_aio_operations(comp
);
199 comp
->wait_for_safe();
203 void TestRadosClient::flush_aio_operations(AioCompletionImpl
*c
) {
206 WaitForFlush
*wait_for_flush
= new WaitForFlush();
207 wait_for_flush
->count
.set(m_finishers
.size());
208 wait_for_flush
->aio_finisher
= m_aio_finisher
;
209 wait_for_flush
->c
= c
;
211 for (size_t i
= 0; i
< m_finishers
.size(); ++i
) {
212 AioFunctionContext
*ctx
= new AioFunctionContext(
213 boost::bind(&WaitForFlush::flushed
, wait_for_flush
),
215 m_finishers
[i
]->queue(ctx
);
219 int TestRadosClient::aio_watch_flush(AioCompletionImpl
*c
) {
221 Context
*ctx
= new FunctionContext(boost::bind(
222 &TestRadosClient::finish_aio_completion
, this, c
, _1
));
223 get_watch_notify()->aio_flush(this, ctx
);
227 void TestRadosClient::finish_aio_completion(AioCompletionImpl
*c
, int r
) {
228 librados::finish_aio_completion(c
, r
);
231 Finisher
*TestRadosClient::get_finisher(const std::string
&oid
) {
232 std::size_t h
= m_hash(oid
);
233 return m_finishers
[h
% m_finishers
.size()];
236 } // namespace librados