1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2012 New Dream Network
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.
13 #ifndef TEST_OBJECTSTORE_STATE_H_
14 #define TEST_OBJECTSTORE_STATE_H_
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>
23 typedef boost::mt11213b rngen_t;
25 class TestObjectStoreState {
31 ghobject_t m_meta_obj;
32 ObjectStore::Sequencer m_osr;
33 map<int, hobject_t*> m_objects;
36 coll_entry_t(int i, char *coll_buf, char *meta_obj_buf)
38 m_pgid(pg_t(i, 1), shard_id_t::NO_SHARD),
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) {
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);
55 hobject_t *get_obj(int id, bool remove);
56 hobject_t *get_obj_at(int pos, bool remove, int *key = NULL);
60 boost::shared_ptr<ObjectStore> m_store;
61 map<int, coll_entry_t*> m_collections;
62 vector<int> m_collections_ids;
64 int m_num_objs_per_coll;
68 std::atomic<int> m_in_flight = { 0 };
69 Mutex m_finished_lock;
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);
78 void wait_for_done() {
79 Mutex::Locker locker(m_finished_lock);
81 m_finished_cond.Wait(m_finished_lock);
84 void set_max_in_flight(int max) {
85 m_max_in_flight = max;
87 void set_num_objs_per_coll(int val) {
88 m_num_objs_per_coll = val;
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++; }
96 static const int m_default_num_colls = 30;
97 // The pool ID used for collection creation, ID 0 is preserve for other tests
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);
106 ~TestObjectStoreState() {
107 map<int, coll_entry_t*>::iterator it = m_collections.begin();
108 while (it != m_collections.end()) {
111 m_collections.erase(it++);
115 void init(int colls, int objs);
117 init(m_default_num_colls, 0);
120 int inc_in_flight() {
121 return ++m_in_flight;
124 int dec_in_flight() {
125 return --m_in_flight;
128 coll_entry_t *coll_create(int id);
130 class C_OnFinished: public Context {
132 TestObjectStoreState *m_state;
135 explicit C_OnFinished(TestObjectStoreState *state) : m_state(state) { }
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();
146 #endif /* TEST_OBJECTSTORE_STATE_H_ */