]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/objectstore/TestObjectStoreState.h
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / test / objectstore / TestObjectStoreState.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2012 New Dream Network
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 */
13 #ifndef TEST_OBJECTSTORE_STATE_H_
14 #define TEST_OBJECTSTORE_STATE_H_
15
16 #include <boost/scoped_ptr.hpp>
17 #include <boost/random/mersenne_twister.hpp>
18 #include <boost/random/uniform_int.hpp>
19 #include <map>
20 #include <vector>
21
22 #include "os/ObjectStore.h"
23 #include "common/Cond.h"
24
25 typedef boost::mt11213b rngen_t;
26
27 class TestObjectStoreState {
28 public:
29 struct coll_entry_t {
30 spg_t m_pgid;
31 coll_t m_cid;
32 ghobject_t m_meta_obj;
33 ObjectStore::CollectionHandle m_ch;
34 std::map<int, hobject_t*> m_objects;
35 int m_next_object_id;
36
37 coll_entry_t(spg_t pgid, ObjectStore::CollectionHandle& ch,
38 char *meta_obj_buf)
39 : m_pgid(pgid),
40 m_cid(m_pgid),
41 m_meta_obj(hobject_t(sobject_t(object_t(meta_obj_buf), CEPH_NOSNAP))),
42 m_ch(ch),
43 m_next_object_id(0) {
44 m_meta_obj.hobj.pool = m_pgid.pool();
45 m_meta_obj.hobj.set_hash(m_pgid.ps());
46 }
47 ~coll_entry_t();
48
49 hobject_t *touch_obj(int id);
50 bool check_for_obj(int id);
51 hobject_t *get_obj(int id);
52 hobject_t *remove_obj(int id);
53 hobject_t *get_obj_at(int pos, int *key = NULL);
54 hobject_t *remove_obj_at(int pos, int *key = NULL);
55 hobject_t *replace_obj(int id, hobject_t *obj);
56 int get_random_obj_id(rngen_t& gen);
57
58 private:
59 hobject_t *get_obj(int id, bool remove);
60 hobject_t *get_obj_at(int pos, bool remove, int *key = NULL);
61 };
62
63 protected:
64 boost::shared_ptr<ObjectStore> m_store;
65 std::map<coll_t, coll_entry_t*> m_collections;
66 std::vector<coll_t> m_collections_ids;
67 int m_next_coll_nr;
68 int m_num_objs_per_coll;
69 int m_num_objects;
70
71 int m_max_in_flight;
72 std::atomic<int> m_in_flight = { 0 };
73 ceph::mutex m_finished_lock = ceph::make_mutex("Finished Lock");
74 ceph::condition_variable m_finished_cond;
75
76 void rebuild_id_vec() {
77 m_collections_ids.clear();
78 m_collections_ids.reserve(m_collections.size());
79 for (auto& i : m_collections) {
80 m_collections_ids.push_back(i.first);
81 }
82 }
83
84 void wait_for_ready() {
85 std::unique_lock locker{m_finished_lock};
86 m_finished_cond.wait(locker, [this] {
87 return m_max_in_flight <= 0 || m_in_flight < m_max_in_flight;
88 });
89 }
90
91 void wait_for_done() {
92 std::unique_lock locker{m_finished_lock};
93 m_finished_cond.wait(locker, [this] { return m_in_flight == 0; });
94 }
95
96 void set_max_in_flight(int max) {
97 m_max_in_flight = max;
98 }
99 void set_num_objs_per_coll(int val) {
100 m_num_objs_per_coll = val;
101 }
102
103 coll_entry_t *get_coll(coll_t cid, bool erase = false);
104 coll_entry_t *get_coll_at(int pos, bool erase = false);
105 int get_next_pool_id() { return m_next_pool++; }
106
107 private:
108 static const int m_default_num_colls = 30;
109 // The pool ID used for collection creation, ID 0 is preserve for other tests
110 int m_next_pool;
111
112 public:
113 explicit TestObjectStoreState(ObjectStore *store) :
114 m_next_coll_nr(0), m_num_objs_per_coll(10), m_num_objects(0),
115 m_max_in_flight(0), m_next_pool(2) {
116 m_store.reset(store);
117 }
118 ~TestObjectStoreState() {
119 auto it = m_collections.begin();
120 while (it != m_collections.end()) {
121 if (it->second)
122 delete it->second;
123 m_collections.erase(it++);
124 }
125 }
126
127 void init(int colls, int objs);
128 void init() {
129 init(m_default_num_colls, 0);
130 }
131
132 int inc_in_flight() {
133 return ++m_in_flight;
134 }
135
136 int dec_in_flight() {
137 return --m_in_flight;
138 }
139
140 coll_entry_t *coll_create(spg_t pgid, ObjectStore::CollectionHandle ch);
141
142 class C_OnFinished: public Context {
143 protected:
144 TestObjectStoreState *m_state;
145
146 public:
147 explicit C_OnFinished(TestObjectStoreState *state) : m_state(state) { }
148
149 void finish(int r) override {
150 std::lock_guard locker{m_state->m_finished_lock};
151 m_state->dec_in_flight();
152 m_state->m_finished_cond.notify_all();
153
154 }
155 };
156 };
157
158 #endif /* TEST_OBJECTSTORE_STATE_H_ */