Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "os/ObjectStore.h"
17 #include <boost/scoped_ptr.hpp>
18 #include <boost/random/mersenne_twister.hpp>
19 #include <boost/random/uniform_int.hpp>
20 #include <map>
21 #include <vector>
22
23 typedef boost::mt11213b rngen_t;
24
25 class TestObjectStoreState {
26 public:
27   struct coll_entry_t {
28     int m_id;
29     spg_t m_pgid;
30     coll_t m_coll;
31     ghobject_t m_meta_obj;
32     ObjectStore::Sequencer m_osr;
33     map<int, hobject_t*> m_objects;
34     int m_next_object_id;
35
36     coll_entry_t(int i, char *coll_buf, char *meta_obj_buf)
37       : m_id(i),
38         m_pgid(pg_t(i, 1), shard_id_t::NO_SHARD),
39         m_coll(m_pgid),
40         m_meta_obj(hobject_t(sobject_t(object_t(meta_obj_buf), CEPH_NOSNAP))),
41       m_osr(coll_buf), m_next_object_id(0) {
42     }
43     ~coll_entry_t();
44
45     hobject_t *touch_obj(int id);
46     bool check_for_obj(int id);
47     hobject_t *get_obj(int id);
48     hobject_t *remove_obj(int id);
49     hobject_t *get_obj_at(int pos, int *key = NULL);
50     hobject_t *remove_obj_at(int pos, int *key = NULL);
51     hobject_t *replace_obj(int id, hobject_t *obj);
52     int get_random_obj_id(rngen_t& gen);
53
54    private:
55     hobject_t *get_obj(int id, bool remove);
56     hobject_t *get_obj_at(int pos, bool remove, int *key = NULL);
57   };
58
59  protected:
60   boost::shared_ptr<ObjectStore> m_store;
61   map<int, coll_entry_t*> m_collections;
62   vector<int> m_collections_ids;
63   int m_next_coll_nr;
64   int m_num_objs_per_coll;
65   int m_num_objects;
66
67   int m_max_in_flight;
68   std::atomic<int> m_in_flight = { 0 };
69   Mutex m_finished_lock;
70   Cond m_finished_cond;
71
72   void wait_for_ready() {
73     Mutex::Locker locker(m_finished_lock);
74     while ((m_max_in_flight > 0) && (m_in_flight >= m_max_in_flight))
75       m_finished_cond.Wait(m_finished_lock);
76   }
77
78   void wait_for_done() {
79     Mutex::Locker locker(m_finished_lock);
80     while (m_in_flight)
81       m_finished_cond.Wait(m_finished_lock);
82   }
83
84   void set_max_in_flight(int max) {
85     m_max_in_flight = max;
86   }
87   void set_num_objs_per_coll(int val) {
88     m_num_objs_per_coll = val;
89   }
90
91   coll_entry_t *get_coll(int key, bool erase = false);
92   coll_entry_t *get_coll_at(int pos, bool erase = false);
93   int get_next_pool_id() { return m_next_pool++; }
94
95  private:
96   static const int m_default_num_colls = 30;
97   // The pool ID used for collection creation, ID 0 is preserve for other tests
98   int m_next_pool;
99
100  public:
101   explicit TestObjectStoreState(ObjectStore *store) :
102     m_next_coll_nr(0), m_num_objs_per_coll(10), m_num_objects(0),
103     m_max_in_flight(0), m_finished_lock("Finished Lock"), m_next_pool(1) {
104     m_store.reset(store);
105   }
106   ~TestObjectStoreState() { 
107     map<int, coll_entry_t*>::iterator it = m_collections.begin();
108     while (it != m_collections.end()) {
109       if (it->second)
110         delete it->second;
111       m_collections.erase(it++);
112     }
113   }
114
115   void init(int colls, int objs);
116   void init() {
117     init(m_default_num_colls, 0);
118   }
119
120   int inc_in_flight() {
121     return ++m_in_flight;
122   }
123
124   int dec_in_flight() {
125     return --m_in_flight;
126   }
127
128   coll_entry_t *coll_create(int id);
129
130   class C_OnFinished: public Context {
131    protected:
132     TestObjectStoreState *m_state;
133
134    public:
135     explicit C_OnFinished(TestObjectStoreState *state) : m_state(state) { }
136
137     void finish(int r) override {
138       Mutex::Locker locker(m_state->m_finished_lock);
139       m_state->dec_in_flight();
140       m_state->m_finished_cond.Signal();
141
142     }
143   };
144 };
145
146 #endif /* TEST_OBJECTSTORE_STATE_H_ */