]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
11fdf7f2 | 4 | #include "test/librados/test_cxx.h" |
7c673cae FG |
5 | #include "test/journal/RadosTestFixture.h" |
6 | #include "cls/journal/cls_journal_client.h" | |
7 | #include "include/stringify.h" | |
8 | #include "common/WorkQueue.h" | |
9 | #include "journal/Settings.h" | |
10 | ||
20effc67 TL |
11 | using namespace std::chrono_literals; |
12 | ||
7c673cae | 13 | RadosTestFixture::RadosTestFixture() |
9f95a23c TL |
14 | : m_timer_lock(ceph::make_mutex("m_timer_lock")), |
15 | m_listener(this) { | |
7c673cae FG |
16 | } |
17 | ||
18 | void RadosTestFixture::SetUpTestCase() { | |
19 | _pool_name = get_temp_pool_name(); | |
20 | ASSERT_EQ("", create_one_pool_pp(_pool_name, _rados)); | |
21 | ||
22 | CephContext* cct = reinterpret_cast<CephContext*>(_rados.cct()); | |
23 | _thread_pool = new ThreadPool(cct, "RadosTestFixture::_thread_pool", | |
24 | "tp_test", 1); | |
25 | _thread_pool->start(); | |
26 | } | |
27 | ||
28 | void RadosTestFixture::TearDownTestCase() { | |
29 | _thread_pool->stop(); | |
30 | delete _thread_pool; | |
31 | ||
32 | ASSERT_EQ(0, destroy_one_pool_pp(_pool_name, _rados)); | |
33 | } | |
34 | ||
35 | std::string RadosTestFixture::get_temp_oid() { | |
36 | ++_oid_number; | |
37 | return "oid" + stringify(_oid_number); | |
38 | } | |
39 | ||
40 | void RadosTestFixture::SetUp() { | |
41 | ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), m_ioctx)); | |
42 | ||
43 | CephContext* cct = reinterpret_cast<CephContext*>(m_ioctx.cct()); | |
f67539c2 TL |
44 | m_work_queue = new ContextWQ("RadosTestFixture::m_work_queue", |
45 | ceph::make_timespan(60), | |
7c673cae FG |
46 | _thread_pool); |
47 | ||
48 | m_timer = new SafeTimer(cct, m_timer_lock, true); | |
49 | m_timer->init(); | |
50 | } | |
51 | ||
52 | void RadosTestFixture::TearDown() { | |
53 | for (auto metadata : m_metadatas) { | |
54 | C_SaferCond ctx; | |
55 | metadata->shut_down(&ctx); | |
56 | ASSERT_EQ(0, ctx.wait()); | |
57 | } | |
58 | ||
59 | { | |
9f95a23c | 60 | std::lock_guard locker{m_timer_lock}; |
7c673cae FG |
61 | m_timer->shutdown(); |
62 | } | |
63 | delete m_timer; | |
64 | ||
65 | m_work_queue->drain(); | |
66 | delete m_work_queue; | |
67 | } | |
68 | ||
69 | int RadosTestFixture::create(const std::string &oid, uint8_t order, | |
70 | uint8_t splay_width) { | |
71 | return cls::journal::client::create(m_ioctx, oid, order, splay_width, -1); | |
72 | } | |
73 | ||
9f95a23c | 74 | ceph::ref_t<journal::JournalMetadata> RadosTestFixture::create_metadata( |
7c673cae | 75 | const std::string &oid, const std::string &client_id, |
9f95a23c | 76 | double commit_interval, int max_concurrent_object_sets) { |
7c673cae FG |
77 | journal::Settings settings; |
78 | settings.commit_interval = commit_interval; | |
7c673cae FG |
79 | settings.max_concurrent_object_sets = max_concurrent_object_sets; |
80 | ||
9f95a23c TL |
81 | auto metadata = ceph::make_ref<journal::JournalMetadata>( |
82 | m_work_queue, m_timer, &m_timer_lock, m_ioctx, oid, client_id, settings); | |
7c673cae FG |
83 | m_metadatas.push_back(metadata); |
84 | return metadata; | |
85 | } | |
86 | ||
87 | int RadosTestFixture::append(const std::string &oid, const bufferlist &bl) { | |
88 | librados::ObjectWriteOperation op; | |
89 | op.append(bl); | |
90 | return m_ioctx.operate(oid, &op); | |
91 | } | |
92 | ||
93 | int RadosTestFixture::client_register(const std::string &oid, | |
94 | const std::string &id, | |
95 | const std::string &description) { | |
96 | bufferlist data; | |
97 | data.append(description); | |
98 | return cls::journal::client::client_register(m_ioctx, oid, id, data); | |
99 | } | |
100 | ||
101 | int RadosTestFixture::client_commit(const std::string &oid, | |
102 | const std::string &id, | |
103 | const cls::journal::ObjectSetPosition &commit_position) { | |
104 | librados::ObjectWriteOperation op; | |
105 | cls::journal::client::client_commit(&op, id, commit_position); | |
106 | return m_ioctx.operate(oid, &op); | |
107 | } | |
108 | ||
109 | bufferlist RadosTestFixture::create_payload(const std::string &payload) { | |
110 | bufferlist bl; | |
111 | bl.append(payload); | |
112 | return bl; | |
113 | } | |
114 | ||
9f95a23c | 115 | int RadosTestFixture::init_metadata(const ceph::ref_t<journal::JournalMetadata>& metadata) { |
7c673cae FG |
116 | C_SaferCond cond; |
117 | metadata->init(&cond); | |
118 | return cond.wait(); | |
119 | } | |
120 | ||
9f95a23c TL |
121 | bool RadosTestFixture::wait_for_update(const ceph::ref_t<journal::JournalMetadata>& metadata) { |
122 | std::unique_lock locker{m_listener.mutex}; | |
7c673cae | 123 | while (m_listener.updates[metadata.get()] == 0) { |
9f95a23c | 124 | if (m_listener.cond.wait_for(locker, 10s) == std::cv_status::timeout) { |
7c673cae FG |
125 | return false; |
126 | } | |
127 | } | |
128 | --m_listener.updates[metadata.get()]; | |
129 | return true; | |
130 | } | |
131 | ||
132 | std::string RadosTestFixture::_pool_name; | |
133 | librados::Rados RadosTestFixture::_rados; | |
134 | uint64_t RadosTestFixture::_oid_number = 0; | |
135 | ThreadPool *RadosTestFixture::_thread_pool = nullptr; |