1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "test/librados_test_stub/TestMemCluster.h"
5 #include "test/librados_test_stub/TestMemRadosClient.h"
6
7 namespace librados {
8
9 TestMemCluster::File::File()
10 : snap_id(), exists(true) {
11 }
12
13 TestMemCluster::File::File(const File &rhs)
14 : data(rhs.data),
15 mtime(rhs.mtime),
16 snap_id(rhs.snap_id),
17 exists(rhs.exists) {
18 }
19
20 TestMemCluster::Pool::Pool() = default;
21
22 TestMemCluster::TestMemCluster()
23 : m_next_nonce(static_cast<uint32_t>(reinterpret_cast<uint64_t>(this))) {
24 }
25
(1) Event exn_spec_violation: |
An exception of type "std::length_error" is thrown but the throw list "throw()" doesn't allow it to be thrown. This will cause a call to unexpected() which usually calls terminate(). |
Also see events: |
[fun_call_w_exception] |
26 TestMemCluster::~TestMemCluster() {
27 for (auto pool_pair : m_pools) {
(2) Event fun_call_w_exception: |
Called function throws an exception of type "std::length_error". [details] |
Also see events: |
[exn_spec_violation] |
28 pool_pair.second->put();
29 }
30 }
31
32 TestRadosClient *TestMemCluster::create_rados_client(CephContext *cct) {
33 return new TestMemRadosClient(cct, this);
34 }
35
36 int TestMemCluster::register_object_handler(int64_t pool_id,
37 const ObjectLocator& locator,
38 ObjectHandler* object_handler) {
39 std::lock_guard locker{m_lock};
40 auto pool = get_pool(m_lock, pool_id);
41 if (pool == nullptr) {
42 return -ENOENT;
43 }
44
45 std::unique_lock pool_locker{pool->file_lock};
46 auto file_it = pool->files.find(locator);
47 if (file_it == pool->files.end()) {
48 return -ENOENT;
49 }
50
51 auto& object_handlers = pool->file_handlers[locator];
52 auto it = object_handlers.find(object_handler);
53 ceph_assert(it == object_handlers.end());
54
55 object_handlers.insert(object_handler);
56 return 0;
57 }
58
59 void TestMemCluster::unregister_object_handler(int64_t pool_id,
60 const ObjectLocator& locator,
61 ObjectHandler* object_handler) {
62 std::lock_guard locker{m_lock};
63 auto pool = get_pool(m_lock, pool_id);
64 if (pool == nullptr) {
65 return;
66 }
67
68 std::unique_lock pool_locker{pool->file_lock};
69 auto handlers_it = pool->file_handlers.find(locator);
70 if (handlers_it == pool->file_handlers.end()) {
71 return;
72 }
73
74 auto& object_handlers = handlers_it->second;
75 object_handlers.erase(object_handler);
76 }
77
78 int TestMemCluster::pool_create(const std::string &pool_name) {
79 std::lock_guard locker{m_lock};
80 if (m_pools.find(pool_name) != m_pools.end()) {
81 return -EEXIST;
82 }
83 Pool *pool = new Pool();
84 pool->pool_id = ++m_pool_id;
85 m_pools[pool_name] = pool;
86 return 0;
87 }
88
89 int TestMemCluster::pool_delete(const std::string &pool_name) {
90 std::lock_guard locker{m_lock};
91 Pools::iterator iter = m_pools.find(pool_name);
92 if (iter == m_pools.end()) {
93 return -ENOENT;
94 }
95 iter->second->put();
96 m_pools.erase(iter);
97 return 0;
98 }
99
100 int TestMemCluster::pool_get_base_tier(int64_t pool_id, int64_t* base_tier) {
101 // TODO
102 *base_tier = pool_id;
103 return 0;
104 }
105
106 int TestMemCluster::pool_list(std::list<std::pair<int64_t, std::string> >& v) {
107 std::lock_guard locker{m_lock};
108 v.clear();
109 for (Pools::iterator iter = m_pools.begin(); iter != m_pools.end(); ++iter) {
110 v.push_back(std::make_pair(iter->second->pool_id, iter->first));
111 }
112 return 0;
113 }
114
115 int64_t TestMemCluster::pool_lookup(const std::string &pool_name) {
116 std::lock_guard locker{m_lock};
117 Pools::iterator iter = m_pools.find(pool_name);
118 if (iter == m_pools.end()) {
119 return -ENOENT;
120 }
121 return iter->second->pool_id;
122 }
123
124 int TestMemCluster::pool_reverse_lookup(int64_t id, std::string *name) {
125 std::lock_guard locker{m_lock};
126 for (Pools::iterator iter = m_pools.begin(); iter != m_pools.end(); ++iter) {
127 if (iter->second->pool_id == id) {
128 *name = iter->first;
129 return 0;
130 }
131 }
132 return -ENOENT;
133 }
134
135 TestMemCluster::Pool *TestMemCluster::get_pool(int64_t pool_id) {
136 std::lock_guard locker{m_lock};
137 return get_pool(m_lock, pool_id);
138 }
139
140 TestMemCluster::Pool *TestMemCluster::get_pool(const ceph::mutex& lock,
141 int64_t pool_id) {
142 for (auto &pool_pair : m_pools) {
143 if (pool_pair.second->pool_id == pool_id) {
144 return pool_pair.second;
145 }
146 }
147 return nullptr;
148 }
149
150 TestMemCluster::Pool *TestMemCluster::get_pool(const std::string &pool_name) {
151 std::lock_guard locker{m_lock};
152 Pools::iterator iter = m_pools.find(pool_name);
153 if (iter != m_pools.end()) {
154 return iter->second;
155 }
156 return nullptr;
157 }
158
159 void TestMemCluster::allocate_client(uint32_t *nonce, uint64_t *global_id) {
160 std::lock_guard locker{m_lock};
161 *nonce = m_next_nonce++;
162 *global_id = m_next_global_id++;
163 }
164
165 void TestMemCluster::deallocate_client(uint32_t nonce) {
166 std::lock_guard locker{m_lock};
167 m_blacklist.erase(nonce);
168 }
169
170 bool TestMemCluster::is_blacklisted(uint32_t nonce) const {
171 std::lock_guard locker{m_lock};
172 return (m_blacklist.find(nonce) != m_blacklist.end());
173 }
174
175 void TestMemCluster::blacklist(uint32_t nonce) {
176 m_watch_notify.blacklist(nonce);
177
178 std::lock_guard locker{m_lock};
179 m_blacklist.insert(nonce);
180 }
181
182 void TestMemCluster::transaction_start(const ObjectLocator& locator) {
183 std::unique_lock locker{m_lock};
184 m_transaction_cond.wait(locker, [&locator, this] {
185 return m_transactions.count(locator) == 0;
186 });
187 auto result = m_transactions.insert(locator);
188 ceph_assert(result.second);
189 }
190
191 void TestMemCluster::transaction_finish(const ObjectLocator& locator) {
192 std::lock_guard locker{m_lock};
193 size_t count = m_transactions.erase(locator);
194 ceph_assert(count == 1);
195 m_transaction_cond.notify_all();
196 }
197
198 } // namespace librados
199
200